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.

199 lines
4.2KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include <QtGui/QMainWindow>
  19. #include <QtGui/QCloseEvent>
  20. #ifdef Q_WS_X11
  21. # include <QtGui/QX11EmbedContainer>
  22. #endif
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Engine Helpers, defined in CarlaEngine.cpp
  26. extern QMainWindow* getEngineHostWindow(CarlaEngine* const engine);
  27. class CarlaPluginGUI : public QMainWindow
  28. {
  29. public:
  30. class Callback
  31. {
  32. public:
  33. virtual ~Callback() {}
  34. virtual void guiClosedCallback() = 0;
  35. };
  36. CarlaPluginGUI(CarlaEngine* const engine, Callback* const callback);
  37. ~CarlaPluginGUI();
  38. void idle();
  39. void resizeLater(int width, int height);
  40. // Parent UIs
  41. void* getContainerWinId();
  42. void closeContainer();
  43. // Qt4 UIs, TODO
  44. protected:
  45. void closeEvent(QCloseEvent* const event);
  46. private:
  47. Callback* const kCallback;
  48. QWidget* fContainer;
  49. int fNextWidth;
  50. int fNextHeight;
  51. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginGUI)
  52. };
  53. // -------------------------------------------------------------------
  54. // CarlaPluginGUI
  55. CarlaPluginGUI::CarlaPluginGUI(CarlaEngine* const engine, Callback* const callback)
  56. : QMainWindow(getEngineHostWindow(engine)),
  57. kCallback(callback),
  58. fContainer(nullptr),
  59. fNextWidth(0),
  60. fNextHeight(0)
  61. {
  62. CARLA_ASSERT(callback != nullptr);
  63. carla_debug("CarlaPluginGUI::CarlaPluginGUI(%p, %p)", engine, callback);
  64. }
  65. CarlaPluginGUI::~CarlaPluginGUI()
  66. {
  67. carla_debug("CarlaPluginGUI::~CarlaPluginGUI()");
  68. closeContainer();
  69. }
  70. void CarlaPluginGUI::idle()
  71. {
  72. if (fNextWidth > 0 && fNextHeight > 0)
  73. {
  74. setFixedSize(fNextWidth, fNextHeight);
  75. fNextWidth = 0;
  76. fNextHeight = 0;
  77. }
  78. }
  79. void CarlaPluginGUI::resizeLater(int width, int height)
  80. {
  81. CARLA_ASSERT_INT(width > 0, width);
  82. CARLA_ASSERT_INT(height > 0, height);
  83. if (width <= 0)
  84. return;
  85. if (height <= 0)
  86. return;
  87. fNextWidth = width;
  88. fNextHeight = height;
  89. }
  90. void* CarlaPluginGUI::getContainerWinId()
  91. {
  92. carla_debug("CarlaPluginGUI::getContainerWinId()");
  93. if (fContainer == nullptr)
  94. {
  95. #ifdef Q_WS_X11
  96. QX11EmbedContainer* container(new QX11EmbedContainer(this));
  97. #else
  98. QWidget* container(new QWidget(this));
  99. #endif
  100. setCentralWidget(container);
  101. fContainer = container;
  102. }
  103. return (void*)fContainer->winId();
  104. }
  105. void CarlaPluginGUI::closeContainer()
  106. {
  107. carla_debug("CarlaPluginGUI::closeContainer()");
  108. if (fContainer != nullptr)
  109. {
  110. #ifdef Q_WS_X11
  111. delete (QX11EmbedContainer*)fContainer;
  112. #else
  113. delete (QWidget*)fContainer;
  114. #endif
  115. fContainer = nullptr;
  116. }
  117. }
  118. void CarlaPluginGUI::closeEvent(QCloseEvent* const event)
  119. {
  120. carla_debug("CarlaPluginGUI::closeEvent(%p)", event);
  121. CARLA_ASSERT(event != nullptr);
  122. if (event == nullptr)
  123. return;
  124. if (! event->spontaneous())
  125. {
  126. event->ignore();
  127. return;
  128. }
  129. if (kCallback != nullptr)
  130. kCallback->guiClosedCallback();
  131. QMainWindow::closeEvent(event);
  132. }
  133. // -------------------------------------------------------------------
  134. // CarlaPluginGUI
  135. #if 0
  136. void createUiIfNeeded(CarlaPluginGUI::Callback* const callback)
  137. {
  138. if (gui != nullptr)
  139. return;
  140. gui = new CarlaPluginGUI(engine, callback);
  141. }
  142. void destroyUiIfNeeded()
  143. {
  144. if (gui == nullptr)
  145. return;
  146. gui->close();
  147. delete gui;
  148. gui = nullptr;
  149. }
  150. void resizeUiLater(int width, int height)
  151. {
  152. if (gui == nullptr)
  153. return;
  154. gui->resizeLater(width, height);
  155. }
  156. #endif
  157. CARLA_BACKEND_END_NAMESPACE