Transient Designer: image and text for manual, small change to the UI
[calf.git] / src / plugin_gui_window.cpp
blob0d8e206340d370c864c2e0fb967092c9b7f14115
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 plugin_gui_window *self = (plugin_gui_window *)data;
245 self->gui->destroy_child_widgets(GTK_WIDGET(self->toplevel));
246 delete self;
249 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
251 preset_access_iface *pai = gui->preset_access;
252 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
253 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
254 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
255 for (unsigned int i = 0; i < pvec.size(); i++)
257 if (pvec[i].plugin != gui->effect_name)
258 continue;
259 stringstream ss;
260 ss << (builtin ? "builtin_preset" : "user_preset") << i;
261 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
262 if (ch != ' ' && ++ch == ':')
263 ch = 'A';
264 if (ch > 'Z')
265 ch = ' ';
267 string sv = ss.str();
268 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
269 string name = prefix + pvec[i].name;
270 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
271 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(pai, i, builtin), activate_preset_params::action_destroy_notify);
273 preset_xml += preset_post_xml;
274 return preset_xml;
277 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
279 string command_xml = command_pre_xml;
280 plugin_command_info *ci = gui->plugin->get_metadata_iface()->get_commands();
281 if (!ci)
282 return "";
283 for(int i = 0; ci->name; i++, ci++)
285 stringstream ss;
286 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
288 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
289 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), activate_preset_params::action_destroy_notify);
290 command_xml += ss.str();
292 command_xml += command_post_xml;
293 return command_xml;
296 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
298 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
299 if(preset_actions) {
300 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
301 preset_actions = NULL;
304 if (builtin)
305 builtin_preset_actions = gtk_action_group_new("builtin_presets");
306 else
307 user_preset_actions = gtk_action_group_new("user_presets");
308 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
309 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
310 GError *error = NULL;
311 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
314 gboolean plugin_gui_window::on_idle(void *data)
316 plugin_gui_window *self = (plugin_gui_window *)data;
317 if (!self->refresh_controller.check_redraw(GTK_WIDGET(self->toplevel)))
318 return TRUE;
319 self->gui->on_idle();
320 return TRUE;
323 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
325 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
326 gtk_window_set_icon_name(toplevel, "calf_plugin");
327 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_NORMAL);
328 gtk_window_set_role(toplevel, "calf_plugin");
330 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 0));
331 gtk_widget_set_name(GTK_WIDGET(vbox), "Calf-Plugin");
333 GtkRequisition req, req2;
334 gtk_window_set_title(GTK_WINDOW (toplevel), title);
335 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
337 gui = new plugin_gui(this);
338 gui->effect_name = effect;
340 ui_mgr = gtk_ui_manager_new();
341 std_actions = gtk_action_group_new("default");
342 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
343 GError *error = NULL;
344 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
345 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
347 command_actions = gtk_action_group_new("commands");
349 char ch = '0';
350 fill_gui_presets(true, ch);
351 fill_gui_presets(false, ch);
353 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
354 gtk_widget_set_name(GTK_WIDGET(gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar")), "Calf-Menu");
356 // determine size without content
357 gtk_widget_show_all(GTK_WIDGET(vbox));
358 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
359 // printf("size request %dx%d\n", req2.width, req2.height);
361 GtkWidget *container;
362 const char *xml = _jh->get_metadata_iface()->get_gui_xml();
363 assert(xml);
364 container = gui->create_from_xml(_jh, xml);
365 gtk_widget_show_all(GTK_WIDGET(container));
366 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
367 gtk_widget_show(GTK_WIDGET(sw));
368 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
369 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
370 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(container));
371 gtk_widget_set_name(GTK_WIDGET(sw), "Calf-Container");
372 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
374 gui->show_rack_ears(environment->get_config()->rack_ears);
376 gtk_widget_size_request(GTK_WIDGET(container), &req);
377 int wx = max(req.width + 10, req2.width);
378 int wy = req.height + req2.height + 10;
379 // printf("size request %dx%d\n", req.width, req.height);
380 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
381 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
382 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
383 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
384 // printf("size set %dx%d\n", wx, wy);
385 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
386 g_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (on_window_destroyed), (plugin_gui_window *)this);
387 if (main)
388 main->set_window(gui->plugin, this);
390 source_id = g_timeout_add_full(G_PRIORITY_DEFAULT, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
391 gtk_ui_manager_ensure_update(ui_mgr);
392 gui->plugin->send_configures(gui);
394 notifier = environment->get_config_db()->add_listener(this);
397 void plugin_gui_window::on_config_change()
399 environment->get_config()->load(environment->get_config_db());
400 gui->show_rack_ears(environment->get_config()->rack_ears);
403 void plugin_gui_window::cleanup()
405 if (notifier)
407 delete notifier;
408 notifier = NULL;
410 if (source_id)
411 g_source_remove(source_id);
412 source_id = 0;
415 void plugin_gui_window::close()
417 cleanup();
418 gtk_widget_destroy(GTK_WIDGET(toplevel));
421 plugin_gui_window::~plugin_gui_window()
423 if (main)
424 main->set_window(gui->plugin, NULL);
425 cleanup();
426 delete gui;