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>
29 using namespace calf_plugins
;
32 /******************************** GUI proper ********************************/
34 plugin_gui::plugin_gui(plugin_gui_window
*_window
)
35 : last_status_serial_no(0)
43 preset_access
= new gui_preset_access(this);
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
, "tuner"))
83 return new tuner_param_control
;
84 if (!strcmp(element
, "keyboard"))
85 return new keyboard_param_control
;
86 if (!strcmp(element
, "curve"))
87 return new curve_param_control
;
88 if (!strcmp(element
, "meterscale"))
89 return new meter_scale_param_control
;
90 if (!strcmp(element
, "led"))
91 return new led_param_control
;
92 if (!strcmp(element
, "tube"))
93 return new tube_param_control
;
94 if (!strcmp(element
, "entry"))
95 return new entry_param_control
;
96 if (!strcmp(element
, "filechooser"))
97 return new filechooser_param_control
;
98 if (!strcmp(element
, "listview"))
99 return new listview_param_control
;
100 if (!strcmp(element
, "notebook"))
101 return new notebook_param_control
;
102 if (!strcmp(element
, "table"))
103 return new table_container
;
104 if (!strcmp(element
, "vbox"))
105 return new vbox_container
;
106 if (!strcmp(element
, "hbox"))
107 return new hbox_container
;
108 if (!strcmp(element
, "align"))
109 return new alignment_container
;
110 if (!strcmp(element
, "frame"))
111 return new frame_container
;
112 if (!strcmp(element
, "scrolled"))
113 return new scrolled_container
;
117 void plugin_gui::xml_element_start(void *data
, const char *element
, const char *attributes
[])
119 plugin_gui
*gui
= (plugin_gui
*)data
;
120 gui
->xml_element_start(element
, attributes
);
123 int plugin_gui::get_param_no_by_name(string param_name
)
126 map
<string
, int>::iterator it
= param_name_map
.find(param_name
);
127 if (it
== param_name_map
.end())
128 g_error("Unknown parameter %s", param_name
.c_str());
130 param_no
= it
->second
;
135 void plugin_gui::xml_element_start(const char *element
, const char *attributes
[])
141 control_base::xml_attribute_map xam
;
144 xam
[attributes
[0]] = attributes
[1];
148 if (!strcmp(element
, "if"))
150 if (!xam
.count("cond") || xam
["cond"].empty())
152 g_error("Incorrect <if cond=\"[!]symbol\"> element");
154 string cond
= xam
["cond"];
156 if (cond
.substr(0, 1) == "!") {
160 if (window
->environment
->check_condition(cond
.c_str()) == state
)
165 control_base
*cc
= create_widget_from_xml(element
, attributes
);
167 g_error("Unxpected element %s in GUI definition\n", element
);
174 void plugin_gui::xml_element_end(void *data
, const char *element
)
176 plugin_gui
*gui
= (plugin_gui
*)data
;
177 if (gui
->ignore_stack
) {
181 if (!strcmp(element
, "if"))
184 control_base
*control
= gui
->stack
.back();
187 gui
->stack
.pop_back();
188 if (gui
->stack
.empty())
190 gui
->top_container
= control
;
191 gtk_widget_show_all(control
->widget
);
194 gui
->stack
.back()->add(control
);
198 GtkWidget
*plugin_gui::create_from_xml(plugin_ctl_iface
*_plugin
, const char *xml
)
200 top_container
= NULL
;
201 parser
= XML_ParserCreate("UTF-8");
206 param_name_map
.clear();
207 read_serials
.clear();
208 int size
= plugin
->get_metadata_iface()->get_param_count();
209 read_serials
.resize(size
);
210 for (int i
= 0; i
< size
; i
++)
211 param_name_map
[plugin
->get_metadata_iface()->get_param_props(i
)->short_name
] = i
;
213 XML_SetUserData(parser
, this);
214 XML_SetElementHandler(parser
, xml_element_start
, xml_element_end
);
215 XML_Status status
= XML_Parse(parser
, xml
, strlen(xml
), 1);
216 if (status
== XML_STATUS_ERROR
)
218 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser
)));
221 XML_ParserFree(parser
);
222 last_status_serial_no
= plugin
->send_status_updates(this, 0);
223 GtkWidget
*eventbox
= gtk_event_box_new();
224 GtkWidget
*decoTable
= gtk_table_new(3, 1, FALSE
);
227 // overall background
228 //GtkWidget *bgImg = gtk_image_new_from_file(PKGLIBDIR "/background.png");
229 //gtk_widget_set_size_request(GTK_WIDGET(bgImg), 1, 1);
231 // images for left side
232 GtkWidget
*nwImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_nw.png");
233 GtkWidget
*swImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_sw.png");
234 GtkWidget
*wImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_w.png");
235 gtk_widget_set_size_request(GTK_WIDGET(wImg
), 56, 1);
237 // images for right side
238 GtkWidget
*neImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_ne.png");
239 GtkWidget
*seImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_se.png");
240 GtkWidget
*eImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_e.png");
241 GtkWidget
*logoImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_e_logo.png");
242 gtk_widget_set_size_request(GTK_WIDGET(eImg
), 56, 1);
245 leftBox
= gtk_vbox_new(FALSE
, 0);
246 gtk_box_pack_start(GTK_BOX(leftBox
), GTK_WIDGET(nwImg
), FALSE
, FALSE
, 0);
247 gtk_box_pack_start(GTK_BOX(leftBox
), GTK_WIDGET(wImg
), TRUE
, TRUE
, 0);
248 gtk_box_pack_end(GTK_BOX(leftBox
), GTK_WIDGET(swImg
), FALSE
, FALSE
, 0);
251 rightBox
= gtk_vbox_new(FALSE
, 0);
252 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(neImg
), FALSE
, FALSE
, 0);
253 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(eImg
), TRUE
, TRUE
, 0);
254 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(logoImg
), FALSE
, FALSE
, 0);
255 gtk_box_pack_end(GTK_BOX(rightBox
), GTK_WIDGET(seImg
), FALSE
, FALSE
, 0);
257 //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);
258 gtk_table_attach(GTK_TABLE(decoTable
), GTK_WIDGET(leftBox
), 0, 1, 0, 1, (GtkAttachOptions
)(0), (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
), 0, 0);
259 gtk_table_attach(GTK_TABLE(decoTable
), GTK_WIDGET(rightBox
), 2, 3, 0, 1, (GtkAttachOptions
)(0), (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
), 0, 0);
261 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);
262 gtk_container_add( GTK_CONTAINER(eventbox
), decoTable
);
263 gtk_widget_set_name( GTK_WIDGET(eventbox
), "Calf-Plugin" );
265 // create window with viewport
266 // GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
267 // gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
268 // gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
269 // gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(decoTable));
271 return GTK_WIDGET(eventbox
);
274 void plugin_gui::send_configure(const char *key
, const char *value
)
276 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
277 for (unsigned int i
= 0; i
< params
.size(); i
++)
279 assert(params
[i
] != NULL
);
280 send_configure_iface
*sci
= dynamic_cast<send_configure_iface
*>(params
[i
]);
282 sci
->send_configure(key
, value
);
286 void plugin_gui::send_status(const char *key
, const char *value
)
288 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
289 for (unsigned int i
= 0; i
< params
.size(); i
++)
291 assert(params
[i
] != NULL
);
292 send_updates_iface
*sui
= dynamic_cast<send_updates_iface
*>(params
[i
]);
294 sui
->send_status(key
, value
);
298 void plugin_gui::remove_param_ctl(int param
, param_control
*ctl
)
300 std::multimap
<int, param_control
*>::iterator it
= par2ctl
.find(param
);
301 while(it
!= par2ctl
.end() && it
->first
== param
)
303 if (it
->second
== ctl
)
305 std::multimap
<int, param_control
*>::iterator orig
= it
;
307 par2ctl
.erase(it
, orig
);
313 unsigned last
= params
.size() - 1;
314 for (unsigned i
= 0; i
< params
.size(); ++i
)
316 if (params
[i
] == ctl
)
319 std::swap(params
[i
], params
[last
]);
320 params
.erase(params
.begin() + last
, params
.end());
325 void plugin_gui::on_idle()
327 set
<unsigned> changed
;
328 for (unsigned i
= 0; i
< read_serials
.size(); i
++)
330 int write_serial
= plugin
->get_write_serial(i
);
331 if (write_serial
- read_serials
[i
] > 0)
333 read_serials
[i
] = write_serial
;
337 for (unsigned i
= 0; i
< params
.size(); i
++)
339 int param_no
= params
[i
]->param_no
;
342 const parameter_properties
&props
= *plugin
->get_metadata_iface()->get_param_props(param_no
);
343 bool is_output
= (props
.flags
& PF_PROP_OUTPUT
) != 0;
344 if (is_output
|| (param_no
!= -1 && changed
.count(param_no
)))
347 params
[i
]->on_idle();
349 last_status_serial_no
= plugin
->send_status_updates(this, last_status_serial_no
);
350 // XXXKF iterate over par2ctl, too...
353 void plugin_gui::refresh()
355 for (unsigned int i
= 0; i
< params
.size(); i
++)
357 plugin
->send_configures(this);
358 last_status_serial_no
= plugin
->send_status_updates(this, last_status_serial_no
);
361 void plugin_gui::refresh(int param_no
, param_control
*originator
)
363 std::multimap
<int, param_control
*>::iterator it
= par2ctl
.find(param_no
);
364 while(it
!= par2ctl
.end() && it
->first
== param_no
)
366 if (it
->second
!= originator
)
372 void plugin_gui::set_param_value(int param_no
, float value
, param_control
*originator
)
374 plugin
->set_param_value(param_no
, value
);
378 /// Get a radio button group (if it exists) for a parameter
379 GSList
*plugin_gui::get_radio_group(int param
)
381 map
<int, GSList
*>::const_iterator i
= param_radio_groups
.find(param
);
382 if (i
== param_radio_groups
.end())
388 /// Set a radio button group for a parameter
389 void plugin_gui::set_radio_group(int param
, GSList
*group
)
391 param_radio_groups
[param
] = group
;
394 void plugin_gui::show_rack_ears(bool show
)
396 // if hidden, add a no-show-all attribute so that LV2 host doesn't accidentally override
397 // the setting by doing a show_all on the outermost container
398 gtk_widget_set_no_show_all(leftBox
, !show
);
399 gtk_widget_set_no_show_all(rightBox
, !show
);
402 gtk_widget_show(leftBox
);
403 gtk_widget_show(rightBox
);
407 gtk_widget_hide(leftBox
);
408 gtk_widget_hide(rightBox
);
412 void plugin_gui::on_automation_add(GtkWidget
*widget
, void *user_data
)
414 plugin_gui
*self
= (plugin_gui
*)user_data
;
415 self
->plugin
->add_automation(self
->context_menu_last_designator
, automation_range(0, 1, self
->context_menu_param_no
));
418 void plugin_gui::on_automation_delete(GtkWidget
*widget
, void *user_data
)
420 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
421 ame
->gui
->plugin
->delete_automation(ame
->source
, ame
->gui
->context_menu_param_no
);
424 void plugin_gui::on_automation_set_lower_or_upper(automation_menu_entry
*ame
, bool is_upper
)
426 const parameter_properties
*props
= plugin
->get_metadata_iface()->get_param_props(context_menu_param_no
);
427 float mapped
= props
->to_01(plugin
->get_param_value(context_menu_param_no
));
429 automation_map mappings
;
430 plugin
->get_automation(context_menu_param_no
, mappings
);
431 automation_map::const_iterator i
= mappings
.find(ame
->source
);
432 if (i
!= mappings
.end())
435 plugin
->add_automation(context_menu_last_designator
, automation_range(i
->second
.min_value
, mapped
, context_menu_param_no
));
437 plugin
->add_automation(context_menu_last_designator
, automation_range(mapped
, i
->second
.max_value
, context_menu_param_no
));
441 void plugin_gui::on_automation_set_lower(GtkWidget
*widget
, void *user_data
)
443 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
444 ame
->gui
->on_automation_set_lower_or_upper(ame
, false);
447 void plugin_gui::on_automation_set_upper(GtkWidget
*widget
, void *user_data
)
449 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
450 ame
->gui
->on_automation_set_lower_or_upper(ame
, true);
453 void plugin_gui::cleanup_automation_entries()
455 for (int i
= 0; i
< (int)automation_menu_callback_data
.size(); i
++)
456 delete automation_menu_callback_data
[i
];
457 automation_menu_callback_data
.clear();
460 void plugin_gui::on_control_popup(param_control
*ctl
, int param_no
)
462 cleanup_automation_entries();
465 context_menu_param_no
= param_no
;
466 GtkWidget
*menu
= gtk_menu_new();
468 multimap
<uint32_t, automation_range
> mappings
;
469 plugin
->get_automation(param_no
, mappings
);
471 context_menu_last_designator
= plugin
->get_last_automation_source();
474 if (context_menu_last_designator
!= 0xFFFFFFFF)
477 ss
<< "_Bind to: Ch" << (1 + (context_menu_last_designator
>> 8)) << ", CC#" << (context_menu_last_designator
& 127);
478 item
= gtk_menu_item_new_with_mnemonic(ss
.str().c_str());
479 g_signal_connect(item
, "activate", (GCallback
)on_automation_add
, this);
480 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
484 item
= gtk_menu_item_new_with_label("Send CC to automate");
485 gtk_widget_set_sensitive(item
, FALSE
);
486 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
489 for(multimap
<uint32_t, automation_range
>::const_iterator i
= mappings
.begin(); i
!= mappings
.end(); ++i
)
491 automation_menu_entry
*ame
= new automation_menu_entry(this, i
->first
);
492 automation_menu_callback_data
.push_back(ame
);
494 ss
<< "Mapping: Ch" << (1 + (i
->first
>> 8)) << ", CC#" << (i
->first
& 127);
495 item
= gtk_menu_item_new_with_label(ss
.str().c_str());
496 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
498 GtkWidget
*submenu
= gtk_menu_new();
499 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), submenu
);
501 item
= gtk_menu_item_new_with_mnemonic("_Delete");
502 g_signal_connect(item
, "activate", (GCallback
)on_automation_delete
, ame
);
503 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
505 item
= gtk_menu_item_new_with_mnemonic("Set _lower limit");
506 g_signal_connect(item
, "activate", (GCallback
)on_automation_set_lower
, ame
);
507 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
509 item
= gtk_menu_item_new_with_mnemonic("Set _upper limit");
510 g_signal_connect(item
, "activate", (GCallback
)on_automation_set_upper
, ame
);
511 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
512 //g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
516 gtk_widget_show_all(menu
);
517 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
, 3, gtk_get_current_event_time());
520 void plugin_gui::destroy_child_widgets(GtkWidget
*parent
)
522 if (parent
&& GTK_IS_CONTAINER(parent
))
524 GList
*children
= gtk_container_get_children(GTK_CONTAINER(parent
));
525 for(GList
*p
= children
; p
; p
= p
->next
)
526 gtk_widget_destroy(GTK_WIDGET(p
->data
));
527 g_list_free(children
);
531 plugin_gui::~plugin_gui()
533 cleanup_automation_entries();
534 delete preset_access
;
537 /***************************** GUI environment ********************************************/
539 bool window_update_controller::check_redraw(GtkWidget
*toplevel
)
541 GdkWindow
*gdkwin
= gtk_widget_get_window(toplevel
);
545 if (!gdk_window_is_viewable(gdkwin
))
547 GdkWindowState state
= gdk_window_get_state(gdkwin
);
548 if (state
& GDK_WINDOW_STATE_ICONIFIED
)
551 if (refresh_counter
& 15)
557 /***************************** GUI environment ********************************************/
559 gui_environment::gui_environment()
561 keyfile
= g_key_file_new();
563 gchar
*fn
= g_build_filename(getenv("HOME"), ".calfrc", NULL
);
564 string filename
= fn
;
566 g_key_file_load_from_file(keyfile
, filename
.c_str(), (GKeyFileFlags
)(G_KEY_FILE_KEEP_COMMENTS
| G_KEY_FILE_KEEP_TRANSLATIONS
), NULL
);
568 config_db
= new calf_utils::gkeyfile_config_db(keyfile
, filename
.c_str(), "gui");
569 gui_config
.load(config_db
);
572 gui_environment::~gui_environment()
576 g_key_file_free(keyfile
);