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