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.

Widget.hpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. */
  47. struct BaseEvent {
  48. /** Currently active keyboard modifiers. @see Modifier */
  49. uint mod;
  50. /** Event flags. @see EventFlag */
  51. uint flags;
  52. /** Event timestamp (if any). */
  53. uint time;
  54. /** Constructor for default/null values */
  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. @see onKeyboard
  70. */
  71. struct KeyboardEvent : BaseEvent {
  72. /** True if the key was pressed, false if released. */
  73. bool press;
  74. /** Unicode point of the key pressed. */
  75. uint key;
  76. /** Raw keycode. */
  77. uint keycode;
  78. /** Constructor for default/null values */
  79. KeyboardEvent() noexcept
  80. : BaseEvent(),
  81. press(false),
  82. key(0),
  83. keycode(0) {}
  84. };
  85. /**
  86. Special keyboard event.
  87. DEPRECATED This used to be part of DPF due to pugl, but now deprecated and simply non-functional.
  88. All events go through KeyboardEvent or CharacterInputEvent, use those instead.
  89. */
  90. struct DISTRHO_DEPRECATED_BY("KeyboardEvent") SpecialEvent : BaseEvent {
  91. bool press;
  92. Key key;
  93. /** Constructor for default/null values */
  94. SpecialEvent() noexcept
  95. : BaseEvent(),
  96. press(false),
  97. key(Key(0)) {}
  98. };
  99. /**
  100. Character input event.
  101. This event represents text input, usually as the result of a key press.
  102. The text is given both as a Unicode character code and a UTF-8 string.
  103. Note that this event is generated by the platform's input system,
  104. so there is not necessarily a direct correspondence between text events and physical key presses.
  105. For example, with some input methods a sequence of several key presses will generate a single character.
  106. @see onCharacterInput
  107. */
  108. struct CharacterInputEvent : BaseEvent {
  109. /** Raw key code. */
  110. uint keycode;
  111. /** Unicode character code. */
  112. uint character;
  113. /** UTF-8 string. */
  114. char string[8];
  115. /** Constructor for default/null values */
  116. CharacterInputEvent() noexcept
  117. : BaseEvent(),
  118. keycode(0),
  119. character(0),
  120. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  121. string{'\0','\0','\0','\0','\0','\0','\0','\0'} {}
  122. #else
  123. string() { std::memset(string, 0, sizeof(string)); }
  124. #endif
  125. };
  126. /**
  127. Mouse press or release event.
  128. @see onMouse
  129. */
  130. struct MouseEvent : BaseEvent {
  131. /** The button number starting from 1. @see MouseButton */
  132. uint button;
  133. /** True if the button was pressed, false if released. */
  134. bool press;
  135. /** The widget-relative coordinates of the pointer. */
  136. Point<double> pos;
  137. /** The absolute coordinates of the pointer. */
  138. Point<double> absolutePos;
  139. /** Constructor for default/null values */
  140. MouseEvent() noexcept
  141. : BaseEvent(),
  142. button(0),
  143. press(false),
  144. pos(0.0, 0.0),
  145. absolutePos(0.0, 0.0) {}
  146. };
  147. /**
  148. Mouse motion event.
  149. @see onMotion
  150. */
  151. struct MotionEvent : BaseEvent {
  152. /** The widget-relative coordinates of the pointer. */
  153. Point<double> pos;
  154. /** The absolute coordinates of the pointer. */
  155. Point<double> absolutePos;
  156. /** Constructor for default/null values */
  157. MotionEvent() noexcept
  158. : BaseEvent(),
  159. pos(0.0, 0.0),
  160. absolutePos(0.0, 0.0) {}
  161. };
  162. /**
  163. Mouse scroll event.
  164. The scroll distance is expressed in "lines",
  165. an arbitrary unit that corresponds to a single tick of a detented mouse wheel.
  166. For example, `delta.y` = 1.0 scrolls 1 line up.
  167. Some systems and devices support finer resolution and/or higher values for fast scrolls,
  168. so programs should handle any value gracefully.
  169. @see onScroll
  170. */
  171. struct ScrollEvent : BaseEvent {
  172. /** The widget-relative coordinates of the pointer. */
  173. Point<double> pos;
  174. /** The absolute coordinates of the pointer. */
  175. Point<double> absolutePos;
  176. /** The scroll distance. */
  177. Point<double> delta;
  178. /** The direction of the scroll or "smooth". */
  179. ScrollDirection direction;
  180. /** Constructor for default/null values */
  181. ScrollEvent() noexcept
  182. : BaseEvent(),
  183. pos(0.0, 0.0),
  184. absolutePos(0.0, 0.0),
  185. delta(0.0, 0.0),
  186. direction(kScrollSmooth) {}
  187. };
  188. /**
  189. Resize event.
  190. @see onResize
  191. */
  192. struct ResizeEvent {
  193. /** The new widget size. */
  194. Size<uint> size;
  195. /** The previous size, can be null. */
  196. Size<uint> oldSize;
  197. /** Constructor for default/null values */
  198. ResizeEvent() noexcept
  199. : size(0, 0),
  200. oldSize(0, 0) {}
  201. };
  202. /**
  203. Widget position changed event.
  204. @see onPositionChanged
  205. */
  206. struct PositionChangedEvent {
  207. /** The new absolute position of the widget. */
  208. Point<int> pos;
  209. /** The previous absolute position of the widget. */
  210. Point<int> oldPos;
  211. /** Constructor for default/null values */
  212. PositionChangedEvent() noexcept
  213. : pos(0, 0),
  214. oldPos(0, 0) {}
  215. };
  216. private:
  217. /**
  218. Private constructor, reserved for TopLevelWidget class.
  219. */
  220. explicit Widget(TopLevelWidget* topLevelWidget);
  221. /**
  222. Private constructor, reserved for SubWidget class.
  223. */
  224. explicit Widget(Widget* widgetToGroupTo);
  225. public:
  226. /**
  227. Destructor.
  228. */
  229. virtual ~Widget();
  230. /**
  231. Check if this widget is visible within its parent window.
  232. Invisible widgets do not receive events except resize.
  233. */
  234. bool isVisible() const noexcept;
  235. /**
  236. Set widget visible (or not) according to @a visible.
  237. */
  238. void setVisible(bool visible);
  239. /**
  240. Show widget.
  241. This is the same as calling setVisible(true).
  242. */
  243. void show();
  244. /**
  245. Hide widget.
  246. This is the same as calling setVisible(false).
  247. */
  248. void hide();
  249. /**
  250. Get width.
  251. */
  252. uint getWidth() const noexcept;
  253. /**
  254. Get height.
  255. */
  256. uint getHeight() const noexcept;
  257. /**
  258. Get size.
  259. */
  260. const Size<uint> getSize() const noexcept;
  261. /**
  262. Set width.
  263. */
  264. void setWidth(uint width) noexcept;
  265. /**
  266. Set height.
  267. */
  268. void setHeight(uint height) noexcept;
  269. /**
  270. Set size using @a width and @a height values.
  271. */
  272. void setSize(uint width, uint height) noexcept;
  273. /**
  274. Set size.
  275. */
  276. void setSize(const Size<uint>& size) noexcept;
  277. /**
  278. Get the Id associated with this widget.
  279. @see setId
  280. */
  281. uint getId() const noexcept;
  282. /**
  283. Set an Id to be associated with this widget.
  284. @see getId
  285. */
  286. void setId(uint id) noexcept;
  287. /**
  288. Get the application associated with this widget's window.
  289. This is the same as calling `getTopLevelWidget()->getApp()`.
  290. */
  291. Application& getApp() const noexcept;
  292. /**
  293. Get the window associated with this widget.
  294. This is the same as calling `getTopLevelWidget()->getWindow()`.
  295. */
  296. Window& getWindow() const noexcept;
  297. /**
  298. Get the graphics context associated with this widget's window.
  299. GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable,
  300. for example GraphicsContext.
  301. @see CairoSubWidget, CairoTopLevelWidget
  302. */
  303. const GraphicsContext& getGraphicsContext() const noexcept;
  304. /**
  305. Get top-level widget, as passed directly in the constructor
  306. or going up the chain of group widgets until it finds the top-level one.
  307. */
  308. TopLevelWidget* getTopLevelWidget() const noexcept;
  309. /**
  310. Request repaint of this widget's area to the window this widget belongs to.
  311. On the raw Widget class this function does nothing.
  312. */
  313. virtual void repaint() noexcept;
  314. DISTRHO_DEPRECATED_BY("getApp()")
  315. Application& getParentApp() const noexcept { return getApp(); }
  316. DISTRHO_DEPRECATED_BY("getWindow()")
  317. Window& getParentWindow() const noexcept { return getWindow(); }
  318. protected:
  319. /**
  320. A function called to draw the widget contents.
  321. */
  322. virtual void onDisplay() = 0;
  323. /**
  324. A function called when a key is pressed or released.
  325. @return True to stop event propagation, false otherwise.
  326. */
  327. virtual bool onKeyboard(const KeyboardEvent&);
  328. /**
  329. A function called when an UTF-8 character is received.
  330. @return True to stop event propagation, false otherwise.
  331. */
  332. virtual bool onCharacterInput(const CharacterInputEvent&);
  333. /**
  334. A function called when a mouse button is pressed or released.
  335. @return True to stop event propagation, false otherwise.
  336. */
  337. virtual bool onMouse(const MouseEvent&);
  338. /**
  339. A function called when the pointer moves.
  340. @return True to stop event propagation, false otherwise.
  341. */
  342. virtual bool onMotion(const MotionEvent&);
  343. /**
  344. A function called on scrolling (e.g. mouse wheel or track pad).
  345. @return True to stop event propagation, false otherwise.
  346. */
  347. virtual bool onScroll(const ScrollEvent&);
  348. /**
  349. A function called when the widget is resized.
  350. */
  351. virtual void onResize(const ResizeEvent&);
  352. /**
  353. A function called when a special key is pressed or released.
  354. DEPRECATED use onKeyboard or onCharacterInput
  355. */
  356. #if defined(__clang__)
  357. # pragma clang diagnostic push
  358. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  359. #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 460
  360. # pragma GCC diagnostic push
  361. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  362. #endif
  363. virtual bool onSpecial(const SpecialEvent&) { return false; }
  364. #if defined(__clang__)
  365. # pragma clang diagnostic pop
  366. #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 460
  367. # pragma GCC diagnostic pop
  368. #endif
  369. private:
  370. struct PrivateData;
  371. PrivateData* const pData;
  372. friend class SubWidget;
  373. friend class TopLevelWidget;
  374. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  375. };
  376. // --------------------------------------------------------------------------------------------------------------------
  377. END_NAMESPACE_DGL
  378. #endif // DGL_WIDGET_HPP_INCLUDED