+ AutoHell: print the dependency detection result for the GUI code shared between...
[calf.git] / src / gui.cpp
blob69cc3d0fa6db83afe07356b4b632f804bb583b11
1 /* Calf DSP Library
2 * GUI functions for a plugin.
3 * Copyright (C) 2007-2009 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 <config.h>
22 #include <assert.h>
23 #include <calf/giface.h>
24 #include <calf/gui.h>
25 #include <calf/gui_controls.h>
26 #include <calf/preset.h>
27 #include <calf/preset_gui.h>
28 #include <calf/main_win.h>
29 #include <gdk/gdk.h>
31 #include <iostream>
33 using namespace calf_plugins;
34 using namespace std;
36 /******************************** control base classes **********************/
38 void control_container::set_std_properties()
40 if (attribs.find("widget-name") != attribs.end())
42 string name = attribs["widget-name"];
43 if (container) {
44 gtk_widget_set_name(GTK_WIDGET(container), name.c_str());
49 void param_control::set_std_properties()
51 if (attribs.find("widget-name") != attribs.end())
53 string name = attribs["widget-name"];
54 if (widget) {
55 gtk_widget_set_name(widget, name.c_str());
60 GtkWidget *param_control::create_label()
62 label = gtk_label_new ("");
63 gtk_label_set_width_chars (GTK_LABEL (label), 12);
64 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
65 return label;
68 void param_control::update_label()
70 parameter_properties &props = get_props();
71 gtk_label_set_text (GTK_LABEL (label), props.to_string(gui->plugin->get_param_value(param_no)).c_str());
74 void param_control::hook_params()
76 if (param_no != -1) {
77 gui->add_param_ctl(param_no, this);
79 gui->params.push_back(this);
82 param_control::~param_control()
84 if (label)
85 gtk_widget_destroy(label);
86 if (widget)
87 gtk_widget_destroy(widget);
90 /******************************** GUI proper ********************************/
92 plugin_gui::plugin_gui(plugin_gui_window *_window)
93 : last_status_serial_no(0)
94 , window(_window)
96 ignore_stack = 0;
97 top_container = NULL;
98 current_control = NULL;
99 param_count = 0;
100 container = NULL;
101 effect_name = NULL;
104 static void window_destroyed(GtkWidget *window, gpointer data)
106 delete (plugin_gui_window *)data;
109 static void action_destroy_notify(gpointer data)
111 delete (activate_preset_params *)data;
114 void control_base::require_attribute(const char *name)
116 if (attribs.count(name) == 0) {
117 g_error("Missing attribute: %s", name);
121 void control_base::require_int_attribute(const char *name)
123 if (attribs.count(name) == 0) {
124 g_error("Missing attribute: %s", name);
126 if (attribs[name].empty() || attribs[name].find_first_not_of("0123456789") != string::npos) {
127 g_error("Wrong data type on attribute: %s (required integer)", name);
131 int control_base::get_int(const char *name, int def_value)
133 if (attribs.count(name) == 0)
134 return def_value;
135 const std::string &v = attribs[name];
136 if (v.empty() || v.find_first_not_of("-+0123456789") != string::npos)
137 return def_value;
138 return atoi(v.c_str());
141 float control_base::get_float(const char *name, float def_value)
143 if (attribs.count(name) == 0)
144 return def_value;
145 const std::string &v = attribs[name];
146 if (v.empty() || v.find_first_not_of("-+0123456789.") != string::npos)
147 return def_value;
148 stringstream ss(v);
149 float value;
150 ss >> value;
151 return value;
154 /******************************** GUI proper ********************************/
156 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
158 if (!strcmp(element, "knob"))
159 return new knob_param_control;
160 if (!strcmp(element, "hscale"))
161 return new hscale_param_control;
162 if (!strcmp(element, "vscale"))
163 return new vscale_param_control;
164 if (!strcmp(element, "combo"))
165 return new combo_box_param_control;
166 if (!strcmp(element, "check"))
167 return new check_param_control;
168 if (!strcmp(element, "toggle"))
169 return new toggle_param_control;
170 if (!strcmp(element, "spin"))
171 return new spin_param_control;
172 if (!strcmp(element, "button"))
173 return new button_param_control;
174 if (!strcmp(element, "label"))
175 return new label_param_control;
176 if (!strcmp(element, "value"))
177 return new value_param_control;
178 if (!strcmp(element, "vumeter"))
179 return new vumeter_param_control;
180 if (!strcmp(element, "line-graph"))
181 return new line_graph_param_control;
182 if (!strcmp(element, "keyboard"))
183 return new keyboard_param_control;
184 if (!strcmp(element, "curve"))
185 return new curve_param_control;
186 if (!strcmp(element, "led"))
187 return new led_param_control;
188 if (!strcmp(element, "entry"))
189 return new entry_param_control;
190 if (!strcmp(element, "filechooser"))
191 return new filechooser_param_control;
192 if (!strcmp(element, "listview"))
193 return new listview_param_control;
194 return NULL;
197 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
199 if (!strcmp(element, "table"))
200 return new table_container;
201 if (!strcmp(element, "vbox"))
202 return new vbox_container;
203 if (!strcmp(element, "hbox"))
204 return new hbox_container;
205 if (!strcmp(element, "align"))
206 return new alignment_container;
207 if (!strcmp(element, "frame"))
208 return new frame_container;
209 if (!strcmp(element, "notebook"))
210 return new notebook_container;
211 if (!strcmp(element, "scrolled"))
212 return new scrolled_container;
213 return NULL;
216 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
218 plugin_gui *gui = (plugin_gui *)data;
219 gui->xml_element_start(element, attributes);
222 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
224 if (ignore_stack) {
225 ignore_stack++;
226 return;
228 control_base::xml_attribute_map xam;
229 while(*attributes)
231 xam[attributes[0]] = attributes[1];
232 attributes += 2;
235 if (!strcmp(element, "if"))
237 if (!xam.count("cond") || xam["cond"].empty())
239 g_error("Incorrect <if cond=\"[!]symbol\"> element");
241 string cond = xam["cond"];
242 bool state = true;
243 if (cond.substr(0, 1) == "!") {
244 state = false;
245 cond.erase(0, 1);
247 if (window->main->check_condition(cond.c_str()) == state)
248 return;
249 ignore_stack = 1;
250 return;
252 control_container *cc = create_container_from_xml(element, attributes);
253 if (cc != NULL)
255 cc->attribs = xam;
256 cc->create(this, element, xam);
257 cc->set_std_properties();
258 gtk_container_set_border_width(cc->container, cc->get_int("border"));
260 container_stack.push_back(cc);
261 current_control = NULL;
262 return;
264 if (!container_stack.empty())
266 current_control = create_control_from_xml(element, attributes);
267 if (current_control)
269 current_control->attribs = xam;
270 int param_no = -1;
271 if (xam.count("param"))
273 map<string, int>::iterator it = param_name_map.find(xam["param"]);
274 if (it == param_name_map.end())
275 g_error("Unknown parameter %s", xam["param"].c_str());
276 else
277 param_no = it->second;
279 if (param_no != -1)
280 current_control->param_variable = plugin->get_param_props(param_no)->short_name;
281 current_control->create(this, param_no);
282 current_control->set_std_properties();
283 current_control->init_xml(element);
284 current_control->set();
285 current_control->hook_params();
286 return;
289 g_error("Unxpected element %s in GUI definition\n", element);
292 void plugin_gui::xml_element_end(void *data, const char *element)
294 plugin_gui *gui = (plugin_gui *)data;
295 if (gui->ignore_stack) {
296 gui->ignore_stack--;
297 return;
299 if (!strcmp(element, "if"))
301 return;
303 if (gui->current_control)
305 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
306 gui->current_control = NULL;
307 return;
309 unsigned int ss = gui->container_stack.size();
310 if (ss > 1) {
311 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
313 else
314 gui->top_container = gui->container_stack[0];
315 gui->container_stack.pop_back();
319 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
321 current_control = NULL;
322 top_container = NULL;
323 parser = XML_ParserCreate("UTF-8");
324 plugin = _plugin;
325 container_stack.clear();
326 ignore_stack = 0;
328 param_name_map.clear();
329 int size = plugin->get_param_count();
330 for (int i = 0; i < size; i++)
331 param_name_map[plugin->get_param_props(i)->short_name] = i;
333 XML_SetUserData(parser, this);
334 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
335 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
336 if (status == XML_STATUS_ERROR)
338 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
341 XML_ParserFree(parser);
342 last_status_serial_no = plugin->send_status_updates(this, 0);
343 return GTK_WIDGET(top_container->container);
346 void plugin_gui::send_configure(const char *key, const char *value)
348 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
349 for (unsigned int i = 0; i < params.size(); i++)
351 assert(params[i] != NULL);
352 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
353 if (sci)
354 sci->send_configure(key, value);
358 void plugin_gui::send_status(const char *key, const char *value)
360 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
361 for (unsigned int i = 0; i < params.size(); i++)
363 assert(params[i] != NULL);
364 send_updates_iface *sui = dynamic_cast<send_updates_iface *>(params[i]);
365 if (sui)
366 sui->send_status(key, value);
370 void plugin_gui::on_idle()
372 for (unsigned int i = 0; i < params.size(); i++)
374 if (params[i]->param_no != -1)
376 parameter_properties &props = *plugin->get_param_props(params[i]->param_no);
377 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
378 if (is_output) {
379 params[i]->set();
382 params[i]->on_idle();
384 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
385 // XXXKF iterate over par2ctl, too...
388 void plugin_gui::refresh()
390 for (unsigned int i = 0; i < params.size(); i++)
391 params[i]->set();
392 plugin->send_configures(this);
393 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
396 void plugin_gui::refresh(int param_no, param_control *originator)
398 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
399 while(it != par2ctl.end() && it->first == param_no)
401 if (it->second != originator)
402 it->second->set();
403 it++;
407 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
409 plugin->set_param_value(param_no, value);
410 refresh(param_no);
413 plugin_gui::~plugin_gui()
415 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
417 delete *i;
422 /******************************* Actions **************************************************/
424 static void store_preset_action(GtkAction *action, plugin_gui_window *gui_win)
426 store_preset(GTK_WINDOW(gui_win->toplevel), gui_win->gui);
429 static const GtkActionEntry actions[] = {
430 { "PresetMenuAction", NULL, "_Preset", NULL, "Preset operations", NULL },
431 { "BuiltinPresetMenuAction", NULL, "_Built-in", NULL, "Built-in (factory) presets", NULL },
432 { "UserPresetMenuAction", NULL, "_User", NULL, "User (your) presets", NULL },
433 { "CommandMenuAction", NULL, "_Command", NULL, "Plugin-related commands", NULL },
434 { "store-preset", "gtk-save-as", "Store preset", NULL, "Store a current setting as preset", (GCallback)store_preset_action },
437 /***************************** GUI window ********************************************/
439 static const char *ui_xml =
440 "<ui>\n"
441 " <menubar>\n"
442 " <menu action=\"PresetMenuAction\">\n"
443 " <menuitem action=\"store-preset\"/>\n"
444 " <separator/>\n"
445 " <placeholder name=\"builtin_presets\"/>\n"
446 " <separator/>\n"
447 " <placeholder name=\"user_presets\"/>\n"
448 " </menu>\n"
449 " <placeholder name=\"commands\"/>\n"
450 " </menubar>\n"
451 "</ui>\n"
454 static const char *general_preset_pre_xml =
455 "<ui>\n"
456 " <menubar>\n"
457 " <menu action=\"PresetMenuAction\">\n";
459 static const char *builtin_preset_pre_xml =
460 " <placeholder name=\"builtin_presets\">\n";
462 static const char *user_preset_pre_xml =
463 " <placeholder name=\"user_presets\">\n";
465 static const char *preset_post_xml =
466 " </placeholder>\n"
467 " </menu>\n"
468 " </menubar>\n"
469 "</ui>\n"
472 static const char *command_pre_xml =
473 "<ui>\n"
474 " <menubar>\n"
475 " <placeholder name=\"commands\">\n"
476 " <menu action=\"CommandMenuAction\">\n";
478 static const char *command_post_xml =
479 " </menu>\n"
480 " </placeholder>\n"
481 " </menubar>\n"
482 "</ui>\n"
485 plugin_gui_window::plugin_gui_window(main_window_iface *_main)
487 toplevel = NULL;
488 ui_mgr = NULL;
489 std_actions = NULL;
490 builtin_preset_actions = NULL;
491 user_preset_actions = NULL;
492 command_actions = NULL;
493 main = _main;
494 assert(main);
497 string plugin_gui_window::make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch)
499 string preset_xml = string(general_preset_pre_xml) + (builtin ? builtin_preset_pre_xml : user_preset_pre_xml);
500 preset_vector &pvec = (builtin ? get_builtin_presets() : get_user_presets()).presets;
501 GtkActionGroup *preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
502 for (unsigned int i = 0; i < pvec.size(); i++)
504 if (pvec[i].plugin != gui->effect_name)
505 continue;
506 stringstream ss;
507 ss << (builtin ? "builtin_preset" : "user_preset") << i;
508 preset_xml += " <menuitem name=\"" + pvec[i].name+"\" action=\""+ss.str()+"\"/>\n";
509 if (ch != ' ' && ++ch == ':')
510 ch = 'A';
511 if (ch > 'Z')
512 ch = ' ';
514 string sv = ss.str();
515 string prefix = ch == ' ' ? string() : string("_")+ch+" ";
516 string name = prefix + pvec[i].name;
517 GtkActionEntry ae = { sv.c_str(), NULL, name.c_str(), NULL, NULL, (GCallback)activate_preset };
518 gtk_action_group_add_actions_full(preset_actions, &ae, 1, (gpointer)new activate_preset_params(gui, i, builtin), action_destroy_notify);
520 preset_xml += preset_post_xml;
521 return preset_xml;
524 string plugin_gui_window::make_gui_command_list(GtkActionGroup *grp)
526 string command_xml = command_pre_xml;
527 plugin_command_info *ci = gui->plugin->get_commands();
528 if (!ci)
529 return "";
530 for(int i = 0; ci->name; i++, ci++)
532 stringstream ss;
533 ss << " <menuitem name=\"" << ci->name << "\" action=\"" << ci->label << "\"/>\n";
535 GtkActionEntry ae = { ci->label, NULL, ci->name, NULL, ci->description, (GCallback)activate_command };
536 gtk_action_group_add_actions_full(command_actions, &ae, 1, (gpointer)new activate_command_params(gui, i), action_destroy_notify);
537 command_xml += ss.str();
539 command_xml += command_post_xml;
540 return command_xml;
543 void plugin_gui_window::fill_gui_presets(bool builtin, char &ch)
545 GtkActionGroup *&preset_actions = builtin ? builtin_preset_actions : user_preset_actions;
546 if(preset_actions) {
547 gtk_ui_manager_remove_action_group(ui_mgr, preset_actions);
548 preset_actions = NULL;
551 if (builtin)
552 builtin_preset_actions = gtk_action_group_new("builtin_presets");
553 else
554 user_preset_actions = gtk_action_group_new("user_presets");
555 string preset_xml = make_gui_preset_list(preset_actions, builtin, ch);
556 gtk_ui_manager_insert_action_group(ui_mgr, preset_actions, 0);
557 GError *error = NULL;
558 gtk_ui_manager_add_ui_from_string(ui_mgr, preset_xml.c_str(), -1, &error);
561 gboolean plugin_gui_window::on_idle(void *data)
563 plugin_gui_window *self = (plugin_gui_window *)data;
564 self->gui->on_idle();
565 return TRUE;
568 void plugin_gui_window::create(plugin_ctl_iface *_jh, const char *title, const char *effect)
570 toplevel = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
571 gtk_window_set_default_icon_name("calf");
572 gtk_widget_set_name(GTK_WIDGET(toplevel), "calf-plugin");
573 gtk_window_set_type_hint(toplevel, GDK_WINDOW_TYPE_HINT_DIALOG);
574 GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(false, 5));
576 GtkRequisition req, req2;
577 gtk_window_set_title(GTK_WINDOW (toplevel), title);
578 gtk_container_add(GTK_CONTAINER(toplevel), GTK_WIDGET(vbox));
580 gui = new plugin_gui(this);
581 gui->effect_name = effect;
583 ui_mgr = gtk_ui_manager_new();
584 std_actions = gtk_action_group_new("default");
585 gtk_action_group_add_actions(std_actions, actions, sizeof(actions)/sizeof(actions[0]), this);
586 GError *error = NULL;
587 gtk_ui_manager_insert_action_group(ui_mgr, std_actions, 0);
588 gtk_ui_manager_add_ui_from_string(ui_mgr, ui_xml, -1, &error);
590 command_actions = gtk_action_group_new("commands");
592 char ch = '0';
593 fill_gui_presets(true, ch);
594 fill_gui_presets(false, ch);
596 gtk_box_pack_start(GTK_BOX(vbox), gtk_ui_manager_get_widget(ui_mgr, "/ui/menubar"), false, false, 0);
598 // determine size without content
599 gtk_widget_show_all(GTK_WIDGET(vbox));
600 gtk_widget_size_request(GTK_WIDGET(vbox), &req2);
601 // printf("size request %dx%d\n", req2.width, req2.height);
603 GtkWidget *container;
604 const char *xml = _jh->get_gui_xml();
605 assert(xml);
606 container = gui->create_from_xml(_jh, xml);
608 string command_xml = make_gui_command_list(command_actions);
609 gtk_ui_manager_insert_action_group(ui_mgr, command_actions, 0);
610 gtk_ui_manager_add_ui_from_string(ui_mgr, command_xml.c_str(), -1, &error);
612 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
613 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
614 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
615 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), container);
617 gtk_box_pack_start(GTK_BOX(vbox), sw, true, true, 0);
619 gtk_widget_show_all(GTK_WIDGET(sw));
620 gtk_widget_size_request(container, &req);
621 int wx = max(req.width + 10, req2.width);
622 int wy = req.height + req2.height + 10;
623 // printf("size request %dx%d\n", req.width, req.height);
624 // printf("resize to %dx%d\n", max(req.width + 10, req2.width), req.height + req2.height + 10);
625 gtk_window_set_default_size(GTK_WINDOW(toplevel), wx, wy);
626 gtk_window_resize(GTK_WINDOW(toplevel), wx, wy);
627 //gtk_widget_set_size_request(GTK_WIDGET(toplevel), max(req.width + 10, req2.width), req.height + req2.height + 10);
628 // printf("size set %dx%d\n", wx, wy);
629 // gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(sw), GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, req.height, 20, 100, 100)));
630 gtk_signal_connect (GTK_OBJECT (toplevel), "destroy", G_CALLBACK (window_destroyed), (plugin_gui_window *)this);
631 main->set_window(gui->plugin, this);
633 source_id = g_timeout_add_full(G_PRIORITY_LOW, 1000/30, on_idle, this, NULL); // 30 fps should be enough for everybody
634 gtk_ui_manager_ensure_update(ui_mgr);
635 gui->plugin->send_configures(gui);
638 void plugin_gui_window::close()
640 if (source_id)
641 g_source_remove(source_id);
642 source_id = 0;
643 gtk_widget_destroy(GTK_WIDGET(toplevel));
646 plugin_gui_window::~plugin_gui_window()
648 if (source_id)
649 g_source_remove(source_id);
650 main->set_window(gui->plugin, NULL);
651 delete gui;
654 void calf_plugins::activate_command(GtkAction *action, activate_command_params *params)
656 plugin_gui *gui = params->gui;
657 gui->plugin->execute(params->function_idx);
658 gui->refresh();