Audio plugin host https://kx.studio/carla
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.

386 lines
8.9KB

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