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 8.1KB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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 App;
  23. class Window;
  24. // -----------------------------------------------------------------------
  25. /**
  26. Base DGL Widget class.
  27. This is the base Widget class, from which all widgets are built.
  28. All widgets have a parent Window where they'll be drawn.
  29. This parent is never changed during the widget lifetime.
  30. Widgets receive events in relative coordinates.
  31. (0, 0) means its top-left position.
  32. Windows paint widgets in the order they are constructed.
  33. Early widgets are drawn first, at the bottom, then newer ones on top.
  34. Events are sent in the inverse order so that the top-most widget gets
  35. a chance to catch the event and stop its propagation.
  36. All widget event callbacks do nothing by default.
  37. */
  38. class Widget
  39. {
  40. public:
  41. /**
  42. Base event data.
  43. @param mod The currently active modifiers.
  44. @param time The timestamp (if any) of the currently-processing event.
  45. */
  46. struct BaseEvent {
  47. Modifier mod;
  48. uint32_t time;
  49. };
  50. /**
  51. Keyboard event.
  52. @param press True if the key was pressed, false if released.
  53. @param key Unicode point of the key pressed.
  54. @see onKeyboard
  55. */
  56. struct KeyboardEvent : BaseEvent {
  57. bool press;
  58. uint key;
  59. };
  60. /**
  61. Special keyboard event.
  62. @param press True if the key was pressed, false if released.
  63. @param key The key pressed.
  64. @see onSpecial
  65. */
  66. struct SpecialEvent : BaseEvent {
  67. bool press;
  68. Key key;
  69. };
  70. /**
  71. Mouse event.
  72. @param button The button number (1 = left, 2 = middle, 3 = right).
  73. @param press True if the key was pressed, false if released.
  74. @param pos The widget-relative coordinates of the pointer.
  75. @see onMouse
  76. */
  77. struct MouseEvent : BaseEvent {
  78. int button;
  79. bool press;
  80. Point<int> pos;
  81. };
  82. /**
  83. Mouse motion event.
  84. @param pos The widget-relative coordinates of the pointer.
  85. @see onMotion
  86. */
  87. struct MotionEvent : BaseEvent {
  88. Point<int> pos;
  89. };
  90. /**
  91. Mouse scroll event.
  92. @param pos The widget-relative coordinates of the pointer.
  93. @param delta The scroll distance.
  94. @see onScroll
  95. */
  96. struct ScrollEvent : BaseEvent {
  97. Point<int> pos;
  98. Point<float> delta;
  99. };
  100. /**
  101. Resize event.
  102. @param size The new widget size.
  103. @param oldSize The previous size, may be null.
  104. @see onResize
  105. */
  106. struct ResizeEvent {
  107. Size<int> size;
  108. Size<int> oldSize;
  109. };
  110. /**
  111. Constructor.
  112. */
  113. explicit Widget(Window& parent);
  114. /**
  115. Destructor.
  116. */
  117. virtual ~Widget();
  118. /**
  119. Check if this widget is visible within its parent window.
  120. Invisible widgets do not receive events except resize.
  121. */
  122. bool isVisible() const noexcept;
  123. /**
  124. Set widget visible (or not) according to @a yesNo.
  125. */
  126. void setVisible(bool yesNo);
  127. /**
  128. Show widget.
  129. This is the same as calling setVisible(true).
  130. */
  131. void show();
  132. /**
  133. Hide widget.
  134. This is the same as calling setVisible(false).
  135. */
  136. void hide();
  137. /**
  138. Get width.
  139. */
  140. int getWidth() const noexcept;
  141. /**
  142. Get height.
  143. */
  144. int getHeight() const noexcept;
  145. /**
  146. Get size.
  147. */
  148. const Size<int>& getSize() const noexcept;
  149. /**
  150. Set width.
  151. */
  152. virtual void setWidth(int width) noexcept;
  153. /**
  154. Set height.
  155. */
  156. virtual void setHeight(int height) noexcept;
  157. /**
  158. Set size using @a width and @a height values.
  159. */
  160. virtual void setSize(int width, int height) noexcept;
  161. /**
  162. Set size.
  163. */
  164. virtual void setSize(const Size<int>& size) noexcept;
  165. /**
  166. Get absolute X.
  167. */
  168. int getAbsoluteX() const noexcept;
  169. /**
  170. Get absolute Y.
  171. */
  172. int getAbsoluteY() const noexcept;
  173. /**
  174. Get absolute position.
  175. */
  176. const Point<int>& getAbsolutePos() const noexcept;
  177. /**
  178. Set absolute X.
  179. */
  180. void setAbsoluteX(int x) noexcept;
  181. /**
  182. Set absolute Y.
  183. */
  184. void setAbsoluteY(int y) noexcept;
  185. /**
  186. Set absolute position using @a x and @a y values.
  187. */
  188. void setAbsolutePos(int x, int y) noexcept;
  189. /**
  190. Set absolute position.
  191. */
  192. void setAbsolutePos(const Point<int>& pos) noexcept;
  193. /**
  194. Get this widget's window application.
  195. Same as calling getParentWindow().getApp().
  196. */
  197. App& getParentApp() const noexcept;
  198. /**
  199. Get parent window, as passed in the constructor.
  200. */
  201. Window& getParentWindow() const noexcept;
  202. /**
  203. Check if this widget contains the point defined by @a X and @a Y.
  204. */
  205. bool contains(int x, int y) const noexcept;
  206. /**
  207. Check if this widget contains the point @a pos.
  208. */
  209. bool contains(const Point<int>& pos) const noexcept;
  210. /**
  211. Tell this widget's window to repaint itself.
  212. */
  213. void repaint() noexcept;
  214. protected:
  215. /**
  216. A function called to draw the view contents with OpenGL.
  217. */
  218. virtual void onDisplay() = 0;
  219. /**
  220. A function called when a key is pressed or released.
  221. @return True to stop event propagation, false otherwise.
  222. */
  223. virtual bool onKeyboard(const KeyboardEvent&);
  224. /**
  225. A function called when a special key is pressed or released.
  226. @return True to stop event propagation, false otherwise.
  227. */
  228. virtual bool onSpecial(const SpecialEvent&);
  229. /**
  230. A function called when a mouse button is pressed or released.
  231. @return True to stop event propagation, false otherwise.
  232. */
  233. virtual bool onMouse(const MouseEvent&);
  234. /**
  235. A function called when the pointer moves.
  236. @return True to stop event propagation, false otherwise.
  237. */
  238. virtual bool onMotion(const MotionEvent&);
  239. /**
  240. A function called on scrolling (e.g. mouse wheel or track pad).
  241. @return True to stop event propagation, false otherwise.
  242. */
  243. virtual bool onScroll(const ScrollEvent&);
  244. /**
  245. A function called when the widget is resized.
  246. */
  247. virtual void onResize(const ResizeEvent&);
  248. /**
  249. Tell the parent window this widget needs the full viewport.
  250. When enabled, the local widget coordinates are ignored.
  251. @note: This is an internal function;
  252. You do not need it under normal circumstances.
  253. */
  254. void setNeedsFullViewport(bool yesNo) noexcept;
  255. /**
  256. Tell the parent window this widget needs scaling.
  257. When enabled, the widget viewport is scaled to match width&height.
  258. @note: This is an internal function;
  259. You do not need it under normal circumstances.
  260. */
  261. void setNeedsScaling(bool yesNo) noexcept;
  262. private:
  263. Window& fParent;
  264. bool fNeedsFullViewport;
  265. bool fNeedsScaling;
  266. bool fVisible;
  267. Rectangle<int> fArea;
  268. friend class CairoWidget;
  269. friend class Window;
  270. friend class StandaloneWindow;
  271. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  272. };
  273. // -----------------------------------------------------------------------
  274. END_NAMESPACE_DGL
  275. #endif // DGL_WIDGET_HPP_INCLUDED