RingModulator: meter_in with MeterScale
[calf.git] / src / gui.cpp
blobfd219466c9a86abdd8ba16e58e6b58ecb96e1f35
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 param_count = 0;
41 container = NULL;
42 effect_name = NULL;
43 preset_access = new gui_preset_access(this);
44 optclosed = false;
45 optwidget = NULL;
46 optwindow = NULL;
47 opttitle = NULL;
50 control_base *plugin_gui::create_widget_from_xml(const char *element, const char *attributes[])
52 if (!strcmp(element, "knob"))
53 return new knob_param_control;
54 if (!strcmp(element, "hscale"))
55 return new hscale_param_control;
56 if (!strcmp(element, "vscale"))
57 return new vscale_param_control;
58 if (!strcmp(element, "combo"))
59 return new combo_box_param_control;
60 if (!strcmp(element, "check"))
61 return new check_param_control;
62 if (!strcmp(element, "radio"))
63 return new radio_param_control;
64 if (!strcmp(element, "toggle"))
65 return new toggle_param_control;
66 if (!strcmp(element, "tap"))
67 return new tap_button_param_control;
68 if (!strcmp(element, "spin"))
69 return new spin_param_control;
70 if (!strcmp(element, "button"))
71 return new button_param_control;
72 if (!strcmp(element, "label"))
73 return new label_param_control;
74 if (!strcmp(element, "value"))
75 return new value_param_control;
76 if (!strcmp(element, "vumeter"))
77 return new vumeter_param_control;
78 if (!strcmp(element, "line-graph"))
79 return new line_graph_param_control;
80 if (!strcmp(element, "phase-graph"))
81 return new phase_graph_param_control;
82 if (!strcmp(element, "keyboard"))
83 return new keyboard_param_control;
84 if (!strcmp(element, "curve"))
85 return new curve_param_control;
86 if (!strcmp(element, "meterscale"))
87 return new meter_scale_param_control;
88 if (!strcmp(element, "led"))
89 return new led_param_control;
90 if (!strcmp(element, "tube"))
91 return new tube_param_control;
92 if (!strcmp(element, "entry"))
93 return new entry_param_control;
94 if (!strcmp(element, "filechooser"))
95 return new filechooser_param_control;
96 if (!strcmp(element, "listview"))
97 return new listview_param_control;
98 if (!strcmp(element, "notebook"))
99 return new notebook_param_control;
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_base *cc = create_widget_from_xml(element, attributes);
164 if (cc == NULL)
165 g_error("Unxpected element %s in GUI definition\n", element);
167 cc->attribs = xam;
168 cc->create(this);
169 stack.push_back(cc);
172 void plugin_gui::xml_element_end(void *data, const char *element)
174 plugin_gui *gui = (plugin_gui *)data;
175 if (gui->ignore_stack) {
176 gui->ignore_stack--;
177 return;
179 if (!strcmp(element, "if"))
180 return;
182 control_base *control = gui->stack.back();
183 control->created();
185 gui->stack.pop_back();
186 if (gui->stack.empty())
188 gui->top_container = control;
189 gtk_widget_show_all(control->widget);
191 else
192 gui->stack.back()->add(control);
196 GtkWidget *plugin_gui::create_from_xml(plugin_ctl_iface *_plugin, const char *xml)
198 top_container = NULL;
199 parser = XML_ParserCreate("UTF-8");
200 plugin = _plugin;
201 stack.clear();
202 ignore_stack = 0;
204 param_name_map.clear();
205 read_serials.clear();
206 int size = plugin->get_metadata_iface()->get_param_count();
207 read_serials.resize(size);
208 for (int i = 0; i < size; i++)
209 param_name_map[plugin->get_metadata_iface()->get_param_props(i)->short_name] = i;
211 XML_SetUserData(parser, this);
212 XML_SetElementHandler(parser, xml_element_start, xml_element_end);
213 XML_Status status = XML_Parse(parser, xml, strlen(xml), 1);
214 if (status == XML_STATUS_ERROR)
216 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser)));
219 XML_ParserFree(parser);
220 last_status_serial_no = plugin->send_status_updates(this, 0);
221 GtkWidget *eventbox = gtk_event_box_new();
222 GtkWidget *decoTable = gtk_table_new(3, 1, FALSE);
224 // decorations
225 // overall background
226 //GtkWidget *bgImg = gtk_image_new_from_file(PKGLIBDIR "/background.png");
227 //gtk_widget_set_size_request(GTK_WIDGET(bgImg), 1, 1);
229 // images for left side
230 GtkWidget *nwImg = gtk_image_new_from_file(PKGLIBDIR "/side_nw.png");
231 GtkWidget *swImg = gtk_image_new_from_file(PKGLIBDIR "/side_sw.png");
232 GtkWidget *wImg = gtk_image_new_from_file(PKGLIBDIR "/side_w.png");
233 gtk_widget_set_size_request(GTK_WIDGET(wImg), 56, 1);
235 // images for right side
236 GtkWidget *neImg = gtk_image_new_from_file(PKGLIBDIR "/side_ne.png");
237 GtkWidget *seImg = gtk_image_new_from_file(PKGLIBDIR "/side_se.png");
238 GtkWidget *eImg = gtk_image_new_from_file(PKGLIBDIR "/side_e.png");
239 GtkWidget *logoImg = gtk_image_new_from_file(PKGLIBDIR "/side_e_logo.png");
240 gtk_widget_set_size_request(GTK_WIDGET(eImg), 56, 1);
242 // pack left box
243 leftBox = gtk_vbox_new(FALSE, 0);
244 gtk_box_pack_start(GTK_BOX(leftBox), GTK_WIDGET(nwImg), FALSE, FALSE, 0);
245 gtk_box_pack_start(GTK_BOX(leftBox), GTK_WIDGET(wImg), TRUE, TRUE, 0);
246 gtk_box_pack_end(GTK_BOX(leftBox), GTK_WIDGET(swImg), FALSE, FALSE, 0);
248 // pack right box
249 rightBox = gtk_vbox_new(FALSE, 0);
250 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(neImg), FALSE, FALSE, 0);
251 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(eImg), TRUE, TRUE, 0);
252 gtk_box_pack_start(GTK_BOX(rightBox), GTK_WIDGET(logoImg), FALSE, FALSE, 0);
253 gtk_box_pack_end(GTK_BOX(rightBox), GTK_WIDGET(seImg), FALSE, FALSE, 0);
255 //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);
256 gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(leftBox), 0, 1, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 0, 0);
257 gtk_table_attach(GTK_TABLE(decoTable), GTK_WIDGET(rightBox), 2, 3, 0, 1, (GtkAttachOptions)(0), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 0, 0);
259 gtk_table_attach(GTK_TABLE(decoTable), top_container->widget, 1, 2, 0, 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), 15, 5);
260 gtk_container_add( GTK_CONTAINER(eventbox), decoTable );
261 gtk_widget_set_name( GTK_WIDGET(eventbox), "Calf-Plugin" );
263 // create window with viewport
264 // GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
265 // gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
266 // gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
267 // gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(decoTable));
269 return GTK_WIDGET(eventbox);
272 void plugin_gui::send_configure(const char *key, const char *value)
274 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
275 for (unsigned int i = 0; i < params.size(); i++)
277 assert(params[i] != NULL);
278 send_configure_iface *sci = dynamic_cast<send_configure_iface *>(params[i]);
279 if (sci)
280 sci->send_configure(key, value);
284 void plugin_gui::send_status(const char *key, const char *value)
286 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
287 for (unsigned int i = 0; i < params.size(); i++)
289 assert(params[i] != NULL);
290 send_updates_iface *sui = dynamic_cast<send_updates_iface *>(params[i]);
291 if (sui)
292 sui->send_status(key, value);
296 void plugin_gui::remove_param_ctl(int param, param_control *ctl)
298 std::multimap<int, param_control *>::iterator it = par2ctl.find(param);
299 while(it != par2ctl.end() && it->first == param)
301 if (it->second == ctl)
303 std::multimap<int, param_control *>::iterator orig = it;
304 ++orig;
305 par2ctl.erase(it, orig);
306 it = orig;
308 else
309 ++it;
311 unsigned last = params.size() - 1;
312 for (unsigned i = 0; i < params.size(); ++i)
314 if (params[i] == ctl)
316 if (i != last)
317 std::swap(params[i], params[last]);
318 params.erase(params.begin() + last, params.end());
323 void plugin_gui::on_idle()
325 set<unsigned> changed;
326 for (unsigned i = 0; i < read_serials.size(); i++)
328 int write_serial = plugin->get_write_serial(i);
329 if (write_serial - read_serials[i] > 0)
331 read_serials[i] = write_serial;
332 changed.insert(i);
335 for (unsigned i = 0; i < params.size(); i++)
337 int param_no = params[i]->param_no;
338 if (param_no != -1)
340 const parameter_properties &props = *plugin->get_metadata_iface()->get_param_props(param_no);
341 bool is_output = (props.flags & PF_PROP_OUTPUT) != 0;
342 if (is_output || (param_no != -1 && changed.count(param_no)))
343 params[i]->set();
345 params[i]->on_idle();
347 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
348 // XXXKF iterate over par2ctl, too...
351 void plugin_gui::refresh()
353 for (unsigned int i = 0; i < params.size(); i++)
354 params[i]->set();
355 plugin->send_configures(this);
356 last_status_serial_no = plugin->send_status_updates(this, last_status_serial_no);
359 void plugin_gui::refresh(int param_no, param_control *originator)
361 std::multimap<int, param_control *>::iterator it = par2ctl.find(param_no);
362 while(it != par2ctl.end() && it->first == param_no)
364 if (it->second != originator)
365 it->second->set();
366 ++it;
370 void plugin_gui::set_param_value(int param_no, float value, param_control *originator)
372 plugin->set_param_value(param_no, value);
373 refresh(param_no);
376 /// Get a radio button group (if it exists) for a parameter
377 GSList *plugin_gui::get_radio_group(int param)
379 map<int, GSList *>::const_iterator i = param_radio_groups.find(param);
380 if (i == param_radio_groups.end())
381 return NULL;
382 else
383 return i->second;
386 /// Set a radio button group for a parameter
387 void plugin_gui::set_radio_group(int param, GSList *group)
389 param_radio_groups[param] = group;
392 void plugin_gui::show_rack_ears(bool show)
394 // if hidden, add a no-show-all attribute so that LV2 host doesn't accidentally override
395 // the setting by doing a show_all on the outermost container
396 gtk_widget_set_no_show_all(leftBox, !show);
397 gtk_widget_set_no_show_all(rightBox, !show);
398 if (show)
400 gtk_widget_show(leftBox);
401 gtk_widget_show(rightBox);
403 else
405 gtk_widget_hide(leftBox);
406 gtk_widget_hide(rightBox);
410 void plugin_gui::on_automation_add(GtkWidget *widget, void *user_data)
412 plugin_gui *self = (plugin_gui *)user_data;
413 self->plugin->add_automation(self->context_menu_last_designator, automation_range(0, 1, self->context_menu_param_no));
416 void plugin_gui::on_automation_delete(GtkWidget *widget, void *user_data)
418 automation_menu_entry *ame = (automation_menu_entry *)user_data;
419 ame->gui->plugin->delete_automation(ame->source, ame->gui->context_menu_param_no);
422 void plugin_gui::on_automation_set_lower_or_upper(automation_menu_entry *ame, bool is_upper)
424 const parameter_properties *props = plugin->get_metadata_iface()->get_param_props(context_menu_param_no);
425 float mapped = props->to_01(plugin->get_param_value(context_menu_param_no));
427 automation_map mappings;
428 plugin->get_automation(context_menu_param_no, mappings);
429 automation_map::const_iterator i = mappings.find(ame->source);
430 if (i != mappings.end())
432 if (is_upper)
433 plugin->add_automation(context_menu_last_designator, automation_range(i->second.min_value, mapped, context_menu_param_no));
434 else
435 plugin->add_automation(context_menu_last_designator, automation_range(mapped, i->second.max_value, context_menu_param_no));
439 void plugin_gui::on_automation_set_lower(GtkWidget *widget, void *user_data)
441 automation_menu_entry *ame = (automation_menu_entry *)user_data;
442 ame->gui->on_automation_set_lower_or_upper(ame, false);
445 void plugin_gui::on_automation_set_upper(GtkWidget *widget, void *user_data)
447 automation_menu_entry *ame = (automation_menu_entry *)user_data;
448 ame->gui->on_automation_set_lower_or_upper(ame, true);
451 void plugin_gui::cleanup_automation_entries()
453 for (int i = 0; i < (int)automation_menu_callback_data.size(); i++)
454 delete automation_menu_callback_data[i];
455 automation_menu_callback_data.clear();
458 void plugin_gui::on_control_popup(param_control *ctl, int param_no)
460 cleanup_automation_entries();
461 if (param_no == -1)
462 return;
463 context_menu_param_no = param_no;
464 GtkWidget *menu = gtk_menu_new();
466 multimap<uint32_t, automation_range> mappings;
467 plugin->get_automation(param_no, mappings);
469 context_menu_last_designator = plugin->get_last_automation_source();
471 GtkWidget *item;
472 if (context_menu_last_designator != 0xFFFFFFFF)
474 stringstream ss;
475 ss << "_Bind to: Ch" << (1 + (context_menu_last_designator >> 8)) << ", CC#" << (context_menu_last_designator & 127);
476 item = gtk_menu_item_new_with_mnemonic(ss.str().c_str());
477 g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
478 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
480 else
482 item = gtk_menu_item_new_with_label("Send CC to automate");
483 gtk_widget_set_sensitive(item, FALSE);
484 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
487 for(multimap<uint32_t, automation_range>::const_iterator i = mappings.begin(); i != mappings.end(); ++i)
489 automation_menu_entry *ame = new automation_menu_entry(this, i->first);
490 automation_menu_callback_data.push_back(ame);
491 stringstream ss;
492 ss << "Mapping: Ch" << (1 + (i->first >> 8)) << ", CC#" << (i->first & 127);
493 item = gtk_menu_item_new_with_label(ss.str().c_str());
494 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
496 GtkWidget *submenu = gtk_menu_new();
497 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
499 item = gtk_menu_item_new_with_mnemonic("_Delete");
500 g_signal_connect(item, "activate", (GCallback)on_automation_delete, ame);
501 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
503 item = gtk_menu_item_new_with_mnemonic("Set _lower limit");
504 g_signal_connect(item, "activate", (GCallback)on_automation_set_lower, ame);
505 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
507 item = gtk_menu_item_new_with_mnemonic("Set _upper limit");
508 g_signal_connect(item, "activate", (GCallback)on_automation_set_upper, ame);
509 gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
510 //g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
514 gtk_widget_show_all(menu);
515 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, gtk_get_current_event_time());
518 void plugin_gui::destroy_child_widgets(GtkWidget *parent)
520 if (parent && GTK_IS_CONTAINER(parent))
522 GList *children = gtk_container_get_children(GTK_CONTAINER(parent));
523 for(GList *p = children; p; p = p->next)
524 gtk_widget_destroy(GTK_WIDGET(p->data));
525 g_list_free(children);
529 plugin_gui::~plugin_gui()
531 cleanup_automation_entries();
532 delete preset_access;
535 /***************************** GUI environment ********************************************/
537 bool window_update_controller::check_redraw(GtkWidget *toplevel)
539 GdkWindow *gdkwin = gtk_widget_get_window(toplevel);
540 if (!gdkwin)
541 return false;
543 if (!gdk_window_is_viewable(gdkwin))
544 return false;
545 GdkWindowState state = gdk_window_get_state(gdkwin);
546 if (state & GDK_WINDOW_STATE_ICONIFIED)
548 ++refresh_counter;
549 if (refresh_counter & 15)
550 return false;
552 return true;
555 /***************************** GUI environment ********************************************/
557 gui_environment::gui_environment()
559 keyfile = g_key_file_new();
561 gchar *fn = g_build_filename(getenv("HOME"), ".calfrc", NULL);
562 string filename = fn;
563 g_free(fn);
564 g_key_file_load_from_file(keyfile, filename.c_str(), (GKeyFileFlags)(G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS), NULL);
566 config_db = new calf_utils::gkeyfile_config_db(keyfile, filename.c_str(), "gui");
567 gui_config.load(config_db);
570 gui_environment::~gui_environment()
572 delete config_db;
573 if (keyfile)
574 g_key_file_free(keyfile);