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.

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