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.

238 lines
5.8KB

  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. // -----------------------------------------------------------------------
  20. // Forward class names
  21. // #ifdef DISTRHO_DEFINES_H_INCLUDED
  22. // START_NAMESPACE_DISTRHO
  23. // class UI;
  24. // END_NAMESPACE_DISTRHO
  25. // #endif
  26. START_NAMESPACE_DGL
  27. // class Application;
  28. // class NanoWidget;
  29. // class Window;
  30. // class StandaloneWindow;
  31. // class SubWidget;
  32. class TopLevelWidget;
  33. using namespace Events;
  34. // -----------------------------------------------------------------------
  35. /**
  36. Base DGL Widget class.
  37. This is the base Widget class, from which all widgets are built.
  38. All widgets have a parent Window where they'll be drawn.
  39. This parent is never changed during the widget lifetime.
  40. Widgets receive events in relative coordinates.
  41. (0, 0) means its top-left position.
  42. Windows paint widgets in the order they are constructed.
  43. Early widgets are drawn first, at the bottom, then newer ones on top.
  44. Events are sent in the inverse order so that the top-most widget gets
  45. a chance to catch the event and stop its propagation.
  46. All widget event callbacks do nothing by default.
  47. */
  48. class Widget
  49. {
  50. /**
  51. Constructor, reserved for DGL ..
  52. use TopLevelWidget or SubWidget instead
  53. */
  54. explicit Widget(TopLevelWidget& topLevelWidget);
  55. public:
  56. /**
  57. Destructor.
  58. */
  59. virtual ~Widget();
  60. /**
  61. Check if this widget is visible within its parent window.
  62. Invisible widgets do not receive events except resize.
  63. */
  64. bool isVisible() const noexcept;
  65. /**
  66. Set widget visible (or not) according to @a yesNo.
  67. */
  68. void setVisible(bool yesNo);
  69. /**
  70. Show widget.
  71. This is the same as calling setVisible(true).
  72. */
  73. void show();
  74. /**
  75. Hide widget.
  76. This is the same as calling setVisible(false).
  77. */
  78. void hide();
  79. /**
  80. Get width.
  81. */
  82. uint getWidth() const noexcept;
  83. /**
  84. Get height.
  85. */
  86. uint getHeight() const noexcept;
  87. /**
  88. Get size.
  89. */
  90. const Size<uint>& getSize() const noexcept;
  91. /**
  92. Set width.
  93. */
  94. void setWidth(uint width) noexcept;
  95. /**
  96. Set height.
  97. */
  98. void setHeight(uint height) noexcept;
  99. /**
  100. Set size using @a width and @a height values.
  101. */
  102. void setSize(uint width, uint height) noexcept;
  103. /**
  104. Set size.
  105. */
  106. void setSize(const Size<uint>& size) noexcept;
  107. /**
  108. Get top-level widget, as passed in the constructor.
  109. */
  110. TopLevelWidget& getTopLevelWidget() const noexcept;
  111. /**
  112. Tell this widget's window to repaint itself.
  113. FIXME better description, partial redraw
  114. */
  115. void repaint() noexcept;
  116. /**
  117. Get the Id associated with this widget.
  118. @see setId
  119. */
  120. uint getId() const noexcept;
  121. /**
  122. Set an Id to be associated with this widget.
  123. @see getId
  124. */
  125. void setId(uint id) noexcept;
  126. protected:
  127. /**
  128. A function called to draw the view contents with OpenGL.
  129. */
  130. virtual void onDisplay() = 0;
  131. /**
  132. A function called when a key is pressed or released.
  133. @return True to stop event propagation, false otherwise.
  134. */
  135. virtual bool onKeyboard(const KeyboardEvent&);
  136. /**
  137. A function called when a special key is pressed or released.
  138. @return True to stop event propagation, false otherwise.
  139. */
  140. virtual bool onSpecial(const SpecialEvent&);
  141. /**
  142. A function called when an UTF-8 character is received.
  143. @return True to stop event propagation, false otherwise.
  144. */
  145. virtual bool onCharacterInput(const CharacterInputEvent&);
  146. /**
  147. A function called when a mouse button is pressed or released.
  148. @return True to stop event propagation, false otherwise.
  149. */
  150. virtual bool onMouse(const MouseEvent&);
  151. /**
  152. A function called when the pointer moves.
  153. @return True to stop event propagation, false otherwise.
  154. */
  155. virtual bool onMotion(const MotionEvent&);
  156. /**
  157. A function called on scrolling (e.g. mouse wheel or track pad).
  158. @return True to stop event propagation, false otherwise.
  159. */
  160. virtual bool onScroll(const ScrollEvent&);
  161. /**
  162. A function called when the widget is resized.
  163. */
  164. virtual void onResize(const ResizeEvent&);
  165. private:
  166. struct PrivateData;
  167. PrivateData* const pData;
  168. // friend class NanoWidget;
  169. // friend class Window;
  170. // friend class StandaloneWindow;
  171. friend class TopLevelWidget;
  172. // #ifdef DISTRHO_DEFINES_H_INCLUDED
  173. // friend class DISTRHO_NAMESPACE::UI;
  174. // #endif
  175. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
  176. };
  177. #if 0
  178. // TODO: should we remove this?
  179. /**
  180. Get this widget's window application.
  181. Same as calling getParentWindow().getApp().
  182. */
  183. Application& getParentApp() const noexcept;
  184. /**
  185. Get parent window, as passed in the constructor.
  186. */
  187. Window& getParentWindow() const noexcept;
  188. #endif
  189. // -----------------------------------------------------------------------
  190. END_NAMESPACE_DGL
  191. #endif // DGL_WIDGET_HPP_INCLUDED