1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: colrctrl.cxx,v $
10 * $Revision: 1.20.76.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <svx/dialogs.hrc>
37 #include <tools/list.hxx>
38 #include <sfx2/viewsh.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <sfx2/dispatch.hxx>
41 #include <vcl/image.hxx>
43 #include <svx/colrctrl.hxx>
45 #include <svx/svdview.hxx>
46 #include "drawitem.hxx"
47 #include <svx/colritem.hxx>
49 #include <svx/xtable.hxx>
50 #include <svx/dialmgr.hxx>
52 #include <vcl/svapp.hxx>
54 SFX_IMPL_DOCKINGWINDOW( SvxColorChildWindow
, SID_COLOR_CONTROL
)
56 // ------------------------
57 // - SvxColorValueSetData -
58 // ------------------------
60 class SvxColorValueSetData
: public TransferableHelper
64 XFillExchangeData maData
;
68 virtual void AddSupportedFormats();
69 virtual sal_Bool
GetData( const ::com::sun::star::datatransfer::DataFlavor
& rFlavor
);
70 virtual sal_Bool
WriteObject( SotStorageStreamRef
& rxOStm
, void* pUserObject
, sal_uInt32 nUserObjectId
, const ::com::sun::star::datatransfer::DataFlavor
& rFlavor
);
74 SvxColorValueSetData( const XFillAttrSetItem
& rSetItem
) :
78 // -----------------------------------------------------------------------------
80 void SvxColorValueSetData::AddSupportedFormats()
82 AddFormat( SOT_FORMATSTR_ID_XFA
);
85 // -----------------------------------------------------------------------------
87 sal_Bool
SvxColorValueSetData::GetData( const ::com::sun::star::datatransfer::DataFlavor
& rFlavor
)
89 sal_Bool bRet
= sal_False
;
91 if( SotExchange::GetFormat( rFlavor
) == SOT_FORMATSTR_ID_XFA
)
93 SetObject( &maData
, 0, rFlavor
);
100 // -----------------------------------------------------------------------------
102 sal_Bool
SvxColorValueSetData::WriteObject( SotStorageStreamRef
& rxOStm
, void*, sal_uInt32
, const ::com::sun::star::datatransfer::DataFlavor
& )
105 return( rxOStm
->GetError() == ERRCODE_NONE
);
108 /*************************************************************************
110 |* SvxColorValueSet: Ctor
112 \************************************************************************/
114 SvxColorValueSet::SvxColorValueSet( Window
* _pParent
, WinBits nWinStyle
) :
115 ValueSet( _pParent
, nWinStyle
),
116 DragSourceHelper( this ),
121 /*************************************************************************
123 |* SvxColorValueSet: Ctor
125 \************************************************************************/
127 SvxColorValueSet::SvxColorValueSet( Window
* _pParent
, const ResId
& rResId
) :
128 ValueSet( _pParent
, rResId
),
129 DragSourceHelper( this ),
134 /*************************************************************************
136 |* SvxColorValueSet: MouseButtonDown
138 \************************************************************************/
140 void SvxColorValueSet::MouseButtonDown( const MouseEvent
& rMEvt
)
142 // Fuer Mac noch anders handlen !
146 ValueSet::MouseButtonDown( rMEvt
);
151 MouseEvent
aMEvt( rMEvt
.GetPosPixel(),
155 rMEvt
.GetModifier() );
156 ValueSet::MouseButtonDown( aMEvt
);
159 aDragPosPixel
= GetPointerPosPixel();
162 /*************************************************************************
164 |* SvxColorValueSet: MouseButtonUp
166 \************************************************************************/
168 void SvxColorValueSet::MouseButtonUp( const MouseEvent
& rMEvt
)
170 // Fuer Mac noch anders handlen !
174 ValueSet::MouseButtonUp( rMEvt
);
179 MouseEvent
aMEvt( rMEvt
.GetPosPixel(),
183 rMEvt
.GetModifier() );
184 ValueSet::MouseButtonUp( aMEvt
);
189 /*************************************************************************
193 \************************************************************************/
195 void SvxColorValueSet::Command(const CommandEvent
& rCEvt
)
198 ValueSet::Command(rCEvt
);
201 /*************************************************************************
205 \************************************************************************/
207 void SvxColorValueSet::StartDrag( sal_Int8
, const Point
& )
209 Application::PostUserEvent(STATIC_LINK(this, SvxColorValueSet
, ExecDragHdl
));
212 /*************************************************************************
214 |* Drag&Drop asynchron ausfuehren
216 \************************************************************************/
218 void SvxColorValueSet::DoDrag()
220 SfxObjectShell
* pDocSh
= SfxObjectShell::Current();
221 USHORT nItemId
= GetItemId( aDragPosPixel
);
223 if( pDocSh
&& nItemId
)
225 XFillAttrSetItem
aXFillSetItem( &pDocSh
->GetPool() );
226 SfxItemSet
& rSet
= aXFillSetItem
.GetItemSet();
228 rSet
.Put( XFillColorItem( GetItemText( nItemId
), GetItemColor( nItemId
) ) );
229 rSet
.Put(XFillStyleItem( ( 1 == nItemId
) ? XFILL_NONE
: XFILL_SOLID
) );
232 ( new SvxColorValueSetData( aXFillSetItem
) )->StartDrag( this, DND_ACTION_COPY
);
237 /*************************************************************************
241 \************************************************************************/
243 IMPL_STATIC_LINK(SvxColorValueSet
, ExecDragHdl
, void*, EMPTYARG
)
245 // Als Link, damit asynchron ohne ImpMouseMoveMsg auf dem Stack auch die
246 // Farbleiste geloescht werden darf
251 /*************************************************************************
253 |* Ableitung vom SfxChildWindow als "Behaelter" fuer Animator
255 \************************************************************************/
257 SvxColorChildWindow::SvxColorChildWindow( Window
* _pParent
,
259 SfxBindings
* pBindings
,
260 SfxChildWinInfo
* pInfo
) :
261 SfxChildWindow( _pParent
, nId
)
263 SvxColorDockingWindow
* pWin
= new SvxColorDockingWindow( pBindings
, this,
264 _pParent
, SVX_RES( RID_SVXCTRL_COLOR
) );
267 eChildAlignment
= SFX_ALIGN_BOTTOM
;
269 pWin
->Initialize( pInfo
);
274 /*************************************************************************
276 |* Ctor: SvxColorDockingWindow
278 \************************************************************************/
280 SvxColorDockingWindow::SvxColorDockingWindow
282 SfxBindings
* _pBindings
,
288 SfxDockingWindow( _pBindings
, pCW
, _pParent
, rResId
),
290 pColorTable ( NULL
),
291 aColorSet ( this, ResId( 1, *rResId
.GetResMgr() ) ),
292 nLeftSlot ( SID_ATTR_FILL_COLOR
),
293 nRightSlot ( SID_ATTR_LINE_COLOR
),
296 aColorSize ( 14, 14 )
301 aColorSet
.SetStyle( aColorSet
.GetStyle() | WB_ITEMBORDER
);
302 aColorSet
.SetSelectHdl( LINK( this, SvxColorDockingWindow
, SelectHdl
) );
304 // Get the model from the view shell. Using SfxObjectShell::Current()
305 // is unreliable when called at the wrong times.
306 SfxObjectShell
* pDocSh
= NULL
;
307 if (_pBindings
!= NULL
)
309 SfxDispatcher
* pDispatcher
= _pBindings
->GetDispatcher();
310 if (pDispatcher
!= NULL
)
312 SfxViewFrame
* pFrame
= pDispatcher
->GetFrame();
315 SfxViewShell
* pViewShell
= pFrame
->GetViewShell();
316 if (pViewShell
!= NULL
)
317 pDocSh
= pViewShell
->GetObjectShell();
324 const SfxPoolItem
* pItem
= pDocSh
->GetItem( SID_COLOR_TABLE
);
327 pColorTable
= ( (SvxColorTableItem
*) pItem
)->GetColorTable();
331 aItemSize
= aColorSet
.CalcItemSizePixel( aColorSize
);
332 aItemSize
.Width() = aItemSize
.Width() + aColorSize
.Width();
333 aItemSize
.Width() /= 2;
334 aItemSize
.Height() = aItemSize
.Height() + aColorSize
.Height();
335 aItemSize
.Height() /= 2;
339 StartListening( *_pBindings
, TRUE
);
343 /*************************************************************************
345 |* Dtor: SvxColorDockingWindow
347 \************************************************************************/
349 SvxColorDockingWindow::~SvxColorDockingWindow()
351 EndListening( GetBindings() );
354 /*************************************************************************
358 \************************************************************************/
360 void SvxColorDockingWindow::Notify( SfxBroadcaster
& , const SfxHint
& rHint
)
362 const SfxPoolItemHint
*pPoolItemHint
= PTR_CAST(SfxPoolItemHint
, &rHint
);
364 && ( pPoolItemHint
->GetObject()->ISA( SvxColorTableItem
) ) )
366 // Die Liste der Farben hat sich geaendert
367 pColorTable
= ( (SvxColorTableItem
*) pPoolItemHint
->GetObject() )->GetColorTable();
372 /*************************************************************************
376 \************************************************************************/
378 void SvxColorDockingWindow::FillValueSet()
384 // Erster Eintrag: unsichtbar
385 long nPtX
= aColorSize
.Width() - 1;
386 long nPtY
= aColorSize
.Height() - 1;
388 aVD
.SetOutputSizePixel( aColorSize
);
389 aVD
.SetLineColor( Color( COL_BLACK
) );
390 aVD
.SetBackground( Wallpaper( Color( COL_WHITE
) ) );
391 aVD
.DrawLine( Point(), Point( nPtX
, nPtY
) );
392 aVD
.DrawLine( Point( 0, nPtY
), Point( nPtX
, 0 ) );
394 Bitmap
aBmp( aVD
.GetBitmap( Point(), aColorSize
) );
396 aColorSet
.InsertItem( (USHORT
)1, Image(aBmp
), SVX_RESSTR( RID_SVXSTR_INVISIBLE
) );
399 nCount
= pColorTable
->Count();
401 for( long i
= 0; i
< nCount
; i
++ )
403 pEntry
= pColorTable
->GetColor( i
);
404 aColorSet
.InsertItem( (USHORT
)i
+2,
405 pEntry
->GetColor(), pEntry
->GetName() );
410 /*************************************************************************
414 \************************************************************************/
416 void SvxColorDockingWindow::SetSize()
418 // Groesse fuer ValueSet berechnen
419 Size aSize
= GetOutputSizePixel();
423 // Zeilen und Spalten berechnen
424 nCols
= (USHORT
) ( aSize
.Width() / aItemSize
.Width() );
425 nLines
= (USHORT
) ( (float) aSize
.Height() / (float) aItemSize
.Height() /*+ 0.35*/ );
429 // Scrollbar setzen/entfernen
430 WinBits nBits
= aColorSet
.GetStyle();
431 if ( nLines
* nCols
>= nCount
)
432 nBits
&= ~WB_VSCROLL
;
435 aColorSet
.SetStyle( nBits
);
438 long nScrollWidth
= aColorSet
.GetScrollWidth();
439 if( nScrollWidth
> 0 )
441 // Spalten mit ScrollBar berechnen
442 nCols
= (USHORT
) ( ( aSize
.Width() - nScrollWidth
) / aItemSize
.Width() );
444 aColorSet
.SetColCount( nCols
);
446 if( IsFloatingMode() )
447 aColorSet
.SetLineCount( nLines
);
450 aColorSet
.SetLineCount( 0 ); // sonst wird LineHeight ignoriert
451 aColorSet
.SetItemHeight( aItemSize
.Height() );
454 aColorSet
.SetPosSizePixel( Point( 2, 2 ), aSize
);
457 /*************************************************************************
459 |* SvxColorDockingWindow: Close
461 \************************************************************************/
463 BOOL
SvxColorDockingWindow::Close()
465 SfxBoolItem
aItem( SID_COLOR_CONTROL
, FALSE
);
466 GetBindings().GetDispatcher()->Execute(
467 SID_COLOR_CONTROL
, SFX_CALLMODE_ASYNCHRON
| SFX_CALLMODE_RECORD
, &aItem
, 0L );
468 SfxDockingWindow::Close();
472 /*************************************************************************
476 \************************************************************************/
478 IMPL_LINK( SvxColorDockingWindow
, SelectHdl
, void *, EMPTYARG
)
480 SfxDispatcher
* pDispatcher
= GetBindings().GetDispatcher();
481 USHORT nPos
= aColorSet
.GetSelectItemId();
482 Color
aColor( aColorSet
.GetItemColor( nPos
) );
483 String
aStr( aColorSet
.GetItemText( nPos
) );
485 if (aColorSet
.IsLeftButton())
487 if ( nLeftSlot
== SID_ATTR_FILL_COLOR
)
489 if ( nPos
== 1 ) // unsichtbar
491 XFillStyleItem
aXFillStyleItem( XFILL_NONE
);
492 pDispatcher
->Execute( nLeftSlot
, SFX_CALLMODE_RECORD
, &aXFillStyleItem
, 0L );
498 // Wenn wir eine DrawView haben und uns im TextEdit-Modus befinden,
499 // wird nicht die Flaechen-, sondern die Textfarbe zugewiesen
500 SfxViewShell
* pViewSh
= SfxViewShell::Current();
503 SdrView
* pView
= pViewSh
->GetDrawView();
504 if ( pView
&& pView
->IsTextEdit() )
506 SvxColorItem
aTextColorItem( aColor
, SID_ATTR_CHAR_COLOR
);
507 pDispatcher
->Execute(
508 SID_ATTR_CHAR_COLOR
, SFX_CALLMODE_RECORD
, &aTextColorItem
, 0L );
514 XFillStyleItem
aXFillStyleItem( XFILL_SOLID
);
515 XFillColorItem
aXFillColorItem( aStr
, aColor
);
516 pDispatcher
->Execute(
517 nLeftSlot
, SFX_CALLMODE_RECORD
, &aXFillColorItem
, &aXFillStyleItem
, 0L );
521 else if ( nPos
!= 1 ) // unsichtbar
523 SvxColorItem
aLeftColorItem( aColor
, nLeftSlot
);
524 pDispatcher
->Execute( nLeftSlot
, SFX_CALLMODE_RECORD
, &aLeftColorItem
, 0L );
529 if ( nRightSlot
== SID_ATTR_LINE_COLOR
)
531 if( nPos
== 1 ) // unsichtbar
533 XLineStyleItem
aXLineStyleItem( XLINE_NONE
);
534 pDispatcher
->Execute( nRightSlot
, SFX_CALLMODE_RECORD
, &aXLineStyleItem
, 0L );
538 // Sollte der LineStyle unsichtbar sein, so wird er auf SOLID gesetzt
539 SfxViewShell
* pViewSh
= SfxViewShell::Current();
542 SdrView
* pView
= pViewSh
->GetDrawView();
545 SfxItemSet
aAttrSet( pView
->GetModel()->GetItemPool() );
546 pView
->GetAttributes( aAttrSet
);
547 if ( aAttrSet
.GetItemState( XATTR_LINESTYLE
) != SFX_ITEM_DONTCARE
)
549 XLineStyle eXLS
= (XLineStyle
)
550 ( (const XLineStyleItem
&)aAttrSet
.Get( XATTR_LINESTYLE
) ).GetValue();
551 if ( eXLS
== XLINE_NONE
)
553 XLineStyleItem
aXLineStyleItem( XLINE_SOLID
);
554 pDispatcher
->Execute( nRightSlot
, SFX_CALLMODE_RECORD
, &aXLineStyleItem
, 0L );
560 XLineColorItem
aXLineColorItem( aStr
, aColor
);
561 pDispatcher
->Execute( nRightSlot
, SFX_CALLMODE_RECORD
, &aXLineColorItem
, 0L );
564 else if ( nPos
!= 1 ) // unsichtbar
566 SvxColorItem
aRightColorItem( aColor
, nRightSlot
);
567 pDispatcher
->Execute( nRightSlot
, SFX_CALLMODE_RECORD
, &aRightColorItem
, 0L );
574 /*************************************************************************
578 \************************************************************************/
581 void SvxColorDockingWindow::Resizing( Size
& rNewSize
)
583 rNewSize
.Width() -= 4;
584 rNewSize
.Height() -= 4;
586 // Spalten und Reihen ermitteln
587 nCols
= (USHORT
) ( (float) rNewSize
.Width() / (float) aItemSize
.Width() + 0.5 );
588 nLines
= (USHORT
) ( (float) rNewSize
.Height() / (float) aItemSize
.Height() + 0.5 );
592 // Scrollbar setzen/entfernen
593 WinBits nBits
= aColorSet
.GetStyle();
594 if ( nLines
* nCols
>= nCount
)
595 nBits
&= ~WB_VSCROLL
;
598 aColorSet
.SetStyle( nBits
);
601 long nScrollWidth
= aColorSet
.GetScrollWidth();
602 if( nScrollWidth
> 0 )
604 // Spalten mit ScrollBar berechnen
605 nCols
= (USHORT
) ( ( ( (float) rNewSize
.Width() - (float) nScrollWidth
) )
606 / (float) aItemSize
.Width() + 0.5 );
611 // Max. Reihen anhand der gegebenen Spalten berechnen
612 long nMaxLines
= nCount
/ nCols
;
616 nLines
= sal::static_int_cast
< USHORT
>(
617 std::min
< long >( nLines
, nMaxLines
) );
619 // Groesse des Windows setzen
620 rNewSize
.Width() = nCols
* aItemSize
.Width() + nScrollWidth
+ 4;
621 rNewSize
.Height() = nLines
* aItemSize
.Height() + 4;
624 /*************************************************************************
628 \************************************************************************/
630 void SvxColorDockingWindow::Resize()
632 if ( !IsFloatingMode() || !GetFloatingWindow()->IsRollUp() )
634 SfxDockingWindow::Resize();
639 void SvxColorDockingWindow::GetFocus (void)
641 SfxDockingWindow::GetFocus();
642 // Grab the focus to the color value set so that it can be controlled
643 // with the keyboard.
644 aColorSet
.GrabFocus();
647 long SvxColorDockingWindow::Notify( NotifyEvent
& rNEvt
)
650 if( ( rNEvt
.GetType() == EVENT_KEYINPUT
) )
652 KeyEvent aKeyEvt
= *rNEvt
.GetKeyEvent();
653 USHORT nKeyCode
= aKeyEvt
.GetKeyCode().GetCode();
657 GrabFocusToDocument();
663 return nRet
? nRet
: SfxDockingWindow::Notify( rNEvt
);