FINAL CUT
fscrollbar.h
1 /***********************************************************************
2 * fscrollbar.h - Widget FScrollBar *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2012-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  * ▕ FScrollBar ▏
40  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▏
41  */
42 
43 #ifndef FSCROLLBAR_H
44 #define FSCROLLBAR_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 <functional>
51 #include <memory>
52 
53 #include "final/fapplication.h"
54 #include "final/fwidget.h"
55 #include "final/util/flog.h"
56 
57 namespace finalcut
58 {
59 
60 // class forward declaration
61 class FScrollBar;
62 
63 // Legacy code compatibility
64 using FScrollbar = FScrollBar;
65 
66 // Global using-declaration
67 using FScrollBarPtr = std::shared_ptr<FScrollBar>;
68 
69 //----------------------------------------------------------------------
70 // class FScrollBar
71 //----------------------------------------------------------------------
72 
73 class FScrollBar : public FWidget
74 {
75  public:
76  // Using-declaration
77  using FWidget::setGeometry;
78 
79  // Enumeration
80  enum class ScrollType : uInt8
81  {
82  None = 0,
83  Jump = 1,
84  StepBackward = 2,
85  StepForward = 3,
86  PageBackward = 4,
87  PageForward = 5,
88  WheelUp = 6,
89  WheelDown = 7,
90  WheelLeft = 8,
91  WheelRight = 9
92  };
93 
94  // Constructors
95  explicit FScrollBar (FWidget* = nullptr);
96  explicit FScrollBar (Orientation = Orientation::Vertical, FWidget* = nullptr);
97 
98  // Destructor
99  ~FScrollBar() override;
100 
101  // Accessors
102  auto getClassName() const -> FString override;
103  auto getValue() const noexcept -> int;
104  auto getScrollType() const -> ScrollType;
105 
106  // Mutators
107  void setMinimum (int);
108  void setMaximum (int);
109  void setRange (int, int);
110  void setValue (int);
111  void setSteps (double);
112  void setPageSize (int, int);
113  void setOrientation (Orientation);
114  void setSize (const FSize&, bool = true) override;
115  void setGeometry (const FPoint&, const FSize&, bool = true) override;
116 
117  // Methods
118  void resize() override;
119  void redraw() override;
120  void calculateSliderValues();
121  void drawBar();
122 
123  // Event handlers
124  void onMouseDown (FMouseEvent*) override;
125  void onMouseUp (FMouseEvent*) override;
126  void onMouseMove (FMouseEvent*) override;
127  void onWheel (FWheelEvent*) override;
128  void onTimer (FTimerEvent*) override;
129 
130  private:
131  // Methods
132  void init();
133  void draw() override;
134  void drawVerticalBar();
135  void drawVerticalBackgroundLine();
136  void drawHorizontalBar();
137  void drawHorizontalBackgroundColumn();
138  void drawButtons();
139  void drawNewFontButtons();
140  void drawDefaultButtons();
141  auto getClickedScrollType (int, int) const -> ScrollType;
142  auto getVerticalClickedScrollType (int) const -> ScrollType;
143  auto getHorizontalClickedScrollType (int) const -> ScrollType;
144  auto getNewFontHorizontalScrollType (int) const -> ScrollType;
145  auto getHorizontalScrollType (int) const -> ScrollType;
146  auto getSliderClickPos (int, int) const -> int;
147  auto isMouseOutsideScrollBar (int, int) const -> bool;
148  auto shouldStopTimer() const -> bool;
149  void jumpToClickPos (int, int);
150  void jumpToClickPos (int);
151  void handleJumpScroll (int, int);
152  void stopTimer();
153  void avoidScrollOvershoot();
154  void processScroll();
155  void changeOnResize();
156  void handleWidgetFocus() const;
157  void handleSliderClick (int, int);
158  void handleTrackClick (int, int);
159 
160  // Data members
161  ScrollType scroll_type{ScrollType::None};
162  bool threshold_reached{false};
163  int threshold_time{500};
164  int repeat_time{80};
165  int slider_click_pos{-1};
166  int slider_click_stop_pos{-1};
167  int current_slider_pos{-1};
168  int slider_pos{0};
169  std::size_t slider_length{18}; // = bar_length
170  std::size_t bar_length{18}; // = length - 2
171  int val{0};
172  int min{0};
173  int max{99};
174  int pagesize{0};
175  double steps{1};
176  std::size_t length{20};
177  Orientation bar_orientation{Orientation::Vertical};
178  int max_color{FVTerm::getFOutput()->getMaxColor()};
179 };
180 
181 // non-member functions
182 //----------------------------------------------------------------------
183 template <typename Instance
184  , typename Callback>
185 void initScrollBar ( FScrollBarPtr& bar
186  , Orientation o
187  , Instance cb_instance
188  , const Callback& cb_handler )
189 {
190  bar = std::make_shared<FScrollBar>(o, cb_instance);
191  bar->setMinimum(0);
192  bar->setValue(0);
193  bar->hide();
194  bar->addCallback
195  (
196  "change-value",
197  std::bind(cb_handler, cb_instance, bar.get())
198  );
199 }
200 
201 
202 // FScrollBar inline functions
203 //----------------------------------------------------------------------
204 inline auto FScrollBar::getClassName() const -> FString
205 { return "FScrollBar"; }
206 
207 //----------------------------------------------------------------------
208 inline auto FScrollBar::getValue() const noexcept -> int
209 { return val; }
210 
211 //----------------------------------------------------------------------
212 inline auto FScrollBar::getScrollType() const -> ScrollType
213 { return scroll_type; }
214 
215 } // namespace finalcut
216 
217 #endif // FSCROLLBAR_H
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: fstring.h:82
Definition: fwidget.h:129
Definition: fscrollbar.h:73