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.

482 lines
12KB

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