[geany/geany] 58c814: plugins: Pass pdata to PluginCallback function by default

Thomas Martitz git-noreply at xxxxx
Sun Aug 23 18:01:41 UTC 2015


Branch:      refs/heads/master
Author:      Thomas Martitz <kugel at rockbox.org>
Committer:   Thomas Martitz <kugel at rockbox.org>
Date:        Sun, 23 Aug 2015 18:01:41 UTC
Commit:      58c8144afc10d31ce4473d70fd542369349c1f70
             https://github.com/geany/geany/commit/58c8144afc10d31ce4473d70fd542369349c1f70

Log Message:
-----------
plugins: Pass pdata to PluginCallback function by default

If the plugin did not set its own user_data we set it to whatever it set
with geany_plugin_register_full() or geany_plugin_set_data().
This is particularly convinient because PluginCallback is usually statically
allocated, at which point dynamically allocated plugin data doesn't exists yet.


Modified Paths:
--------------
    plugins/demoplugin.c
    src/plugindata.h
    src/plugins.c

Modified: plugins/demoplugin.c
16 lines changed, 13 insertions(+), 3 deletions(-)
===================================================================
@@ -43,7 +43,11 @@ static gchar *welcome_text = NULL;
 static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
 								 SCNotification *nt, gpointer data)
 {
-	GeanyData *geany_data = data;
+	/* data == GeanyPlugin because the data member of PluginCallback was set to NULL
+	 * and this plugin has called geany_plugin_set_data() with the GeanyPlugin pointer as
+	 * data */
+	GeanyPlugin *plugin = data;
+	GeanyData *geany_data = plugin->geany_data;
 
 	/* For detailed documentation about the SCNotification struct, please see
 	 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
@@ -137,8 +141,14 @@ static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
 
 	welcome_text = g_strdup(_("Hello World!"));
 
-	demo_callbacks[0].user_data = geany_data;
-
+	/* This might seem strange but is a method to get the GeanyPlugin pointer passed to
+	 * on_editor_notify(). PluginCallback functions get the same data that was set via
+	 * GEANY_PLUING_REGISTER_FULL() or geany_plugin_set_data() by default (unless the data pointer
+	 * was set to non-NULL at compile time).
+	 * This is really only done for demoing PluginCallback. Actual plugins will use real custom
+	 * data and perhaps embed the GeanyPlugin or GeanyData pointer their if they also use
+	 * PluginCallback. */
+	geany_plugin_set_data(plugin, plugin, NULL);
 	return TRUE;
 }
 


Modified: src/plugindata.h
4 lines changed, 3 insertions(+), 1 deletions(-)
===================================================================
@@ -185,7 +185,9 @@ typedef struct PluginCallback
 	GCallback	callback;
 	/** Set to TRUE to connect your handler with g_signal_connect_after(). */
 	gboolean	after;
-	/** The user data passed to the signal handler. */
+	/** The user data passed to the signal handler. If set to NULL then the signal
+	 * handler will receive the data set with geany_plugin_register_full() or
+	 * geany_plugin_set_data() */
 	gpointer	user_data;
 }
 PluginCallback;


Modified: src/plugins.c
3 lines changed, 2 insertions(+), 1 deletions(-)
===================================================================
@@ -206,8 +206,9 @@ static void add_callbacks(Plugin *plugin, PluginCallback *callbacks)
 	{
 		cb = &callbacks[i];
 
+		/* Pass the callback data as default user_data if none was set by the plugin itself */
 		plugin_signal_connect(&plugin->public, NULL, cb->signal_name, cb->after,
-			cb->callback, cb->user_data);
+			cb->callback, cb->user_data ? cb->user_data : plugin->cb_data);
 	}
 }
 



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list