xref: /AOO41X/main/cui/source/dialogs/colorpicker.cxx (revision d3553c6b68aef63d45d1605bdabbc6fea7b9e42f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_cui.hxx"
24 
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
27 #include <com/sun/star/beans/XPropertyAccess.hpp>
28 #include <com/sun/star/lang/XInitialization.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/datatransfer/XTransferable.hpp>
31 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
32 #include <com/sun/star/awt/XWindow.hpp>
33 #include <cppuhelper/compbase4.hxx>
34 #include <comphelper/broadcasthelper.hxx>
35 #include <vcl/dialog.hxx>
36 #include <vcl/button.hxx>
37 #include <vcl/fixed.hxx>
38 #include <vcl/edit.hxx>
39 #include <vcl/field.hxx>
40 #include <vcl/bmpacc.hxx>
41 #include <vcl/decoview.hxx>
42 #include <vcl/sound.hxx>
43 #include <vcl/svapp.hxx>
44 #include <toolkit/helper/vclunohelper.hxx>
45 #include <sot/exchange.hxx>
46 #include <sot/formats.hxx>
47 #include <sax/tools/converter.hxx>
48 #include "dialmgr.hxx"
49 #include "colorpicker.hrc"
50 #include <cmath>
51 
52 using rtl::OUString;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::ui::dialogs;
56 using namespace ::com::sun::star::beans;
57 
58 namespace cui
59 {
60 const sal_uInt16 COLORMODE_RGB =  0x10;
61 const sal_uInt16 COLORMODE_HSV =  0x20;
62 const sal_uInt16 COLORMODE_CMYK = 0x40;
63 
64 const sal_uInt16 COLORCOMP_RED   = 0x10;
65 const sal_uInt16 COLORCOMP_GREEN = 0x11;
66 const sal_uInt16 COLORCOMP_BLUE  = 0x12;
67 
68 const sal_uInt16 COLORCOMP_HUE  = 0x20;
69 const sal_uInt16 COLORCOMP_SAT  = 0x21;
70 const sal_uInt16 COLORCOMP_BRI  = 0x22;
71 
72 const sal_uInt16 COLORCOMP_CYAN    = 0x40;
73 const sal_uInt16 COLORCOMP_YELLOW  = 0x41;
74 const sal_uInt16 COLORCOMP_MAGENTA = 0x42;
75 const sal_uInt16 COLORCOMP_KEY     = 0x43;
76 
77 // -----------------------------------------------------------------------
78 // color space conversion
79 // RGB  = 0 .. 1
80 // H    = 0 .. 360
81 // SV   = 0 .. 1
82 // CMYK = 0 .. 1
83 // -----------------------------------------------------------------------
84 
85 static void RGBtoHSV( double dR, double dG, double dB, double& dH, double& dS, double& dV )
86 {
87     // Brightness = max(R, G, B);
88     dV = std::max( dR, std::max( dG, dB ) );
89 
90     double cDelta = dV - std::min( dR, std::min( dG, dB ) );
91 
92     // Saturation = max - min / max
93     if( dV > 0 )
94         dS = cDelta / dV;
95     else
96         dS = 0.0;
97 
98     dH = 0.0;
99 
100     if( !basegfx::fTools::equalZero( dS ) )
101     {
102         if( basegfx::fTools::equal( dR, dV ) )
103         {
104             dH = ( dG - dB ) / cDelta;
105         }
106         else if( basegfx::fTools::equal( dG, dV ) )
107         {
108             dH = 2.0 + ( dB - dR ) / cDelta;
109         }
110         else if ( basegfx::fTools::equal( dB, dV ) )
111         {
112             dH = 4.0 + ( dR - dG ) / cDelta;
113         }
114         dH *= 60.0;
115 
116         if( dH < 0.0 )
117             dH += 360.0;
118     }
119 }
120 
121 static void HSVtoRGB(double dH, double dS, double dV, double& dR, double& dG, double& dB )
122 {
123     if( basegfx::fTools::equalZero( dS ) )
124     {
125         dR = dV;
126         dG = dV;
127         dB = dV;
128     }
129     else
130     {
131         if( basegfx::fTools::equal( dH, 360.0 ) )
132             dH = 0.0;
133         else
134             dH /= 60.0;
135 
136         sal_uInt16 n = (sal_uInt16) dH;
137         double f = dH - n;
138 
139         double a = dV * ( 1.0 - dS );
140         double b = dV * ( 1.0 - ( dS * f ) );
141         double c = dV * ( 1.0 - ( dS * ( 1.0 - f ) ) );
142 
143         switch( n )
144         {
145             case 0: dR = dV; dG = c;  dB = a;  break;
146             case 1: dR = b;  dG = dV; dB = a;  break;
147             case 2: dR = a;  dG = dV; dB = c;  break;
148             case 3: dR = a;  dG = b;  dB = dV; break;
149             case 4: dR = c;  dG = a;  dB = dV; break;
150             case 5: dR = dV; dG = a;  dB = b;  break;
151         }
152     }
153 }
154 
155 // -----------------------------------------------------------------------
156 
157 // CMYK values from 0 to 1
158 static void CMYKtoRGB( double fCyan, double fMagenta, double fYellow, double fKey, double& dR, double& dG, double& dB )
159 {
160     fCyan = (fCyan * ( 1.0 - fKey )) + fKey;
161     fMagenta = (fMagenta * ( 1.0 - fKey )) + fKey;
162     fYellow = (fYellow * ( 1.0 - fKey )) + fKey;
163 
164     dR = std::max( std::min( ( 1.0 - fCyan ), 1.0), 0.0 );
165     dG = std::max( std::min( ( 1.0 - fMagenta ), 1.0), 0.0 );
166     dB = std::max( std::min( ( 1.0 - fYellow ), 1.0), 0.0 );
167 }
168 
169 // -----------------------------------------------------------------------
170 
171 // CMY results from 0 to 1
172 static void RGBtoCMYK( double dR, double dG, double dB, double& fCyan, double& fMagenta, double& fYellow, double& fKey )
173 {
174     fCyan = 1 - dR;
175     fMagenta = 1 - dG;
176     fYellow = 1 - dB;
177 
178     //CMYK and CMY values from 0 to 1
179     fKey = 1.0;
180     if( fCyan < fKey ) fKey = fCyan;
181     if( fMagenta < fKey ) fKey = fMagenta;
182     if( fYellow < fKey ) fKey = fYellow;
183 
184     if( basegfx::fTools::equal( fKey, 1.0 ) )
185     {
186         //Black
187        fCyan = 0.0;
188        fMagenta = 0.0;
189        fYellow = 0.0;
190     }
191     else
192     {
193        fCyan = ( fCyan - fKey ) / ( 1.0 - fKey );
194        fMagenta = ( fMagenta - fKey ) / ( 1.0 - fKey );
195        fYellow = ( fYellow - fKey ) / ( 1.0 - fKey );
196     }
197 }
198 
199 // ====================================================================
200 
201 class HexColorControl : public Edit
202 {
203 public:
204     HexColorControl( Window* pParent, const ResId& rResId );
205 
206     virtual long PreNotify( NotifyEvent& rNEvt );
207     virtual void Paste();
208 
209     void SetColor( sal_Int32 nColor );
210     sal_Int32 GetColor();
211 
212 private:
213     bool ImplProcessKeyInput( const KeyEvent& rKEv );
214 };
215 
216 HexColorControl::HexColorControl( Window* pParent, const ResId& rResId )
217 : Edit( pParent, rResId )
218 {
219     SetMaxTextLen( 6 );
220 }
221 
222 // -----------------------------------------------------------------------
223 
224 void HexColorControl::SetColor( sal_Int32 nColor )
225 {
226     ::rtl::OUStringBuffer aBuffer;
227     sax::Converter::convertColor( aBuffer, nColor );
228     SetText( aBuffer.makeStringAndClear().copy(1) );
229 }
230 
231 // -----------------------------------------------------------------------
232 
233 sal_Int32 HexColorControl::GetColor()
234 {
235     sal_Int32 nColor = -1;
236 
237     OUString aStr( RTL_CONSTASCII_USTRINGPARAM( "#" ) );
238     aStr += GetText();
239     sal_Int32 nLen = aStr.getLength();
240     if( nLen < 7 )
241     {
242         static const sal_Char* pNullStr = "000000";
243         aStr += OUString::createFromAscii( &pNullStr[nLen-1] );
244     }
245 
246     sax::Converter::convertColor( nColor, aStr );
247 
248     if( nColor == -1 )
249         SetControlBackground( Color( COL_RED ) );
250     else
251         SetControlBackground();
252 
253     return nColor;
254 }
255 
256 // -----------------------------------------------------------------------
257 
258 long HexColorControl::PreNotify( NotifyEvent& rNEvt )
259 {
260     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
261     {
262         if ( ImplProcessKeyInput( *rNEvt.GetKeyEvent() ) )
263             return 1;
264     }
265 
266     return Edit::PreNotify( rNEvt );
267 }
268 
269 // -----------------------------------------------------------------------
270 
271 void HexColorControl::Paste()
272 {
273     ::com::sun::star::uno::Reference<com::sun::star::datatransfer::clipboard::XClipboard> aClipboard(GetClipboard());
274     if ( aClipboard.is() )
275     {
276         ::com::sun::star::uno::Reference< ::com::sun::star::datatransfer::XTransferable > xDataObj;
277 
278         const sal_uInt32 nRef = Application::ReleaseSolarMutex();
279 
280         try
281         {
282             xDataObj = aClipboard->getContents();
283         }
284         catch( const ::com::sun::star::uno::Exception& )
285         {
286         }
287 
288         Application::AcquireSolarMutex( nRef );
289 
290         if ( xDataObj.is() )
291         {
292             ::com::sun::star::datatransfer::DataFlavor aFlavor;
293             SotExchange::GetFormatDataFlavor( SOT_FORMAT_STRING, aFlavor );
294             try
295             {
296                 ::com::sun::star::uno::Any aData = xDataObj->getTransferData( aFlavor );
297                 ::rtl::OUString aText;
298                 aData >>= aText;
299 
300                 if( aText.getLength() && aText.matchAsciiL( "#", 1, 0 ) )
301                     aText = aText.copy(1);
302 
303                 if( aText.getLength() > 6 )
304                     aText = aText.copy( 0, 6 );
305 
306                 SetText( aText );
307             }
308             catch( const ::com::sun::star::uno::Exception& )
309             {
310             }
311         }
312     }
313 }
314 
315 // -----------------------------------------------------------------------
316 
317 bool HexColorControl::ImplProcessKeyInput( const KeyEvent& rKEv )
318 {
319     const KeyCode& rKeyCode = rKEv.GetKeyCode();
320 
321     if( rKeyCode.GetGroup() == KEYGROUP_ALPHA && !rKeyCode.IsMod1() && !rKeyCode.IsMod2() )
322     {
323         if( (rKeyCode.GetCode() < KEY_A) || (rKeyCode.GetCode() > KEY_F) )
324         {
325             Sound::Beep();
326             return true;
327         }
328     }
329     else if( rKeyCode.GetGroup() == KEYGROUP_NUM )
330     {
331         if( rKeyCode.IsShift() )
332         {
333             Sound::Beep();
334             return true;
335         }
336     }
337     return false;
338 }
339 
340 // ====================================================================
341 
342 class ColorPreviewControl : public Control
343 {
344 public:
345     ColorPreviewControl( Window* pParent, const ResId& rResId );
346 
347     virtual void        Paint( const Rectangle& rRect );
348 
349     void SetColor( const Color& rColor );
350 private:
351     Color maColor;
352 };
353 
354 // -----------------------------------------------------------------------
355 
356 ColorPreviewControl::ColorPreviewControl( Window* pParent, const ResId& rResId )
357 : Control( pParent, rResId )
358 {
359     SetFillColor( maColor );
360     SetLineColor( maColor );
361 }
362 
363 // -----------------------------------------------------------------------
364 
365 void ColorPreviewControl::SetColor( const Color& rCol )
366 {
367     if( rCol != maColor )
368     {
369         maColor = rCol;
370         SetFillColor( maColor );
371         SetLineColor( maColor );
372         Invalidate();
373     }
374 }
375 
376 // -----------------------------------------------------------------------
377 
378 void ColorPreviewControl::Paint( const Rectangle& rRect )
379 {
380     DrawRect( rRect );
381 }
382 
383 // ====================================================================
384 
385 enum ColorMode { HUE, SATURATION, BRIGHTNESS, RED, GREEN, BLUE };
386 const ColorMode DefaultMode = HUE;
387 
388 class ColorFieldControl : public Control
389 {
390 public:
391     ColorFieldControl( Window* pParent, const ResId& rResId );
392     ~ColorFieldControl();
393 
394     virtual void        MouseMove( const MouseEvent& rMEvt );
395     virtual void        MouseButtonDown( const MouseEvent& rMEvt );
396     virtual void        MouseButtonUp( const MouseEvent& rMEvt );
397     virtual void        KeyInput( const KeyEvent& rKEvt );
398     virtual void        Paint( const Rectangle& rRect );
399     virtual void        Resize();
400 
401     void                UpdateBitmap();
402     void                ShowPosition( const Point& rPos, bool bUpdate );
403     void                UpdatePosition();
404     void                Modify();
405 
406     void                SetValues( Color aColor, ColorMode eMode, double x, double y );
407     double              GetX();
408     double              GetY();
409 
410     void                KeyMove( int dx, int dy );
411 
412     void                SetModifyHdl( Link& rLink ) { maModifyHdl = rLink; }
413 
414 private:
415     Link maModifyHdl;
416     ColorMode meMode;
417     Color maColor;
418     double mdX;
419     double mdY;
420     Point maPosition;
421     Bitmap* mpBitmap;
422     std::vector< sal_uInt8 > maRGB_Horiz;
423     std::vector< sal_uInt16 > maGrad_Horiz;
424     std::vector< sal_uInt16 > maPercent_Horiz;
425     std::vector< sal_uInt8 > maRGB_Vert;
426     std::vector< sal_uInt16 > maPercent_Vert;
427     bool mbInit;
428 };
429 
430 // -----------------------------------------------------------------------
431 
432 ColorFieldControl::ColorFieldControl( Window* pParent, const ResId& rResId )
433 : Control( pParent, rResId )
434 , meMode( DefaultMode )
435 , mdX( -1.0 )
436 , mdY( -1.0 )
437 , mpBitmap( 0 )
438 , mbInit( false )
439 {
440     SetControlBackground();
441 }
442 
443 // -----------------------------------------------------------------------
444 
445 ColorFieldControl::~ColorFieldControl()
446 {
447     delete mpBitmap;
448 }
449 
450 // -----------------------------------------------------------------------
451 
452 void ColorFieldControl::UpdateBitmap()
453 {
454     const Size aSize( GetOutputSizePixel() );
455 
456     if( mpBitmap && mpBitmap->GetSizePixel() != aSize )
457         delete mpBitmap, mpBitmap = NULL;
458 
459     const sal_Int32 nWidth = aSize.Width();
460     const sal_Int32 nHeight = aSize.Height();
461 
462     if( !mpBitmap )
463     {
464         mpBitmap = new Bitmap( aSize, 24 );
465 
466         maRGB_Horiz.resize( nWidth );
467         maGrad_Horiz.resize( nWidth );
468         maPercent_Horiz.resize( nWidth );
469 
470         sal_uInt8* pRGB = &(*maRGB_Horiz.begin());
471         sal_uInt16* pGrad = &(*maGrad_Horiz.begin());
472         sal_uInt16* pPercent = &(*maPercent_Horiz.begin());
473 
474         for( sal_Int32 x = 0; x < nWidth; x++ )
475         {
476             *pRGB++ = static_cast< sal_uInt8 >( (x * 256) / nWidth );
477             *pGrad++ = static_cast< sal_uInt16 >( (x * 359) / nWidth );
478             *pPercent++ = static_cast< sal_uInt16 >( (x * 100) / nWidth );
479         }
480 
481         maRGB_Vert.resize( nHeight );
482         maPercent_Vert.resize( nHeight );
483 
484         pRGB = &(*maRGB_Vert.begin());
485         pPercent = &(*maPercent_Vert.begin());
486 
487         sal_Int32 y = nHeight;
488         while( y-- )
489         {
490             *pRGB++ = static_cast< sal_uInt8 >( (y * 256) / nHeight );
491             *pPercent++ = static_cast< sal_uInt16 >( (y * 100) / nHeight );
492         }
493     }
494 
495     sal_uInt8* pRGB_Horiz = &(*maRGB_Horiz.begin());
496     sal_uInt16* pGrad_Horiz = &(*maGrad_Horiz.begin());
497     sal_uInt16* pPercent_Horiz = &(*maPercent_Horiz.begin());
498     sal_uInt8* pRGB_Vert = &(*maRGB_Vert.begin());
499     sal_uInt16* pPercent_Vert = &(*maPercent_Vert.begin());
500 
501     BitmapWriteAccess* pWriteAccess = mpBitmap->AcquireWriteAccess();
502     if( pWriteAccess )
503     {
504         BitmapColor aBitmapColor( maColor );
505 
506         sal_uInt16 nHue, nSat, nBri;
507         maColor.RGBtoHSB( nHue, nSat, nBri );
508 
509         // this has been unlooped for performance reason, please do not merge back!
510 
511         sal_uInt16 y = nHeight,x;
512 
513         switch( meMode )
514         {
515         case HUE:
516             while( y-- )
517             {
518                 nBri = pPercent_Vert[y];
519                 x = nWidth;
520                 while( x-- )
521                 {
522                     nSat = pPercent_Horiz[x];
523                     pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
524                 }
525             }
526             break;
527         case SATURATION:
528             while( y-- )
529             {
530                 nBri = pPercent_Vert[y];
531                 x = nWidth;
532                 while( x-- )
533                 {
534                     nHue = pGrad_Horiz[x];
535                     pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
536                 }
537             }
538             break;
539         case BRIGHTNESS:
540             while( y-- )
541             {
542                 nSat = pPercent_Vert[y];
543                 x = nWidth;
544                 while( x-- )
545                 {
546                     nHue = pGrad_Horiz[x];
547                     pWriteAccess->SetPixel( y, x, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
548                 }
549             }
550             break;
551         case RED:
552             while( y-- )
553             {
554                 aBitmapColor.SetGreen( pRGB_Vert[y] );
555                 x = nWidth;
556                 while( x-- )
557                 {
558                     aBitmapColor.SetBlue( pRGB_Horiz[x] );
559                     pWriteAccess->SetPixel( y, x, aBitmapColor );
560                 }
561             }
562             break;
563         case GREEN:
564             while( y-- )
565             {
566                 aBitmapColor.SetRed( pRGB_Vert[y] );
567                 x = nWidth;
568                 while( x-- )
569                 {
570                     aBitmapColor.SetBlue( pRGB_Horiz[x] );
571                     pWriteAccess->SetPixel( y, x, aBitmapColor );
572                 }
573             }
574             break;
575         case BLUE:
576             while( y-- )
577             {
578                 aBitmapColor.SetGreen( pRGB_Vert[y] );
579                 x = nWidth;
580                 while( x-- )
581                 {
582                     aBitmapColor.SetRed( pRGB_Horiz[x] );
583                     pWriteAccess->SetPixel( y, x, aBitmapColor );
584                 }
585             }
586             break;
587         }
588 
589         mpBitmap->ReleaseAccess( pWriteAccess );
590     }
591 }
592 
593 // -----------------------------------------------------------------------
594 
595 void ColorFieldControl::ShowPosition( const Point& rPos, bool bUpdate )
596 {
597     if( !mpBitmap )
598     {
599         UpdateBitmap();
600         Invalidate();
601     }
602 
603     const Size aSize( mpBitmap->GetSizePixel() );
604 
605     long nX = rPos.X();
606     long nY = rPos.Y();
607     if( nX < 0L )
608         nX = 0L;
609     else if( nX >= aSize.Width() )
610         nX = aSize.Width() - 1L;
611 
612     if( nY < 0L )
613         nY= 0L;
614     else if( nY >= aSize.Height() )
615         nY = aSize.Height() - 1L;
616 
617     Point aPos = maPosition;
618     maPosition.X() = nX - 5;
619     maPosition.Y() = nY - 5;
620     Invalidate( Rectangle( aPos, Size( 11, 11) ) );
621     Invalidate( Rectangle( maPosition, Size( 11, 11) ) );
622 
623     if( bUpdate )
624     {
625         mdX = (double)nX / (double)(aSize.Width()-1);
626         mdY = (double)(aSize.Height()-1-nY) / (double)(aSize.Height()-1);
627 
628         BitmapReadAccess* pReadAccess = mpBitmap->AcquireReadAccess();
629         if( pReadAccess != NULL )
630         {
631             // mpBitmap always has a bit count of 24 => use of GetPixel(...) is safe
632             maColor = pReadAccess->GetPixel( nY, nX );
633             mpBitmap->ReleaseAccess( pReadAccess );
634             pReadAccess = NULL;
635         }
636     }
637 }
638 // -----------------------------------------------------------------------
639 
640 void ColorFieldControl::MouseMove( const MouseEvent& rMEvt )
641 {
642     if( rMEvt.IsLeft() )
643     {
644         ShowPosition( rMEvt.GetPosPixel(), true );
645         Modify();
646     }
647 }
648 
649 // -----------------------------------------------------------------------
650 void ColorFieldControl::MouseButtonDown( const MouseEvent& rMEvt )
651 {
652     if( rMEvt.IsLeft() && !rMEvt.IsShift() )
653     {
654         CaptureMouse();
655         ShowPosition( rMEvt.GetPosPixel(), true );
656         Modify();
657     }
658 }
659 
660 // -----------------------------------------------------------------------
661 void ColorFieldControl::MouseButtonUp( const MouseEvent& )
662 {
663     if( IsMouseCaptured() )
664         ReleaseMouse();
665 }
666 
667 // -----------------------------------------------------------------------
668 
669 void ColorFieldControl::KeyMove( int dx, int dy )
670 {
671     Size aSize( GetOutputSizePixel() );
672     Point aPos( mdX * aSize.Width(), (1.0 - mdY) * aSize.Height() );
673     aPos.X() += dx;
674     aPos.Y() += dy;
675     if( aPos.X() < 0 )
676         aPos.X() += aSize.Width();
677     else if( aPos.X() >= aSize.Width() )
678         aPos.X() -= aSize.Width();
679 
680     if( aPos.Y() < 0 )
681         aPos.Y() += aSize.Height();
682     else if( aPos.Y() >= aSize.Height() )
683         aPos.Y() -= aSize.Height();
684 
685     ShowPosition( aPos, true );
686     Modify();
687 }
688 
689 // -----------------------------------------------------------------------
690 
691 void ColorFieldControl::KeyInput( const KeyEvent& rKEvt )
692 {
693     bool   bShift = rKEvt.GetKeyCode().IsShift();
694     bool   bCtrl = rKEvt.GetKeyCode().IsMod1();
695     bool   bAlt = rKEvt.GetKeyCode().IsMod2();
696 
697     if ( !bAlt && !bShift )
698     {
699         switch( rKEvt.GetKeyCode().GetCode() )
700         {
701         case KEY_DOWN:      KeyMove(  0, bCtrl ?  5  :  1 ); return;
702         case KEY_UP:        KeyMove(  0, bCtrl ? -5  : -1 ); return;
703         case KEY_LEFT:      KeyMove( bCtrl ? -5  : -1,  0 ); return;
704         case KEY_RIGHT:     KeyMove( bCtrl ?  5  :  1,  0 ); return;
705         }
706     }
707     Control::KeyInput( rKEvt );
708 }
709 
710 // -----------------------------------------------------------------------
711 
712 void ColorFieldControl::Paint( const Rectangle& rRect )
713 {
714     if( !mpBitmap )
715         UpdateBitmap();
716 
717     Bitmap aOutputBitmap( *mpBitmap );
718 
719     if( GetBitCount() <= 8 )
720         aOutputBitmap.Dither();
721 
722     DrawBitmap( rRect.TopLeft(), rRect.GetSize(), rRect.TopLeft(), rRect.GetSize(), aOutputBitmap );
723 
724     // draw circle around current color
725     if( maColor.IsDark() )
726         SetLineColor( COL_WHITE );
727     else
728         SetLineColor( COL_BLACK );
729 
730     SetFillColor();
731 
732     DrawEllipse( Rectangle( maPosition, Size( 11, 11) ) );
733 }
734 
735 // -----------------------------------------------------------------------
736 
737 void ColorFieldControl::Resize()
738 {
739     UpdateBitmap();
740     Control::Resize();
741 }
742 
743 // -----------------------------------------------------------------------
744 
745 void ColorFieldControl::Modify()
746 {
747     maModifyHdl.Call( this );
748 }
749 
750 // -----------------------------------------------------------------------
751 
752 void ColorFieldControl::SetValues( Color aColor, ColorMode eMode, double x, double y )
753 {
754     bool bUpdateBitmap = (maColor!= aColor) || (meMode != eMode);
755     if( bUpdateBitmap || (mdX != x) || (mdY != y) )
756     {
757         maColor = aColor;
758         meMode = eMode;
759         mdX = x;
760         mdY = y;
761 
762         if( bUpdateBitmap )
763             UpdateBitmap();
764         UpdatePosition();
765         if( bUpdateBitmap )
766             Invalidate();
767     }
768 }
769 
770 // -----------------------------------------------------------------------
771 
772 double ColorFieldControl::GetX()
773 {
774     return mdX;
775 }
776 
777 // -----------------------------------------------------------------------
778 
779 double ColorFieldControl::GetY()
780 {
781     return mdY;
782 }
783 
784 // -----------------------------------------------------------------------
785 
786 void ColorFieldControl::UpdatePosition()
787 {
788     Size aSize( GetOutputSizePixel() );
789     ShowPosition( Point( mdX * aSize.Width(), (1.0 - mdY) * aSize.Height() ), false );
790 }
791 
792 // ====================================================================
793 
794 class ColorSliderControl : public Control
795 {
796 public:
797     ColorSliderControl( Window* pParent, const ResId& rResId );
798     ~ColorSliderControl();
799 
800     virtual void        MouseMove( const MouseEvent& rMEvt );
801     virtual void        MouseButtonDown( const MouseEvent& rMEvt );
802     virtual void        MouseButtonUp( const MouseEvent& rMEvt );
803     virtual void        KeyInput( const KeyEvent& rKEvt );
804     virtual void        Paint( const Rectangle& rRect );
805     virtual void        Resize();
806 
807     void                UpdateBitmap();
808     void                ChangePosition( long nY );
809     void                Modify();
810 
811     void SetValue( const Color& rColor, ColorMode eMode, double dValue );
812     double GetValue() const { return mdValue; }
813 
814     void                KeyMove( int dy );
815 
816     void SetModifyHdl( Link& rLink ) { maModifyHdl = rLink; }
817 
818     sal_Int16 GetLevel() const { return mnLevel; }
819 
820 private:
821     Link maModifyHdl;
822     Color maColor;
823     ColorMode meMode;
824     Bitmap* mpBitmap;
825     sal_Int16 mnLevel;
826     double mdValue;
827 };
828 
829 // -----------------------------------------------------------------------
830 
831 ColorSliderControl::ColorSliderControl( Window* pParent, const ResId& rResId )
832 : Control( pParent, rResId )
833 , meMode( DefaultMode )
834 , mpBitmap( 0 )
835 , mnLevel( 0 )
836 , mdValue( -1.0 )
837 {
838     SetControlBackground();
839 }
840 
841 // -----------------------------------------------------------------------
842 
843 ColorSliderControl::~ColorSliderControl()
844 {
845     delete mpBitmap;
846 }
847 
848 // -----------------------------------------------------------------------
849 
850 void ColorSliderControl::UpdateBitmap()
851 {
852     Size aSize( 1, GetOutputSizePixel().Height() );
853 
854     if( mpBitmap && mpBitmap->GetSizePixel() != aSize )
855         delete mpBitmap, mpBitmap = NULL;
856 
857     if( !mpBitmap )
858         mpBitmap = new Bitmap( aSize, 24 );
859 
860     BitmapWriteAccess* pWriteAccess = mpBitmap->AcquireWriteAccess();
861 
862     if( pWriteAccess )
863     {
864         const long nY = aSize.Height()-1;
865 
866         BitmapColor aBitmapColor( maColor );
867 
868         sal_uInt16 nHue, nSat, nBri;
869         maColor.RGBtoHSB( nHue, nSat, nBri );
870 
871         // this has been unlooped for performance reason, please do not merge back!
872 
873         switch( meMode )
874         {
875         case HUE:
876             nSat = 100;
877             nBri = 100;
878             for( long y = 0; y <= nY; y++ )
879             {
880                 nHue = static_cast< sal_uInt16 >( (359 * y) / nY );
881                 aBitmapColor = BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) );
882                 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
883             }
884             break;
885 
886         case SATURATION:
887             nBri = std::max( (sal_uInt16)32, nBri );
888             for( long y = 0; y <= nY; y++ )
889             {
890                 nSat = static_cast< sal_uInt16 >( (100 * y) / nY );
891                 pWriteAccess->SetPixel( nY-y, 0, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
892             }
893             break;
894 
895         case BRIGHTNESS:
896             for( long y = 0; y <= nY; y++ )
897             {
898                 nBri = static_cast< sal_uInt16 >( (100 * y) / nY );
899                 pWriteAccess->SetPixel( nY-y, 0, BitmapColor( Color( Color::HSBtoRGB( nHue, nSat, nBri ) ) ) );
900             }
901             break;
902 
903         case RED:
904             for( long y = 0; y <= nY; y++ )
905             {
906                 aBitmapColor.SetRed( sal_uInt8( ((long)255 * y) / nY ) );
907                 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
908             }
909             break;
910 
911         case GREEN:
912             for( long y = 0; y <= nY; y++ )
913             {
914                 aBitmapColor.SetGreen( sal_uInt8( ((long)255 * y) / nY ) );
915                 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
916             }
917             break;
918 
919         case BLUE:
920             for( long y = 0; y <= nY; y++ )
921             {
922                 aBitmapColor.SetBlue( sal_uInt8( ((long)255 * y) / nY ) );
923                 pWriteAccess->SetPixel( nY-y, 0, aBitmapColor );
924             }
925             break;
926         }
927 
928         mpBitmap->ReleaseAccess( pWriteAccess );
929     }
930 }
931 
932 // -----------------------------------------------------------------------
933 
934 void ColorSliderControl::ChangePosition( long nY )
935 {
936     const long nHeight = GetOutputSizePixel().Height() - 1;
937 
938     if( nY < 0L )
939         nY = 0;
940     else if( nY > nHeight )
941         nY = nHeight;
942 
943     mnLevel = nY;
944     mdValue = ((double)(nHeight - nY)) / (double)nHeight;
945 }
946 
947 // -----------------------------------------------------------------------
948 
949 void ColorSliderControl::MouseMove( const MouseEvent& rMEvt )
950 {
951     if( rMEvt.IsLeft() )
952     {
953         ChangePosition( rMEvt.GetPosPixel().Y() );
954         Modify();
955     }
956 }
957 
958 // -----------------------------------------------------------------------
959 void ColorSliderControl::MouseButtonDown( const MouseEvent& rMEvt )
960 {
961     if( rMEvt.IsLeft() && !rMEvt.IsShift() )
962     {
963         CaptureMouse();
964         ChangePosition( rMEvt.GetPosPixel().Y() );
965         Modify();
966     }
967 }
968 
969 // -----------------------------------------------------------------------
970 void ColorSliderControl::MouseButtonUp( const MouseEvent& )
971 {
972     if( IsMouseCaptured() )
973         ReleaseMouse();
974 }
975 
976 // -----------------------------------------------------------------------
977 
978 void ColorSliderControl::KeyMove( int dy )
979 {
980     ChangePosition( mnLevel + dy );
981     Modify();
982 }
983 
984 // -----------------------------------------------------------------------
985 
986 void ColorSliderControl::KeyInput( const KeyEvent& rKEvt )
987 {
988     if ( !rKEvt.GetKeyCode().IsMod2() && !rKEvt.GetKeyCode().IsShift() )
989     {
990         switch( rKEvt.GetKeyCode().GetCode() )
991         {
992         case KEY_DOWN:      KeyMove(  rKEvt.GetKeyCode().IsMod1() ?  5 :  1 ); return;
993         case KEY_UP:        KeyMove(  rKEvt.GetKeyCode().IsMod1() ? -5 : -1 ); return;
994         }
995     }
996 
997     Control::KeyInput( rKEvt );
998 }
999 // -----------------------------------------------------------------------
1000 
1001 void ColorSliderControl::Paint( const Rectangle& /*rRect*/ )
1002 {
1003     if( !mpBitmap )
1004         UpdateBitmap();
1005 
1006     const Size aSize( GetOutputSizePixel() );
1007 
1008     Bitmap aOutputBitmap( *mpBitmap );
1009 
1010     if( GetBitCount() <= 8 )
1011         aOutputBitmap.Dither();
1012 
1013     Point aPos;
1014     int x = aSize.Width();
1015     while( x-- )
1016     {
1017         DrawBitmap( aPos, aOutputBitmap );
1018         aPos.X() += 1;
1019     }
1020 }
1021 
1022 // -----------------------------------------------------------------------
1023 
1024 void ColorSliderControl::Resize()
1025 {
1026     UpdateBitmap();
1027     Control::Resize();
1028 }
1029 
1030 // -----------------------------------------------------------------------
1031 
1032 void ColorSliderControl::Modify()
1033 {
1034     maModifyHdl.Call( this );
1035 }
1036 
1037 // -----------------------------------------------------------------------
1038 
1039 void ColorSliderControl::SetValue( const Color& rColor, ColorMode eMode, double dValue )
1040 {
1041     bool bUpdateBitmap = (rColor != maColor) || (eMode != meMode);
1042     if( bUpdateBitmap || (mdValue != dValue))
1043     {
1044         maColor = rColor;
1045         mdValue = dValue;
1046         mnLevel = (1.0-dValue) * GetOutputSizePixel().Height();
1047         meMode = eMode;
1048         if( bUpdateBitmap )
1049             UpdateBitmap();
1050         Invalidate();
1051     }
1052 }
1053 
1054 // ====================================================================
1055 
1056 const sal_uInt16 UPDATE_RGB = 0x01;
1057 const sal_uInt16 UPDATE_CMYK = 0x02;
1058 const sal_uInt16 UPDATE_HSB = 0x04;
1059 const sal_uInt16 UPDATE_COLORCHOOSER = 0x08;
1060 const sal_uInt16 UPDATE_COLORSLIDER = 0x10;
1061 const sal_uInt16 UPDATE_HEX = 0x20;
1062 const sal_uInt16 UPDATE_ALL = 0xff;
1063 
1064 class ColorPickerDialog : public ModalDialog
1065 {
1066 public:
1067     ColorPickerDialog( Window* pParent, sal_Int32 nColor, sal_Int16 nMode );
1068 
1069     void update_color( sal_uInt16 n = UPDATE_ALL );
1070 
1071     DECL_LINK( ColorModifyHdl, void * );
1072     DECL_LINK( ModeModifyHdl, void * );
1073 
1074     sal_Int32 GetColor() const;
1075 
1076     void setColorComponent( sal_uInt16 nComp, double dValue );
1077 
1078 private:
1079     Color maPreviousColor;
1080     sal_Int16 mnDialogMode;
1081     ColorMode meMode;
1082 
1083     double mdRed, mdGreen, mdBlue;
1084     double mdHue, mdSat, mdBri;
1085     double mdCyan, mdMagenta, mdYellow, mdKey;
1086 
1087 private:
1088     ColorFieldControl  maColorField;
1089     ColorSliderControl maColorSlider;
1090     ColorPreviewControl maColorPreview;
1091     ColorPreviewControl maColorPrevious;
1092 
1093     FixedImage maFISliderLeft;
1094     FixedImage maFISliderRight;
1095     Image maSliderImage;
1096 
1097 #if 0
1098     ImageButton maBtnPicker;
1099 #endif
1100 
1101     FixedLine maFLRGB;
1102 
1103     RadioButton maRBRed;
1104     RadioButton maRBGreen;
1105     RadioButton maRBBlue;
1106     RadioButton maRBHue;
1107     RadioButton maRBSaturation;
1108     RadioButton maRBBrightness;
1109 
1110     FixedText maFTRed;
1111     MetricField maMFRed;
1112     FixedText maFTGreen;
1113     MetricField maMFGreen;
1114     FixedText maFTBlue;
1115     MetricField maMFBlue;
1116     FixedText maFTHex;
1117     HexColorControl maEDHex;
1118 
1119     FixedLine maFLHSB;
1120     FixedText maFTHue;
1121     MetricField maMFHue;
1122     FixedText maFTSaturation;
1123     MetricField maMFSaturation;
1124     FixedText maFTBrightness;
1125     MetricField maMFBrightness;
1126 
1127     FixedLine maFLCMYK;
1128     FixedText maFTCyan;
1129     MetricField maMFCyan;
1130     FixedText maFTMagenta;
1131     MetricField maMFMagenta;
1132     FixedText maFTYellow;
1133     MetricField maMFYellow;
1134     FixedText maFTKey;
1135     MetricField maMFKey;
1136 
1137     FixedLine maFLBottmLine;
1138     HelpButton maBTNHelp;
1139     OKButton maBTNOk;
1140     CancelButton maBTNCancel;
1141 };
1142 
1143 // --------------------------------------------------------------------
1144 
1145 ColorPickerDialog::ColorPickerDialog( Window* pParent, sal_Int32 nColor, sal_Int16 nMode )
1146 : ModalDialog( pParent, CUI_RES( RID_CUI_DIALOG_COLORPICKER ) )
1147 , maPreviousColor( nColor )
1148 , mnDialogMode( nMode )
1149 , meMode( DefaultMode )
1150 , maColorField( this, CUI_RES( CT_COLORFIELD ) )
1151 , maColorSlider( this, CUI_RES( CT_COLORSLIDER ) )
1152 , maColorPreview( this, CUI_RES( CT_PREVIEW ) )
1153 , maColorPrevious( this, CUI_RES( CT_PREVIOUS ) )
1154 , maFISliderLeft(  this, CUI_RES( CT_LEFT_SLIDER ) )
1155 , maFISliderRight( this, CUI_RES( CT_RIGHT_SLIDER ) )
1156 , maSliderImage( CUI_RES( CT_SLIDERIMG ) )
1157 #if 0
1158 , maBtnPicker( this, CUI_RES( PB_PICKER ) )
1159 #endif
1160 , maFLRGB( this, CUI_RES( FL_RGB ) )
1161 , maRBRed( this, CUI_RES( CT_RED ) )
1162 , maRBGreen( this, CUI_RES( CT_GREEN ) )
1163 , maRBBlue( this, CUI_RES( CT_BLUE ) )
1164 , maRBHue( this, CUI_RES( CT_HUE ) )
1165 , maRBSaturation( this, CUI_RES( CT_SATURATION ) )
1166 , maRBBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1167 , maFTRed( this, CUI_RES( CT_RED ) )
1168 , maMFRed( this, CUI_RES( CT_RED ) )
1169 , maFTGreen( this, CUI_RES( CT_GREEN ) )
1170 , maMFGreen( this, CUI_RES( CT_GREEN ) )
1171 , maFTBlue( this, CUI_RES( CT_BLUE ) )
1172 , maMFBlue( this, CUI_RES( CT_BLUE ) )
1173 , maFTHex( this, CUI_RES( CT_HEX ) )
1174 , maEDHex( this, CUI_RES( CT_HEX ) )
1175 , maFLHSB( this, CUI_RES( FL_HSB ) )
1176 , maFTHue( this, CUI_RES( CT_HUE ) )
1177 , maMFHue( this, CUI_RES( CT_HUE ) )
1178 , maFTSaturation( this, CUI_RES( CT_SATURATION ) )
1179 , maMFSaturation( this, CUI_RES( CT_SATURATION ) )
1180 , maFTBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1181 , maMFBrightness( this, CUI_RES( CT_BRIGHTNESS ) )
1182 , maFLCMYK( this, CUI_RES( FL_CMYK ) )
1183 , maFTCyan( this, CUI_RES( CT_CYAN ) )
1184 , maMFCyan( this, CUI_RES( CT_CYAN ) )
1185 , maFTMagenta( this, CUI_RES( CT_MAGENTA ) )
1186 , maMFMagenta( this, CUI_RES( CT_MAGENTA ) )
1187 , maFTYellow( this, CUI_RES( CT_YELLOW ) )
1188 , maMFYellow( this, CUI_RES( CT_YELLOW ) )
1189 , maFTKey( this, CUI_RES( CT_KEY ) )
1190 , maMFKey( this, CUI_RES( CT_KEY ) )
1191 
1192 , maFLBottmLine( this, CUI_RES( FT_BOTTOMLINE ) )
1193 , maBTNHelp( this, CUI_RES( BTN_HELP ) )
1194 , maBTNOk( this, CUI_RES( BTN_OK ) )
1195 , maBTNCancel( this, CUI_RES( BTN_CANCEL ) )
1196 {
1197     FreeResource();
1198 
1199     String sUnitText;
1200     sUnitText.Append( ' ' );
1201     sUnitText.Append( (sal_Unicode) 0xb0 );
1202 
1203     maMFHue.SetCustomUnitText( sUnitText );
1204 
1205     Link aLink( LINK( this, ColorPickerDialog, ColorModifyHdl ) );
1206     maColorField.SetModifyHdl( aLink );
1207     maColorSlider.SetModifyHdl( aLink );
1208 
1209     maMFRed.SetModifyHdl( aLink );
1210     maMFGreen.SetModifyHdl( aLink );
1211     maMFBlue.SetModifyHdl( aLink );
1212 
1213     maMFCyan.SetModifyHdl( aLink );
1214     maMFMagenta.SetModifyHdl( aLink );
1215     maMFYellow.SetModifyHdl( aLink );
1216     maMFKey.SetModifyHdl( aLink );
1217 
1218     maMFHue.SetModifyHdl( aLink );
1219     maMFSaturation.SetModifyHdl( aLink );
1220     maMFBrightness.SetModifyHdl( aLink );
1221 
1222     maEDHex.SetModifyHdl( aLink );
1223 
1224     aLink = LINK( this, ColorPickerDialog, ModeModifyHdl );
1225     maRBRed.SetToggleHdl( aLink );
1226     maRBGreen.SetToggleHdl( aLink );
1227     maRBBlue.SetToggleHdl( aLink );
1228     maRBHue.SetToggleHdl( aLink );
1229     maRBSaturation.SetToggleHdl( aLink );
1230     maRBBrightness.SetToggleHdl( aLink );
1231 
1232     Image aSliderImage( maSliderImage );
1233 
1234     maFISliderLeft.SetImage( aSliderImage );
1235 
1236     BitmapEx aTmpBmp( maSliderImage.GetBitmapEx() );
1237     aTmpBmp.Mirror( BMP_MIRROR_HORZ );
1238     maFISliderRight.SetImage( Image( aTmpBmp  ) );
1239 
1240     Size aSize( maSliderImage.GetSizePixel() );
1241     maFISliderLeft.SetSizePixel( aSize );
1242     maFISliderRight.SetSizePixel( aSize );
1243 
1244     Point aPos( maColorSlider.GetPosPixel() );
1245 
1246     aPos.X() -= aSize.Width();
1247     aPos.Y() -= aSize.Height() / 2;
1248     maFISliderLeft.SetPosPixel( aPos );
1249 
1250     aPos.X() += aSize.Width() + maColorSlider.GetSizePixel().Width();
1251     maFISliderRight.SetPosPixel( aPos );
1252 
1253     Color aColor( nColor );
1254 
1255     // modify
1256     if( mnDialogMode == 2 )
1257     {
1258         maColorPreview.SetSizePixel( maColorPrevious.GetSizePixel() );
1259         maColorPrevious.SetColor( aColor );
1260         maColorPrevious.Show( true );
1261     }
1262 
1263     mdRed = ((double)aColor.GetRed()) / 255.0;
1264     mdGreen = ((double)aColor.GetGreen()) / 255.0;
1265     mdBlue = ((double)aColor.GetBlue()) / 255.0;
1266 
1267     RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1268     RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1269 
1270     update_color();
1271 }
1272 
1273 // --------------------------------------------------------------------
1274 
1275 static int toInt( double dValue, double bRange )
1276 {
1277     return static_cast< int >( std::floor((dValue * bRange) + 0.5 ) );
1278 }
1279 
1280 sal_Int32 ColorPickerDialog::GetColor() const
1281 {
1282     return Color( toInt(mdRed,255.0), toInt(mdGreen,255.0), toInt(mdBlue,255.0) ).GetColor();
1283 }
1284 
1285 void ColorPickerDialog::update_color( sal_uInt16 n )
1286 {
1287     sal_uInt8 nRed = toInt(mdRed,255.0);
1288     sal_uInt8 nGreen = toInt(mdGreen,255.0);
1289     sal_uInt8 nBlue = toInt(mdBlue,255.0);
1290 
1291     Color aColor( nRed, nGreen, nBlue );
1292 
1293     if( n & UPDATE_RGB ) // update RGB
1294     {
1295         maMFRed.SetValue( nRed );
1296         maMFGreen.SetValue( nGreen );
1297         maMFBlue.SetValue( nBlue );
1298     }
1299 
1300     if( n & UPDATE_CMYK ) // update CMYK
1301     {
1302         maMFCyan.SetValue( toInt( mdCyan, 100.0 ) );
1303         maMFMagenta.SetValue( toInt( mdMagenta, 100.0 ) );
1304         maMFYellow.SetValue( toInt( mdYellow, 100.0 ) );
1305         maMFKey.SetValue( toInt( mdKey, 100.0 ) );
1306     }
1307 
1308     if( n & UPDATE_HSB ) // update HSB
1309     {
1310         maMFHue.SetValue( toInt( mdHue, 1.0 ) );
1311         maMFSaturation.SetValue( toInt( mdSat, 100.0 ) );
1312         maMFBrightness.SetValue( toInt( mdBri, 100.0 ) );
1313     }
1314 
1315     if( n & UPDATE_COLORCHOOSER ) // update Color Chooser 1
1316     {
1317         switch( meMode )
1318         {
1319         case HUE:           maColorField.SetValues( aColor, meMode, mdSat, mdBri ); break;
1320         case SATURATION:    maColorField.SetValues( aColor, meMode, mdHue / 360.0, mdBri ); break;
1321         case BRIGHTNESS:    maColorField.SetValues( aColor, meMode, mdHue / 360.0, mdSat ); break;
1322         case RED:           maColorField.SetValues( aColor, meMode, mdBlue, mdGreen ); break;
1323         case GREEN:         maColorField.SetValues( aColor, meMode, mdBlue, mdRed ); break;
1324         case BLUE:          maColorField.SetValues( aColor, meMode, mdRed, mdGreen ); break;
1325         }
1326     }
1327 
1328     if( n & UPDATE_COLORSLIDER ) // update Color Chooser 2
1329     {
1330         switch( meMode )
1331         {
1332         case HUE:           maColorSlider.SetValue( aColor, meMode, mdHue / 360.0 ); break;
1333         case SATURATION:    maColorSlider.SetValue( aColor, meMode, mdSat ); break;
1334         case BRIGHTNESS:    maColorSlider.SetValue( aColor, meMode, mdBri ); break;
1335         case RED:           maColorSlider.SetValue( aColor, meMode, mdRed ); break;
1336         case GREEN:         maColorSlider.SetValue( aColor, meMode, mdGreen ); break;
1337         case BLUE:          maColorSlider.SetValue( aColor, meMode, mdBlue ); break;
1338         }
1339     }
1340 
1341     if( n & UPDATE_HEX ) // update hex
1342     {
1343         maEDHex.SetColor( aColor.GetColor()  );
1344     }
1345 
1346     {
1347         Point aPos( 0, maColorSlider.GetLevel() + maColorSlider.GetPosPixel().Y() - 1 );
1348 
1349         aPos.X() = maFISliderLeft.GetPosPixel().X();
1350         if( aPos != maFISliderLeft.GetPosPixel() )
1351         {
1352             maFISliderLeft.SetPosPixel( aPos );
1353 
1354             aPos.X() = maFISliderRight.GetPosPixel().X();
1355             maFISliderRight.SetPosPixel( aPos );
1356         }
1357     }
1358 
1359     maColorPreview.SetColor( aColor );
1360 }
1361 
1362 // --------------------------------------------------------------------
1363 
1364 IMPL_LINK( ColorPickerDialog, ColorModifyHdl, void *, p )
1365 {
1366     sal_uInt16 n = 0;
1367 
1368     if( p == &maColorField )
1369     {
1370         double x = maColorField.GetX();
1371         double y = maColorField.GetY();
1372 
1373         switch( meMode )
1374         {
1375         case HUE:           mdSat = x; setColorComponent( COLORCOMP_BRI, y ); break;
1376         case SATURATION:    mdHue = x * 360.0; setColorComponent( COLORCOMP_BRI, y ); break;
1377         case BRIGHTNESS:    mdHue = x * 360.0; setColorComponent( COLORCOMP_SAT, y ); break;
1378         case RED:           mdBlue = x; setColorComponent( COLORCOMP_GREEN, y ); break;
1379         case GREEN:         mdBlue = x; setColorComponent( COLORCOMP_RED, y ); break;
1380         case BLUE:          mdRed = x; setColorComponent( COLORCOMP_GREEN, y ); break;
1381         }
1382 
1383         n = UPDATE_ALL&~(UPDATE_COLORCHOOSER);
1384     }
1385     else if( p == &maColorSlider )
1386     {
1387         double dValue = maColorSlider.GetValue();
1388         switch( meMode )
1389         {
1390         case HUE:           setColorComponent( COLORCOMP_HUE, dValue * 360.0 ); break;
1391         case SATURATION:    setColorComponent( COLORCOMP_SAT, dValue ); break;
1392         case BRIGHTNESS:    setColorComponent( COLORCOMP_BRI, dValue ); break;
1393         case RED:           setColorComponent( COLORCOMP_RED, dValue ); break;
1394         case GREEN:         setColorComponent( COLORCOMP_GREEN, dValue ); break;
1395         case BLUE:          setColorComponent( COLORCOMP_BLUE, dValue ); break;
1396         }
1397 
1398         n = UPDATE_ALL&~(UPDATE_COLORSLIDER);
1399     }
1400     else if( p == &maMFRed )
1401     {
1402         setColorComponent( COLORCOMP_RED, ((double)maMFRed.GetValue()) / 255.0 );
1403         n = UPDATE_ALL&~(UPDATE_RGB);
1404     }
1405     else if( p == &maMFGreen )
1406     {
1407         setColorComponent( COLORCOMP_GREEN, ((double)maMFGreen.GetValue()) / 255.0 );
1408         n = UPDATE_ALL&~(UPDATE_RGB);
1409     }
1410     else if( p == &maMFBlue )
1411     {
1412         setColorComponent( COLORCOMP_BLUE, ((double)maMFBlue.GetValue()) / 255.0 );
1413         n = UPDATE_ALL&~(UPDATE_RGB);
1414     }
1415     else if( p == &maMFHue )
1416     {
1417         setColorComponent( COLORCOMP_HUE, (double)maMFHue.GetValue() );
1418         n = UPDATE_ALL&~(UPDATE_HSB);
1419     }
1420     else if( p == &maMFSaturation )
1421     {
1422         setColorComponent( COLORCOMP_SAT, ((double)maMFSaturation.GetValue()) / 100.0 );
1423         n = UPDATE_ALL&~(UPDATE_HSB);
1424     }
1425     else if( p == &maMFBrightness )
1426     {
1427         setColorComponent( COLORCOMP_BRI, ((double)maMFBrightness.GetValue()) / 100.0 );
1428         n = UPDATE_ALL&~(UPDATE_HSB);
1429     }
1430     else if( p == &maMFCyan )
1431     {
1432         setColorComponent( COLORCOMP_CYAN, ((double)maMFCyan.GetValue()) / 100.0 );
1433         n = UPDATE_ALL&~(UPDATE_CMYK);
1434     }
1435     else if( p == &maMFMagenta )
1436     {
1437         setColorComponent( COLORCOMP_MAGENTA, ((double)maMFMagenta.GetValue()) / 100.0 );
1438         n = UPDATE_ALL&~(UPDATE_CMYK);
1439     }
1440     else if( p == &maMFYellow )
1441     {
1442         setColorComponent( COLORCOMP_YELLOW, ((double)maMFYellow.GetValue()) / 100.0 );
1443         n = UPDATE_ALL&~(UPDATE_CMYK);
1444     }
1445     else if( p == &maMFKey )
1446     {
1447         setColorComponent( COLORCOMP_KEY, ((double)maMFKey.GetValue()) / 100.0 );
1448         n = UPDATE_ALL&~(UPDATE_CMYK);
1449     }
1450     else if( p == &maEDHex )
1451     {
1452         sal_Int32 nColor = maEDHex.GetColor();
1453 
1454         if( nColor != -1 )
1455         {
1456             Color aColor( nColor );
1457 
1458             if( aColor != GetColor() )
1459             {
1460                 // #121251# green and blue were wrongly taken
1461                 mdRed = ((double)aColor.GetRed()) / 255.0;
1462                 mdGreen = ((double)aColor.GetGreen()) / 255.0;
1463                 mdBlue = ((double)aColor.GetBlue()) / 255.0;
1464 
1465                 RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1466                 RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1467                 n = UPDATE_ALL&~(UPDATE_HEX);
1468             }
1469         }
1470     }
1471 
1472     if( n )
1473         update_color( n );
1474 
1475     return 0;
1476 }
1477 
1478 // --------------------------------------------------------------------
1479 
1480 IMPL_LINK( ColorPickerDialog, ModeModifyHdl, void *, EMPTYARG )
1481 {
1482     ColorMode eMode = HUE;
1483 
1484     if( maRBRed.IsChecked() )
1485     {
1486         eMode = RED;
1487     }
1488     else if( maRBGreen.IsChecked() )
1489     {
1490         eMode = GREEN;
1491     }
1492     else if( maRBBlue.IsChecked() )
1493     {
1494         eMode = BLUE;
1495     }
1496     else if( maRBSaturation.IsChecked() )
1497     {
1498         eMode = SATURATION;
1499     }
1500     else if( maRBBrightness.IsChecked() )
1501     {
1502         eMode = BRIGHTNESS;
1503     }
1504 
1505     if( meMode != eMode )
1506     {
1507         meMode = eMode;
1508         update_color( UPDATE_COLORCHOOSER | UPDATE_COLORSLIDER );
1509     }
1510 
1511     return 0;
1512 }
1513 
1514 // --------------------------------------------------------------------
1515 
1516 void ColorPickerDialog::setColorComponent( sal_uInt16 nComp, double dValue )
1517 {
1518     switch( nComp )
1519     {
1520     case COLORCOMP_RED:     mdRed = dValue; break;
1521     case COLORCOMP_GREEN:   mdGreen = dValue; break;
1522     case COLORCOMP_BLUE:    mdBlue = dValue; break;
1523     case COLORCOMP_HUE:     mdHue = dValue; break;
1524     case COLORCOMP_SAT:     mdSat = dValue; break;
1525     case COLORCOMP_BRI:     mdBri = dValue; break;
1526     case COLORCOMP_CYAN:    mdCyan = dValue; break;
1527     case COLORCOMP_YELLOW:  mdYellow = dValue; break;
1528     case COLORCOMP_MAGENTA: mdMagenta = dValue; break;
1529     case COLORCOMP_KEY:     mdKey = dValue; break;
1530     }
1531 
1532     if( nComp & COLORMODE_RGB )
1533     {
1534         RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1535         RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1536     }
1537     else if( nComp & COLORMODE_HSV )
1538     {
1539         HSVtoRGB( mdHue, mdSat, mdBri, mdRed, mdGreen, mdBlue );
1540         RGBtoCMYK( mdRed, mdGreen, mdBlue, mdCyan, mdMagenta, mdYellow, mdKey );
1541     }
1542     else
1543     {
1544         CMYKtoRGB( mdCyan, mdMagenta, mdYellow, mdKey, mdRed, mdGreen, mdBlue );
1545         RGBtoHSV( mdRed, mdGreen, mdBlue, mdHue, mdSat, mdBri );
1546     }
1547 }
1548 
1549 // --------------------------------------------------------------------
1550 
1551 typedef ::cppu::WeakComponentImplHelper4< XServiceInfo, XExecutableDialog, XInitialization, XPropertyAccess > ColorPickerBase;
1552 
1553 class ColorPicker : protected ::comphelper::OBaseMutex,    // Struct for right initalization of mutex member! Must be first of baseclasses.
1554                     public ColorPickerBase
1555 {
1556 public:
1557     ColorPicker( Reference< XComponentContext > const & xContext );
1558 
1559     // XInitialization
1560     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException);
1561 
1562     // XInitialization
1563     virtual OUString SAL_CALL getImplementationName(  ) throw (RuntimeException);
1564     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException);
1565     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
1566 
1567     // XPropertyAccess
1568     virtual Sequence< PropertyValue > SAL_CALL getPropertyValues(  ) throw (RuntimeException);
1569     virtual void SAL_CALL setPropertyValues( const Sequence< PropertyValue >& aProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException);
1570 
1571     // XExecutableDialog
1572     virtual void SAL_CALL setTitle( const OUString& aTitle ) throw (RuntimeException);
1573     virtual sal_Int16 SAL_CALL execute(  ) throw (RuntimeException);
1574 
1575 private:
1576     Reference< XComponentContext > mxContext;
1577     OUString msTitle;
1578     const OUString msColorKey;
1579     const OUString msModeKey;
1580     sal_Int32 mnColor;
1581     sal_Int16 mnMode;
1582     Reference< ::com::sun::star::awt::XWindow > mxParent;
1583 };
1584 
1585 // --------------------------------------------------------------------
1586 
1587 OUString SAL_CALL ColorPicker_getImplementationName()
1588 {
1589     return OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.cui.ColorPicker" ) );
1590 }
1591 
1592 // --------------------------------------------------------------------
1593 
1594 Reference< XInterface > SAL_CALL ColorPicker_createInstance( Reference< XComponentContext > const & xContext ) SAL_THROW( (Exception) )
1595 {
1596     return static_cast<XWeak*>( new ColorPicker( xContext ) );
1597 }
1598 
1599 // --------------------------------------------------------------------
1600 
1601 Sequence< OUString > SAL_CALL ColorPicker_getSupportedServiceNames() throw( RuntimeException )
1602 {
1603     Sequence< OUString > seq(1);
1604     seq[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.ColorPicker" ) );
1605     return seq;
1606 }
1607 
1608 // --------------------------------------------------------------------
1609 
1610 ColorPicker::ColorPicker( Reference< XComponentContext > const & xContext )
1611 : ColorPickerBase( m_aMutex )
1612 , mxContext( xContext )
1613 , msColorKey( RTL_CONSTASCII_USTRINGPARAM( "Color" ) )
1614 , msModeKey( RTL_CONSTASCII_USTRINGPARAM( "Mode" ) )
1615 , mnColor( 0 )
1616 , mnMode( 0 )
1617 {
1618 }
1619 
1620 // --------------------------------------------------------------------
1621 
1622 // XInitialization
1623 void SAL_CALL ColorPicker::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
1624 {
1625     if( aArguments.getLength() == 1 )
1626     {
1627         aArguments[0] >>= mxParent;
1628     }
1629 }
1630 
1631 // --------------------------------------------------------------------
1632 
1633 // XInitialization
1634 OUString SAL_CALL ColorPicker::getImplementationName(  ) throw (RuntimeException)
1635 {
1636     return ColorPicker_getImplementationName();
1637 }
1638 
1639 // --------------------------------------------------------------------
1640 
1641 sal_Bool SAL_CALL ColorPicker::supportsService( const OUString& sServiceName ) throw (RuntimeException)
1642 {
1643     return sServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "com.sun.star.ui.dialogs.ColorPicker" ) );
1644 }
1645 
1646 // --------------------------------------------------------------------
1647 
1648 Sequence< OUString > SAL_CALL ColorPicker::getSupportedServiceNames(  ) throw (RuntimeException)
1649 {
1650     return ColorPicker_getSupportedServiceNames();
1651 }
1652 
1653 // --------------------------------------------------------------------
1654 
1655 // XPropertyAccess
1656 Sequence< PropertyValue > SAL_CALL ColorPicker::getPropertyValues(  ) throw (RuntimeException)
1657 {
1658     Sequence< PropertyValue > props(1);
1659     props[0].Name = msColorKey;
1660     props[0].Value <<= mnColor;
1661     return props;
1662 }
1663 
1664 // --------------------------------------------------------------------
1665 
1666 void SAL_CALL ColorPicker::setPropertyValues( const Sequence< PropertyValue >& aProps ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
1667 {
1668     for( sal_Int32 n = 0; n < aProps.getLength(); n++ )
1669     {
1670         if( aProps[n].Name.equals( msColorKey ) )
1671         {
1672             aProps[n].Value >>= mnColor;
1673         }
1674         else if( aProps[n].Name.equals( msModeKey ) )
1675         {
1676             aProps[n].Value >>= mnMode;
1677         }
1678     }
1679 }
1680 
1681 // --------------------------------------------------------------------
1682 
1683 // XExecutableDialog
1684 void SAL_CALL ColorPicker::setTitle( const OUString& sTitle ) throw (RuntimeException)
1685 {
1686     msTitle = sTitle;
1687 }
1688 
1689 // --------------------------------------------------------------------
1690 
1691 sal_Int16 SAL_CALL ColorPicker::execute(  ) throw (RuntimeException)
1692 {
1693     ColorPickerDialog aDlg( VCLUnoHelper::GetWindow( mxParent ), mnColor, mnMode );
1694     sal_Int16 ret = aDlg.Execute();
1695     if( ret )
1696         mnColor = aDlg.GetColor();
1697 
1698     return ret;
1699 }
1700 
1701 // --------------------------------------------------------------------
1702 
1703 }
1704 
1705 // eof
1706