Merge pull request #581 from techee/symbollist_sort
[geany-mirror.git] / plugins / demoplugin.c
blob58947368a8f4e3dc7245aa5776adad2689f20567
1 /*
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.
22 /**
23 * Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
24 * Tools menu.
26 * Note: This is not installed by default, but (on *nix) you can build it as follows:
27 * cd plugins
28 * make demoplugin.so
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 */
38 static GtkWidget *main_menu_item = NULL;
39 /* text to be shown in the plugin dialog */
40 static gchar *welcome_text = NULL;
43 static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
44 SCNotification *nt, gpointer data)
46 /* data == GeanyPlugin because the data member of PluginCallback was set to NULL
47 * and this plugin has called geany_plugin_set_data() with the GeanyPlugin pointer as
48 * data */
49 GeanyPlugin *plugin = data;
50 GeanyData *geany_data = plugin->geany_data;
52 /* For detailed documentation about the SCNotification struct, please see
53 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
54 switch (nt->nmhdr.code)
56 case SCN_UPDATEUI:
57 /* This notification is sent very often, you should not do time-consuming tasks here */
58 break;
59 case SCN_CHARADDED:
60 /* For demonstrating purposes simply print the typed character in the status bar */
61 ui_set_statusbar(FALSE, _("Typed character: %c"), nt->ch);
62 break;
63 case SCN_URIDROPPED:
65 /* Show a message dialog with the dropped URI list when files (i.e. a list of
66 * filenames) is dropped to the editor widget) */
67 if (nt->text != NULL)
69 GtkWidget *dialog;
71 dialog = gtk_message_dialog_new(
72 GTK_WINDOW(geany_data->main_widgets->window),
73 GTK_DIALOG_DESTROY_WITH_PARENT,
74 GTK_MESSAGE_INFO,
75 GTK_BUTTONS_OK,
76 _("The following files were dropped:"));
77 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
78 "%s", nt->text);
80 gtk_dialog_run(GTK_DIALOG(dialog));
81 gtk_widget_destroy(dialog);
83 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
84 * notification, i.e. Geany won't open the passed files */
85 return TRUE;
89 return FALSE;
93 static PluginCallback demo_callbacks[] =
95 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
96 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
97 * can prevent Geany from processing the notification. Use this with care. */
98 { "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
99 { NULL, NULL, FALSE, NULL }
103 /* Callback when the menu item is clicked. */
104 static void
105 item_activate(GtkMenuItem *menuitem, gpointer gdata)
107 GtkWidget *dialog;
108 GeanyPlugin *plugin = gdata;
109 GeanyData *geany_data = plugin->geany_data;
111 dialog = gtk_message_dialog_new(
112 GTK_WINDOW(geany_data->main_widgets->window),
113 GTK_DIALOG_DESTROY_WITH_PARENT,
114 GTK_MESSAGE_INFO,
115 GTK_BUTTONS_OK,
116 "%s", welcome_text);
117 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
118 _("(From the %s plugin)"), plugin->info->name);
120 gtk_dialog_run(GTK_DIALOG(dialog));
121 gtk_widget_destroy(dialog);
125 /* Called by Geany to initialize the plugin */
126 static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
128 GtkWidget *demo_item;
129 GeanyData *geany_data = plugin->geany_data;
131 /* Add an item to the Tools menu */
132 demo_item = gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
133 gtk_widget_show(demo_item);
134 gtk_container_add(GTK_CONTAINER(geany_data->main_widgets->tools_menu), demo_item);
135 g_signal_connect(demo_item, "activate", G_CALLBACK(item_activate), plugin);
137 /* make the menu item sensitive only when documents are open */
138 ui_add_document_sensitive(demo_item);
139 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
140 main_menu_item = demo_item;
142 welcome_text = g_strdup(_("Hello World!"));
144 /* This might seem strange but is a method to get the GeanyPlugin pointer passed to
145 * on_editor_notify(). PluginCallback functions get the same data that was set via
146 * GEANY_PLUING_REGISTER_FULL() or geany_plugin_set_data() by default (unless the data pointer
147 * was set to non-NULL at compile time).
148 * This is really only done for demoing PluginCallback. Actual plugins will use real custom
149 * data and perhaps embed the GeanyPlugin or GeanyData pointer their if they also use
150 * PluginCallback. */
151 geany_plugin_set_data(plugin, plugin, NULL);
152 return TRUE;
156 /* Callback connected in demo_configure(). */
157 static void
158 on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
160 /* catch OK or Apply clicked */
161 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
163 /* We only have one pref here, but for more you would use a struct for user_data */
164 GtkWidget *entry = GTK_WIDGET(user_data);
166 g_free(welcome_text);
167 welcome_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
168 /* maybe the plugin should write here the settings into a file
169 * (e.g. using GLib's GKeyFile API)
170 * all plugin specific files should be created in:
171 * geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
172 * 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 * demo_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 static GtkWidget *demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
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 demo_init(). */
209 static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
211 /* remove the menu item added in demo_init() */
212 gtk_widget_destroy(main_menu_item);
213 /* release other allocated strings and objects */
214 g_free(welcome_text);
217 void geany_load_module(GeanyPlugin *plugin)
219 /* main_locale_init() must be called for your package before any localization can be done */
220 main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
221 plugin->info->name = _("Demo");
222 plugin->info->description = _("Example plugin.");
223 plugin->info->version = "0.4";
224 plugin->info->author = _("The Geany developer team");
226 plugin->funcs->init = demo_init;
227 plugin->funcs->configure = demo_configure;
228 plugin->funcs->help = NULL; /* This demo has no help but it is an option */
229 plugin->funcs->cleanup = demo_cleanup;
230 plugin->funcs->callbacks = demo_callbacks;
232 GEANY_PLUGIN_REGISTER(plugin, 225);