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.

375 lines
10KB

  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_WINDOW_HPP_INCLUDED
  17. #define DGL_WINDOW_HPP_INCLUDED
  18. #include "Geometry.hpp"
  19. START_NAMESPACE_DGL
  20. class Application;
  21. class TopLevelWidget;
  22. // -----------------------------------------------------------------------
  23. /**
  24. DGL Window class.
  25. This is the where all OS-related events initially happen, before being propagated to any widgets.
  26. A Window MUST have an Application instance tied to it.
  27. It is not possible to swap Application instances from within the lifetime of a Window.
  28. But it is possible to completely change the Widgets that a Window contains during its lifetime.
  29. Typically the event handling functions as following:
  30. Application -> Window -> Top-Level-Widget -> SubWidgets
  31. Please note that, unlike many other graphical toolkits out there,
  32. DGL makes a clear distinction between a Window and a Widget.
  33. You cannot directly draw in a Window, you need to create a Widget for that.
  34. Also, a Window MUST have a single top-level Widget.
  35. The Window will take care of global screen positioning and resizing, everything else is sent for widgets to handle.
  36. ...
  37. */
  38. class Window
  39. {
  40. public:
  41. /**
  42. Constructor for a regular, standalone window.
  43. */
  44. explicit Window(Application& app);
  45. /**
  46. Constructor for an embed Window without known size,
  47. typically used in modules or plugins that run inside another host.
  48. */
  49. explicit Window(Application& app,
  50. uintptr_t parentWindowHandle,
  51. double scaleFactor,
  52. bool resizable);
  53. /**
  54. Constructor for an embed Window with known size,
  55. typically used in modules or plugins that run inside another host.
  56. */
  57. explicit Window(Application& app,
  58. uintptr_t parentWindowHandle,
  59. uint width,
  60. uint height,
  61. double scaleFactor,
  62. bool resizable);
  63. /**
  64. Destructor.
  65. */
  66. virtual ~Window();
  67. /**
  68. Whether this Window is embed into another (usually not DGL-controlled) Window.
  69. */
  70. bool isEmbed() const noexcept;
  71. /**
  72. Check if this window is visible / mapped.
  73. Invisible windows do not receive events except resize.
  74. @see setVisible(bool)
  75. */
  76. bool isVisible() const noexcept;
  77. /**
  78. Set windows visible (or not) according to @a visible.
  79. Only valid for standalones, embed windows are always visible.
  80. @see isVisible(), hide(), show()
  81. */
  82. void setVisible(bool visible);
  83. /**
  84. Show window.
  85. This is the same as calling setVisible(true).
  86. @see isVisible(), setVisible(bool)
  87. */
  88. void show();
  89. /**
  90. Hide window.
  91. This is the same as calling setVisible(false).
  92. @see isVisible(), setVisible(bool)
  93. */
  94. void hide();
  95. /**
  96. Hide window and notify application of a window close event.
  97. The application event-loop will stop when all windows have been closed.
  98. For standalone windows only, has no effect if window is embed.
  99. @see isEmbed()
  100. @note It is possible to hide the window while not stopping the event-loop.
  101. A closed window is always hidden, but the reverse is not always true.
  102. */
  103. void close();
  104. bool isResizable() const noexcept;
  105. void setResizable(bool resizable);
  106. /**
  107. Get width.
  108. */
  109. uint getWidth() const noexcept;
  110. /**
  111. Get height.
  112. */
  113. uint getHeight() const noexcept;
  114. /**
  115. Get size.
  116. */
  117. Size<uint> getSize() const noexcept;
  118. /**
  119. Set width.
  120. */
  121. void setWidth(uint width);
  122. /**
  123. Set height.
  124. */
  125. void setHeight(uint height);
  126. /**
  127. Set size using @a width and @a height values.
  128. */
  129. void setSize(uint width, uint height);
  130. /**
  131. Set size.
  132. */
  133. void setSize(const Size<uint>& size);
  134. /**
  135. Get the title of the window previously set with setTitle().
  136. */
  137. const char* getTitle() const noexcept;
  138. /**
  139. Set the title of the window, typically displayed in the title bar or in window switchers.
  140. This only makes sense for non-embedded windows.
  141. */
  142. void setTitle(const char* title);
  143. /**
  144. Check if key repeat events are ignored.
  145. */
  146. bool isIgnoringKeyRepeat() const noexcept;
  147. /**
  148. Set to ignore (or not) key repeat events according to @a ignore.
  149. */
  150. void setIgnoringKeyRepeat(bool ignore) noexcept;
  151. /**
  152. Get the application associated with this window.
  153. */
  154. Application& getApp() const noexcept;
  155. /**
  156. Get the "native" window handle.
  157. Returned value depends on the platform:
  158. - HaikuOS: This is a pointer to a `BView`.
  159. - MacOS: This is a pointer to an `NSView*`.
  160. - Windows: This is a `HWND`.
  161. - Everything else: This is an [X11] `Window`.
  162. */
  163. uintptr_t getNativeWindowHandle() const noexcept;
  164. /**
  165. Get the scale factor requested for this window.
  166. This is purely informational, and up to developers to choose what to do with it.
  167. If you do not want to deal with this yourself,
  168. consider using setGeometryConstraints() where you can specify to automatically scale the window contents.
  169. @see setGeometryConstraints
  170. */
  171. double getScaleFactor() const noexcept;
  172. /**
  173. Grab the keyboard input focus.
  174. */
  175. void focus();
  176. /**
  177. Request repaint of this window, for the entire area.
  178. */
  179. void repaint() noexcept;
  180. /**
  181. Request partial repaint of this window, with bounds according to @a rect.
  182. */
  183. void repaint(const Rectangle<uint>& rect) noexcept;
  184. /**
  185. Set geometry constraints for the Window when resized by the user, and optionally scale contents automatically.
  186. */
  187. void setGeometryConstraints(uint minimumWidth,
  188. uint minimumHeight,
  189. bool keepAspectRatio = false,
  190. bool automaticallyScale = false);
  191. /*
  192. void setTransientWinId(uintptr_t winId);
  193. */
  194. // TODO deprecated
  195. inline bool getIgnoringKeyRepeat() const noexcept { return isIgnoringKeyRepeat(); }
  196. inline double getScaling() const noexcept { return getScaling(); }
  197. protected:
  198. /**
  199. A function called when the window is resized.
  200. If there is a top-level widget associated with this window, its size will be set right after this function.
  201. */
  202. virtual void onReshape(uint width, uint height);
  203. /**
  204. A function called when the window is attempted to be closed.
  205. Returning true closes the window, which is the default behaviour.
  206. Override this method and return false to prevent the window from being closed by the user.
  207. */
  208. virtual bool onClose();
  209. private:
  210. struct PrivateData;
  211. PrivateData* const pData;
  212. friend class Application;
  213. friend class TopLevelWidget;
  214. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Window);
  215. };
  216. // -----------------------------------------------------------------------
  217. END_NAMESPACE_DGL
  218. /* TODO
  219. * add focusEvent with CrossingMode arg
  220. * add eventcrossing/enter-leave event
  221. */
  222. // class StandaloneWindow;
  223. // class Widget;
  224. // #ifdef DISTRHO_DEFINES_H_INCLUDED
  225. // START_NAMESPACE_DISTRHO
  226. // class UI;
  227. // class UIExporter;
  228. // END_NAMESPACE_DISTRHO
  229. // #endif
  230. #if 0
  231. #ifndef DGL_FILE_BROWSER_DISABLED
  232. /**
  233. File browser options.
  234. */
  235. struct FileBrowserOptions {
  236. const char* startDir;
  237. const char* title;
  238. uint width;
  239. uint height;
  240. /**
  241. File browser buttons.
  242. 0 means hidden.
  243. 1 means visible and unchecked.
  244. 2 means visible and checked.
  245. */
  246. struct Buttons {
  247. uint listAllFiles;
  248. uint showHidden;
  249. uint showPlaces;
  250. /** Constuctor for default values */
  251. Buttons()
  252. : listAllFiles(2),
  253. showHidden(1),
  254. showPlaces(1) {}
  255. } buttons;
  256. /** Constuctor for default values */
  257. FileBrowserOptions()
  258. : startDir(nullptr),
  259. title(nullptr),
  260. width(0),
  261. height(0),
  262. buttons() {}
  263. };
  264. #endif // DGL_FILE_BROWSER_DISABLED
  265. static Window& withTransientParentWindow(Window& transientParentWindow);
  266. void exec(bool lockWait = false);
  267. const GraphicsContext& getGraphicsContext() const noexcept;
  268. void addIdleCallback(IdleCallback* const callback);
  269. void removeIdleCallback(IdleCallback* const callback);
  270. #ifndef DGL_FILE_BROWSER_DISABLED
  271. bool openFileBrowser(const FileBrowserOptions& options);
  272. #endif
  273. protected:
  274. #ifndef DGL_FILE_BROWSER_DISABLED
  275. virtual void fileBrowserSelected(const char* filename);
  276. #endif
  277. // internal
  278. void _setAutoScaling(double scaling) noexcept;
  279. virtual void _addWidget(Widget* const widget);
  280. virtual void _removeWidget(Widget* const widget);
  281. void _idle();
  282. bool handlePluginKeyboard(const bool press, const uint key);
  283. bool handlePluginSpecial(const bool press, const Key key);
  284. // friend class Widget;
  285. // friend class StandaloneWindow;
  286. // #ifdef DISTRHO_DEFINES_H_INCLUDED
  287. // friend class DISTRHO_NAMESPACE::UI;
  288. // friend class DISTRHO_NAMESPACE::UIExporter;
  289. // #endif
  290. // Prevent copies
  291. // #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  292. // Window& operator=(Window&) = delete;
  293. // Window& operator=(const Window&) = delete;
  294. // #else
  295. // Window& operator=(Window&);
  296. // Window& operator=(const Window&);
  297. // #endif
  298. #endif
  299. // -----------------------------------------------------------------------
  300. #endif // DGL_WINDOW_HPP_INCLUDED