LibreOffice Module svx (master)  1
itemwin.cxx
Go to the documentation of this file.
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 #include <com/sun/star/drawing/FillStyle.hpp>
21 #include <com/sun/star/frame/XDispatchProvider.hpp>
22 #include <com/sun/star/frame/XFrame.hpp>
23 
25 #include <sfx2/tbxctrl.hxx>
26 #include <sfx2/viewsh.hxx>
27 #include <sfx2/module.hxx>
28 
29 #include <vcl/event.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 #include <vcl/virdev.hxx>
33 
34 #include <svx/dialmgr.hxx>
35 #include <svx/strings.hrc>
36 
37 #include <svx/xlnwtit.hxx>
38 #include <svx/xtable.hxx>
39 #include <svx/itemwin.hxx>
40 #include <svtools/unitconv.hxx>
41 #include "linemetricbox.hxx"
42 
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::frame;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::beans;
48 
50  vcl::Window* pParent, const Reference< XFrame >& rFrame )
51  : InterimItemWindow(pParent, "svx/ui/metricfieldbox.ui", "MetricFieldBox")
52  , m_xWidget(m_xBuilder->weld_metric_spin_button("metricfield", FieldUnit::MM))
53  , nCurValue(0)
54  , eDestPoolUnit(MapUnit::Map100thMM)
55  , eDlgUnit(SfxModule::GetModuleFieldUnit(rFrame))
56  , mxFrame(rFrame)
57 {
58  InitControlBase(&m_xWidget->get_widget());
59 
60  m_xWidget->set_range(0, 5000, FieldUnit::NONE);
61  m_xWidget->connect_value_changed(LINK(this, SvxMetricField, ModifyHdl));
62  m_xWidget->connect_focus_in(LINK(this, SvxMetricField, FocusInHdl));
63  m_xWidget->get_widget().connect_key_press(LINK(this, SvxMetricField, KeyInputHdl));
64 
66 
67  SetSizePixel(m_xWidget->get_preferred_size());
68 }
69 
71 {
72  m_xWidget.reset();
74 }
75 
77 {
78  disposeOnce();
79 }
80 
81 void SvxMetricField::set_sensitive(bool bSensitive)
82 {
83  Enable(bSensitive);
84  m_xWidget->set_sensitive(bSensitive);
85  if (!bSensitive)
86  m_xWidget->set_text("");
87 }
88 
90 {
91  if ( pItem )
92  {
93  // tdf#132169 we always get the value in MapUnit::Map100thMM but have
94  // to set it in the core metric of the target application
95  if (pItem->GetValue() != GetCoreValue(*m_xWidget, MapUnit::Map100thMM))
96  SetMetricValue(*m_xWidget, pItem->GetValue(), MapUnit::Map100thMM);
97  }
98  else
99  m_xWidget->set_text("");
100 }
101 
103 {
104  auto nTmp = GetCoreValue(*m_xWidget, eDestPoolUnit);
105  XLineWidthItem aLineWidthItem( nTmp );
106 
107  Any a;
108  aLineWidthItem.QueryValue( a );
109  Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue("LineWidth", a) };
110  SfxToolBoxControl::Dispatch( Reference< XDispatchProvider >( mxFrame->getController(), UNO_QUERY ),
111  ".uno:LineWidth",
112  aArgs );
113 }
114 
116 {
117  if( SfxViewShell::Current() )
118  {
119  vcl::Window* pShellWnd = SfxViewShell::Current()->GetWindow();
120  if ( pShellWnd )
121  pShellWnd->GrabFocus();
122  }
123 }
124 
126 {
127  eDestPoolUnit = eUnit;
128 }
129 
131 {
133  if ( eDlgUnit != eTmpUnit )
134  {
135  eDlgUnit = eTmpUnit;
137  }
138 }
139 
141 {
142  nCurValue = m_xWidget->get_value(FieldUnit::NONE);
143 }
144 
145 IMPL_LINK(SvxMetricField, KeyInputHdl, const KeyEvent&, rKEvt, bool)
146 {
147  bool bHandled = false;
148 
149  sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
150 
151  if (nCode == KEY_ESCAPE)
152  {
153  m_xWidget->set_value(nCurValue, FieldUnit::NONE);
154  ModifyHdl(*m_xWidget);
155  ReleaseFocus_Impl();
156  bHandled = true;
157  }
158 
159  return bHandled || ChildKeyInput(rKEvt);
160 }
161 
163 {
164  if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
165  (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
166  {
167  SetSizePixel(m_xWidget->get_preferred_size());
168  }
169 
171 }
172 
174 {
175  rListBox.freeze();
176 
177  rListBox.append_text(SvxResId(RID_SVXSTR_INVISIBLE));
178  rListBox.append_text(SvxResId(RID_SVXSTR_COLOR));
179  rListBox.append_text(SvxResId(RID_SVXSTR_GRADIENT));
180  rListBox.append_text(SvxResId(RID_SVXSTR_HATCH));
181  rListBox.append_text(SvxResId(RID_SVXSTR_BITMAP));
182  rListBox.append_text(SvxResId(RID_SVXSTR_PATTERN));
183 
184  rListBox.thaw();
185 
186  rListBox.set_active(1); // solid color
187 }
188 
189 namespace
190 {
191  void formatBitmapExToSize(BitmapEx& rBitmapEx, const Size& rSize)
192  {
193  if(rBitmapEx.IsEmpty() || rSize.IsEmpty())
194  return;
195 
197  pVirtualDevice->SetOutputSizePixel(rSize);
198 
199  if(rBitmapEx.IsAlpha())
200  {
201  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
202 
203  if(rStyleSettings.GetPreviewUsesCheckeredBackground())
204  {
205  const Point aNull(0, 0);
206  static const sal_uInt32 nLen(8);
207  static const Color aW(COL_WHITE);
208  static const Color aG(0xef, 0xef, 0xef);
209 
210  pVirtualDevice->DrawCheckered(aNull, rSize, nLen, aW, aG);
211  }
212  else
213  {
214  pVirtualDevice->SetBackground(rStyleSettings.GetFieldColor());
215  pVirtualDevice->Erase();
216  }
217  }
218 
219  if(rBitmapEx.GetSizePixel().Width() >= rSize.Width() && rBitmapEx.GetSizePixel().Height() >= rSize.Height())
220  {
221  rBitmapEx.Scale(rSize);
222  pVirtualDevice->DrawBitmapEx(Point(0, 0), rBitmapEx);
223  }
224  else
225  {
226  const Size aBitmapSize(rBitmapEx.GetSizePixel());
227 
228  for(tools::Long y(0); y < rSize.Height(); y += aBitmapSize.Height())
229  {
230  for(tools::Long x(0); x < rSize.Width(); x += aBitmapSize.Width())
231  {
232  pVirtualDevice->DrawBitmapEx(
233  Point(x, y),
234  rBitmapEx);
235  }
236  }
237  }
238 
239  rBitmapEx = pVirtualDevice->GetBitmapEx(Point(0, 0), rSize);
240  }
241 } // end of anonymous namespace
242 
244 {
245  if( !pList.is() )
246  return;
247 
248  tools::Long nCount = pList->Count();
250  rBox.freeze();
251 
252  for( tools::Long i = 0; i < nCount; i++ )
253  {
254  const XHatchEntry* pEntry = pList->GetHatch(i);
255  const BitmapEx aBitmapEx = pList->GetUiBitmap( i );
256  if( !aBitmapEx.IsEmpty() )
257  {
258  const Size aBmpSize(aBitmapEx.GetSizePixel());
259  pVD->SetOutputSizePixel(aBmpSize, false);
260  pVD->DrawBitmapEx(Point(), aBitmapEx);
261  rBox.append("", pEntry->GetName(), *pVD);
262  }
263  else
264  rBox.append_text(pEntry->GetName());
265  }
266 
267  rBox.thaw();
268 }
269 
271 {
272  if( !pList.is() )
273  return;
274 
275  tools::Long nCount = pList->Count();
277  rBox.freeze();
278 
279  for( tools::Long i = 0; i < nCount; i++ )
280  {
281  const XGradientEntry* pEntry = pList->GetGradient(i);
282  const BitmapEx aBitmapEx = pList->GetUiBitmap( i );
283  if( !aBitmapEx.IsEmpty() )
284  {
285  const Size aBmpSize(aBitmapEx.GetSizePixel());
286  pVD->SetOutputSizePixel(aBmpSize, false);
287  pVD->DrawBitmapEx(Point(), aBitmapEx);
288  rBox.append("", pEntry->GetName(), *pVD);
289  }
290  else
291  rBox.append_text(pEntry->GetName());
292  }
293 
294  rBox.thaw();
295 }
296 
298 {
299  if( !pList.is() )
300  return;
301 
302  tools::Long nCount = pList->Count();
303  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
304  const Size aSize(rStyleSettings.GetListBoxPreviewDefaultPixelSize());
306  pVD->SetOutputSizePixel(aSize, false);
307  rBox.freeze();
308 
309  for( tools::Long i = 0; i < nCount; i++ )
310  {
311  const XBitmapEntry* pEntry = pList->GetBitmap( i );
312  BitmapEx aBitmapEx = pEntry->GetGraphicObject().GetGraphic().GetBitmapEx();
313  formatBitmapExToSize(aBitmapEx, aSize);
314  pVD->DrawBitmapEx(Point(), aBitmapEx);
315  rBox.append("", pEntry->GetName(), *pVD);
316  }
317 
318  rBox.thaw();
319 }
320 
322 {
323  if( !pList.is() )
324  return;
325 
326  tools::Long nCount = pList->Count();
327  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
328  const Size aSize(rStyleSettings.GetListBoxPreviewDefaultPixelSize());
330  pVD->SetOutputSizePixel(aSize, false);
331  rBox.freeze();
332 
333  for( tools::Long i = 0; i < nCount; i++ )
334  {
335  const XBitmapEntry* pEntry = pList->GetBitmap( i );
336  BitmapEx aBitmapEx = pEntry->GetGraphicObject().GetGraphic().GetBitmapEx();
337  formatBitmapExToSize(aBitmapEx, aSize);
338  pVD->DrawBitmapEx(Point(), aBitmapEx);
339  rBox.append("", pEntry->GetName(), *pVD);
340  }
341 
342  rBox.thaw();
343 
344 }
345 
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
MapUnit eDestPoolUnit
void SetFieldUnit(weld::MetricSpinButton &rField, FieldUnit eUnit, bool bAll)
sal_Int32 GetValue() const
FieldUnit eDlgUnit
std::unique_ptr< weld::MetricSpinButton > m_xWidget
FieldUnit
SvxMetricField(vcl::Window *pParent, const css::uno::Reference< css::frame::XFrame > &rFrame)
Definition: itemwin.cxx:49
virtual void DataChanged(const DataChangedEvent &rDCEvt) override
Definition: itemwin.cxx:162
virtual ~SvxMetricField() override
Definition: itemwin.cxx:76
SVX_DLLPUBLIC void Fill(weld::ComboBox &, const XHatchListRef &pList)
Definition: itemwin.cxx:243
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
long Long
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
virtual void dispose() override
Definition: itemwin.cxx:70
virtual void SetSizePixel(const Size &rNewSize)
static void ReleaseFocus_Impl()
Definition: itemwin.cxx:115
bool GetPreviewUsesCheckeredBackground() const
DataChangedEventType GetType() const
void append(const OUString &rId, const OUString &rStr)
float x
const Size & GetListBoxPreviewDefaultPixelSize() const
void set_sensitive(bool bSensitive)
Definition: itemwin.cxx:81
FieldUnit GetModuleFieldUnit(const SfxItemSet &rSet)
Definition: dlgutil.cxx:31
virtual void thaw()=0
constexpr tools::Long Width() const
bool IsAlpha() const
void Enable(bool bEnable=true, bool bChild=true)
int nCount
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
AllSettingsFlags GetFlags() const
static SfxViewShell * Current()
sal_uInt16 nCode
const GraphicObject & GetGraphicObject() const
Definition: xtable.hxx:125
css::uno::Reference< css::frame::XFrame > mxFrame
float y
const OUString & GetName() const
static FieldUnit GetModuleFieldUnit(css::uno::Reference< css::frame::XFrame > const &i_frame)
bool IsEmpty() const
SVX_DLLPUBLIC void Fill(weld::ComboBox &rListBox)
Definition: itemwin.cxx:173
bool IsEmpty() const
int i
uno_Any a
virtual void freeze()=0
int GetCoreValue(const weld::MetricSpinButton &rField, MapUnit eUnit)
const Color & GetFieldColor() const
void Update(const XLineWidthItem *pItem)
Definition: itemwin.cxx:89
void SetDestCoreUnit(MapUnit eUnit)
Definition: itemwin.cxx:125
virtual void set_active(int pos)=0
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
void SetMetricValue(weld::MetricSpinButton &rField, int nCoreValue, MapUnit eUnit)
void GrabFocus()
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
Definition: xattr.cxx:919
void RefreshDlgUnit()
Definition: itemwin.cxx:130
const Graphic & GetGraphic() const
constexpr tools::Long Height() const
constexpr sal_uInt16 KEY_ESCAPE
virtual void dispose() override
vcl::Window * GetWindow() const
IMPL_LINK(SvxMetricField, KeyInputHdl, const KeyEvent &, rKEvt, bool)
Definition: itemwin.cxx:145
IMPL_LINK_NOARG(SvxMetricField, ModifyHdl, weld::MetricSpinButton &, void)
Definition: itemwin.cxx:102
MapUnit
void append_text(const OUString &rStr)
std::unique_ptr< weld::Image > m_xWidget
const Size & GetSizePixel() const
void Dispatch(const OUString &aCommand, css::uno::Sequence< css::beans::PropertyValue > const &aArgs)
void InitControlBase(weld::Widget *pWidget)
virtual void DataChanged(const DataChangedEvent &rDCEvt)