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.

178 lines
4.3KB

  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)
  39. : DialogWindow("JucePluginWindow", Colour(50, 50, 200), true, false),
  40. fClosed(false),
  41. fShown(false),
  42. fTransientId(parentId)
  43. {
  44. setVisible(false);
  45. setOpaque(true);
  46. setResizable(false, false);
  47. setUsingNativeTitleBar(true);
  48. }
  49. void show(Component* const comp)
  50. {
  51. fClosed = false;
  52. fShown = true;
  53. centreWithSize(comp->getWidth(), comp->getHeight());
  54. setContentNonOwned(comp, true);
  55. if (! isOnDesktop())
  56. addToDesktop();
  57. #ifndef CARLA_OS_LINUX
  58. setAlwaysOnTop(true);
  59. #endif
  60. setTransient();
  61. setVisible(true);
  62. toFront(true);
  63. #ifndef CARLA_OS_LINUX
  64. postCommandMessage(0);
  65. #endif
  66. }
  67. void hide()
  68. {
  69. setVisible(false);
  70. if (isOnDesktop())
  71. removeFromDesktop();
  72. clearContentComponent();
  73. }
  74. bool wasClosedByUser() const noexcept
  75. {
  76. return fClosed;
  77. }
  78. protected:
  79. void closeButtonPressed() override
  80. {
  81. fClosed = true;
  82. }
  83. bool escapeKeyPressed() override
  84. {
  85. fClosed = true;
  86. return true;
  87. }
  88. int getDesktopWindowStyleFlags() const override
  89. {
  90. return ComponentPeer::windowHasDropShadow | ComponentPeer::windowHasTitleBar | ComponentPeer::windowHasCloseButton;
  91. }
  92. #ifndef CARLA_OS_LINUX
  93. void handleCommandMessage(const int comamndId) override
  94. {
  95. CARLA_SAFE_ASSERT_RETURN(comamndId == 0,);
  96. if (fShown)
  97. {
  98. fShown = false;
  99. setAlwaysOnTop(false);
  100. }
  101. }
  102. #endif
  103. private:
  104. volatile bool fClosed;
  105. bool fShown;
  106. const uintptr_t fTransientId;
  107. void setTransient()
  108. {
  109. if (fTransientId == 0)
  110. return;
  111. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  112. Display* const display = XWindowSystem::getInstance()->getDisplay();
  113. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  114. ::Window window = (::Window)getWindowHandle();
  115. CARLA_SAFE_ASSERT_RETURN(window != 0,);
  116. XSetTransientForHint(display, window, static_cast<::Window>(fTransientId));
  117. #endif
  118. #ifdef CARLA_OS_MAC
  119. NSView* const view = (NSView*)getWindowHandle();
  120. CARLA_SAFE_ASSERT_RETURN(view != nullptr,);
  121. NSWindow* const window = [view window];
  122. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  123. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:fTransientId];
  124. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  125. [parentWindow addChildWindow:window
  126. ordered:NSWindowAbove];
  127. #endif
  128. #ifdef CARLA_OS_WIN
  129. const HWND window = (HWND)getWindowHandle();
  130. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  131. SetWindowLongPtr(window, GWLP_HWNDPARENT, static_cast<LONG_PTR>(fTransientId));
  132. #endif
  133. }
  134. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePluginWindow)
  135. };
  136. } // namespace juce
  137. using juce::JucePluginWindow;
  138. // -----------------------------------------------------------------------
  139. #endif // JUCE_PLUGIN_WINDOW_HPP_INCLUDED