1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <sot/exchange.hxx>
23 #include <svx/strings.hrc>
24 #include <svx/svxids.hrc>
26 #include <sfx2/viewsh.hxx>
27 #include <sfx2/objsh.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/viewfrm.hxx>
30 #include <vcl/image.hxx>
31 #include <vcl/transfer.hxx>
33 #include <colrctrl.hxx>
35 #include <svx/svdview.hxx>
36 #include <svx/drawitem.hxx>
37 #include <svx/xfillit0.hxx>
38 #include <svx/xflclit.hxx>
39 #include <editeng/colritem.hxx>
40 #include <svx/xlineit0.hxx>
41 #include <svx/xlnclit.hxx>
42 #include <svx/xtable.hxx>
43 #include <svx/dialmgr.hxx>
45 #include <vcl/virdev.hxx>
47 #include <com/sun/star/beans/NamedValue.hpp>
49 using namespace com::sun::star
;
51 class SvxColorValueSetData final
: public TransferDataContainer
54 uno::Sequence
<beans::NamedValue
> m_Data
;
56 virtual void AddSupportedFormats() override
;
57 virtual bool GetData(const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& rDestDoc
) override
;
60 SvxColorValueSetData()
64 void SetData(const uno::Sequence
<beans::NamedValue
>& rData
)
67 ClearFormats(); // invalidate m_aAny so new data will take effect
71 void SvxColorValueSetData::AddSupportedFormats()
73 AddFormat( SotClipboardFormatId::XFA
);
76 bool SvxColorValueSetData::GetData( const css::datatransfer::DataFlavor
& rFlavor
, const OUString
& /*rDestDoc*/ )
80 if( SotExchange::GetFormat( rFlavor
) == SotClipboardFormatId::XFA
)
82 SetAny(uno::Any(m_Data
));
89 void SvxColorValueSet_docking::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
91 SvxColorValueSet::SetDrawingArea(pDrawingArea
);
92 SetAccessibleName(SvxResId(STR_COLORTABLE
));
93 SetStyle(GetStyle() | WB_ITEMBORDER
);
95 m_xHelper
.set(new SvxColorValueSetData
);
96 rtl::Reference
<TransferDataContainer
> xHelper(m_xHelper
);
97 SetDragDataTransferable(xHelper
, DND_ACTION_COPY
);
100 SvxColorValueSet_docking::SvxColorValueSet_docking(std::unique_ptr
<weld::ScrolledWindow
> xWindow
)
101 : SvxColorValueSet(std::move(xWindow
))
106 bool SvxColorValueSet_docking::MouseButtonDown( const MouseEvent
& rMEvt
)
110 // For Mac still handle differently!
114 bRet
= SvxColorValueSet::MouseButtonDown( rMEvt
);
118 mbLeftButton
= false;
119 MouseEvent
aMEvt( rMEvt
.GetPosPixel(),
123 rMEvt
.GetModifier() );
124 bRet
= SvxColorValueSet::MouseButtonDown( aMEvt
);
130 bool SvxColorValueSet_docking::MouseButtonUp( const MouseEvent
& rMEvt
)
134 // For Mac still handle differently!
138 bRet
= SvxColorValueSet::MouseButtonUp( rMEvt
);
142 mbLeftButton
= false;
143 MouseEvent
aMEvt( rMEvt
.GetPosPixel(),
147 rMEvt
.GetModifier() );
148 bRet
= SvxColorValueSet::MouseButtonUp( aMEvt
);
155 bool SvxColorValueSet_docking::StartDrag()
157 sal_uInt16 nPos
= GetSelectedItemId();
158 Color
aItemColor( GetItemColor( nPos
) );
159 OUString
sItemText( GetItemText( nPos
) );
161 drawing::FillStyle eStyle
= ((1 == nPos
)
162 ? drawing::FillStyle_NONE
163 : drawing::FillStyle_SOLID
);
165 XFillColorItem
const color(sItemText
, aItemColor
);
166 XFillStyleItem
const style(eStyle
);
168 color
.QueryValue(c
, 0);
169 style
.QueryValue(s
, 0);
171 uno::Sequence
<beans::NamedValue
> props
{ { "FillColor", std::move(c
) },
172 { "FillStyle", std::move(s
) } };
173 m_xHelper
->SetData(props
);
178 constexpr sal_uInt16 gnLeftSlot
= SID_ATTR_FILL_COLOR
;
179 constexpr sal_uInt16 gnRightSlot
= SID_ATTR_LINE_COLOR
;
181 SvxColorDockingWindow::SvxColorDockingWindow(SfxBindings
* _pBindings
, SfxChildWindow
* pCW
, vcl::Window
* _pParent
)
182 : SfxDockingWindow(_pBindings
, pCW
, _pParent
,
183 "DockingColorWindow", "svx/ui/dockingcolorwindow.ui")
184 , xColorSet(new SvxColorValueSet_docking(m_xBuilder
->weld_scrolled_window("valuesetwin", true)))
185 , xColorSetWin(new weld::CustomWeld(*m_xBuilder
, "valueset", *xColorSet
))
187 SetText(SvxResId(STR_COLORTABLE
));
188 SetQuickHelpText(SvxResId(RID_SVXSTR_COLORBAR
));
189 SetSizePixel(LogicToPixel(Size(150, 22), MapMode(MapUnit::MapAppFont
)));
190 SetHelpId(HID_CTRL_COLOR
);
192 xColorSet
->SetSelectHdl( LINK( this, SvxColorDockingWindow
, SelectHdl
) );
193 xColorSet
->SetHelpId(HID_COLOR_CTL_COLORS
);
195 // Get the model from the view shell. Using SfxObjectShell::Current()
196 // is unreliable when called at the wrong times.
197 SfxObjectShell
* pDocSh
= nullptr;
198 if (_pBindings
!= nullptr)
200 SfxDispatcher
* pDispatcher
= _pBindings
->GetDispatcher();
201 if (pDispatcher
!= nullptr)
203 SfxViewFrame
* pFrame
= pDispatcher
->GetFrame();
204 if (pFrame
!= nullptr)
206 SfxViewShell
* pViewShell
= pFrame
->GetViewShell();
207 if (pViewShell
!= nullptr)
208 pDocSh
= pViewShell
->GetObjectShell();
215 const SfxPoolItem
* pItem
= pDocSh
->GetItem( SID_COLOR_TABLE
);
218 pColorList
= static_cast<const SvxColorListItem
*>(pItem
)->GetColorList();
223 Size aItemSize
= xColorSet
->CalcItemSizePixel(Size(SvxColorValueSet::getEntryEdgeLength(), SvxColorValueSet::getEntryEdgeLength()));
224 aItemSize
.setWidth( aItemSize
.Width() + SvxColorValueSet::getEntryEdgeLength() );
225 aItemSize
.setWidth( aItemSize
.Width() / 2 );
226 aItemSize
.setHeight( aItemSize
.Height() + SvxColorValueSet::getEntryEdgeLength() );
227 aItemSize
.setHeight( aItemSize
.Height() / 2 );
229 if (_pBindings
!= nullptr)
230 StartListening(*_pBindings
, DuplicateHandling::Prevent
);
233 SvxColorDockingWindow::~SvxColorDockingWindow()
238 void SvxColorDockingWindow::dispose()
240 EndListening( GetBindings() );
241 xColorSetWin
.reset();
243 SfxDockingWindow::dispose();
246 void SvxColorDockingWindow::Notify( SfxBroadcaster
& , const SfxHint
& rHint
)
248 const SfxPoolItemHint
* pPoolItemHint
= dynamic_cast<const SfxPoolItemHint
*>(&rHint
);
250 if (auto pColorListItem
= dynamic_cast<const SvxColorListItem
*>(pPoolItemHint
->GetObject()))
252 // The list of colors has changed
253 pColorList
= pColorListItem
->GetColorList();
258 void SvxColorDockingWindow::FillValueSet()
260 if( !pColorList
.is() )
265 xColorSet
->addEntriesForXColorList(*pColorList
, 2);
267 // create the last entry for 'invisible/none'
268 const Size
aColorSize(SvxColorValueSet::getEntryEdgeLength(), SvxColorValueSet::getEntryEdgeLength());
269 tools::Long nPtX
= aColorSize
.Width() - 1;
270 tools::Long nPtY
= aColorSize
.Height() - 1;
271 ScopedVclPtrInstance
< VirtualDevice
> pVD
;
273 pVD
->SetOutputSizePixel( aColorSize
);
274 pVD
->SetLineColor( COL_BLACK
);
275 pVD
->SetBackground( Wallpaper( COL_WHITE
) );
276 pVD
->DrawLine( Point(), Point( nPtX
, nPtY
) );
277 pVD
->DrawLine( Point( 0, nPtY
), Point( nPtX
, 0 ) );
279 BitmapEx
aBmp( pVD
->GetBitmapEx( Point(), aColorSize
) );
281 xColorSet
->InsertItem( sal_uInt16(1), Image(aBmp
), SvxResId( RID_SVXSTR_INVISIBLE
) );
284 bool SvxColorDockingWindow::Close()
286 SfxBoolItem
aItem( SID_COLOR_CONTROL
, false );
287 GetBindings().GetDispatcher()->ExecuteList(SID_COLOR_CONTROL
,
288 SfxCallMode::ASYNCHRON
| SfxCallMode::RECORD
, { &aItem
});
289 SfxDockingWindow::Close();
293 IMPL_LINK_NOARG(SvxColorDockingWindow
, SelectHdl
, ValueSet
*, void)
295 SfxDispatcher
* pDispatcher
= GetBindings().GetDispatcher();
296 sal_uInt16 nPos
= xColorSet
->GetSelectedItemId();
297 Color
aColor( xColorSet
->GetItemColor( nPos
) );
298 OUString
aStr( xColorSet
->GetItemText( nPos
) );
300 if (xColorSet
->IsLeftButton())
302 if ( gnLeftSlot
== SID_ATTR_FILL_COLOR
)
304 if ( nPos
== 1 ) // invisible
306 XFillStyleItem
aXFillStyleItem( drawing::FillStyle_NONE
);
307 pDispatcher
->ExecuteList(gnLeftSlot
, SfxCallMode::RECORD
,
308 { &aXFillStyleItem
});
314 // If we have a DrawView and we are in TextEdit mode, then
315 // not the area color but the text color is assigned
316 SfxViewShell
* pViewSh
= SfxViewShell::Current();
319 SdrView
* pView
= pViewSh
->GetDrawView();
320 if ( pView
&& pView
->IsTextEdit() )
322 SvxColorItem
aTextColorItem( aColor
, SID_ATTR_CHAR_COLOR
);
323 pDispatcher
->ExecuteList(SID_ATTR_CHAR_COLOR
,
324 SfxCallMode::RECORD
, { &aTextColorItem
});
330 XFillStyleItem
aXFillStyleItem( drawing::FillStyle_SOLID
);
331 XFillColorItem
aXFillColorItem( aStr
, aColor
);
332 pDispatcher
->ExecuteList(gnLeftSlot
, SfxCallMode::RECORD
,
333 { &aXFillColorItem
, &aXFillStyleItem
});
337 else if ( nPos
!= 1 ) // invisible
339 SvxColorItem
aLeftColorItem( aColor
, gnLeftSlot
);
340 pDispatcher
->ExecuteList(gnLeftSlot
, SfxCallMode::RECORD
,
341 { &aLeftColorItem
});
346 if ( gnRightSlot
== SID_ATTR_LINE_COLOR
)
348 if( nPos
== 1 ) // invisible
350 XLineStyleItem
aXLineStyleItem( drawing::LineStyle_NONE
);
351 pDispatcher
->ExecuteList(gnRightSlot
, SfxCallMode::RECORD
,
352 { &aXLineStyleItem
});
356 // If the LineStyle is invisible, it is set to SOLID
357 SfxViewShell
* pViewSh
= SfxViewShell::Current();
360 SdrView
* pView
= pViewSh
->GetDrawView();
363 SfxItemSet
aAttrSet(pView
->GetModel().GetItemPool());
364 pView
->GetAttributes( aAttrSet
);
365 if ( aAttrSet
.GetItemState( XATTR_LINESTYLE
) != SfxItemState::DONTCARE
)
367 drawing::LineStyle eXLS
=
368 aAttrSet
.Get( XATTR_LINESTYLE
).GetValue();
369 if ( eXLS
== drawing::LineStyle_NONE
)
371 XLineStyleItem
aXLineStyleItem( drawing::LineStyle_SOLID
);
372 pDispatcher
->ExecuteList(gnRightSlot
,
373 SfxCallMode::RECORD
, { &aXLineStyleItem
});
379 XLineColorItem
aXLineColorItem( aStr
, aColor
);
380 pDispatcher
->ExecuteList(gnRightSlot
, SfxCallMode::RECORD
,
381 { &aXLineColorItem
});
384 else if ( nPos
!= 1 ) // invisible
386 SvxColorItem
aRightColorItem( aColor
, gnRightSlot
);
387 pDispatcher
->ExecuteList(gnRightSlot
, SfxCallMode::RECORD
,
388 { &aRightColorItem
});
393 void SvxColorDockingWindow::GetFocus()
395 SfxDockingWindow::GetFocus();
398 // Grab the focus to the color value set so that it can be controlled
399 // with the keyboard.
400 xColorSet
->GrabFocus();
404 bool SvxColorDockingWindow::EventNotify( NotifyEvent
& rNEvt
)
407 if( rNEvt
.GetType() == NotifyEventType::KEYINPUT
)
409 KeyEvent aKeyEvt
= *rNEvt
.GetKeyEvent();
410 sal_uInt16 nKeyCode
= aKeyEvt
.GetKeyCode().GetCode();
414 GrabFocusToDocument();
420 return bRet
|| SfxDockingWindow::EventNotify(rNEvt
);
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */