FINAL CUT
fbutton.h
1 /***********************************************************************
2 * fbutton.h - Widget FButton *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2012-2025 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  * ▕ FButton ▏
40  * ▕▁▁▁▁▁▁▁▁▁▏
41  */
42 
43 #ifndef FBUTTON_H
44 #define FBUTTON_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 #include "final/fwidgetcolors.h"
52 
53 namespace finalcut
54 {
55 
56 //----------------------------------------------------------------------
57 // class FButton
58 //----------------------------------------------------------------------
59 
60 class FButton : public FWidget
61 {
62  public:
63  // Using-declaration
64  using FWidget::delAccelerator;
65 
66  // Constructors
67  explicit FButton (FWidget* = nullptr);
68  explicit FButton (FString&&, FWidget* = nullptr);
69 
70  // Destructor
71  ~FButton() override;
72 
73  // Overloaded operator
74  auto operator = (const FString&) -> FButton&;
75 
76  // Accessors
77  auto getClassName() const -> FString override;
78  auto getText() const -> FString;
79 
80  // Mutators
81  void setForegroundColor (FColor) override;
82  void setBackgroundColor (FColor) override;
83  void setHotkeyForegroundColor (FColor);
84  void setFocusForegroundColor (FColor);
85  void setFocusBackgroundColor (FColor);
86  void setInactiveForegroundColor (FColor);
87  void setInactiveBackgroundColor (FColor);
88  void resetColors() override;
89  void setNoUnderline (bool = true);
90  void unsetNoUnderline();
91  void setEnable (bool = true) override;
92  void unsetEnable() override;
93  void setDisable() override;
94  void setFlat (bool = true);
95  void unsetFlat();
96  void setShadow (bool = true);
97  void unsetShadow();
98  void setDown (bool = true);
99  void setUp();
100  void setClickAnimation (bool = true);
101  void unsetClickAnimation();
102  void setText (const FString&);
103 
104  // Predicates
105  auto isFlat() const -> bool;
106  auto isDown() const noexcept -> bool;
107  auto hasShadow() const -> bool;
108  auto hasClickAnimation() const noexcept -> bool;
109 
110  // Methods
111  void hide() override;
112 
113  // Event handlers
114  void onKeyPress (FKeyEvent*) override;
115  void onMouseDown (FMouseEvent*) override;
116  void onMouseUp (FMouseEvent*) override;
117  void onMouseMove (FMouseEvent*) override;
118  void onWheel (FWheelEvent*) override;
119  void onTimer (FTimerEvent*) override;
120  void onAccel (FAccelEvent*) override;
121  void onFocusIn (FFocusEvent*) override;
122  void onFocusOut (FFocusEvent*) override;
123 
124  private:
125  // Constants
126  static constexpr auto NOT_SET = static_cast<std::size_t>(-1);
127 
128  // Methods
129  void init();
130  void setHotkeyAccelerator();
131  void detectHotkey();
132  auto clickAnimationIndent (const FWidget*) -> std::size_t;
133  void clearRightMargin (const FWidget*);
134  void drawMarginLeft();
135  void drawMarginRight();
136  void drawTopBottomBackground();
137  void printLeadingSpaces (std::size_t&);
138  void setCursorPositionOnButton();
139  void modifyStyle() const;
140  void printButtonText (const FString&, std::size_t&);
141  auto hasMoreCharacter (std::size_t, std::size_t) const -> bool;
142  void setHotkeyStyle() const;
143  void resetHotkeyStyle() const;
144  void printEllipsis();
145  void resetStyle() const;
146  void printTrailingSpaces();
147  void drawButtonTextLine (const FString&);
148  void draw() override;
149  void initializeDrawing();
150  void finalizingDrawing() const;
151  auto getSpaceChar() const -> wchar_t;
152  void handleMonochronBackground() const;
153  void drawFlatBorder();
154  void drawShadow();
155  void updateButtonColor();
156  void processClick() const;
157 
158  // Data members
159  FString text{};
160  bool button_down{false};
161  bool active_focus{false};
162  bool click_animation{true};
163  int click_time{150};
164  wchar_t space_char{L' '};
165  FColor button_fg{FColor::Default};
166  FColor button_bg{FColor::Default};
167  FColor button_hotkey_fg{FColor::Default};
168  FColor button_focus_fg{FColor::Default};
169  FColor button_focus_bg{FColor::Default};
170  FColor button_inactive_fg{FColor::Default};
171  FColor button_inactive_bg{FColor::Default};
172  std::size_t hotkeypos{NOT_SET};
173  std::size_t indent{0};
174  std::size_t center_offset{0};
175  std::size_t vcenter_offset{0};
176  std::size_t column_width{0};
177 };
178 
179 // FButton inline functions
180 //----------------------------------------------------------------------
181 inline auto FButton::getClassName() const -> FString
182 { return "FButton"; }
183 
184 //----------------------------------------------------------------------
185 inline auto FButton::getText() const -> FString
186 { return text; }
187 
188 //----------------------------------------------------------------------
189 inline void FButton::unsetNoUnderline()
190 { setNoUnderline(false); }
191 
192 //----------------------------------------------------------------------
193 inline void FButton::unsetEnable()
194 { setEnable(false); }
195 
196 //----------------------------------------------------------------------
197 inline void FButton::setDisable()
198 { setEnable(false); }
199 
200 //----------------------------------------------------------------------
201 inline void FButton::unsetFlat()
202 { setFlat(false); }
203 
204 //----------------------------------------------------------------------
205 inline void FButton::unsetShadow()
206 { setShadow(false); }
207 
208 //----------------------------------------------------------------------
209 inline void FButton::setUp()
210 { setDown(false); }
211 
212 //----------------------------------------------------------------------
213 inline void FButton::setClickAnimation(bool enable)
214 { click_animation = enable; }
215 
216 //----------------------------------------------------------------------
217 inline void FButton::unsetClickAnimation()
218 { setClickAnimation(false); }
219 
220 //----------------------------------------------------------------------
221 inline auto FButton::isFlat() const -> bool
222 { return getFlags().feature.flat; }
223 
224 //----------------------------------------------------------------------
225 inline auto FButton::isDown() const noexcept -> bool
226 { return button_down; }
227 
228 //----------------------------------------------------------------------
229 inline auto FButton::hasShadow() const -> bool
230 { return getFlags().shadow.shadow; }
231 
232 //----------------------------------------------------------------------
233 inline auto FButton::hasClickAnimation() const noexcept -> bool
234 { return click_animation; }
235 
236 } // namespace finalcut
237 
238 #endif // FBUTTON_H
Definition: fbutton.h:60
Definition: fevent.h:207
Definition: fevent.h:144
Definition: fevent.h:311
Definition: fevent.h:182
Definition: class_template.cpp:25
Definition: fstring.h:82
Definition: fwidget.h:129
Definition: fevent.h:231
Definition: fevent.h:124