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.

437 lines
12KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. START_NAMESPACE_DGL
  20. // --------------------------------------------------------------------------------------------------------------------
  21. // Forward class names
  22. class Application;
  23. class SubWidget;
  24. class TopLevelWidget;
  25. class Window;
  26. // --------------------------------------------------------------------------------------------------------------------
  27. /**
  28. Base DGL Widget class.
  29. This is the base Widget class, from which all widgets are built.
  30. All widgets have a parent widget where they'll be drawn, this can be the top-level widget or a group widget.
  31. This parent is never changed during a widget's lifetime.
  32. Widgets receive events in relative coordinates. (0, 0) means its top-left position.
  33. The top-level widget will draw subwidgets in the order they are constructed.
  34. Early subwidgets are drawn first, at the bottom, then newer ones on top.
  35. Events are sent in the inverse order so that the top-most widgets get
  36. a chance to catch the event and stop its propagation.
  37. All widget event callbacks do nothing by default and onDisplay MUST be reimplemented by subclasses.
  38. @note It is not possible to subclass this Widget class directly, you must use SubWidget or TopLevelWidget instead.
  39. */
  40. class Widget
  41. {
  42. public:
  43. /**
  44. Base event data.
  45. These are the fields present on all Widget events.
  46. @a mod Currently active keyboard modifiers, @see Modifier.
  47. @a mod Event flags, @see Flag.
  48. @a time Event timestamp (if any).
  49. */
  50. struct BaseEvent {
  51. uint mod;
  52. uint flags;
  53. uint time;
  54. /** Constuctor */
  55. BaseEvent() noexcept : mod(0x0), flags(0x0), time(0) {}
  56. /** Destuctor */
  57. virtual ~BaseEvent() noexcept {}
  58. };
  59. /**
  60. Keyboard event.
  61. This event represents low-level key presses and releases.
  62. This can be used for "direct" keyboard handing like key bindings, but must not be interpreted as text input.
  63. Keys are represented portably as Unicode code points, using the "natural" code point for the key.
  64. The @a key field is the code for the pressed key, without any modifiers applied.
  65. For example, a press or release of the 'A' key will have `key` 97 ('a')
  66. regardless of whether shift or control are being held.
  67. Alternatively, the raw @a keycode can be used to work directly with physical keys,
  68. but note that this value is not portable and differs between platforms and hardware.
  69. @a press True if the key was pressed, false if released.
  70. @a key Unicode point of the key pressed.
  71. @a keycode Raw keycode.
  72. @see onKeyboard
  73. */
  74. struct KeyboardEvent : BaseEvent {
  75. bool press;
  76. uint key;
  77. uint keycode;
  78. /** Constuctor */
  79. KeyboardEvent() noexcept
  80. : BaseEvent(),
  81. press(false),
  82. key(0),
  83. keycode(0) {}
  84. };
  85. /**
  86. Special keyboard event.
  87. This event allows the use of keys that do not have unicode points.
  88. Note that some are non-printable keys.
  89. @a press True if the key was pressed, false if released.
  90. @a key The key pressed.
  91. @see onSpecial
  92. */
  93. struct SpecialEvent : BaseEvent {
  94. bool press;
  95. Key key;
  96. /** Constuctor */
  97. SpecialEvent() noexcept
  98. : BaseEvent(),
  99. press(false),
  100. key(Key(0)) {}
  101. };
  102. /**
  103. Character input event.
  104. This event represents text input, usually as the result of a key press.
  105. The text is given both as a Unicode character code and a UTF-8 string.
  106. Note that this event is generated by the platform's input system,
  107. so there is not necessarily a direct correspondence between text events and physical key presses.
  108. For example, with some input methods a sequence of several key presses will generate a single character.
  109. @a keycode Raw key code.
  110. @a character Unicode character code.
  111. @a string UTF-8 string.
  112. @see onCharacterInput
  113. */
  114. struct CharacterInputEvent : BaseEvent {
  115. uint keycode;
  116. uint character;
  117. char string[8];
  118. /** Constuctor */
  119. CharacterInputEvent() noexcept
  120. : BaseEvent(),
  121. keycode(0),
  122. character(0),
  123. string{'\0','\0','\0','\0','\0','\0','\0','\0'} {}
  124. };
  125. /**
  126. Mouse press or release event.
  127. @a button The button number starting from 1 (1 = left, 2 = middle, 3 = right).
  128. @a press True if the button was pressed, false if released.
  129. @a pos The widget-relative coordinates of the pointer.
  130. @see onMouse
  131. */
  132. struct MouseEvent : BaseEvent {
  133. uint button;
  134. bool press;
  135. Point<double> pos;
  136. /** Constuctor */
  137. MouseEvent() noexcept
  138. : BaseEvent(),
  139. button(0),
  140. press(false),
  141. pos(0.0, 0.0) {}
  142. };
  143. /**
  144. Mouse motion event.
  145. @a pos The widget-relative coordinates of the pointer.
  146. @see onMotion
  147. */
  148. struct MotionEvent : BaseEvent {
  149. Point<double> pos;
  150. /** Constuctor */
  151. MotionEvent() noexcept
  152. : BaseEvent(),
  153. pos(0.0, 0.0) {}
  154. };
  155. /**
  156. Mouse scroll event.
  157. The scroll distance is expressed in "lines",
  158. an arbitrary unit that corresponds to a single tick of a detented mouse wheel.
  159. For example, `delta.y` = 1.0 scrolls 1 line up.
  160. Some systems and devices support finer resolution and/or higher values for fast scrolls,
  161. so programs should handle any value gracefully.
  162. @a pos The widget-relative coordinates of the pointer.
  163. @a delta The scroll distance.
  164. @a direction The direction of the scroll or "smooth".
  165. @see onScroll
  166. */
  167. struct ScrollEvent : BaseEvent {
  168. Point<double> pos;
  169. Point<double> delta;
  170. ScrollDirection direction;
  171. /** Constuctor */
  172. ScrollEvent() noexcept
  173. : BaseEvent(),
  174. pos(0.0, 0.0),
  175. delta(0.0, 0.0),
  176. direction(kScrollSmooth) {}
  177. };
  178. /**
  179. Resize event.
  180. @a size The new widget size.
  181. @a oldSize The previous size, may be null.
  182. @see onResize
  183. */
  184. struct ResizeEvent {
  185. Size<uint> size;
  186. Size<uint> oldSize;
  187. /** Constuctor */
  188. ResizeEvent() noexcept
  189. : size(0, 0),
  190. oldSize(0, 0) {}
  191. };
  192. /**
  193. Widget position changed event.
  194. @a pos The new absolute position of the widget.
  195. @a oldPos The previous absolute position of the widget.
  196. @see onPositionChanged
  197. */
  198. struct PositionChangedEvent {
  199. Point<int> pos;
  200. Point<int> oldPos;
  201. /** Constuctor */
  202. PositionChangedEvent() noexcept
  203. : pos(0, 0),
  204. oldPos(0, 0) {}
  205. };
  206. private:
  207. /**
  208. Private constructor, reserved for TopLevelWidget class.
  209. */
  210. explicit Widget(TopLevelWidget* topLevelWidget);
  211. /**
  212. Private constructor, reserved for SubWidget class.
  213. */
  214. explicit Widget(Widget* widgetToGroupTo);
  215. public:
  216. /**
  217. Destructor.
  218. */
  219. virtual ~Widget();
  220. /**
  221. Check if this widget is visible within its parent window.
  222. Invisible widgets do not receive events except resize.
  223. */
  224. bool isVisible() const noexcept;
  225. /**
  226. Set widget visible (or not) according to @a visible.
  227. */
  228. void setVisible(bool visible);
  229. /**
  230. Show widget.
  231. This is the same as calling setVisible(true).
  232. */
  233. void show();
  234. /**
  235. Hide widget.
  236. This is the same as calling setVisible(false).
  237. */
  238. void hide();
  239. /**
  240. Get width.
  241. */
  242. uint getWidth() const noexcept;
  243. /**
  244. Get height.
  245. */
  246. uint getHeight() const noexcept;
  247. /**
  248. Get size.
  249. */
  250. const Size<uint> getSize() const noexcept;
  251. /**
  252. Set width.
  253. */
  254. void setWidth(uint width) noexcept;
  255. /**
  256. Set height.
  257. */
  258. void setHeight(uint height) noexcept;
  259. /**
  260. Set size using @a width and @a height values.
  261. */
  262. void setSize(uint width, uint height) noexcept;
  263. /**
  264. Set size.
  265. */
  266. void setSize(const Size<uint>& size) noexcept;
  267. /**
  268. Get the Id associated with this widget.
  269. @see setId
  270. */
  271. uint getId() const noexcept;
  272. /**
  273. Set an Id to be associated with this widget.
  274. @see getId
  275. */
  276. void setId(uint id) noexcept;
  277. /**
  278. Get the application associated with this widget's window.
  279. This is the same as calling `getTopLevelWidget()->getApp()`.
  280. */
  281. Application& getApp() const noexcept;
  282. /**
  283. Get the window associated with this widget.
  284. This is the same as calling `getTopLevelWidget()->getWindow()`.
  285. */
  286. Window& getWindow() const noexcept;
  287. /**
  288. Get the graphics context associated with this widget's window.
  289. GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable,
  290. for example GraphicsContext.
  291. @see CairoSubWidget, CairoTopLevelWidget
  292. */
  293. const GraphicsContext& getGraphicsContext() const noexcept;
  294. /**
  295. Get top-level widget, as passed directly in the constructor
  296. or going up the chain of group widgets until it finds the top-level one.
  297. */
  298. TopLevelWidget* getTopLevelWidget() const noexcept;
  299. /**
  300. Request repaint of this widget's area to the window this widget belongs to.
  301. On the raw Widget class this function does nothing.
  302. */
  303. virtual void repaint() noexcept;
  304. DISTRHO_DEPRECATED_BY("getApp()")
  305. Application& getParentApp() const noexcept { return getApp(); }
  306. DISTRHO_DEPRECATED_BY("getWindow()")
  307. Window& getParentWindow() const noexcept { return getWindow(); }
  308. protected:
  309. /**
  310. A function called to draw the widget contents.
  311. */
  312. virtual void onDisplay() = 0;
  313. /**
  314. A function called when a key is pressed or released.
  315. @return True to stop event propagation, false otherwise.
  316. */
  317. virtual bool onKeyboard(const KeyboardEvent&);
  318. /**
  319. A function called when a special key is pressed or released.
  320. @return True to stop event propagation, false otherwise.
  321. */
  322. virtual bool onSpecial(const SpecialEvent&);
  323. /**
  324. A function called when an UTF-8 character is received.
  325. @return True to stop event propagation, false otherwise.
  326. */
  327. virtual bool onCharacterInput(const CharacterInputEvent&);
  328. /**
  329. A function called when a mouse button is pressed or released.
  330. @return True to stop event propagation, false otherwise.
  331. */
  332. virtual bool onMouse(const MouseEvent&);
  333. /**
  334. A function called when the pointer moves.
  335. @return True to stop event propagation, false otherwise.
  336. */
  337. virtual bool onMotion(const MotionEvent&);
  338. /**
  339. A function called on scrolling (e.g. mouse wheel or track pad).
  340. @return True to stop event propagation, false otherwise.
  341. */
  342. virtual bool onScroll(const ScrollEvent&);
  343. /**
  344. A function called when the widget is resized.
  345. */
  346. virtual void onResize(const ResizeEvent&);
  347. private:
  348. struct PrivateData;
  349. PrivateData* const pData;
  350. friend class SubWidget;
  351. friend class TopLevelWidget;
  352. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  353. };
  354. // --------------------------------------------------------------------------------------------------------------------
  355. END_NAMESPACE_DGL
  356. #endif // DGL_WIDGET_HPP_INCLUDED