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.

390 lines
9.0KB

  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. #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. Constructor.
  154. */
  155. explicit Widget(Window& parent);
  156. /**
  157. Constructor for a subwidget.
  158. */
  159. explicit Widget(Widget* groupWidget);
  160. /**
  161. Destructor.
  162. */
  163. virtual ~Widget();
  164. /**
  165. Check if this widget is visible within its parent window.
  166. Invisible widgets do not receive events except resize.
  167. */
  168. bool isVisible() const noexcept;
  169. /**
  170. Set widget visible (or not) according to @a yesNo.
  171. */
  172. void setVisible(bool yesNo);
  173. /**
  174. Show widget.
  175. This is the same as calling setVisible(true).
  176. */
  177. void show();
  178. /**
  179. Hide widget.
  180. This is the same as calling setVisible(false).
  181. */
  182. void hide();
  183. /**
  184. Get width.
  185. */
  186. uint getWidth() const noexcept;
  187. /**
  188. Get height.
  189. */
  190. uint getHeight() const noexcept;
  191. /**
  192. Get size.
  193. */
  194. const Size<uint>& getSize() const noexcept;
  195. /**
  196. Set width.
  197. */
  198. void setWidth(uint width) noexcept;
  199. /**
  200. Set height.
  201. */
  202. void setHeight(uint height) noexcept;
  203. /**
  204. Set size using @a width and @a height values.
  205. */
  206. void setSize(uint width, uint height) noexcept;
  207. /**
  208. Set size.
  209. */
  210. void setSize(const Size<uint>& size) noexcept;
  211. /**
  212. Get absolute X.
  213. */
  214. int getAbsoluteX() const noexcept;
  215. /**
  216. Get absolute Y.
  217. */
  218. int getAbsoluteY() const noexcept;
  219. /**
  220. Get absolute position.
  221. */
  222. const Point<int>& getAbsolutePos() const noexcept;
  223. /**
  224. Set absolute X.
  225. */
  226. void setAbsoluteX(int x) noexcept;
  227. /**
  228. Set absolute Y.
  229. */
  230. void setAbsoluteY(int y) noexcept;
  231. /**
  232. Set absolute position using @a x and @a y values.
  233. */
  234. void setAbsolutePos(int x, int y) noexcept;
  235. /**
  236. Set absolute position.
  237. */
  238. void setAbsolutePos(const Point<int>& pos) noexcept;
  239. /**
  240. Get this widget's window application.
  241. Same as calling getParentWindow().getApp().
  242. */
  243. Application& getParentApp() const noexcept;
  244. /**
  245. Get parent window, as passed in the constructor.
  246. */
  247. Window& getParentWindow() const noexcept;
  248. /**
  249. Check if this widget contains the point defined by @a x and @a y.
  250. */
  251. bool contains(int x, int y) const noexcept;
  252. /**
  253. Check if this widget contains the point @a pos.
  254. */
  255. bool contains(const Point<int>& pos) const noexcept;
  256. /**
  257. Tell this widget's window to repaint itself.
  258. */
  259. void repaint() noexcept;
  260. /**
  261. Get the Id associated with this widget.
  262. @see setId
  263. */
  264. uint getId() const noexcept;
  265. /**
  266. Set an Id to be associated with this widget.
  267. @see getId
  268. */
  269. void setId(uint id) noexcept;
  270. protected:
  271. /**
  272. A function called to draw the view contents with OpenGL.
  273. */
  274. virtual void onDisplay() = 0;
  275. /**
  276. A function called when a key is pressed or released.
  277. @return True to stop event propagation, false otherwise.
  278. */
  279. virtual bool onKeyboard(const KeyboardEvent&);
  280. /**
  281. A function called when a special key is pressed or released.
  282. @return True to stop event propagation, false otherwise.
  283. */
  284. virtual bool onSpecial(const SpecialEvent&);
  285. /**
  286. A function called when a mouse button is pressed or released.
  287. @return True to stop event propagation, false otherwise.
  288. */
  289. virtual bool onMouse(const MouseEvent&);
  290. /**
  291. A function called when the pointer moves.
  292. @return True to stop event propagation, false otherwise.
  293. */
  294. virtual bool onMotion(const MotionEvent&);
  295. /**
  296. A function called on scrolling (e.g. mouse wheel or track pad).
  297. @return True to stop event propagation, false otherwise.
  298. */
  299. virtual bool onScroll(const ScrollEvent&);
  300. /**
  301. A function called when the widget is resized.
  302. */
  303. virtual void onResize(const ResizeEvent&);
  304. private:
  305. struct PrivateData;
  306. PrivateData* const pData;
  307. /** @internal */
  308. explicit Widget(Widget* groupWidget, bool addToSubWidgets);
  309. friend class ImageSlider;
  310. friend class NanoWidget;
  311. friend class Window;
  312. friend class StandaloneWindow;
  313. #ifdef DISTRHO_DEFINES_H_INCLUDED
  314. friend class DISTRHO_NAMESPACE::UI;
  315. #endif
  316. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  317. };
  318. // -----------------------------------------------------------------------
  319. END_NAMESPACE_DGL
  320. #endif // DGL_WIDGET_HPP_INCLUDED