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.

148 lines
3.7KB

  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 Point CocoaPoint
  27. # import <Cocoa/Cocoa.h>
  28. # undef Component
  29. # undef Point
  30. #endif
  31. // -----------------------------------------------------------------------
  32. namespace juce {
  33. class JucePluginWindow : public DialogWindow
  34. {
  35. public:
  36. JucePluginWindow(const uintptr_t parentId)
  37. : DialogWindow("JucePluginWindow", Colour(50, 50, 200), true, false),
  38. fClosed(false),
  39. fTransientId(parentId)
  40. {
  41. setVisible(false);
  42. //setAlwaysOnTop(true);
  43. setOpaque(true);
  44. setResizable(false, false);
  45. setUsingNativeTitleBar(true);
  46. }
  47. void show(Component* const comp)
  48. {
  49. fClosed = false;
  50. centreWithSize(comp->getWidth(), comp->getHeight());
  51. setContentNonOwned(comp, true);
  52. if (! isOnDesktop())
  53. addToDesktop();
  54. setVisible(true);
  55. setTransient();
  56. }
  57. void hide()
  58. {
  59. setVisible(false);
  60. if (isOnDesktop())
  61. removeFromDesktop();
  62. clearContentComponent();
  63. }
  64. bool wasClosedByUser() const noexcept
  65. {
  66. return fClosed;
  67. }
  68. protected:
  69. void closeButtonPressed() override
  70. {
  71. fClosed = true;
  72. }
  73. bool escapeKeyPressed() override
  74. {
  75. fClosed = true;
  76. return true;
  77. }
  78. private:
  79. volatile bool fClosed;
  80. const uintptr_t fTransientId;
  81. void setTransient()
  82. {
  83. if (fTransientId == 0)
  84. return;
  85. #if defined(CARLA_OS_LINUX) && defined(HAVE_X11)
  86. Display* const display = XWindowSystem::getInstance()->getDisplay();
  87. CARLA_SAFE_ASSERT_RETURN(display != nullptr,);
  88. ::Window window = (::Window)getWindowHandle();
  89. CARLA_SAFE_ASSERT_RETURN(window != 0,);
  90. XSetTransientForHint(display, window, static_cast<::Window>(fTransientId));
  91. #endif
  92. #ifdef CARLA_OS_MAC
  93. NSView* const view = (NSView*)getWindowHandle();
  94. CARLA_SAFE_ASSERT_RETURN(view != nullptr,);
  95. NSWindow* const window = [view window];
  96. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  97. NSWindow* const parentWindow = [NSApp windowWithWindowNumber:fTransientId];
  98. CARLA_SAFE_ASSERT_RETURN(parentWindow != nullptr,);
  99. [parentWindow addChildWindow:window
  100. ordered:NSWindowAbove];
  101. #endif
  102. #ifdef CARLA_OS_WIN
  103. const HWND window = (HWND)getWindowHandle();
  104. CARLA_SAFE_ASSERT_RETURN(window != nullptr,);
  105. SetWindowLongPtr(window, GWLP_HWNDPARENT, static_cast<LONG_PTR>(fTransientId));
  106. #endif
  107. }
  108. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(JucePluginWindow)
  109. };
  110. } // namespace juce
  111. using juce::JucePluginWindow;
  112. // -----------------------------------------------------------------------
  113. #endif // JUCE_PLUGIN_WINDOW_HPP_INCLUDED