info bars: Explicitely move focus back to the editor when closing
[geany-mirror.git] / src / pluginutils.c
blob317aed6003b57baf6d78a4bc14a491667d3e65f7
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #ifdef HAVE_PLUGINS
32 #include "pluginutils.h"
34 #include "app.h"
35 #include "geanyobject.h"
36 #include "plugindata.h"
37 #include "pluginprivate.h"
38 #include "plugins.h"
39 #include "support.h"
40 #include "toolbar.h"
41 #include "ui_utils.h"
42 #include "utils.h"
45 /** Inserts a toolbar item before the Quit button, or after the previous plugin toolbar item.
46 * A separator is added on the first call to this function, and will be shown when @a item is
47 * shown; hidden when @a item is hidden.
48 * @note You should still destroy @a item yourself, usually in @ref plugin_cleanup().
49 * @param plugin Must be @ref geany_plugin.
50 * @param item The item to add. */
51 void plugin_add_toolbar_item(GeanyPlugin *plugin, GtkToolItem *item)
53 GtkToolbar *toolbar = GTK_TOOLBAR(main_widgets.toolbar);
54 gint pos;
55 GeanyAutoSeparator *autosep;
57 g_return_if_fail(plugin);
58 autosep = &plugin->priv->toolbar_separator;
60 if (!autosep->widget)
62 GtkToolItem *sep;
64 pos = toolbar_get_insert_position();
66 sep = gtk_separator_tool_item_new();
67 gtk_toolbar_insert(toolbar, sep, pos);
68 autosep->widget = GTK_WIDGET(sep);
70 toolbar_item_ref(sep);
72 else
74 pos = gtk_toolbar_get_item_index(toolbar, GTK_TOOL_ITEM(autosep->widget));
75 g_return_if_fail(pos >= 0);
78 gtk_toolbar_insert(toolbar, item, pos + autosep->item_count + 1);
79 toolbar_item_ref(item);
81 /* hide the separator widget if there are no toolbar items showing for the plugin */
82 ui_auto_separator_add_ref(autosep, GTK_WIDGET(item));
86 /** Ensures that a plugin's module (*.so) will never be unloaded.
87 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
88 * using the GObject system.
90 * @param plugin Must be @ref geany_plugin.
92 * @since 0.16
94 void plugin_module_make_resident(GeanyPlugin *plugin)
96 g_return_if_fail(plugin);
98 g_module_make_resident(plugin->priv->module);
102 /** Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
103 * @param plugin Must be @ref geany_plugin.
104 * @param object Object to connect to, or @c NULL when using @link pluginsignals.c Geany signals @endlink.
105 * @param signal_name The name of the signal. For a list of available
106 * signals, please see the @link pluginsignals.c Signal documentation @endlink.
107 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
108 * (if supported by @a signal_name).
109 * @param callback The function to call when the signal is emitted.
110 * @param user_data The user data passed to the signal handler.
111 * @see plugin_callbacks.
113 * @warning Before version 1.25 (API < 218),
114 * this should only be used on objects that outlive the plugin, never on
115 * objects that will get destroyed before the plugin is unloaded. For objects
116 * created and destroyed by the plugin, you can simply use @c g_signal_connect(),
117 * since all handlers are disconnected when the object is destroyed anyway.
118 * For objects that may or may not outlive the plugin (like @link GeanyEditor.sci
119 * a document's @c ScintillaObject @endlink, which is destroyed when the document
120 * is closed), you currently have to manually handle both situations, when the
121 * plugin is unloaded before the object is destroyed (and then, you have to
122 * disconnect the signal on @c plugin_cleanup()), and when the object is destroyed
123 * during the plugin's lifetime (in which case you cannot and should not disconnect
124 * manually in @c plugin_cleanup() since it already has been disconnected and the
125 * object has been destroyed), and disconnect yourself or not as appropriate.
126 * @note Since version 1.25 (API >= 218), the object lifetime is watched and so the above
127 * restriction does not apply. However, for objects destroyed by the plugin,
128 * @c g_signal_connect() is safe and has lower overhead. */
129 void plugin_signal_connect(GeanyPlugin *plugin,
130 GObject *object, const gchar *signal_name, gboolean after,
131 GCallback callback, gpointer user_data)
133 gulong id;
134 SignalConnection sc;
136 g_return_if_fail(plugin != NULL);
137 g_return_if_fail(object == NULL || G_IS_OBJECT(object));
139 if (!object)
140 object = geany_object;
142 id = after ?
143 g_signal_connect_after(object, signal_name, callback, user_data) :
144 g_signal_connect(object, signal_name, callback, user_data);
146 if (!plugin->priv->signal_ids)
147 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
149 sc.object = object;
150 sc.handler_id = id;
151 g_array_append_val(plugin->priv->signal_ids, sc);
153 /* watch the object lifetime to nuke our pointers to it */
154 plugin_watch_object(plugin->priv, object);
158 typedef struct PluginSourceData
160 Plugin *plugin;
161 GList list_link; /* element of plugin->sources cointaining this GSource */
162 GSourceFunc function;
163 gpointer user_data;
164 } PluginSourceData;
167 /* prepend psd->list_link to psd->plugin->sources */
168 static void psd_register(PluginSourceData *psd, GSource *source)
170 psd->list_link.data = source;
171 psd->list_link.prev = NULL;
172 psd->list_link.next = psd->plugin->sources;
173 if (psd->list_link.next)
174 psd->list_link.next->prev = &psd->list_link;
175 psd->plugin->sources = &psd->list_link;
179 /* removes psd->list_link from psd->plugin->sources */
180 static void psd_unregister(PluginSourceData *psd)
182 if (psd->list_link.next)
183 psd->list_link.next->prev = psd->list_link.prev;
184 if (psd->list_link.prev)
185 psd->list_link.prev->next = psd->list_link.next;
186 else /* we were the first of the list, update the plugin->sources pointer */
187 psd->plugin->sources = psd->list_link.next;
191 static void on_plugin_source_destroy(gpointer data)
193 PluginSourceData *psd = data;
195 psd_unregister(psd);
196 g_slice_free1(sizeof *psd, psd);
200 static gboolean on_plugin_source_callback(gpointer data)
202 PluginSourceData *psd = data;
204 return psd->function(psd->user_data);
208 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
209 * unloading time */
210 static guint plugin_source_add(GeanyPlugin *plugin, GSource *source, GSourceFunc func, gpointer data)
212 guint id;
213 PluginSourceData *psd = g_slice_alloc(sizeof *psd);
215 psd->plugin = plugin->priv;
216 psd->function = func;
217 psd->user_data = data;
219 g_source_set_callback(source, on_plugin_source_callback, psd, on_plugin_source_destroy);
220 psd_register(psd, source);
221 id = g_source_attach(source, NULL);
222 g_source_unref(source);
224 return id;
228 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
229 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
231 * @param plugin Must be @ref geany_plugin.
232 * @param interval The time between calls to the function, in milliseconds.
233 * @param function The function to call after the given timeout.
234 * @param data The user data passed to the function.
235 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
236 * directly if you want to manage this event source manually).
238 * @see g_timeout_add()
239 * @since 0.21, plugin API 205.
241 guint plugin_timeout_add(GeanyPlugin *plugin, guint interval, GSourceFunc function, gpointer data)
243 return plugin_source_add(plugin, g_timeout_source_new(interval), function, data);
247 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
248 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
250 * @param plugin Must be @ref geany_plugin.
251 * @param interval The time between calls to the function, in seconds.
252 * @param function The function to call after the given timeout.
253 * @param data The user data passed to the function.
254 * @return the ID of the event source (you generally won't need it, or better use
255 * g_timeout_add_seconds() directly if you want to manage this event source manually).
257 * @see g_timeout_add_seconds()
258 * @since 0.21, plugin API 205.
260 guint plugin_timeout_add_seconds(GeanyPlugin *plugin, guint interval, GSourceFunc function,
261 gpointer data)
263 return plugin_source_add(plugin, g_timeout_source_new_seconds(interval), function, data);
267 /** Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
268 * it to run after the plugin has been unloaded (which may lead to a segfault).
270 * @param plugin Must be @ref geany_plugin.
271 * @param function The function to call in IDLE time.
272 * @param data The user data passed to the function.
273 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
274 * directly if you want to manage this event source manually).
276 * @see g_idle_add()
277 * @since 0.21, plugin API 205.
279 guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
281 return plugin_source_add(plugin, g_idle_source_new(), function, data);
285 /** Sets up or resizes a keybinding group for the plugin.
286 * You should then call keybindings_set_item() for each keybinding in the group.
287 * @param plugin Must be @ref geany_plugin.
288 * @param section_name Name used in the configuration file, such as @c "html_chars".
289 * @param count Number of keybindings for the group.
290 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
291 * @return The plugin's keybinding group.
292 * @since 0.19. */
293 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
294 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
296 Plugin *priv = plugin->priv;
298 priv->key_group = keybindings_set_group(priv->key_group, section_name,
299 priv->info.name, count, callback);
300 return priv->key_group;
304 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
306 p->configure_single(main_widgets.window);
310 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
312 GtkWidget *page = NULL; /* some plugins don't have prefs */
314 if (p->configure)
316 page = p->configure(GTK_DIALOG(dialog));
318 if (! GTK_IS_WIDGET(page))
320 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
321 p->info.name);
322 return NULL;
324 else
326 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
328 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
329 gtk_container_add(GTK_CONTAINER(align), page);
330 page = gtk_vbox_new(FALSE, 0);
331 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
334 else if (p->configure_single)
336 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
337 GtkWidget *btn;
339 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
341 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
342 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
343 gtk_container_add(GTK_CONTAINER(align), btn);
344 page = align;
346 return page;
350 /* multiple plugin configure dialog
351 * current_plugin can be NULL */
352 static void configure_plugins(Plugin *current_plugin)
354 GtkWidget *dialog, *vbox, *nb;
355 GList *node;
356 gint cur_page = -1;
358 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
359 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
360 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
361 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
362 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
363 gtk_widget_set_name(dialog, "GeanyDialog");
365 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
366 nb = gtk_notebook_new();
367 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
368 gtk_box_pack_start(GTK_BOX(vbox), nb, TRUE, TRUE, 0);
370 foreach_list(node, active_plugin_list)
372 Plugin *p = node->data;
373 GtkWidget *page = create_pref_page(p, dialog);
375 if (page)
377 GtkWidget *label = gtk_label_new(p->info.name);
378 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
380 if (p == current_plugin)
381 cur_page = n;
384 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
386 gtk_widget_show_all(vbox);
387 if (cur_page >= 0)
388 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
390 /* run the dialog */
391 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
393 else
394 utils_beep();
396 gtk_widget_destroy(dialog);
400 /** Shows the plugin's configure dialog.
401 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
402 * @param plugin Must be @ref geany_plugin.
403 * @since 0.19. */
404 /* if NULL, show all plugins */
405 void plugin_show_configure(GeanyPlugin *plugin)
407 Plugin *p;
409 if (!plugin)
411 configure_plugins(NULL);
412 return;
414 p = plugin->priv;
416 if (p->configure)
417 configure_plugins(p);
418 else
420 g_return_if_fail(p->configure_single);
421 p->configure_single(main_widgets.window);
426 struct BuilderConnectData
428 gpointer user_data;
429 GeanyPlugin *plugin;
433 static void connect_plugin_signals(GtkBuilder *builder, GObject *object,
434 const gchar *signal_name, const gchar *handler_name,
435 GObject *connect_object, GConnectFlags flags, gpointer user_data)
437 gpointer symbol = NULL;
438 struct BuilderConnectData *data = user_data;
440 if (!g_module_symbol(data->plugin->priv->module, handler_name, &symbol))
442 g_warning("Failed to locate signal handler for '%s': %s",
443 signal_name, g_module_error());
444 return;
447 plugin_signal_connect(data->plugin, object, signal_name, FALSE,
448 G_CALLBACK(symbol) /*ub?*/, data->user_data);
453 * Allows auto-connecting Glade/GtkBuilder signals in plugins.
455 * When a plugin uses GtkBuilder to load some UI from file/string,
456 * the gtk_builder_connect_signals() function is unable to automatically
457 * connect to the plugin's signal handlers. A plugin could itself use
458 * the gtk_builder_connect_signals_full() function to automatically
459 * connect to the signal handler functions by loading it's GModule
460 * and retrieving pointers to the handler functions, but rather than
461 * each plugin having to do that, this function handles it automatically.
463 * @code
464 * ...
465 * GeanyPlugin *geany_plugin;
467 * G_MODULE_EXPORT void
468 * myplugin_button_clicked(GtkButton *button, gpointer user_data)
470 * g_print("Button pressed\n");
473 * void plugin_init(GeanyData *data)
475 * GtkBuilder *builder = gtk_builder_new();
476 * gtk_builder_add_from_file(builder, "gui.glade", NULL);
477 * plugin_builder_connect_signals(geany_plugin, builder, NULL);
478 * ...
480 * @endcode
482 * @note It's important that you prefix your callback handlers with
483 * a plugin-specific prefix to avoid clashing with other plugins since
484 * the function symbols will be exported process-wide.
486 * @param plugin Must be @ref geany_plugin.
487 * @param builder The GtkBuilder to connect signals with.
488 * @param user_data User data to pass to the connected signal handlers.
490 * @since 1.24, plugin API 217.
492 void plugin_builder_connect_signals(GeanyPlugin *plugin,
493 GtkBuilder *builder, gpointer user_data)
495 struct BuilderConnectData data = { NULL };
497 g_return_if_fail(plugin != NULL && plugin->priv != NULL);
498 g_return_if_fail(plugin->priv->module != NULL);
499 g_return_if_fail(GTK_IS_BUILDER(builder));
501 data.user_data = user_data;
502 data.plugin = plugin;
504 gtk_builder_connect_signals_full(builder, connect_plugin_signals, &data);
508 #endif