FINAL CUT
fspinbox.h
1 /***********************************************************************
2 * fspinbox.h - Widget FSpinBox *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2019-2026 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  * ▕ FSpinBox ▏
40  * ▕▁▁▁▁▁▁▁▁▁▁▏
41  */
42 
43 #ifndef FSPINBOX_H
44 #define FSPINBOX_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 <limits>
51 
52 #include "final/fwidget.h"
53 #include "final/widget/flineedit.h"
54 
55 namespace finalcut
56 {
57 
58 // class forward declaration
59 class FLineEdit;
60 
61 //----------------------------------------------------------------------
62 // class FSpinBox
63 //----------------------------------------------------------------------
64 
65 class FSpinBox : public FWidget
66 {
67  public:
68  // Using-declaration
69  using FWidget::setGeometry;
70  using LabelOrientation = FLineEdit::LabelOrientation;
71 
72  // Constructors
73  explicit FSpinBox (FWidget* = nullptr);
74 
75  // Destructor
76  ~FSpinBox() noexcept override;
77 
78  // Accessors
79  auto getClassName() const -> FString override;
80  auto getValue() const noexcept -> sInt64;
81  auto getPrefix() const -> FString;
82  auto getSuffix() const -> FString;
83  auto getLabelOrientation() const -> LabelOrientation;
84 
85  // Mutators
86  void setSize (const FSize&, bool = true) override;
87  void setGeometry (const FPoint&, const FSize&, bool = true) override;
88  void setEnable (bool = true) override;
89  void unsetEnable() override;
90  void setDisable() override;
91  void setShadow (bool = true);
92  void unsetShadow();
93  void setValue (sInt64);
94  void setMinValue (sInt64);
95  void setMaxValue (sInt64);
96  void setRange (sInt64, sInt64);
97  void setPrefix (const FString&);
98  void setSuffix (const FString&);
99  void setLabelText (const FString&);
100  void setLabelOrientation (const LabelOrientation);
101 
102  // Predicate
103  auto hasShadow() const -> bool;
104 
105  // Methods
106  void hide() override;
107 
108  // Event handlers
109  void onKeyPress (FKeyEvent*) override;
110  void onMouseDown (FMouseEvent*) override;
111  void onMouseUp (FMouseEvent*) override;
112  void onWheel (FWheelEvent*) override;
113  void onTimer (FTimerEvent*) override;
114  void onFocusIn (FFocusEvent*) override;
115  void onFailAtChildFocus (FFocusEvent*) override;
116 
117  private:
118  // Using-declaration
119  using KeyMap = std::unordered_map<FKey, std::function<void(FKeyEvent*)>, EnumHash<FKey>>;
120 
121  // Enumeration
122  enum class SpiningState : uInt8
123  {
124  None = 0,
125  Up = 1,
126  Down = 2
127  };
128 
129  // Methods
130  void init();
131  void mapKeyFunctions();
132  void draw() override;
133  void updateInputField();
134  void increaseValue (sInt64 = 1);
135  void decreaseValue (sInt64 = 1);
136  void processActivate() const;
137  void processChanged() const;
138  void forceFocus();
139 
140  // Callback methods
141  void cb_inputFieldActivate() const;
142  void cb_inputFieldChange (const FLineEdit&);
143 
144  // Data members
145  KeyMap key_map{};
146  FLineEdit input_field{this};
147  sInt64 value{0};
148  sInt64 min{std::numeric_limits<sInt64>::min()};
149  sInt64 max{std::numeric_limits<sInt64>::max()};
150  FString pfix{};
151  FString sfix{};
152  SpiningState spining_state{SpiningState::None};
153  bool threshold_reached{false};
154  int threshold_time{500};
155  int repeat_time{80};
156 };
157 
158 
159 // FSpinBox inline functions
160 //----------------------------------------------------------------------
161 inline auto FSpinBox::getClassName() const -> FString
162 { return "FSpinBox"; }
163 
164 //----------------------------------------------------------------------
165 inline auto FSpinBox::getValue() const noexcept -> sInt64
166 { return value; }
167 
168 //----------------------------------------------------------------------
169 inline auto FSpinBox::getPrefix() const -> FString
170 { return pfix; }
171 
172 //----------------------------------------------------------------------
173 inline auto FSpinBox::getSuffix() const -> FString
174 { return sfix; }
175 
176 //----------------------------------------------------------------------
177 inline auto FSpinBox::getLabelOrientation() const -> FLineEdit::LabelOrientation
178 { return input_field.getLabelOrientation(); }
179 
180 //----------------------------------------------------------------------
181 inline void FSpinBox::unsetEnable()
182 { setEnable(false); }
183 
184 //----------------------------------------------------------------------
185 inline void FSpinBox::setDisable()
186 { setEnable(false); }
187 
188 //----------------------------------------------------------------------
189 inline void FSpinBox::unsetShadow()
190 { setShadow(false); }
191 
192 //----------------------------------------------------------------------
193 inline auto FSpinBox::hasShadow() const -> bool
194 { return getFlags().shadow.shadow; }
195 
196 //----------------------------------------------------------------------
197 inline void FSpinBox::setLabelText (const FString& s)
198 { input_field.setLabelText(s); }
199 
200 //----------------------------------------------------------------------
201 inline void FSpinBox::setLabelOrientation (const LabelOrientation o)
202 { input_field.setLabelOrientation(o); }
203 
204 } // namespace finalcut
205 
206 #endif // FSPINBOX_H
Definition: fevent.h:207
Definition: fevent.h:144
Definition: fevent.h:311
Definition: fevent.h:182
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fsize.h:55
Definition: fspinbox.h:65
Definition: fstring.h:82
Definition: fwidget.h:129
Definition: flineedit.h:67
Definition: fevent.h:124
Definition: ftypes.h:161