Save encoding in session as text
[geany-mirror.git] / src / pluginutils.c
blobcf68d52e5b07e531c946aac68cadb288b48cc8e7
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 gtk_toolbar_insert(toolbar, item, pos + 1);
67 toolbar_item_ref(sep);
68 toolbar_item_ref(item);
70 else
72 pos = gtk_toolbar_get_item_index(toolbar, GTK_TOOL_ITEM(autosep->widget));
73 g_return_if_fail(pos >= 0);
74 gtk_toolbar_insert(toolbar, item, pos);
75 toolbar_item_ref(item);
77 /* hide the separator widget if there are no toolbar items showing for the plugin */
78 ui_auto_separator_add_ref(autosep, GTK_WIDGET(item));
82 /** Ensures that a plugin's module (*.so) will never be unloaded.
83 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
84 * using the GObject system.
86 * @param plugin Must be @ref geany_plugin.
88 * @since 0.16
90 void plugin_module_make_resident(GeanyPlugin *plugin)
92 g_return_if_fail(plugin);
94 g_module_make_resident(plugin->priv->module);
98 /** Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
99 * @param plugin Must be @ref geany_plugin.
100 * @param object Object to connect to, or @c NULL when using @link pluginsignals.c Geany signals @endlink.
101 * @param signal_name The name of the signal. For a list of available
102 * signals, please see the @link pluginsignals.c Signal documentation @endlink.
103 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
104 * (if supported by @a signal_name).
105 * @param callback The function to call when the signal is emitted.
106 * @param user_data The user data passed to the signal handler.
107 * @see plugin_callbacks. */
108 void plugin_signal_connect(GeanyPlugin *plugin,
109 GObject *object, const gchar *signal_name, gboolean after,
110 GCallback callback, gpointer user_data)
112 gulong id;
113 SignalConnection sc;
115 if (!object)
116 object = geany_object;
118 id = after ?
119 g_signal_connect_after(object, signal_name, callback, user_data) :
120 g_signal_connect(object, signal_name, callback, user_data);
122 if (!plugin->priv->signal_ids)
123 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
125 sc.object = object;
126 sc.handler_id = id;
127 g_array_append_val(plugin->priv->signal_ids, sc);
131 typedef struct PluginSourceData
133 Plugin *plugin;
134 GList list_link; /* element of plugin->sources cointaining this GSource */
135 GSourceFunc function;
136 gpointer user_data;
137 } PluginSourceData;
140 /* prepend psd->list_link to psd->plugin->sources */
141 static void psd_register(PluginSourceData *psd, GSource *source)
143 psd->list_link.data = source;
144 psd->list_link.prev = NULL;
145 psd->list_link.next = psd->plugin->sources;
146 if (psd->list_link.next)
147 psd->list_link.next->prev = &psd->list_link;
148 psd->plugin->sources = &psd->list_link;
152 /* removes psd->list_link from psd->plugin->sources */
153 static void psd_unregister(PluginSourceData *psd)
155 if (psd->list_link.next)
156 psd->list_link.next->prev = psd->list_link.prev;
157 if (psd->list_link.prev)
158 psd->list_link.prev->next = psd->list_link.next;
159 else /* we were the first of the list, update the plugin->sources pointer */
160 psd->plugin->sources = psd->list_link.next;
164 static void on_plugin_source_destroy(gpointer data)
166 PluginSourceData *psd = data;
168 psd_unregister(psd);
169 g_slice_free1(sizeof *psd, psd);
173 static gboolean on_plugin_source_callback(gpointer data)
175 PluginSourceData *psd = data;
177 return psd->function(psd->user_data);
181 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
182 * unloading time */
183 static guint plugin_source_add(GeanyPlugin *plugin, GSource *source, GSourceFunc func, gpointer data)
185 guint id;
186 PluginSourceData *psd = g_slice_alloc(sizeof *psd);
188 psd->plugin = plugin->priv;
189 psd->function = func;
190 psd->user_data = data;
192 g_source_set_callback(source, on_plugin_source_callback, psd, on_plugin_source_destroy);
193 psd_register(psd, source);
194 id = g_source_attach(source, NULL);
195 g_source_unref(source);
197 return id;
201 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
202 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
204 * @param plugin Must be @ref geany_plugin.
205 * @param interval The time between calls to the function, in milliseconds.
206 * @param function The function to call after the given timeout.
207 * @param data The user data passed to the function.
208 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
209 * directly if you want to manage this event source manually).
211 * @see g_timeout_add()
212 * @since 0.21, plugin API 205.
214 guint plugin_timeout_add(GeanyPlugin *plugin, guint interval, GSourceFunc function, gpointer data)
216 return plugin_source_add(plugin, g_timeout_source_new(interval), function, data);
220 /** Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
221 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
223 * @param plugin Must be @ref geany_plugin.
224 * @param interval The time between calls to the function, in seconds.
225 * @param function The function to call after the given timeout.
226 * @param data The user data passed to the function.
227 * @return the ID of the event source (you generally won't need it, or better use
228 * g_timeout_add_seconds() directly if you want to manage this event source manually).
230 * @see g_timeout_add_seconds()
231 * @since 0.21, plugin API 205.
233 guint plugin_timeout_add_seconds(GeanyPlugin *plugin, guint interval, GSourceFunc function,
234 gpointer data)
236 return plugin_source_add(plugin, g_timeout_source_new_seconds(interval), function, data);
240 /** Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
241 * it to run after the plugin has been unloaded (which may lead to a segfault).
243 * @param plugin Must be @ref geany_plugin.
244 * @param function The function to call in IDLE time.
245 * @param data The user data passed to the function.
246 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
247 * directly if you want to manage this event source manually).
249 * @see g_idle_add()
250 * @since 0.21, plugin API 205.
252 guint plugin_idle_add(GeanyPlugin *plugin, GSourceFunc function, gpointer data)
254 return plugin_source_add(plugin, g_idle_source_new(), function, data);
258 /** Sets up or resizes a keybinding group for the plugin.
259 * You should then call keybindings_set_item() for each keybinding in the group.
260 * @param plugin Must be @ref geany_plugin.
261 * @param section_name Name used in the configuration file, such as @c "html_chars".
262 * @param count Number of keybindings for the group.
263 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
264 * @return The plugin's keybinding group.
265 * @since 0.19. */
266 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
267 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
269 Plugin *priv = plugin->priv;
271 priv->key_group = keybindings_set_group(priv->key_group, section_name,
272 priv->info.name, count, callback);
273 return priv->key_group;
277 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
279 p->configure_single(main_widgets.window);
283 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
285 GtkWidget *page = NULL; /* some plugins don't have prefs */
287 if (p->configure)
289 page = p->configure(GTK_DIALOG(dialog));
291 if (! GTK_IS_WIDGET(page))
293 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
294 p->info.name);
295 return NULL;
297 else
299 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
301 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
302 gtk_container_add(GTK_CONTAINER(align), page);
303 page = gtk_vbox_new(FALSE, 0);
304 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
307 else if (p->configure_single)
309 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
310 GtkWidget *btn;
312 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
314 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
315 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
316 gtk_container_add(GTK_CONTAINER(align), btn);
317 page = align;
319 return page;
323 /* multiple plugin configure dialog
324 * current_plugin can be NULL */
325 static void configure_plugins(Plugin *current_plugin)
327 GtkWidget *dialog, *vbox, *nb;
328 GList *node;
329 gint cur_page = -1;
331 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
332 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
333 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
334 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
335 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
336 gtk_widget_set_name(dialog, "GeanyDialog");
338 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
339 nb = gtk_notebook_new();
340 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
341 gtk_container_add(GTK_CONTAINER(vbox), nb);
343 foreach_list(node, active_plugin_list)
345 Plugin *p = node->data;
346 GtkWidget *page = create_pref_page(p, dialog);
348 if (page)
350 GtkWidget *label = gtk_label_new(p->info.name);
351 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
353 if (p == current_plugin)
354 cur_page = n;
357 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
359 gtk_widget_show_all(vbox);
360 if (cur_page >= 0)
361 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
363 /* run the dialog */
364 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
366 else
367 utils_beep();
369 gtk_widget_destroy(dialog);
373 /** Shows the plugin's configure dialog.
374 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
375 * @param plugin Must be @ref geany_plugin.
376 * @since 0.19. */
377 /* if NULL, show all plugins */
378 void plugin_show_configure(GeanyPlugin *plugin)
380 Plugin *p;
382 if (!plugin)
384 configure_plugins(NULL);
385 return;
387 p = plugin->priv;
389 if (p->configure)
390 configure_plugins(p);
391 else
393 g_return_if_fail(p->configure_single);
394 p->configure_single(main_widgets.window);
399 #endif