Merge pull request #22 from RetroX/patch-1
[geany-mirror.git] / plugins / demoplugin.c
blobad00ea8f9e34646e7b45b1e98b01c3338cb5f53f
1 /*
2 * demoplugin.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 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
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.
23 /**
24 * Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
25 * Tools menu.
27 * Note: This is not installed by default, but (on *nix) you can build it as follows:
28 * cd plugins
29 * make demoplugin.so
31 * Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
32 * - it will be loaded at next startup.
36 #include "geanyplugin.h" /* plugin API, always comes first */
37 #include "Scintilla.h" /* for the SCNotification struct */
40 /* These items are set by Geany before plugin_init() is called. */
41 GeanyPlugin *geany_plugin;
42 GeanyData *geany_data;
43 GeanyFunctions *geany_functions;
46 /* Check that the running Geany supports the plugin API version used below, and check
47 * for binary compatibility. */
48 PLUGIN_VERSION_CHECK(147)
50 /* All plugins must set name, description, version and author. */
51 PLUGIN_SET_INFO(_("Demo"), _("Example plugin."), "0.1" , _("The Geany developer team"))
54 static GtkWidget *main_menu_item = NULL;
55 /* text to be shown in the plugin dialog */
56 static gchar *welcome_text = NULL;
60 static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
61 SCNotification *nt, gpointer data)
63 /* For detailed documentation about the SCNotification struct, please see
64 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
65 switch (nt->nmhdr.code)
67 case SCN_UPDATEUI:
68 /* This notification is sent very often, you should not do time-consuming tasks here */
69 break;
70 case SCN_CHARADDED:
71 /* For demonstrating purposes simply print the typed character in the status bar */
72 ui_set_statusbar(FALSE, _("Typed character: %c"), nt->ch);
73 break;
74 case SCN_URIDROPPED:
76 /* Show a message dialog with the dropped URI list when files (i.e. a list of
77 * filenames) is dropped to the editor widget) */
78 if (nt->text != NULL)
80 GtkWidget *dialog;
82 dialog = gtk_message_dialog_new(
83 GTK_WINDOW(geany->main_widgets->window),
84 GTK_DIALOG_DESTROY_WITH_PARENT,
85 GTK_MESSAGE_INFO,
86 GTK_BUTTONS_OK,
87 _("The following files were dropped:"));
88 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
89 "%s", nt->text);
91 gtk_dialog_run(GTK_DIALOG(dialog));
92 gtk_widget_destroy(dialog);
94 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
95 * notification, i.e. Geany won't open the passed files */
96 return TRUE;
100 return FALSE;
104 PluginCallback plugin_callbacks[] =
106 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
107 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
108 * can prevent Geany from processing the notification. Use this with care. */
109 { "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
110 { NULL, NULL, FALSE, NULL }
114 /* Callback when the menu item is clicked. */
115 static void
116 item_activate(GtkMenuItem *menuitem, gpointer gdata)
118 GtkWidget *dialog;
120 dialog = gtk_message_dialog_new(
121 GTK_WINDOW(geany->main_widgets->window),
122 GTK_DIALOG_DESTROY_WITH_PARENT,
123 GTK_MESSAGE_INFO,
124 GTK_BUTTONS_OK,
125 "%s", welcome_text);
126 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
127 _("(From the %s plugin)"), geany_plugin->info->name);
129 gtk_dialog_run(GTK_DIALOG(dialog));
130 gtk_widget_destroy(dialog);
134 /* Called by Geany to initialize the plugin.
135 * Note: data is the same as geany_data. */
136 void plugin_init(GeanyData *data)
138 GtkWidget *demo_item;
140 /* Add an item to the Tools menu */
141 demo_item = gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
142 gtk_widget_show(demo_item);
143 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), demo_item);
144 g_signal_connect(demo_item, "activate", G_CALLBACK(item_activate), NULL);
146 /* make the menu item sensitive only when documents are open */
147 ui_add_document_sensitive(demo_item);
148 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
149 main_menu_item = demo_item;
151 welcome_text = g_strdup(_("Hello World!"));
155 /* Callback connected in plugin_configure(). */
156 static void
157 on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
159 /* catch OK or Apply clicked */
160 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
162 /* We only have one pref here, but for more you would use a struct for user_data */
163 GtkWidget *entry = GTK_WIDGET(user_data);
165 g_free(welcome_text);
166 welcome_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
167 /* maybe the plugin should write here the settings into a file
168 * (e.g. using GLib's GKeyFile API)
169 * all plugin specific files should be created in:
170 * geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
171 * e.g. this could be: ~/.config/geany/plugins/Demo/, please use geany->app->configdir */
176 /* Called by Geany to show the plugin's configure dialog. This function is always called after
177 * plugin_init() was called.
178 * You can omit this function if the plugin doesn't need to be configured.
179 * Note: parent is the parent window which can be used as the transient window for the created
180 * dialog. */
181 GtkWidget *plugin_configure(GtkDialog *dialog)
183 GtkWidget *label, *entry, *vbox;
185 /* example configuration dialog */
186 vbox = gtk_vbox_new(FALSE, 6);
188 /* add a label and a text entry to the dialog */
189 label = gtk_label_new(_("Welcome text to show:"));
190 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
191 entry = gtk_entry_new();
192 if (welcome_text != NULL)
193 gtk_entry_set_text(GTK_ENTRY(entry), welcome_text);
195 gtk_container_add(GTK_CONTAINER(vbox), label);
196 gtk_container_add(GTK_CONTAINER(vbox), entry);
198 gtk_widget_show_all(vbox);
200 /* Connect a callback for when the user clicks a dialog button */
201 g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), entry);
202 return vbox;
206 /* Called by Geany before unloading the plugin.
207 * Here any UI changes should be removed, memory freed and any other finalization done.
208 * Be sure to leave Geany as it was before plugin_init(). */
209 void plugin_cleanup(void)
211 /* remove the menu item added in plugin_init() */
212 gtk_widget_destroy(main_menu_item);
213 /* release other allocated strings and objects */
214 g_free(welcome_text);