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.

317 lines
9.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_WINDOW_PRIVATE_DATA_HPP_INCLUDED
  17. #define DGL_WINDOW_PRIVATE_DATA_HPP_INCLUDED
  18. #include "../Window.hpp"
  19. #include "../Widget.hpp"
  20. #include "ApplicationPrivateData.hpp"
  21. #include "pugl.hpp"
  22. #include <list>
  23. START_NAMESPACE_DGL
  24. class TopLevelWidget;
  25. // -----------------------------------------------------------------------
  26. struct Window::PrivateData : IdleCallback {
  27. /** Reference to the DGL Application class this (private data) window associates with. */
  28. Application& app;
  29. /** Direct access to the DGL Application private data where we registers ourselves in. */
  30. Application::PrivateData* const appData;
  31. /** Pointer to the the DGL Window class that this private data belongs to. */
  32. Window* const self;
  33. /** Pugl view instance. */
  34. PuglView* view;
  35. /** Pugl view instance of the transient parent window. */
  36. PuglView* const transientParentView;
  37. /** Reserved space for graphics context. */
  38. mutable uint8_t graphicsContext[sizeof(void*)];
  39. /** The top-level widgets associated with this Window. */
  40. std::list<TopLevelWidget*> topLevelWidgets;
  41. /** Whether this Window is closed (not visible or counted in the Application it is tied to).
  42. Defaults to true unless embed (embed windows are never closed). */
  43. bool isClosed;
  44. /** Whether this Window is currently visible/mapped. Defaults to false. */
  45. bool isVisible;
  46. /** Whether this Window is embed into another (usually not DGL-controlled) Window. */
  47. const bool isEmbed;
  48. /** Scale factor to report to widgets on request, purely informational. */
  49. double scaleFactor;
  50. /** Automatic scaling to apply on widgets, implemented internally. */
  51. bool autoScaling;
  52. double autoScaleFactor;
  53. /** Pugl geometry constraints access. */
  54. uint minWidth, minHeight;
  55. bool keepAspectRatio;
  56. /** Whether to ignore idle callback requests, useful for temporary windows. */
  57. bool ignoreIdleCallbacks;
  58. /** Render to a picture file when non-null, automatically free+unset after saving. */
  59. char* filenameToRenderInto;
  60. #ifdef DISTRHO_OS_WINDOWS
  61. /** Selected file for openFileBrowser on windows, stored for fake async operation. */
  62. const char* win32SelectedFile;
  63. #endif
  64. /** Modal window setup. */
  65. struct Modal {
  66. PrivateData* parent; // parent of this window (so we can become modal)
  67. PrivateData* child; // child window to give focus to when modal mode is enabled
  68. bool enabled; // wherever modal mode is enabled (only possible if parent != null)
  69. /** Constructor for a non-modal window. */
  70. Modal() noexcept
  71. : parent(nullptr),
  72. child(nullptr),
  73. enabled(false) {}
  74. /** Constructor for a modal window (with a parent). */
  75. Modal(PrivateData* const p) noexcept
  76. : parent(p),
  77. child(nullptr),
  78. enabled(false) {}
  79. /** Destructor. */
  80. ~Modal() noexcept
  81. {
  82. DISTRHO_SAFE_ASSERT(! enabled);
  83. }
  84. DISTRHO_DECLARE_NON_COPYABLE(Modal)
  85. DISTRHO_PREVENT_HEAP_ALLOCATION
  86. } modal;
  87. /** Constructor for a regular, standalone window. */
  88. explicit PrivateData(Application& app, Window* self);
  89. /** Constructor for a modal window. */
  90. explicit PrivateData(Application& app, Window* self, PrivateData* ppData);
  91. /** Constructor for an embed Window, with a few extra hints from the host side. */
  92. explicit PrivateData(Application& app, Window* self, uintptr_t parentWindowHandle, double scaling, bool resizable);
  93. /** Constructor for an embed Window, with a few extra hints from the host side. */
  94. explicit PrivateData(Application& app, Window* self, uintptr_t parentWindowHandle,
  95. uint width, uint height, double scaling, bool resizable);
  96. /** Destructor. */
  97. ~PrivateData() override;
  98. /** Helper initialization function called at the end of all this class constructors. */
  99. void initPre(uint width, uint height, bool resizable);
  100. /** Helper initialization function called on the Window constructor after we are done. */
  101. bool initPost();
  102. /** Hide window and notify application of a window close event.
  103. * Does nothing if window is embed (that is, not standalone).
  104. * The application event-loop will stop when all windows have been closed.
  105. *
  106. * @note It is possible to hide the window while not stopping the event-loop.
  107. * A closed window is always hidden, but the reverse is not always true.
  108. */
  109. void close();
  110. void show();
  111. void hide();
  112. void focus();
  113. void setResizable(bool resizable);
  114. const GraphicsContext& getGraphicsContext() const noexcept;
  115. // idle callback stuff
  116. void idleCallback() override;
  117. bool addIdleCallback(IdleCallback* callback, uint timerFrequencyInMs);
  118. bool removeIdleCallback(IdleCallback* callback);
  119. #ifndef DGL_FILE_BROWSER_DISABLED
  120. // file handling
  121. bool openFileBrowser(const Window::FileBrowserOptions& options);
  122. # ifdef DISTRHO_OS_MAC
  123. static void openPanelCallback(PuglView* view, const char* path);
  124. # endif
  125. #endif
  126. static void renderToPicture(const char* filename, const GraphicsContext& context, uint width, uint height);
  127. // modal handling
  128. void startModal();
  129. void stopModal();
  130. void runAsModal(bool blockWait);
  131. // pugl events
  132. void onPuglConfigure(double width, double height);
  133. void onPuglExpose();
  134. void onPuglClose();
  135. void onPuglFocus(bool focus, CrossingMode mode);
  136. void onPuglKey(const Widget::KeyboardEvent& ev);
  137. void onPuglText(const Widget::CharacterInputEvent& ev);
  138. void onPuglMouse(const Widget::MouseEvent& ev);
  139. void onPuglMotion(const Widget::MotionEvent& ev);
  140. void onPuglScroll(const Widget::ScrollEvent& ev);
  141. // Pugl event handling entry point
  142. static PuglStatus puglEventCallback(PuglView* view, const PuglEvent* event);
  143. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PrivateData)
  144. };
  145. // -----------------------------------------------------------------------
  146. END_NAMESPACE_DGL
  147. #if 0
  148. // #if defined(DISTRHO_OS_HAIKU)
  149. // BApplication* bApplication;
  150. // BView* bView;
  151. // BWindow* bWindow;
  152. #if defined(DISTRHO_OS_MAC)
  153. // NSView<PuglGenericView>* mView;
  154. // id mWindow;
  155. // id mParentWindow;
  156. # ifndef DGL_FILE_BROWSER_DISABLED
  157. NSOpenPanel* fOpenFilePanel;
  158. id fFilePanelDelegate;
  159. # endif
  160. #elif defined(DISTRHO_OS_WINDOWS)
  161. // HWND hwnd;
  162. // HWND hwndParent;
  163. # ifndef DGL_FILE_BROWSER_DISABLED
  164. String fSelectedFile;
  165. # endif
  166. #endif
  167. #endif
  168. #if 0
  169. // -----------------------------------------------------------------------
  170. // Window Private
  171. struct Window::PrivateData {
  172. // -------------------------------------------------------------------
  173. bool handlePluginSpecial(const bool press, const Key key)
  174. {
  175. DBGp("PUGL: handlePluginSpecial : %i %i\n", press, key);
  176. if (fModal.childFocus != nullptr)
  177. {
  178. fModal.childFocus->focus();
  179. return true;
  180. }
  181. int mods = 0x0;
  182. switch (key)
  183. {
  184. case kKeyShift:
  185. mods |= kModifierShift;
  186. break;
  187. case kKeyControl:
  188. mods |= kModifierControl;
  189. break;
  190. case kKeyAlt:
  191. mods |= kModifierAlt;
  192. break;
  193. default:
  194. break;
  195. }
  196. if (mods != 0x0)
  197. {
  198. if (press)
  199. fView->mods |= mods;
  200. else
  201. fView->mods &= ~(mods);
  202. }
  203. Widget::SpecialEvent ev;
  204. ev.press = press;
  205. ev.key = key;
  206. ev.mod = static_cast<Modifier>(fView->mods);
  207. ev.time = 0;
  208. FOR_EACH_WIDGET_INV(rit)
  209. {
  210. Widget* const widget(*rit);
  211. if (widget->isVisible() && widget->onSpecial(ev))
  212. return true;
  213. }
  214. return false;
  215. }
  216. #if defined(DISTRHO_OS_MAC) && !defined(DGL_FILE_BROWSER_DISABLED)
  217. static void openPanelDidEnd(NSOpenPanel* panel, int returnCode, void *userData)
  218. {
  219. PrivateData* pData = (PrivateData*)userData;
  220. if (returnCode == NSOKButton)
  221. {
  222. NSArray* urls = [panel URLs];
  223. NSURL* fileUrl = nullptr;
  224. for (NSUInteger i = 0, n = [urls count]; i < n && !fileUrl; ++i)
  225. {
  226. NSURL* url = (NSURL*)[urls objectAtIndex:i];
  227. if ([url isFileURL])
  228. fileUrl = url;
  229. }
  230. if (fileUrl)
  231. {
  232. PuglView* view = pData->fView;
  233. if (view->fileSelectedFunc)
  234. {
  235. const char* fileName = [fileUrl.path UTF8String];
  236. view->fileSelectedFunc(view, fileName);
  237. }
  238. }
  239. }
  240. [pData->fOpenFilePanel release];
  241. pData->fOpenFilePanel = nullptr;
  242. }
  243. #endif
  244. // -------------------------------------------------------------------
  245. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PrivateData)
  246. };
  247. #endif
  248. #endif // DGL_WINDOW_PRIVATE_DATA_HPP_INCLUDED