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