Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / tbxctrls / colrctrl.cxx
blobda829d127e5da7aa8e4d54d9cb42698ec7697954
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 <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>
44 #include <helpids.h>
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
53 private:
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;
59 public:
60 SvxColorValueSetData()
64 void SetData(const uno::Sequence<beans::NamedValue>& rData)
66 m_Data = 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*/ )
78 bool bRet = false;
80 if( SotExchange::GetFormat( rFlavor ) == SotClipboardFormatId::XFA )
82 SetAny(uno::Any(m_Data));
83 bRet = true;
86 return bRet;
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))
102 , mbLeftButton(true)
106 bool SvxColorValueSet_docking::MouseButtonDown( const MouseEvent& rMEvt )
108 bool bRet;
110 // For Mac still handle differently!
111 if( rMEvt.IsLeft() )
113 mbLeftButton = true;
114 bRet = SvxColorValueSet::MouseButtonDown( rMEvt );
116 else
118 mbLeftButton = false;
119 MouseEvent aMEvt( rMEvt.GetPosPixel(),
120 rMEvt.GetClicks(),
121 rMEvt.GetMode(),
122 MOUSE_LEFT,
123 rMEvt.GetModifier() );
124 bRet = SvxColorValueSet::MouseButtonDown( aMEvt );
127 return bRet;
130 bool SvxColorValueSet_docking::MouseButtonUp( const MouseEvent& rMEvt )
132 bool bRet;
134 // For Mac still handle differently!
135 if( rMEvt.IsLeft() )
137 mbLeftButton = true;
138 bRet = SvxColorValueSet::MouseButtonUp( rMEvt );
140 else
142 mbLeftButton = false;
143 MouseEvent aMEvt( rMEvt.GetPosPixel(),
144 rMEvt.GetClicks(),
145 rMEvt.GetMode(),
146 MOUSE_LEFT,
147 rMEvt.GetModifier() );
148 bRet = SvxColorValueSet::MouseButtonUp( aMEvt );
150 SetNoSelection();
152 return bRet;
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);
167 uno::Any c, s;
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);
175 return false;
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();
213 if ( pDocSh )
215 const SfxPoolItem* pItem = pDocSh->GetItem( SID_COLOR_TABLE );
216 if( pItem )
218 pColorList = static_cast<const SvxColorListItem*>(pItem)->GetColorList();
219 FillValueSet();
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()
235 disposeOnce();
238 void SvxColorDockingWindow::dispose()
240 EndListening( GetBindings() );
241 xColorSetWin.reset();
242 xColorSet.reset();
243 SfxDockingWindow::dispose();
246 void SvxColorDockingWindow::Notify( SfxBroadcaster& , const SfxHint& rHint )
248 const SfxPoolItemHint* pPoolItemHint = dynamic_cast<const SfxPoolItemHint*>(&rHint);
249 if ( pPoolItemHint )
250 if (auto pColorListItem = dynamic_cast<const SvxColorListItem*>(pPoolItemHint->GetObject()))
252 // The list of colors has changed
253 pColorList = pColorListItem->GetColorList();
254 FillValueSet();
258 void SvxColorDockingWindow::FillValueSet()
260 if( !pColorList.is() )
261 return;
263 xColorSet->Clear();
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();
290 return true;
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 });
310 else
312 bool bDone = false;
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();
317 if ( pViewSh )
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 });
325 bDone = true;
328 if ( !bDone )
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 });
344 else
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 });
354 else
356 // If the LineStyle is invisible, it is set to SOLID
357 SfxViewShell* pViewSh = SfxViewShell::Current();
358 if ( pViewSh )
360 SdrView* pView = pViewSh->GetDrawView();
361 if ( pView )
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();
396 if (xColorSet)
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 )
406 bool bRet = false;
407 if( rNEvt.GetType() == NotifyEventType::KEYINPUT )
409 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
410 sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
411 switch( nKeyCode )
413 case KEY_ESCAPE:
414 GrabFocusToDocument();
415 bRet = true;
416 break;
420 return bRet || SfxDockingWindow::EventNotify(rNEvt);
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */