Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / vcl / button.hxx
blob96689dd048768d3efb36080c1f8d72eb9f3311cb
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 <tools/color.hxx>
25 #include <vcl/dllapi.h>
26 #include <vcl/image.hxx>
27 #include <vcl/ctrl.hxx>
28 #include <vcl/bitmap.hxx>
29 #include <vcl/salnativewidgets.hxx>
30 #include <rsc/rsc-vcl-shared-types.hxx>
31 #include <vcl/vclptr.hxx>
32 #include <memory>
33 #include <vector>
35 #include <com/sun/star/frame/FeatureStateEvent.hpp>
37 class UserDrawEvent;
38 class ImplCommonButtonData;
39 enum class DrawButtonFlags;
41 class VCL_DLLPUBLIC Button : public Control
43 private:
44 std::unique_ptr<ImplCommonButtonData> mpButtonData;
45 Link<Button*,void> maClickHdl;
47 /// Command URL (like .uno:Save) in case the button should handle it.
48 OUString maCommand;
50 Button (const Button &) = delete;
51 Button & operator= (const Button &) = delete;
52 public:
53 SAL_DLLPRIVATE DrawButtonFlags ImplGetButtonState() const;
54 SAL_DLLPRIVATE DrawButtonFlags& ImplGetButtonState();
55 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( OUString& rText, WinBits nWinStyle, DrawFlags nDrawFlags );
56 SAL_DLLPRIVATE void ImplDrawAlignedImage(OutputDevice* pDev, Point& rPos, Size& rSize,
57 sal_uLong nImageSep, DrawFlags nDrawFlags,
58 DrawTextFlags nTextStyle, tools::Rectangle *pSymbolRect=nullptr, bool bAddImageSep = false );
59 SAL_DLLPRIVATE void ImplSetFocusRect( const tools::Rectangle &rFocusRect );
60 SAL_DLLPRIVATE const tools::Rectangle& ImplGetFocusRect() const;
61 SAL_DLLPRIVATE void ImplSetSymbolAlign( SymbolAlign eAlign );
62 /// The x-coordinate of the vertical separator line, use in MenuButton subclass only.
63 SAL_DLLPRIVATE long ImplGetSeparatorX() const;
64 SAL_DLLPRIVATE void ImplSetSeparatorX( long nX );
66 protected:
67 explicit Button( WindowType nType );
69 public:
70 virtual ~Button() override;
71 virtual void dispose() override;
73 virtual void Click();
75 void SetClickHdl( const Link<Button*,void>& rLink ) { maClickHdl = rLink; }
76 const Link<Button*,void>& GetClickHdl() const { return maClickHdl; }
78 /// Setup handler for UNO commands so that commands like .uno:Something are handled automagically by this button.
79 void SetCommandHandler(const OUString& aCommand);
80 const OUString GetCommand() const { return maCommand; }
82 static OUString GetStandardText( StandardButtonType eButton );
84 bool SetModeImage( const Image& rImage );
85 const Image GetModeImage( ) const;
86 bool HasImage() const;
87 void SetImageAlign( ImageAlign eAlign );
88 ImageAlign GetImageAlign() const;
90 void EnableImageDisplay( bool bEnable );
91 void EnableTextDisplay( bool bEnable );
93 void SetFocusRect( const tools::Rectangle& rFocusRect );
94 bool IsSmallSymbol() const;
95 void SetSmallSymbol();
96 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
98 /// Sets the button state according to the FeatureStateEvent emitted by an Uno state change.
99 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent);
101 virtual FactoryFunction GetUITestFactory() const override;
103 protected:
105 /// Handler for click, in case we want the button to handle uno commands (.uno:Something).
106 DECL_STATIC_LINK(Button, dispatchCommandHandler, Button*, void);
109 enum class PushButtonDropdownStyle
111 NONE = 0x0000,
112 Toolbox = 0x0001,
113 MenuButton = 0x0002, //visual down arrow
114 SplitMenuButton = 0x0003, //visual down arrow and separator line
117 class VCL_DLLPUBLIC PushButton : public Button
119 public:
120 SAL_DLLPRIVATE void ImplSetDefButton( bool bSet );
121 SAL_DLLPRIVATE void ImplDrawPushButtonFrame(vcl::RenderContext& rRenderContext, tools::Rectangle& rRect, DrawButtonFlags nStyle);
122 SAL_DLLPRIVATE static bool ImplHitTestPushButton(vcl::Window* pDev, const Point& rPos);
123 SAL_DLLPRIVATE bool ImplIsDefButton() const;
125 explicit PushButton( vcl::Window* pParent, WinBits nStyle = 0 );
127 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
128 virtual void Tracking( const TrackingEvent& rTEvt ) override;
129 virtual void KeyInput( const KeyEvent& rKEvt ) override;
130 virtual void KeyUp( const KeyEvent& rKEvt ) override;
131 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
132 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
133 virtual void Resize() override;
134 virtual void GetFocus() override;
135 virtual void LoseFocus() override;
136 virtual void StateChanged( StateChangedType nType ) override;
137 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
138 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
140 void Toggle();
142 void SetSymbol( SymbolType eSymbol );
143 SymbolType GetSymbol() const { return meSymbol; }
144 void SetSymbolAlign( SymbolAlign eAlign );
146 void SetDropDown( PushButtonDropdownStyle nStyle );
148 void SetState( TriState eState );
149 TriState GetState() const { return meState; }
150 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
152 void Check( bool bCheck = true );
153 bool IsChecked() const;
155 void SetPressed( bool bPressed );
156 bool IsPressed() const { return mbPressed; }
158 void EndSelection();
160 Size CalcMinimumSize() const;
161 virtual Size GetOptimalSize() const override;
163 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
164 virtual void ShowFocus(const tools::Rectangle& rRect) override;
166 protected:
167 PushButtonDropdownStyle mnDDStyle;
168 bool mbIsActive;
170 SAL_DLLPRIVATE void ImplInitPushButtonData();
171 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
172 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
173 SAL_DLLPRIVATE void ImplDrawPushButtonContent(OutputDevice* pDev, DrawFlags nDrawFlags,
174 const tools::Rectangle& rRect, bool bMenuBtnSep);
175 SAL_DLLPRIVATE void ImplDrawPushButton(vcl::RenderContext& rRenderContext);
176 using Button::ImplGetTextStyle;
177 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( DrawFlags nDrawFlags ) const;
178 SAL_DLLPRIVATE bool IsSymbol() const { return ( (meSymbol != SymbolType::DONTKNOW) && (meSymbol != SymbolType::IMAGE) ); }
180 PushButton( const PushButton & ) = delete;
181 PushButton& operator=( const PushButton & )
182 = delete;
184 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
186 using Control::ImplInitSettings;
187 using Window::ImplInit;
189 explicit PushButton( WindowType nType );
191 virtual void FillLayoutData() const override;
192 virtual const vcl::Font&
193 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
194 virtual const Color&
195 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
197 private:
198 SymbolType meSymbol;
199 TriState meState;
200 bool mbPressed;
203 inline void PushButton::Check( bool bCheck )
205 SetState( (bCheck) ? TRISTATE_TRUE : TRISTATE_FALSE );
208 inline bool PushButton::IsChecked() const
210 return (GetState() == TRISTATE_TRUE);
213 class VCL_DLLPUBLIC OKButton : public PushButton
215 protected:
216 using PushButton::ImplInit;
217 private:
218 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
220 OKButton (const OKButton &) = delete;
221 OKButton & operator= (const OKButton &) = delete;
223 public:
224 explicit OKButton( vcl::Window* pParent, WinBits nStyle = WB_DEFBUTTON );
226 virtual void Click() override;
229 class VCL_DLLPUBLIC CancelButton : public PushButton
231 protected:
232 using PushButton::ImplInit;
233 private:
234 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
236 CancelButton (const CancelButton &) = delete;
237 CancelButton & operator= (const CancelButton &) = delete;
239 public:
240 explicit CancelButton( vcl::Window* pParent, WinBits nStyle = 0 );
242 virtual void Click() override;
245 class VCL_DLLPUBLIC CloseButton : public CancelButton
247 public:
248 explicit CloseButton(vcl::Window* pParent, WinBits nStyle = 0);
251 class VCL_DLLPUBLIC HelpButton : public PushButton
253 protected:
254 using PushButton::ImplInit;
255 private:
256 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
258 HelpButton( const HelpButton & ) = delete;
259 HelpButton & operator= ( const HelpButton & ) = delete;
261 public:
262 explicit HelpButton( vcl::Window* pParent, WinBits nStyle = 0 );
264 virtual void Click() override;
267 class VCL_DLLPUBLIC RadioButton : public Button
269 private:
270 std::shared_ptr< std::vector< VclPtr< RadioButton > > > m_xGroup;
271 tools::Rectangle maStateRect;
272 tools::Rectangle maMouseRect;
273 Image maImage;
274 bool mbChecked;
275 bool mbSaveValue;
276 bool mbRadioCheck;
277 bool mbStateChanged;
278 Link<RadioButton&,void> maToggleHdl;
279 // when mbLegacyNoTextAlign is set then the old behaviour where
280 // the WB_LEFT, WB_RIGHT & WB_CENTER affect the image placement
281 // occurs, otherwise the image ( radiobutton circle ) is placed
282 // to the left or right ( depending on RTL or LTR settings )
283 bool mbLegacyNoTextAlign;
284 SAL_DLLPRIVATE void ImplInitRadioButtonData();
285 SAL_DLLPRIVATE WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
286 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
287 SAL_DLLPRIVATE void ImplDrawRadioButtonState(vcl::RenderContext& rRenderContext);
288 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, DrawFlags nDrawFlags,
289 const Point& rPos, const Size& rSize,
290 const Size& rImageSize, tools::Rectangle& rStateRect,
291 tools::Rectangle& rMouseRect );
292 SAL_DLLPRIVATE void ImplDrawRadioButton(vcl::RenderContext& rRenderContext );
293 SAL_DLLPRIVATE void ImplUncheckAllOther();
294 SAL_DLLPRIVATE Size ImplGetRadioImageSize() const;
295 SAL_DLLPRIVATE long ImplGetImageToTextDistance() const;
297 RadioButton(const RadioButton &) = delete;
298 RadioButton& operator= (const RadioButton &) = delete;
300 protected:
301 using Control::ImplInitSettings;
302 using Window::ImplInit;
303 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
305 public:
306 SAL_DLLPRIVATE void ImplCallClick( bool bGrabFocus = false, GetFocusFlags nFocusFlags = GetFocusFlags::NONE );
307 SAL_DLLPRIVATE void ImplSetMinimumNWFSize();
309 protected:
310 virtual void FillLayoutData() const override;
311 virtual const vcl::Font&
312 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
313 virtual const Color&
314 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
316 void SetMouseRect( const tools::Rectangle& _rMouseRect ) { maMouseRect = _rMouseRect; }
317 void SetStateRect( const tools::Rectangle& _rStateRect ) { maStateRect = _rStateRect; }
319 // draws the radio button (the knob image), in its current state (pressed/checked)
320 // at the usual location, which can be overridden with SetStateRect
321 void DrawRadioButtonState(vcl::RenderContext& rRenderContext);
323 public:
324 explicit RadioButton( vcl::Window* pParent, WinBits nWinStyle = 0 );
325 virtual ~RadioButton() override;
326 virtual void dispose() override;
328 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
329 virtual void Tracking( const TrackingEvent& rTEvt ) override;
330 virtual void KeyInput( const KeyEvent& rKEvt ) override;
331 virtual void KeyUp( const KeyEvent& rKEvt ) override;
332 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
333 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
334 virtual void Resize() override;
335 virtual void GetFocus() override;
336 virtual void LoseFocus() override;
337 virtual void StateChanged( StateChangedType nType ) override;
338 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
339 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
341 void Toggle();
343 bool IsStateChanged() const { return mbStateChanged; }
345 void EnableRadioCheck( bool bRadioCheck ) { mbRadioCheck = bRadioCheck; }
346 bool IsRadioCheckEnabled() const { return mbRadioCheck; }
348 bool SetModeRadioImage( const Image& rImage );
349 const Image& GetModeRadioImage( ) const { return maImage;}
351 void SetState( bool bCheck );
352 void Check( bool bCheck = true );
353 bool IsChecked() const { return mbChecked; }
355 void SaveValue() { mbSaveValue = IsChecked(); }
356 bool GetSavedValue() const { return mbSaveValue; }
357 bool IsValueChangedFromSaved() const { return mbSaveValue != IsChecked(); }
359 static Image GetRadioImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
361 Size CalcMinimumSize() const;
362 virtual Size GetOptimalSize() const override;
364 void SetToggleHdl( const Link<RadioButton&,void>& rLink ) { maToggleHdl = rLink; }
366 /** GetRadioButtonGroup returns a list of pointers to <code>RadioButton</code>s in the same group.
368 The pointers in the returned list are valid at the time call returns. However rescheduling
369 or giving up the SolarMutex may mean events get executed that lead to the pointers getting
370 invalid.
372 @param bIncludeThis
373 defines whether <code>this</code> is contained in the returned list
375 @return
376 on return contains the <code>RadioButton</code>s
377 in the same group as this <code>RadioButton</code>.
379 std::vector<VclPtr<RadioButton> > GetRadioButtonGroup(bool bIncludeThis = true) const;
381 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
384 * Group this RadioButton with another
386 void group(RadioButton &rOther);
387 virtual void ShowFocus(const tools::Rectangle& rRect) override;
389 virtual FactoryFunction GetUITestFactory() const override;
392 class VCL_DLLPUBLIC CheckBox : public Button
394 private:
395 tools::Rectangle maStateRect;
396 tools::Rectangle maMouseRect;
397 TriState meState;
398 TriState meSaveValue;
399 bool mbTriState;
400 Link<CheckBox&,void> maToggleHdl;
401 // when mbLegacyNoTextAlign is set then the old behaviour where
402 // the WB_LEFT, WB_RIGHT & WB_CENTER affect the image placement
403 // occurs, otherwise the image ( checkbox box ) is placed
404 // to the left or right ( depending on RTL or LTR settings )
405 bool mbLegacyNoTextAlign;
406 SAL_DLLPRIVATE void ImplInitCheckBoxData();
407 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
408 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
409 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, DrawFlags nDrawFlags,
410 const Point& rPos, const Size& rSize,
411 const Size& rImageSize, tools::Rectangle& rStateRect,
412 tools::Rectangle& rMouseRect );
413 SAL_DLLPRIVATE void ImplDrawCheckBox(vcl::RenderContext& rRenderContext );
414 SAL_DLLPRIVATE long ImplGetImageToTextDistance() const;
415 SAL_DLLPRIVATE Size ImplGetCheckImageSize() const;
417 CheckBox(const CheckBox &) = delete;
418 CheckBox& operator= (const CheckBox &) = delete;
420 protected:
421 using Control::ImplInitSettings;
422 using Window::ImplInit;
423 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
424 virtual void FillLayoutData() const override;
425 virtual const vcl::Font& GetCanonicalFont( const StyleSettings& _rStyle ) const override;
426 virtual const Color& GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
428 virtual void ImplDrawCheckBoxState(vcl::RenderContext& rRenderContext);
429 SAL_DLLPRIVATE const tools::Rectangle& GetStateRect() const { return maStateRect; }
430 SAL_DLLPRIVATE const tools::Rectangle& GetMouseRect() const { return maMouseRect; }
432 public:
433 SAL_DLLPRIVATE void ImplCheck();
434 SAL_DLLPRIVATE void ImplSetMinimumNWFSize();
435 public:
436 explicit CheckBox( vcl::Window* pParent, WinBits nStyle = 0 );
438 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
439 virtual void Tracking( const TrackingEvent& rTEvt ) override;
440 virtual void KeyInput( const KeyEvent& rKEvt ) override;
441 virtual void KeyUp( const KeyEvent& rKEvt ) override;
442 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
443 virtual void Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags ) override;
444 virtual void Resize() override;
445 virtual void GetFocus() override;
446 virtual void LoseFocus() override;
447 virtual void StateChanged( StateChangedType nType ) override;
448 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
449 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
451 void Toggle();
453 void SetState( TriState eState );
454 TriState GetState() const { return meState; }
456 void Check( bool bCheck = true );
457 bool IsChecked() const;
459 void EnableTriState( bool bTriState = true );
460 bool IsTriStateEnabled() const { return mbTriState; }
462 void SaveValue() { meSaveValue = GetState(); }
463 TriState GetSavedValue() const { return meSaveValue; }
464 bool IsValueChangedFromSaved() const { return meSaveValue != GetState(); }
466 static Image GetCheckImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
468 Size CalcMinimumSize( long nMaxWidth = 0 ) const;
469 virtual Size GetOptimalSize() const override;
471 void SetToggleHdl( const Link<CheckBox&,void>& rLink ) { maToggleHdl = rLink; }
472 void SetLegacyNoTextAlign( bool bVal ) { mbLegacyNoTextAlign = bVal; }
474 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
475 virtual void ShowFocus(const tools::Rectangle& rRect) override;
477 virtual FactoryFunction GetUITestFactory() const override;
480 inline void CheckBox::Check( bool bCheck )
482 SetState( (bCheck) ? TRISTATE_TRUE : TRISTATE_FALSE );
485 inline bool CheckBox::IsChecked() const
487 return (GetState() == TRISTATE_TRUE);
490 class VCL_DLLPUBLIC ImageButton : public PushButton
492 protected:
493 using PushButton::ImplInitStyle;
495 private:
496 SAL_DLLPRIVATE void ImplInitStyle();
498 ImageButton( const ImageButton & ) = delete;
499 ImageButton & operator= ( const ImageButton & ) = delete;
501 public:
502 ImageButton( vcl::Window* pParent, WinBits nStyle = 0 );
505 class VCL_DLLPUBLIC ImageRadioButton : public RadioButton
507 ImageRadioButton( const ImageRadioButton & ) = delete;
508 ImageRadioButton & operator= ( const ImageRadioButton & ) = delete;
510 public:
511 explicit ImageRadioButton( vcl::Window* pParent );
514 class VCL_DLLPUBLIC TriStateBox : public CheckBox
516 TriStateBox( const TriStateBox & ) = delete;
517 TriStateBox & operator= ( const TriStateBox & ) = delete;
519 public:
520 explicit TriStateBox( vcl::Window* pParent, WinBits nStyle );
523 class VCL_DLLPUBLIC DisclosureButton : public CheckBox
525 protected:
526 SAL_DLLPRIVATE virtual void ImplDrawCheckBoxState(vcl::RenderContext& rRenderContext) override;
528 public:
529 explicit DisclosureButton( vcl::Window* pParent );
531 virtual void KeyInput( const KeyEvent& rKEvt ) override;
534 #endif // INCLUDED_VCL_BUTTON_HXX
536 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */