r5162 | eht16 | 2010-08-15 13:53:09 +0100 (Sun, 15 Aug 2010) | 1 line
[geany-mirror.git] / src / pluginutils.c
blob0c8e55ece783f991f41e3f540887ecf8c519a0b8
1 /*
2 * pluginutils.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2009-2010 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.
22 * $Id$
25 /** @file pluginutils.h
26 * Plugin utility functions.
27 * These functions all take the @ref geany_plugin symbol as their first argument. */
29 #include "geany.h"
31 #ifdef HAVE_PLUGINS
33 #include "pluginutils.h"
34 #include "pluginprivate.h"
36 #include "ui_utils.h"
37 #include "toolbar.h"
38 #include "utils.h"
39 #include "support.h"
40 #include "plugins.h"
43 /** Inserts a toolbar item before the Quit button, or after the previous plugin toolbar item.
44 * A separator is added on the first call to this function, and will be shown when @a item is
45 * shown; hidden when @a item is hidden.
46 * @note You should still destroy @a item yourself, usually in @ref plugin_cleanup().
47 * @param plugin Must be @ref geany_plugin.
48 * @param item The item to add. */
49 void plugin_add_toolbar_item(GeanyPlugin *plugin, GtkToolItem *item)
51 GtkToolbar *toolbar = GTK_TOOLBAR(main_widgets.toolbar);
52 gint pos;
53 GeanyAutoSeparator *autosep;
55 g_return_if_fail(plugin);
56 autosep = &plugin->priv->toolbar_separator;
58 if (!autosep->widget)
60 GtkToolItem *sep;
62 pos = toolbar_get_insert_position();
64 sep = gtk_separator_tool_item_new();
65 gtk_toolbar_insert(toolbar, sep, pos);
66 autosep->widget = GTK_WIDGET(sep);
68 gtk_toolbar_insert(toolbar, item, pos + 1);
70 toolbar_item_ref(sep);
71 toolbar_item_ref(item);
73 else
75 pos = gtk_toolbar_get_item_index(toolbar, GTK_TOOL_ITEM(autosep->widget));
76 g_return_if_fail(pos >= 0);
77 gtk_toolbar_insert(toolbar, item, pos);
78 toolbar_item_ref(item);
80 /* hide the separator widget if there are no toolbar items showing for the plugin */
81 ui_auto_separator_add_ref(autosep, GTK_WIDGET(item));
85 /** Ensures that a plugin's module (*.so) will never be unloaded.
86 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
87 * using the GObject system.
89 * @param plugin Must be @ref geany_plugin.
91 * @since 0.16
93 void plugin_module_make_resident(GeanyPlugin *plugin)
95 g_return_if_fail(plugin);
97 g_module_make_resident(plugin->priv->module);
101 /** Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
102 * @param plugin Must be @ref geany_plugin.
103 * @param object Object to connect to, or @c NULL when using @link signals Geany signals @endlink.
104 * @param signal_name The name of the signal. For a list of available
105 * signals, please see the @link signals Signal documentation @endlink.
106 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
107 * (if supported by @a signal_name).
108 * @param callback The function to call when the signal is emitted.
109 * @param user_data The user data passed to the signal handler.
110 * @see plugin_callbacks. */
111 void plugin_signal_connect(GeanyPlugin *plugin,
112 GObject *object, const gchar *signal_name, gboolean after,
113 GCallback callback, gpointer user_data)
115 gulong id;
116 SignalConnection sc;
118 if (!object)
119 object = geany_object;
121 id = after ?
122 g_signal_connect_after(object, signal_name, callback, user_data) :
123 g_signal_connect(object, signal_name, callback, user_data);
125 if (!plugin->priv->signal_ids)
126 plugin->priv->signal_ids = g_array_new(FALSE, FALSE, sizeof(SignalConnection));
128 sc.object = object;
129 sc.handler_id = id;
130 g_array_append_val(plugin->priv->signal_ids, sc);
134 /** Sets up or resizes a keybinding group for the plugin.
135 * You should then call keybindings_set_item() for each keybinding in the group.
136 * @param plugin Must be @ref geany_plugin.
137 * @param section_name Name used in the configuration file, such as @c "html_chars".
138 * @param count Number of keybindings for the group.
139 * @param callback Group callback, or @c NULL if you only want individual keybinding callbacks.
140 * @return The plugin's keybinding group.
141 * @since 0.19. */
142 GeanyKeyGroup *plugin_set_key_group(GeanyPlugin *plugin,
143 const gchar *section_name, gsize count, GeanyKeyGroupCallback callback)
145 Plugin *priv = plugin->priv;
147 priv->key_group = keybindings_set_group(priv->key_group, section_name,
148 priv->info.name, count, callback);
149 return priv->key_group;
153 static void on_pref_btn_clicked(gpointer btn, Plugin *p)
155 p->configure_single(main_widgets.window);
159 static GtkWidget *create_pref_page(Plugin *p, GtkWidget *dialog)
161 GtkWidget *page = NULL; /* some plugins don't have prefs */
163 if (p->configure)
165 page = p->configure(GTK_DIALOG(dialog));
167 if (! GTK_IS_WIDGET(page))
169 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
170 p->info.name);
171 return NULL;
173 else
175 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 1, 1);
177 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
178 gtk_container_add(GTK_CONTAINER(align), page);
179 page = gtk_vbox_new(FALSE, 0);
180 gtk_box_pack_start(GTK_BOX(page), align, TRUE, TRUE, 0);
183 else if (p->configure_single)
185 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 0, 0);
186 GtkWidget *btn;
188 gtk_alignment_set_padding(GTK_ALIGNMENT(align), 6, 6, 6, 6);
190 btn = gtk_button_new_from_stock(GTK_STOCK_PREFERENCES);
191 g_signal_connect(btn, "clicked", G_CALLBACK(on_pref_btn_clicked), p);
192 gtk_container_add(GTK_CONTAINER(align), btn);
193 page = align;
195 return page;
199 /* multiple plugin configure dialog
200 * current_plugin can be NULL */
201 static void configure_plugins(Plugin *current_plugin)
203 GtkWidget *dialog, *vbox, *nb;
204 GList *node;
205 gint cur_page = -1;
207 dialog = gtk_dialog_new_with_buttons(_("Configure Plugins"),
208 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
209 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
210 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
211 GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
212 gtk_widget_set_name(dialog, "GeanyDialog");
214 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
215 nb = gtk_notebook_new();
216 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb), TRUE);
217 gtk_container_add(GTK_CONTAINER(vbox), nb);
219 foreach_list(node, active_plugin_list)
221 Plugin *p = node->data;
222 GtkWidget *page = create_pref_page(p, dialog);
224 if (page)
226 GtkWidget *label = gtk_label_new(p->info.name);
227 gint n = gtk_notebook_append_page(GTK_NOTEBOOK(nb), page, label);
229 if (p == current_plugin)
230 cur_page = n;
233 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb)))
235 gtk_widget_show_all(vbox);
236 if (cur_page >= 0)
237 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb), cur_page);
239 /* run the dialog */
240 while (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_APPLY);
242 else
243 utils_beep();
245 gtk_widget_destroy(dialog);
249 /** Shows the plugin's configure dialog.
250 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
251 * @param plugin Must be @ref geany_plugin.
252 * @since 0.19. */
253 /* if NULL, show all plugins */
254 void plugin_show_configure(GeanyPlugin *plugin)
256 Plugin *p;
258 if (!plugin)
260 configure_plugins(NULL);
261 return;
263 p = plugin->priv;
265 if (p->configure)
266 configure_plugins(p);
267 else
269 g_return_if_fail(p->configure_single);
270 p->configure_single(main_widgets.window);
275 #endif