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