DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Widget.hpp 9.5KB

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