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.

198 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 CarlaBridgeToolkitGtk2: public CarlaBridgeToolkit
  26. {
  27. public:
  28. CarlaBridgeToolkitGtk2(const char* const title)
  29. : CarlaBridgeToolkit(title),
  30. settings("Cadence", "Carla-Gtk2UIs")
  31. {
  32. qDebug("CarlaBridgeToolkitGtk2::CarlaBridgeToolkitGtk2(%s)", title);
  33. window = nullptr;
  34. lastX = lastY = 0;
  35. lastWidth = lastHeight = 0;
  36. }
  37. ~CarlaBridgeToolkitGtk2()
  38. {
  39. qDebug("CarlaBridgeToolkitGtk2::~CarlaBridgeToolkitGtk2()");
  40. }
  41. void init()
  42. {
  43. qDebug("CarlaBridgeToolkitGtk2::init()");
  44. static int argc = 0;
  45. static char** argv = { nullptr };
  46. gtk_init(&argc, &argv);
  47. }
  48. void exec(CarlaBridgeClient* const client)
  49. {
  50. qDebug("CarlaBridgeToolkitGtk2::exec(%p)", client);
  51. Q_ASSERT(client);
  52. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  53. gtk_container_add(GTK_CONTAINER(window), (GtkWidget*)client->getWidget());
  54. gtk_window_set_resizable(GTK_WINDOW(window), client->isResizable());
  55. gtk_window_set_title(GTK_WINDOW(window), m_title);
  56. gtk_window_get_position(GTK_WINDOW(window), &lastX, &lastY);
  57. gtk_window_get_size(GTK_WINDOW(window), &lastWidth, &lastHeight);
  58. if (settings.contains(QString("%1/pos_x").arg(m_title)))
  59. {
  60. lastX = settings.value(QString("%1/pos_x").arg(m_title), lastX).toInt();
  61. lastY = settings.value(QString("%1/pos_y").arg(m_title), lastY).toInt();
  62. gtk_window_move(GTK_WINDOW(window), lastX, lastY);
  63. if (client->isResizable())
  64. {
  65. lastWidth = settings.value(QString("%1/width").arg(m_title), lastWidth).toInt();
  66. lastHeight = settings.value(QString("%1/height").arg(m_title), lastHeight).toInt();
  67. gtk_window_resize(GTK_WINDOW(window), lastWidth, lastHeight);
  68. }
  69. }
  70. g_timeout_add(50, gtk_ui_timeout, this);
  71. g_signal_connect(window, "destroy", G_CALLBACK(gtk_ui_destroy), this);
  72. m_client = client;
  73. m_client->sendOscUpdate();
  74. #ifdef QTCREATOR_TEST
  75. show();
  76. #endif
  77. // Main loop
  78. gtk_main();
  79. }
  80. void quit()
  81. {
  82. qDebug("CarlaBridgeToolkitGtk2::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("CarlaBridgeToolkitGtk2::show()");
  94. Q_ASSERT(window);
  95. if (window)
  96. gtk_widget_show_all(window);
  97. }
  98. void hide()
  99. {
  100. qDebug("CarlaBridgeToolkitGtk2::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("CarlaBridgeToolkitGtk2::resize(%i, %i)", width, height);
  108. Q_ASSERT(window);
  109. if (window)
  110. gtk_window_resize(GTK_WINDOW(window), width, height);
  111. }
  112. private:
  113. GtkWidget* window;
  114. QSettings settings;
  115. gint lastX, lastY, lastWidth, lastHeight;
  116. static void gtk_ui_destroy(GtkWidget*, gpointer data)
  117. {
  118. CarlaBridgeToolkitGtk2* const _this_ = (CarlaBridgeToolkitGtk2*)data;
  119. _this_->handleDestroy();
  120. gtk_main_quit();
  121. }
  122. static gboolean gtk_ui_timeout(gpointer data)
  123. {
  124. CarlaBridgeToolkitGtk2* const _this_ = (CarlaBridgeToolkitGtk2*)data;
  125. return _this_->handleTimeout();
  126. }
  127. // ---------------------------------------------------------------------
  128. void handleDestroy()
  129. {
  130. window = nullptr;
  131. m_client = nullptr;
  132. settings.setValue(QString("%1/pos_x").arg(m_title), lastX);
  133. settings.setValue(QString("%1/pos_y").arg(m_title), lastY);
  134. settings.setValue(QString("%1/width").arg(m_title), lastWidth);
  135. settings.setValue(QString("%1/height").arg(m_title), lastHeight);
  136. settings.sync();
  137. }
  138. gboolean handleTimeout()
  139. {
  140. if (window)
  141. {
  142. gtk_window_get_position(GTK_WINDOW(window), &lastX, &lastY);
  143. gtk_window_get_size(GTK_WINDOW(window), &lastWidth, &lastHeight);
  144. }
  145. return m_client ? m_client->runMessages() : false;
  146. }
  147. };
  148. // -------------------------------------------------------------------------
  149. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(const char* const title)
  150. {
  151. return new CarlaBridgeToolkitGtk2(title);
  152. }
  153. CARLA_BRIDGE_END_NAMESPACE