geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

demoplugin.c
Go to the documentation of this file.
1/*
2 * demoplugin.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2007 The Geany contributors
5 *
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.
10 *
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.
15 *
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.
19 */
20
21/**
22 * Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
23 * Tools menu.
24 *
25 * Note: This is not installed by default, but (on *nix) you can build it as follows:
26 * cd plugins
27 * make demoplugin.so
28 *
29 * Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
30 * - it will be loaded at next startup.
31 */
32
33
34#include "geanyplugin.h" /* plugin API, always comes first */
35#include "Scintilla.h" /* for the SCNotification struct */
36
37static GtkWidget *main_menu_item = NULL;
38/* text to be shown in the plugin dialog */
39static gchar *welcome_text = NULL;
40
41
42static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
43 SCNotification *nt, gpointer data)
44{
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;
50
51 /* For detailed documentation about the SCNotification struct, please see
52 * http://www.scintilla.org/ScintillaDoc.html#Notifications. */
53 switch (nt->nmhdr.code)
54 {
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:
63 {
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)
67 {
68 GtkWidget *dialog;
69
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);
78
79 gtk_dialog_run(GTK_DIALOG(dialog));
80 gtk_widget_destroy(dialog);
81 }
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;
85 }
86 }
87
88 return FALSE;
89}
90
91
93{
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 }
99};
100
101
102/* Callback when the menu item is clicked. */
103static void
104item_activate(GtkMenuItem *menuitem, gpointer gdata)
105{
106 GtkWidget *dialog;
107 GeanyPlugin *plugin = gdata;
109
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);
118
119 gtk_dialog_run(GTK_DIALOG(dialog));
120 gtk_widget_destroy(dialog);
121}
122
123
124/* Called by Geany to initialize the plugin */
125static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
126{
127 GtkWidget *demo_item;
129
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);
135
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;
140
141 welcome_text = g_strdup(_("Hello World!"));
142
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;
152}
153
154
155/* Callback connected in demo_configure(). */
156static void
157on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
158{
159 /* catch OK or Apply clicked */
160 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
161 {
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);
164
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 */
172 }
173}
174
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. */
180static GtkWidget *demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
181{
182 GtkWidget *label, *entry, *vbox;
183
184 /* example configuration dialog */
185 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
186
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);
193
194 gtk_container_add(GTK_CONTAINER(vbox), label);
195 gtk_container_add(GTK_CONTAINER(vbox), entry);
196
198
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;
202}
203
204
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(). */
208static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
209{
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);
214}
215
217{
218 /* main_locale_init() must be called for your package before any localization can be done */
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");
224
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;
230
231 GEANY_PLUGIN_REGISTER(plugin, 225);
232}
Interface to the edit control.
#define SCN_CHARADDED
Definition: Scintilla.h:1127
#define SCN_UPDATEUI
Definition: Scintilla.h:1133
#define SCN_URIDROPPED
Definition: Scintilla.h:1140
const gchar * label
Definition: build.c:2676
static PluginCallback demo_callbacks[]
Definition: demoplugin.c:92
static gboolean on_editor_notify(GObject *object, GeanyEditor *editor, SCNotification *nt, gpointer data)
Definition: demoplugin.c:42
static void on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
Definition: demoplugin.c:157
static GtkWidget * demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
Definition: demoplugin.c:180
static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
Definition: demoplugin.c:208
static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
Definition: demoplugin.c:125
static GtkWidget * main_menu_item
Demo plugin - example of a basic plugin for Geany.
Definition: demoplugin.c:37
void geany_load_module(GeanyPlugin *plugin)
Called by Geany when a plugin library is loaded.
Definition: demoplugin.c:216
static gchar * welcome_text
Definition: demoplugin.c:39
static void item_activate(GtkMenuItem *menuitem, gpointer gdata)
Definition: demoplugin.c:104
Single include for plugins.
void main_locale_init(const gchar *locale_dir, const gchar *package)
Initialises the gettext translation system.
Definition: libmain.c:489
void geany_plugin_set_data(GeanyPlugin *plugin, gpointer data, GDestroyNotify free_func)
Add additional data that corresponds to the plugin.
Definition: pluginutils.c:613
#define GEANY_PLUGIN_REGISTER(plugin, min_api_version)
Convenience macro to register a plugin.
Definition: plugindata.h:324
const GeanyData * geany_data
Geany owned data pointers.
Definition: pluginsymbols.c:59
#define NULL
Definition: rbtree.h:150
GtkWidget * entry
Definition: search.c:118
GtkWidget * dialog
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
gtk_widget_show_all(dialog)
This contains pointers to global variables owned by Geany for plugins to use.
Definition: plugindata.h:167
struct GeanyMainWidgets * main_widgets
Important widgets in the main window.
Definition: plugindata.h:169
Editor-owned fields for each document.
Definition: editor.h:150
GtkWidget * window
Main window.
Definition: ui_utils.h:80
GtkWidget * tools_menu
Most plugins add menu items to the Tools menu.
Definition: ui_utils.h:85
gboolean(* init)(GeanyPlugin *plugin, gpointer pdata)
Called to initialize the plugin, when the user activates it (must not be NULL)
Definition: plugindata.h:300
GtkWidget *(* configure)(GeanyPlugin *plugin, GtkDialog *dialog, gpointer pdata)
plugins configure dialog, optional (can be NULL)
Definition: plugindata.h:302
PluginCallback * callbacks
Array of plugin-provided signal handlers.
Definition: plugindata.h:298
void(* help)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin should show some help, optional (can be NULL)
Definition: plugindata.h:304
void(* cleanup)(GeanyPlugin *plugin, gpointer pdata)
Called when the plugin is disabled or when Geany exits (must not be NULL)
Definition: plugindata.h:306
Basic information for the plugin and identification.
Definition: plugindata.h:233
GeanyData * geany_data
Pointer to global GeanyData intance.
Definition: plugindata.h:235
PluginInfo * info
Fields set in plugin_set_info().
Definition: plugindata.h:234
GeanyPluginFuncs * funcs
Functions implemented by the plugin, set in geany_load_module()
Definition: plugindata.h:236
Callback array entry type used with the plugin_callbacks symbol.
Definition: plugindata.h:148
const gchar * version
The version of the plugin.
Definition: plugindata.h:101
const gchar * description
The description of the plugin.
Definition: plugindata.h:99
const gchar * name
The name of the plugin.
Definition: plugindata.h:97
const gchar * author
The author of the plugin.
Definition: plugindata.h:103
Sci_NotifyHeader nmhdr
Definition: Scintilla.h:1229
const char * text
Definition: Scintilla.h:1245
unsigned int code
Definition: Scintilla.h:1225
#define _(String)
Definition: support.h:42
#define GETTEXT_PACKAGE
Definition: support.h:37
void ui_add_document_sensitive(GtkWidget *widget)
Adds a widget to the list of widgets that should be set sensitive/insensitive when some documents are...
Definition: ui_utils.c:976
void ui_set_statusbar(gboolean log, const gchar *format,...)
Displays text on the statusbar.
Definition: ui_utils.c:168