Rack: Menu with custom icons
[calf.git] / src / plugin_gui_window.cpp
blob49f0a55111128a9ecd7892a7ab5f453e59f8ef09
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@zamaudio.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 and Fader Control\n"
130 "\n"
131 "* Use SHIFT-dragging for increased precision\n"
132 "* Mouse wheel is also supported\n"
133 "* Middle click opens a text entry\n"
134 "* Right click a knob to assign a MIDI controller\n"
135 "\n"
136 "2. Rack Ears\n"
137 "\n"
138 "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 (LV2 GTK+, LV2 External, JACK host).\n"
139 "\n"
141 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));
142 if (!dlg)
143 return;
146 gtk_window_set_title(GTK_WINDOW(dlg), "Tips and Tricks");
147 gtk_dialog_run(GTK_DIALOG(dlg));
148 gtk_widget_destroy(GTK_WIDGET(dlg));
153 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
155 plugin_gui *gui = params->gui;
156 gui->plugin->execute(params->function_idx);
157 gui->refresh();
161 static const GtkActionEntry actions[] = {
162 { "PresetMenuAction", NULL, "_Preset", NULL, "Preset operations", NULL },
163 { "BuiltinPresetMenuAction", NULL, "_Built-in", NULL, "Built-in (factory) presets", NULL },
164 { "UserPresetMenuAction", NULL, "_User", NULL, "User (your) presets", NULL },
165 { "CommandMenuAction", NULL, "_Command", NULL, "Plugin-related commands", NULL },
166 { "HelpMenuAction", NULL, "_Help", NULL, "Help-related commands", NULL },
167 { "store-preset", "gtk-save-as", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
168 { "about", "gtk-about", "_About...", NULL, "About this plugin", (GCallback)about_action },
169 { "HelpMenuItemAction", "gtk-help", "_Help", NULL, "Show manual page for this plugin", (GCallback)help_action },
170 { "tips-tricks", NULL, "_Tips and tricks...", NULL, "Show a list of tips and tricks", (GCallback)tips_tricks_action },
173 /***************************** GUI window ********************************************/
175 static const char *ui_xml =
176 "<ui>\n"
177 " <menubar>\n"
178 " <menu action=\"PresetMenuAction\">\n"
179 " <menuitem action=\"store-preset\"/>\n"
180 " <separator/>\n"
181 " <placeholder name=\"builtin_presets\"/>\n"
182 " <separator/>\n"
183 " <placeholder name=\"user_presets\"/>\n"
184 " </menu>\n"
185 " <placeholder name=\"commands\"/>\n"
186 " <menu action=\"HelpMenuAction\">\n"
187 " <menuitem action=\"HelpMenuItemAction\"/>\n"
188 " <menuitem action=\"tips-tricks\"/>\n"
189 " <separator/>\n"
190 " <menuitem action=\"about\"/>\n"
191 " </menu>\n"
192 " </menubar>\n"
193 "</ui>\n"
196 static const char *general_preset_pre_xml =
197 "<ui>\n"
198 " <menubar>\n"
199 " <menu action=\"PresetMenuAction\">\n";
201 static const char *builtin_preset_pre_xml =
202 " <placeholder name=\"builtin_presets\">\n";
204 static const char *user_preset_pre_xml =
205 " <placeholder name=\"user_presets\">\n";
207 static const char *preset_post_xml =
208 " </placeholder>\n"
209 " </menu>\n"
210 " </menubar>\n"
211 "</ui>\n"
214 static const char *command_pre_xml =
215 "<ui>\n"
216 " <menubar>\n"
217 " <placeholder name=\"commands\">\n"
218 " <menu action=\"CommandMenuAction\">\n";
220 static const char *command_post_xml =
221 " </menu>\n"
222 " </placeholder>\n"
223 " </menubar>\n"
224 "</ui>\n"
227 plugin_gui_window::plugin_gui_window(gui_environment_iface *_env, main_window_iface *_main)
229 toplevel = NULL;
230 ui_mgr = NULL;
231 std_actions = NULL;
232 builtin_preset_actions = NULL;
233 user_preset_actions = NULL;
234 command_actions = NULL;
235 environment = _env;
236 notifier = NULL;
237 main = _main;
238 assert(environment);
242 void plugin_gui_window::on_window_destroyed(GtkWidget *window, gpointer data)
244 delete (plugin_gui_window *)data;
247 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
249 preset_access_iface *pai = gui->preset_access;
250 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
251 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
252 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
253 for (unsigned int i = 0; i < pvec.size(); i++)
255 if (pvec[i].plugin != gui->effect_name)
256 continue;
257 stringstream ss;
258 ss << (builtin ? "builtin_preset" : "user_preset") << i;
259 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
260 if (ch != ' ' && ++ch == ':')
261 ch = 'A';
262 if (ch > 'Z')
263 ch = ' ';
265 string sv = ss.str();
266 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
267 string name = prefix + pvec[i].name;
268 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
269 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(pai, i, builtin), activate_preset_params::action_destroy_notify);
271 preset_xml += preset_post_xml;
272 return preset_xml;
275 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
277 string command_xml = command_pre_xml;
278 plugin_command_info *ci = gui->plugin->get_metadata_iface()->get_commands();
279 if (!ci)
280 return "";
281 for(int i = 0; ci->name; i++, ci++)
283 stringstream ss;
284 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
286 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
287 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), activate_preset_params::action_destroy_notify);
288 command_xml += ss.str();
290 command_xml += command_post_xml;
291 return command_xml;
294 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
296 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
297 if(preset_actions) {
298 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
299 preset_actions = NULL;
302 if (builtin)
303 builtin_preset_actions = gtk_action_group_new("builtin_presets");
304 else
305 user_preset_actions = gtk_action_group_new("user_presets");
306 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
307 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
308 GError *error = NULL;
309 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
312 gboolean plugin_gui_window::on_idle(void *data)
314 plugin_gui_window *self = (plugin_gui_window *)data;
315 if (!self->refresh_controller.check_redraw(GTK_WIDGET(self->toplevel)))
316 return TRUE;
317 self->gui->on_idle();
318 return TRUE;
321 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
323 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
324 gtk_window_set_icon_name(toplevel, "calf_plugin");
325 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_NORMAL);
326 gtk_window_set_role(toplevel, "calf_plugin");
328 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 0));
329 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-Plugin");
331 GtkRequisition req, req2;
332 gtk_window_set_title(GTK_WINDOW (toplevel), title);
333 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
335 gui = new plugin_gui(this);
336 gui->effect_name = effect;
338 ui_mgr = gtk_ui_manager_new();
339 std_actions = gtk_action_group_new("default");
340 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
341 GError *error = NULL;
342 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
343 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
345 command_actions = gtk_action_group_new("commands");
347 char ch = '0';
348 fill_gui_presets(true, ch);
349 fill_gui_presets(false, ch);
351 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
352 gtk_widget_set_name(GTK_WIDGET(gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar")), "Calf-Menu");
354 // determine size without content
355 gtk_widget_show_all(GTK_WIDGET(vbox));
356 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
357 // printf("size request %dx%d\n", req2.width, req2.height);
359 GtkWidget *container;
360 const char *xml = _jh->get_metadata_iface()->get_gui_xml();
361 assert(xml);
362 container = gui->create_from_xml(_jh, xml);
363 gtk_widget_show_all(GTK_WIDGET(container));
364 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
365 gtk_widget_show(GTK_WIDGET(sw));
366 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
367 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
368 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(container));
369 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-Container");
370 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
372 gui->show_rack_ears(environment->get_config()->rack_ears);
374 gtk_widget_size_request(GTK_WIDGET(container), &req);
375 int wx = max(req.width + 10, req2.width);
376 int wy = req.height + req2.height + 10;
377 // printf("size request %dx%d\n", req.width, req.height);
378 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
379 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
380 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
381 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
382 // printf("size set %dx%d\n", wx, wy);
383 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
384 g_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (on_window_destroyed), (plugin_gui_window *)this);
385 if (main)
386 main->set_window(gui->plugin, this);
388 source_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
389 gtk_ui_manager_ensure_update(ui_mgr);
390 gui->plugin->send_configures(gui);
392 notifier = environment->get_config_db()->add_listener(this);
395 void plugin_gui_window::on_config_change()
397 environment->get_config()->load(environment->get_config_db());
398 gui->show_rack_ears(environment->get_config()->rack_ears);
401 void plugin_gui_window::cleanup()
403 if (notifier)
405 delete notifier;
406 notifier = NULL;
408 if (source_id)
409 g_source_remove(source_id);
410 source_id = 0;
413 void plugin_gui_window::close()
415 cleanup();
416 gtk_widget_destroy(GTK_WIDGET(toplevel));
419 plugin_gui_window::~plugin_gui_window()
421 if (main)
422 main->set_window(gui->plugin, NULL);
423 cleanup();
424 delete gui;