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.

201 lines
5.2KB

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