LibreOffice Module vcl (master)  1
weldutils.hxx
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 
10 #ifndef INCLUDED_VCL_WELDUTILS_HXX
11 #define INCLUDED_VCL_WELDUTILS_HXX
12 
13 #include <com/sun/star/awt/XWindow.hpp>
14 #include <com/sun/star/frame/XDispatch.hpp>
15 #include <com/sun/star/frame/XFrame.hpp>
16 #include <com/sun/star/frame/XStatusListener.hpp>
17 #include <com/sun/star/uno/Reference.hxx>
19 #include <cppuhelper/compbase.hxx>
20 #include <tools/time.hxx>
21 #include <vcl/dllapi.h>
22 #include <vcl/formatter.hxx>
23 #include <vcl/timer.hxx>
24 #include <vcl/weld.hxx>
25 
26 class CalendarWrapper;
27 
28 namespace vcl
29 {
30 class Window;
31 }
32 
33 namespace weld
34 {
35 typedef cppu::WeakComponentImplHelper<css::awt::XWindow> TransportAsXWindow_Base;
36 
37 class VCL_DLLPUBLIC TransportAsXWindow : public TransportAsXWindow_Base
38 {
39 private:
40  osl::Mutex m_aHelperMtx;
43 
50 
51 public:
52  TransportAsXWindow(weld::Widget* pWeldWidget, weld::Builder* pWeldWidgetBuilder = nullptr)
53  : TransportAsXWindow_Base(m_aHelperMtx)
54  , m_pWeldWidget(pWeldWidget)
55  , m_pWeldWidgetBuilder(pWeldWidgetBuilder)
56  , m_aWindowListeners(m_aHelperMtx)
57  , m_aKeyListeners(m_aHelperMtx)
58  , m_aFocusListeners(m_aHelperMtx)
59  , m_aMouseListeners(m_aHelperMtx)
60  , m_aMotionListeners(m_aHelperMtx)
61  , m_aPaintListeners(m_aHelperMtx)
62  {
63  }
64 
65  weld::Widget* getWidget() const { return m_pWeldWidget; }
66 
67  weld::Builder* getBuilder() const { return m_pWeldWidgetBuilder; }
68 
69  virtual void clear()
70  {
71  m_pWeldWidget = nullptr;
72  m_pWeldWidgetBuilder = nullptr;
73  }
74 
75  // css::awt::XWindow
76  void SAL_CALL setPosSize(sal_Int32, sal_Int32, sal_Int32, sal_Int32, sal_Int16) override
77  {
78  throw css::uno::RuntimeException("not implemented");
79  }
80 
81  css::awt::Rectangle SAL_CALL getPosSize() override
82  {
83  throw css::uno::RuntimeException("not implemented");
84  }
85 
86  void SAL_CALL setVisible(sal_Bool bVisible) override { m_pWeldWidget->set_visible(bVisible); }
87 
88  void SAL_CALL setEnable(sal_Bool bSensitive) override
89  {
90  m_pWeldWidget->set_sensitive(bSensitive);
91  }
92 
93  void SAL_CALL setFocus() override { m_pWeldWidget->grab_focus(); }
94 
95  void SAL_CALL
96  addWindowListener(const css::uno::Reference<css::awt::XWindowListener>& rListener) override
97  {
98  m_aWindowListeners.addInterface(rListener);
99  }
100 
101  void SAL_CALL
102  removeWindowListener(const css::uno::Reference<css::awt::XWindowListener>& rListener) override
103  {
104  m_aWindowListeners.removeInterface(rListener);
105  }
106 
107  void SAL_CALL
108  addFocusListener(const css::uno::Reference<css::awt::XFocusListener>& rListener) override
109  {
110  m_aFocusListeners.addInterface(rListener);
111  }
112 
113  void SAL_CALL
114  removeFocusListener(const css::uno::Reference<css::awt::XFocusListener>& rListener) override
115  {
116  m_aFocusListeners.removeInterface(rListener);
117  }
118 
119  void SAL_CALL
120  addKeyListener(const css::uno::Reference<css::awt::XKeyListener>& rListener) override
121  {
122  m_aKeyListeners.addInterface(rListener);
123  }
124 
125  void SAL_CALL
126  removeKeyListener(const css::uno::Reference<css::awt::XKeyListener>& rListener) override
127  {
128  m_aKeyListeners.removeInterface(rListener);
129  }
130 
131  void SAL_CALL
132  addMouseListener(const css::uno::Reference<css::awt::XMouseListener>& rListener) override
133  {
134  m_aMouseListeners.addInterface(rListener);
135  }
136 
137  void SAL_CALL
138  removeMouseListener(const css::uno::Reference<css::awt::XMouseListener>& rListener) override
139  {
140  m_aMouseListeners.removeInterface(rListener);
141  }
142 
143  void SAL_CALL addMouseMotionListener(
144  const css::uno::Reference<css::awt::XMouseMotionListener>& rListener) override
145  {
146  m_aMotionListeners.addInterface(rListener);
147  }
148 
150  const css::uno::Reference<css::awt::XMouseMotionListener>& rListener) override
151  {
152  m_aMotionListeners.removeInterface(rListener);
153  }
154 
155  void SAL_CALL
156  addPaintListener(const css::uno::Reference<css::awt::XPaintListener>& rListener) override
157  {
158  m_aPaintListeners.addInterface(rListener);
159  }
160 
161  void SAL_CALL
162  removePaintListener(const css::uno::Reference<css::awt::XPaintListener>& rListener) override
163  {
164  m_aPaintListeners.removeInterface(rListener);
165  }
166 };
167 
168 // don't export to avoid duplicate WeakImplHelper definitions with MSVC
169 class SAL_DLLPUBLIC_TEMPLATE WidgetStatusListener_Base
170  : public cppu::WeakImplHelper<css::frame::XStatusListener>
171 {
172 };
173 
175 {
176 public:
177  WidgetStatusListener(weld::Widget* widget, const OUString& rCommand);
178 
179 private:
183  css::uno::Reference<css::frame::XDispatch> mxDispatch;
184  css::util::URL maCommandURL;
185  css::uno::Reference<css::frame::XFrame> mxFrame;
186 
187 public:
188  void SAL_CALL statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
189 
190  void SAL_CALL disposing(const css::lang::EventObject& /*Source*/) override;
191 
192  const css::uno::Reference<css::frame::XFrame>& getFrame() const { return mxFrame; }
193 
194  void startListening();
195 
196  void dispose();
197 };
198 
200 {
201 public:
202  EntryFormatter(weld::Entry& rEntry);
204 
205  weld::Entry& get_widget() { return m_rEntry; }
206 
207  // public Formatter overrides, drives interactions with the Entry
208  virtual Selection GetEntrySelection() const override;
209  virtual OUString GetEntryText() const override;
210  virtual void SetEntryText(const OUString& rText, const Selection& rSel) override;
211  virtual void SetEntryTextColor(const Color* pColor) override;
212  virtual SelectionOptions GetEntrySelectionOptions() const override;
213  virtual void FieldModified() override;
214 
215  // public Formatter overrides, drives optional SpinButton settings
216  virtual void ClearMinValue() override;
217  virtual void SetMinValue(double dMin) override;
218  virtual void ClearMaxValue() override;
219  virtual void SetMaxValue(double dMin) override;
220 
221  virtual void SetSpinSize(double dStep) override;
222 
223  void SetEntrySelectionOptions(SelectionOptions eOptions) { m_eOptions = eOptions; }
224 
225  /* EntryFormatter will set listeners to "changed" and "focus-out" of the
226  Entry so users that want to add their own listeners to those must set
227  them through this formatter and not directly on that entry.
228 
229  If EntryFormatter is used with a weld::FormattedSpinButton this is
230  handled transparently by the FormattedSpinButton for the user and the
231  handlers can be set on the FormattedSpinButton
232  */
233  void connect_changed(const Link<weld::Entry&, void>& rLink) { m_aModifyHdl = rLink; }
234  void connect_focus_out(const Link<weld::Widget&, void>& rLink) { m_aFocusOutHdl = rLink; }
235 
236  virtual ~EntryFormatter() override;
237 
238 private:
244  DECL_DLLPRIVATE_LINK(ModifyHdl, weld::Entry&, void);
245  DECL_DLLPRIVATE_LINK(FocusOutHdl, weld::Widget&, void);
246  void Init();
247 
248  // private Formatter overrides
249  virtual void UpdateCurrentValue(double dCurrentValue) override;
250 };
251 
253 {
254 public:
257 
258  virtual ~DoubleNumericFormatter() override;
259 
260 private:
261  virtual bool CheckText(const OUString& sText) const override;
262 
263  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat) override;
264  void ResetConformanceTester();
265 
266  std::unique_ptr<validation::NumberValidator> m_pNumberValidator;
267 };
268 
270 {
271 public:
274 
275  void SetUseThousandSep(bool b);
276  void SetCurrencySymbol(const OUString& rStr);
277 
278  virtual ~LongCurrencyFormatter() override;
279 
280 private:
281  DECL_LINK(FormatOutputHdl, LinkParamNone*, bool);
282  DECL_LINK(ParseInputHdl, sal_Int64*, TriState);
283 
284  void Init();
285 
288 };
289 
291 {
292 public:
293  TimeFormatter(weld::Entry& rEntry);
295 
296  void SetExtFormat(ExtTimeFieldFormat eFormat);
297  void SetDuration(bool bDuration);
298  void SetTimeFormat(TimeFieldFormat eTimeFormat);
299 
300  void SetMin(const tools::Time& rNewMin);
301  void SetMax(const tools::Time& rNewMax);
302 
303  void SetTime(const tools::Time& rNewTime);
305 
306  virtual ~TimeFormatter() override;
307 
308 private:
309  DECL_LINK(FormatOutputHdl, LinkParamNone*, bool);
310  DECL_LINK(ParseInputHdl, sal_Int64*, TriState);
311  DECL_LINK(CursorChangedHdl, weld::Entry&, void);
312 
313  void Init();
314 
315  static tools::Time ConvertValue(int nValue);
316  static int ConvertValue(const tools::Time& rTime);
317 
318  OUString FormatNumber(int nValue) const;
319 
323 };
324 
326 {
327 public:
328  DateFormatter(weld::Entry& rEntry);
329 
330  void SetMin(const Date& rNewMin);
331  void SetMax(const Date& rNewMax);
332 
333  void SetDate(const Date& rNewDate);
334  Date GetDate();
335 
336  void SetExtDateFormat(ExtDateFieldFormat eFormat);
337  void SetShowDateCentury(bool bShowCentury);
338 
339  virtual ~DateFormatter() override;
340 
341 private:
342  DECL_LINK(FormatOutputHdl, LinkParamNone*, bool);
343  DECL_LINK(ParseInputHdl, sal_Int64*, TriState);
344  DECL_LINK(CursorChangedHdl, weld::Entry&, void);
345 
346  void Init();
347  CalendarWrapper& GetCalendarWrapper() const;
348 
349  OUString FormatNumber(int nValue) const;
350 
352  mutable std::unique_ptr<CalendarWrapper> m_xCalendarWrapper;
353 };
354 
356 {
357 public:
358  PatternFormatter(weld::Entry& rEntry);
359  ~PatternFormatter();
360 
361  weld::Entry& get_widget() { return m_rEntry; }
362 
363  void SetMask(const OString& rEditMask, const OUString& rLiteralMask);
364  void SetStrictFormat(bool bStrict);
365  void ReformatAll();
366 
367  /* PatternFormatter will set listeners to "changed", "focus-out", "focus-in"
368  and "key-press" of the Entry so users that want to add their own listeners
369  to those must set them through this formatter and not directly on that entry.
370  */
371  void connect_changed(const Link<weld::Entry&, void>& rLink) { m_aModifyHdl = rLink; }
372  void connect_focus_out(const Link<weld::Widget&, void>& rLink) { m_aFocusOutHdl = rLink; }
373  void connect_focus_in(const Link<weld::Widget&, void>& rLink) { m_aFocusInHdl = rLink; }
374  void connect_key_press(const Link<const KeyEvent&, bool>& rLink) { m_aKeyPressHdl = rLink; }
375 
376  void Modify();
377 
378 private:
388  OString m_aEditMask;
389  OUString m_aLiteralMask;
390 
391  void EntryGainFocus();
392  void EntryLostFocus();
393  DECL_DLLPRIVATE_LINK(ModifyHdl, weld::Entry&, void);
394  DECL_DLLPRIVATE_LINK(FocusInHdl, weld::Widget&, void);
395  DECL_DLLPRIVATE_LINK(FocusOutHdl, weld::Widget&, void);
396  DECL_DLLPRIVATE_LINK(KeyInputHdl, const KeyEvent&, bool);
397 };
398 
400  : public std::enable_shared_from_this<ButtonPressRepeater>
401 {
402 private:
407  bool m_bModKey;
408 
409  DECL_LINK(MousePressHdl, const MouseEvent&, bool);
410  DECL_LINK(MouseReleaseHdl, const MouseEvent&, bool);
411  DECL_LINK(RepeatTimerHdl, Timer*, void);
412 
413 public:
415  const Link<const CommandEvent&, void>& rContextLink
417  void Stop() { m_aRepeat.Stop(); }
418  bool IsModKeyPressed() const { return m_bModKey; }
419 };
420 
421 // get the row the iterator is on
422 VCL_DLLPUBLIC size_t GetAbsPos(const weld::TreeView& rTreeView, const weld::TreeIter& rIter);
423 
424 // an entry is visible if all parents are expanded
425 VCL_DLLPUBLIC bool IsEntryVisible(const weld::TreeView& rTreeView, const weld::TreeIter& rIter);
426 
427 // A Parent's Children are turned into Children of the Parent which comes next in hierarchy
429 
430 // return the min height of a weld::Entry
432 
433 // return the weld::Window of the SalFrame rOutWin is in, and convert rRect
434 // from relative to rOutWin to relative to that weld::Window suitable for use
435 // with popup_at_rect
437 
438 // Use Application::GetDefaultDevice to set the PointFont rFont to the OutputDevice
439 VCL_DLLPUBLIC void SetPointFont(OutputDevice& rDevice, const vcl::Font& rFont);
440 }
441 
442 #endif
443 
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Link< Button &, void > m_aLink
Definition: weldutils.hxx:405
comphelper::OInterfaceContainerHelper2 m_aMouseListeners
Definition: weldutils.hxx:47
Link< const KeyEvent &, bool > m_aKeyPressHdl
Definition: weldutils.hxx:383
weld::Builder * getBuilder() const
Definition: weldutils.hxx:67
void Init()
weld::Widget * m_pWeldWidget
Definition: weldutils.hxx:41
ExtTimeFieldFormat
Definition: vclenum.hxx:146
comphelper::OInterfaceContainerHelper2 m_aPaintListeners
Definition: weldutils.hxx:49
TimeFieldFormat
Definition: vclenum.hxx:28
TransportAsXWindow(weld::Widget *pWeldWidget, weld::Builder *pWeldWidgetBuilder=nullptr)
Definition: weldutils.hxx:52
size_t GetAbsPos(const weld::TreeView &rTreeView, const weld::TreeIter &rIter)
Definition: builder.cxx:444
void SAL_CALL addFocusListener(const css::uno::Reference< css::awt::XFocusListener > &rListener) override
Definition: weldutils.hxx:108
SelectionOptions
Definition: settings.hxx:179
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
void SAL_CALL addMouseMotionListener(const css::uno::Reference< css::awt::XMouseMotionListener > &rListener) override
Definition: weldutils.hxx:143
sal_Int32 addInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
FORMAT_CHANGE_TYPE
Definition: formatter.hxx:75
Any GetTime(const OUString &val)
Link< weld::Entry &, void > m_aModifyHdl
Definition: weldutils.hxx:241
void RemoveParentKeepChildren(weld::TreeView &rTreeView, weld::TreeIter &rParent)
Definition: weldutils.cxx:117
sal_Int32 removeInterface(const css::uno::Reference< css::uno::XInterface > &rxIFace)
comphelper::OInterfaceContainerHelper2 m_aMotionListeners
Definition: weldutils.hxx:48
TimeFormat m_eTimeFormat
Definition: weldutils.hxx:321
void SAL_CALL removeMouseListener(const css::uno::Reference< css::awt::XMouseListener > &rListener) override
Definition: weldutils.hxx:138
void SAL_CALL removeKeyListener(const css::uno::Reference< css::awt::XKeyListener > &rListener) override
Definition: weldutils.hxx:126
virtual void clear()
Definition: weldutils.hxx:69
void connect_changed(const Link< weld::Entry &, void > &rLink)
Definition: weldutils.hxx:233
void connect_focus_out(const Link< weld::Widget &, void > &rLink)
Definition: weldutils.hxx:372
TimeFormat
Definition: vclenum.hxx:141
void SAL_CALL addPaintListener(const css::uno::Reference< css::awt::XPaintListener > &rListener) override
Definition: weldutils.hxx:156
Link< weld::Widget &, void > m_aFocusOutHdl
Definition: weldutils.hxx:242
css::uno::Reference< css::frame::XDispatch > mxDispatch
The widget on which actions are performed.
Definition: weldutils.hxx:183
OUString FormatNumber(sal_uInt32 nNum, SvxNumType nFormat, LanguageType nLang)
void SAL_CALL addKeyListener(const css::uno::Reference< css::awt::XKeyListener > &rListener) override
Definition: weldutils.hxx:120
css::util::URL maCommandURL
Definition: weldutils.hxx:184
void SAL_CALL removePaintListener(const css::uno::Reference< css::awt::XPaintListener > &rListener) override
Definition: weldutils.hxx:162
ExtDateFieldFormat m_eFormat
Definition: weldutils.hxx:351
weld::Entry & m_rEntry
Definition: weldutils.hxx:239
bool IsModKeyPressed() const
Definition: weldutils.hxx:418
Link< weld::Widget &, void > m_aFocusInHdl
Definition: weldutils.hxx:381
void connect_focus_out(const Link< weld::Widget &, void > &rLink)
Definition: weldutils.hxx:234
An auto-timer is a multi-shot timer re-emitting itself at interval until destroyed or stopped...
Definition: timer.hxx:72
void connect_focus_in(const Link< weld::Widget &, void > &rLink)
Definition: weldutils.hxx:373
cppu::WeakComponentImplHelper< css::awt::XWindow > TransportAsXWindow_Base
Definition: weldutils.hxx:35
void SAL_CALL setEnable(sal_Bool bSensitive) override
Definition: weldutils.hxx:88
weld::Window * GetPopupParent(vcl::Window &rOutWin, tools::Rectangle &rRect)
Definition: weldutils.cxx:603
void SAL_CALL setFocus() override
Definition: weldutils.hxx:93
comphelper::OInterfaceContainerHelper2 m_aKeyListeners
Definition: weldutils.hxx:45
const Link< const CommandEvent &, void > m_aContextLink
Definition: weldutils.hxx:406
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:303
unsigned char sal_Bool
Link< weld::Entry &, void > m_aModifyHdl
Definition: weldutils.hxx:380
comphelper::OInterfaceContainerHelper2 m_aFocusListeners
Definition: weldutils.hxx:46
TimeFieldFormat m_eFormat
Definition: weldutils.hxx:320
void SAL_CALL setVisible(sal_Bool bVisible) override
Definition: weldutils.hxx:86
weld::Entry & get_widget()
Definition: weldutils.hxx:205
void SAL_CALL removeFocusListener(const css::uno::Reference< css::awt::XFocusListener > &rListener) override
Definition: weldutils.hxx:114
Link< weld::Widget &, void > m_aFocusOutHdl
Definition: weldutils.hxx:382
void SAL_CALL addWindowListener(const css::uno::Reference< css::awt::XWindowListener > &rListener) override
Definition: weldutils.hxx:96
ExtDateFieldFormat
Definition: vclenum.hxx:154
weld::Entry & get_widget()
Definition: weldutils.hxx:361
weld::Builder * m_pWeldWidgetBuilder
Definition: weldutils.hxx:42
void Stop()
Definition: scheduler.cxx:587
std::unique_ptr< CalendarWrapper > m_xCalendarWrapper
Definition: weldutils.hxx:352
std::unique_ptr< validation::NumberValidator > m_pNumberValidator
Definition: weldutils.hxx:266
virtual void grab_focus()=0
void SAL_CALL removeMouseMotionListener(const css::uno::Reference< css::awt::XMouseMotionListener > &rListener) override
Definition: weldutils.hxx:149
void connect_key_press(const Link< const KeyEvent &, bool > &rLink)
Definition: weldutils.hxx:374
void SetPointFont(OutputDevice &rDevice, const vcl::Font &rFont)
Definition: weldutils.cxx:616
weld::Button & m_rButton
Definition: weldutils.hxx:403
css::uno::Reference< css::frame::XFrame > mxFrame
Definition: weldutils.hxx:185
comphelper::OInterfaceContainerHelper2 m_aWindowListeners
Definition: weldutils.hxx:44
void SAL_CALL addMouseListener(const css::uno::Reference< css::awt::XMouseListener > &rListener) override
Definition: weldutils.hxx:132
sal_Int64 ConvertValue(sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits, FieldUnit eInUnit, FieldUnit eOutUnit)
Definition: field.cxx:1020
weld::Entry & m_rEntry
Definition: weldutils.hxx:379
void SAL_CALL removeWindowListener(const css::uno::Reference< css::awt::XWindowListener > &rListener) override
Definition: weldutils.hxx:102
Definition: timer.hxx:26
SelectionOptions m_eOptions
Definition: weldutils.hxx:243
void dispose()
TriState
void connect_changed(const Link< weld::Entry &, void > &rLink)
Definition: weldutils.hxx:371
void SetEntrySelectionOptions(SelectionOptions eOptions)
Definition: weldutils.hxx:223
const css::uno::Reference< css::frame::XFrame > & getFrame() const
Definition: weldutils.hxx:192
int GetMinimumEditHeight()
Definition: weldutils.cxx:489
bool IsEntryVisible(const weld::TreeView &rTreeView, const weld::TreeIter &rIter)
Definition: builder.cxx:462
void SAL_CALL setPosSize(sal_Int32, sal_Int32, sal_Int32, sal_Int32, sal_Int16) override
Definition: weldutils.hxx:76
virtual void set_visible(bool visible)
Definition: weld.hxx:100
weld::Widget * getWidget() const
Definition: weldutils.hxx:65
virtual void set_sensitive(bool sensitive)=0
sal_Int16 nValue
css::awt::Rectangle SAL_CALL getPosSize() override
Definition: weldutils.hxx:81
weld::FormattedSpinButton * m_pSpinButton
Definition: weldutils.hxx:240