Vocoder: toggle switch for detection LEDs
[calf.git] / src / gui.cpp
blob64b8e190def0073b45daca4717b4134caff01334
1 /* Calf DSP Library
2 * GUI functions for a plugin.
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 /******************************** GUI proper ********************************/
34 plugin_gui::plugin_gui(plugin_gui_window *_window)
35 : last_status_serial_no(0)
36 , window(_window)
38 ignore_stack = 0;
39 top_container = NULL;
40 current_control = NULL;
41 param_count = 0;
42 container = NULL;
43 effect_name = NULL;
44 preset_access = new gui_preset_access(this);
47 param_control *plugin_gui::create_control_from_xml(const char *element, const char *attributes[])
49 if (!strcmp(element, "knob"))
50 return new knob_param_control;
51 if (!strcmp(element, "hscale"))
52 return new hscale_param_control;
53 if (!strcmp(element, "vscale"))
54 return new vscale_param_control;
55 if (!strcmp(element, "combo"))
56 return new combo_box_param_control;
57 if (!strcmp(element, "check"))
58 return new check_param_control;
59 if (!strcmp(element, "radio"))
60 return new radio_param_control;
61 if (!strcmp(element, "toggle"))
62 return new toggle_param_control;
63 if (!strcmp(element, "tap"))
64 return new tap_button_param_control;
65 if (!strcmp(element, "spin"))
66 return new spin_param_control;
67 if (!strcmp(element, "button"))
68 return new button_param_control;
69 if (!strcmp(element, "label"))
70 return new label_param_control;
71 if (!strcmp(element, "value"))
72 return new value_param_control;
73 if (!strcmp(element, "vumeter"))
74 return new vumeter_param_control;
75 if (!strcmp(element, "line-graph"))
76 return new line_graph_param_control;
77 if (!strcmp(element, "phase-graph"))
78 return new phase_graph_param_control;
79 if (!strcmp(element, "keyboard"))
80 return new keyboard_param_control;
81 if (!strcmp(element, "curve"))
82 return new curve_param_control;
83 if (!strcmp(element, "led"))
84 return new led_param_control;
85 if (!strcmp(element, "tube"))
86 return new tube_param_control;
87 if (!strcmp(element, "entry"))
88 return new entry_param_control;
89 if (!strcmp(element, "filechooser"))
90 return new filechooser_param_control;
91 if (!strcmp(element, "listview"))
92 return new listview_param_control;
93 if (!strcmp(element, "notebook"))
94 return new notebook_param_control;
95 return NULL;
98 control_container *plugin_gui::create_container_from_xml(const char *element, const char *attributes[])
100 if (!strcmp(element, "table"))
101 return new table_container;
102 if (!strcmp(element, "vbox"))
103 return new vbox_container;
104 if (!strcmp(element, "hbox"))
105 return new hbox_container;
106 if (!strcmp(element, "align"))
107 return new alignment_container;
108 if (!strcmp(element, "frame"))
109 return new frame_container;
110 if (!strcmp(element, "scrolled"))
111 return new scrolled_container;
112 return NULL;
115 void plugin_gui::xml_element_start(void *data, const char *element, const char *attributes[])
117 plugin_gui *gui = (plugin_gui *)data;
118 gui->xml_element_start(element, attributes);
121 int plugin_gui::get_param_no_by_name(string param_name)
123 int param_no = -1;
124 map<string, int>::iterator it = param_name_map.find(param_name);
125 if (it == param_name_map.end())
126 g_error("Unknown parameter %s", param_name.c_str());
127 else
128 param_no = it->second;
130 return param_no;
133 void plugin_gui::xml_element_start(const char *element, const char *attributes[])
135 if (ignore_stack) {
136 ignore_stack++;
137 return;
139 control_base::xml_attribute_map xam;
140 while(*attributes)
142 xam[attributes[0]] = attributes[1];
143 attributes += 2;
146 if (!strcmp(element, "if"))
148 if (!xam.count("cond") || xam["cond"].empty())
150 g_error("Incorrect <if cond=\"[!]symbol\"> element");
152 string cond = xam["cond"];
153 bool state = true;
154 if (cond.substr(0, 1) == "!") {
155 state = false;
156 cond.erase(0, 1);
158 if (window->environment->check_condition(cond.c_str()) == state)
159 return;
160 ignore_stack = 1;
161 return;
163 control_container *cc = create_container_from_xml(element, attributes);
164 if (cc != NULL)
166 cc->attribs = xam;
167 cc->create(this, element, xam);
168 cc->set_std_properties();
169 gtk_container_set_border_width(cc->container, cc->get_int("border"));
171 container_stack.push_back(cc);
172 current_control = NULL;
173 return;
175 if (!container_stack.empty())
177 current_control = create_control_from_xml(element, attributes);
178 if (current_control)
180 current_control->control_name = element;
181 current_control->attribs = xam;
182 int param_no = -1;
183 if (xam.count("param"))
185 param_no = get_param_no_by_name(xam["param"]);
187 if (param_no != -1)
188 current_control->param_variable = plugin->get_metadata_iface()->get_param_props(param_no)->short_name;
189 current_control->create(this, param_no);
190 current_control->set_std_properties();
191 current_control->init_xml(element);
192 current_control->set();
193 current_control->hook_params();
194 current_control->add_context_menu_handler();
195 if (current_control->is_container()) {
196 gtk_container_set_border_width(current_control->container, current_control->get_int("border"));
197 container_stack.push_back(current_control);
199 return;
202 g_error("Unxpected element %s in GUI definition\n", element);
205 void plugin_gui::xml_element_end(void *data, const char *element)
207 plugin_gui *gui = (plugin_gui *)data;
208 if (gui->ignore_stack) {
209 gui->ignore_stack--;
210 return;
212 if (!strcmp(element, "if"))
214 return;
216 if (gui->current_control and !gui->current_control->is_container())
218 (*gui->container_stack.rbegin())->add(gui->current_control->widget, gui->current_control);
219 gui->current_control = NULL;
220 return;
222 unsigned int ss = gui->container_stack.size();
223 if (ss > 1) {
224 gui->container_stack[ss - 2]->add(GTK_WIDGET(gui->container_stack[ss - 1]->container), gui->container_stack[ss - 1]);
226 else
227 gui->top_container = gui->container_stack[0];
228 gui->container_stack.pop_back();
229 gui->current_control = NULL;
233 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
235 current_control = NULL;
236 top_container = NULL;
237 parser = XML_ParserCreate("UTF-8");
238 plugin = _plugin;
239 container_stack.clear();
240 ignore_stack = 0;
242 param_name_map.clear();
243 read_serials.clear();
244 int size = plugin->get_metadata_iface()->get_param_count();
245 read_serials.resize(size);
246 for (int i = 0; i < size; i++)
247 param_name_map[plugin->get_metadata_iface()->get_param_props(i)->short_name] = i;
249 XML_SetUserData(parser, this);
250 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
251 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
252 if (status == XML_STATUS_ERROR)
254 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
257 XML_ParserFree(parser);
258 last_status_serial_no = plugin->send_status_updates(this, 0);
259 GtkWidget *eventbox = gtk_event_box_new();
260 GtkWidget *decoTable = gtk_table_new(3, 1, FALSE);
262 // decorations
263 // overall background
264 //GtkWidget *bgImg = gtk_image_new_from_file(PKGLIBDIR "/background.png");
265 //gtk_widget_set_size_request(GTK_WIDGET(bgImg), 1, 1);
267 // images for left side
268 GtkWidget *nwImg = gtk_image_new_from_file(PKGLIBDIR "/side_nw.png");
269 GtkWidget *swImg = gtk_image_new_from_file(PKGLIBDIR "/side_sw.png");
270 GtkWidget *wImg = gtk_image_new_from_file(PKGLIBDIR "/side_w.png");
271 gtk_widget_set_size_request(GTK_WIDGET(wImg), 56, 1);
273 // images for right side
274 GtkWidget *neImg = gtk_image_new_from_file(PKGLIBDIR "/side_ne.png");
275 GtkWidget *seImg = gtk_image_new_from_file(PKGLIBDIR "/side_se.png");
276 GtkWidget *eImg = gtk_image_new_from_file(PKGLIBDIR "/side_e.png");
277 GtkWidget *logoImg = gtk_image_new_from_file(PKGLIBDIR "/side_e_logo.png");
278 gtk_widget_set_size_request(GTK_WIDGET(eImg), 56, 1);
280 // pack left box
281 leftBox = gtk_vbox_new(FALSE, 0);
282 gtk_box_pack_start(GTK_BOX(leftBox), GTK_WIDGET(nwImg), FALSE, FALSE, 0);
283 gtk_box_pack_start(GTK_BOX(leftBox), GTK_WIDGET(wImg), TRUE, TRUE, 0);
284 gtk_box_pack_end(GTK_BOX(leftBox), GTK_WIDGET(swImg), FALSE, FALSE, 0);
286 // pack right box
287 rightBox = gtk_vbox_new(FALSE, 0);
288 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(neImg), FALSE, FALSE, 0);
289 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(eImg), TRUE, TRUE, 0);
290 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(logoImg), FALSE, FALSE, 0);
291 gtk_box_pack_end(GTK_BOX(rightBox), GTK_WIDGET(seImg), FALSE, FALSE, 0);
293 //gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(bgImg), 0, 2, 0, 2, (GtkAttachOptions)(GTK_EXPAND | GTK_SHRINK | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_SHRINK | GTK_FILL), 0, 0);
294 gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(leftBox), 0, 1, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 0, 0);
295 gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(rightBox), 2, 3, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 0, 0);
297 gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(top_container->container), 1, 2, 0, 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 15, 5);
298 gtk_container_add( GTK_CONTAINER(eventbox), decoTable );
299 gtk_widget_set_name( GTK_WIDGET(eventbox), "Calf-Plugin" );
301 // create window with viewport
302 // GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
303 // gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
304 // gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
305 // gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(decoTable));
307 return GTK_WIDGET(eventbox);
310 void plugin_gui::send_configure(const char *key, const char *value)
312 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
313 for (unsigned int i = 0; i < params.size(); i++)
315 assert(params[i] != NULL);
316 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
317 if (sci)
318 sci->send_configure(key, value);
322 void plugin_gui::send_status(const char *key, const char *value)
324 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
325 for (unsigned int i = 0; i < params.size(); i++)
327 assert(params[i] != NULL);
328 send_updates_iface *sui = dynamic_cast<send_updates_iface *>(params[i]);
329 if (sui)
330 sui->send_status(key, value);
334 void plugin_gui::on_idle()
336 set<unsigned> changed;
337 for (unsigned i = 0; i < read_serials.size(); i++)
339 int write_serial = plugin->get_write_serial(i);
340 if (write_serial - read_serials[i] > 0)
342 read_serials[i] = write_serial;
343 changed.insert(i);
346 for (unsigned i = 0; i < params.size(); i++)
348 int param_no = params[i]->param_no;
349 if (param_no != -1)
351 const parameter_properties &props = *plugin->get_metadata_iface()->get_param_props(param_no);
352 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
353 if (is_output || (param_no != -1 && changed.count(param_no)))
354 params[i]->set();
356 params[i]->on_idle();
358 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
359 // XXXKF iterate over par2ctl, too...
362 void plugin_gui::refresh()
364 for (unsigned int i = 0; i < params.size(); i++)
365 params[i]->set();
366 plugin->send_configures(this);
367 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
370 void plugin_gui::refresh(int param_no, param_control *originator)
372 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
373 while(it != par2ctl.end() && it->first == param_no)
375 if (it->second != originator)
376 it->second->set();
377 it++;
381 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
383 plugin->set_param_value(param_no, value);
384 refresh(param_no);
387 /// Get a radio button group (if it exists) for a parameter
388 GSList *plugin_gui::get_radio_group(int param)
390 map<int, GSList *>::const_iterator i = param_radio_groups.find(param);
391 if (i == param_radio_groups.end())
392 return NULL;
393 else
394 return i->second;
397 /// Set a radio button group for a parameter
398 void plugin_gui::set_radio_group(int param, GSList *group)
400 param_radio_groups[param] = group;
403 void plugin_gui::show_rack_ears(bool show)
405 // if hidden, add a no-show-all attribute so that LV2 host doesn't accidentally override
406 // the setting by doing a show_all on the outermost container
407 gtk_widget_set_no_show_all(leftBox, !show);
408 gtk_widget_set_no_show_all(rightBox, !show);
409 if (show)
411 gtk_widget_show(leftBox);
412 gtk_widget_show(rightBox);
414 else
416 gtk_widget_hide(leftBox);
417 gtk_widget_hide(rightBox);
421 void plugin_gui::on_automation_add(GtkWidget *widget, void *user_data)
423 plugin_gui *self = (plugin_gui *)user_data;
424 self->plugin->add_automation(self->context_menu_last_designator, automation_range(0, 1, self->context_menu_param_no));
427 void plugin_gui::on_automation_delete(GtkWidget *widget, void *user_data)
429 automation_menu_entry *ame = (automation_menu_entry *)user_data;
430 ame->gui->plugin->delete_automation(ame->source, ame->gui->context_menu_param_no);
433 void plugin_gui::on_automation_set_lower_or_upper(automation_menu_entry *ame, bool is_upper)
435 const parameter_properties *props = plugin->get_metadata_iface()->get_param_props(context_menu_param_no);
436 float mapped = props->to_01(plugin->get_param_value(context_menu_param_no));
438 automation_map mappings;
439 plugin->get_automation(context_menu_param_no, mappings);
440 automation_map::const_iterator i = mappings.find(ame->source);
441 if (i != mappings.end())
443 if (is_upper)
444 plugin->add_automation(context_menu_last_designator, automation_range(i->second.min_value, mapped, context_menu_param_no));
445 else
446 plugin->add_automation(context_menu_last_designator, automation_range(mapped, i->second.max_value, context_menu_param_no));
450 void plugin_gui::on_automation_set_lower(GtkWidget *widget, void *user_data)
452 automation_menu_entry *ame = (automation_menu_entry *)user_data;
453 ame->gui->on_automation_set_lower_or_upper(ame, false);
456 void plugin_gui::on_automation_set_upper(GtkWidget *widget, void *user_data)
458 automation_menu_entry *ame = (automation_menu_entry *)user_data;
459 ame->gui->on_automation_set_lower_or_upper(ame, true);
462 void plugin_gui::cleanup_automation_entries()
464 for (int i = 0; i < (int)automation_menu_callback_data.size(); i++)
465 delete automation_menu_callback_data[i];
466 automation_menu_callback_data.clear();
469 void plugin_gui::on_control_popup(param_control *ctl, int param_no)
471 cleanup_automation_entries();
472 if (param_no == -1)
473 return;
474 context_menu_param_no = param_no;
475 GtkWidget *menu = gtk_menu_new();
477 multimap<uint32_t, automation_range> mappings;
478 plugin->get_automation(param_no, mappings);
480 context_menu_last_designator = plugin->get_last_automation_source();
482 GtkWidget *item;
483 if (context_menu_last_designator != 0xFFFFFFFF)
485 stringstream ss;
486 ss << "_Bind to: Ch" << (1 + (context_menu_last_designator >> 8)) << ", CC#" << (context_menu_last_designator & 127);
487 item = gtk_menu_item_new_with_mnemonic(ss.str().c_str());
488 g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
489 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
491 else
493 item = gtk_menu_item_new_with_label("Send CC to automate");
494 gtk_widget_set_sensitive(item, FALSE);
495 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
498 for(multimap<uint32_t, automation_range>::const_iterator i = mappings.begin(); i != mappings.end(); i++)
500 automation_menu_entry *ame = new automation_menu_entry(this, i->first);
501 automation_menu_callback_data.push_back(ame);
502 stringstream ss;
503 ss << "Mapping: Ch" << (1 + (i->first >> 8)) << ", CC#" << (i->first & 127);
504 item = gtk_menu_item_new_with_label(ss.str().c_str());
505 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
507 GtkWidget *submenu = gtk_menu_new();
508 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
510 item = gtk_menu_item_new_with_mnemonic("_Delete");
511 g_signal_connect(item, "activate", (GCallback)on_automation_delete, ame);
512 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
514 item = gtk_menu_item_new_with_mnemonic("Set _lower limit");
515 g_signal_connect(item, "activate", (GCallback)on_automation_set_lower, ame);
516 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
518 item = gtk_menu_item_new_with_mnemonic("Set _upper limit");
519 g_signal_connect(item, "activate", (GCallback)on_automation_set_upper, ame);
520 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
521 //g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
525 gtk_widget_show_all(menu);
526 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, gtk_get_current_event_time());
529 plugin_gui::~plugin_gui()
531 cleanup_automation_entries();
532 delete preset_access;
533 for (std::vector<param_control *>::iterator i = params.begin(); i != params.end(); i++)
535 delete *i;
539 /***************************** GUI environment ********************************************/
541 bool window_update_controller::check_redraw(GtkWidget *toplevel)
543 GdkWindow *gdkwin = gtk_widget_get_window(toplevel);
544 if (!gdkwin)
545 return false;
547 if (!gdk_window_is_viewable(gdkwin))
548 return false;
549 GdkWindowState state = gdk_window_get_state(gdkwin);
550 if (state & GDK_WINDOW_STATE_ICONIFIED)
552 ++refresh_counter;
553 if (refresh_counter & 15)
554 return false;
556 return true;
559 /***************************** GUI environment ********************************************/
561 gui_environment::gui_environment()
563 keyfile = g_key_file_new();
565 gchar *fn = g_build_filename(getenv("HOME"), ".calfrc", NULL);
566 string filename = fn;
567 g_free(fn);
568 g_key_file_load_from_file(keyfile, filename.c_str(), (GKeyFileFlags)(G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS), NULL);
570 config_db = new calf_utils::gkeyfile_config_db(keyfile, filename.c_str(), "gui");
571 gui_config.load(config_db);
574 gui_environment::~gui_environment()
576 delete config_db;
577 if (keyfile)
578 g_key_file_free(keyfile);