bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / dialog / _bmpmask.cxx
blobd932210f3a96ed42d0cb0d089fc7b3136eb4e1e5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/wrkwin.hxx>
21 #include <tools/shl.hxx>
22 #include <vcl/metaact.hxx>
23 #include <svtools/valueset.hxx>
24 #include <svl/eitem.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <svtools/colrdlg.hxx>
28 #define BMPMASK_PRIVATE
30 #include <svx/dialmgr.hxx>
31 #include <svx/bmpmask.hxx>
32 #include <svx/dialogs.hrc>
33 #include <bmpmask.hrc>
34 #include <svx/svxids.hrc>
36 //-------------------------------------------------------------------------
38 #define BMP_RESID(nId) ResId(nId, DIALOG_MGR())
39 #define TRANSP_COL (Color( 252, 252, 252 ))
40 #define OWN_CALLMODE SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD
42 //-------------------------------------------------------------------------
44 #define TEST_COLS() \
45 { \
46 nR = aCol.GetRed(); nG = aCol.GetGreen(); nB = aCol.GetBlue(); \
47 for( i = 0; i < nCount; i++ ) \
48 { \
49 if ( ( pMinR[i] <= nR ) && ( pMaxR[i] >= nR ) && \
50 ( pMinG[i] <= nG ) && ( pMaxG[i] >= nG ) && \
51 ( pMinB[i] <= nB ) && ( pMaxB[i] >= nB ) ) \
52 { \
53 aCol = pDstCols[i]; bReplace = sal_True; break; \
54 } \
55 } \
58 // -------------------------------------------------------------------------
60 SFX_IMPL_DOCKINGWINDOW_WITHID( SvxBmpMaskChildWindow, SID_BMPMASK )
62 // -------------------------------------------------------------------------
64 class ColorWindow : public Control
66 Color aColor;
69 public:
71 ColorWindow( Window* pParent, WinBits nWinStyle ) :
72 Control( pParent, nWinStyle ),
73 aColor( COL_WHITE ) {};
75 ColorWindow( Window* pParent, const ResId& rId ) :
76 Control( pParent, rId ),
77 aColor( COL_WHITE ) {};
79 void SetColor( const Color& rColor )
81 aColor = rColor;
82 Invalidate();
85 virtual void Paint( const Rectangle& rRect );
88 //-------------------------------------------------------------------------
90 class MaskSet : public ValueSet
92 SvxBmpMask* pSvxBmpMask;
95 public:
96 MaskSet( SvxBmpMask* pParent, const ResId& rId );
98 virtual void Select();
99 virtual void KeyInput( const KeyEvent& rKEvt );
100 virtual void GetFocus();
102 void onEditColor();
105 //-------------------------------------------------------------------------
107 MaskSet::MaskSet( SvxBmpMask* pParent, const ResId& rId ) :
108 ValueSet ( pParent, rId ),
109 pSvxBmpMask ( pParent )
113 //-------------------------------------------------------------------------
115 void MaskSet::Select()
117 ValueSet::Select();
119 pSvxBmpMask->onSelect( this );
122 void MaskSet::GetFocus()
124 SelectItem( 1 );
125 pSvxBmpMask->onSelect( this );
128 void MaskSet::KeyInput( const KeyEvent& rKEvt )
130 KeyCode aCode = rKEvt.GetKeyCode();
132 // if the key has a modifier we don't care
133 if( aCode.GetModifier() )
135 ValueSet::KeyInput( rKEvt );
137 else
139 // check for keys that interests us
140 switch ( aCode.GetCode() )
142 case KEY_SPACE:
143 onEditColor();
144 break;
145 default:
146 ValueSet::KeyInput( rKEvt );
152 void MaskSet::onEditColor()
154 SvColorDialog* pColorDlg = new SvColorDialog( GetParent() );
156 pColorDlg->SetColor(GetItemColor(1));
158 if( pColorDlg->Execute() )
159 SetItemColor( 1, pColorDlg->GetColor() );
161 delete pColorDlg;
164 //-------------------------------------------------------------------------
166 class MaskData
168 SvxBmpMask* pMask;
169 sal_Bool bIsReady;
170 sal_Bool bExecState;
171 SfxBindings& rBindings;
173 public:
174 MaskData( SvxBmpMask* pBmpMask, SfxBindings& rBind );
176 sal_Bool IsCbxReady() const { return bIsReady; }
177 void SetExecState( sal_Bool bState ) { bExecState = bState; }
178 sal_Bool IsExecReady() const { return bExecState; }
180 DECL_LINK( PipetteHdl, ToolBox* pTbx );
181 DECL_LINK( CbxHdl, CheckBox* pCbx );
182 DECL_LINK( CbxTransHdl, CheckBox* pCbx );
183 DECL_LINK( FocusLbHdl, ColorLB* pLb );
184 DECL_LINK(ExecHdl, void *);
187 //-------------------------------------------------------------------------
189 MaskData::MaskData( SvxBmpMask* pBmpMask, SfxBindings& rBind ) :
191 pMask ( pBmpMask ),
192 bIsReady ( sal_False ),
193 bExecState ( sal_False ),
194 rBindings ( rBind )
199 //-------------------------------------------------------------------------
201 IMPL_LINK( MaskData, PipetteHdl, ToolBox*, pTbx )
203 SfxBoolItem aBItem( SID_BMPMASK_PIPETTE,
204 pTbx->IsItemChecked( TBI_PIPETTE ) );
206 rBindings.GetDispatcher()->Execute( SID_BMPMASK_PIPETTE, OWN_CALLMODE, &aBItem, 0L );
208 return 0;
211 //-------------------------------------------------------------------------
213 IMPL_LINK( MaskData, CbxHdl, CheckBox*, pCbx )
215 bIsReady = pMask->aCbx1.IsChecked() || pMask->aCbx2.IsChecked() ||
216 pMask->aCbx3.IsChecked() || pMask->aCbx4.IsChecked();
218 if ( bIsReady && IsExecReady() )
219 pMask->aBtnExec.Enable();
220 else
221 pMask->aBtnExec.Disable();
223 // When a checkbox is checked, the pipette is enabled
224 if ( pCbx->IsChecked() )
226 MaskSet* pSet = NULL;
228 if ( pCbx == &( pMask->aCbx1 ) )
229 pSet = pMask->pQSet1;
230 else if ( pCbx == &( pMask->aCbx2 ) )
231 pSet = pMask->pQSet2;
232 else if ( pCbx == &( pMask->aCbx3 ) )
233 pSet = pMask->pQSet3;
234 else // if ( pCbx == &( pMask->aCbx4 ) )
235 pSet = pMask->pQSet4;
237 pSet->SelectItem( 1 );
238 pSet->Select();
240 pMask->aTbxPipette.CheckItem( TBI_PIPETTE, sal_True );
241 PipetteHdl( &( pMask->aTbxPipette ) );
244 return 0;
247 //-------------------------------------------------------------------------
249 IMPL_LINK( MaskData, CbxTransHdl, CheckBox*, pCbx )
251 bIsReady = pCbx->IsChecked();
252 if ( bIsReady )
254 pMask->pQSet1->Disable();
255 pMask->pQSet2->Disable();
256 pMask->pQSet3->Disable();
257 pMask->pQSet4->Disable();
258 pMask->pCtlPipette->Disable();
259 pMask->aCbx1.Disable();
260 pMask->aSp1.Disable();
261 pMask->aCbx2.Disable();
262 pMask->aSp2.Disable();
263 pMask->aCbx3.Disable();
264 pMask->aSp3.Disable();
265 pMask->aCbx4.Disable();
266 pMask->aSp4.Disable();
267 pMask->aTbxPipette.Disable();
269 pMask->aLbColor1.Disable();
270 pMask->aLbColor2.Disable();
271 pMask->aLbColor3.Disable();
272 pMask->aLbColor4.Disable();
273 pMask->aLbColorTrans.Enable();
275 else
277 pMask->pQSet1->Enable();
278 pMask->pQSet2->Enable();
279 pMask->pQSet3->Enable();
280 pMask->pQSet4->Enable();
281 pMask->pCtlPipette->Enable();
282 pMask->aCbx1.Enable();
283 pMask->aSp1.Enable();
284 pMask->aCbx2.Enable();
285 pMask->aSp2.Enable();
286 pMask->aCbx3.Enable();
287 pMask->aSp3.Enable();
288 pMask->aCbx4.Enable();
289 pMask->aSp4.Enable();
290 pMask->aTbxPipette.Enable();
292 pMask->aLbColor1.Enable();
293 pMask->aLbColor2.Enable();
294 pMask->aLbColor3.Enable();
295 pMask->aLbColor4.Enable();
296 pMask->aLbColorTrans.Disable();
298 bIsReady = pMask->aCbx1.IsChecked() || pMask->aCbx2.IsChecked() ||
299 pMask->aCbx3.IsChecked() || pMask->aCbx4.IsChecked();
302 if ( bIsReady && IsExecReady() )
303 pMask->aBtnExec.Enable();
304 else
305 pMask->aBtnExec.Disable();
307 return 0L;
310 //-------------------------------------------------------------------------
312 IMPL_LINK( MaskData, FocusLbHdl, ColorLB*, pLb )
314 pMask->pQSet1->SelectItem( pLb == &( pMask->aLbColor1 ) ? 1 : 0 );
315 pMask->pQSet2->SelectItem( pLb == &( pMask->aLbColor2 ) ? 1 : 0 );
316 pMask->pQSet3->SelectItem( pLb == &( pMask->aLbColor3 ) ? 1 : 0 );
317 pMask->pQSet4->SelectItem( pLb == &( pMask->aLbColor4 ) ? 1 : 0 );
319 return 0;
322 //-------------------------------------------------------------------------
324 IMPL_LINK_NOARG(MaskData, ExecHdl)
326 SfxBoolItem aBItem( SID_BMPMASK_EXEC, sal_True );
327 rBindings.GetDispatcher()->Execute( SID_BMPMASK_EXEC, OWN_CALLMODE, &aBItem, 0L );
329 return 0L;
332 //-------------------------------------------------------------------------
334 void ColorWindow::Paint( const Rectangle &/*Rect*/ )
336 const Color& rOldLineColor = GetLineColor();
337 const Color& rOldFillColor = GetFillColor();
339 SetLineColor( aColor );
340 SetFillColor( aColor );
342 DrawRect( Rectangle( Point(), GetSizePixel() ) );
344 SetLineColor( rOldLineColor );
345 SetFillColor( rOldFillColor );
348 //-------------------------------------------------------------------------
350 SvxBmpMaskSelectItem::SvxBmpMaskSelectItem( sal_uInt16 nId_, SvxBmpMask& rMask,
351 SfxBindings& rBindings ) :
352 SfxControllerItem ( nId_, rBindings ),
353 rBmpMask ( rMask)
357 //-------------------------------------------------------------------------
359 void SvxBmpMaskSelectItem::StateChanged( sal_uInt16 nSID, SfxItemState /*eState*/,
360 const SfxPoolItem* pItem )
362 if ( ( nSID == SID_BMPMASK_EXEC ) && pItem )
364 const SfxBoolItem* pStateItem = PTR_CAST( SfxBoolItem, pItem );
366 DBG_ASSERT( pStateItem || pItem == 0, "SfxBoolItem erwartet");
368 rBmpMask.SetExecState( pStateItem->GetValue() );
372 //-------------------------------------------------------------------------
374 SvxBmpMaskChildWindow::SvxBmpMaskChildWindow( Window* pParent_, sal_uInt16 nId,
375 SfxBindings* pBindings,
376 SfxChildWinInfo* pInfo ) :
377 SfxChildWindow( pParent_, nId )
379 pWindow = new SvxBmpMask( pBindings, this, pParent_,
380 BMP_RESID( RID_SVXDLG_BMPMASK ) );
381 SvxBmpMask* pDlg = (SvxBmpMask*) pWindow;
383 eChildAlignment = SFX_ALIGN_NOALIGNMENT;
385 pDlg->Initialize( pInfo );
388 //-------------------------------------------------------------------------
390 SvxBmpMask::SvxBmpMask( SfxBindings *pBindinx,
391 SfxChildWindow *pCW,
392 Window* pParent,
393 const ResId& rResId ) :
394 SfxDockingWindow ( pBindinx, pCW, pParent, rResId ),
395 aTbxPipette ( this, BMP_RESID( TBX_PIPETTE ) ),
396 pCtlPipette ( new ColorWindow( this, BMP_RESID( WND_PIPETTE ) ) ),
397 aBtnExec ( this, BMP_RESID( BTN_EXEC ) ),
398 aGrpQ ( this, BMP_RESID( GRP_Q ) ),
400 aFt1 ( this, BMP_RESID ( FT_1 ) ),
401 aFt2 ( this, BMP_RESID ( FT_2 ) ),
402 aFt3 ( this, BMP_RESID ( FT_3 ) ),
404 aCbx1 ( this, BMP_RESID( CBX_1 ) ),
405 pQSet1 ( new MaskSet( this, BMP_RESID( QCOL_1 ) ) ),
406 aSp1 ( this, BMP_RESID( SP_1 ) ),
407 aLbColor1 ( this, BMP_RESID ( LB_1 ) ),
409 aCbx2 ( this, BMP_RESID( CBX_2 ) ),
410 pQSet2 ( new MaskSet( this, BMP_RESID( QCOL_2 ) ) ),
411 aSp2 ( this, BMP_RESID( SP_2 ) ),
412 aLbColor2 ( this, BMP_RESID ( LB_2 ) ),
414 aCbx3 ( this, BMP_RESID( CBX_3 ) ),
415 pQSet3 ( new MaskSet( this, BMP_RESID( QCOL_3 ) ) ),
416 aSp3 ( this, BMP_RESID( SP_3 ) ),
417 aLbColor3 ( this, BMP_RESID ( LB_3 ) ),
419 aCbx4 ( this, BMP_RESID( CBX_4 ) ),
420 pQSet4 ( new MaskSet( this, BMP_RESID( QCOL_4 ) ) ),
421 aSp4 ( this, BMP_RESID( SP_4 ) ),
422 aLbColor4 ( this, BMP_RESID ( LB_4 ) ),
424 pData ( new MaskData( this, *pBindinx ) ),
425 aCbxTrans ( this, BMP_RESID( CBX_TRANS ) ),
426 aLbColorTrans ( this, BMP_RESID ( LB_TRANS ) ),
427 aPipetteColor ( COL_WHITE ),
428 aSelItem ( SID_BMPMASK_EXEC, *this, *pBindinx ),
429 maImgPipette ( BMP_RESID ( IMG_PIPETTE ) )
431 FreeResource();
433 ApplyStyle();
435 aTbxPipette.SetSizePixel( aTbxPipette.CalcWindowSizePixel() );
436 aTbxPipette.SetSelectHdl( LINK( pData, MaskData, PipetteHdl ) );
437 aBtnExec.SetClickHdl( LINK( pData, MaskData, ExecHdl ) );
439 aCbx1.SetClickHdl( LINK( pData, MaskData, CbxHdl ) );
440 aCbx2.SetClickHdl( LINK( pData, MaskData, CbxHdl ) );
441 aCbx3.SetClickHdl( LINK( pData, MaskData, CbxHdl ) );
442 aCbx4.SetClickHdl( LINK( pData, MaskData, CbxHdl ) );
443 aCbxTrans.SetClickHdl( LINK( pData, MaskData, CbxTransHdl ) );
445 SetAccessibleNames ();
447 aLbColor1.SetGetFocusHdl( LINK( pData, MaskData, FocusLbHdl ) );
448 aLbColor2.SetGetFocusHdl( LINK( pData, MaskData, FocusLbHdl ) );
449 aLbColor3.SetGetFocusHdl( LINK( pData, MaskData, FocusLbHdl ) );
450 aLbColor4.SetGetFocusHdl( LINK( pData, MaskData, FocusLbHdl ) );
451 aLbColorTrans.Disable();
453 aSp1.SetValue( 10 );
454 aSp2.SetValue( 10 );
455 aSp3.SetValue( 10 );
456 aSp4.SetValue( 10 );
458 pQSet1->SetStyle( pQSet1->GetStyle() | WB_DOUBLEBORDER | WB_ITEMBORDER );
459 pQSet1->SetColCount( 1 );
460 pQSet1->SetLineCount( 1 );
461 pQSet1->InsertItem( 1, aPipetteColor );
462 pQSet1->SelectItem( 1 );
464 pQSet2->SetStyle( pQSet2->GetStyle() | WB_DOUBLEBORDER | WB_ITEMBORDER );
465 pQSet2->SetColCount( 1 );
466 pQSet2->SetLineCount( 1 );
467 pQSet2->InsertItem( 1, aPipetteColor );
468 pQSet2->SelectItem( 0 );
470 pQSet3->SetStyle( pQSet3->GetStyle() | WB_DOUBLEBORDER | WB_ITEMBORDER );
471 pQSet3->SetColCount( 1 );
472 pQSet3->SetLineCount( 1 );
473 pQSet3->InsertItem( 1, aPipetteColor );
474 pQSet3->SelectItem( 0 );
476 pQSet4->SetStyle( pQSet4->GetStyle() | WB_DOUBLEBORDER | WB_ITEMBORDER );
477 pQSet4->SetColCount( 1 );
478 pQSet4->SetLineCount( 1 );
479 pQSet4->InsertItem( 1, aPipetteColor );
480 pQSet4->SelectItem( 0 );
482 pQSet1->Show();
483 pQSet2->Show();
484 pQSet3->Show();
485 pQSet4->Show();
487 aCbx1.SetAccessibleRelationMemberOf( &aGrpQ );
488 pQSet1->SetAccessibleRelationMemberOf( &aGrpQ );
489 aSp1.SetAccessibleRelationMemberOf( &aGrpQ );
490 aLbColor1.SetAccessibleRelationMemberOf( &aGrpQ );
491 aCbx1.SetAccessibleRelationLabeledBy( &aFt1 );
492 pQSet1->SetAccessibleRelationLabeledBy( &aFt1 );
493 aSp1.SetAccessibleRelationLabeledBy( &aFt2 );
494 aLbColor1.SetAccessibleRelationLabeledBy( &aFt3 );
495 aCbx2.SetAccessibleRelationMemberOf( &aGrpQ );
496 pQSet2->SetAccessibleRelationMemberOf( &aGrpQ );
497 aSp2.SetAccessibleRelationMemberOf( &aGrpQ );
498 aLbColor2.SetAccessibleRelationMemberOf( &aGrpQ );
499 aCbx2.SetAccessibleRelationLabeledBy( &aFt1 );
500 pQSet2->SetAccessibleRelationLabeledBy( &aFt1 );
501 aSp2.SetAccessibleRelationLabeledBy( &aFt2 );
502 aLbColor2.SetAccessibleRelationLabeledBy( &aFt3 );
503 aCbx3.SetAccessibleRelationMemberOf( &aGrpQ );
504 pQSet3->SetAccessibleRelationMemberOf( &aGrpQ );
505 aSp3.SetAccessibleRelationMemberOf( &aGrpQ );
506 aLbColor3.SetAccessibleRelationMemberOf( &aGrpQ );
507 aCbx3.SetAccessibleRelationLabeledBy( &aFt1 );
508 pQSet3->SetAccessibleRelationLabeledBy( &aFt1 );
509 aSp3.SetAccessibleRelationLabeledBy( &aFt2 );
510 aLbColor3.SetAccessibleRelationLabeledBy( &aFt3 );
511 aCbx4.SetAccessibleRelationMemberOf( &aGrpQ );
512 pQSet4->SetAccessibleRelationMemberOf( &aGrpQ );
513 aSp4.SetAccessibleRelationMemberOf( &aGrpQ );
514 aLbColor4.SetAccessibleRelationMemberOf( &aGrpQ );
515 aCbx4.SetAccessibleRelationLabeledBy( &aFt1 );
516 pQSet4->SetAccessibleRelationLabeledBy( &aFt1 );
517 aSp4.SetAccessibleRelationLabeledBy( &aFt2 );
518 aLbColor4.SetAccessibleRelationLabeledBy( &aFt3 );
519 aLbColorTrans.SetAccessibleRelationLabeledBy( &aCbxTrans );
520 aLbColorTrans.SetAccessibleRelationMemberOf( &aGrpQ );
521 aCbxTrans.SetAccessibleRelationMemberOf( &aGrpQ );
524 //-------------------------------------------------------------------------
526 SvxBmpMask::~SvxBmpMask()
528 delete pQSet1;
529 delete pQSet2;
530 delete pQSet3;
531 delete pQSet4;
532 delete pCtlPipette;
533 delete pData;
536 //-------------------------------------------------------------------------
538 /** is called by a MaskSet when it is selected */
539 void SvxBmpMask::onSelect( MaskSet* pSet )
541 // now deselect all other value sets
542 if( pSet != pQSet1 )
543 pQSet1->SelectItem( 0 );
545 if( pSet != pQSet2 )
546 pQSet2->SelectItem( 0 );
548 if( pSet != pQSet3 )
549 pQSet3->SelectItem( 0 );
551 if( pSet != pQSet4 )
552 pQSet4->SelectItem( 0 );
555 //-------------------------------------------------------------------------
557 sal_Bool SvxBmpMask::Close()
559 SfxBoolItem aItem2( SID_BMPMASK_PIPETTE, sal_False );
560 GetBindings().GetDispatcher()->Execute( SID_BMPMASK_PIPETTE, OWN_CALLMODE, &aItem2, 0L );
562 return SfxDockingWindow::Close();
565 //-------------------------------------------------------------------------
567 sal_Bool SvxBmpMask::NeedsColorList() const
569 return ( aLbColor1.GetEntryCount() == 0 );
572 //-------------------------------------------------------------------------
574 void SvxBmpMask::SetColorList( const XColorListRef &pList )
576 if ( pList.is() && ( pList != pColLst ) )
578 const String aTransp( BMP_RESID( RID_SVXDLG_BMPMASK_STR_TRANSP ) );
580 pColLst = pList;
582 aLbColorTrans.Fill( pColLst );
583 aLbColorTrans.SelectEntryPos( 0 );
585 aLbColor1.Fill( pColLst );
586 aLbColor1.InsertEntry( TRANSP_COL, aTransp, 0 );
587 aLbColor1.SelectEntryPos( 0 );
589 aLbColor2.Fill( pColLst );
590 aLbColor2.InsertEntry( TRANSP_COL, aTransp, 0 );
591 aLbColor2.SelectEntryPos( 0 );
593 aLbColor3.Fill( pColLst );
594 aLbColor3.InsertEntry( TRANSP_COL, aTransp, 0 );
595 aLbColor3.SelectEntryPos( 0 );
597 aLbColor4.Fill( pColLst );
598 aLbColor4.InsertEntry( TRANSP_COL, aTransp, 0 );
599 aLbColor4.SelectEntryPos( 0 );
603 //-------------------------------------------------------------------------
605 void SvxBmpMask::SetColor( const Color& rColor )
607 aPipetteColor = rColor;
608 pCtlPipette->SetColor( aPipetteColor );
611 //-------------------------------------------------------------------------
613 void SvxBmpMask::PipetteClicked()
615 if( pQSet1->GetSelectItemId() == 1 )
617 aCbx1.Check( sal_True );
618 pData->CbxHdl( &aCbx1 );
619 pQSet1->SetItemColor( 1, aPipetteColor );
621 else if( pQSet2->GetSelectItemId() == 1 )
623 aCbx2.Check( sal_True );
624 pData->CbxHdl( &aCbx2 );
625 pQSet2->SetItemColor( 1, aPipetteColor );
627 else if( pQSet3->GetSelectItemId() == 1 )
629 aCbx3.Check( sal_True );
630 pData->CbxHdl( &aCbx3 );
631 pQSet3->SetItemColor( 1, aPipetteColor );
633 else if( pQSet4->GetSelectItemId() == 1 )
635 aCbx4.Check( sal_True );
636 pData->CbxHdl( &aCbx4 );
637 pQSet4->SetItemColor( 1, aPipetteColor );
640 aTbxPipette.CheckItem( TBI_PIPETTE, sal_False );
641 pData->PipetteHdl( &aTbxPipette );
644 //-------------------------------------------------------------------------
646 void SvxBmpMask::SetExecState( sal_Bool bEnable )
648 pData->SetExecState( bEnable );
650 if ( pData->IsExecReady() && pData->IsCbxReady() )
651 aBtnExec.Enable();
652 else
653 aBtnExec.Disable();
656 //-------------------------------------------------------------------------
658 sal_uInt16 SvxBmpMask::InitColorArrays( Color* pSrcCols, Color* pDstCols, sal_uIntPtr* pTols )
660 sal_uInt16 nCount = 0;
662 if ( aCbx1.IsChecked() )
664 pSrcCols[nCount] = pQSet1->GetItemColor( 1 );
665 pDstCols[nCount] = aLbColor1.GetSelectEntryColor();
666 pTols[nCount++] = static_cast<sal_uIntPtr>(aSp1.GetValue());
669 if ( aCbx2.IsChecked() )
671 pSrcCols[nCount] = pQSet2->GetItemColor( 1 );
672 pDstCols[nCount] = aLbColor2.GetSelectEntryColor();
673 pTols[nCount++] = static_cast<sal_uIntPtr>(aSp2.GetValue());
676 if ( aCbx3.IsChecked() )
678 pSrcCols[nCount] = pQSet3->GetItemColor( 1 );
679 pDstCols[nCount] = aLbColor3.GetSelectEntryColor();
680 pTols[nCount++] = static_cast<sal_uIntPtr>(aSp3.GetValue());
683 if ( aCbx4.IsChecked() )
685 pSrcCols[nCount] = pQSet4->GetItemColor( 1 );
686 pDstCols[nCount] = aLbColor4.GetSelectEntryColor();
687 pTols[nCount++] = static_cast<sal_uIntPtr>(aSp4.GetValue());
690 return nCount;
693 //-------------------------------------------------------------------------
695 Bitmap SvxBmpMask::ImpMask( const Bitmap& rBitmap )
697 Bitmap aBitmap( rBitmap );
698 Color pSrcCols[4];
699 Color pDstCols[4];
700 sal_uIntPtr pTols[4];
701 const sal_uInt16 nCount = InitColorArrays( pSrcCols, pDstCols, pTols );
703 EnterWait();
704 aBitmap.Replace( pSrcCols, pDstCols, nCount, pTols );
705 LeaveWait();
707 return aBitmap;
710 //-------------------------------------------------------------------------
712 BitmapEx SvxBmpMask::ImpMaskTransparent( const BitmapEx& rBitmapEx, const Color& rColor, const long nTol )
714 EnterWait();
716 BitmapEx aBmpEx;
717 Bitmap aMask( rBitmapEx.GetBitmap().CreateMask( rColor, nTol ) );
719 if( rBitmapEx.IsTransparent() )
720 aMask.CombineSimple( rBitmapEx.GetMask(), BMP_COMBINE_OR );
722 aBmpEx = BitmapEx( rBitmapEx.GetBitmap(), aMask );
723 LeaveWait();
725 return aBmpEx;
728 //-------------------------------------------------------------------------
730 Animation SvxBmpMask::ImpMask( const Animation& rAnimation )
732 Animation aAnimation( rAnimation );
733 Color pSrcCols[4];
734 Color pDstCols[4];
735 sal_uIntPtr pTols[4];
736 InitColorArrays( pSrcCols, pDstCols, pTols );
737 sal_uInt16 nAnimationCount = aAnimation.Count();
739 for( sal_uInt16 i = 0; i < nAnimationCount; i++ )
741 AnimationBitmap aAnimBmp( aAnimation.Get( i ) );
742 aAnimBmp.aBmpEx = Mask( aAnimBmp.aBmpEx ).GetBitmapEx();
743 aAnimation.Replace( aAnimBmp, i );
746 return aAnimation;
749 //-------------------------------------------------------------------------
751 GDIMetaFile SvxBmpMask::ImpMask( const GDIMetaFile& rMtf )
753 GDIMetaFile aMtf;
754 Color pSrcCols[4];
755 Color pDstCols[4];
756 sal_uIntPtr pTols[4];
757 sal_uInt16 nCount = InitColorArrays( pSrcCols, pDstCols, pTols );
758 sal_Bool pTrans[4];
760 // If no color is selected, we copy only the Mtf
761 if( !nCount )
762 aMtf = rMtf;
763 else
765 Color aCol;
766 long nVal;
767 long nTol;
768 long nR;
769 long nG;
770 long nB;
771 long* pMinR = new long[nCount];
772 long* pMaxR = new long[nCount];
773 long* pMinG = new long[nCount];
774 long* pMaxG = new long[nCount];
775 long* pMinB = new long[nCount];
776 long* pMaxB = new long[nCount];
777 sal_uInt16 i;
778 sal_Bool bReplace;
780 aMtf.SetPrefSize( rMtf.GetPrefSize() );
781 aMtf.SetPrefMapMode( rMtf.GetPrefMapMode() );
783 // Prepare Color comparison array
784 for( i = 0; i < nCount; i++ )
786 nTol = ( pTols[i] * 255L ) / 100L;
788 nVal = ( (long) pSrcCols[i].GetRed() );
789 pMinR[i] = std::max( nVal - nTol, 0L );
790 pMaxR[i] = std::min( nVal + nTol, 255L );
792 nVal = ( (long) pSrcCols[i].GetGreen() );
793 pMinG[i] = std::max( nVal - nTol, 0L );
794 pMaxG[i] = std::min( nVal + nTol, 255L );
796 nVal = ( (long) pSrcCols[i].GetBlue() );
797 pMinB[i] = std::max( nVal - nTol, 0L );
798 pMaxB[i] = std::min( nVal + nTol, 255L );
800 pTrans[ i ] = ( pDstCols[ i ] == TRANSP_COL );
803 // Investigate actions and if necessary replace colors
804 for( size_t nAct = 0, nActCount = rMtf.GetActionSize(); nAct < nActCount; nAct++ )
806 MetaAction* pAction = rMtf.GetAction( nAct );
808 bReplace = sal_False;
810 switch( pAction->GetType() )
812 case( META_PIXEL_ACTION ):
814 MetaPixelAction* pAct = (MetaPixelAction*) pAction;
816 aCol = pAct->GetColor();
817 TEST_COLS();
819 if( bReplace )
820 pAct = new MetaPixelAction( pAct->GetPoint(), aCol );
821 else
822 pAct->Duplicate();
824 aMtf.AddAction( pAct );
826 break;
828 case( META_LINECOLOR_ACTION ):
830 MetaLineColorAction* pAct = (MetaLineColorAction*) pAction;
832 aCol = pAct->GetColor();
833 TEST_COLS();
835 if( bReplace )
836 pAct = new MetaLineColorAction( aCol, !pTrans[ i ] );
837 else
838 pAct->Duplicate();
840 aMtf.AddAction( pAct );
842 break;
844 case( META_FILLCOLOR_ACTION ):
846 MetaFillColorAction* pAct = (MetaFillColorAction*) pAction;
848 aCol = pAct->GetColor();
849 TEST_COLS();
851 if( bReplace )
852 pAct = new MetaFillColorAction( aCol, !pTrans[ i ] );
853 else
854 pAct->Duplicate();
856 aMtf.AddAction( pAct );
858 break;
860 case( META_TEXTCOLOR_ACTION ):
862 MetaTextColorAction* pAct = (MetaTextColorAction*) pAction;
864 aCol = pAct->GetColor();
865 TEST_COLS();
867 if( bReplace )
868 pAct = new MetaTextColorAction( aCol );
869 else
870 pAct->Duplicate();
872 aMtf.AddAction( pAct );
874 break;
876 case( META_TEXTFILLCOLOR_ACTION ):
878 MetaTextFillColorAction* pAct = (MetaTextFillColorAction*) pAction;
880 aCol = pAct->GetColor();
881 TEST_COLS();
883 if( bReplace )
884 pAct = new MetaTextFillColorAction( aCol, !pTrans[ i ] );
885 else
886 pAct->Duplicate();
888 aMtf.AddAction( pAct );
890 break;
892 case( META_FONT_ACTION ):
894 MetaFontAction* pAct = (MetaFontAction*) pAction;
895 Font aFont( pAct->GetFont() );
897 aCol = aFont.GetColor();
898 TEST_COLS();
900 if( bReplace )
902 aFont.SetColor( aCol );
903 pAct = new MetaFontAction( aFont );
905 else
906 pAct->Duplicate();
908 aMtf.AddAction( pAct );
910 break;
912 case( META_WALLPAPER_ACTION ):
914 MetaWallpaperAction* pAct = (MetaWallpaperAction*) pAction;
915 Wallpaper aWall( pAct->GetWallpaper() );
917 aCol = aWall.GetColor();
918 TEST_COLS();
920 if( bReplace )
922 aWall.SetColor( aCol );
923 pAct = new MetaWallpaperAction( pAct->GetRect(), aWall );
925 else
926 pAct->Duplicate();
928 aMtf.AddAction( pAct );
930 break;
932 case( META_BMP_ACTION ):
934 MetaBmpAction* pAct = (MetaBmpAction*) pAction;
935 const Bitmap aBmp( Mask( pAct->GetBitmap() ).GetBitmap() );
937 pAct = new MetaBmpAction( pAct->GetPoint(), aBmp );
938 aMtf.AddAction( pAct );
940 break;
942 case( META_BMPSCALE_ACTION ):
944 MetaBmpScaleAction* pAct = (MetaBmpScaleAction*) pAction;
945 const Bitmap aBmp( Mask( pAct->GetBitmap() ).GetBitmap() );
947 pAct = new MetaBmpScaleAction( pAct->GetPoint(), pAct->GetSize(), aBmp );
948 aMtf.AddAction( pAct );
950 break;
952 case( META_BMPSCALEPART_ACTION ):
954 MetaBmpScalePartAction* pAct = (MetaBmpScalePartAction*) pAction;
955 const Bitmap aBmp( Mask( pAct->GetBitmap() ).GetBitmap() );
957 pAct = new MetaBmpScalePartAction( pAct->GetDestPoint(), pAct->GetDestSize(),
958 pAct->GetSrcPoint(), pAct->GetSrcSize(), aBmp );
959 aMtf.AddAction( pAct );
961 break;
963 case( META_BMPEX_ACTION ):
965 MetaBmpExAction* pAct = (MetaBmpExAction*) pAction;
966 const BitmapEx aBmpEx( Mask( pAct->GetBitmapEx() ).GetBitmapEx() );
968 pAct = new MetaBmpExAction( pAct->GetPoint(), aBmpEx );
969 aMtf.AddAction( pAct );
971 break;
973 case( META_BMPEXSCALE_ACTION ):
975 MetaBmpExScaleAction* pAct = (MetaBmpExScaleAction*) pAction;
976 const BitmapEx aBmpEx( Mask( pAct->GetBitmapEx() ).GetBitmapEx() );
978 pAct = new MetaBmpExScaleAction( pAct->GetPoint(), pAct->GetSize(), aBmpEx );
979 aMtf.AddAction( pAct );
981 break;
983 case( META_BMPEXSCALEPART_ACTION ):
985 MetaBmpExScalePartAction* pAct = (MetaBmpExScalePartAction*) pAction;
986 const BitmapEx aBmpEx( Mask( pAct->GetBitmapEx() ).GetBitmapEx() );
988 pAct = new MetaBmpExScalePartAction( pAct->GetDestPoint(), pAct->GetDestSize(),
989 pAct->GetSrcPoint(), pAct->GetSrcSize(), aBmpEx );
990 aMtf.AddAction( pAct );
992 break;
994 default:
996 pAction->Duplicate();
997 aMtf.AddAction( pAction );
999 break;
1003 delete[] pMinR;
1004 delete[] pMaxR;
1005 delete[] pMinG;
1006 delete[] pMaxG;
1007 delete[] pMinB;
1008 delete[] pMaxB;
1011 LeaveWait();
1013 return aMtf;
1016 //-------------------------------------------------------------------------
1018 BitmapEx SvxBmpMask::ImpReplaceTransparency( const BitmapEx& rBmpEx, const Color& rColor )
1020 if( rBmpEx.IsTransparent() )
1022 Bitmap aBmp( rBmpEx.GetBitmap() );
1023 aBmp.Replace( rBmpEx.GetMask(), rColor );
1024 return aBmp;
1026 else
1027 return rBmpEx;
1030 //-------------------------------------------------------------------------
1032 Animation SvxBmpMask::ImpReplaceTransparency( const Animation& rAnim, const Color& rColor )
1034 Animation aAnimation( rAnim );
1035 sal_uInt16 nAnimationCount = aAnimation.Count();
1037 for( sal_uInt16 i = 0; i < nAnimationCount; i++ )
1039 AnimationBitmap aAnimBmp( aAnimation.Get( i ) );
1040 aAnimBmp.aBmpEx = ImpReplaceTransparency( aAnimBmp.aBmpEx, rColor );
1041 aAnimation.Replace( aAnimBmp, i );
1044 return aAnimation;
1047 //-------------------------------------------------------------------------
1049 GDIMetaFile SvxBmpMask::ImpReplaceTransparency( const GDIMetaFile& rMtf, const Color& rColor )
1051 VirtualDevice aVDev;
1052 GDIMetaFile aMtf;
1053 const MapMode& rPrefMap = rMtf.GetPrefMapMode();
1054 const Size& rPrefSize = rMtf.GetPrefSize();
1055 const size_t nActionCount = rMtf.GetActionSize();
1057 aVDev.EnableOutput( sal_False );
1058 aMtf.Record( &aVDev );
1059 aMtf.SetPrefSize( rPrefSize );
1060 aMtf.SetPrefMapMode( rPrefMap );
1061 aVDev.SetLineColor( rColor );
1062 aVDev.SetFillColor( rColor );
1064 // retrieve one action at the time; first
1065 // set the whole area to the replacement color.
1066 aVDev.DrawRect( Rectangle( rPrefMap.GetOrigin(), rPrefSize ) );
1067 for ( size_t i = 0; i < nActionCount; i++ )
1069 MetaAction* pAct = rMtf.GetAction( i );
1071 pAct->Duplicate();
1072 aMtf.AddAction( pAct );
1075 aMtf.Stop();
1076 aMtf.WindStart();
1078 return aMtf;
1081 //-------------------------------------------------------------------------
1083 Graphic SvxBmpMask::Mask( const Graphic& rGraphic )
1085 Graphic aGraphic( rGraphic );
1086 const Color aReplColor( aLbColorTrans.GetSelectEntryColor() );
1088 switch( rGraphic.GetType() )
1090 case( GRAPHIC_BITMAP ):
1092 if( rGraphic.IsAnimated() )
1094 // Replace transparency?
1095 if ( aCbxTrans.IsChecked() )
1096 aGraphic = ImpReplaceTransparency( rGraphic.GetAnimation(), aReplColor );
1097 else
1098 aGraphic = ImpMask( rGraphic.GetAnimation() );
1100 else
1102 // Replace transparency?
1103 if( aCbxTrans.IsChecked() )
1105 if( aGraphic.IsTransparent() )
1107 BitmapEx aBmpEx( ImpReplaceTransparency( aGraphic.GetBitmapEx(), aReplColor ) );
1108 const Size aSize( aBmpEx.GetSizePixel() );
1110 if( aSize.Width() && aSize.Height() )
1111 aGraphic = aBmpEx;
1114 else
1116 Color pSrcCols[4];
1117 Color pDstCols[4];
1118 sal_uIntPtr pTols[4];
1119 sal_uInt16 nCount = InitColorArrays( pSrcCols, pDstCols, pTols );
1121 if( nCount )
1123 // first set all transparent colors
1124 for( sal_uInt16 i = 0; i < nCount; i++ )
1126 // Do we have a transparent color?
1127 if( pDstCols[i] == TRANSP_COL )
1129 BitmapEx aBmpEx( ImpMaskTransparent( aGraphic.GetBitmapEx(),
1130 pSrcCols[ i ], pTols[ i ] ) );
1131 const Size aSize( aBmpEx.GetSizePixel() );
1133 if( aSize.Width() && aSize.Height() )
1134 aGraphic = aBmpEx;
1138 // now replace it again with the normal colors
1139 Bitmap aBitmap( ImpMask( aGraphic.GetBitmap() ) );
1140 Size aSize( aBitmap.GetSizePixel() );
1142 if ( aSize.Width() && aSize.Height() )
1144 if ( aGraphic.IsTransparent() )
1145 aGraphic = Graphic( BitmapEx( aBitmap, aGraphic.GetBitmapEx().GetMask() ) );
1146 else
1147 aGraphic = aBitmap;
1153 break;
1155 case( GRAPHIC_GDIMETAFILE ):
1157 GDIMetaFile aMtf( aGraphic.GetGDIMetaFile() );
1159 // Replace transparency?
1160 if( aCbxTrans.IsChecked() )
1161 aMtf = ImpReplaceTransparency( aMtf, aReplColor );
1162 else
1163 aMtf = ImpMask( aMtf );
1165 Size aSize( aMtf.GetPrefSize() );
1166 if ( aSize.Width() && aSize.Height() )
1167 aGraphic = Graphic( aMtf );
1168 else
1169 aGraphic = rGraphic;
1171 break;
1173 default:
1174 aGraphic = rGraphic;
1175 break;
1178 if( aGraphic != rGraphic )
1180 aGraphic.SetPrefSize( rGraphic.GetPrefSize() );
1181 aGraphic.SetPrefMapMode( rGraphic.GetPrefMapMode() );
1184 return aGraphic;
1187 //-------------------------------------------------------------------------
1189 sal_Bool SvxBmpMask::IsEyedropping() const
1191 return aTbxPipette.IsItemChecked( TBI_PIPETTE );
1194 void SvxBmpMask::DataChanged( const DataChangedEvent& rDCEvt )
1196 SfxDockingWindow::DataChanged( rDCEvt );
1198 if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
1199 ApplyStyle();
1202 void SvxBmpMask::ApplyStyle()
1204 aTbxPipette.SetItemImage( TBI_PIPETTE, maImgPipette );
1208 /** Set an accessible name for the source color check boxes. Without this
1209 the lengthy description is read.
1211 void SvxBmpMask::SetAccessibleNames (void)
1213 String sSourceColor (BMP_RESID( RID_SVXDLG_BMPMASK_STR_SOURCECOLOR));
1214 String sSourceColorN;
1216 sSourceColorN = sSourceColor;
1217 sSourceColorN.AppendAscii (" 1");
1218 aCbx1.SetAccessibleName (sSourceColorN);
1220 sSourceColorN = sSourceColor;
1221 sSourceColorN.AppendAscii (" 2");
1222 aCbx2.SetAccessibleName (sSourceColorN);
1224 sSourceColorN = sSourceColor;
1225 sSourceColorN.AppendAscii (" 3");
1226 aCbx3.SetAccessibleName (sSourceColorN);
1228 sSourceColorN = sSourceColor;
1229 sSourceColorN.AppendAscii (" 4");
1230 aCbx4.SetAccessibleName (sSourceColorN);
1233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */