cool#6580 sw: fix infinite loop when changing document language
[LibreOffice.git] / include / vcl / button.hxx
blobf37b714faf9f04d5a7e8c704e74c1144b7c4c555
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 #ifndef INCLUDED_VCL_BUTTON_HXX
21 #define INCLUDED_VCL_BUTTON_HXX
23 #include <tools/solar.h>
24 #include <vcl/dllapi.h>
25 #include <vcl/image.hxx>
26 #include <vcl/ctrl.hxx>
27 #include <vcl/vclenum.hxx>
28 #include <memory>
29 #include <vector>
31 namespace com { namespace sun { namespace star { namespace frame { struct FeatureStateEvent; } } } }
32 template <class T> class VclPtr;
34 class Color;
35 class ImplCommonButtonData;
36 enum class DrawButtonFlags;
38 class VCL_DLLPUBLIC Button : public Control
40 private:
41 std::unique_ptr<ImplCommonButtonData> mpButtonData;
42 Link<Button*,void> maClickHdl;
44 /// Command URL (like .uno:Save) in case the button should handle it.
45 OUString maCommand;
47 Button (const Button &) = delete;
48 Button & operator= (const Button &) = delete;
49 public:
50 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( WinBits nWinStyle, DrawFlags nDrawFlags );
51 SAL_DLLPRIVATE void ImplDrawAlignedImage(OutputDevice* pDev, Point& rPos, Size& rSize,
52 sal_uLong nImageSep,
53 DrawTextFlags nTextStyle, tools::Rectangle *pSymbolRect=nullptr, bool bAddImageSep = false );
54 SAL_DLLPRIVATE void ImplSetFocusRect( const tools::Rectangle &rFocusRect );
55 SAL_DLLPRIVATE const tools::Rectangle& ImplGetFocusRect() const;
56 SAL_DLLPRIVATE void ImplSetSymbolAlign( SymbolAlign eAlign );
57 /// The x-coordinate of the vertical separator line, use in MenuButton subclass only.
58 SAL_DLLPRIVATE long ImplGetSeparatorX() const;
59 SAL_DLLPRIVATE void ImplSetSeparatorX( long nX );
61 protected:
62 explicit Button( WindowType nType );
64 public:
65 virtual ~Button() override;
66 virtual void dispose() override;
68 virtual void Click();
70 void SetClickHdl( const Link<Button*,void>& rLink ) { maClickHdl = rLink; }
71 const Link<Button*,void>& GetClickHdl() const { return maClickHdl; }
73 /// Setup handler for UNO commands so that commands like .uno:Something are handled automagically by this button.
74 void SetCommandHandler(const OUString& aCommand);
75 OUString const & GetCommand() const { return maCommand; }
77 void SetModeImage( const Image& rImage );
78 Image const & GetModeImage( ) const;
79 bool HasImage() const;
80 void SetImageAlign( ImageAlign eAlign );
81 ImageAlign GetImageAlign() const;
82 DrawButtonFlags GetButtonState() const;
83 DrawButtonFlags& GetButtonState();
86 bool IsSmallSymbol() const;
87 void SetSmallSymbol();
88 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
90 /// Sets the button state according to the FeatureStateEvent emitted by a Uno state change.
91 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent);
93 virtual FactoryFunction GetUITestFactory() const override;
95 virtual boost::property_tree::ptree DumpAsPropertyTree() override;
97 protected:
99 /// Handler for click, in case we want the button to handle uno commands (.uno:Something).
100 DECL_STATIC_LINK(Button, dispatchCommandHandler, Button*, void);
103 enum class PushButtonDropdownStyle
105 NONE = 0x0000,
106 Toolbox = 0x0001,
107 MenuButton = 0x0002, //visual down arrow
108 SplitMenuButton = 0x0003, //visual down arrow and separator line
111 class VCL_DLLPUBLIC PushButton : public Button
113 public:
114 SAL_DLLPRIVATE void ImplSetDefButton( bool bSet );
115 SAL_DLLPRIVATE void ImplDrawPushButtonFrame(vcl::RenderContext& rRenderContext, tools::Rectangle& rRect, DrawButtonFlags nStyle);
116 SAL_DLLPRIVATE static bool ImplHitTestPushButton(vcl::Window const * pDev, const Point& rPos);
117 SAL_DLLPRIVATE bool ImplIsDefButton() const;
119 explicit PushButton( vcl::Window* pParent, WinBits nStyle = 0 );
121 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
122 virtual void Tracking( const TrackingEvent& rTEvt ) override;
123 virtual void KeyInput( const KeyEvent& rKEvt ) override;
124 virtual void KeyUp( const KeyEvent& rKEvt ) override;
125 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
126 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
127 virtual void Resize() override;
128 virtual void GetFocus() override;
129 virtual void LoseFocus() override;
130 virtual void StateChanged( StateChangedType nType ) override;
131 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
132 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
134 void Toggle();
136 void SetSymbol( SymbolType eSymbol );
137 SymbolType GetSymbol() const { return meSymbol; }
138 void SetSymbolAlign( SymbolAlign eAlign );
140 void SetDropDown( PushButtonDropdownStyle nStyle );
142 void SetState( TriState eState );
143 TriState GetState() const { return meState; }
144 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
146 void Check( bool bCheck = true );
147 bool IsChecked() const;
149 void SetPressed( bool bPressed );
150 bool IsPressed() const { return mbPressed; }
152 void EndSelection();
154 Size CalcMinimumSize() const;
155 virtual Size GetOptimalSize() const override;
157 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
158 virtual void ShowFocus(const tools::Rectangle& rRect) override;
160 void setAction(bool bIsAction)
162 mbIsAction = bIsAction;
165 bool isAction() const
167 return mbIsAction;
170 protected:
171 PushButtonDropdownStyle mnDDStyle;
172 bool mbIsActive;
174 SAL_DLLPRIVATE void ImplInitPushButtonData();
175 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
176 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
177 SAL_DLLPRIVATE void ImplDrawPushButtonContent(OutputDevice* pDev, DrawFlags nDrawFlags,
178 const tools::Rectangle& rRect, bool bMenuBtnSep,
179 DrawButtonFlags nButtonFlags);
180 SAL_DLLPRIVATE void ImplDrawPushButton(vcl::RenderContext& rRenderContext);
181 using Button::ImplGetTextStyle;
182 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( DrawFlags nDrawFlags ) const;
183 SAL_DLLPRIVATE bool IsSymbol() const { return ( (meSymbol != SymbolType::DONTKNOW) && (meSymbol != SymbolType::IMAGE) ); }
185 PushButton( const PushButton & ) = delete;
186 PushButton& operator=( const PushButton & )
187 = delete;
189 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
191 using Control::ImplInitSettings;
192 using Window::ImplInit;
194 explicit PushButton( WindowType nType );
196 virtual void FillLayoutData() const override;
197 virtual const vcl::Font&
198 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
199 virtual const Color&
200 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
202 private:
203 SymbolType meSymbol;
204 TriState meState;
205 bool mbPressed;
206 bool mbIsAction;
209 inline void PushButton::Check( bool bCheck )
211 SetState( bCheck ? TRISTATE_TRUE : TRISTATE_FALSE );
214 inline bool PushButton::IsChecked() const
216 return (GetState() == TRISTATE_TRUE);
219 class VCL_DLLPUBLIC OKButton : public PushButton
221 protected:
222 using PushButton::ImplInit;
223 private:
224 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
226 OKButton (const OKButton &) = delete;
227 OKButton & operator= (const OKButton &) = delete;
229 public:
230 explicit OKButton( vcl::Window* pParent, WinBits nStyle = WB_DEFBUTTON );
232 virtual void Click() override;
235 class VCL_DLLPUBLIC CancelButton : public PushButton
237 protected:
238 using PushButton::ImplInit;
239 private:
240 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
242 CancelButton (const CancelButton &) = delete;
243 CancelButton & operator= (const CancelButton &) = delete;
245 public:
246 explicit CancelButton( vcl::Window* pParent, WinBits nStyle = 0 );
248 virtual void Click() override;
251 class CloseButton final : public CancelButton
253 public:
254 explicit CloseButton(vcl::Window* pParent, WinBits nStyle = 0);
257 class VCL_DLLPUBLIC HelpButton : public PushButton
259 protected:
260 using PushButton::ImplInit;
261 private:
262 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
264 HelpButton( const HelpButton & ) = delete;
265 HelpButton & operator= ( const HelpButton & ) = delete;
267 virtual void StateChanged( StateChangedType nStateChange ) override;
269 public:
270 explicit HelpButton( vcl::Window* pParent, WinBits nStyle = 0 );
272 virtual void Click() override;
275 class VCL_DLLPUBLIC RadioButton : public Button
277 private:
278 friend class VclBuilder;
280 std::shared_ptr< std::vector< VclPtr< RadioButton > > > m_xGroup;
281 tools::Rectangle maStateRect;
282 tools::Rectangle maMouseRect;
283 Image maImage;
284 bool mbChecked;
285 bool mbRadioCheck;
286 bool mbStateChanged;
287 Link<RadioButton&,void> maToggleHdl;
288 SAL_DLLPRIVATE void ImplInitRadioButtonData();
289 SAL_DLLPRIVATE WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
290 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
291 SAL_DLLPRIVATE void ImplDrawRadioButtonState(vcl::RenderContext& rRenderContext);
292 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, DrawFlags nDrawFlags,
293 const Point& rPos, const Size& rSize,
294 const Size& rImageSize, tools::Rectangle& rStateRect,
295 tools::Rectangle& rMouseRect );
296 SAL_DLLPRIVATE void ImplDrawRadioButton(vcl::RenderContext& rRenderContext );
297 SAL_DLLPRIVATE void ImplUncheckAllOther( const bool bSetStyle = true);
298 SAL_DLLPRIVATE Size ImplGetRadioImageSize() const;
299 SAL_DLLPRIVATE long ImplGetImageToTextDistance() const;
301 RadioButton(const RadioButton &) = delete;
302 RadioButton& operator= (const RadioButton &) = delete;
304 protected:
305 using Control::ImplInitSettings;
306 using Window::ImplInit;
307 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
309 public:
310 SAL_DLLPRIVATE void ImplCallClick( bool bGrabFocus = false, GetFocusFlags nFocusFlags = GetFocusFlags::NONE );
311 SAL_DLLPRIVATE void ImplSetMinimumNWFSize();
313 protected:
314 virtual void FillLayoutData() const override;
315 virtual const vcl::Font&
316 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
317 virtual const Color&
318 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
320 public:
321 explicit RadioButton( vcl::Window* pParent, WinBits nWinStyle = 0 );
322 virtual ~RadioButton() override;
323 virtual void dispose() override;
325 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
326 virtual void Tracking( const TrackingEvent& rTEvt ) override;
327 virtual void KeyInput( const KeyEvent& rKEvt ) override;
328 virtual void KeyUp( const KeyEvent& rKEvt ) override;
329 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
330 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
331 virtual void Resize() override;
332 virtual void GetFocus() override;
333 virtual void LoseFocus() override;
334 virtual void StateChanged( StateChangedType nType ) override;
335 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
336 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
338 void Toggle();
340 bool IsStateChanged() const { return mbStateChanged; }
342 void EnableRadioCheck( bool bRadioCheck ) { mbRadioCheck = bRadioCheck; }
343 bool IsRadioCheckEnabled() const { return mbRadioCheck; }
345 void SetModeRadioImage( const Image& rImage );
347 void SetState( bool bCheck );
348 void Check( bool bCheck = true );
349 bool IsChecked() const { return mbChecked; }
351 static Image GetRadioImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
353 Size CalcMinimumSize( long nMaxWidth = 0 ) const;
354 virtual Size GetOptimalSize() const override;
356 void SetToggleHdl( const Link<RadioButton&,void>& rLink ) { maToggleHdl = rLink; }
358 /** GetRadioButtonGroup returns a list of pointers to <code>RadioButton</code>s in the same group.
360 The pointers in the returned list are valid at the time call returns. However rescheduling
361 or giving up the SolarMutex may mean events get executed that lead to the pointers getting
362 invalid.
364 @param bIncludeThis
365 defines whether <code>this</code> is contained in the returned list
367 @return
368 on return contains the <code>RadioButton</code>s
369 in the same group as this <code>RadioButton</code>.
371 std::vector<VclPtr<RadioButton> > GetRadioButtonGroup(bool bIncludeThis = true) const;
373 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
376 * Group this RadioButton with another
378 void group(RadioButton &rOther);
379 virtual void ShowFocus(const tools::Rectangle& rRect) override;
381 /// Button hes additional stuff that we need to dump too.
382 boost::property_tree::ptree DumpAsPropertyTree() override;
384 virtual FactoryFunction GetUITestFactory() const override;
387 class VCL_DLLPUBLIC CheckBox : public Button
389 private:
390 tools::Rectangle maStateRect;
391 tools::Rectangle maMouseRect;
392 TriState meState;
393 TriState meSaveValue;
394 bool mbTriState;
395 Link<CheckBox&,void> maToggleHdl;
396 // when mbLegacyNoTextAlign is set then the old behaviour where
397 // the WB_LEFT, WB_RIGHT & WB_CENTER affect the image placement
398 // occurs, otherwise the image ( checkbox box ) is placed
399 // to the left or right ( depending on RTL or LTR settings )
400 bool mbLegacyNoTextAlign;
401 SAL_DLLPRIVATE void ImplInitCheckBoxData();
402 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
403 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
404 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, DrawFlags nDrawFlags,
405 const Point& rPos, const Size& rSize,
406 const Size& rImageSize, tools::Rectangle& rStateRect,
407 tools::Rectangle& rMouseRect );
408 SAL_DLLPRIVATE void ImplDrawCheckBox(vcl::RenderContext& rRenderContext );
409 SAL_DLLPRIVATE long ImplGetImageToTextDistance() const;
410 SAL_DLLPRIVATE Size ImplGetCheckImageSize() const;
412 CheckBox(const CheckBox &) = delete;
413 CheckBox& operator= (const CheckBox &) = delete;
415 protected:
416 using Control::ImplInitSettings;
417 using Window::ImplInit;
418 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
419 virtual void FillLayoutData() const override;
420 virtual const vcl::Font& GetCanonicalFont( const StyleSettings& _rStyle ) const override;
421 virtual const Color& GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
423 virtual void ImplDrawCheckBoxState(vcl::RenderContext& rRenderContext);
424 SAL_DLLPRIVATE const tools::Rectangle& GetStateRect() const { return maStateRect; }
425 SAL_DLLPRIVATE const tools::Rectangle& GetMouseRect() const { return maMouseRect; }
427 public:
428 SAL_DLLPRIVATE void ImplCheck();
429 SAL_DLLPRIVATE void ImplSetMinimumNWFSize();
430 public:
431 explicit CheckBox( vcl::Window* pParent, WinBits nStyle = 0 );
433 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
434 virtual void Tracking( const TrackingEvent& rTEvt ) override;
435 virtual void KeyInput( const KeyEvent& rKEvt ) override;
436 virtual void KeyUp( const KeyEvent& rKEvt ) override;
437 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
438 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
439 virtual void Resize() override;
440 virtual void GetFocus() override;
441 virtual void LoseFocus() override;
442 virtual void StateChanged( StateChangedType nType ) override;
443 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
444 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
446 void Toggle();
448 void SetState( TriState eState );
449 TriState GetState() const { return meState; }
451 void Check( bool bCheck = true );
452 bool IsChecked() const;
454 void EnableTriState( bool bTriState = true );
455 bool IsTriStateEnabled() const { return mbTriState; }
457 void SaveValue() { meSaveValue = GetState(); }
458 TriState GetSavedValue() const { return meSaveValue; }
459 bool IsValueChangedFromSaved() const { return meSaveValue != GetState(); }
461 static Image GetCheckImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
463 Size CalcMinimumSize( long nMaxWidth = 0 ) const;
464 virtual Size GetOptimalSize() const override;
466 void SetToggleHdl( const Link<CheckBox&,void>& rLink ) { maToggleHdl = rLink; }
467 void SetLegacyNoTextAlign( bool bVal ) { mbLegacyNoTextAlign = bVal; }
469 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
470 virtual void ShowFocus(const tools::Rectangle& rRect) override;
472 /// Button hes additional stuff that we need to dump too.
473 boost::property_tree::ptree DumpAsPropertyTree() override;
475 virtual FactoryFunction GetUITestFactory() const override;
478 inline void CheckBox::Check( bool bCheck )
480 SetState( bCheck ? TRISTATE_TRUE : TRISTATE_FALSE );
483 inline bool CheckBox::IsChecked() const
485 return (GetState() == TRISTATE_TRUE);
488 class VCL_DLLPUBLIC ImageButton final : public PushButton
490 protected:
491 using PushButton::ImplInitStyle;
493 private:
494 SAL_DLLPRIVATE void ImplInitStyle();
496 ImageButton( const ImageButton & ) = delete;
497 ImageButton & operator= ( const ImageButton & ) = delete;
499 public:
500 ImageButton( vcl::Window* pParent, WinBits nStyle = 0 );
503 class VCL_DLLPUBLIC ImageRadioButton : public RadioButton
505 ImageRadioButton( const ImageRadioButton & ) = delete;
506 ImageRadioButton & operator= ( const ImageRadioButton & ) = delete;
508 public:
509 explicit ImageRadioButton( vcl::Window* pParent );
512 class VCL_DLLPUBLIC TriStateBox final : public CheckBox
514 TriStateBox( const TriStateBox & ) = delete;
515 TriStateBox & operator= ( const TriStateBox & ) = delete;
517 public:
518 explicit TriStateBox( vcl::Window* pParent, WinBits nStyle );
521 class DisclosureButton final : public CheckBox
523 virtual void ImplDrawCheckBoxState(vcl::RenderContext& rRenderContext) override;
525 public:
526 explicit DisclosureButton( vcl::Window* pParent );
528 virtual void KeyInput( const KeyEvent& rKEvt ) override;
531 #endif // INCLUDED_VCL_BUTTON_HXX
533 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */