DISTRHO Plugin Framework
Widget.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any purpose with
6  * or without fee is hereby granted, provided that the above copyright notice and this
7  * permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10  * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef DGL_WIDGET_HPP_INCLUDED
18 #define DGL_WIDGET_HPP_INCLUDED
19 
20 #include "Geometry.hpp"
21 
22 #include <vector>
23 
24 // -----------------------------------------------------------------------
25 // Forward class names
26 
27 START_NAMESPACE_DISTRHO
28 class UI;
29 END_NAMESPACE_DISTRHO
30 
31 START_NAMESPACE_DGL
32 
33 class Application;
34 class ImageSlider;
35 class NanoWidget;
36 class Window;
37 class StandaloneWindow;
38 
39 // -----------------------------------------------------------------------
40 
41 /**
42  Base DGL Widget class.
43 
44  This is the base Widget class, from which all widgets are built.
45 
46  All widgets have a parent Window where they'll be drawn.
47  This parent is never changed during the widget lifetime.
48 
49  Widgets receive events in relative coordinates.
50  (0, 0) means its top-left position.
51 
52  Windows paint widgets in the order they are constructed.
53  Early widgets are drawn first, at the bottom, then newer ones on top.
54  Events are sent in the inverse order so that the top-most widget gets
55  a chance to catch the event and stop its propagation.
56 
57  All widget event callbacks do nothing by default.
58  */
59 class Widget
60 {
61 public:
62  /**
63  Base event data.
64  @a mod The currently active keyboard modifiers, @see Modifier.
65  @a time The timestamp (if any).
66  */
67  struct BaseEvent {
68  uint mod;
69  uint32_t time;
70 
71  /** Constuctor */
72  BaseEvent() noexcept : mod(0x0), time(0) {}
73  /** Destuctor */
74  virtual ~BaseEvent() noexcept {}
75  };
76 
77  /**
78  Keyboard event.
79  @a press True if the key was pressed, false if released.
80  @a key Unicode point of the key pressed.
81  @see onKeyboard
82  */
84  bool press;
85  uint key;
86 
87  /** Constuctor */
88  KeyboardEvent() noexcept
89  : BaseEvent(),
90  press(false),
91  key(0) {}
92  };
93 
94  /**
95  Special keyboard event.
96  @a press True if the key was pressed, false if released.
97  @a key The key pressed.
98  @see onSpecial
99  */
101  bool press;
102  Key key;
103 
104  /** Constuctor */
105  SpecialEvent() noexcept
106  : BaseEvent(),
107  press(false),
108  key(Key(0)) {}
109  };
110 
111  /**
112  Mouse event.
113  @a button The button number (1 = left, 2 = middle, 3 = right).
114  @a press True if the key was pressed, false if released.
115  @a pos The widget-relative coordinates of the pointer.
116  @see onMouse
117  */
119  int button;
120  bool press;
121  Point<int> pos;
122 
123  /** Constuctor */
124  MouseEvent() noexcept
125  : BaseEvent(),
126  button(0),
127  press(false),
128  pos(0, 0) {}
129  };
130 
131  /**
132  Mouse motion event.
133  @a pos The widget-relative coordinates of the pointer.
134  @see onMotion
135  */
137  Point<int> pos;
138 
139  /** Constuctor */
140  MotionEvent() noexcept
141  : BaseEvent(),
142  pos(0, 0) {}
143  };
144 
145  /**
146  Mouse scroll event.
147  @a pos The widget-relative coordinates of the pointer.
148  @a delta The scroll distance.
149  @see onScroll
150  */
152  Point<int> pos;
153  Point<float> delta;
154 
155  /** Constuctor */
156  ScrollEvent() noexcept
157  : BaseEvent(),
158  pos(0, 0),
159  delta(0.0f, 0.0f) {}
160  };
161 
162  /**
163  Resize event.
164  @a size The new widget size.
165  @a oldSize The previous size, may be null.
166  @see onResize
167  */
168  struct ResizeEvent {
169  Size<uint> size;
170  Size<uint> oldSize;
171 
172  /** Constuctor */
173  ResizeEvent() noexcept
174  : size(0, 0),
175  oldSize(0, 0) {}
176  };
177 
178  /**
179  Constructor.
180  */
181  explicit Widget(Window& parent);
182 
183  /**
184  Constructor for a subwidget.
185  */
186  explicit Widget(Widget* groupWidget);
187 
188  /**
189  Destructor.
190  */
191  virtual ~Widget();
192 
193  /**
194  Check if this widget is visible within its parent window.
195  Invisible widgets do not receive events except resize.
196  */
197  bool isVisible() const noexcept;
198 
199  /**
200  Set widget visible (or not) according to @a yesNo.
201  */
202  void setVisible(bool yesNo);
203 
204  /**
205  Show widget.
206  This is the same as calling setVisible(true).
207  */
208  void show();
209 
210  /**
211  Hide widget.
212  This is the same as calling setVisible(false).
213  */
214  void hide();
215 
216  /**
217  Get width.
218  */
219  uint getWidth() const noexcept;
220 
221  /**
222  Get height.
223  */
224  uint getHeight() const noexcept;
225 
226  /**
227  Get size.
228  */
229  const Size<uint>& getSize() const noexcept;
230 
231  /**
232  Set width.
233  */
234  void setWidth(uint width) noexcept;
235 
236  /**
237  Set height.
238  */
239  void setHeight(uint height) noexcept;
240 
241  /**
242  Set size using @a width and @a height values.
243  */
244  void setSize(uint width, uint height) noexcept;
245 
246  /**
247  Set size.
248  */
249  void setSize(const Size<uint>& size) noexcept;
250 
251  /**
252  Get absolute X.
253  */
254  int getAbsoluteX() const noexcept;
255 
256  /**
257  Get absolute Y.
258  */
259  int getAbsoluteY() const noexcept;
260 
261  /**
262  Get absolute position.
263  */
264  const Point<int>& getAbsolutePos() const noexcept;
265 
266  /**
267  Set absolute X.
268  */
269  void setAbsoluteX(int x) noexcept;
270 
271  /**
272  Set absolute Y.
273  */
274  void setAbsoluteY(int y) noexcept;
275 
276  /**
277  Set absolute position using @a x and @a y values.
278  */
279  void setAbsolutePos(int x, int y) noexcept;
280 
281  /**
282  Set absolute position.
283  */
284  void setAbsolutePos(const Point<int>& pos) noexcept;
285 
286  /**
287  Get this widget's window application.
288  Same as calling getParentWindow().getApp().
289  */
290  Application& getParentApp() const noexcept;
291 
292  /**
293  Get parent window, as passed in the constructor.
294  */
295  Window& getParentWindow() const noexcept;
296 
297  /**
298  Check if this widget contains the point defined by @a x and @a y.
299  */
300  bool contains(int x, int y) const noexcept;
301 
302  /**
303  Check if this widget contains the point @a pos.
304  */
305  bool contains(const Point<int>& pos) const noexcept;
306 
307  /**
308  Tell this widget's window to repaint itself.
309  */
310  void repaint() noexcept;
311 
312  /**
313  Get the Id associated with this widget.
314  @see setId
315  */
316  uint getId() const noexcept;
317 
318  /**
319  Set an Id to be associated with this widget.
320  @see getId
321  */
322  void setId(uint id) noexcept;
323 
324 protected:
325  /**
326  A function called to draw the view contents with OpenGL.
327  */
328  virtual void onDisplay() = 0;
329 
330  /**
331  A function called when a key is pressed or released.
332  @return True to stop event propagation, false otherwise.
333  */
334  virtual bool onKeyboard(const KeyboardEvent&);
335 
336  /**
337  A function called when a special key is pressed or released.
338  @return True to stop event propagation, false otherwise.
339  */
340  virtual bool onSpecial(const SpecialEvent&);
341 
342  /**
343  A function called when a mouse button is pressed or released.
344  @return True to stop event propagation, false otherwise.
345  */
346  virtual bool onMouse(const MouseEvent&);
347 
348  /**
349  A function called when the pointer moves.
350  @return True to stop event propagation, false otherwise.
351  */
352  virtual bool onMotion(const MotionEvent&);
353 
354  /**
355  A function called on scrolling (e.g. mouse wheel or track pad).
356  @return True to stop event propagation, false otherwise.
357  */
358  virtual bool onScroll(const ScrollEvent&);
359 
360  /**
361  A function called when the widget is resized.
362  */
363  virtual void onResize(const ResizeEvent&);
364 
365 private:
366  struct PrivateData;
367  PrivateData* const pData;
368 
369  /** @internal */
370  explicit Widget(Widget* groupWidget, bool addToSubWidgets);
371 
372  friend class ImageSlider;
373  friend class NanoWidget;
374  friend class Window;
375  friend class StandaloneWindow;
376  friend class DISTRHO_NAMESPACE::UI;
377 
378  DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
379 };
380 
381 // -----------------------------------------------------------------------
382 
383 END_NAMESPACE_DGL
384 
385 #endif // DGL_WIDGET_HPP_INCLUDED
Window & getParentWindow() const noexcept
const Point< int > & getAbsolutePos() const noexcept
virtual bool onSpecial(const SpecialEvent &)
virtual bool onScroll(const ScrollEvent &)
virtual bool onMouse(const MouseEvent &)
MouseEvent() noexcept
Definition: Widget.hpp:124
Definition: Widget.hpp:100
ScrollEvent() noexcept
Definition: Widget.hpp:156
ResizeEvent() noexcept
Definition: Widget.hpp:173
void setId(uint id) noexcept
Definition: NanoVG.hpp:862
Definition: Window.hpp:30
virtual void onDisplay()=0
uint getWidth() const noexcept
MotionEvent() noexcept
Definition: Widget.hpp:140
bool contains(int x, int y) const noexcept
void setSize(uint width, uint height) noexcept
KeyboardEvent() noexcept
Definition: Widget.hpp:88
Definition: Widget.hpp:83
void setAbsolutePos(int x, int y) noexcept
virtual ~Widget()
int getAbsoluteX() const noexcept
void setAbsoluteX(int x) noexcept
Definition: StandaloneWindow.hpp:28
virtual void onResize(const ResizeEvent &)
Definition: Widget.hpp:136
void setAbsoluteY(int y) noexcept
void setHeight(uint height) noexcept
const Size< uint > & getSize() const noexcept
bool isVisible() const noexcept
void hide()
Definition: Application.hpp:41
Definition: Widget.hpp:168
Definition: Widget.hpp:67
BaseEvent() noexcept
Definition: Widget.hpp:72
virtual bool onMotion(const MotionEvent &)
Definition: DistrhoUtils.hpp:227
Definition: Widget.hpp:59
virtual bool onKeyboard(const KeyboardEvent &)
Definition: Widget.hpp:118
virtual ~BaseEvent() noexcept
Definition: Widget.hpp:74
uint getId() const noexcept
uint getHeight() const noexcept
Definition: Widget.hpp:151
Widget(Window &parent)
void setWidth(uint width) noexcept
SpecialEvent() noexcept
Definition: Widget.hpp:105
Definition: DistrhoUI.hpp:48
void repaint() noexcept
Definition: ImageWidgets.hpp:166
void show()
Application & getParentApp() const noexcept
void setVisible(bool yesNo)
int getAbsoluteY() const noexcept