update dev300-m58
[ooovba.git] / svx / source / tbxctrls / colrctrl.cxx
blobc7df83a5257f6d1fb3223867bd5e4883aef9b8e1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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>
48 #include "xattr.hxx"
49 #include <svx/xtable.hxx>
50 #include <svx/dialmgr.hxx>
51 #include "xexch.hxx"
52 #include <vcl/svapp.hxx>
54 SFX_IMPL_DOCKINGWINDOW( SvxColorChildWindow, SID_COLOR_CONTROL )
56 // ------------------------
57 // - SvxColorValueSetData -
58 // ------------------------
60 class SvxColorValueSetData : public TransferableHelper
62 private:
64 XFillExchangeData maData;
66 protected:
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 );
72 public:
74 SvxColorValueSetData( const XFillAttrSetItem& rSetItem ) :
75 maData( 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 );
94 bRet = sal_True;
97 return bRet;
100 // -----------------------------------------------------------------------------
102 sal_Bool SvxColorValueSetData::WriteObject( SotStorageStreamRef& rxOStm, void*, sal_uInt32 , const ::com::sun::star::datatransfer::DataFlavor& )
104 *rxOStm << maData;
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 ),
117 bLeft (TRUE)
121 /*************************************************************************
123 |* SvxColorValueSet: Ctor
125 \************************************************************************/
127 SvxColorValueSet::SvxColorValueSet( Window* _pParent, const ResId& rResId ) :
128 ValueSet( _pParent, rResId ),
129 DragSourceHelper( this ),
130 bLeft (TRUE)
134 /*************************************************************************
136 |* SvxColorValueSet: MouseButtonDown
138 \************************************************************************/
140 void SvxColorValueSet::MouseButtonDown( const MouseEvent& rMEvt )
142 // Fuer Mac noch anders handlen !
143 if( rMEvt.IsLeft() )
145 bLeft = TRUE;
146 ValueSet::MouseButtonDown( rMEvt );
148 else
150 bLeft = FALSE;
151 MouseEvent aMEvt( rMEvt.GetPosPixel(),
152 rMEvt.GetClicks(),
153 rMEvt.GetMode(),
154 MOUSE_LEFT,
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 !
171 if( rMEvt.IsLeft() )
173 bLeft = TRUE;
174 ValueSet::MouseButtonUp( rMEvt );
176 else
178 bLeft = FALSE;
179 MouseEvent aMEvt( rMEvt.GetPosPixel(),
180 rMEvt.GetClicks(),
181 rMEvt.GetMode(),
182 MOUSE_LEFT,
183 rMEvt.GetModifier() );
184 ValueSet::MouseButtonUp( aMEvt );
186 SetNoSelection();
189 /*************************************************************************
191 |* Command-Event
193 \************************************************************************/
195 void SvxColorValueSet::Command(const CommandEvent& rCEvt)
197 // Basisklasse
198 ValueSet::Command(rCEvt);
201 /*************************************************************************
203 |* StartDrag
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 ) );
231 EndSelection();
232 ( new SvxColorValueSetData( aXFillSetItem ) )->StartDrag( this, DND_ACTION_COPY );
233 ReleaseMouse();
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
247 pThis->DoDrag();
248 return(0);
251 /*************************************************************************
253 |* Ableitung vom SfxChildWindow als "Behaelter" fuer Animator
255 \************************************************************************/
257 SvxColorChildWindow::SvxColorChildWindow( Window* _pParent,
258 USHORT nId,
259 SfxBindings* pBindings,
260 SfxChildWinInfo* pInfo ) :
261 SfxChildWindow( _pParent, nId )
263 SvxColorDockingWindow* pWin = new SvxColorDockingWindow( pBindings, this,
264 _pParent, SVX_RES( RID_SVXCTRL_COLOR ) );
265 pWindow = pWin;
267 eChildAlignment = SFX_ALIGN_BOTTOM;
269 pWin->Initialize( pInfo );
274 /*************************************************************************
276 |* Ctor: SvxColorDockingWindow
278 \************************************************************************/
280 SvxColorDockingWindow::SvxColorDockingWindow
282 SfxBindings* _pBindings,
283 SfxChildWindow* pCW,
284 Window* _pParent,
285 const ResId& rResId
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 ),
294 nCols ( 20 ),
295 nLines ( 1 ),
296 aColorSize ( 14, 14 )
299 FreeResource();
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();
313 if (pFrame != NULL)
315 SfxViewShell* pViewShell = pFrame->GetViewShell();
316 if (pViewShell != NULL)
317 pDocSh = pViewShell->GetObjectShell();
322 if ( pDocSh )
324 const SfxPoolItem* pItem = pDocSh->GetItem( SID_COLOR_TABLE );
325 if( pItem )
327 pColorTable = ( (SvxColorTableItem*) pItem )->GetColorTable();
328 FillValueSet();
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;
337 SetSize();
338 aColorSet.Show();
339 StartListening( *_pBindings, TRUE );
343 /*************************************************************************
345 |* Dtor: SvxColorDockingWindow
347 \************************************************************************/
349 SvxColorDockingWindow::~SvxColorDockingWindow()
351 EndListening( GetBindings() );
354 /*************************************************************************
356 |* Notify
358 \************************************************************************/
360 void SvxColorDockingWindow::Notify( SfxBroadcaster& , const SfxHint& rHint )
362 const SfxPoolItemHint *pPoolItemHint = PTR_CAST(SfxPoolItemHint, &rHint);
363 if ( pPoolItemHint
364 && ( pPoolItemHint->GetObject()->ISA( SvxColorTableItem ) ) )
366 // Die Liste der Farben hat sich geaendert
367 pColorTable = ( (SvxColorTableItem*) pPoolItemHint->GetObject() )->GetColorTable();
368 FillValueSet();
372 /*************************************************************************
374 |* FillValueSet
376 \************************************************************************/
378 void SvxColorDockingWindow::FillValueSet()
380 if( pColorTable )
382 aColorSet.Clear();
384 // Erster Eintrag: unsichtbar
385 long nPtX = aColorSize.Width() - 1;
386 long nPtY = aColorSize.Height() - 1;
387 VirtualDevice aVD;
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 ) );
398 XColorEntry* pEntry;
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 /*************************************************************************
412 |* SetSize
414 \************************************************************************/
416 void SvxColorDockingWindow::SetSize()
418 // Groesse fuer ValueSet berechnen
419 Size aSize = GetOutputSizePixel();
420 aSize.Width() -= 4;
421 aSize.Height() -= 4;
423 // Zeilen und Spalten berechnen
424 nCols = (USHORT) ( aSize.Width() / aItemSize.Width() );
425 nLines = (USHORT) ( (float) aSize.Height() / (float) aItemSize.Height() /*+ 0.35*/ );
426 if( nLines == 0 )
427 nLines++;
429 // Scrollbar setzen/entfernen
430 WinBits nBits = aColorSet.GetStyle();
431 if ( nLines * nCols >= nCount )
432 nBits &= ~WB_VSCROLL;
433 else
434 nBits |= WB_VSCROLL;
435 aColorSet.SetStyle( nBits );
437 // ScrollBar ?
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 );
448 else
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();
469 return( TRUE );
472 /*************************************************************************
474 |* SelectHdl
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 );
494 else
496 BOOL bDone = FALSE;
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();
501 if ( pViewSh )
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 );
509 bDone = TRUE;
512 if ( !bDone )
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 );
527 else
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 );
536 else
538 // Sollte der LineStyle unsichtbar sein, so wird er auf SOLID gesetzt
539 SfxViewShell* pViewSh = SfxViewShell::Current();
540 if ( pViewSh )
542 SdrView* pView = pViewSh->GetDrawView();
543 if ( pView )
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 );
571 return 0;
574 /*************************************************************************
576 |* Resizing
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 );
589 if( nLines == 0 )
590 nLines = 1;
592 // Scrollbar setzen/entfernen
593 WinBits nBits = aColorSet.GetStyle();
594 if ( nLines * nCols >= nCount )
595 nBits &= ~WB_VSCROLL;
596 else
597 nBits |= WB_VSCROLL;
598 aColorSet.SetStyle( nBits );
600 // ScrollBar ?
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 );
608 if( nCols <= 1 )
609 nCols = 2;
611 // Max. Reihen anhand der gegebenen Spalten berechnen
612 long nMaxLines = nCount / nCols;
613 if( nCount % nCols )
614 nMaxLines++;
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 /*************************************************************************
626 |* Resize
628 \************************************************************************/
630 void SvxColorDockingWindow::Resize()
632 if ( !IsFloatingMode() || !GetFloatingWindow()->IsRollUp() )
633 SetSize();
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 )
649 long nRet = 0;
650 if( ( rNEvt.GetType() == EVENT_KEYINPUT ) )
652 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
653 USHORT nKeyCode = aKeyEvt.GetKeyCode().GetCode();
654 switch( nKeyCode )
656 case KEY_ESCAPE:
657 GrabFocusToDocument();
658 nRet = 1;
659 break;
663 return nRet ? nRet : SfxDockingWindow::Notify( rNEvt );