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
;
44 /* Check that the running Geany supports the plugin API version used below, and check
45 * for binary compatibility. */
46 PLUGIN_VERSION_CHECK(147)
48 /* All plugins must set name, description, version and author. */
49 PLUGIN_SET_INFO(_("Demo"), _("Example plugin."), "0.1" , _("The Geany developer team"))
52 static GtkWidget
*main_menu_item
= NULL
;
53 /* text to be shown in the plugin dialog */
54 static gchar
*welcome_text
= NULL
;
58 static gboolean
on_editor_notify(GObject
*object
, GeanyEditor
*editor
,
59 SCNotification
*nt
, gpointer data
)
61 /* For detailed documentation about the SCNotification struct, please see
62 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
63 switch (nt
->nmhdr
.code
)
66 /* This notification is sent very often, you should not do time-consuming tasks here */
69 /* For demonstrating purposes simply print the typed character in the status bar */
70 ui_set_statusbar(FALSE
, _("Typed character: %c"), nt
->ch
);
74 /* Show a message dialog with the dropped URI list when files (i.e. a list of
75 * filenames) is dropped to the editor widget) */
80 dialog
= gtk_message_dialog_new(
81 GTK_WINDOW(geany
->main_widgets
->window
),
82 GTK_DIALOG_DESTROY_WITH_PARENT
,
85 _("The following files were dropped:"));
86 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
89 gtk_dialog_run(GTK_DIALOG(dialog
));
90 gtk_widget_destroy(dialog
);
92 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
93 * notification, i.e. Geany won't open the passed files */
102 PluginCallback plugin_callbacks
[] =
104 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
105 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
106 * can prevent Geany from processing the notification. Use this with care. */
107 { "editor-notify", (GCallback
) &on_editor_notify
, FALSE
, NULL
},
108 { NULL
, NULL
, FALSE
, NULL
}
112 /* Callback when the menu item is clicked. */
114 item_activate(GtkMenuItem
*menuitem
, gpointer gdata
)
118 dialog
= gtk_message_dialog_new(
119 GTK_WINDOW(geany
->main_widgets
->window
),
120 GTK_DIALOG_DESTROY_WITH_PARENT
,
124 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog
),
125 _("(From the %s plugin)"), geany_plugin
->info
->name
);
127 gtk_dialog_run(GTK_DIALOG(dialog
));
128 gtk_widget_destroy(dialog
);
132 /* Called by Geany to initialize the plugin.
133 * Note: data is the same as geany_data. */
134 void plugin_init(GeanyData
*data
)
136 GtkWidget
*demo_item
;
138 /* Add an item to the Tools menu */
139 demo_item
= gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
140 gtk_widget_show(demo_item
);
141 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), demo_item
);
142 g_signal_connect(demo_item
, "activate", G_CALLBACK(item_activate
), NULL
);
144 /* make the menu item sensitive only when documents are open */
145 ui_add_document_sensitive(demo_item
);
146 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
147 main_menu_item
= demo_item
;
149 welcome_text
= g_strdup(_("Hello World!"));
153 /* Callback connected in plugin_configure(). */
155 on_configure_response(GtkDialog
*dialog
, gint response
, gpointer user_data
)
157 /* catch OK or Apply clicked */
158 if (response
== GTK_RESPONSE_OK
|| response
== GTK_RESPONSE_APPLY
)
160 /* We only have one pref here, but for more you would use a struct for user_data */
161 GtkWidget
*entry
= GTK_WIDGET(user_data
);
163 g_free(welcome_text
);
164 welcome_text
= g_strdup(gtk_entry_get_text(GTK_ENTRY(entry
)));
165 /* maybe the plugin should write here the settings into a file
166 * (e.g. using GLib's GKeyFile API)
167 * all plugin specific files should be created in:
168 * geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
169 * e.g. this could be: ~/.config/geany/plugins/Demo/, please use geany->app->configdir */
174 /* Called by Geany to show the plugin's configure dialog. This function is always called after
175 * plugin_init() was called.
176 * You can omit this function if the plugin doesn't need to be configured.
177 * Note: parent is the parent window which can be used as the transient window for the created
179 GtkWidget
*plugin_configure(GtkDialog
*dialog
)
181 GtkWidget
*label
, *entry
, *vbox
;
183 /* example configuration dialog */
184 vbox
= gtk_vbox_new(FALSE
, 6);
186 /* add a label and a text entry to the dialog */
187 label
= gtk_label_new(_("Welcome text to show:"));
188 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
189 entry
= gtk_entry_new();
190 if (welcome_text
!= NULL
)
191 gtk_entry_set_text(GTK_ENTRY(entry
), welcome_text
);
193 gtk_container_add(GTK_CONTAINER(vbox
), label
);
194 gtk_container_add(GTK_CONTAINER(vbox
), entry
);
196 gtk_widget_show_all(vbox
);
198 /* Connect a callback for when the user clicks a dialog button */
199 g_signal_connect(dialog
, "response", G_CALLBACK(on_configure_response
), entry
);
204 /* Called by Geany before unloading the plugin.
205 * Here any UI changes should be removed, memory freed and any other finalization done.
206 * Be sure to leave Geany as it was before plugin_init(). */
207 void plugin_cleanup(void)
209 /* remove the menu item added in plugin_init() */
210 gtk_widget_destroy(main_menu_item
);
211 /* release other allocated strings and objects */
212 g_free(welcome_text
);