Audio plugin host https://kx.studio/carla
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.

186 lines
4.6KB

  1. /*
  2. * Juce Plugin Window Helper
  3. * Copyright (C) 2013-2019 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 doc/GPL.txt file.
  16. */
  17. #ifndef JUCE_PLUGIN_WINDOW_HPP_INCLUDED
  18. #define JUCE_PLUGIN_WINDOW_HPP_INCLUDED
  19. #include "CarlaJuceUtils.hpp"
  20. #include "AppConfig.h"
  21. #include "juce_gui_basics/juce_gui_basics.h"
  22. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  23. # include <X11/Xlib.h>
  24. #elif defined(CARLA_OS_MAC)
  25. # define Component CocoaComponent
  26. # define MemoryBlock CocoaMemoryBlock
  27. # define Point CocoaPoint
  28. # import <Cocoa/Cocoa.h>
  29. # undef Component
  30. # undef MemoryBlock
  31. # undef Point
  32. #endif
  33. // -----------------------------------------------------------------------
  34. namespace juce {
  35. class JucePluginWindow : public DialogWindow
  36. {
  37. public:
  38. JucePluginWindow(const uintptr_t parentId, const bool isStandalone)
  39. : DialogWindow("JucePluginWindow", Colour(50, 50, 200), true, false),
  40. fIsStandalone(isStandalone),
  41. fClosed(false),
  42. fShown(false),
  43. fTransientId(parentId)
  44. {
  45. setVisible(false);
  46. setOpaque(true);
  47. setResizable(false, false);
  48. setUsingNativeTitleBar(true);
  49. }
  50. void show(Component* const comp)
  51. {
  52. fClosed = false;
  53. fShown = true;
  54. centreWithSize(comp->getWidth(), comp->getHeight());
  55. setContentNonOwned(comp, true);
  56. if (! isOnDesktop())
  57. addToDesktop();
  58. #ifndef CARLA_OS_LINUX
  59. setAlwaysOnTop(true);
  60. #endif
  61. setTransient();
  62. setVisible(true);
  63. toFront(true);
  64. #ifndef CARLA_OS_LINUX
  65. postCommandMessage(0);
  66. #endif
  67. }
  68. void hide()
  69. {
  70. setVisible(false);
  71. if (isOnDesktop())
  72. removeFromDesktop();
  73. clearContentComponent();
  74. }
  75. bool wasClosedByUser() const noexcept
  76. {
  77. return fClosed;
  78. }
  79. protected:
  80. void closeButtonPressed() override
  81. {
  82. fClosed = true;
  83. }
  84. bool escapeKeyPressed() override
  85. {
  86. fClosed = true;
  87. return true;
  88. }
  89. int getDesktopWindowStyleFlags() const override
  90. {
  91. int wflags = 0;
  92. wflags |= ComponentPeer::windowHasCloseButton;
  93. wflags |= ComponentPeer::windowHasDropShadow;
  94. wflags |= ComponentPeer::windowHasTitleBar;
  95. if (fIsStandalone)
  96. wflags |= ComponentPeer::windowAppearsOnTaskbar;
  97. return wflags;
  98. }
  99. #ifndef CARLA_OS_LINUX
  100. void handleCommandMessage(const int comamndId) override
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(comamndId == 0,);
  103. if (fShown)
  104. {
  105. fShown = false;
  106. setAlwaysOnTop(false);
  107. }
  108. }
  109. #endif
  110. private:
  111. const bool fIsStandalone;
  112. volatile bool fClosed;
  113. bool fShown;
  114. const uintptr_t fTransientId;
  115. void setTransient()
  116. {
  117. if (fTransientId == 0)
  118. return;
  119. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  120. Display* const display = XWindowSystem::getInstance()->getDisplay();
  121. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  122. ::Window window = (::Window)getWindowHandle();
  123. CARLA_SAFE_ASSERT_RETURN(window != 0,);
  124. XSetTransientForHint(display, window, static_cast<::Window>(fTransientId));
  125. #endif
  126. #ifdef CARLA_OS_MAC
  127. NSView* const view = (NSView*)getWindowHandle();
  128. CARLA_SAFE_ASSERT_RETURN(view != nullptr,);
  129. NSWindow* const window = [view window];
  130. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  131. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:fTransientId];
  132. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  133. [parentWindow addChildWindow:window
  134. ordered:NSWindowAbove];
  135. #endif
  136. #ifdef CARLA_OS_WIN
  137. const HWND window = (HWND)getWindowHandle();
  138. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  139. SetWindowLongPtr(window, GWLP_HWNDPARENT, static_cast<LONG_PTR>(fTransientId));
  140. #endif
  141. }
  142. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePluginWindow)
  143. };
  144. } // namespace juce
  145. using juce::JucePluginWindow;
  146. // -----------------------------------------------------------------------
  147. #endif // JUCE_PLUGIN_WINDOW_HPP_INCLUDED