Collection of DPF-based plugins for packaging
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.

484 lines
14KB

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