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 :
21 : #include <svx/dialogs.hrc>
22 :
23 : #include <sfx2/viewsh.hxx>
24 : #include <sfx2/objsh.hxx>
25 : #include <sfx2/dispatch.hxx>
26 : #include <vcl/image.hxx>
27 :
28 : #include <svx/colrctrl.hxx>
29 :
30 : #include <svx/svdview.hxx>
31 : #include "svx/drawitem.hxx"
32 : #include <editeng/colritem.hxx>
33 : #include "svx/xattr.hxx"
34 : #include <svx/xtable.hxx>
35 : #include <svx/dialmgr.hxx>
36 : #include "svx/xexch.hxx"
37 : #include <vcl/svapp.hxx>
38 :
39 : // ------------------------
40 : // - SvxColorValueSetData -
41 : // ------------------------
42 :
43 0 : class SvxColorValueSetData : public TransferableHelper
44 : {
45 : private:
46 :
47 : XFillExchangeData maData;
48 :
49 : protected:
50 :
51 : virtual void AddSupportedFormats();
52 : virtual sal_Bool GetData( const ::com::sun::star::datatransfer::DataFlavor& rFlavor );
53 : virtual sal_Bool WriteObject( SotStorageStreamRef& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId, const ::com::sun::star::datatransfer::DataFlavor& rFlavor );
54 :
55 : public:
56 :
57 0 : SvxColorValueSetData( const XFillAttrSetItem& rSetItem ) :
58 0 : maData( rSetItem ) {}
59 : };
60 :
61 : // -----------------------------------------------------------------------------
62 :
63 0 : void SvxColorValueSetData::AddSupportedFormats()
64 : {
65 0 : AddFormat( SOT_FORMATSTR_ID_XFA );
66 0 : }
67 :
68 : // -----------------------------------------------------------------------------
69 :
70 0 : sal_Bool SvxColorValueSetData::GetData( const ::com::sun::star::datatransfer::DataFlavor& rFlavor )
71 : {
72 0 : sal_Bool bRet = sal_False;
73 :
74 0 : if( SotExchange::GetFormat( rFlavor ) == SOT_FORMATSTR_ID_XFA )
75 : {
76 0 : SetObject( &maData, 0, rFlavor );
77 0 : bRet = sal_True;
78 : }
79 :
80 0 : return bRet;
81 : }
82 :
83 : // -----------------------------------------------------------------------------
84 :
85 0 : sal_Bool SvxColorValueSetData::WriteObject( SotStorageStreamRef& rxOStm, void*, sal_uInt32 , const ::com::sun::star::datatransfer::DataFlavor& )
86 : {
87 0 : *rxOStm << maData;
88 0 : return( rxOStm->GetError() == ERRCODE_NONE );
89 : }
90 :
91 : /*************************************************************************
92 : |*
93 : |* SvxColorValueSet_docking: Ctor
94 : |*
95 : \************************************************************************/
96 :
97 0 : SvxColorValueSet_docking::SvxColorValueSet_docking( Window* _pParent, const ResId& rResId ) :
98 : SvxColorValueSet( _pParent, rResId ),
99 : DragSourceHelper( this ),
100 0 : mbLeftButton(true)
101 : {
102 0 : SetAccessibleName(String( SVX_RES( STR_COLORTABLE ) ));
103 0 : }
104 :
105 : /*************************************************************************
106 : |*
107 : |* SvxColorValueSet_docking: MouseButtonDown
108 : |*
109 : \************************************************************************/
110 :
111 0 : void SvxColorValueSet_docking::MouseButtonDown( const MouseEvent& rMEvt )
112 : {
113 : // Fuer Mac noch anders handlen !
114 0 : if( rMEvt.IsLeft() )
115 : {
116 0 : mbLeftButton = true;
117 0 : SvxColorValueSet::MouseButtonDown( rMEvt );
118 : }
119 : else
120 : {
121 0 : mbLeftButton = false;
122 0 : MouseEvent aMEvt( rMEvt.GetPosPixel(),
123 0 : rMEvt.GetClicks(),
124 0 : rMEvt.GetMode(),
125 : MOUSE_LEFT,
126 0 : rMEvt.GetModifier() );
127 0 : SvxColorValueSet::MouseButtonDown( aMEvt );
128 : }
129 :
130 0 : aDragPosPixel = GetPointerPosPixel();
131 0 : }
132 :
133 : /*************************************************************************
134 : |*
135 : |* SvxColorValueSet_docking: MouseButtonUp
136 : |*
137 : \************************************************************************/
138 :
139 0 : void SvxColorValueSet_docking::MouseButtonUp( const MouseEvent& rMEvt )
140 : {
141 : // Fuer Mac noch anders handlen !
142 0 : if( rMEvt.IsLeft() )
143 : {
144 0 : mbLeftButton = true;
145 0 : SvxColorValueSet::MouseButtonUp( rMEvt );
146 : }
147 : else
148 : {
149 0 : mbLeftButton = false;
150 0 : MouseEvent aMEvt( rMEvt.GetPosPixel(),
151 0 : rMEvt.GetClicks(),
152 0 : rMEvt.GetMode(),
153 : MOUSE_LEFT,
154 0 : rMEvt.GetModifier() );
155 0 : SvxColorValueSet::MouseButtonUp( aMEvt );
156 : }
157 0 : SetNoSelection();
158 0 : }
159 :
160 : /*************************************************************************
161 : |*
162 : |* Command-Event
163 : |*
164 : \************************************************************************/
165 :
166 0 : void SvxColorValueSet_docking::Command(const CommandEvent& rCEvt)
167 : {
168 : // Basisklasse
169 0 : SvxColorValueSet::Command(rCEvt);
170 0 : }
171 :
172 : /*************************************************************************
173 : |*
174 : |* StartDrag
175 : |*
176 : \************************************************************************/
177 :
178 0 : void SvxColorValueSet_docking::StartDrag( sal_Int8 , const Point& )
179 : {
180 0 : Application::PostUserEvent(STATIC_LINK(this, SvxColorValueSet_docking, ExecDragHdl));
181 0 : }
182 :
183 : /*************************************************************************
184 : |*
185 : |* Drag&Drop asynchron ausfuehren
186 : |*
187 : \************************************************************************/
188 :
189 0 : void SvxColorValueSet_docking::DoDrag()
190 : {
191 0 : SfxObjectShell* pDocSh = SfxObjectShell::Current();
192 0 : sal_uInt16 nItemId = GetItemId( aDragPosPixel );
193 :
194 0 : if( pDocSh && nItemId )
195 : {
196 0 : XFillAttrSetItem aXFillSetItem( &pDocSh->GetPool() );
197 0 : SfxItemSet& rSet = aXFillSetItem.GetItemSet();
198 :
199 0 : rSet.Put( XFillColorItem( GetItemText( nItemId ), GetItemColor( nItemId ) ) );
200 0 : rSet.Put(XFillStyleItem( ( 1 == nItemId ) ? XFILL_NONE : XFILL_SOLID ) );
201 :
202 0 : EndSelection();
203 0 : ( new SvxColorValueSetData( aXFillSetItem ) )->StartDrag( this, DND_ACTION_COPY );
204 0 : ReleaseMouse();
205 : }
206 0 : }
207 :
208 0 : IMPL_STATIC_LINK(SvxColorValueSet_docking, ExecDragHdl, void*, EMPTYARG)
209 : {
210 : // Als Link, damit asynchron ohne ImpMouseMoveMsg auf dem Stack auch die
211 : // Farbleiste geloescht werden darf
212 0 : pThis->DoDrag();
213 0 : return(0);
214 : }
215 :
216 : /*************************************************************************
217 : |*
218 : |* Ctor: SvxColorDockingWindow
219 : |*
220 : \************************************************************************/
221 :
222 0 : SvxColorDockingWindow::SvxColorDockingWindow
223 : (
224 : SfxBindings* _pBindings,
225 : SfxChildWindow* pCW,
226 : Window* _pParent,
227 : const ResId& rResId
228 : ) :
229 :
230 : SfxDockingWindow( _pBindings, pCW, _pParent, rResId ),
231 : pColorList (),
232 0 : aColorSet ( this, ResId( 1, *rResId.GetResMgr() ) ),
233 : nLeftSlot ( SID_ATTR_FILL_COLOR ),
234 : nRightSlot ( SID_ATTR_LINE_COLOR ),
235 : nCols ( 20 ),
236 0 : nLines ( 1 )
237 :
238 : {
239 0 : FreeResource();
240 :
241 0 : aColorSet.SetStyle( aColorSet.GetStyle() | WB_ITEMBORDER );
242 0 : aColorSet.SetSelectHdl( LINK( this, SvxColorDockingWindow, SelectHdl ) );
243 :
244 : // Get the model from the view shell. Using SfxObjectShell::Current()
245 : // is unreliable when called at the wrong times.
246 0 : SfxObjectShell* pDocSh = NULL;
247 0 : if (_pBindings != NULL)
248 : {
249 0 : SfxDispatcher* pDispatcher = _pBindings->GetDispatcher();
250 0 : if (pDispatcher != NULL)
251 : {
252 0 : SfxViewFrame* pFrame = pDispatcher->GetFrame();
253 0 : if (pFrame != NULL)
254 : {
255 0 : SfxViewShell* pViewShell = pFrame->GetViewShell();
256 0 : if (pViewShell != NULL)
257 0 : pDocSh = pViewShell->GetObjectShell();
258 : }
259 : }
260 : }
261 :
262 0 : if ( pDocSh )
263 : {
264 0 : const SfxPoolItem* pItem = pDocSh->GetItem( SID_COLOR_TABLE );
265 0 : if( pItem )
266 : {
267 0 : pColorList = ( (SvxColorListItem*) pItem )->GetColorList();
268 0 : FillValueSet();
269 : }
270 : }
271 :
272 0 : aItemSize = aColorSet.CalcItemSizePixel(Size(aColorSet.getEntryEdgeLength(), aColorSet.getEntryEdgeLength()));
273 0 : aItemSize.Width() = aItemSize.Width() + aColorSet.getEntryEdgeLength();
274 0 : aItemSize.Width() /= 2;
275 0 : aItemSize.Height() = aItemSize.Height() + aColorSet.getEntryEdgeLength();
276 0 : aItemSize.Height() /= 2;
277 :
278 0 : SetSize();
279 0 : aColorSet.Show();
280 0 : StartListening( *_pBindings, sal_True );
281 0 : }
282 :
283 :
284 : /*************************************************************************
285 : |*
286 : |* Dtor: SvxColorDockingWindow
287 : |*
288 : \************************************************************************/
289 :
290 0 : SvxColorDockingWindow::~SvxColorDockingWindow()
291 : {
292 0 : EndListening( GetBindings() );
293 0 : }
294 :
295 : /*************************************************************************
296 : |*
297 : |* Notify
298 : |*
299 : \************************************************************************/
300 :
301 0 : void SvxColorDockingWindow::Notify( SfxBroadcaster& , const SfxHint& rHint )
302 : {
303 0 : const SfxPoolItemHint *pPoolItemHint = PTR_CAST(SfxPoolItemHint, &rHint);
304 0 : if ( pPoolItemHint
305 0 : && ( pPoolItemHint->GetObject()->ISA( SvxColorListItem ) ) )
306 : {
307 : // Die Liste der Farben hat sich geaendert
308 0 : pColorList = ( (SvxColorListItem*) pPoolItemHint->GetObject() )->GetColorList();
309 0 : FillValueSet();
310 : }
311 0 : }
312 :
313 : /*************************************************************************
314 : |*
315 : |* FillValueSet
316 : |*
317 : \************************************************************************/
318 :
319 0 : void SvxColorDockingWindow::FillValueSet()
320 : {
321 0 : if( pColorList.is() )
322 : {
323 0 : nCount = pColorList->Count();
324 0 : aColorSet.Clear();
325 :
326 : // create the first entry for 'invisible/none'
327 0 : const Size aColorSize(aColorSet.getEntryEdgeLength(), aColorSet.getEntryEdgeLength());
328 0 : long nPtX = aColorSize.Width() - 1;
329 0 : long nPtY = aColorSize.Height() - 1;
330 0 : VirtualDevice aVD;
331 :
332 0 : aVD.SetOutputSizePixel( aColorSize );
333 0 : aVD.SetLineColor( Color( COL_BLACK ) );
334 0 : aVD.SetBackground( Wallpaper( Color( COL_WHITE ) ) );
335 0 : aVD.DrawLine( Point(), Point( nPtX, nPtY ) );
336 0 : aVD.DrawLine( Point( 0, nPtY ), Point( nPtX, 0 ) );
337 :
338 0 : Bitmap aBmp( aVD.GetBitmap( Point(), aColorSize ) );
339 :
340 0 : aColorSet.InsertItem( (sal_uInt16)1, Image(aBmp), SVX_RESSTR( RID_SVXSTR_INVISIBLE ) );
341 :
342 0 : aColorSet.addEntriesForXColorList(*pColorList, 2);
343 : }
344 0 : }
345 :
346 : /*************************************************************************
347 : |*
348 : |* SetSize
349 : |*
350 : \************************************************************************/
351 :
352 0 : void SvxColorDockingWindow::SetSize()
353 : {
354 : // Groesse fuer ValueSet berechnen
355 0 : Size aSize = GetOutputSizePixel();
356 0 : aSize.Width() -= 4;
357 0 : aSize.Height() -= 4;
358 :
359 : // Zeilen und Spalten berechnen
360 0 : nCols = (sal_uInt16) ( aSize.Width() / aItemSize.Width() );
361 0 : nLines = (sal_uInt16) ( (float) aSize.Height() / (float) aItemSize.Height() /*+ 0.35*/ );
362 0 : if( nLines == 0 )
363 0 : nLines++;
364 :
365 : // Scrollbar setzen/entfernen
366 0 : WinBits nBits = aColorSet.GetStyle();
367 0 : if ( nLines * nCols >= nCount )
368 0 : nBits &= ~WB_VSCROLL;
369 : else
370 0 : nBits |= WB_VSCROLL;
371 0 : aColorSet.SetStyle( nBits );
372 :
373 : // ScrollBar ?
374 0 : long nScrollWidth = aColorSet.GetScrollWidth();
375 0 : if( nScrollWidth > 0 )
376 : {
377 : // Spalten mit ScrollBar berechnen
378 0 : nCols = (sal_uInt16) ( ( aSize.Width() - nScrollWidth ) / aItemSize.Width() );
379 : }
380 0 : aColorSet.SetColCount( nCols );
381 :
382 0 : if( IsFloatingMode() )
383 0 : aColorSet.SetLineCount( nLines );
384 : else
385 : {
386 0 : aColorSet.SetLineCount( 0 ); // sonst wird LineHeight ignoriert
387 0 : aColorSet.SetItemHeight( aItemSize.Height() );
388 : }
389 :
390 0 : aColorSet.SetPosSizePixel( Point( 2, 2 ), aSize );
391 0 : }
392 :
393 : /*************************************************************************
394 : |*
395 : |* SvxColorDockingWindow: Close
396 : |*
397 : \************************************************************************/
398 :
399 0 : sal_Bool SvxColorDockingWindow::Close()
400 : {
401 0 : SfxBoolItem aItem( SID_COLOR_CONTROL, sal_False );
402 0 : GetBindings().GetDispatcher()->Execute(
403 0 : SID_COLOR_CONTROL, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD, &aItem, 0L );
404 0 : SfxDockingWindow::Close();
405 0 : return( sal_True );
406 : }
407 :
408 : /*************************************************************************
409 : |*
410 : |* SelectHdl
411 : |*
412 : \************************************************************************/
413 :
414 0 : IMPL_LINK_NOARG(SvxColorDockingWindow, SelectHdl)
415 : {
416 0 : SfxDispatcher* pDispatcher = GetBindings().GetDispatcher();
417 0 : sal_uInt16 nPos = aColorSet.GetSelectItemId();
418 0 : Color aColor( aColorSet.GetItemColor( nPos ) );
419 0 : String aStr( aColorSet.GetItemText( nPos ) );
420 :
421 0 : if (aColorSet.IsLeftButton())
422 : {
423 0 : if ( nLeftSlot == SID_ATTR_FILL_COLOR )
424 : {
425 0 : if ( nPos == 1 ) // unsichtbar
426 : {
427 0 : XFillStyleItem aXFillStyleItem( XFILL_NONE );
428 0 : pDispatcher->Execute( nLeftSlot, SFX_CALLMODE_RECORD, &aXFillStyleItem, 0L );
429 : }
430 : else
431 : {
432 0 : bool bDone = false;
433 :
434 : // Wenn wir eine DrawView haben und uns im TextEdit-Modus befinden,
435 : // wird nicht die Flaechen-, sondern die Textfarbe zugewiesen
436 0 : SfxViewShell* pViewSh = SfxViewShell::Current();
437 0 : if ( pViewSh )
438 : {
439 0 : SdrView* pView = pViewSh->GetDrawView();
440 0 : if ( pView && pView->IsTextEdit() )
441 : {
442 0 : SvxColorItem aTextColorItem( aColor, SID_ATTR_CHAR_COLOR );
443 : pDispatcher->Execute(
444 0 : SID_ATTR_CHAR_COLOR, SFX_CALLMODE_RECORD, &aTextColorItem, 0L );
445 0 : bDone = true;
446 : }
447 : }
448 0 : if ( !bDone )
449 : {
450 0 : XFillStyleItem aXFillStyleItem( XFILL_SOLID );
451 0 : XFillColorItem aXFillColorItem( aStr, aColor );
452 : pDispatcher->Execute(
453 0 : nLeftSlot, SFX_CALLMODE_RECORD, &aXFillColorItem, &aXFillStyleItem, 0L );
454 : }
455 : }
456 : }
457 0 : else if ( nPos != 1 ) // unsichtbar
458 : {
459 0 : SvxColorItem aLeftColorItem( aColor, nLeftSlot );
460 0 : pDispatcher->Execute( nLeftSlot, SFX_CALLMODE_RECORD, &aLeftColorItem, 0L );
461 : }
462 : }
463 : else
464 : {
465 0 : if ( nRightSlot == SID_ATTR_LINE_COLOR )
466 : {
467 0 : if( nPos == 1 ) // unsichtbar
468 : {
469 0 : XLineStyleItem aXLineStyleItem( XLINE_NONE );
470 0 : pDispatcher->Execute( nRightSlot, SFX_CALLMODE_RECORD, &aXLineStyleItem, 0L );
471 : }
472 : else
473 : {
474 : // Sollte der LineStyle unsichtbar sein, so wird er auf SOLID gesetzt
475 0 : SfxViewShell* pViewSh = SfxViewShell::Current();
476 0 : if ( pViewSh )
477 : {
478 0 : SdrView* pView = pViewSh->GetDrawView();
479 0 : if ( pView )
480 : {
481 0 : SfxItemSet aAttrSet( pView->GetModel()->GetItemPool() );
482 0 : pView->GetAttributes( aAttrSet );
483 0 : if ( aAttrSet.GetItemState( XATTR_LINESTYLE ) != SFX_ITEM_DONTCARE )
484 : {
485 : XLineStyle eXLS = (XLineStyle)
486 0 : ( (const XLineStyleItem&)aAttrSet.Get( XATTR_LINESTYLE ) ).GetValue();
487 0 : if ( eXLS == XLINE_NONE )
488 : {
489 0 : XLineStyleItem aXLineStyleItem( XLINE_SOLID );
490 0 : pDispatcher->Execute( nRightSlot, SFX_CALLMODE_RECORD, &aXLineStyleItem, 0L );
491 : }
492 0 : }
493 : }
494 : }
495 :
496 0 : XLineColorItem aXLineColorItem( aStr, aColor );
497 0 : pDispatcher->Execute( nRightSlot, SFX_CALLMODE_RECORD, &aXLineColorItem, 0L );
498 : }
499 : }
500 0 : else if ( nPos != 1 ) // unsichtbar
501 : {
502 0 : SvxColorItem aRightColorItem( aColor, nRightSlot );
503 0 : pDispatcher->Execute( nRightSlot, SFX_CALLMODE_RECORD, &aRightColorItem, 0L );
504 : }
505 : }
506 :
507 0 : return 0;
508 : }
509 :
510 : /*************************************************************************
511 : |*
512 : |* Resizing
513 : |*
514 : \************************************************************************/
515 :
516 :
517 0 : void SvxColorDockingWindow::Resizing( Size& rNewSize )
518 : {
519 0 : rNewSize.Width() -= 4;
520 0 : rNewSize.Height() -= 4;
521 :
522 : // Spalten und Reihen ermitteln
523 0 : nCols = (sal_uInt16) ( (float) rNewSize.Width() / (float) aItemSize.Width() + 0.5 );
524 0 : nLines = (sal_uInt16) ( (float) rNewSize.Height() / (float) aItemSize.Height() + 0.5 );
525 0 : if( nLines == 0 )
526 0 : nLines = 1;
527 :
528 : // Scrollbar setzen/entfernen
529 0 : WinBits nBits = aColorSet.GetStyle();
530 0 : if ( nLines * nCols >= nCount )
531 0 : nBits &= ~WB_VSCROLL;
532 : else
533 0 : nBits |= WB_VSCROLL;
534 0 : aColorSet.SetStyle( nBits );
535 :
536 : // ScrollBar ?
537 0 : long nScrollWidth = aColorSet.GetScrollWidth();
538 0 : if( nScrollWidth > 0 )
539 : {
540 : // Spalten mit ScrollBar berechnen
541 0 : nCols = (sal_uInt16) ( ( ( (float) rNewSize.Width() - (float) nScrollWidth ) )
542 0 : / (float) aItemSize.Width() + 0.5 );
543 : }
544 0 : if( nCols <= 1 )
545 0 : nCols = 2;
546 :
547 : // Max. Reihen anhand der gegebenen Spalten berechnen
548 0 : long nMaxLines = nCount / nCols;
549 0 : if( nCount % nCols )
550 0 : nMaxLines++;
551 :
552 : nLines = sal::static_int_cast< sal_uInt16 >(
553 0 : std::min< long >( nLines, nMaxLines ) );
554 :
555 : // Groesse des Windows setzen
556 0 : rNewSize.Width() = nCols * aItemSize.Width() + nScrollWidth + 4;
557 0 : rNewSize.Height() = nLines * aItemSize.Height() + 4;
558 0 : }
559 :
560 : /*************************************************************************
561 : |*
562 : |* Resize
563 : |*
564 : \************************************************************************/
565 :
566 0 : void SvxColorDockingWindow::Resize()
567 : {
568 0 : if ( !IsFloatingMode() || !GetFloatingWindow()->IsRollUp() )
569 0 : SetSize();
570 0 : SfxDockingWindow::Resize();
571 0 : }
572 :
573 :
574 :
575 0 : void SvxColorDockingWindow::GetFocus (void)
576 : {
577 0 : SfxDockingWindow::GetFocus();
578 : // Grab the focus to the color value set so that it can be controlled
579 : // with the keyboard.
580 0 : aColorSet.GrabFocus();
581 0 : }
582 :
583 0 : long SvxColorDockingWindow::Notify( NotifyEvent& rNEvt )
584 : {
585 0 : long nRet = 0;
586 0 : if( ( rNEvt.GetType() == EVENT_KEYINPUT ) )
587 : {
588 0 : KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
589 0 : sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
590 0 : switch( nKeyCode )
591 : {
592 : case KEY_ESCAPE:
593 0 : GrabFocusToDocument();
594 0 : nRet = 1;
595 0 : break;
596 : }
597 : }
598 :
599 0 : return nRet ? nRet : SfxDockingWindow::Notify( rNEvt );
600 216 : }
601 :
602 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|