FINAL CUT
ftogglebutton.h
1 /***********************************************************************
2 * ftogglebutton.h - Intermediate base class for a toggle button *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2014-2024 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Inheritance diagram
24  * ═══════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▏
27  * ▕ FVTerm ▏ ▕ FObject ▏
28  * ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▏
29  * ▲ ▲
30  * │ │
31  * └─────┬─────┘
32  * │
33  * ▕▔▔▔▔▔▔▔▔▔▏
34  * ▕ FWidget ▏
35  * ▕▁▁▁▁▁▁▁▁▁▏
36  * ▲
37  * │
38  * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
39  * ▕ FToggleButton ▏
40  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
41  */
42 
43 #ifndef FTOGGLEBUTTON_H
44 #define FTOGGLEBUTTON_H
45 
46 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
47  #error "Only <final/final.h> can be included directly."
48 #endif
49 
50 #include "final/fwidget.h"
51 
52 namespace finalcut
53 {
54 
55 // class forward declaration
56 class FButtonGroup;
57 
58 //----------------------------------------------------------------------
59 // class FToggleButton - abstract class for FRadioButton, FCheckBox, ...
60 //----------------------------------------------------------------------
61 
62 class FToggleButton : public FWidget
63 {
64  public:
65  // Using-declaration
66  using FWidget::delAccelerator;
67  using FWidget::setGeometry;
68 
69  // Constructors
70  explicit FToggleButton (FWidget* = nullptr);
71  explicit FToggleButton (const FString&, FWidget* = nullptr);
72 
73  // Disable copy constructor
74  FToggleButton (const FToggleButton&) = delete;
75 
76  // Disable move constructor
77  FToggleButton (FToggleButton&&) noexcept = delete;
78 
79  // Destructor
80  ~FToggleButton() override;
81 
82  // Disable copy assignment operator (=)
83  auto operator = (const FToggleButton&) -> FToggleButton& = delete;
84 
85  // Disable move assignment operator (=)
86  auto operator = (FToggleButton&&) noexcept -> FToggleButton& = delete;
87  // Accessors
88  auto getClassName() const -> FString override;
89  auto getText() & -> FString&;
90 
91  // Mutators
92  void setSize (const FSize&, bool = true) override;
93  void setGeometry (const FPoint&, const FSize&, bool = true) override;
94  void resetColors() override;
95  void setNoUnderline (bool = true);
96  void unsetNoUnderline();
97  void setEnable (bool = true) override;
98  void unsetEnable() override;
99  void setDisable() override;
100  void setChecked (bool = true);
101  void unsetChecked();
102  virtual void setText (const FString&);
103 
104  // Inquiries
105  auto isChecked() const noexcept -> bool;
106  auto hasRadiobuttonFocus() const noexcept -> bool;
107 
108  // Method
109  void hide() override;
110 
111  // Event handlers
112  void onMouseDown (FMouseEvent*) override;
113  void onMouseUp (FMouseEvent*) override;
114  void onWheel (FWheelEvent*) override;
115  void onAccel (FAccelEvent*) override;
116  void onFocusIn (FFocusEvent*) override;
117  void onFocusOut (FFocusEvent*) override;
118 
119  protected:
120  // Accessor
121  auto getGroup() const -> FButtonGroup*;
122  friend auto getGroup (const FToggleButton& toggle_btn) -> FButtonGroup*;
123 
124  // Mutator
125  void setHotkeyAccelerator();
126  void setButtonWidth (std::size_t) noexcept;
127  void setLabelOffsetPos (std::size_t) noexcept;
128 
129  // Inquiries
130  auto isRadioButton() const -> bool;
131  auto isCheckboxButton() const -> bool;
132  auto hasGroup() const -> bool;
133 
134  // Methods
135  void draw() override;
136  void drawLabel();
137  void processClick() const;
138  void processToggle() const;
139 
140  // Event handler
141  void onKeyPress (FKeyEvent*) override;
142 
143  private:
144  // Constants
145  static constexpr auto NOT_SET = static_cast<std::size_t>(-1);
146 
147  // Mutator
148  void setGroup (FButtonGroup*);
149  friend void setGroup (FToggleButton&, FButtonGroup*);
150 
151  // Methods
152  void init();
153  void drawText (const FString&, std::size_t);
154  void performButtonAction();
155  void correctSize (FSize&) const;
156 
157  // Data members
158  FButtonGroup* button_group{nullptr};
159  FString text{};
160  std::size_t label_offset_pos{0};
161  std::size_t button_width{0}; // plus margin spaces
162  bool radiobutton_focus{false};
163  bool checked{false};
164 };
165 
166 // FRadioButton inline functions
167 //----------------------------------------------------------------------
168 inline auto FToggleButton::getClassName() const -> FString
169 { return "FToggleButton"; }
170 
171 //----------------------------------------------------------------------
172 inline auto FToggleButton::getText() & -> FString&
173 { return text; }
174 
175 //----------------------------------------------------------------------
176 inline void FToggleButton::unsetNoUnderline()
177 { setNoUnderline(false); }
178 
179 //----------------------------------------------------------------------
180 inline void FToggleButton::unsetEnable()
181 { setEnable(false); }
182 
183 //----------------------------------------------------------------------
184 inline void FToggleButton::setDisable()
185 { setEnable(false); }
186 
187 //----------------------------------------------------------------------
188 inline void FToggleButton::unsetChecked()
189 { setChecked(false); }
190 
191 //----------------------------------------------------------------------
192 inline auto FToggleButton::isChecked() const noexcept -> bool
193 { return checked; }
194 
195 //----------------------------------------------------------------------
196 inline auto FToggleButton::hasRadiobuttonFocus() const noexcept -> bool
197 { return radiobutton_focus; }
198 
199 //----------------------------------------------------------------------
200 inline auto FToggleButton::getGroup() const -> FButtonGroup*
201 { return button_group; }
202 
203 //----------------------------------------------------------------------
204 inline void FToggleButton::setButtonWidth (std::size_t width) noexcept
205 { button_width = width; }
206 
207 //----------------------------------------------------------------------
208 inline void FToggleButton::setLabelOffsetPos (std::size_t offset) noexcept
209 { label_offset_pos = offset; }
210 
211 //----------------------------------------------------------------------
212 inline auto FToggleButton::hasGroup() const -> bool
213 { return button_group; }
214 
215 } // namespace finalcut
216 
217 #endif // FTOGGLEBUTTON_H
Definition: fevent.h:196
Definition: fevent.h:144
Definition: fevent.h:171
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fsize.h:55
Definition: fstring.h:79
Definition: fbuttongroup.h:64
Definition: fwidget.h:129
Definition: fevent.h:220
Definition: fevent.h:124
Definition: ftogglebutton.h:62