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.

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