Add note to ui_hookup_widget() doc comments.
[geany-mirror.git] / src / pluginutils.c
blob62ef11a4b5b947e69d91a193d932a8ab79e13bd2
1 /*
2 * pluginutils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2009-2011 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 /** @file pluginutils.h
24 * Plugin utility functions.
25 * These functions all take the @ref geany_plugin symbol as their first argument. */
27 #include "geany.h"
29 #ifdef HAVE_PLUGINS
31 #include "pluginutils.h"
32 #include "pluginprivate.h"
34 #include "ui_utils.h"
35 #include "toolbar.h"
36 #include "utils.h"
37 #include "support.h"
38 #include "plugins.h"
41 /** Inserts a toolbar item before the Quit button, or after the previous plugin toolbar item.
42 * A separator is added on the first call to this function, and will be shown when @a item is
43 * shown; hidden when @a item is hidden.
44 * @note You should still destroy @a item yourself, usually in @ref plugin_cleanup().
45 * @param plugin Must be @ref geany_plugin.
46 * @param item The item to add. */
47 void plugin_add_toolbar_item(GeanyPlugin *plugin, GtkToolItem *item)
49 GtkToolbar *toolbar = GTK_TOOLBAR(main_widgets.toolbar);
50 gint pos;
51 GeanyAutoSeparator *autosep;
53 g_return_if_fail(plugin);
54 autosep = &plugin->priv->toolbar_separator;
56 if (!autosep->widget)
58 GtkToolItem *sep;
60 pos = toolbar_get_insert_position();
62 sep = gtk_separator_tool_item_new();
63 gtk_toolbar_insert(toolbar, sep, pos);
64 autosep->widget = GTK_WIDGET(sep);
66 gtk_toolbar_insert(toolbar, item, pos + 1);
68 toolbar_item_ref(sep);
69 toolbar_item_ref(item);
71 else
73 pos = gtk_toolbar_get_item_index(toolbar, GTK_TOOL_ITEM(autosep->widget));
74 g_return_if_fail(pos >= 0);
75 gtk_toolbar_insert(toolbar, item, pos);
76 toolbar_item_ref(item);
78 /* hide the separator widget if there are no toolbar items showing for the plugin */
79 ui_auto_separator_add_ref(autosep, GTK_WIDGET(item));
83 /** Ensures that a plugin's module (*.so) will never be unloaded.
84 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
85 * using the GObject system.
87 * @param plugin Must be @ref geany_plugin.
89 * @since 0.16
91 void plugin_module_make_resident(GeanyPlugin *plugin)
93 g_return_if_fail(plugin);
95 g_module_make_resident(plugin->priv->module);
99 /** Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
100 * @param plugin Must be @ref geany_plugin.
101 * @param object Object to connect to, or @c NULL when using @link pluginsignals.c Geany signals @endlink.
102 * @param signal_name The name of the signal. For a list of available
103 * signals, please see the @link pluginsignals.c Signal documentation @endlink.
104 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
105 * (if supported by @a signal_name).
106 * @param callback The function to call when the signal is emitted.
107 * @param user_data The user data passed to the signal handler.
108 * @see plugin_callbacks. */
109 void plugin_signal_connect(GeanyPlugin *plugin,
110 GObject *object, const gchar *signal_name, gboolean after,
111 GCallback callback, gpointer user_data)
113 gulong id;
114 SignalConnection sc;
116 if (!object)
117 object = geany_object;
119 id = after ?
120 g_signal_connect_after(object, signal_name, callback, user_data) :
121 g_signal_connect(object, signal_name, callback, user_data);
123 if (!plugin->priv->signal_ids)
124 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
126 sc.object = object;
127 sc.handler_id = id;
128 g_array_append_val(plugin->priv->signal_ids, sc);
132 typedef struct PluginSourceData
134 Plugin *plugin;
135 GList list_link; /* element of plugin->sources cointaining this GSource */
136 GSourceFunc function;
137 gpointer user_data;
138 } PluginSourceData;
141 /* prepend psd->list_link to psd->plugin->sources */
142 static void psd_register(PluginSourceData *psd, GSource *source)
144 psd->list_link.data = source;
145 psd->list_link.prev = NULL;
146 psd->list_link.next = psd->plugin->sources;
147 if (psd->list_link.next)
148 psd->list_link.next->prev = &psd->list_link;
149 psd->plugin->sources = &psd->list_link;
153 /* removes psd->list_link from psd->plugin->sources */
154 static void psd_unregister(PluginSourceData *psd)
156 if (psd->list_link.next)
157 psd->list_link.next->prev = psd->list_link.prev;
158 if (psd->list_link.prev)
159 psd->list_link.prev->next = psd->list_link.next;
160 else /* we were the first of the list, update the plugin->sources pointer */
161 psd->plugin->sources = psd->list_link.next;
165 static void on_plugin_source_destroy(gpointer data)
167 PluginSourceData *psd = data;
169 psd_unregister(psd);
170 g_slice_free1(sizeof *psd, psd);
174 static gboolean on_plugin_source_callback(gpointer data)
176 PluginSourceData *psd = data;
178 return psd->function(psd->user_data);
182 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
183 * unloading time */
184 static guint plugin_source_add(GeanyPlugin *plugin, GSource *source, GSourceFunc func, gpointer data)
186 guint id;
187 PluginSourceData *psd = g_slice_alloc(sizeof *psd);
189 psd->plugin = plugin->priv;
190 psd->function = func;
191 psd->user_data = data;
193 g_source_set_callback(source, on_plugin_source_callback, psd, on_plugin_source_destroy);
194 psd_register(psd, source);
195 id = g_source_attach(source, NULL);
196 g_source_unref(source);
198 return id;
202 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
203 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
205 * @param plugin Must be @ref geany_plugin.
206 * @param interval The time between calls to the function, in milliseconds.
207 * @param function The function to call after the given timeout.
208 * @param data The user data passed to the function.
209 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
210 * directly if you want to manage this event source manually).
212 * @see g_timeout_add()
213 * @since 0.21, plugin API 205.
215 guint plugin_timeout_add(GeanyPlugin *plugin, guint interval, GSourceFunc function, gpointer data)
217 return plugin_source_add(plugin, g_timeout_source_new(interval), function, data);
221 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
222 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
224 * @param plugin Must be @ref geany_plugin.
225 * @param interval The time between calls to the function, in seconds.
226 * @param function The function to call after the given timeout.
227 * @param data The user data passed to the function.
228 * @return the ID of the event source (you generally won't need it, or better use
229 * g_timeout_add_seconds() directly if you want to manage this event source manually).
231 * @see g_timeout_add_seconds()
232 * @since 0.21, plugin API 205.
234 guint plugin_timeout_add_seconds(GeanyPlugin *plugin, guint interval, GSourceFunc function,
235 gpointer data)
237 return plugin_source_add(plugin, g_timeout_source_new_seconds(interval), function, data);
241 /** Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
242 * it to run after the plugin has been unloaded (which may lead to a segfault).
244 * @param plugin Must be @ref geany_plugin.
245 * @param function The function to call in IDLE time.
246 * @param data The user data passed to the function.
247 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
248 * directly if you want to manage this event source manually).
250 * @see g_idle_add()
251 * @since 0.21, plugin API 205.
253 guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
255 return plugin_source_add(plugin, g_idle_source_new(), function, data);
259 /** Sets up or resizes a keybinding group for the plugin.
260 * You should then call keybindings_set_item() for each keybinding in the group.
261 * @param plugin Must be @ref geany_plugin.
262 * @param section_name Name used in the configuration file, such as @c "html_chars".
263 * @param count Number of keybindings for the group.
264 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
265 * @return The plugin's keybinding group.
266 * @since 0.19. */
267 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
268 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
270 Plugin *priv = plugin->priv;
272 priv->key_group = keybindings_set_group(priv->key_group, section_name,
273 priv->info.name, count, callback);
274 return priv->key_group;
278 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
280 p->configure_single(main_widgets.window);
284 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
286 GtkWidget *page = NULL; /* some plugins don't have prefs */
288 if (p->configure)
290 page = p->configure(GTK_DIALOG(dialog));
292 if (! GTK_IS_WIDGET(page))
294 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
295 p->info.name);
296 return NULL;
298 else
300 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
302 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
303 gtk_container_add(GTK_CONTAINER(align), page);
304 page = gtk_vbox_new(FALSE, 0);
305 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
308 else if (p->configure_single)
310 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
311 GtkWidget *btn;
313 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
315 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
316 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
317 gtk_container_add(GTK_CONTAINER(align), btn);
318 page = align;
320 return page;
324 /* multiple plugin configure dialog
325 * current_plugin can be NULL */
326 static void configure_plugins(Plugin *current_plugin)
328 GtkWidget *dialog, *vbox, *nb;
329 GList *node;
330 gint cur_page = -1;
332 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
333 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
334 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
335 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
336 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
337 gtk_widget_set_name(dialog, "GeanyDialog");
339 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
340 nb = gtk_notebook_new();
341 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
342 gtk_container_add(GTK_CONTAINER(vbox), nb);
344 foreach_list(node, active_plugin_list)
346 Plugin *p = node->data;
347 GtkWidget *page = create_pref_page(p, dialog);
349 if (page)
351 GtkWidget *label = gtk_label_new(p->info.name);
352 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
354 if (p == current_plugin)
355 cur_page = n;
358 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
360 gtk_widget_show_all(vbox);
361 if (cur_page >= 0)
362 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
364 /* run the dialog */
365 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
367 else
368 utils_beep();
370 gtk_widget_destroy(dialog);
374 /** Shows the plugin's configure dialog.
375 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
376 * @param plugin Must be @ref geany_plugin.
377 * @since 0.19. */
378 /* if NULL, show all plugins */
379 void plugin_show_configure(GeanyPlugin *plugin)
381 Plugin *p;
383 if (!plugin)
385 configure_plugins(NULL);
386 return;
388 p = plugin->priv;
390 if (p->configure)
391 configure_plugins(p);
392 else
394 g_return_if_fail(p->configure_single);
395 p->configure_single(main_widgets.window);
400 #endif