Add new API function plugin_builder_connect_signals()
[geany-mirror.git] / src / pluginutils.c
blob102369f0268046e2772d1d13d2a8e78e4390f00c
1 /*
2 * pluginutils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2009-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /** @file pluginutils.h
23 * Plugin utility functions.
24 * These functions all take the @ref geany_plugin symbol as their first argument. */
26 #include "geany.h"
28 #ifdef HAVE_PLUGINS
30 #include "pluginutils.h"
31 #include "pluginprivate.h"
33 #include "ui_utils.h"
34 #include "toolbar.h"
35 #include "utils.h"
36 #include "support.h"
37 #include "plugins.h"
40 /** Inserts a toolbar item before the Quit button, or after the previous plugin toolbar item.
41 * A separator is added on the first call to this function, and will be shown when @a item is
42 * shown; hidden when @a item is hidden.
43 * @note You should still destroy @a item yourself, usually in @ref plugin_cleanup().
44 * @param plugin Must be @ref geany_plugin.
45 * @param item The item to add. */
46 void plugin_add_toolbar_item(GeanyPlugin *plugin, GtkToolItem *item)
48 GtkToolbar *toolbar = GTK_TOOLBAR(main_widgets.toolbar);
49 gint pos;
50 GeanyAutoSeparator *autosep;
52 g_return_if_fail(plugin);
53 autosep = &plugin->priv->toolbar_separator;
55 if (!autosep->widget)
57 GtkToolItem *sep;
59 pos = toolbar_get_insert_position();
61 sep = gtk_separator_tool_item_new();
62 gtk_toolbar_insert(toolbar, sep, pos);
63 autosep->widget = GTK_WIDGET(sep);
65 toolbar_item_ref(sep);
67 else
69 pos = gtk_toolbar_get_item_index(toolbar, GTK_TOOL_ITEM(autosep->widget));
70 g_return_if_fail(pos >= 0);
73 gtk_toolbar_insert(toolbar, item, pos + autosep->item_count + 1);
74 toolbar_item_ref(item);
76 /* hide the separator widget if there are no toolbar items showing for the plugin */
77 ui_auto_separator_add_ref(autosep, GTK_WIDGET(item));
81 /** Ensures that a plugin's module (*.so) will never be unloaded.
82 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
83 * using the GObject system.
85 * @param plugin Must be @ref geany_plugin.
87 * @since 0.16
89 void plugin_module_make_resident(GeanyPlugin *plugin)
91 g_return_if_fail(plugin);
93 g_module_make_resident(plugin->priv->module);
97 /** Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
98 * @param plugin Must be @ref geany_plugin.
99 * @param object Object to connect to, or @c NULL when using @link pluginsignals.c Geany signals @endlink.
100 * @param signal_name The name of the signal. For a list of available
101 * signals, please see the @link pluginsignals.c Signal documentation @endlink.
102 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
103 * (if supported by @a signal_name).
104 * @param callback The function to call when the signal is emitted.
105 * @param user_data The user data passed to the signal handler.
106 * @see plugin_callbacks. */
107 void plugin_signal_connect(GeanyPlugin *plugin,
108 GObject *object, const gchar *signal_name, gboolean after,
109 GCallback callback, gpointer user_data)
111 gulong id;
112 SignalConnection sc;
114 if (!object)
115 object = geany_object;
117 id = after ?
118 g_signal_connect_after(object, signal_name, callback, user_data) :
119 g_signal_connect(object, signal_name, callback, user_data);
121 if (!plugin->priv->signal_ids)
122 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
124 sc.object = object;
125 sc.handler_id = id;
126 g_array_append_val(plugin->priv->signal_ids, sc);
130 typedef struct PluginSourceData
132 Plugin *plugin;
133 GList list_link; /* element of plugin->sources cointaining this GSource */
134 GSourceFunc function;
135 gpointer user_data;
136 } PluginSourceData;
139 /* prepend psd->list_link to psd->plugin->sources */
140 static void psd_register(PluginSourceData *psd, GSource *source)
142 psd->list_link.data = source;
143 psd->list_link.prev = NULL;
144 psd->list_link.next = psd->plugin->sources;
145 if (psd->list_link.next)
146 psd->list_link.next->prev = &psd->list_link;
147 psd->plugin->sources = &psd->list_link;
151 /* removes psd->list_link from psd->plugin->sources */
152 static void psd_unregister(PluginSourceData *psd)
154 if (psd->list_link.next)
155 psd->list_link.next->prev = psd->list_link.prev;
156 if (psd->list_link.prev)
157 psd->list_link.prev->next = psd->list_link.next;
158 else /* we were the first of the list, update the plugin->sources pointer */
159 psd->plugin->sources = psd->list_link.next;
163 static void on_plugin_source_destroy(gpointer data)
165 PluginSourceData *psd = data;
167 psd_unregister(psd);
168 g_slice_free1(sizeof *psd, psd);
172 static gboolean on_plugin_source_callback(gpointer data)
174 PluginSourceData *psd = data;
176 return psd->function(psd->user_data);
180 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
181 * unloading time */
182 static guint plugin_source_add(GeanyPlugin *plugin, GSource *source, GSourceFunc func, gpointer data)
184 guint id;
185 PluginSourceData *psd = g_slice_alloc(sizeof *psd);
187 psd->plugin = plugin->priv;
188 psd->function = func;
189 psd->user_data = data;
191 g_source_set_callback(source, on_plugin_source_callback, psd, on_plugin_source_destroy);
192 psd_register(psd, source);
193 id = g_source_attach(source, NULL);
194 g_source_unref(source);
196 return id;
200 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
201 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
203 * @param plugin Must be @ref geany_plugin.
204 * @param interval The time between calls to the function, in milliseconds.
205 * @param function The function to call after the given timeout.
206 * @param data The user data passed to the function.
207 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
208 * directly if you want to manage this event source manually).
210 * @see g_timeout_add()
211 * @since 0.21, plugin API 205.
213 guint plugin_timeout_add(GeanyPlugin *plugin, guint interval, GSourceFunc function, gpointer data)
215 return plugin_source_add(plugin, g_timeout_source_new(interval), function, data);
219 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
220 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
222 * @param plugin Must be @ref geany_plugin.
223 * @param interval The time between calls to the function, in seconds.
224 * @param function The function to call after the given timeout.
225 * @param data The user data passed to the function.
226 * @return the ID of the event source (you generally won't need it, or better use
227 * g_timeout_add_seconds() directly if you want to manage this event source manually).
229 * @see g_timeout_add_seconds()
230 * @since 0.21, plugin API 205.
232 guint plugin_timeout_add_seconds(GeanyPlugin *plugin, guint interval, GSourceFunc function,
233 gpointer data)
235 return plugin_source_add(plugin, g_timeout_source_new_seconds(interval), function, data);
239 /** Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
240 * it to run after the plugin has been unloaded (which may lead to a segfault).
242 * @param plugin Must be @ref geany_plugin.
243 * @param function The function to call in IDLE time.
244 * @param data The user data passed to the function.
245 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
246 * directly if you want to manage this event source manually).
248 * @see g_idle_add()
249 * @since 0.21, plugin API 205.
251 guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
253 return plugin_source_add(plugin, g_idle_source_new(), function, data);
257 /** Sets up or resizes a keybinding group for the plugin.
258 * You should then call keybindings_set_item() for each keybinding in the group.
259 * @param plugin Must be @ref geany_plugin.
260 * @param section_name Name used in the configuration file, such as @c "html_chars".
261 * @param count Number of keybindings for the group.
262 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
263 * @return The plugin's keybinding group.
264 * @since 0.19. */
265 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
266 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
268 Plugin *priv = plugin->priv;
270 priv->key_group = keybindings_set_group(priv->key_group, section_name,
271 priv->info.name, count, callback);
272 return priv->key_group;
276 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
278 p->configure_single(main_widgets.window);
282 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
284 GtkWidget *page = NULL; /* some plugins don't have prefs */
286 if (p->configure)
288 page = p->configure(GTK_DIALOG(dialog));
290 if (! GTK_IS_WIDGET(page))
292 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
293 p->info.name);
294 return NULL;
296 else
298 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
300 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
301 gtk_container_add(GTK_CONTAINER(align), page);
302 page = gtk_vbox_new(FALSE, 0);
303 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
306 else if (p->configure_single)
308 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
309 GtkWidget *btn;
311 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
313 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
314 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
315 gtk_container_add(GTK_CONTAINER(align), btn);
316 page = align;
318 return page;
322 /* multiple plugin configure dialog
323 * current_plugin can be NULL */
324 static void configure_plugins(Plugin *current_plugin)
326 GtkWidget *dialog, *vbox, *nb;
327 GList *node;
328 gint cur_page = -1;
330 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
331 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
332 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
333 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
334 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
335 gtk_widget_set_name(dialog, "GeanyDialog");
337 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
338 nb = gtk_notebook_new();
339 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
340 gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
342 foreach_list(node, active_plugin_list)
344 Plugin *p = node->data;
345 GtkWidget *page = create_pref_page(p, dialog);
347 if (page)
349 GtkWidget *label = gtk_label_new(p->info.name);
350 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
352 if (p == current_plugin)
353 cur_page = n;
356 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
358 gtk_widget_show_all(vbox);
359 if (cur_page >= 0)
360 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
362 /* run the dialog */
363 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
365 else
366 utils_beep();
368 gtk_widget_destroy(dialog);
372 /** Shows the plugin's configure dialog.
373 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
374 * @param plugin Must be @ref geany_plugin.
375 * @since 0.19. */
376 /* if NULL, show all plugins */
377 void plugin_show_configure(GeanyPlugin *plugin)
379 Plugin *p;
381 if (!plugin)
383 configure_plugins(NULL);
384 return;
386 p = plugin->priv;
388 if (p->configure)
389 configure_plugins(p);
390 else
392 g_return_if_fail(p->configure_single);
393 p->configure_single(main_widgets.window);
398 struct BuilderConnectData
400 gpointer user_data;
401 GeanyPlugin *plugin;
405 static void connect_plugin_signals(GtkBuilder *builder, GObject *object,
406 const gchar *signal_name, const gchar *handler_name,
407 GObject *connect_object, GConnectFlags flags, gpointer user_data)
409 gpointer symbol = NULL;
410 struct BuilderConnectData *data = user_data;
412 if (!g_module_symbol(data->plugin->priv->module, handler_name, &symbol))
414 g_warning("Failed to locate signal handler for '%s': %s",
415 signal_name, g_module_error());
416 return;
419 plugin_signal_connect(data->plugin, object, signal_name, FALSE,
420 G_CALLBACK(symbol) /*ub?*/, data->user_data);
425 * Allows auto-connecting Glade/GtkBuilder signals in plugins.
427 * When a plugin uses GtkBuilder to load some UI from file/string,
428 * the gtk_builder_connect_signals() function is unable to automatically
429 * connect to the plugin's signal handlers. A plugin could itself use
430 * the gtk_builder_connect_signals_full() function to automatically
431 * connect to the signal handler functions by loading it's GModule
432 * and retrieving pointers to the handler functions, but rather than
433 * each plugin having to do that, this function handles it automatically.
435 * @code
436 * ...
437 * GeanyPlugin *geany_plugin;
439 * G_MODULE_EXPORT void
440 * myplugin_button_clicked(GtkButton *button, gpointer user_data)
442 * g_print("Button pressed\n");
445 * void plugin_init(GeanyData *data)
447 * GtkBuilder *builder = gtk_builder_new();
448 * gtk_builder_add_from_file(builder, "gui.glade", NULL);
449 * plugin_builder_connect_signals(geany_plugin, builder, NULL);
450 * ...
452 * @endcode
454 * @note It's important that you prefix your callback handlers with
455 * a plugin-specific prefix to avoid clashing with other plugins since
456 * the function symbols will be exported process-wide.
458 * @param plugin Must be @ref geany_plugin.
459 * @param builder The GtkBuilder to connect signals with.
460 * @param user_data User data to pass to the connected signal handlers.
462 * @since 1.24, plugin API 217.
464 void plugin_builder_connect_signals(GeanyPlugin *plugin,
465 GtkBuilder *builder, gpointer user_data)
467 struct BuilderConnectData data = { NULL };
469 g_return_if_fail(plugin != NULL && plugin->priv != NULL);
470 g_return_if_fail(plugin->priv->module != NULL);
471 g_return_if_fail(GTK_IS_BUILDER(builder));
473 data.user_data = user_data;
474 data.plugin = plugin;
476 gtk_builder_connect_signals_full(builder, connect_plugin_signals, &data);
480 #endif