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.

204 lines
5.3KB

  1. /*
  2. * Carla Bridge Toolkit, Gtk version
  3. * Copyright (C) 2011-2014 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. #include "CarlaBridgeClient.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include <gtk/gtk.h>
  20. CARLA_BRIDGE_START_NAMESPACE
  21. // -------------------------------------------------------------------------
  22. static int gargc = 0;
  23. static char** gargv = nullptr;
  24. // -------------------------------------------------------------------------
  25. class CarlaBridgeToolkitGtk : public CarlaBridgeToolkit
  26. {
  27. public:
  28. CarlaBridgeToolkitGtk(CarlaBridgeClient* const client, const char* const windowTitle)
  29. : CarlaBridgeToolkit(client, windowTitle),
  30. fNeedsShow(false),
  31. fWindow(nullptr),
  32. fLastX(0),
  33. fLastY(0),
  34. fLastWidth(0),
  35. fLastHeight(0),
  36. leakDetector_CarlaBridgeToolkitGtk()
  37. {
  38. carla_debug("CarlaBridgeToolkitGtk::CarlaBridgeToolkitGtk(%p, \"%s\")", client, windowTitle);
  39. }
  40. ~CarlaBridgeToolkitGtk() override
  41. {
  42. CARLA_ASSERT(fWindow == nullptr);
  43. carla_debug("CarlaBridgeToolkitGtk::~CarlaBridgeToolkitGtk()");
  44. }
  45. void init() override
  46. {
  47. CARLA_ASSERT(fWindow == nullptr);
  48. carla_debug("CarlaBridgeToolkitGtk::init()");
  49. gtk_init(&gargc, &gargv);
  50. fWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  51. gtk_window_resize(GTK_WINDOW(fWindow), 30, 30);
  52. gtk_widget_hide(fWindow);
  53. }
  54. void exec(const bool showUI) override
  55. {
  56. CARLA_ASSERT(kClient != nullptr);
  57. CARLA_ASSERT(fWindow != nullptr);
  58. carla_debug("CarlaBridgeToolkitGtk::exec(%s)", bool2str(showUI));
  59. GtkWidget* const widget((GtkWidget*)kClient->getWidget());
  60. gtk_container_add(GTK_CONTAINER(fWindow), widget);
  61. gtk_window_set_resizable(GTK_WINDOW(fWindow), kClient->isResizable());
  62. gtk_window_set_title(GTK_WINDOW(fWindow), kWindowTitle);
  63. if (showUI || fNeedsShow)
  64. {
  65. show();
  66. fNeedsShow = false;
  67. }
  68. g_timeout_add(30, gtk_ui_timeout, this);
  69. g_signal_connect(fWindow, "destroy", G_CALLBACK(gtk_ui_destroy), this);
  70. // First idle
  71. handleTimeout();
  72. // Main loop
  73. gtk_main();
  74. }
  75. void quit() override
  76. {
  77. carla_debug("CarlaBridgeToolkitGtk::quit()");
  78. if (fWindow != nullptr)
  79. {
  80. gtk_widget_destroy(fWindow);
  81. fWindow = nullptr;
  82. if (gtk_main_level() != 0)
  83. gtk_main_quit();
  84. }
  85. }
  86. void show() override
  87. {
  88. carla_debug("CarlaBridgeToolkitGtk::show()");
  89. fNeedsShow = true;
  90. if (fWindow != nullptr)
  91. gtk_widget_show_all(fWindow);
  92. }
  93. void hide() override
  94. {
  95. carla_debug("CarlaBridgeToolkitGtk::hide()");
  96. fNeedsShow = false;
  97. if (fWindow != nullptr)
  98. gtk_widget_hide(fWindow);
  99. }
  100. void resize(int width, int height) override
  101. {
  102. CARLA_ASSERT(fWindow != nullptr);
  103. carla_debug("CarlaBridgeToolkitGtk::resize(%i, %i)", width, height);
  104. if (fWindow != nullptr)
  105. gtk_window_resize(GTK_WINDOW(fWindow), width, height);
  106. }
  107. // ---------------------------------------------------------------------
  108. protected:
  109. bool fNeedsShow;
  110. GtkWidget* fWindow;
  111. gint fLastX;
  112. gint fLastY;
  113. gint fLastWidth;
  114. gint fLastHeight;
  115. void handleDestroy()
  116. {
  117. carla_debug("CarlaBridgeToolkitGtk::handleDestroy()");
  118. fWindow = nullptr;
  119. }
  120. gboolean handleTimeout()
  121. {
  122. if (fWindow != nullptr)
  123. {
  124. gtk_window_get_position(GTK_WINDOW(fWindow), &fLastX, &fLastY);
  125. gtk_window_get_size(GTK_WINDOW(fWindow), &fLastWidth, &fLastHeight);
  126. }
  127. kClient->uiIdle();
  128. return kClient->oscIdle();
  129. }
  130. // ---------------------------------------------------------------------
  131. private:
  132. static void gtk_ui_destroy(GtkWidget*, gpointer data)
  133. {
  134. CARLA_ASSERT(data != nullptr);
  135. if (CarlaBridgeToolkitGtk* const _this_ = (CarlaBridgeToolkitGtk*)data)
  136. _this_->handleDestroy();
  137. gtk_main_quit();
  138. }
  139. static gboolean gtk_ui_timeout(gpointer data)
  140. {
  141. CARLA_ASSERT(data != nullptr);
  142. if (CarlaBridgeToolkitGtk* const _this_ = (CarlaBridgeToolkitGtk*)data)
  143. return _this_->handleTimeout();
  144. return false;
  145. }
  146. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgeToolkitGtk)
  147. };
  148. // -------------------------------------------------------------------------
  149. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(CarlaBridgeClient* const client, const char* const windowTitle)
  150. {
  151. return new CarlaBridgeToolkitGtk(client, windowTitle);
  152. }
  153. // -------------------------------------------------------------------------
  154. CARLA_BRIDGE_END_NAMESPACE