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.

194 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. last_x = last_y = 0;
  35. last_width = last_height = 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. 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), &last_x, &last_y);
  57. gtk_window_get_size(GTK_WINDOW(window), &last_width, &last_height);
  58. if (settings.contains(QString("%1/pos_x").arg(m_title)))
  59. {
  60. last_x = settings.value(QString("%1/pos_x").arg(m_title), last_x).toInt();
  61. last_y = settings.value(QString("%1/pos_y").arg(m_title), last_y).toInt();
  62. gtk_window_move(GTK_WINDOW(window), last_x, last_y);
  63. if (client->isResizable())
  64. {
  65. last_width = settings.value(QString("%1/width").arg(m_title), last_width).toInt();
  66. last_height = settings.value(QString("%1/height").arg(m_title), last_height).toInt();
  67. gtk_window_resize(GTK_WINDOW(window), last_width, last_height);
  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. client->oscSendUpdate();
  74. // Main loop
  75. gtk_main();
  76. }
  77. void quit()
  78. {
  79. qDebug("CarlaBridgeToolkitGtk2::quit()");
  80. if (window)
  81. {
  82. gtk_widget_destroy(window);
  83. gtk_main_quit();
  84. window = nullptr;
  85. }
  86. m_client = nullptr;
  87. }
  88. void show()
  89. {
  90. qDebug("CarlaBridgeToolkitGtk2::show()");
  91. assert(window);
  92. if (window)
  93. gtk_widget_show_all(window);
  94. }
  95. void hide()
  96. {
  97. qDebug("CarlaBridgeToolkitGtk2::hide()");
  98. assert(window);
  99. if (window)
  100. gtk_widget_hide_all(window);
  101. }
  102. void resize(int width, int height)
  103. {
  104. qDebug("CarlaBridgeToolkitGtk2::resize(%i, %i)", width, height);
  105. assert(window);
  106. if (window)
  107. gtk_window_resize(GTK_WINDOW(window), width, height);
  108. }
  109. private:
  110. GtkWidget* window;
  111. QSettings settings;
  112. gint last_x, last_y, last_width, last_height;
  113. static void gtk_ui_destroy(GtkWidget*, gpointer data)
  114. {
  115. CarlaBridgeToolkitGtk2* const _this_ = (CarlaBridgeToolkitGtk2*)data;
  116. _this_->handleDestroy();
  117. gtk_main_quit();
  118. }
  119. static gboolean gtk_ui_timeout(gpointer data)
  120. {
  121. CarlaBridgeToolkitGtk2* const _this_ = (CarlaBridgeToolkitGtk2*)data;
  122. return _this_->handleTimeout();
  123. }
  124. // ---------------------------------------------------------------------
  125. void handleDestroy()
  126. {
  127. window = nullptr;
  128. m_client = nullptr;
  129. settings.setValue(QString("%1/pos_x").arg(m_title), last_x);
  130. settings.setValue(QString("%1/pos_y").arg(m_title), last_y);
  131. settings.setValue(QString("%1/width").arg(m_title), last_width);
  132. settings.setValue(QString("%1/height").arg(m_title), last_height);
  133. settings.sync();
  134. }
  135. gboolean handleTimeout()
  136. {
  137. if (window)
  138. {
  139. gtk_window_get_position(GTK_WINDOW(window), &last_x, &last_y);
  140. gtk_window_get_size(GTK_WINDOW(window), &last_width, &last_height);
  141. }
  142. return m_client ? m_client->runMessages() : false;
  143. }
  144. };
  145. // -------------------------------------------------------------------------
  146. CarlaBridgeToolkit* CarlaBridgeToolkit::createNew(const char* const title)
  147. {
  148. return new CarlaBridgeToolkitGtk2(title);
  149. }
  150. CARLA_BRIDGE_END_NAMESPACE