Start to make it easier to compile the core in isolation
[geany-mirror.git] / src / pluginutils.c
blobe8c91c5023661dac6281bfc8a6e506805b02d26c
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.
108 * @warning Before version 1.25 (API < 218),
109 * this should only be used on objects that outlive the plugin, never on
110 * objects that will get destroyed before the plugin is unloaded. For objects
111 * created and destroyed by the plugin, you can simply use @c g_signal_connect(),
112 * since all handlers are disconnected when the object is destroyed anyway.
113 * For objects that may or may not outlive the plugin (like @link GeanyEditor.sci
114 * a document's @c ScintillaObject @endlink, which is destroyed when the document
115 * is closed), you currently have to manually handle both situations, when the
116 * plugin is unloaded before the object is destroyed (and then, you have to
117 * disconnect the signal on @c plugin_cleanup()), and when the object is destroyed
118 * during the plugin's lifetime (in which case you cannot and should not disconnect
119 * manually in @c plugin_cleanup() since it already has been disconnected and the
120 * object has been destroyed), and disconnect yourself or not as appropriate.
121 * @note Since version 1.25 (API >= 218), the object lifetime is watched and so the above
122 * restriction does not apply. However, for objects destroyed by the plugin,
123 * @c g_signal_connect() is safe and has lower overhead. */
124 void plugin_signal_connect(GeanyPlugin *plugin,
125 GObject *object, const gchar *signal_name, gboolean after,
126 GCallback callback, gpointer user_data)
128 gulong id;
129 SignalConnection sc;
131 g_return_if_fail(plugin != NULL);
132 g_return_if_fail(object == NULL || G_IS_OBJECT(object));
134 if (!object)
135 object = geany_object;
137 id = after ?
138 g_signal_connect_after(object, signal_name, callback, user_data) :
139 g_signal_connect(object, signal_name, callback, user_data);
141 if (!plugin->priv->signal_ids)
142 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
144 sc.object = object;
145 sc.handler_id = id;
146 g_array_append_val(plugin->priv->signal_ids, sc);
148 /* watch the object lifetime to nuke our pointers to it */
149 plugin_watch_object(plugin->priv, object);
153 typedef struct PluginSourceData
155 Plugin *plugin;
156 GList list_link; /* element of plugin->sources cointaining this GSource */
157 GSourceFunc function;
158 gpointer user_data;
159 } PluginSourceData;
162 /* prepend psd->list_link to psd->plugin->sources */
163 static void psd_register(PluginSourceData *psd, GSource *source)
165 psd->list_link.data = source;
166 psd->list_link.prev = NULL;
167 psd->list_link.next = psd->plugin->sources;
168 if (psd->list_link.next)
169 psd->list_link.next->prev = &psd->list_link;
170 psd->plugin->sources = &psd->list_link;
174 /* removes psd->list_link from psd->plugin->sources */
175 static void psd_unregister(PluginSourceData *psd)
177 if (psd->list_link.next)
178 psd->list_link.next->prev = psd->list_link.prev;
179 if (psd->list_link.prev)
180 psd->list_link.prev->next = psd->list_link.next;
181 else /* we were the first of the list, update the plugin->sources pointer */
182 psd->plugin->sources = psd->list_link.next;
186 static void on_plugin_source_destroy(gpointer data)
188 PluginSourceData *psd = data;
190 psd_unregister(psd);
191 g_slice_free1(sizeof *psd, psd);
195 static gboolean on_plugin_source_callback(gpointer data)
197 PluginSourceData *psd = data;
199 return psd->function(psd->user_data);
203 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
204 * unloading time */
205 static guint plugin_source_add(GeanyPlugin *plugin, GSource *source, GSourceFunc func, gpointer data)
207 guint id;
208 PluginSourceData *psd = g_slice_alloc(sizeof *psd);
210 psd->plugin = plugin->priv;
211 psd->function = func;
212 psd->user_data = data;
214 g_source_set_callback(source, on_plugin_source_callback, psd, on_plugin_source_destroy);
215 psd_register(psd, source);
216 id = g_source_attach(source, NULL);
217 g_source_unref(source);
219 return id;
223 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
224 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
226 * @param plugin Must be @ref geany_plugin.
227 * @param interval The time between calls to the function, in milliseconds.
228 * @param function The function to call after the given timeout.
229 * @param data The user data passed to the function.
230 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
231 * directly if you want to manage this event source manually).
233 * @see g_timeout_add()
234 * @since 0.21, plugin API 205.
236 guint plugin_timeout_add(GeanyPlugin *plugin, guint interval, GSourceFunc function, gpointer data)
238 return plugin_source_add(plugin, g_timeout_source_new(interval), function, data);
242 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
243 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
245 * @param plugin Must be @ref geany_plugin.
246 * @param interval The time between calls to the function, in seconds.
247 * @param function The function to call after the given timeout.
248 * @param data The user data passed to the function.
249 * @return the ID of the event source (you generally won't need it, or better use
250 * g_timeout_add_seconds() directly if you want to manage this event source manually).
252 * @see g_timeout_add_seconds()
253 * @since 0.21, plugin API 205.
255 guint plugin_timeout_add_seconds(GeanyPlugin *plugin, guint interval, GSourceFunc function,
256 gpointer data)
258 return plugin_source_add(plugin, g_timeout_source_new_seconds(interval), function, data);
262 /** Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
263 * it to run after the plugin has been unloaded (which may lead to a segfault).
265 * @param plugin Must be @ref geany_plugin.
266 * @param function The function to call in IDLE time.
267 * @param data The user data passed to the function.
268 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
269 * directly if you want to manage this event source manually).
271 * @see g_idle_add()
272 * @since 0.21, plugin API 205.
274 guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
276 return plugin_source_add(plugin, g_idle_source_new(), function, data);
280 /** Sets up or resizes a keybinding group for the plugin.
281 * You should then call keybindings_set_item() for each keybinding in the group.
282 * @param plugin Must be @ref geany_plugin.
283 * @param section_name Name used in the configuration file, such as @c "html_chars".
284 * @param count Number of keybindings for the group.
285 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
286 * @return The plugin's keybinding group.
287 * @since 0.19. */
288 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
289 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
291 Plugin *priv = plugin->priv;
293 priv->key_group = keybindings_set_group(priv->key_group, section_name,
294 priv->info.name, count, callback);
295 return priv->key_group;
299 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
301 p->configure_single(main_widgets.window);
305 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
307 GtkWidget *page = NULL; /* some plugins don't have prefs */
309 if (p->configure)
311 page = p->configure(GTK_DIALOG(dialog));
313 if (! GTK_IS_WIDGET(page))
315 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
316 p->info.name);
317 return NULL;
319 else
321 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
323 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
324 gtk_container_add(GTK_CONTAINER(align), page);
325 page = gtk_vbox_new(FALSE, 0);
326 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
329 else if (p->configure_single)
331 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
332 GtkWidget *btn;
334 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
336 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
337 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
338 gtk_container_add(GTK_CONTAINER(align), btn);
339 page = align;
341 return page;
345 /* multiple plugin configure dialog
346 * current_plugin can be NULL */
347 static void configure_plugins(Plugin *current_plugin)
349 GtkWidget *dialog, *vbox, *nb;
350 GList *node;
351 gint cur_page = -1;
353 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
354 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
355 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
356 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
357 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
358 gtk_widget_set_name(dialog, "GeanyDialog");
360 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
361 nb = gtk_notebook_new();
362 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
363 gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
365 foreach_list(node, active_plugin_list)
367 Plugin *p = node->data;
368 GtkWidget *page = create_pref_page(p, dialog);
370 if (page)
372 GtkWidget *label = gtk_label_new(p->info.name);
373 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
375 if (p == current_plugin)
376 cur_page = n;
379 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
381 gtk_widget_show_all(vbox);
382 if (cur_page >= 0)
383 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
385 /* run the dialog */
386 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
388 else
389 utils_beep();
391 gtk_widget_destroy(dialog);
395 /** Shows the plugin's configure dialog.
396 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
397 * @param plugin Must be @ref geany_plugin.
398 * @since 0.19. */
399 /* if NULL, show all plugins */
400 void plugin_show_configure(GeanyPlugin *plugin)
402 Plugin *p;
404 if (!plugin)
406 configure_plugins(NULL);
407 return;
409 p = plugin->priv;
411 if (p->configure)
412 configure_plugins(p);
413 else
415 g_return_if_fail(p->configure_single);
416 p->configure_single(main_widgets.window);
421 struct BuilderConnectData
423 gpointer user_data;
424 GeanyPlugin *plugin;
428 static void connect_plugin_signals(GtkBuilder *builder, GObject *object,
429 const gchar *signal_name, const gchar *handler_name,
430 GObject *connect_object, GConnectFlags flags, gpointer user_data)
432 gpointer symbol = NULL;
433 struct BuilderConnectData *data = user_data;
435 if (!g_module_symbol(data->plugin->priv->module, handler_name, &symbol))
437 g_warning("Failed to locate signal handler for '%s': %s",
438 signal_name, g_module_error());
439 return;
442 plugin_signal_connect(data->plugin, object, signal_name, FALSE,
443 G_CALLBACK(symbol) /*ub?*/, data->user_data);
448 * Allows auto-connecting Glade/GtkBuilder signals in plugins.
450 * When a plugin uses GtkBuilder to load some UI from file/string,
451 * the gtk_builder_connect_signals() function is unable to automatically
452 * connect to the plugin's signal handlers. A plugin could itself use
453 * the gtk_builder_connect_signals_full() function to automatically
454 * connect to the signal handler functions by loading it's GModule
455 * and retrieving pointers to the handler functions, but rather than
456 * each plugin having to do that, this function handles it automatically.
458 * @code
459 * ...
460 * GeanyPlugin *geany_plugin;
462 * G_MODULE_EXPORT void
463 * myplugin_button_clicked(GtkButton *button, gpointer user_data)
465 * g_print("Button pressed\n");
468 * void plugin_init(GeanyData *data)
470 * GtkBuilder *builder = gtk_builder_new();
471 * gtk_builder_add_from_file(builder, "gui.glade", NULL);
472 * plugin_builder_connect_signals(geany_plugin, builder, NULL);
473 * ...
475 * @endcode
477 * @note It's important that you prefix your callback handlers with
478 * a plugin-specific prefix to avoid clashing with other plugins since
479 * the function symbols will be exported process-wide.
481 * @param plugin Must be @ref geany_plugin.
482 * @param builder The GtkBuilder to connect signals with.
483 * @param user_data User data to pass to the connected signal handlers.
485 * @since 1.24, plugin API 217.
487 void plugin_builder_connect_signals(GeanyPlugin *plugin,
488 GtkBuilder *builder, gpointer user_data)
490 struct BuilderConnectData data = { NULL };
492 g_return_if_fail(plugin != NULL && plugin->priv != NULL);
493 g_return_if_fail(plugin->priv->module != NULL);
494 g_return_if_fail(GTK_IS_BUILDER(builder));
496 data.user_data = user_data;
497 data.plugin = plugin;
499 gtk_builder_connect_signals_full(builder, connect_plugin_signals, &data);
503 #endif