Allow minimizing plugin windows, use slower refresh rates when minimized.
[calf.git] / src / plugin_gui_window.cpp
blob74d0053da6d06ee67f96132c6cbae122af9333dc
1 /* Calf DSP Library
2 * Toplevel window that hosts the plugin GUI.
3 * Copyright (C) 2007-2011 Krzysztof Foltman
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 #include <calf/gui_config.h>
22 #include <calf/gui_controls.h>
23 #include <calf/preset.h>
24 #include <calf/preset_gui.h>
25 #include <gdk/gdk.h>
27 #include <iostream>
29 using namespace calf_plugins;
30 using namespace std;
32 /******************************* Actions **************************************************/
34 namespace {
36 void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
38 if (gui_win->gui->preset_access)
39 gui_win->gui->preset_access->store_preset();
42 struct activate_preset_params
44 preset_access_iface *preset_access;
45 int preset;
46 bool builtin;
48 activate_preset_params(preset_access_iface *_pai, int _preset, bool _builtin)
49 : preset_access(_pai), preset(_preset), builtin(_builtin)
52 static void action_destroy_notify(gpointer data)
54 delete (activate_preset_params *)data;
58 void activate_preset(GtkAction *action, activate_preset_params *params)
60 params->preset_access->activate_preset(params->preset, params->builtin);
63 void help_action(GtkAction *action, plugin_gui_window *gui_win)
65 string uri = "file://" PKGDOCDIR "/" + string(gui_win->gui->plugin->get_metadata_iface()->get_label()) + ".html";
66 GError *error = NULL;
67 if (!gtk_show_uri(gtk_window_get_screen(gui_win->toplevel), uri.c_str(), time(NULL), &error))
69 GtkMessageDialog *dlg = GTK_MESSAGE_DIALOG(gtk_message_dialog_new(gui_win->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_OTHER, GTK_BUTTONS_OK, "%s", error->message));
70 if (!dlg)
71 return;
73 gtk_dialog_run(GTK_DIALOG(dlg));
74 gtk_widget_destroy(GTK_WIDGET(dlg));
76 g_error_free(error);
80 void about_action(GtkAction *action, plugin_gui_window *gui_win)
82 GtkAboutDialog *dlg = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
83 if (!dlg)
84 return;
86 static const char *artists[] = {
87 "Markus Schmidt (GUI, icons)",
88 "Thorsten Wilms (previous icon)",
89 NULL
92 static const char *authors[] = {
93 "Krzysztof Foltman <wdev@foltman.com>",
94 "Hermann Meyer <brummer-@web.de>",
95 "Thor Harald Johansen <thj@thj.no>",
96 "Thorsten Wilms <t_w_@freenet.de>",
97 "Hans Baier <hansfbaier@googlemail.com>",
98 "Torben Hohn <torbenh@gmx.de>",
99 "Markus Schmidt <schmidt@boomshop.net>",
100 "Christian Holschuh <chrisch.holli@gmx.de>",
101 "Tom Szilagyi <tomszilagyi@gmail.com>",
102 "Damien Zammit <damien.zammit@gmail.com>",
103 "David T\xC3\xA4ht <d@teklibre.com>",
104 "Dave Robillard <dave@drobilla.net>",
105 NULL
108 static const char translators[] =
109 "Russian: Alexandre Prokoudine <alexandre.prokoudine@gmail.com>\n"
112 string label = gui_win->gui->plugin->get_metadata_iface()->get_label();
113 gtk_about_dialog_set_name(dlg, ("About Calf " + label).c_str());
114 gtk_about_dialog_set_program_name(dlg, ("Calf " + label).c_str());
115 gtk_about_dialog_set_version(dlg, PACKAGE_VERSION);
116 gtk_about_dialog_set_website(dlg, "http://calf.sourceforge.net/");
117 gtk_about_dialog_set_copyright(dlg, "Copyright \xC2\xA9 2001-2011 Krzysztof Foltman, Thor Harald Johansen, Markus Schmidt and others.\nSee AUTHORS file for the full list.");
118 gtk_about_dialog_set_logo_icon_name(dlg, "calf");
119 gtk_about_dialog_set_artists(dlg, artists);
120 gtk_about_dialog_set_authors(dlg, authors);
121 gtk_about_dialog_set_translator_credits(dlg, translators);
122 gtk_dialog_run(GTK_DIALOG(dlg));
123 gtk_widget_destroy(GTK_WIDGET(dlg));
126 void tips_tricks_action(GtkAction *action, plugin_gui_window *gui_win)
128 static const char tips_and_tricks[] =
129 "1. Knob control\n"
130 "\n"
131 "Use SHIFT-dragging for increased precision. Mouse wheel is also supported.\n"
132 "\n"
133 "2. Rack ears\n"
134 "\n"
135 "If you consider those a waste of screen space, you can turn them off in Preferences dialog in Calf JACK host. The setting affects all versions of the GUI (DSSI, LV2 GTK+, LV2 External, JACK host).\n"
136 "\n"
138 GtkMessageDialog *dlg = GTK_MESSAGE_DIALOG(gtk_message_dialog_new(gui_win->toplevel, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_OTHER, GTK_BUTTONS_OK, "%s", tips_and_tricks));
139 if (!dlg)
140 return;
143 gtk_window_set_title(GTK_WINDOW(dlg), "Tips and Tricks");
144 gtk_dialog_run(GTK_DIALOG(dlg));
145 gtk_widget_destroy(GTK_WIDGET(dlg));
150 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
152 plugin_gui *gui = params->gui;
153 gui->plugin->execute(params->function_idx);
154 gui->refresh();
158 static const GtkActionEntry actions[] = {
159 { "PresetMenuAction", NULL, "_Preset", NULL, "Preset operations", NULL },
160 { "BuiltinPresetMenuAction", NULL, "_Built-in", NULL, "Built-in (factory) presets", NULL },
161 { "UserPresetMenuAction", NULL, "_User", NULL, "User (your) presets", NULL },
162 { "CommandMenuAction", NULL, "_Command", NULL, "Plugin-related commands", NULL },
163 { "HelpMenuAction", NULL, "_Help", NULL, "Help-related commands", NULL },
164 { "store-preset", "gtk-save-as", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
165 { "about", "gtk-about", "_About...", NULL, "About this plugin", (GCallback)about_action },
166 { "HelpMenuItemAction", "gtk-help", "_Help", NULL, "Show manual page for this plugin", (GCallback)help_action },
167 { "tips-tricks", NULL, "_Tips and tricks...", NULL, "Show a list of tips and tricks", (GCallback)tips_tricks_action },
170 /***************************** GUI window ********************************************/
172 static const char *ui_xml =
173 "<ui>\n"
174 " <menubar>\n"
175 " <menu action=\"PresetMenuAction\">\n"
176 " <menuitem action=\"store-preset\"/>\n"
177 " <separator/>\n"
178 " <placeholder name=\"builtin_presets\"/>\n"
179 " <separator/>\n"
180 " <placeholder name=\"user_presets\"/>\n"
181 " </menu>\n"
182 " <placeholder name=\"commands\"/>\n"
183 " <menu action=\"HelpMenuAction\">\n"
184 " <menuitem action=\"HelpMenuItemAction\"/>\n"
185 " <menuitem action=\"tips-tricks\"/>\n"
186 " <separator/>\n"
187 " <menuitem action=\"about\"/>\n"
188 " </menu>\n"
189 " </menubar>\n"
190 "</ui>\n"
193 static const char *general_preset_pre_xml =
194 "<ui>\n"
195 " <menubar>\n"
196 " <menu action=\"PresetMenuAction\">\n";
198 static const char *builtin_preset_pre_xml =
199 " <placeholder name=\"builtin_presets\">\n";
201 static const char *user_preset_pre_xml =
202 " <placeholder name=\"user_presets\">\n";
204 static const char *preset_post_xml =
205 " </placeholder>\n"
206 " </menu>\n"
207 " </menubar>\n"
208 "</ui>\n"
211 static const char *command_pre_xml =
212 "<ui>\n"
213 " <menubar>\n"
214 " <placeholder name=\"commands\">\n"
215 " <menu action=\"CommandMenuAction\">\n";
217 static const char *command_post_xml =
218 " </menu>\n"
219 " </placeholder>\n"
220 " </menubar>\n"
221 "</ui>\n"
224 plugin_gui_window::plugin_gui_window(gui_environment_iface *_env, main_window_iface *_main)
226 toplevel = NULL;
227 ui_mgr = NULL;
228 std_actions = NULL;
229 builtin_preset_actions = NULL;
230 user_preset_actions = NULL;
231 command_actions = NULL;
232 environment = _env;
233 notifier = NULL;
234 main = _main;
235 assert(environment);
239 void plugin_gui_window::on_window_destroyed(GtkWidget *window, gpointer data)
241 delete (plugin_gui_window *)data;
244 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
246 preset_access_iface *pai = gui->preset_access;
247 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
248 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
249 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
250 for (unsigned int i = 0; i < pvec.size(); i++)
252 if (pvec[i].plugin != gui->effect_name)
253 continue;
254 stringstream ss;
255 ss << (builtin ? "builtin_preset" : "user_preset") << i;
256 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
257 if (ch != ' ' && ++ch == ':')
258 ch = 'A';
259 if (ch > 'Z')
260 ch = ' ';
262 string sv = ss.str();
263 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
264 string name = prefix + pvec[i].name;
265 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
266 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(pai, i, builtin), activate_preset_params::action_destroy_notify);
268 preset_xml += preset_post_xml;
269 return preset_xml;
272 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
274 string command_xml = command_pre_xml;
275 plugin_command_info *ci = gui->plugin->get_metadata_iface()->get_commands();
276 if (!ci)
277 return "";
278 for(int i = 0; ci->name; i++, ci++)
280 stringstream ss;
281 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
283 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
284 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), activate_preset_params::action_destroy_notify);
285 command_xml += ss.str();
287 command_xml += command_post_xml;
288 return command_xml;
291 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
293 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
294 if(preset_actions) {
295 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
296 preset_actions = NULL;
299 if (builtin)
300 builtin_preset_actions = gtk_action_group_new("builtin_presets");
301 else
302 user_preset_actions = gtk_action_group_new("user_presets");
303 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
304 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
305 GError *error = NULL;
306 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
309 gboolean plugin_gui_window::on_idle(void *data)
311 plugin_gui_window *self = (plugin_gui_window *)data;
312 if (!self->refresh_controller.check_redraw(GTK_WIDGET(self->toplevel)))
313 return TRUE;
314 self->gui->on_idle();
315 return TRUE;
318 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
320 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
321 gtk_window_set_icon_name(toplevel, "calf_plugin");
322 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_NORMAL);
323 gtk_window_set_role(toplevel, "plugin_ui");
325 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 0));
326 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-Plugin");
328 GtkRequisition req, req2;
329 gtk_window_set_title(GTK_WINDOW (toplevel), title);
330 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
332 gui = new plugin_gui(this);
333 gui->effect_name = effect;
335 ui_mgr = gtk_ui_manager_new();
336 std_actions = gtk_action_group_new("default");
337 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
338 GError *error = NULL;
339 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
340 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
342 command_actions = gtk_action_group_new("commands");
344 char ch = '0';
345 fill_gui_presets(true, ch);
346 fill_gui_presets(false, ch);
348 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
349 gtk_widget_set_name(GTK_WIDGET(gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar")), "Calf-Menu");
351 // determine size without content
352 gtk_widget_show_all(GTK_WIDGET(vbox));
353 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
354 // printf("size request %dx%d\n", req2.width, req2.height);
356 GtkWidget *container;
357 const char *xml = _jh->get_metadata_iface()->get_gui_xml();
358 assert(xml);
359 container = gui->create_from_xml(_jh, xml);
361 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
362 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
363 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
364 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(container));
365 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-Container");
366 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
368 gtk_widget_show_all(GTK_WIDGET(sw));
370 gui->show_rack_ears(environment->get_config()->rack_ears);
372 gtk_widget_size_request(GTK_WIDGET(container), &req);
373 int wx = max(req.width + 10, req2.width);
374 int wy = req.height + req2.height + 10;
375 // printf("size request %dx%d\n", req.width, req.height);
376 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
377 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
378 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
379 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
380 // printf("size set %dx%d\n", wx, wy);
381 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
382 g_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (on_window_destroyed), (plugin_gui_window *)this);
383 if (main)
384 main->set_window(gui->plugin, this);
386 source_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
387 gtk_ui_manager_ensure_update(ui_mgr);
388 gui->plugin->send_configures(gui);
390 notifier = environment->get_config_db()->add_listener(this);
393 void plugin_gui_window::on_config_change()
395 environment->get_config()->load(environment->get_config_db());
396 gui->show_rack_ears(environment->get_config()->rack_ears);
399 void plugin_gui_window::cleanup()
401 if (notifier)
403 delete notifier;
404 notifier = NULL;
406 if (source_id)
407 g_source_remove(source_id);
408 source_id = 0;
411 void plugin_gui_window::close()
413 cleanup();
414 gtk_widget_destroy(GTK_WIDGET(toplevel));
417 plugin_gui_window::~plugin_gui_window()
419 if (main)
420 main->set_window(gui->plugin, NULL);
421 cleanup();
422 delete gui;