Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / vcl / toolkit / button.hxx
blob017c4d06ac547e1858468e9ee05c1c1970451511
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 #pragma once
22 #if !defined(VCL_DLLIMPLEMENTATION) && !defined(TOOLKIT_DLLIMPLEMENTATION) && !defined(VCL_INTERNALS)
23 #error "don't use this in new code"
24 #endif
26 #include <vcl/dllapi.h>
27 #include <vcl/image.hxx>
28 #include <vcl/ctrl.hxx>
29 #include <vcl/vclenum.hxx>
30 #include <memory>
31 #include <vector>
33 namespace com::sun::star::frame { struct FeatureStateEvent; class XFrame; }
34 template <class T> class VclPtr;
36 class Color;
37 class ImplCommonButtonData;
38 enum class DrawButtonFlags;
40 class VCL_DLLPUBLIC Button : public Control
42 private:
43 std::unique_ptr<ImplCommonButtonData> mpButtonData;
44 Link<Button*,void> maClickHdl;
46 /// Command URL (like .uno:Save) in case the button should handle it.
47 OUString maCommand;
49 Button (const Button &) = delete;
50 Button & operator= (const Button &) = delete;
51 public:
52 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( WinBits nWinStyle, SystemTextColorFlags nSystemTextColorFlags ) const;
53 SAL_DLLPRIVATE void ImplDrawAlignedImage(OutputDevice* pDev, Point& rPos, Size& rSize,
54 sal_Int32 nImageSep,
55 DrawTextFlags nTextStyle, tools::Rectangle *pSymbolRect=nullptr, bool bAddImageSep = false );
56 SAL_DLLPRIVATE void ImplSetFocusRect( const tools::Rectangle &rFocusRect );
57 SAL_DLLPRIVATE const tools::Rectangle& ImplGetFocusRect() const;
58 SAL_DLLPRIVATE void ImplSetSymbolAlign( SymbolAlign eAlign );
59 /// The x-coordinate of the vertical separator line, use in MenuButton subclass only.
60 SAL_DLLPRIVATE tools::Long ImplGetSeparatorX() const;
61 SAL_DLLPRIVATE void ImplSetSeparatorX( tools::Long nX );
63 protected:
64 explicit Button( WindowType nType );
66 // for drawing RadioButton or CheckButton that has Text and/or Image
67 SAL_DLLPRIVATE void ImplDrawRadioCheck(OutputDevice* pDev, WinBits nWinStyle, SystemTextColorFlags nSystemTextColorFlags,
68 const Point& rPos, const Size& rSize,
69 const Size& rImageSize, tools::Rectangle& rStateRect,
70 tools::Rectangle& rMouseRect);
71 SAL_DLLPRIVATE tools::Long ImplGetImageToTextDistance() const;
73 public:
74 virtual ~Button() override;
75 virtual void dispose() override;
77 virtual void Click();
79 void SetClickHdl( const Link<Button*,void>& rLink ) { maClickHdl = rLink; }
80 const Link<Button*,void>& GetClickHdl() const { return maClickHdl; }
82 /// Setup handler for UNO commands so that commands like .uno:Something are handled automagically by this button.
83 void SetCommandHandler(const OUString& aCommand, const css::uno::Reference<css::frame::XFrame>& rFrame);
84 OUString const & GetCommand() const { return maCommand; }
86 void SetModeImage( const Image& rImage );
87 Image const & GetModeImage( ) const;
88 bool HasImage() const;
89 void SetImageAlign( ImageAlign eAlign );
90 ImageAlign GetImageAlign() const;
91 DrawButtonFlags GetButtonState() const;
92 DrawButtonFlags& GetButtonState();
94 /// Set an image to use as the complete render view of a custom button,
95 /// instead of the usual contents of a button
96 void SetCustomButtonImage( const Image& rImage );
97 Image const & GetCustomButtonImage() const;
99 bool IsSmallSymbol() const;
100 void SetSmallSymbol();
101 virtual bool set_property(const OUString &rKey, const OUString &rValue) override;
103 /// Sets the button state according to the FeatureStateEvent emitted by a Uno state change.
104 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent);
106 virtual FactoryFunction GetUITestFactory() const override;
108 virtual void DumpAsPropertyTree(tools::JsonWriter&) override;
110 protected:
112 /// Handler for click, in case we want the button to handle uno commands (.uno:Something).
113 DECL_DLLPRIVATE_STATIC_LINK(Button, dispatchCommandHandler, Button*, void);
116 enum class PushButtonDropdownStyle
118 NONE = 0x0000,
119 MenuButton = 0x0002, //visual down arrow
120 SplitMenuButton = 0x0003, //visual down arrow and separator line
123 class VCL_DLLPUBLIC PushButton : public Button
125 public:
126 SAL_DLLPRIVATE void ImplSetDefButton( bool bSet );
127 SAL_DLLPRIVATE void ImplDrawPushButtonFrame(vcl::RenderContext& rRenderContext, tools::Rectangle& rRect, DrawButtonFlags nStyle);
128 SAL_DLLPRIVATE static bool ImplHitTestPushButton(vcl::Window const * pDev, const Point& rPos);
129 SAL_DLLPRIVATE bool ImplIsDefButton() const;
131 explicit PushButton( vcl::Window* pParent, WinBits nStyle = 0 );
133 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
134 virtual void Tracking( const TrackingEvent& rTEvt ) override;
135 virtual void KeyInput( const KeyEvent& rKEvt ) override;
136 virtual void KeyUp( const KeyEvent& rKEvt ) override;
137 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
138 virtual void Draw( OutputDevice* pDev, const Point& rPos, SystemTextColorFlags nFlags ) override;
139 virtual void Resize() override;
140 virtual void GetFocus() override;
141 virtual void LoseFocus() override;
142 virtual void StateChanged( StateChangedType nType ) override;
143 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
144 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
146 void Toggle();
148 void SetSymbol( SymbolType eSymbol );
149 SymbolType GetSymbol() const { return meSymbol; }
150 void SetSymbolAlign( SymbolAlign eAlign );
152 void SetDropDown( PushButtonDropdownStyle nStyle );
154 void SetState( TriState eState );
155 TriState GetState() const { return meState; }
156 virtual void statusChanged(const css::frame::FeatureStateEvent& rEvent) override;
158 void Check( bool bCheck = true );
159 bool IsChecked() const;
161 void SetPressed( bool bPressed );
162 bool IsPressed() const { return mbPressed; }
164 void EndSelection();
166 Size CalcMinimumSize() const;
167 virtual Size GetOptimalSize() const override;
169 virtual bool set_property(const OUString &rKey, const OUString &rValue) override;
170 virtual void ShowFocus(const tools::Rectangle& rRect) override;
172 void setAction(bool bIsAction)
174 mbIsAction = bIsAction;
177 bool isAction() const
179 return mbIsAction;
182 void DumpAsPropertyTree(tools::JsonWriter&) override;
184 bool isToggleButton() { return mbIsToggleButton; }
185 void setToggleButton(bool bIsToggleButton) { mbIsToggleButton = bIsToggleButton; }
187 protected:
188 PushButtonDropdownStyle mnDDStyle;
189 bool mbIsActive;
191 SAL_DLLPRIVATE void ImplInitPushButtonData();
192 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
193 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
194 SAL_DLLPRIVATE void ImplDrawPushButtonContent(OutputDevice* pDev, SystemTextColorFlags nSystemTextColorFlags,
195 const tools::Rectangle& rRect, bool bMenuBtnSep,
196 DrawButtonFlags nButtonFlags);
197 SAL_DLLPRIVATE void ImplDrawPushButton(vcl::RenderContext& rRenderContext);
198 using Button::ImplGetTextStyle;
199 SAL_DLLPRIVATE DrawTextFlags ImplGetTextStyle( SystemTextColorFlags nSystemTextColorFlags ) const;
200 SAL_DLLPRIVATE bool IsSymbol() const { return ( (meSymbol != SymbolType::DONTKNOW) && (meSymbol != SymbolType::IMAGE) ); }
202 PushButton( const PushButton & ) = delete;
203 PushButton& operator=( const PushButton & )
204 = delete;
206 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
208 using Control::ImplInitSettings;
209 using Window::ImplInit;
211 explicit PushButton( WindowType nType );
213 virtual void FillLayoutData() const override;
214 virtual const vcl::Font&
215 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
216 virtual const Color&
217 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
219 private:
220 SymbolType meSymbol;
221 TriState meState;
222 bool mbPressed;
223 bool mbIsAction;
224 bool mbIsToggleButton = false;
227 inline void PushButton::Check( bool bCheck )
229 SetState( bCheck ? TRISTATE_TRUE : TRISTATE_FALSE );
232 inline bool PushButton::IsChecked() const
234 return (GetState() == TRISTATE_TRUE);
237 class VCL_DLLPUBLIC OKButton final : public PushButton
239 private:
240 using PushButton::ImplInit;
242 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
244 OKButton (const OKButton &) = delete;
245 OKButton & operator= (const OKButton &) = delete;
247 public:
248 explicit OKButton( vcl::Window* pParent, WinBits nStyle = WB_DEFBUTTON );
250 virtual void Click() override;
253 class VCL_DLLPUBLIC CancelButton : public PushButton
255 protected:
256 using PushButton::ImplInit;
257 private:
258 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
260 CancelButton (const CancelButton &) = delete;
261 CancelButton & operator= (const CancelButton &) = delete;
263 public:
264 explicit CancelButton( vcl::Window* pParent, WinBits nStyle = 0 );
266 virtual void Click() override;
269 class CloseButton final : public CancelButton
271 public:
272 explicit CloseButton(vcl::Window* pParent);
275 class VCL_DLLPUBLIC HelpButton final : public PushButton
277 private:
278 using PushButton::ImplInit;
279 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
281 HelpButton( const HelpButton & ) = delete;
282 HelpButton & operator= ( const HelpButton & ) = delete;
284 virtual void StateChanged( StateChangedType nStateChange ) override;
286 public:
287 explicit HelpButton( vcl::Window* pParent, WinBits nStyle = 0 );
289 virtual void Click() override;
292 class VCL_DLLPUBLIC CheckBox : public Button
294 private:
295 tools::Rectangle maStateRect;
296 tools::Rectangle maMouseRect;
297 TriState meState;
298 bool mbTriState;
299 Link<CheckBox&,void> maToggleHdl;
300 SAL_DLLPRIVATE void ImplInitCheckBoxData();
301 SAL_DLLPRIVATE static WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle );
302 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
303 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, SystemTextColorFlags nSystemTextColorFlags,
304 const Point& rPos, const Size& rSize,
305 const Size& rImageSize, tools::Rectangle& rStateRect,
306 tools::Rectangle& rMouseRect );
307 SAL_DLLPRIVATE void ImplDrawCheckBox(vcl::RenderContext& rRenderContext );
308 SAL_DLLPRIVATE Size ImplGetCheckImageSize() const;
310 CheckBox(const CheckBox &) = delete;
311 CheckBox& operator= (const CheckBox &) = delete;
313 protected:
314 using Control::ImplInitSettings;
315 using Window::ImplInit;
316 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
317 virtual void FillLayoutData() const override;
318 virtual const vcl::Font& GetCanonicalFont( const StyleSettings& _rStyle ) const override;
319 virtual const Color& GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
320 void ImplAdjustNWFSizes() override;
322 virtual void ImplDrawCheckBoxState(vcl::RenderContext& rRenderContext);
323 SAL_DLLPRIVATE const tools::Rectangle& GetStateRect() const { return maStateRect; }
324 SAL_DLLPRIVATE const tools::Rectangle& GetMouseRect() const { return maMouseRect; }
326 public:
327 SAL_DLLPRIVATE void ImplCheck();
328 public:
329 explicit CheckBox( vcl::Window* pParent, WinBits nStyle = 0 );
331 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
332 virtual void Tracking( const TrackingEvent& rTEvt ) override;
333 virtual void KeyInput( const KeyEvent& rKEvt ) override;
334 virtual void KeyUp( const KeyEvent& rKEvt ) override;
335 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
336 virtual void Draw( OutputDevice* pDev, const Point& rPos, SystemTextColorFlags nFlags ) override;
337 virtual void Resize() override;
338 virtual void GetFocus() override;
339 virtual void LoseFocus() override;
340 virtual void StateChanged( StateChangedType nType ) override;
341 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
342 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
344 void Toggle();
346 void SetState( TriState eState );
347 TriState GetState() const { return meState; }
349 void Check( bool bCheck = true )
351 SetState( bCheck ? TRISTATE_TRUE : TRISTATE_FALSE );
354 bool IsChecked() const
356 return (GetState() == TRISTATE_TRUE);
359 void EnableTriState( bool bTriState = true );
360 bool IsTriStateEnabled() const { return mbTriState; }
362 static Image GetCheckImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
364 Size CalcMinimumSize( tools::Long nMaxWidth = 0 ) const;
365 virtual Size GetOptimalSize() const override;
367 void SetToggleHdl( const Link<CheckBox&,void>& rLink ) { maToggleHdl = rLink; }
369 virtual bool set_property(const OUString &rKey, const OUString &rValue) override;
370 virtual void ShowFocus(const tools::Rectangle& rRect) override;
372 /// Button has additional stuff that we need to dump too.
373 void DumpAsPropertyTree(tools::JsonWriter&) override;
375 virtual FactoryFunction GetUITestFactory() const override;
378 class VCL_DLLPUBLIC RadioButton : public Button
380 private:
381 friend class VclBuilder;
383 std::shared_ptr< std::vector< VclPtr< RadioButton > > > m_xGroup;
384 tools::Rectangle maStateRect;
385 tools::Rectangle maMouseRect;
386 Image maImage;
387 bool mbChecked;
388 bool mbRadioCheck;
389 bool mbStateChanged;
390 bool mbUsesExplicitGroup;
391 Link<RadioButton&,void> maToggleHdl;
392 SAL_DLLPRIVATE void ImplInitRadioButtonData();
393 SAL_DLLPRIVATE WinBits ImplInitStyle( const vcl::Window* pPrevWindow, WinBits nStyle ) const;
394 SAL_DLLPRIVATE void ImplInitSettings( bool bBackground );
395 SAL_DLLPRIVATE void ImplDrawRadioButtonState(vcl::RenderContext& rRenderContext);
396 SAL_DLLPRIVATE void ImplDraw( OutputDevice* pDev, SystemTextColorFlags nSystemTextColorFlags,
397 const Point& rPos, const Size& rSize,
398 const Size& rImageSize, tools::Rectangle& rStateRect,
399 tools::Rectangle& rMouseRect );
400 SAL_DLLPRIVATE void ImplDrawRadioButton(vcl::RenderContext& rRenderContext );
401 SAL_DLLPRIVATE void ImplUncheckAllOther();
402 SAL_DLLPRIVATE Size ImplGetRadioImageSize() const;
404 RadioButton(const RadioButton &) = delete;
405 RadioButton& operator= (const RadioButton &) = delete;
407 protected:
408 using Control::ImplInitSettings;
409 using Window::ImplInit;
410 SAL_DLLPRIVATE void ImplInit( vcl::Window* pParent, WinBits nStyle );
412 public:
413 SAL_DLLPRIVATE void ImplCallClick( bool bGrabFocus = false, GetFocusFlags nFocusFlags = GetFocusFlags::NONE );
415 protected:
416 virtual void FillLayoutData() const override;
417 virtual const vcl::Font&
418 GetCanonicalFont( const StyleSettings& _rStyle ) const override;
419 virtual const Color&
420 GetCanonicalTextColor( const StyleSettings& _rStyle ) const override;
421 void ImplAdjustNWFSizes() override;
423 public:
425 bUsesExplicitGroup of true means that group() is used to set what radiobuttons are part of a group
426 while false means that contiguous radiobuttons are considered part of a group where WB_GROUP designates
427 the start of the group and all contiguous radiobuttons without WB_GROUP set form the rest of the group.
429 true is fairly straightforward, false leads to trick situations and is the legacy case
431 explicit RadioButton(vcl::Window* pParent, bool bUsesExplicitGroup, WinBits nWinStyle);
432 virtual ~RadioButton() override;
433 virtual void dispose() override;
435 virtual void MouseButtonDown( const MouseEvent& rMEvt ) override;
436 virtual void Tracking( const TrackingEvent& rTEvt ) override;
437 virtual void KeyInput( const KeyEvent& rKEvt ) override;
438 virtual void KeyUp( const KeyEvent& rKEvt ) override;
439 virtual void Paint( vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect ) override;
440 virtual void Draw( OutputDevice* pDev, const Point& rPos, SystemTextColorFlags nFlags ) override;
441 virtual void Resize() override;
442 virtual void GetFocus() override;
443 virtual void LoseFocus() override;
444 virtual void StateChanged( StateChangedType nType ) override;
445 virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
446 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
448 void Toggle();
450 bool IsStateChanged() const { return mbStateChanged; }
452 void EnableRadioCheck( bool bRadioCheck ) { mbRadioCheck = bRadioCheck; }
453 bool IsRadioCheckEnabled() const { return mbRadioCheck; }
455 void SetModeRadioImage( const Image& rImage );
457 void SetState( bool bCheck );
458 void Check( bool bCheck = true );
459 bool IsChecked() const { return mbChecked; }
461 static Image GetRadioImage( const AllSettings& rSettings, DrawButtonFlags nFlags );
463 Size CalcMinimumSize( tools::Long nMaxWidth = 0 ) const;
464 virtual Size GetOptimalSize() const override;
466 void SetToggleHdl( const Link<RadioButton&,void>& rLink ) { maToggleHdl = rLink; }
468 /** GetRadioButtonGroup returns a list of pointers to <code>RadioButton</code>s in the same group.
470 The pointers in the returned list are valid at the time call returns. However rescheduling
471 or giving up the SolarMutex may mean events get executed that lead to the pointers getting
472 invalid.
474 @param bIncludeThis
475 defines whether <code>this</code> is contained in the returned list
477 @return
478 on return contains the <code>RadioButton</code>s
479 in the same group as this <code>RadioButton</code>.
481 std::vector<VclPtr<RadioButton> > GetRadioButtonGroup(bool bIncludeThis = true) const;
483 virtual bool set_property(const OUString &rKey, const OUString &rValue) override;
486 * Group this RadioButton with another
488 void group(RadioButton &rOther);
489 virtual void ShowFocus(const tools::Rectangle& rRect) override;
491 /// Button has additional stuff that we need to dump too.
492 void DumpAsPropertyTree(tools::JsonWriter&) override;
494 virtual FactoryFunction GetUITestFactory() const override;
497 class VCL_DLLPUBLIC ImageButton final : public PushButton
499 private:
500 using PushButton::ImplInitStyle;
502 SAL_DLLPRIVATE void ImplInitStyle();
504 ImageButton( const ImageButton & ) = delete;
505 ImageButton & operator= ( const ImageButton & ) = delete;
507 public:
508 ImageButton( vcl::Window* pParent, WinBits nStyle = 0 );
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */