2 * demoplugin.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
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.
23 * Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
26 * Note: This is not installed by default, but (on *nix) you can build it as follows:
30 * Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
31 * - it will be loaded at next startup.
35 #include "geanyplugin.h" /* plugin API, always comes first */
36 #include "Scintilla.h" /* for the SCNotification struct */
39 /* These items are set by Geany before plugin_init() is called. */
40 GeanyPlugin
*geany_plugin
;
41 GeanyData
*geany_data
;
42 GeanyFunctions
*geany_functions
;
45 /* Check that the running Geany supports the plugin API version used below, and check
46 * for binary compatibility. */
47 PLUGIN_VERSION_CHECK(147)
49 /* All plugins must set name, description, version and author. */
50 PLUGIN_SET_INFO(_("Demo"), _("Example plugin."), "0.1" , _("The Geany developer team"))
53 static GtkWidget
*main_menu_item
= NULL
;
54 /* text to be shown in the plugin dialog */
55 static gchar
*welcome_text
= NULL
;
59 static gboolean
on_editor_notify(GObject
*object
, GeanyEditor
*editor
,
60 SCNotification
*nt
, gpointer data
)
62 /* For detailed documentation about the SCNotification struct, please see
63 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
64 switch (nt
->nmhdr
.code
)
67 /* This notification is sent very often, you should not do time-consuming tasks here */
70 /* For demonstrating purposes simply print the typed character in the status bar */
71 ui_set_statusbar(FALSE
, _("Typed character: %c"), nt
->ch
);
75 /* Show a message dialog with the dropped URI list when files (i.e. a list of
76 * filenames) is dropped to the editor widget) */
81 dialog
= gtk_message_dialog_new(
82 GTK_WINDOW(geany
->main_widgets
->window
),
83 GTK_DIALOG_DESTROY_WITH_PARENT
,
86 _("The following files were dropped:"));
87 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
90 gtk_dialog_run(GTK_DIALOG(dialog
));
91 gtk_widget_destroy(dialog
);
93 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
94 * notification, i.e. Geany won't open the passed files */
103 PluginCallback plugin_callbacks
[] =
105 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
106 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
107 * can prevent Geany from processing the notification. Use this with care. */
108 { "editor-notify", (GCallback
) &on_editor_notify
, FALSE
, NULL
},
109 { NULL
, NULL
, FALSE
, NULL
}
113 /* Callback when the menu item is clicked. */
115 item_activate(GtkMenuItem
*menuitem
, gpointer gdata
)
119 dialog
= gtk_message_dialog_new(
120 GTK_WINDOW(geany
->main_widgets
->window
),
121 GTK_DIALOG_DESTROY_WITH_PARENT
,
125 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
126 _("(From the %s plugin)"), geany_plugin
->info
->name
);
128 gtk_dialog_run(GTK_DIALOG(dialog
));
129 gtk_widget_destroy(dialog
);
133 /* Called by Geany to initialize the plugin.
134 * Note: data is the same as geany_data. */
135 void plugin_init(GeanyData
*data
)
137 GtkWidget
*demo_item
;
139 /* Add an item to the Tools menu */
140 demo_item
= gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
141 gtk_widget_show(demo_item
);
142 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), demo_item
);
143 g_signal_connect(demo_item
, "activate", G_CALLBACK(item_activate
), NULL
);
145 /* make the menu item sensitive only when documents are open */
146 ui_add_document_sensitive(demo_item
);
147 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
148 main_menu_item
= demo_item
;
150 welcome_text
= g_strdup(_("Hello World!"));
154 /* Callback connected in plugin_configure(). */
156 on_configure_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
158 /* catch OK or Apply clicked */
159 if (response
== GTK_RESPONSE_OK
|| response
== GTK_RESPONSE_APPLY
)
161 /* We only have one pref here, but for more you would use a struct for user_data */
162 GtkWidget
*entry
= GTK_WIDGET(user_data
);
164 g_free(welcome_text
);
165 welcome_text
= g_strdup(gtk_entry_get_text(GTK_ENTRY(entry
)));
166 /* maybe the plugin should write here the settings into a file
167 * (e.g. using GLib's GKeyFile API)
168 * all plugin specific files should be created in:
169 * geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
170 * e.g. this could be: ~/.config/geany/plugins/Demo/, please use geany->app->configdir */
175 /* Called by Geany to show the plugin's configure dialog. This function is always called after
176 * plugin_init() was called.
177 * You can omit this function if the plugin doesn't need to be configured.
178 * Note: parent is the parent window which can be used as the transient window for the created
180 GtkWidget
*plugin_configure(GtkDialog
*dialog
)
182 GtkWidget
*label
, *entry
, *vbox
;
184 /* example configuration dialog */
185 vbox
= gtk_vbox_new(FALSE
, 6);
187 /* add a label and a text entry to the dialog */
188 label
= gtk_label_new(_("Welcome text to show:"));
189 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
190 entry
= gtk_entry_new();
191 if (welcome_text
!= NULL
)
192 gtk_entry_set_text(GTK_ENTRY(entry
), welcome_text
);
194 gtk_container_add(GTK_CONTAINER(vbox
), label
);
195 gtk_container_add(GTK_CONTAINER(vbox
), entry
);
197 gtk_widget_show_all(vbox
);
199 /* Connect a callback for when the user clicks a dialog button */
200 g_signal_connect(dialog
, "response", G_CALLBACK(on_configure_response
), entry
);
205 /* Called by Geany before unloading the plugin.
206 * Here any UI changes should be removed, memory freed and any other finalization done.
207 * Be sure to leave Geany as it was before plugin_init(). */
208 void plugin_cleanup(void)
210 /* remove the menu item added in plugin_init() */
211 gtk_widget_destroy(main_menu_item
);
212 /* release other allocated strings and objects */
213 g_free(welcome_text
);