FINAL CUT
fcombobox.h
1 /***********************************************************************
2 * fcombobox.h - Widget FComboBox *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2019-2023 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  * ▕▔▔▔▔▔▔▔▔▔▔▔▏1 1▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
39  * ▕ FComboBox ▏- - - -▕ FDropDownListBox ▏
40  * ▕▁▁▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
41  */
42 
43 #ifndef FCOMBOBOX_H
44 #define FCOMBOBOX_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 <utility>
51 
52 #include "final/fwidget.h"
53 #include "final/util/fdata.h"
54 #include "final/widget/flineedit.h"
55 #include "final/widget/flistbox.h"
56 #include "final/widget/fwindow.h"
57 
58 namespace finalcut
59 {
60 
61 // class forward declaration
62 class FLineEdit;
63 class FListBox;
64 
65 //----------------------------------------------------------------------
66 // class FDropDownListBox
67 //----------------------------------------------------------------------
68 
69 class FDropDownListBox : public FWindow
70 {
71  public:
72  // Using-declaration
73  using FWidget::setGeometry;
74 
75  // Constructor
76  explicit FDropDownListBox (FWidget* = nullptr);
77 
78  // Destructor
79  ~FDropDownListBox () override;
80 
81  // Accessors
82  auto getClassName() const -> FString override;
83 
84  // Mutators
85  void setGeometry (const FPoint&, const FSize&, bool = true ) override;
86  // Inquiries
87  auto isEmpty() const -> bool;
88 
89  // Methods
90  void show() override;
91  void hide() override;
92 
93  private:
94  // Methods
95  void init();
96  void draw() override;
97  void drawShadow();
98  auto containsWidget (const FPoint&) -> bool;
99 
100  // Data members
101  FListBox list{this};
102 
103  // Friend functions
104  friend auto closeComboBox (FDropDownListBox*, const FPoint&) -> bool;
105 
106  // Friend classes
107  friend class FComboBox;
108 };
109 
110 // FDropDownListBox inline functions
111 //----------------------------------------------------------------------
112 inline auto FDropDownListBox::getClassName() const -> FString
113 { return "FDropDownListBox"; }
114 
115 //----------------------------------------------------------------------
116 inline auto FDropDownListBox::isEmpty() const -> bool
117 { return list.getCount() == 0; }
118 
119 
120 //----------------------------------------------------------------------
121 // class FComboBox
122 //----------------------------------------------------------------------
123 
124 class FComboBox : public FWidget
125 {
126  public:
127  // Using-declaration
128  using FWidget::setGeometry;
129  using LabelOrientation = FLineEdit::LabelOrientation;
130 
131  // Constructors
132  explicit FComboBox (FWidget* = nullptr);
133 
134  // Destructor
135  ~FComboBox() noexcept override;
136 
137  // Accessors
138  auto getClassName() const -> FString override;
139  auto getCount() const -> std::size_t;
140  auto getText() const -> FString;
141  template <typename DT>
142  auto getItemData() -> clean_fdata_t<DT>&;
143  auto getLabelOrientation() const -> LabelOrientation;
144 
145  // Mutators
146  void setSize (const FSize&, bool = true) override;
147  void setGeometry (const FPoint&, const FSize&, bool = true) override;
148  void setEnable (bool = true) override;
149  void unsetEnable() override;
150  void setDisable() override;
151  void setShadow (bool = true);
152  void unsetShadow();
153  void setEditable (bool = true);
154  void unsetEditable();
155  void setCurrentItem (std::size_t);
156  void setMaxVisibleItems (std::size_t);
157  void setText (const FString&);
158  void clearText();
159  void setLabelText (const FString&);
160  void setLabelOrientation (const LabelOrientation);
161 
162  // Inquiries
163  auto hasShadow() const -> bool;
164 
165  // Methods
166  void insert (const FListBoxItem&);
167  template <typename T
168  , typename DT = std::nullptr_t>
169  void insert (const std::initializer_list<T>& list, DT&& = DT());
170  template <typename ItemT
171  , typename DT = std::nullptr_t>
172  void insert (const ItemT&, DT&& = DT());
173  void remove (std::size_t);
174  void reserve (std::size_t);
175  void clear();
176  virtual void showDropDown();
177  virtual void hideDropDown();
178 
179  // Event handlers
180  void onKeyPress (FKeyEvent*) override;
181  void onMouseDown (FMouseEvent*) override;
182  void onMouseMove (FMouseEvent*) override;
183  void onWheel (FWheelEvent*) override;
184  void onFocusIn (FFocusEvent*) override;
185  void onFocusOut (FFocusEvent*) override;
186  void onFailAtChildFocus (FFocusEvent*) override;
187 
188  private:
189  // Inquiries
190  auto isMouseOverListWindow (const FPoint&) -> bool;
191 
192  // Methods
193  void init();
194  void initCallbacks();
195  void draw() override;
196  void onePosUp();
197  void onePosDown();
198  void passEventToListWindow (const FMouseEvent&);
199  void processClick() const;
200  void processRowChanged() const;
201 
202  // Callback methods
203  void cb_setInputField();
204  void cb_closeComboBox();
205  void cb_inputFieldSwitch();
206  void cb_inputFieldHandOver();
207 
208  // Data members
209  FLineEdit input_field{this};
210  FDropDownListBox list_window{this};
211  std::size_t max_items{8};
212  int nf{0};
213  bool is_editable{true};
214 };
215 
216 // non-member function forward declarations
217 //----------------------------------------------------------------------
218 void closeOpenComboBox();
219 auto closeComboBox (FDropDownListBox*, const FPoint&) -> bool;
220 
221 // FComboBox inline functions
222 //----------------------------------------------------------------------
223 inline auto FComboBox::getClassName() const -> FString
224 { return "FComboBox"; }
225 
226 //----------------------------------------------------------------------
227 inline auto FComboBox::getCount() const -> std::size_t
228 { return list_window.list.getCount(); }
229 
230 //----------------------------------------------------------------------
231 inline auto FComboBox::getText() const -> FString
232 { return input_field.getText(); }
233 
234 //----------------------------------------------------------------------
235 template <typename DT>
236 inline auto FComboBox::getItemData() -> clean_fdata_t<DT>&
237 {
238  const std::size_t index = list_window.list.currentItem();
239  return list_window.list.getItem(index).getData<DT>();
240 }
241 
242 //----------------------------------------------------------------------
243 inline auto FComboBox::getLabelOrientation() const -> FLineEdit::LabelOrientation
244 { return input_field.getLabelOrientation(); }
245 
246 //----------------------------------------------------------------------
247 inline void FComboBox::unsetEnable()
248 { setEnable(false); }
249 
250 //----------------------------------------------------------------------
251 inline void FComboBox::setDisable()
252 { setEnable(false); }
253 
254 //----------------------------------------------------------------------
255 inline void FComboBox::unsetShadow()
256 { setShadow(false); }
257 
258 //----------------------------------------------------------------------
259 inline void FComboBox::unsetEditable()
260 { setEditable(false); }
261 
262 //----------------------------------------------------------------------
263 inline auto FComboBox::hasShadow() const -> bool
264 { return getFlags().shadow.shadow; }
265 
266 //----------------------------------------------------------------------
267 template <typename T
268  , typename DT>
269 void FComboBox::insert (const std::initializer_list<T>& list, DT&& d)
270 {
271  for (const auto& item : list)
272  {
273  FListBoxItem listItem (FString() << item, std::forward<DT>(d));
274  insert (listItem);
275  }
276 }
277 
278 //----------------------------------------------------------------------
279 template <typename ItemT
280  , typename DT>
281 void FComboBox::insert (const ItemT& item, DT&& d)
282 {
283  FListBoxItem listItem (FString() << item, std::forward<DT>(d));
284  insert (listItem);
285 }
286 
287 //----------------------------------------------------------------------
288 inline void FComboBox::reserve (std::size_t new_cap)
289 { list_window.list.reserve(new_cap); }
290 
291 //----------------------------------------------------------------------
292 inline void FComboBox::setText (const FString& s)
293 { input_field.setText(s); }
294 
295 //----------------------------------------------------------------------
296 inline void FComboBox::clearText()
297 { input_field.clear(); }
298 
299 //----------------------------------------------------------------------
300 inline void FComboBox::setLabelText (const FString& s)
301 { input_field.setLabelText(s); }
302 
303 //----------------------------------------------------------------------
304 inline void FComboBox::setLabelOrientation (const LabelOrientation o)
305 { input_field.setLabelOrientation(o); }
306 
307 } // namespace finalcut
308 
309 #endif // FCOMBOBOX_H
310 
311 
Definition: fwindow.h:67
Definition: fevent.h:196
Definition: fevent.h:144
Definition: flistbox.h:173
Definition: fevent.h:171
Definition: class_template.cpp:25
Definition: fpoint.h:50
Definition: fsize.h:55
Definition: fcombobox.h:69
Definition: flistbox.h:71
Definition: fcombobox.h:124
Definition: fstring.h:79
Definition: fwidget.h:129
Definition: flineedit.h:67
Definition: fevent.h:124