LV2: less heavy-handed approach to lifecycle issues
[calf.git] / src / calf / gui.h
blob68c60a8bc2028fa2f2f40087a2697c1863f5c5b3
1 /* Calf DSP Library
2 * Universal GUI module
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 02111-1307, USA.
20 #ifndef __CALF_GUI_H
21 #define __CALF_GUI_H
23 #include <expat.h>
24 #include <map>
25 #include <set>
26 #include <string>
27 #include <vector>
28 #include <gtk/gtk.h>
29 #include "giface.h"
30 #include "gui_config.h"
31 #include "jackhost.h"
33 namespace calf_plugins {
35 class window_update_controller
37 int refresh_counter;
38 public:
39 window_update_controller() : refresh_counter() {}
40 bool check_redraw(GtkWidget *toplevel);
43 class plugin_gui;
44 class jack_host;
46 struct control_base
48 virtual bool is_container() { return false; };
49 GtkContainer *container;
50 GtkWidget *widget, *entrywin;
51 std::string control_name;
52 typedef std::map<std::string, std::string> xml_attribute_map;
53 xml_attribute_map attribs;
54 plugin_gui *gui;
55 void require_attribute(const char *name);
56 void require_int_attribute(const char *name);
57 int get_int(const char *name, int def_value = 0);
58 float get_float(const char *name, float def_value = 0.f);
59 virtual void set_visibilty(bool state);
60 virtual void add(GtkWidget *w, control_base *base) { gtk_container_add(container, w); }
61 /// called after creation, so that all standard properties can be set
62 virtual void set_std_properties() = 0;
66 struct control_container: public control_base
68 virtual GtkWidget *create(plugin_gui *_gui, const char *element, xml_attribute_map &attributes)=0;
69 virtual void set_std_properties();
70 virtual ~control_container() {}
73 #define _GUARD_CHANGE_ if (in_change) return; guard_change __gc__(this);
75 struct param_control: public control_base
77 int param_no;
78 std::string param_variable;
79 int in_change;
80 bool has_entry;
81 float old_displayed_value;
83 struct guard_change {
84 param_control *pc;
85 guard_change(param_control *_pc) : pc(_pc) { pc->in_change++; }
86 ~guard_change() { pc->in_change--; }
89 param_control();
90 inline const parameter_properties &get_props();
92 virtual void init_xml(const char *element) {}
93 /// called to create a widget for a control
94 virtual GtkWidget *create(plugin_gui *_gui, int _param_no)=0;
95 virtual void created(){};
96 /// called to transfer the value from control to parameter(s)
97 virtual void get()=0;
98 /// called to transfer the value from parameter(s) to control
99 virtual void set()=0;
100 /// called on DSSI configure()
101 virtual void configure(const char *key, const char *value) {}
102 virtual void hook_params();
103 virtual void on_idle() {}
104 virtual void set_std_properties();
105 virtual void add_context_menu_handler();
106 virtual ~param_control();
107 virtual void do_popup_menu();
108 static gboolean on_button_press_event(GtkWidget *widget, GdkEventButton *event, void *user_data);
109 static gboolean on_popup_menu(GtkWidget *widget, void *user_data);
110 virtual void create_value_entry(GtkWidget *widget, int x, int y);
111 virtual void destroy_value_entry();
112 static gboolean value_entry_action(GtkEntry *widget, GdkEvent *event, void *user_data);
113 static gboolean value_entry_unfocus(GtkWidget *widget, GdkEventFocus *event, void *user_data);
114 static gboolean value_entry_click(GtkWidget *widget, GdkEventButton *event, void *user_data);
118 class plugin_gui_window;
120 class plugin_gui: public send_configure_iface, public send_updates_iface
122 protected:
123 int param_count;
124 std::multimap<int, param_control *> par2ctl;
125 XML_Parser parser;
126 param_control *current_control;
127 std::vector<control_base *> container_stack;
128 std::vector<param_control *> control_stack;
129 control_base *top_container;
130 std::map<std::string, int> param_name_map;
131 int ignore_stack;
132 int last_status_serial_no;
133 std::map<int, GSList *> param_radio_groups;
134 GtkWidget *leftBox, *rightBox;
135 int context_menu_param_no;
136 uint32_t context_menu_last_designator;
137 std::vector<control_container *> all_containers;
139 struct automation_menu_entry {
140 plugin_gui *gui;
141 int source;
142 automation_menu_entry(plugin_gui *_gui, int _source)
143 : gui(_gui), source(_source) {}
145 std::vector<automation_menu_entry *> automation_menu_callback_data;
147 static void on_automation_add(GtkWidget *widget, void *user_data);
148 static void on_automation_delete(GtkWidget *widget, void *user_data);
149 static void on_automation_set_lower(GtkWidget *widget, void *user_data);
150 static void on_automation_set_upper(GtkWidget *widget, void *user_data);
151 void on_automation_set_lower_or_upper(automation_menu_entry *ame, bool is_upper);
152 public:
153 plugin_gui_window *window;
154 GtkWidget *container;
155 const char *effect_name;
156 plugin_ctl_iface *plugin;
157 preset_access_iface *preset_access;
158 std::vector<param_control *> params;
159 std::vector<int> read_serials;
161 /* For optional lv2ui:show interface. */
162 bool optclosed;
163 GtkWidget* optwidget;
164 GtkWidget* optwindow;
165 const char* opttitle;
167 plugin_gui(plugin_gui_window *_window);
168 GtkWidget *create_from_xml(plugin_ctl_iface *_plugin, const char *xml);
169 param_control *create_control_from_xml(const char *element, const char *attributes[]);
170 control_container *create_container_from_xml(const char *element, const char *attributes[]);
172 void add_param_ctl(int param, param_control *ctl) { par2ctl.insert(std::pair<int, param_control *>(param, ctl)); }
173 void refresh();
174 void refresh(int param_no, param_control *originator = NULL);
175 int get_param_no_by_name(std::string param_name);
176 void xml_element_start(const char *element, const char *attributes[]);
177 void set_param_value(int param_no, float value, param_control *originator = NULL);
178 /// Called on change of configure variable
179 void send_configure(const char *key, const char *value);
180 /// Called on change of status variable
181 void send_status(const char *key, const char *value);
182 void on_idle();
183 /// Get a radio button group (if it exists) for a parameter
184 GSList *get_radio_group(int param);
185 /// Set a radio button group for a parameter
186 void set_radio_group(int param, GSList *group);
187 /// Show rack ear widgets
188 void show_rack_ears(bool show);
189 /// Pop-up a context menu for a control
190 void on_control_popup(param_control *ctl, int param_no);
191 /// Clean up callback data allocated for the automation pop-up menu
192 void cleanup_automation_entries();
193 ~plugin_gui();
194 static void xml_element_start(void *data, const char *element, const char *attributes[]);
195 static void xml_element_end(void *data, const char *element);
198 class main_window_iface;
199 class main_window_owner_iface;
201 /// A class used to inform the plugin GUIs about the environment they run in
202 /// (currently: what plugin features are accessible)
203 struct gui_environment_iface
205 virtual bool check_condition(const char *name) = 0;
206 virtual calf_utils::config_db_iface *get_config_db() = 0;
207 virtual calf_utils::gui_config *get_config() = 0;
208 virtual ~gui_environment_iface() {}
211 /// An interface that wraps UI-dependent elements of the session
212 struct session_environment_iface
214 /// Called to initialise the UI libraries
215 virtual void init_gui(int &argc, char **&argv) = 0;
216 /// Create an appropriate version of the main window
217 virtual main_window_iface *create_main_window() = 0;
218 /// Called to start the UI loop
219 virtual void start_gui_loop() = 0;
220 /// Called from within event handlers to finish the UI loop
221 virtual void quit_gui_loop() = 0;
222 virtual ~session_environment_iface() {}
225 /// Trivial implementation of gui_environment_iface
226 class gui_environment: public gui_environment_iface
228 private:
229 GKeyFile *keyfile;
230 calf_utils::config_db_iface *config_db;
231 calf_utils::gui_config gui_config;
233 public:
234 std::set<std::string> conditions;
236 public:
237 gui_environment();
238 virtual bool check_condition(const char *name) { return conditions.count(name) != 0; }
239 virtual calf_utils::config_db_iface *get_config_db() { return config_db; }
240 virtual calf_utils::gui_config *get_config() { return &gui_config; }
241 ~gui_environment();
244 /// Interface used by the plugin to communicate with the main hosting window
245 struct main_window_iface: public progress_report_iface
247 /// Set owner pointer
248 virtual void set_owner(main_window_owner_iface *owner) = 0;
249 /// Add a condition to the list of conditions supported by the host
250 virtual void add_condition(const std::string &name) = 0;
251 /// Create the actual window associated with this interface
252 virtual void create() = 0;
253 /// Add the plugin to the window
254 virtual void add_plugin(jack_host *plugin) = 0;
255 /// Remove the plugin from the window
256 virtual void del_plugin(plugin_ctl_iface *plugin) = 0;
257 /// Refresh the plugin UI
258 virtual void refresh_plugin(plugin_ctl_iface *plugin) = 0;
259 /// Bind the plugin window to the plugin
260 virtual void set_window(plugin_ctl_iface *plugin, plugin_gui_window *window) = 0;
261 /// Refresh preset lists on all windows (if, for example, a new preset has been created)
262 virtual void refresh_all_presets(bool builtin_too) = 0;
263 /// Default open file operation
264 virtual void open_file() = 0;
265 /// Default save file operation
266 virtual bool save_file() = 0;
267 /// Called to clean up when host quits
268 virtual void on_closed() = 0;
269 /// Show an error message
270 virtual void show_error(const std::string &text) = 0;
273 virtual ~main_window_iface() {}
276 struct main_window_owner_iface
278 virtual void new_plugin(const char *name) = 0;
279 virtual void remove_plugin(plugin_ctl_iface *plugin) = 0;
280 virtual char *open_file(const char *name) = 0;
281 virtual char *save_file(const char *name) = 0;
282 virtual void reorder_plugins() = 0;
283 /// Return JACK client name (or its counterpart) to put in window title bars
284 virtual std::string get_client_name() const = 0;
285 /// Called on 'destroy' event of the main window
286 virtual void on_main_window_destroy() = 0;
287 /// Called from idle handler
288 virtual void on_idle() = 0;
289 /// Get the file name of the current rack
290 virtual std::string get_current_filename() const = 0;
291 /// Set the file name of the current rack
292 virtual void set_current_filename(const std::string &name) = 0;
293 virtual ~main_window_owner_iface() {}
296 class plugin_gui_window: public calf_utils::config_listener_iface
298 private:
299 void cleanup();
300 window_update_controller refresh_controller;
301 public:
302 plugin_gui *gui;
303 GtkWindow *toplevel;
304 GtkUIManager *ui_mgr;
305 GtkActionGroup *std_actions, *builtin_preset_actions, *user_preset_actions, *command_actions;
306 gui_environment_iface *environment;
307 main_window_iface *main;
308 int source_id;
309 calf_utils::config_notifier_iface *notifier;
311 plugin_gui_window(gui_environment_iface *_env, main_window_iface *_main);
312 std::string make_gui_preset_list(GtkActionGroup *grp, bool builtin, char &ch);
313 std::string make_gui_command_list(GtkActionGroup *grp);
314 void fill_gui_presets(bool builtin, char &ch);
315 void create(plugin_ctl_iface *_plugin, const char *title, const char *effect);
316 void close();
317 virtual void on_config_change();
318 static gboolean on_idle(void *data);
319 static void on_window_destroyed(GtkWidget *window, gpointer data);
320 ~plugin_gui_window();
324 inline const parameter_properties &param_control::get_props()
326 return *gui->plugin->get_metadata_iface()->get_param_props(param_no);
329 class null_audio_module;
331 struct activate_command_params
333 typedef void (*CommandFunc)(null_audio_module *);
334 plugin_gui *gui;
335 int function_idx;
337 activate_command_params(plugin_gui *_gui, int _idx)
338 : gui(_gui), function_idx(_idx)
343 void activate_command(GtkAction *action, activate_command_params *params);
345 class cairo_impl: public calf_plugins::cairo_iface
347 public:
348 cairo_t *context;
349 virtual void set_source_rgba(float r, float g, float b, float a = 1.f) { cairo_set_source_rgba(context, r, g, b, a); }
350 virtual void set_line_width(float width) { cairo_set_line_width(context, width); }
351 virtual void set_dash(const double *dash, int length) { cairo_set_dash(context, dash, length, 0); }
352 virtual void draw_label(const char *label, float x, float y, int pos, float margin, float align) {
353 cairo_text_extents_t extents;
354 cairo_text_extents(context, label, &extents);
355 switch(pos) {
356 case 0:
357 default:
358 // top
359 cairo_move_to(context, x - extents.width / 2, y - margin);
360 break;
361 case 1:
362 // right
363 cairo_move_to(context, x + margin, y + 2);
364 break;
365 case 2:
366 // bottom
367 cairo_move_to(context, x - extents.width / 2, y + margin + extents.height);
368 break;
369 case 3:
370 // left
371 cairo_move_to(context, x - margin - extents.width, y + 2);
372 break;
374 cairo_show_text(context, label);
380 #endif