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
24 #include <comphelper/propertyvalue.hxx>
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::beans;
47
SvxMetricField(vcl::Window * pParent,const Reference<XFrame> & rFrame)48 SvxMetricField::SvxMetricField(
49 vcl::Window* pParent, const Reference< XFrame >& rFrame )
50 : InterimItemWindow(pParent, u"svx/ui/metricfieldbox.ui"_ustr, u"MetricFieldBox"_ustr)
51 , m_xWidget(m_xBuilder->weld_metric_spin_button(u"metricfield"_ustr, FieldUnit::MM))
52 , nCurValue(0)
53 , eDestPoolUnit(MapUnit::Map100thMM)
54 , eDlgUnit(SfxModule::GetModuleFieldUnit(rFrame))
55 , mxFrame(rFrame)
56 {
57 InitControlBase(&m_xWidget->get_widget());
58
59 m_xWidget->set_range(0, 5000, FieldUnit::NONE);
60 m_xWidget->connect_value_changed(LINK(this, SvxMetricField, ModifyHdl));
61 m_xWidget->connect_focus_in(LINK(this, SvxMetricField, FocusInHdl));
62 m_xWidget->get_widget().connect_key_press(LINK(this, SvxMetricField, KeyInputHdl));
63
64 SetFieldUnit(*m_xWidget, eDlgUnit);
65
66 SetSizePixel(m_xWidget->get_preferred_size());
67 }
68
dispose()69 void SvxMetricField::dispose()
70 {
71 m_xWidget.reset();
72 InterimItemWindow::dispose();
73 }
74
~SvxMetricField()75 SvxMetricField::~SvxMetricField()
76 {
77 disposeOnce();
78 }
79
set_sensitive(bool bSensitive)80 void SvxMetricField::set_sensitive(bool bSensitive)
81 {
82 Enable(bSensitive);
83 m_xWidget->set_sensitive(bSensitive);
84 if (!bSensitive)
85 m_xWidget->set_text(u""_ustr);
86 }
87
Update(const XLineWidthItem * pItem)88 void SvxMetricField::Update( const XLineWidthItem* pItem )
89 {
90 if ( pItem )
91 {
92 // tdf#132169 we always get the value in MapUnit::Map100thMM but have
93 // to set it in the core metric of the target application
94 if (pItem->GetValue() != GetCoreValue(*m_xWidget, MapUnit::Map100thMM))
95 SetMetricValue(*m_xWidget, pItem->GetValue(), MapUnit::Map100thMM);
96 }
97 else
98 m_xWidget->set_text(u""_ustr);
99 }
100
IMPL_LINK_NOARG(SvxMetricField,ModifyHdl,weld::MetricSpinButton &,void)101 IMPL_LINK_NOARG(SvxMetricField, ModifyHdl, weld::MetricSpinButton&, void)
102 {
103 auto nTmp = GetCoreValue(*m_xWidget, eDestPoolUnit);
104 XLineWidthItem aLineWidthItem( nTmp );
105
106 Any a;
107 aLineWidthItem.QueryValue( a );
108 Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue(u"LineWidth"_ustr, a) };
109 SfxToolBoxControl::Dispatch( Reference< XDispatchProvider >( mxFrame->getController(), UNO_QUERY ),
110 u".uno:LineWidth"_ustr,
111 aArgs );
112 }
113
ReleaseFocus_Impl()114 void SvxMetricField::ReleaseFocus_Impl()
115 {
116 if (const SfxViewShell* pViewShell = SfxViewShell::Current())
117 {
118 vcl::Window* pShellWnd = pViewShell->GetWindow();
119 if ( pShellWnd )
120 pShellWnd->GrabFocus();
121 }
122 }
123
SetDestCoreUnit(MapUnit eUnit)124 void SvxMetricField::SetDestCoreUnit( MapUnit eUnit )
125 {
126 eDestPoolUnit = eUnit;
127 }
128
RefreshDlgUnit()129 void SvxMetricField::RefreshDlgUnit()
130 {
131 FieldUnit eTmpUnit = SfxModule::GetModuleFieldUnit( mxFrame );
132 if ( eDlgUnit != eTmpUnit )
133 {
134 eDlgUnit = eTmpUnit;
135 SetFieldUnit(*m_xWidget, eDlgUnit);
136 }
137 }
138
IMPL_LINK_NOARG(SvxMetricField,FocusInHdl,weld::Widget &,void)139 IMPL_LINK_NOARG(SvxMetricField, FocusInHdl, weld::Widget&, void)
140 {
141 nCurValue = m_xWidget->get_value(FieldUnit::NONE);
142 }
143
IMPL_LINK(SvxMetricField,KeyInputHdl,const KeyEvent &,rKEvt,bool)144 IMPL_LINK(SvxMetricField, KeyInputHdl, const KeyEvent&, rKEvt, bool)
145 {
146 bool bHandled = false;
147
148 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode();
149
150 if (nCode == KEY_ESCAPE)
151 {
152 m_xWidget->set_value(nCurValue, FieldUnit::NONE);
153 ModifyHdl(*m_xWidget);
154 ReleaseFocus_Impl();
155 bHandled = true;
156 }
157
158 return bHandled || ChildKeyInput(rKEvt);
159 }
160
DataChanged(const DataChangedEvent & rDCEvt)161 void SvxMetricField::DataChanged( const DataChangedEvent& rDCEvt )
162 {
163 if ( (rDCEvt.GetType() == DataChangedEventType::SETTINGS) &&
164 (rDCEvt.GetFlags() & AllSettingsFlags::STYLE) )
165 {
166 SetSizePixel(m_xWidget->get_preferred_size());
167 }
168
169 InterimItemWindow::DataChanged( rDCEvt );
170 }
171
Fill(weld::ComboBox & rListBox)172 void SvxFillTypeBox::Fill(weld::ComboBox& rListBox)
173 {
174 rListBox.freeze();
175
176 rListBox.append_text(SvxResId(RID_SVXSTR_INVISIBLE));
177 rListBox.append_text(SvxResId(RID_SVXSTR_COLOR));
178 rListBox.append_text(SvxResId(RID_SVXSTR_GRADIENT));
179 rListBox.append_text(SvxResId(RID_SVXSTR_HATCH));
180 rListBox.append_text(SvxResId(RID_SVXSTR_BITMAP));
181 rListBox.append_text(SvxResId(RID_SVXSTR_PATTERN));
182 rListBox.append_text(SvxResId(RID_SVXSTR_USE_BACKGROUND));
183
184 rListBox.thaw();
185
186 rListBox.set_active(1); // solid color
187 }
188
189 namespace
190 {
formatBitmapExToSize(BitmapEx & rBitmapEx,const Size & rSize)191 void formatBitmapExToSize(BitmapEx& rBitmapEx, const Size& rSize)
192 {
193 if(rBitmapEx.IsEmpty() || rSize.IsEmpty())
194 return;
195
196 ScopedVclPtrInstance< VirtualDevice > pVirtualDevice;
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
Fill(weld::ComboBox & rBox,const XHatchListRef & pList)243 void SvxFillAttrBox::Fill(weld::ComboBox& rBox, const XHatchListRef &pList)
244 {
245 if( !pList.is() )
246 return;
247
248 tools::Long nCount = pList->Count();
249 ScopedVclPtrInstance< VirtualDevice > pVD;
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(u""_ustr, pEntry->GetName(), *pVD);
262 }
263 else
264 rBox.append_text(pEntry->GetName());
265 }
266
267 rBox.thaw();
268 }
269
Fill(weld::ComboBox & rBox,const XGradientListRef & pList)270 void SvxFillAttrBox::Fill(weld::ComboBox& rBox, const XGradientListRef &pList)
271 {
272 if( !pList.is() )
273 return;
274
275 tools::Long nCount = pList->Count();
276 ScopedVclPtrInstance< VirtualDevice > pVD;
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(u""_ustr, pEntry->GetName(), *pVD);
289 }
290 else
291 rBox.append_text(pEntry->GetName());
292 }
293
294 rBox.thaw();
295 }
296
Fill(weld::ComboBox & rBox,const XBitmapListRef & pList)297 void SvxFillAttrBox::Fill(weld::ComboBox& rBox, const XBitmapListRef &pList)
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());
305 ScopedVclPtrInstance< VirtualDevice > pVD;
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(u""_ustr, pEntry->GetName(), *pVD);
316 }
317
318 rBox.thaw();
319 }
320
Fill(weld::ComboBox & rBox,const XPatternListRef & pList)321 void SvxFillAttrBox::Fill(weld::ComboBox& rBox, const XPatternListRef &pList)
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());
329 ScopedVclPtrInstance< VirtualDevice > pVD;
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(u""_ustr, pEntry->GetName(), *pVD);
340 }
341
342 rBox.thaw();
343
344 }
345
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
347