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