Merge pull request #3084 from techee/basic
[geany-mirror.git] / plugins / demoplugin.c
blob171a5113c3158e741e2bb39cb8b26cf63c009198
1 /*
2 * demoplugin.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /**
22 * Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
23 * Tools menu.
25 * Note: This is not installed by default, but (on *nix) you can build it as follows:
26 * cd plugins
27 * make demoplugin.so
29 * Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
30 * - it will be loaded at next startup.
34 #include "geanyplugin.h" /* plugin API, always comes first */
35 #include "Scintilla.h" /* for the SCNotification struct */
37 static GtkWidget *main_menu_item = NULL;
38 /* text to be shown in the plugin dialog */
39 static gchar *welcome_text = NULL;
42 static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
43 SCNotification *nt, gpointer data)
45 /* data == GeanyPlugin because the data member of PluginCallback was set to NULL
46 * and this plugin has called geany_plugin_set_data() with the GeanyPlugin pointer as
47 * data */
48 GeanyPlugin *plugin = data;
49 GeanyData *geany_data = plugin->geany_data;
51 /* For detailed documentation about the SCNotification struct, please see
52 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
53 switch (nt->nmhdr.code)
55 case SCN_UPDATEUI:
56 /* This notification is sent very often, you should not do time-consuming tasks here */
57 break;
58 case SCN_CHARADDED:
59 /* For demonstrating purposes simply print the typed character in the status bar */
60 ui_set_statusbar(FALSE, _("Typed character: %c"), nt->ch);
61 break;
62 case SCN_URIDROPPED:
64 /* Show a message dialog with the dropped URI list when files (i.e. a list of
65 * filenames) is dropped to the editor widget) */
66 if (nt->text != NULL)
68 GtkWidget *dialog;
70 dialog = gtk_message_dialog_new(
71 GTK_WINDOW(geany_data->main_widgets->window),
72 GTK_DIALOG_DESTROY_WITH_PARENT,
73 GTK_MESSAGE_INFO,
74 GTK_BUTTONS_OK,
75 _("The following files were dropped:"));
76 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
77 "%s", nt->text);
79 gtk_dialog_run(GTK_DIALOG(dialog));
80 gtk_widget_destroy(dialog);
82 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
83 * notification, i.e. Geany won't open the passed files */
84 return TRUE;
88 return FALSE;
92 static PluginCallback demo_callbacks[] =
94 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
95 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
96 * can prevent Geany from processing the notification. Use this with care. */
97 { "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
98 { NULL, NULL, FALSE, NULL }
102 /* Callback when the menu item is clicked. */
103 static void
104 item_activate(GtkMenuItem *menuitem, gpointer gdata)
106 GtkWidget *dialog;
107 GeanyPlugin *plugin = gdata;
108 GeanyData *geany_data = plugin->geany_data;
110 dialog = gtk_message_dialog_new(
111 GTK_WINDOW(geany_data->main_widgets->window),
112 GTK_DIALOG_DESTROY_WITH_PARENT,
113 GTK_MESSAGE_INFO,
114 GTK_BUTTONS_OK,
115 "%s", welcome_text);
116 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
117 _("(From the %s plugin)"), plugin->info->name);
119 gtk_dialog_run(GTK_DIALOG(dialog));
120 gtk_widget_destroy(dialog);
124 /* Called by Geany to initialize the plugin */
125 static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
127 GtkWidget *demo_item;
128 GeanyData *geany_data = plugin->geany_data;
130 /* Add an item to the Tools menu */
131 demo_item = gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
132 gtk_widget_show(demo_item);
133 gtk_container_add(GTK_CONTAINER(geany_data->main_widgets->tools_menu), demo_item);
134 g_signal_connect(demo_item, "activate", G_CALLBACK(item_activate), plugin);
136 /* make the menu item sensitive only when documents are open */
137 ui_add_document_sensitive(demo_item);
138 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
139 main_menu_item = demo_item;
141 welcome_text = g_strdup(_("Hello World!"));
143 /* This might seem strange but is a method to get the GeanyPlugin pointer passed to
144 * on_editor_notify(). PluginCallback functions get the same data that was set via
145 * GEANY_PLUING_REGISTER_FULL() or geany_plugin_set_data() by default (unless the data pointer
146 * was set to non-NULL at compile time).
147 * This is really only done for demoing PluginCallback. Actual plugins will use real custom
148 * data and perhaps embed the GeanyPlugin or GeanyData pointer their if they also use
149 * PluginCallback. */
150 geany_plugin_set_data(plugin, plugin, NULL);
151 return TRUE;
155 /* Callback connected in demo_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 */
175 /* Called by Geany to show the plugin's configure dialog. This function is always called after
176 * demo_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
179 * dialog. */
180 static GtkWidget *demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
182 GtkWidget *label, *entry, *vbox;
184 /* example configuration dialog */
185 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 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);
201 return vbox;
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 demo_init(). */
208 static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
210 /* remove the menu item added in demo_init() */
211 gtk_widget_destroy(main_menu_item);
212 /* release other allocated strings and objects */
213 g_free(welcome_text);
216 void geany_load_module(GeanyPlugin *plugin)
218 /* main_locale_init() must be called for your package before any localization can be done */
219 main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
220 plugin->info->name = _("Demo");
221 plugin->info->description = _("Example plugin.");
222 plugin->info->version = "0.4";
223 plugin->info->author = _("The Geany developer team");
225 plugin->funcs->init = demo_init;
226 plugin->funcs->configure = demo_configure;
227 plugin->funcs->help = NULL; /* This demo has no help but it is an option */
228 plugin->funcs->cleanup = demo_cleanup;
229 plugin->funcs->callbacks = demo_callbacks;
231 GEANY_PLUGIN_REGISTER(plugin, 225);