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-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  * ▕ 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  // Inquiries
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  void printEllipsis();
142  void resetStyle() const;
143  void printTrailingSpaces (std::size_t);
144  void drawButtonTextLine (const FString&);
145  void draw() override;
146  void initializeDrawing();
147  void finalizingDrawing() const;
148  auto getSpaceChar() const -> wchar_t;
149  void handleMonochronBackground() const;
150  void drawFlatBorder();
151  void drawShadow();
152  void updateButtonColor();
153  void processClick() const;
154 
155  // Data members
156  FString text{};
157  bool button_down{false};
158  bool active_focus{false};
159  bool click_animation{true};
160  int click_time{150};
161  wchar_t space_char{L' '};
162  FColor button_fg{FColor::Default};
163  FColor button_bg{FColor::Default};
164  FColor button_hotkey_fg{FColor::Default};
165  FColor button_focus_fg{FColor::Default};
166  FColor button_focus_bg{FColor::Default};
167  FColor button_inactive_fg{FColor::Default};
168  FColor button_inactive_bg{FColor::Default};
169  std::size_t hotkeypos{NOT_SET};
170  std::size_t indent{0};
171  std::size_t center_offset{0};
172  std::size_t vcenter_offset{0};
173  std::size_t column_width{0};
174 };
175 
176 // FButton inline functions
177 //----------------------------------------------------------------------
178 inline auto FButton::getClassName() const -> FString
179 { return "FButton"; }
180 
181 //----------------------------------------------------------------------
182 inline auto FButton::getText() const -> FString
183 { return text; }
184 
185 //----------------------------------------------------------------------
186 inline void FButton::unsetNoUnderline()
187 { setNoUnderline(false); }
188 
189 //----------------------------------------------------------------------
190 inline void FButton::unsetEnable()
191 { setEnable(false); }
192 
193 //----------------------------------------------------------------------
194 inline void FButton::setDisable()
195 { setEnable(false); }
196 
197 //----------------------------------------------------------------------
198 inline void FButton::unsetFlat()
199 { setFlat(false); }
200 
201 //----------------------------------------------------------------------
202 inline void FButton::unsetShadow()
203 { setShadow(false); }
204 
205 //----------------------------------------------------------------------
206 inline void FButton::setUp()
207 { setDown(false); }
208 
209 //----------------------------------------------------------------------
210 inline void FButton::setClickAnimation(bool enable)
211 { click_animation = enable; }
212 
213 //----------------------------------------------------------------------
214 inline void FButton::unsetClickAnimation()
215 { setClickAnimation(false); }
216 
217 //----------------------------------------------------------------------
218 inline auto FButton::isFlat() const -> bool
219 { return getFlags().feature.flat; }
220 
221 //----------------------------------------------------------------------
222 inline auto FButton::isDown() const noexcept -> bool
223 { return button_down; }
224 
225 //----------------------------------------------------------------------
226 inline auto FButton::hasShadow() const -> bool
227 { return getFlags().shadow.shadow; }
228 
229 //----------------------------------------------------------------------
230 inline auto FButton::hasClickAnimation() const noexcept -> bool
231 { return click_animation; }
232 
233 } // namespace finalcut
234 
235 #endif // FBUTTON_H
Definition: fbutton.h:60
Definition: fevent.h:196
Definition: fevent.h:144
Definition: fevent.h:300
Definition: fevent.h:171
Definition: class_template.cpp:25
Definition: fstring.h:79
Definition: fwidget.h:129
Definition: fevent.h:220
Definition: fevent.h:124