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.

385 lines
12KB

  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 a modal window, by having another window as its parent.
  47. The Application instance must be the same between the 2 windows.
  48. */
  49. explicit Window(Application& app, Window& parent);
  50. /**
  51. Constructor for an embed Window without known size,
  52. typically used in modules or plugins that run inside another host.
  53. */
  54. explicit Window(Application& app,
  55. uintptr_t parentWindowHandle,
  56. double scaleFactor,
  57. bool resizable);
  58. /**
  59. Constructor for an embed Window with known size,
  60. typically used in modules or plugins that run inside another host.
  61. */
  62. explicit Window(Application& app,
  63. uintptr_t parentWindowHandle,
  64. uint width,
  65. uint height,
  66. double scaleFactor,
  67. bool resizable);
  68. /**
  69. Destructor.
  70. */
  71. virtual ~Window();
  72. /**
  73. Whether this Window is embed into another (usually not DGL-controlled) Window.
  74. */
  75. bool isEmbed() const noexcept;
  76. /**
  77. Check if this window is visible / mapped.
  78. Invisible windows do not receive events except resize.
  79. @see setVisible(bool)
  80. */
  81. bool isVisible() const noexcept;
  82. /**
  83. Set windows visible (or not) according to @a visible.
  84. Only valid for standalones, embed windows are always visible.
  85. @see isVisible(), hide(), show()
  86. */
  87. void setVisible(bool visible);
  88. /**
  89. Show window.
  90. This is the same as calling setVisible(true).
  91. @see isVisible(), setVisible(bool)
  92. */
  93. void show();
  94. /**
  95. Hide window.
  96. This is the same as calling setVisible(false).
  97. @see isVisible(), setVisible(bool)
  98. */
  99. void hide();
  100. /**
  101. Hide window and notify application of a window close event.
  102. The application event-loop will stop when all windows have been closed.
  103. For standalone windows only, has no effect if window is embed.
  104. @see isEmbed()
  105. @note It is possible to hide the window while not stopping the event-loop.
  106. A closed window is always hidden, but the reverse is not always true.
  107. */
  108. void close();
  109. bool isResizable() const noexcept;
  110. void setResizable(bool resizable);
  111. /**
  112. Get width.
  113. */
  114. uint getWidth() const noexcept;
  115. /**
  116. Get height.
  117. */
  118. uint getHeight() const noexcept;
  119. /**
  120. Get size.
  121. */
  122. Size<uint> getSize() const noexcept;
  123. /**
  124. Set width.
  125. */
  126. void setWidth(uint width);
  127. /**
  128. Set height.
  129. */
  130. void setHeight(uint height);
  131. /**
  132. Set size using @a width and @a height values.
  133. */
  134. void setSize(uint width, uint height);
  135. /**
  136. Set size.
  137. */
  138. void setSize(const Size<uint>& size);
  139. /**
  140. Get the title of the window previously set with setTitle().
  141. */
  142. const char* getTitle() const noexcept;
  143. /**
  144. Set the title of the window, typically displayed in the title bar or in window switchers.
  145. This only makes sense for non-embedded windows.
  146. */
  147. void setTitle(const char* title);
  148. /**
  149. Check if key repeat events are ignored.
  150. */
  151. bool isIgnoringKeyRepeat() const noexcept;
  152. /**
  153. Set to ignore (or not) key repeat events according to @a ignore.
  154. */
  155. void setIgnoringKeyRepeat(bool ignore) noexcept;
  156. /**
  157. Add a callback function to be triggered on every idle cycle or on a specific timer frequency.
  158. You can add more than one, and remove them at anytime with removeIdleCallback().
  159. This can be used to perform some action at a regular interval with relatively low frequency.
  160. If providing a timer frequency, there are a few things to note:
  161. 1. There is a platform-specific limit to the number of supported timers, and overhead associated with each,
  162. so you should create only a few timers and perform several tasks in one if necessary.
  163. 2. This timer frequency is not guaranteed to have a resolution better than 10ms
  164. (the maximum timer resolution on Windows) and may be rounded up if it is too short.
  165. On X11 and MacOS, a resolution of about 1ms can usually be relied on.
  166. */
  167. bool addIdleCallback(IdleCallback* callback, uint timerFrequencyInMs = 0);
  168. /**
  169. Remove an idle callback previously added via addIdleCallback().
  170. */
  171. bool removeIdleCallback(IdleCallback* callback);
  172. /**
  173. Get the application associated with this window.
  174. */
  175. Application& getApp() const noexcept;
  176. /**
  177. Get the graphics context associated with this window.
  178. GraphicsContext is an empty struct and needs to be casted into a different type in order to be usable,
  179. for example GraphicsContext.
  180. @see CairoSubWidget, CairoTopLevelWidget
  181. */
  182. const GraphicsContext& getGraphicsContext() const noexcept;
  183. /**
  184. Get the "native" window handle.
  185. Returned value depends on the platform:
  186. - HaikuOS: This is a pointer to a `BView`.
  187. - MacOS: This is a pointer to an `NSView*`.
  188. - Windows: This is a `HWND`.
  189. - Everything else: This is an [X11] `Window`.
  190. */
  191. uintptr_t getNativeWindowHandle() const noexcept;
  192. /**
  193. Get the scale factor requested for this window.
  194. This is purely informational, and up to developers to choose what to do with it.
  195. If you do not want to deal with this yourself,
  196. consider using setGeometryConstraints() where you can specify to automatically scale the window contents.
  197. @see setGeometryConstraints
  198. */
  199. double getScaleFactor() const noexcept;
  200. /**
  201. Grab the keyboard input focus.
  202. */
  203. void focus();
  204. /**
  205. Request repaint of this window, for the entire area.
  206. */
  207. void repaint() noexcept;
  208. /**
  209. Request partial repaint of this window, with bounds according to @a rect.
  210. */
  211. void repaint(const Rectangle<uint>& rect) noexcept;
  212. /**
  213. Run this window as a modal, blocking input events from the parent.
  214. Only valid for windows that have been created with another window as parent (as passed in the constructor).
  215. Can optionally block-wait, but such option is only available if the application is running as standalone.
  216. */
  217. void runAsModal(bool blockWait = false);
  218. /**
  219. Set geometry constraints for the Window when resized by the user, and optionally scale contents automatically.
  220. */
  221. void setGeometryConstraints(uint minimumWidth,
  222. uint minimumHeight,
  223. bool keepAspectRatio = false,
  224. bool automaticallyScale = false);
  225. /*
  226. void setTransientWinId(uintptr_t winId);
  227. */
  228. DISTRHO_DEPRECATED_BY("isIgnoringKeyRepeat()")
  229. inline bool getIgnoringKeyRepeat() const noexcept { return isIgnoringKeyRepeat(); }
  230. DISTRHO_DEPRECATED_BY("getScaleFactor()")
  231. inline double getScaling() const noexcept { return getScaleFactor(); }
  232. DISTRHO_DEPRECATED_BY("runAsModal(bool)")
  233. inline void exec(bool blockWait = false) { runAsModal(blockWait); }
  234. protected:
  235. /**
  236. A function called when the window is attempted to be closed.
  237. Returning true closes the window, which is the default behaviour.
  238. Override this method and return false to prevent the window from being closed by the user.
  239. */
  240. virtual bool onClose();
  241. /**
  242. A function called when the window gains or loses the keyboard focus.
  243. The default implementation does nothing.
  244. */
  245. virtual void onFocus(bool focus, CrossingMode mode);
  246. /**
  247. A function called when the window is resized.
  248. If there is a top-level widget associated with this window, its size will be set right after this function.
  249. */
  250. virtual void onReshape(uint width, uint height);
  251. private:
  252. struct PrivateData;
  253. PrivateData* const pData;
  254. friend class Application;
  255. friend class TopLevelWidget;
  256. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Window);
  257. };
  258. // -----------------------------------------------------------------------
  259. END_NAMESPACE_DGL
  260. /* TODO
  261. * add focusEvent with CrossingMode arg
  262. * add eventcrossing/enter-leave event
  263. */
  264. #if 0
  265. #ifndef DGL_FILE_BROWSER_DISABLED
  266. /**
  267. File browser options.
  268. */
  269. struct FileBrowserOptions {
  270. const char* startDir;
  271. const char* title;
  272. uint width;
  273. uint height;
  274. /**
  275. File browser buttons.
  276. 0 means hidden.
  277. 1 means visible and unchecked.
  278. 2 means visible and checked.
  279. */
  280. struct Buttons {
  281. uint listAllFiles;
  282. uint showHidden;
  283. uint showPlaces;
  284. /** Constuctor for default values */
  285. Buttons()
  286. : listAllFiles(2),
  287. showHidden(1),
  288. showPlaces(1) {}
  289. } buttons;
  290. /** Constuctor for default values */
  291. FileBrowserOptions()
  292. : startDir(nullptr),
  293. title(nullptr),
  294. width(0),
  295. height(0),
  296. buttons() {}
  297. };
  298. #endif // DGL_FILE_BROWSER_DISABLED
  299. void addIdleCallback(IdleCallback* const callback);
  300. void removeIdleCallback(IdleCallback* const callback);
  301. #ifndef DGL_FILE_BROWSER_DISABLED
  302. bool openFileBrowser(const FileBrowserOptions& options);
  303. #endif
  304. protected:
  305. #ifndef DGL_FILE_BROWSER_DISABLED
  306. virtual void fileBrowserSelected(const char* filename);
  307. #endif
  308. bool handlePluginKeyboard(const bool press, const uint key);
  309. bool handlePluginSpecial(const bool press, const Key key);
  310. #endif
  311. // -----------------------------------------------------------------------
  312. #endif // DGL_WINDOW_HPP_INCLUDED