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.

242 lines
6.7KB

  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 "Events.hpp"
  19. START_NAMESPACE_DGL
  20. // --------------------------------------------------------------------------------------------------------------------
  21. // Forward class names
  22. class Application;
  23. class SubWidget;
  24. class TopLevelWidget;
  25. class Window;
  26. using namespace Events;
  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. /**
  44. Private constructor, reserved for TopLevelWidget class.
  45. */
  46. explicit Widget(TopLevelWidget* topLevelWidget);
  47. /**
  48. Private constructor, reserved for SubWidget class.
  49. */
  50. explicit Widget(Widget* widgetToGroupTo);
  51. public:
  52. /**
  53. Destructor.
  54. */
  55. virtual ~Widget();
  56. /**
  57. Check if this widget is visible within its parent window.
  58. Invisible widgets do not receive events except resize.
  59. */
  60. bool isVisible() const noexcept;
  61. /**
  62. Set widget visible (or not) according to @a visible.
  63. */
  64. void setVisible(bool visible);
  65. /**
  66. Show widget.
  67. This is the same as calling setVisible(true).
  68. */
  69. void show();
  70. /**
  71. Hide widget.
  72. This is the same as calling setVisible(false).
  73. */
  74. void hide();
  75. /**
  76. Get width.
  77. */
  78. uint getWidth() const noexcept;
  79. /**
  80. Get height.
  81. */
  82. uint getHeight() const noexcept;
  83. /**
  84. Get size.
  85. */
  86. const Size<uint> getSize() const noexcept;
  87. /**
  88. Set width.
  89. */
  90. void setWidth(uint width) noexcept;
  91. /**
  92. Set height.
  93. */
  94. void setHeight(uint height) noexcept;
  95. /**
  96. Set size using @a width and @a height values.
  97. */
  98. void setSize(uint width, uint height) noexcept;
  99. /**
  100. Set size.
  101. */
  102. void setSize(const Size<uint>& size) noexcept;
  103. /**
  104. Get the Id associated with this widget.
  105. @see setId
  106. */
  107. uint getId() const noexcept;
  108. /**
  109. Set an Id to be associated with this widget.
  110. @see getId
  111. */
  112. void setId(uint id) noexcept;
  113. /**
  114. Get the application associated with this widget's window.
  115. This is the same as calling `getTopLevelWidget()->getApp()`.
  116. */
  117. Application& getApp() const noexcept;
  118. /**
  119. Get the window associated with this widget.
  120. This is the same as calling `getTopLevelWidget()->getWindow()`.
  121. */
  122. Window& getWindow() const noexcept;
  123. /**
  124. Get the graphics context associated with this widget's window.
  125. GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable,
  126. for example GraphicsContext.
  127. @see CairoSubWidget, CairoTopLevelWidget
  128. */
  129. const GraphicsContext& getGraphicsContext() const noexcept;
  130. /**
  131. Get top-level widget, as passed directly in the constructor
  132. or going up the chain of group widgets until it finds the top-level one.
  133. */
  134. TopLevelWidget* getTopLevelWidget() const noexcept;
  135. /**
  136. Request repaint of this widget's area to the window this widget belongs to.
  137. On the raw Widget class this function does nothing.
  138. */
  139. virtual void repaint() noexcept;
  140. DISTRHO_DEPRECATED_BY("getApp()")
  141. Application& getParentApp() const noexcept { return getApp(); }
  142. DISTRHO_DEPRECATED_BY("getWindow()")
  143. Window& getParentWindow() const noexcept { return getWindow(); }
  144. protected:
  145. /**
  146. A function called to draw the widget contents.
  147. */
  148. virtual void onDisplay() = 0;
  149. /**
  150. A function called when a key is pressed or released.
  151. @return True to stop event propagation, false otherwise.
  152. */
  153. virtual bool onKeyboard(const KeyboardEvent&);
  154. /**
  155. A function called when a special key is pressed or released.
  156. @return True to stop event propagation, false otherwise.
  157. */
  158. virtual bool onSpecial(const SpecialEvent&);
  159. /**
  160. A function called when an UTF-8 character is received.
  161. @return True to stop event propagation, false otherwise.
  162. */
  163. virtual bool onCharacterInput(const CharacterInputEvent&);
  164. /**
  165. A function called when a mouse button is pressed or released.
  166. @return True to stop event propagation, false otherwise.
  167. */
  168. virtual bool onMouse(const MouseEvent&);
  169. /**
  170. A function called when the pointer moves.
  171. @return True to stop event propagation, false otherwise.
  172. */
  173. virtual bool onMotion(const MotionEvent&);
  174. /**
  175. A function called on scrolling (e.g. mouse wheel or track pad).
  176. @return True to stop event propagation, false otherwise.
  177. */
  178. virtual bool onScroll(const ScrollEvent&);
  179. /**
  180. A function called when the widget is resized.
  181. */
  182. virtual void onResize(const ResizeEvent&);
  183. private:
  184. struct PrivateData;
  185. PrivateData* const pData;
  186. friend class SubWidget;
  187. friend class TopLevelWidget;
  188. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  189. };
  190. // --------------------------------------------------------------------------------------------------------------------
  191. END_NAMESPACE_DGL
  192. #endif // DGL_WIDGET_HPP_INCLUDED