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
, "keyboard"))
83 return new keyboard_param_control
;
84 if (!strcmp(element
, "curve"))
85 return new curve_param_control
;
86 if (!strcmp(element
, "led"))
87 return new led_param_control
;
88 if (!strcmp(element
, "tube"))
89 return new tube_param_control
;
90 if (!strcmp(element
, "entry"))
91 return new entry_param_control
;
92 if (!strcmp(element
, "filechooser"))
93 return new filechooser_param_control
;
94 if (!strcmp(element
, "listview"))
95 return new listview_param_control
;
96 if (!strcmp(element
, "notebook"))
97 return new notebook_param_control
;
98 if (!strcmp(element
, "table"))
99 return new table_container
;
100 if (!strcmp(element
, "vbox"))
101 return new vbox_container
;
102 if (!strcmp(element
, "hbox"))
103 return new hbox_container
;
104 if (!strcmp(element
, "align"))
105 return new alignment_container
;
106 if (!strcmp(element
, "frame"))
107 return new frame_container
;
108 if (!strcmp(element
, "scrolled"))
109 return new scrolled_container
;
113 void plugin_gui::xml_element_start(void *data
, const char *element
, const char *attributes
[])
115 plugin_gui
*gui
= (plugin_gui
*)data
;
116 gui
->xml_element_start(element
, attributes
);
119 int plugin_gui::get_param_no_by_name(string param_name
)
122 map
<string
, int>::iterator it
= param_name_map
.find(param_name
);
123 if (it
== param_name_map
.end())
124 g_error("Unknown parameter %s", param_name
.c_str());
126 param_no
= it
->second
;
131 void plugin_gui::xml_element_start(const char *element
, const char *attributes
[])
137 control_base::xml_attribute_map xam
;
140 xam
[attributes
[0]] = attributes
[1];
144 if (!strcmp(element
, "if"))
146 if (!xam
.count("cond") || xam
["cond"].empty())
148 g_error("Incorrect <if cond=\"[!]symbol\"> element");
150 string cond
= xam
["cond"];
152 if (cond
.substr(0, 1) == "!") {
156 if (window
->environment
->check_condition(cond
.c_str()) == state
)
161 control_base
*cc
= create_widget_from_xml(element
, attributes
);
163 g_error("Unxpected element %s in GUI definition\n", element
);
170 void plugin_gui::xml_element_end(void *data
, const char *element
)
172 plugin_gui
*gui
= (plugin_gui
*)data
;
173 if (gui
->ignore_stack
) {
177 if (!strcmp(element
, "if"))
180 control_base
*control
= gui
->stack
.back();
183 gui
->stack
.pop_back();
184 if (gui
->stack
.empty())
186 gui
->top_container
= control
;
187 gtk_widget_show_all(control
->widget
);
190 gui
->stack
.back()->add(control
);
194 GtkWidget
*plugin_gui::create_from_xml(plugin_ctl_iface
*_plugin
, const char *xml
)
196 top_container
= NULL
;
197 parser
= XML_ParserCreate("UTF-8");
202 param_name_map
.clear();
203 read_serials
.clear();
204 int size
= plugin
->get_metadata_iface()->get_param_count();
205 read_serials
.resize(size
);
206 for (int i
= 0; i
< size
; i
++)
207 param_name_map
[plugin
->get_metadata_iface()->get_param_props(i
)->short_name
] = i
;
209 XML_SetUserData(parser
, this);
210 XML_SetElementHandler(parser
, xml_element_start
, xml_element_end
);
211 XML_Status status
= XML_Parse(parser
, xml
, strlen(xml
), 1);
212 if (status
== XML_STATUS_ERROR
)
214 g_error("Parse error: %s in XML", XML_ErrorString(XML_GetErrorCode(parser
)));
217 XML_ParserFree(parser
);
218 last_status_serial_no
= plugin
->send_status_updates(this, 0);
219 GtkWidget
*eventbox
= gtk_event_box_new();
220 GtkWidget
*decoTable
= gtk_table_new(3, 1, FALSE
);
223 // overall background
224 //GtkWidget *bgImg = gtk_image_new_from_file(PKGLIBDIR "/background.png");
225 //gtk_widget_set_size_request(GTK_WIDGET(bgImg), 1, 1);
227 // images for left side
228 GtkWidget
*nwImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_nw.png");
229 GtkWidget
*swImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_sw.png");
230 GtkWidget
*wImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_w.png");
231 gtk_widget_set_size_request(GTK_WIDGET(wImg
), 56, 1);
233 // images for right side
234 GtkWidget
*neImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_ne.png");
235 GtkWidget
*seImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_se.png");
236 GtkWidget
*eImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_e.png");
237 GtkWidget
*logoImg
= gtk_image_new_from_file(PKGLIBDIR
"/side_e_logo.png");
238 gtk_widget_set_size_request(GTK_WIDGET(eImg
), 56, 1);
241 leftBox
= gtk_vbox_new(FALSE
, 0);
242 gtk_box_pack_start(GTK_BOX(leftBox
), GTK_WIDGET(nwImg
), FALSE
, FALSE
, 0);
243 gtk_box_pack_start(GTK_BOX(leftBox
), GTK_WIDGET(wImg
), TRUE
, TRUE
, 0);
244 gtk_box_pack_end(GTK_BOX(leftBox
), GTK_WIDGET(swImg
), FALSE
, FALSE
, 0);
247 rightBox
= gtk_vbox_new(FALSE
, 0);
248 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(neImg
), FALSE
, FALSE
, 0);
249 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(eImg
), TRUE
, TRUE
, 0);
250 gtk_box_pack_start(GTK_BOX(rightBox
), GTK_WIDGET(logoImg
), FALSE
, FALSE
, 0);
251 gtk_box_pack_end(GTK_BOX(rightBox
), GTK_WIDGET(seImg
), FALSE
, FALSE
, 0);
253 //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);
254 gtk_table_attach(GTK_TABLE(decoTable
), GTK_WIDGET(leftBox
), 0, 1, 0, 1, (GtkAttachOptions
)(0), (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
), 0, 0);
255 gtk_table_attach(GTK_TABLE(decoTable
), GTK_WIDGET(rightBox
), 2, 3, 0, 1, (GtkAttachOptions
)(0), (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
), 0, 0);
257 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);
258 gtk_container_add( GTK_CONTAINER(eventbox
), decoTable
);
259 gtk_widget_set_name( GTK_WIDGET(eventbox
), "Calf-Plugin" );
261 // create window with viewport
262 // GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
263 // gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
264 // gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_NONE);
265 // gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(decoTable));
267 return GTK_WIDGET(eventbox
);
270 void plugin_gui::send_configure(const char *key
, const char *value
)
272 // XXXKF this should really be replaced by a separate list of SCI-capable param controls
273 for (unsigned int i
= 0; i
< params
.size(); i
++)
275 assert(params
[i
] != NULL
);
276 send_configure_iface
*sci
= dynamic_cast<send_configure_iface
*>(params
[i
]);
278 sci
->send_configure(key
, value
);
282 void plugin_gui::send_status(const char *key
, const char *value
)
284 // XXXKF this should really be replaced by a separate list of SUI-capable param controls
285 for (unsigned int i
= 0; i
< params
.size(); i
++)
287 assert(params
[i
] != NULL
);
288 send_updates_iface
*sui
= dynamic_cast<send_updates_iface
*>(params
[i
]);
290 sui
->send_status(key
, value
);
294 void plugin_gui::remove_param_ctl(int param
, param_control
*ctl
)
296 std::multimap
<int, param_control
*>::iterator it
= par2ctl
.find(param
);
297 while(it
!= par2ctl
.end() && it
->first
== param
)
299 if (it
->second
== ctl
)
301 std::multimap
<int, param_control
*>::iterator orig
= it
;
303 par2ctl
.erase(it
, orig
);
309 unsigned last
= params
.size() - 1;
310 for (unsigned i
= 0; i
< params
.size(); ++i
)
312 if (params
[i
] == ctl
)
315 std::swap(params
[i
], params
[last
]);
316 params
.erase(params
.begin() + last
, params
.end());
321 void plugin_gui::on_idle()
323 set
<unsigned> changed
;
324 for (unsigned i
= 0; i
< read_serials
.size(); i
++)
326 int write_serial
= plugin
->get_write_serial(i
);
327 if (write_serial
- read_serials
[i
] > 0)
329 read_serials
[i
] = write_serial
;
333 for (unsigned i
= 0; i
< params
.size(); i
++)
335 int param_no
= params
[i
]->param_no
;
338 const parameter_properties
&props
= *plugin
->get_metadata_iface()->get_param_props(param_no
);
339 bool is_output
= (props
.flags
& PF_PROP_OUTPUT
) != 0;
340 if (is_output
|| (param_no
!= -1 && changed
.count(param_no
)))
343 params
[i
]->on_idle();
345 last_status_serial_no
= plugin
->send_status_updates(this, last_status_serial_no
);
346 // XXXKF iterate over par2ctl, too...
349 void plugin_gui::refresh()
351 for (unsigned int i
= 0; i
< params
.size(); i
++)
353 plugin
->send_configures(this);
354 last_status_serial_no
= plugin
->send_status_updates(this, last_status_serial_no
);
357 void plugin_gui::refresh(int param_no
, param_control
*originator
)
359 std::multimap
<int, param_control
*>::iterator it
= par2ctl
.find(param_no
);
360 while(it
!= par2ctl
.end() && it
->first
== param_no
)
362 if (it
->second
!= originator
)
368 void plugin_gui::set_param_value(int param_no
, float value
, param_control
*originator
)
370 plugin
->set_param_value(param_no
, value
);
374 /// Get a radio button group (if it exists) for a parameter
375 GSList
*plugin_gui::get_radio_group(int param
)
377 map
<int, GSList
*>::const_iterator i
= param_radio_groups
.find(param
);
378 if (i
== param_radio_groups
.end())
384 /// Set a radio button group for a parameter
385 void plugin_gui::set_radio_group(int param
, GSList
*group
)
387 param_radio_groups
[param
] = group
;
390 void plugin_gui::show_rack_ears(bool show
)
392 // if hidden, add a no-show-all attribute so that LV2 host doesn't accidentally override
393 // the setting by doing a show_all on the outermost container
394 gtk_widget_set_no_show_all(leftBox
, !show
);
395 gtk_widget_set_no_show_all(rightBox
, !show
);
398 gtk_widget_show(leftBox
);
399 gtk_widget_show(rightBox
);
403 gtk_widget_hide(leftBox
);
404 gtk_widget_hide(rightBox
);
408 void plugin_gui::on_automation_add(GtkWidget
*widget
, void *user_data
)
410 plugin_gui
*self
= (plugin_gui
*)user_data
;
411 self
->plugin
->add_automation(self
->context_menu_last_designator
, automation_range(0, 1, self
->context_menu_param_no
));
414 void plugin_gui::on_automation_delete(GtkWidget
*widget
, void *user_data
)
416 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
417 ame
->gui
->plugin
->delete_automation(ame
->source
, ame
->gui
->context_menu_param_no
);
420 void plugin_gui::on_automation_set_lower_or_upper(automation_menu_entry
*ame
, bool is_upper
)
422 const parameter_properties
*props
= plugin
->get_metadata_iface()->get_param_props(context_menu_param_no
);
423 float mapped
= props
->to_01(plugin
->get_param_value(context_menu_param_no
));
425 automation_map mappings
;
426 plugin
->get_automation(context_menu_param_no
, mappings
);
427 automation_map::const_iterator i
= mappings
.find(ame
->source
);
428 if (i
!= mappings
.end())
431 plugin
->add_automation(context_menu_last_designator
, automation_range(i
->second
.min_value
, mapped
, context_menu_param_no
));
433 plugin
->add_automation(context_menu_last_designator
, automation_range(mapped
, i
->second
.max_value
, context_menu_param_no
));
437 void plugin_gui::on_automation_set_lower(GtkWidget
*widget
, void *user_data
)
439 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
440 ame
->gui
->on_automation_set_lower_or_upper(ame
, false);
443 void plugin_gui::on_automation_set_upper(GtkWidget
*widget
, void *user_data
)
445 automation_menu_entry
*ame
= (automation_menu_entry
*)user_data
;
446 ame
->gui
->on_automation_set_lower_or_upper(ame
, true);
449 void plugin_gui::cleanup_automation_entries()
451 for (int i
= 0; i
< (int)automation_menu_callback_data
.size(); i
++)
452 delete automation_menu_callback_data
[i
];
453 automation_menu_callback_data
.clear();
456 void plugin_gui::on_control_popup(param_control
*ctl
, int param_no
)
458 cleanup_automation_entries();
461 context_menu_param_no
= param_no
;
462 GtkWidget
*menu
= gtk_menu_new();
464 multimap
<uint32_t, automation_range
> mappings
;
465 plugin
->get_automation(param_no
, mappings
);
467 context_menu_last_designator
= plugin
->get_last_automation_source();
470 if (context_menu_last_designator
!= 0xFFFFFFFF)
473 ss
<< "_Bind to: Ch" << (1 + (context_menu_last_designator
>> 8)) << ", CC#" << (context_menu_last_designator
& 127);
474 item
= gtk_menu_item_new_with_mnemonic(ss
.str().c_str());
475 g_signal_connect(item
, "activate", (GCallback
)on_automation_add
, this);
476 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
480 item
= gtk_menu_item_new_with_label("Send CC to automate");
481 gtk_widget_set_sensitive(item
, FALSE
);
482 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
485 for(multimap
<uint32_t, automation_range
>::const_iterator i
= mappings
.begin(); i
!= mappings
.end(); ++i
)
487 automation_menu_entry
*ame
= new automation_menu_entry(this, i
->first
);
488 automation_menu_callback_data
.push_back(ame
);
490 ss
<< "Mapping: Ch" << (1 + (i
->first
>> 8)) << ", CC#" << (i
->first
& 127);
491 item
= gtk_menu_item_new_with_label(ss
.str().c_str());
492 gtk_menu_shell_append(GTK_MENU_SHELL(menu
), item
);
494 GtkWidget
*submenu
= gtk_menu_new();
495 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), submenu
);
497 item
= gtk_menu_item_new_with_mnemonic("_Delete");
498 g_signal_connect(item
, "activate", (GCallback
)on_automation_delete
, ame
);
499 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
501 item
= gtk_menu_item_new_with_mnemonic("Set _lower limit");
502 g_signal_connect(item
, "activate", (GCallback
)on_automation_set_lower
, ame
);
503 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
505 item
= gtk_menu_item_new_with_mnemonic("Set _upper limit");
506 g_signal_connect(item
, "activate", (GCallback
)on_automation_set_upper
, ame
);
507 gtk_menu_shell_append(GTK_MENU_SHELL(submenu
), item
);
508 //g_signal_connect(item, "activate", (GCallback)on_automation_add, this);
512 gtk_widget_show_all(menu
);
513 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
, 3, gtk_get_current_event_time());
516 void plugin_gui::destroy_child_widgets(GtkWidget
*parent
)
518 if (parent
&& GTK_IS_CONTAINER(parent
))
520 GList
*children
= gtk_container_get_children(GTK_CONTAINER(parent
));
521 for(GList
*p
= children
; p
; p
= p
->next
)
522 gtk_widget_destroy(GTK_WIDGET(p
->data
));
523 g_list_free(children
);
527 plugin_gui::~plugin_gui()
529 cleanup_automation_entries();
530 delete preset_access
;
533 /***************************** GUI environment ********************************************/
535 bool window_update_controller::check_redraw(GtkWidget
*toplevel
)
537 GdkWindow
*gdkwin
= gtk_widget_get_window(toplevel
);
541 if (!gdk_window_is_viewable(gdkwin
))
543 GdkWindowState state
= gdk_window_get_state(gdkwin
);
544 if (state
& GDK_WINDOW_STATE_ICONIFIED
)
547 if (refresh_counter
& 15)
553 /***************************** GUI environment ********************************************/
555 gui_environment::gui_environment()
557 keyfile
= g_key_file_new();
559 gchar
*fn
= g_build_filename(getenv("HOME"), ".calfrc", NULL
);
560 string filename
= fn
;
562 g_key_file_load_from_file(keyfile
, filename
.c_str(), (GKeyFileFlags
)(G_KEY_FILE_KEEP_COMMENTS
| G_KEY_FILE_KEEP_TRANSLATIONS
), NULL
);
564 config_db
= new calf_utils::gkeyfile_config_db(keyfile
, filename
.c_str(), "gui");
565 gui_config
.load(config_db
);
568 gui_environment::~gui_environment()
572 g_key_file_free(keyfile
);