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.

203 lines
5.3KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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.h"
  18. #ifdef BRIDGE_LV2_X11
  19. # error X11 UI uses Qt4
  20. #endif
  21. #include <gtk/gtk.h>
  22. #include <QtCore/QSettings>
  23. CARLA_BRIDGE_START_NAMESPACE
  24. // -------------------------------------------------------------------------
  25. class CarlaToolkitGtk2 : public CarlaToolkit
  26. {
  27. public:
  28. CarlaToolkitGtk2(const char* const title)
  29. : CarlaToolkit(title),
  30. settings("Cadence", "Carla-Gtk2UIs")
  31. {
  32. qDebug("CarlaToolkitGtk2::CarlaToolkitGtk2(%s)", title);
  33. window = nullptr;
  34. lastX = lastY = 0;
  35. lastWidth = lastHeight = 0;
  36. }
  37. ~CarlaToolkitGtk2()
  38. {
  39. qDebug("CarlaToolkitGtk2::~CarlaToolkitGtk2()");
  40. }
  41. void init()
  42. {
  43. qDebug("CarlaToolkitGtk2::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("CarlaToolkitGtk2::exec(%p)", client);
  51. Q_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("CarlaToolkitGtk2::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("CarlaToolkitGtk2::show()");
  94. Q_ASSERT(window);
  95. if (window)
  96. gtk_widget_show_all(window);
  97. }
  98. void hide()
  99. {
  100. qDebug("CarlaToolkitGtk2::hide()");
  101. Q_ASSERT(window);
  102. if (window)
  103. gtk_widget_hide_all(window);
  104. }
  105. void resize(int width, int height)
  106. {
  107. qDebug("CarlaToolkitGtk2::resize(%i, %i)", width, height);
  108. Q_ASSERT(window);
  109. if (window)
  110. gtk_window_resize(GTK_WINDOW(window), width, height);
  111. }
  112. // ---------------------------------------------------------------------
  113. protected:
  114. void handleDestroy()
  115. {
  116. qDebug("CarlaToolkitGtk2::handleDestroy()");
  117. window = nullptr;
  118. m_client = nullptr;
  119. settings.setValue(QString("%1/pos_x").arg(m_title), lastX);
  120. settings.setValue(QString("%1/pos_y").arg(m_title), lastY);
  121. settings.setValue(QString("%1/width").arg(m_title), lastWidth);
  122. settings.setValue(QString("%1/height").arg(m_title), lastHeight);
  123. settings.sync();
  124. }
  125. gboolean handleTimeout()
  126. {
  127. if (window)
  128. {
  129. gtk_window_get_position(GTK_WINDOW(window), &lastX, &lastY);
  130. gtk_window_get_size(GTK_WINDOW(window), &lastWidth, &lastHeight);
  131. }
  132. return m_client ? m_client->runMessages() : false;
  133. }
  134. // ---------------------------------------------------------------------
  135. private:
  136. GtkWidget* window;
  137. QSettings settings;
  138. gint lastX, lastY, lastWidth, lastHeight;
  139. static void gtk_ui_destroy(GtkWidget*, gpointer data)
  140. {
  141. CarlaToolkitGtk2* const _this_ = (CarlaToolkitGtk2*)data;
  142. _this_->handleDestroy();
  143. gtk_main_quit();
  144. }
  145. static gboolean gtk_ui_timeout(gpointer data)
  146. {
  147. CarlaToolkitGtk2* const _this_ = (CarlaToolkitGtk2*)data;
  148. return _this_->handleTimeout();
  149. }
  150. };
  151. // -------------------------------------------------------------------------
  152. CarlaToolkit* CarlaToolkit::createNew(const char* const title)
  153. {
  154. return new CarlaToolkitGtk2(title);
  155. }
  156. CARLA_BRIDGE_END_NAMESPACE