Collection of tools useful for audio production
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.

209 lines
5.5KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_bridge_client.hpp"
  18. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  19. # error Cocoa/HWND/X11 UI uses Qt
  20. #endif
  21. #include <gtk/gtk.h>
  22. #include <QtCore/QSettings>
  23. CARLA_BRIDGE_START_NAMESPACE
  24. // -------------------------------------------------------------------------
  25. class CarlaToolkitGtk : public CarlaToolkit
  26. {
  27. public:
  28. CarlaToolkitGtk(const char* const title)
  29. : CarlaToolkit(title),
  30. settings("Cadence", "Carla-Gtk2UIs")
  31. {
  32. qDebug("CarlaToolkitGtk::CarlaToolkitGtk(%s)", title);
  33. window = nullptr;
  34. lastX = lastY = 0;
  35. lastWidth = lastHeight = 0;
  36. }
  37. ~CarlaToolkitGtk()
  38. {
  39. qDebug("CarlaToolkitGtk::~CarlaToolkitGtk()");
  40. }
  41. void init()
  42. {
  43. qDebug("CarlaToolkitGtk::init()");
  44. static int argc = 0;
  45. static char** argv = { nullptr };
  46. gtk_init(&argc, &argv);
  47. }
  48. void exec(CarlaClient* const client, const bool showGui)
  49. {
  50. qDebug("CarlaToolkitGtk::exec(%p)", client);
  51. CARLA_ASSERT(client);
  52. m_client = client;
  53. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  54. gtk_container_add(GTK_CONTAINER(window), (GtkWidget*)client->getWidget());
  55. gtk_window_set_resizable(GTK_WINDOW(window), client->isResizable());
  56. gtk_window_set_title(GTK_WINDOW(window), m_title);
  57. gtk_window_get_position(GTK_WINDOW(window), &lastX, &lastY);
  58. gtk_window_get_size(GTK_WINDOW(window), &lastWidth, &lastHeight);
  59. if (settings.contains(QString("%1/pos_x").arg(m_title)))
  60. {
  61. lastX = settings.value(QString("%1/pos_x").arg(m_title), lastX).toInt();
  62. lastY = settings.value(QString("%1/pos_y").arg(m_title), lastY).toInt();
  63. gtk_window_move(GTK_WINDOW(window), lastX, lastY);
  64. if (client->isResizable())
  65. {
  66. lastWidth = settings.value(QString("%1/width").arg(m_title), lastWidth).toInt();
  67. lastHeight = settings.value(QString("%1/height").arg(m_title), lastHeight).toInt();
  68. gtk_window_resize(GTK_WINDOW(window), lastWidth, lastHeight);
  69. }
  70. }
  71. g_timeout_add(50, gtk_ui_timeout, this);
  72. g_signal_connect(window, "destroy", G_CALLBACK(gtk_ui_destroy), this);
  73. if (showGui)
  74. show();
  75. else
  76. m_client->sendOscUpdate();
  77. // Main loop
  78. gtk_main();
  79. }
  80. void quit()
  81. {
  82. qDebug("CarlaToolkitGtk::quit()");
  83. if (window)
  84. {
  85. gtk_widget_destroy(window);
  86. gtk_main_quit();
  87. window = nullptr;
  88. }
  89. m_client = nullptr;
  90. }
  91. void show()
  92. {
  93. qDebug("CarlaToolkitGtk::show()");
  94. CARLA_ASSERT(window);
  95. if (window)
  96. gtk_widget_show_all(window);
  97. }
  98. void hide()
  99. {
  100. qDebug("CarlaToolkitGtk::hide()");
  101. CARLA_ASSERT(window);
  102. if (window)
  103. {
  104. #ifdef BRIDGE_GTK2
  105. gtk_widget_hide_all(window);
  106. #else
  107. gtk_widget_hide(window);
  108. #endif
  109. }
  110. }
  111. void resize(int width, int height)
  112. {
  113. qDebug("CarlaToolkitGtk::resize(%i, %i)", width, height);
  114. CARLA_ASSERT(window);
  115. if (window)
  116. gtk_window_resize(GTK_WINDOW(window), width, height);
  117. }
  118. // ---------------------------------------------------------------------
  119. protected:
  120. void handleDestroy()
  121. {
  122. qDebug("CarlaToolkitGtk::handleDestroy()");
  123. window = nullptr;
  124. m_client = nullptr;
  125. settings.setValue(QString("%1/pos_x").arg(m_title), lastX);
  126. settings.setValue(QString("%1/pos_y").arg(m_title), lastY);
  127. settings.setValue(QString("%1/width").arg(m_title), lastWidth);
  128. settings.setValue(QString("%1/height").arg(m_title), lastHeight);
  129. settings.sync();
  130. }
  131. gboolean handleTimeout()
  132. {
  133. if (window)
  134. {
  135. gtk_window_get_position(GTK_WINDOW(window), &lastX, &lastY);
  136. gtk_window_get_size(GTK_WINDOW(window), &lastWidth, &lastHeight);
  137. }
  138. return m_client ? m_client->oscIdle() : false;
  139. }
  140. // ---------------------------------------------------------------------
  141. private:
  142. GtkWidget* window;
  143. QSettings settings;
  144. gint lastX, lastY, lastWidth, lastHeight;
  145. static void gtk_ui_destroy(GtkWidget*, gpointer data)
  146. {
  147. CarlaToolkitGtk* const _this_ = (CarlaToolkitGtk*)data;
  148. _this_->handleDestroy();
  149. gtk_main_quit();
  150. }
  151. static gboolean gtk_ui_timeout(gpointer data)
  152. {
  153. CarlaToolkitGtk* const _this_ = (CarlaToolkitGtk*)data;
  154. return _this_->handleTimeout();
  155. }
  156. };
  157. // -------------------------------------------------------------------------
  158. CarlaToolkit* CarlaToolkit::createNew(const char* const title)
  159. {
  160. return new CarlaToolkitGtk(title);
  161. }
  162. CARLA_BRIDGE_END_NAMESPACE