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.

201 lines
4.8KB

  1. /*
  2. * DISTRHO Ildaeil Plugin
  3. * Copyright (C) 2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. #include "src/DistrhoDefines.h"
  18. #if defined(DISTRHO_OS_HAIKU)
  19. #elif defined(DISTRHO_OS_MAC)
  20. # import <Cocoa/Cocoa.h>
  21. #elif defined(DISTRHO_OS_WINDOWS)
  22. #else
  23. # include <X11/Xlib.h>
  24. # include <X11/Xutil.h>
  25. #endif
  26. #include "PluginHostWindow.hpp"
  27. #ifdef DISTRHO_OS_MAC
  28. @interface IldaeilPluginView : NSView
  29. - (void)resizeWithOldSuperviewSize:(NSSize)oldSize;
  30. @end
  31. @implementation IldaeilPluginView
  32. - (void)resizeWithOldSuperviewSize:(NSSize)oldSize
  33. {
  34. [super resizeWithOldSuperviewSize:oldSize];
  35. }
  36. @end
  37. #endif
  38. START_NAMESPACE_DGL
  39. #if defined(DISTRHO_OS_HAIKU)
  40. #elif defined(DISTRHO_OS_MAC)
  41. #elif defined(DISTRHO_OS_WINDOWS)
  42. #else
  43. #endif
  44. struct PluginHostWindow::PrivateData
  45. {
  46. Window& parentWindow;
  47. const uintptr_t parentWindowId;
  48. Callbacks* const pluginWindowCallbacks;
  49. #if defined(DISTRHO_OS_HAIKU)
  50. #elif defined(DISTRHO_OS_MAC)
  51. IldaeilPluginView* view;
  52. #elif defined(DISTRHO_OS_WINDOWS)
  53. #else
  54. #endif
  55. bool lookingForChildren;
  56. PrivateData(Window& pw, Callbacks* const cbs)
  57. : parentWindow(pw),
  58. parentWindowId(pw.getNativeWindowHandle()),
  59. pluginWindowCallbacks(cbs),
  60. #if defined(DISTRHO_OS_HAIKU)
  61. #elif defined(DISTRHO_OS_MAC)
  62. view(nullptr),
  63. #elif defined(DISTRHO_OS_WINDOWS)
  64. #else
  65. #endif
  66. lookingForChildren(false)
  67. {
  68. view = [[IldaeilPluginView new]retain];
  69. DISTRHO_SAFE_ASSERT_RETURN(view != nullptr,)
  70. [view setAutoresizingMask:NSViewNotSizable];
  71. [view setAutoresizesSubviews:YES];
  72. [view setHidden:YES];
  73. [(NSView*)parentWindowId addSubview:view];
  74. }
  75. ~PrivateData()
  76. {
  77. #if defined(DISTRHO_OS_HAIKU)
  78. #elif defined(DISTRHO_OS_MAC)
  79. if (view != nullptr)
  80. {
  81. [view release];
  82. view = nullptr;
  83. }
  84. #elif defined(DISTRHO_OS_WINDOWS)
  85. #else
  86. #endif
  87. }
  88. void* attachAndGetWindowHandle()
  89. {
  90. lookingForChildren = true;
  91. #if defined(DISTRHO_OS_HAIKU)
  92. #elif defined(DISTRHO_OS_MAC)
  93. return view;
  94. #elif defined(DISTRHO_OS_WINDOWS)
  95. #else
  96. #endif
  97. }
  98. void detach()
  99. {
  100. #if defined(DISTRHO_OS_HAIKU)
  101. #elif defined(DISTRHO_OS_MAC)
  102. if (view != nullptr)
  103. [view setHidden:YES];
  104. #elif defined(DISTRHO_OS_WINDOWS)
  105. #else
  106. #endif
  107. }
  108. void idle()
  109. {
  110. if (lookingForChildren)
  111. {
  112. #if defined(DISTRHO_OS_HAIKU)
  113. #elif defined(DISTRHO_OS_MAC)
  114. if (view == nullptr)
  115. return;
  116. for (NSView* subview in [view subviews])
  117. {
  118. const double scaleFactor = [[[view window] screen] backingScaleFactor];
  119. const NSSize size = [subview frame].size;
  120. const double width = size.width;
  121. const double height = size.height;
  122. d_stdout("found subview %f %f", width, height);
  123. if (width <= 1 || height <= 1)
  124. break;
  125. lookingForChildren = false;
  126. [view setFrameSize:size];
  127. [view setHidden:NO];
  128. [view setNeedsDisplay:YES];
  129. pluginWindowCallbacks->pluginWindowResized(width * scaleFactor, height * scaleFactor);
  130. break;
  131. }
  132. #elif defined(DISTRHO_OS_WINDOWS)
  133. #else
  134. #endif
  135. }
  136. }
  137. void setPositionAndSize(const uint x, const uint y, const uint width, const uint height)
  138. {
  139. #if defined(DISTRHO_OS_HAIKU)
  140. #elif defined(DISTRHO_OS_MAC)
  141. if (view != nullptr)
  142. {
  143. const double scaleFactor = [[[view window] screen] backingScaleFactor];
  144. [view setFrame:NSMakeRect(x / scaleFactor, y / scaleFactor, width / scaleFactor, height / scaleFactor)];
  145. }
  146. #elif defined(DISTRHO_OS_WINDOWS)
  147. #else
  148. #endif
  149. }
  150. };
  151. PluginHostWindow::PluginHostWindow(Window& parentWindow, Callbacks* const cbs)
  152. : pData(new PrivateData(parentWindow, cbs)) {}
  153. PluginHostWindow::~PluginHostWindow()
  154. {
  155. delete pData;
  156. }
  157. void* PluginHostWindow::attachAndGetWindowHandle()
  158. {
  159. return pData->attachAndGetWindowHandle();
  160. }
  161. void PluginHostWindow::detach()
  162. {
  163. pData->detach();
  164. }
  165. void PluginHostWindow::idle()
  166. {
  167. pData->idle();
  168. }
  169. void PluginHostWindow::setPositionAndSize(const uint x, const uint y, const uint width, const uint height)
  170. {
  171. pData->setPositionAndSize(x, y, width, height);
  172. }
  173. END_NAMESPACE_DGL