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.

313 lines
7.6KB

  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. #include "WindowPrivateData.hpp"
  17. #include "pugl.hpp"
  18. START_NAMESPACE_DGL
  19. // -----------------------------------------------------------------------
  20. // Window
  21. Window::Window(Application& app)
  22. : pData(new PrivateData(app, this)) {}
  23. Window::Window(Application& app, Window& parent)
  24. : pData(new PrivateData(app, this, parent.pData)) {}
  25. Window::Window(Application& app,
  26. const uintptr_t parentWindowHandle,
  27. const double scaleFactor,
  28. const bool resizable)
  29. : pData(new PrivateData(app, this, parentWindowHandle, scaleFactor, resizable)) {}
  30. Window::Window(Application& app,
  31. const uintptr_t parentWindowHandle,
  32. const uint width,
  33. const uint height,
  34. const double scaleFactor,
  35. const bool resizable)
  36. : pData(new PrivateData(app, this, parentWindowHandle, width, height, scaleFactor, resizable)) {}
  37. Window::~Window()
  38. {
  39. delete pData;
  40. }
  41. bool Window::isEmbed() const noexcept
  42. {
  43. return pData->isEmbed;
  44. }
  45. bool Window::isVisible() const noexcept
  46. {
  47. return pData->isVisible;
  48. }
  49. void Window::setVisible(const bool visible)
  50. {
  51. if (visible)
  52. pData->show();
  53. else
  54. pData->hide();
  55. }
  56. void Window::show()
  57. {
  58. pData->show();
  59. }
  60. void Window::hide()
  61. {
  62. pData->hide();
  63. }
  64. void Window::close()
  65. {
  66. pData->close();
  67. }
  68. bool Window::isResizable() const noexcept
  69. {
  70. return puglGetViewHint(pData->view, PUGL_RESIZABLE) == PUGL_TRUE;
  71. }
  72. void Window::setResizable(const bool resizable)
  73. {
  74. pData->setResizable(resizable);
  75. }
  76. uint Window::getWidth() const noexcept
  77. {
  78. const double width = puglGetFrame(pData->view).width;
  79. DISTRHO_SAFE_ASSERT_RETURN(width >= 0.0, 0);
  80. return static_cast<uint>(width + 0.5);
  81. }
  82. uint Window::getHeight() const noexcept
  83. {
  84. const double height = puglGetFrame(pData->view).height;
  85. DISTRHO_SAFE_ASSERT_RETURN(height >= 0.0, 0);
  86. return static_cast<uint>(height + 0.5);
  87. }
  88. Size<uint> Window::getSize() const noexcept
  89. {
  90. const PuglRect rect = puglGetFrame(pData->view);
  91. DISTRHO_SAFE_ASSERT_RETURN(rect.width >= 0.0, Size<uint>());
  92. DISTRHO_SAFE_ASSERT_RETURN(rect.height >= 0.0, Size<uint>());
  93. return Size<uint>(static_cast<uint>(rect.width + 0.5),
  94. static_cast<uint>(rect.height + 0.5));
  95. }
  96. void Window::setWidth(const uint width)
  97. {
  98. setSize(width, getHeight());
  99. }
  100. void Window::setHeight(const uint height)
  101. {
  102. setSize(getWidth(), height);
  103. }
  104. void Window::setSize(const uint width, const uint height)
  105. {
  106. DISTRHO_SAFE_ASSERT_UINT2_RETURN(width > 1 && height > 1, width, height,);
  107. // FIXME add default and min props for this
  108. if (pData->minWidth == 0 && pData->minHeight == 0)
  109. puglSetDefaultSize(pData->view, static_cast<int>(width), static_cast<int>(height));
  110. puglSetWindowSize(pData->view, width, height);
  111. }
  112. void Window::setSize(const Size<uint>& size)
  113. {
  114. setSize(size.getWidth(), size.getHeight());
  115. }
  116. const char* Window::getTitle() const noexcept
  117. {
  118. return puglGetWindowTitle(pData->view);
  119. }
  120. void Window::setTitle(const char* const title)
  121. {
  122. puglSetWindowTitle(pData->view, title);
  123. }
  124. bool Window::isIgnoringKeyRepeat() const noexcept
  125. {
  126. return puglGetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT) == PUGL_TRUE;
  127. }
  128. void Window::setIgnoringKeyRepeat(const bool ignore) noexcept
  129. {
  130. puglSetViewHint(pData->view, PUGL_IGNORE_KEY_REPEAT, ignore);
  131. }
  132. bool Window::addIdleCallback(IdleCallback* const callback, const uint timerFrequencyInMs)
  133. {
  134. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr, false)
  135. return pData->addIdleCallback(callback, timerFrequencyInMs);
  136. }
  137. bool Window::removeIdleCallback(IdleCallback* const callback)
  138. {
  139. DISTRHO_SAFE_ASSERT_RETURN(callback != nullptr, false)
  140. return pData->removeIdleCallback(callback);
  141. }
  142. Application& Window::getApp() const noexcept
  143. {
  144. return pData->app;
  145. }
  146. #ifndef DPF_TEST_WINDOW_CPP
  147. const GraphicsContext& Window::getGraphicsContext() const noexcept
  148. {
  149. return pData->getGraphicsContext();
  150. }
  151. #endif
  152. uintptr_t Window::getNativeWindowHandle() const noexcept
  153. {
  154. return puglGetNativeWindow(pData->view);
  155. }
  156. double Window::getScaleFactor() const noexcept
  157. {
  158. return pData->scaleFactor;
  159. }
  160. void Window::focus()
  161. {
  162. pData->focus();
  163. }
  164. #ifndef DGL_FILE_BROWSER_DISABLED
  165. bool Window::openFileBrowser(const FileBrowserOptions& options)
  166. {
  167. return pData->openFileBrowser(options);
  168. }
  169. #endif
  170. void Window::repaint() noexcept
  171. {
  172. puglPostRedisplay(pData->view);
  173. }
  174. void Window::repaint(const Rectangle<uint>& rect) noexcept
  175. {
  176. const PuglRect prect = {
  177. static_cast<double>(rect.getX()),
  178. static_cast<double>(rect.getY()),
  179. static_cast<double>(rect.getWidth()),
  180. static_cast<double>(rect.getHeight()),
  181. };
  182. puglPostRedisplayRect(pData->view, prect);
  183. }
  184. void Window::runAsModal(bool blockWait)
  185. {
  186. pData->runAsModal(blockWait);
  187. }
  188. void Window::setGeometryConstraints(const uint minimumWidth,
  189. const uint minimumHeight,
  190. const bool keepAspectRatio,
  191. const bool automaticallyScale)
  192. {
  193. DISTRHO_SAFE_ASSERT_RETURN(minimumWidth > 0,);
  194. DISTRHO_SAFE_ASSERT_RETURN(minimumHeight > 0,);
  195. if (pData->isEmbed) {
  196. // Did you forget to set DISTRHO_UI_USER_RESIZABLE ?
  197. DISTRHO_SAFE_ASSERT_RETURN(isResizable(),);
  198. } else if (! isResizable()) {
  199. setResizable(true);
  200. }
  201. pData->minWidth = minimumWidth;
  202. pData->minHeight = minimumHeight;
  203. pData->autoScaling = automaticallyScale;
  204. const double scaleFactor = pData->scaleFactor;
  205. puglSetGeometryConstraints(pData->view,
  206. static_cast<uint>(minimumWidth * scaleFactor + 0.5),
  207. static_cast<uint>(minimumHeight * scaleFactor + 0.5),
  208. keepAspectRatio);
  209. if (scaleFactor != 1.0)
  210. {
  211. const Size<uint> size(getSize());
  212. setSize(static_cast<uint>(size.getWidth() * scaleFactor + 0.5),
  213. static_cast<uint>(size.getHeight() * scaleFactor + 0.5));
  214. }
  215. }
  216. bool Window::onClose()
  217. {
  218. return true;
  219. }
  220. void Window::onFocus(bool, CrossingMode)
  221. {
  222. }
  223. void Window::onReshape(uint, uint)
  224. {
  225. puglFallbackOnResize(pData->view);
  226. }
  227. #ifndef DGL_FILE_BROWSER_DISABLED
  228. void Window::onFileSelected(const char*)
  229. {
  230. }
  231. #endif
  232. #if 0
  233. void Window::setTransientWinId(const uintptr_t winId)
  234. {
  235. puglSetTransientFor(pData->view, winId);
  236. }
  237. // -----------------------------------------------------------------------
  238. bool Window::handlePluginKeyboard(const bool press, const uint key)
  239. {
  240. // TODO
  241. return false;
  242. // return pData->handlePluginKeyboard(press, key);
  243. }
  244. bool Window::handlePluginSpecial(const bool press, const Key key)
  245. {
  246. // TODO
  247. return false;
  248. // return pData->handlePluginSpecial(press, key);
  249. }
  250. #endif
  251. // -----------------------------------------------------------------------
  252. END_NAMESPACE_DGL