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. */
32 #include "pluginutils.h"
35 #include "geanyobject.h"
36 #include "keybindings.h"
37 #include "keybindingsprivate.h"
38 #include "plugindata.h"
39 #include "pluginprivate.h"
47 /** Inserts a toolbar item before the Quit button, or after the previous plugin toolbar item.
48 * A separator is added on the first call to this function, and will be shown when @a item is
49 * shown; hidden when @a item is hidden.
50 * @note You should still destroy @a item yourself, usually in @ref plugin_cleanup().
51 * @param plugin Must be @ref geany_plugin.
52 * @param item The item to add. */
54 void plugin_add_toolbar_item(GeanyPlugin
*plugin
, GtkToolItem
*item
)
56 GtkToolbar
*toolbar
= GTK_TOOLBAR(main_widgets
.toolbar
);
58 GeanyAutoSeparator
*autosep
;
60 g_return_if_fail(plugin
);
61 autosep
= &plugin
->priv
->toolbar_separator
;
67 pos
= toolbar_get_insert_position();
69 sep
= gtk_separator_tool_item_new();
70 gtk_toolbar_insert(toolbar
, sep
, pos
);
71 autosep
->widget
= GTK_WIDGET(sep
);
73 toolbar_item_ref(sep
);
77 pos
= gtk_toolbar_get_item_index(toolbar
, GTK_TOOL_ITEM(autosep
->widget
));
78 g_return_if_fail(pos
>= 0);
81 gtk_toolbar_insert(toolbar
, item
, pos
+ autosep
->item_count
+ 1);
82 toolbar_item_ref(item
);
84 /* hide the separator widget if there are no toolbar items showing for the plugin */
85 ui_auto_separator_add_ref(autosep
, GTK_WIDGET(item
));
89 /** Ensures that a plugin's module (*.so) will never be unloaded.
90 * This is necessary if you register new GTypes in your plugin, e.g. when using own classes
91 * using the GObject system.
93 * @param plugin Must be @ref geany_plugin.
98 void plugin_module_make_resident(GeanyPlugin
*plugin
)
100 g_return_if_fail(plugin
);
101 plugin_make_resident(plugin
->priv
);
106 * Connects a signal which will be disconnected on unloading the plugin, to prevent a possible segfault.
107 * @param plugin Must be @ref geany_plugin.
108 * @param object @nullable Object to connect to, or @c NULL when using @link pluginsignals.c Geany signals @endlink.
109 * @param signal_name The name of the signal. For a list of available
110 * signals, please see the @link pluginsignals.c Signal documentation @endlink.
111 * @param after Set to @c TRUE to call your handler after the main signal handlers have been called
112 * (if supported by @a signal_name).
113 * @param callback The function to call when the signal is emitted.
114 * @param user_data The user data passed to the signal handler.
115 * @see plugin_callbacks.
117 * @warning Before version 1.25 (API < 218),
118 * this should only be used on objects that outlive the plugin, never on
119 * objects that will get destroyed before the plugin is unloaded. For objects
120 * created and destroyed by the plugin, you can simply use @c g_signal_connect(),
121 * since all handlers are disconnected when the object is destroyed anyway.
122 * For objects that may or may not outlive the plugin (like @link GeanyEditor.sci
123 * a document's @c ScintillaObject @endlink, which is destroyed when the document
124 * is closed), you currently have to manually handle both situations, when the
125 * plugin is unloaded before the object is destroyed (and then, you have to
126 * disconnect the signal on @c plugin_cleanup()), and when the object is destroyed
127 * during the plugin's lifetime (in which case you cannot and should not disconnect
128 * manually in @c plugin_cleanup() since it already has been disconnected and the
129 * object has been destroyed), and disconnect yourself or not as appropriate.
130 * @note Since version 1.25 (API >= 218), the object lifetime is watched and so the above
131 * restriction does not apply. However, for objects destroyed by the plugin,
132 * @c g_signal_connect() is safe and has lower overhead.
135 void plugin_signal_connect(GeanyPlugin
*plugin
,
136 GObject
*object
, const gchar
*signal_name
, gboolean after
,
137 GCallback callback
, gpointer user_data
)
142 g_return_if_fail(plugin
!= NULL
);
143 g_return_if_fail(object
== NULL
|| G_IS_OBJECT(object
));
146 object
= geany_object
;
149 g_signal_connect_after(object
, signal_name
, callback
, user_data
) :
150 g_signal_connect(object
, signal_name
, callback
, user_data
);
152 if (!plugin
->priv
->signal_ids
)
153 plugin
->priv
->signal_ids
= g_array_new(FALSE
, FALSE
, sizeof(SignalConnection
));
157 g_array_append_val(plugin
->priv
->signal_ids
, sc
);
159 /* watch the object lifetime to nuke our pointers to it */
160 plugin_watch_object(plugin
->priv
, object
);
164 typedef struct PluginSourceData
167 GList list_link
; /* element of plugin->sources cointaining this GSource */
168 GSourceFunc function
;
173 /* prepend psd->list_link to psd->plugin->sources */
174 static void psd_register(PluginSourceData
*psd
, GSource
*source
)
176 psd
->list_link
.data
= source
;
177 psd
->list_link
.prev
= NULL
;
178 psd
->list_link
.next
= psd
->plugin
->sources
;
179 if (psd
->list_link
.next
)
180 psd
->list_link
.next
->prev
= &psd
->list_link
;
181 psd
->plugin
->sources
= &psd
->list_link
;
185 /* removes psd->list_link from psd->plugin->sources */
186 static void psd_unregister(PluginSourceData
*psd
)
188 if (psd
->list_link
.next
)
189 psd
->list_link
.next
->prev
= psd
->list_link
.prev
;
190 if (psd
->list_link
.prev
)
191 psd
->list_link
.prev
->next
= psd
->list_link
.next
;
192 else /* we were the first of the list, update the plugin->sources pointer */
193 psd
->plugin
->sources
= psd
->list_link
.next
;
197 static void on_plugin_source_destroy(gpointer data
)
199 PluginSourceData
*psd
= data
;
202 g_slice_free1(sizeof *psd
, psd
);
206 static gboolean
on_plugin_source_callback(gpointer data
)
208 PluginSourceData
*psd
= data
;
210 return psd
->function(psd
->user_data
);
214 /* adds the given source to the default GMainContext and to the list of sources to remove at plugin
216 static guint
plugin_source_add(GeanyPlugin
*plugin
, GSource
*source
, GSourceFunc func
, gpointer data
)
219 PluginSourceData
*psd
= g_slice_alloc(sizeof *psd
);
221 psd
->plugin
= plugin
->priv
;
222 psd
->function
= func
;
223 psd
->user_data
= data
;
225 g_source_set_callback(source
, on_plugin_source_callback
, psd
, on_plugin_source_destroy
);
226 psd_register(psd
, source
);
227 id
= g_source_attach(source
, NULL
);
228 g_source_unref(source
);
235 * Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
236 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
238 * @param plugin Must be @ref geany_plugin.
239 * @param interval The time between calls to the function, in milliseconds.
240 * @param function The function to call after the given timeout.
241 * @param data The user data passed to the function.
242 * @return the ID of the event source (you generally won't need it, or better use g_timeout_add()
243 * directly if you want to manage this event source manually).
245 * @see g_timeout_add()
246 * @since 0.21, plugin API 205.
249 guint
plugin_timeout_add(GeanyPlugin
*plugin
, guint interval
, GSourceFunc function
, gpointer data
)
251 return plugin_source_add(plugin
, g_timeout_source_new(interval
), function
, data
);
256 * Adds a GLib main loop timeout callback that will be removed when unloading the plugin,
257 * preventing it to run after the plugin has been unloaded (which may lead to a segfault).
259 * @param plugin Must be @ref geany_plugin.
260 * @param interval The time between calls to the function, in seconds.
261 * @param function The function to call after the given timeout.
262 * @param data The user data passed to the function.
263 * @return the ID of the event source (you generally won't need it, or better use
264 * g_timeout_add_seconds() directly if you want to manage this event source manually).
266 * @see g_timeout_add_seconds()
267 * @since 0.21, plugin API 205.
270 guint
plugin_timeout_add_seconds(GeanyPlugin
*plugin
, guint interval
, GSourceFunc function
,
273 return plugin_source_add(plugin
, g_timeout_source_new_seconds(interval
), function
, data
);
278 * Adds a GLib main loop IDLE callback that will be removed when unloading the plugin, preventing
279 * it to run after the plugin has been unloaded (which may lead to a segfault).
281 * @param plugin Must be @ref geany_plugin.
282 * @param function The function to call in IDLE time.
283 * @param data The user data passed to the function.
284 * @return the ID of the event source (you generally won't need it, or better use g_idle_add()
285 * directly if you want to manage this event source manually).
288 * @since 0.21, plugin API 205.
291 guint
plugin_idle_add(GeanyPlugin
*plugin
, GSourceFunc function
, gpointer data
)
293 return plugin_source_add(plugin
, g_idle_source_new(), function
, data
);
298 * Sets up or resizes a keybinding group for the plugin.
299 * You should then call keybindings_set_item() for each keybinding in the group.
300 * @param plugin Must be @ref geany_plugin.
301 * @param section_name Name of the section used for this group in the keybindings configuration file, i.e. @c "html_chars".
302 * @param count Number of keybindings for the group.
303 * @param callback @nullable Group callback, or @c NULL if you only want individual keybinding callbacks.
304 * @return The plugin's keybinding group.
308 GeanyKeyGroup
*plugin_set_key_group(GeanyPlugin
*plugin
,
309 const gchar
*section_name
, gsize count
, GeanyKeyGroupCallback callback
)
311 Plugin
*priv
= plugin
->priv
;
313 priv
->key_group
= keybindings_set_group(priv
->key_group
, section_name
,
314 priv
->info
.name
, count
, callback
);
315 return priv
->key_group
;
318 /** Sets up or resizes a keybinding group for the plugin
320 * You should then call keybindings_set_item() or keybindings_set_item_full() for each
321 * keybinding in the group.
322 * @param plugin Must be @ref geany_plugin.
323 * @param section_name Name of the section used for this group in the keybindings configuration file, i.e. @c "html_chars".
324 * @param count Number of keybindings for the group.
325 * @param cb @nullable New-style group callback, or @c NULL if you only want individual keybinding callbacks.
326 * @param pdata Plugin specific data, passed to the group callback @a cb.
327 * @param destroy_notify Function that is invoked to free the plugin data when not needed anymore.
328 * @return @transfer{none} The plugin's keybinding group.
330 * @since 1.26 (API 226)
331 * @see See keybindings_set_item
332 * @see See keybindings_set_item_full
335 GeanyKeyGroup
*plugin_set_key_group_full(GeanyPlugin
*plugin
,
336 const gchar
*section_name
, gsize count
,
337 GeanyKeyGroupFunc cb
, gpointer pdata
, GDestroyNotify destroy_notify
)
339 GeanyKeyGroup
*group
;
341 group
= plugin_set_key_group(plugin
, section_name
, count
, NULL
);
343 group
->cb_data
= pdata
;
344 group
->cb_data_destroy
= destroy_notify
;
350 static void on_pref_btn_clicked(gpointer btn
, Plugin
*p
)
352 p
->configure_single(main_widgets
.window
);
356 static GtkWidget
*create_pref_page(Plugin
*p
, GtkWidget
*dialog
)
358 GtkWidget
*page
= NULL
; /* some plugins don't have prefs */
360 if (p
->cbs
.configure
)
362 page
= p
->cbs
.configure(&p
->public, GTK_DIALOG(dialog
), p
->cb_data
);
363 if (! GTK_IS_WIDGET(page
))
365 geany_debug("Invalid widget returned from plugin_configure() in plugin \"%s\"!",
371 GtkWidget
*align
= gtk_alignment_new(0.5, 0.5, 1, 1);
373 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 6, 6, 6, 6);
374 gtk_container_add(GTK_CONTAINER(align
), page
);
375 page
= gtk_vbox_new(FALSE
, 0);
376 gtk_box_pack_start(GTK_BOX(page
), align
, TRUE
, TRUE
, 0);
379 else if (p
->configure_single
)
381 GtkWidget
*align
= gtk_alignment_new(0.5, 0.5, 0, 0);
384 gtk_alignment_set_padding(GTK_ALIGNMENT(align
), 6, 6, 6, 6);
386 btn
= gtk_button_new_from_stock(GTK_STOCK_PREFERENCES
);
387 g_signal_connect(btn
, "clicked", G_CALLBACK(on_pref_btn_clicked
), p
);
388 gtk_container_add(GTK_CONTAINER(align
), btn
);
395 /* multiple plugin configure dialog
396 * current_plugin can be NULL */
397 static void configure_plugins(Plugin
*current_plugin
)
399 GtkWidget
*dialog
, *vbox
, *nb
;
403 dialog
= gtk_dialog_new_with_buttons(_("Configure Plugins"),
404 GTK_WINDOW(main_widgets
.window
), GTK_DIALOG_DESTROY_WITH_PARENT
,
405 GTK_STOCK_APPLY
, GTK_RESPONSE_APPLY
,
406 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
407 GTK_STOCK_OK
, GTK_RESPONSE_OK
, NULL
);
408 gtk_widget_set_name(dialog
, "GeanyDialog");
410 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
411 nb
= gtk_notebook_new();
412 gtk_notebook_set_scrollable(GTK_NOTEBOOK(nb
), TRUE
);
413 gtk_box_pack_start(GTK_BOX(vbox
), nb
, TRUE
, TRUE
, 0);
415 foreach_list(node
, active_plugin_list
)
417 Plugin
*p
= node
->data
;
418 GtkWidget
*page
= create_pref_page(p
, dialog
);
422 GtkWidget
*label
= gtk_label_new(p
->info
.name
);
423 gint n
= gtk_notebook_append_page(GTK_NOTEBOOK(nb
), page
, label
);
425 if (p
== current_plugin
)
429 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(nb
)))
431 gtk_widget_show_all(vbox
);
433 gtk_notebook_set_current_page(GTK_NOTEBOOK(nb
), cur_page
);
436 while (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_APPLY
);
441 gtk_widget_destroy(dialog
);
445 /** Shows the plugin's configure dialog.
446 * The plugin must implement one of the plugin_configure() or plugin_configure_single() symbols.
447 * @param plugin Must be @ref geany_plugin.
449 /* if NULL, show all plugins */
451 void plugin_show_configure(GeanyPlugin
*plugin
)
457 configure_plugins(NULL
);
462 if (p
->cbs
.configure
)
463 configure_plugins(p
);
466 g_return_if_fail(p
->configure_single
);
467 p
->configure_single(main_widgets
.window
);
472 struct BuilderConnectData
479 static void connect_plugin_signals(GtkBuilder
*builder
, GObject
*object
,
480 const gchar
*signal_name
, const gchar
*handler_name
,
481 GObject
*connect_object
, GConnectFlags flags
, gpointer user_data
)
483 gpointer symbol
= NULL
;
484 struct BuilderConnectData
*data
= user_data
;
486 symbol
= plugin_get_module_symbol(data
->plugin
->priv
, handler_name
);
488 plugin_signal_connect(data
->plugin
, object
, signal_name
, FALSE
,
489 G_CALLBACK(symbol
) /*ub?*/, data
->user_data
);
494 * Allows auto-connecting Glade/GtkBuilder signals in plugins.
496 * When a plugin uses GtkBuilder to load some UI from file/string,
497 * the gtk_builder_connect_signals() function is unable to automatically
498 * connect to the plugin's signal handlers. A plugin could itself use
499 * the gtk_builder_connect_signals_full() function to automatically
500 * connect to the signal handler functions by loading it's GModule
501 * and retrieving pointers to the handler functions, but rather than
502 * each plugin having to do that, this function handles it automatically.
506 * GeanyPlugin *geany_plugin;
508 * G_MODULE_EXPORT void
509 * myplugin_button_clicked(GtkButton *button, gpointer user_data)
511 * g_print("Button pressed\n");
514 * void plugin_init(GeanyData *data)
516 * GtkBuilder *builder = gtk_builder_new();
517 * gtk_builder_add_from_file(builder, "gui.glade", NULL);
518 * plugin_builder_connect_signals(geany_plugin, builder, NULL);
523 * @note It's important that you prefix your callback handlers with
524 * a plugin-specific prefix to avoid clashing with other plugins since
525 * the function symbols will be exported process-wide.
527 * @param plugin Must be @ref geany_plugin.
528 * @param builder The GtkBuilder to connect signals with.
529 * @param user_data User data to pass to the connected signal handlers.
531 * @since 1.24, plugin API 217.
534 void plugin_builder_connect_signals(GeanyPlugin
*plugin
,
535 GtkBuilder
*builder
, gpointer user_data
)
537 struct BuilderConnectData data
= { NULL
};
539 g_return_if_fail(plugin
!= NULL
&& plugin
->priv
!= NULL
);
540 g_return_if_fail(GTK_IS_BUILDER(builder
));
542 data
.user_data
= user_data
;
543 data
.plugin
= plugin
;
545 gtk_builder_connect_signals_full(builder
, connect_plugin_signals
, &data
);
549 /** Add additional data that corresponds to the plugin.
551 * @p pdata is the pointer going to be passed to the individual plugin callbacks
552 * of GeanyPlugin::funcs. When the plugin is cleaned up, @p free_func is invoked for the data,
553 * which connects the data to the time the plugin is enabled.
555 * One intended use case is to set GObjects as data and have them destroyed automatically
556 * by passing g_object_unref() as @a free_func, so that member functions can be used
557 * for the @ref GeanyPluginFuncs (via wrappers) but you can set completely custom data.
559 * Be aware that this can only be called once and only by plugins registered via
560 * @ref geany_plugin_register(). So-called legacy plugins cannot use this function.
562 * @note This function must not be called if the plugin was registered with
563 * geany_plugin_register_full().
565 * @param plugin The plugin provided by Geany
566 * @param pdata The plugin's data to associate, must not be @c NULL
567 * @param free_func The destroy notify
569 * @since 1.26 (API 225)
572 void geany_plugin_set_data(GeanyPlugin
*plugin
, gpointer pdata
, GDestroyNotify free_func
)
574 Plugin
*p
= plugin
->priv
;
576 g_return_if_fail(PLUGIN_LOADED_OK(p
));
577 /* Do not allow calling this only to set a notify. */
578 g_return_if_fail(pdata
!= NULL
);
579 /* The rationale to allow only setting the data once is the following:
580 * In the future we want to support proxy plugins (which bind non-C plugins to
581 * Geany's plugin api). These proxy plugins might need to own the data pointer
582 * on behalf of the proxied plugin. However, if not, then the plugin should be
583 * free to use it. This way we can make sure the plugin doesn't accidentally
586 * Better a more limited API now that can be opened up later than a potentially
587 * wrong one that can only be replaced by another one. */
588 if (p
->cb_data
!= NULL
|| p
->cb_data_destroy
!= NULL
)
590 if (PLUGIN_HAS_LOAD_DATA(p
))
591 g_warning("Invalid call to %s(), geany_plugin_register_full() was used. Ignored!\n", G_STRFUNC
);
593 g_warning("Double call to %s(), ignored!", G_STRFUNC
);
598 p
->cb_data_destroy
= free_func
;