Merge pull request #3759 from andy5995/meson-fix-deprecated-str-fmt
[geany-mirror.git] / plugins / demoplugin.c
blob3bd9a678eb2a90687bcabb6bbb4028663db2c73d
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.
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include "geanyplugin.h" /* plugin API, always comes first */
38 #include "Scintilla.h" /* for the SCNotification struct */
40 static GtkWidget *main_menu_item = NULL;
41 /* text to be shown in the plugin dialog */
42 static gchar *welcome_text = NULL;
45 static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
46 SCNotification *nt, gpointer data)
48 /* data == GeanyPlugin because the data member of PluginCallback was set to NULL
49 * and this plugin has called geany_plugin_set_data() with the GeanyPlugin pointer as
50 * data */
51 GeanyPlugin *plugin = data;
52 GeanyData *geany_data = plugin->geany_data;
54 /* For detailed documentation about the SCNotification struct, please see
55 * https://www.scintilla.org/ScintillaDoc.html#Notifications. */
56 switch (nt->nmhdr.code)
58 case SCN_UPDATEUI:
59 /* This notification is sent very often, you should not do time-consuming tasks here */
60 break;
61 case SCN_CHARADDED:
62 /* For demonstrating purposes simply print the typed character in the status bar */
63 ui_set_statusbar(FALSE, _("Typed character: %c"), nt->ch);
64 break;
65 case SCN_URIDROPPED:
67 /* Show a message dialog with the dropped URI list when files (i.e. a list of
68 * filenames) is dropped to the editor widget) */
69 if (nt->text != NULL)
71 GtkWidget *dialog;
73 dialog = gtk_message_dialog_new(
74 GTK_WINDOW(geany_data->main_widgets->window),
75 GTK_DIALOG_DESTROY_WITH_PARENT,
76 GTK_MESSAGE_INFO,
77 GTK_BUTTONS_OK,
78 _("The following files were dropped:"));
79 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
80 "%s", nt->text);
82 gtk_dialog_run(GTK_DIALOG(dialog));
83 gtk_widget_destroy(dialog);
85 /* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
86 * notification, i.e. Geany won't open the passed files */
87 return TRUE;
91 return FALSE;
95 static PluginCallback demo_callbacks[] =
97 /* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
98 * If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
99 * can prevent Geany from processing the notification. Use this with care. */
100 { "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
101 { NULL, NULL, FALSE, NULL }
105 /* Callback when the menu item is clicked. */
106 static void
107 item_activate(GtkMenuItem *menuitem, gpointer gdata)
109 GtkWidget *dialog;
110 GeanyPlugin *plugin = gdata;
111 GeanyData *geany_data = plugin->geany_data;
113 dialog = gtk_message_dialog_new(
114 GTK_WINDOW(geany_data->main_widgets->window),
115 GTK_DIALOG_DESTROY_WITH_PARENT,
116 GTK_MESSAGE_INFO,
117 GTK_BUTTONS_OK,
118 "%s", welcome_text);
119 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
120 _("(From the %s plugin)"), plugin->info->name);
122 gtk_dialog_run(GTK_DIALOG(dialog));
123 gtk_widget_destroy(dialog);
127 /* Called by Geany to initialize the plugin */
128 static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
130 GtkWidget *demo_item;
131 GeanyData *geany_data = plugin->geany_data;
133 /* Add an item to the Tools menu */
134 demo_item = gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
135 gtk_widget_show(demo_item);
136 gtk_container_add(GTK_CONTAINER(geany_data->main_widgets->tools_menu), demo_item);
137 g_signal_connect(demo_item, "activate", G_CALLBACK(item_activate), plugin);
139 /* make the menu item sensitive only when documents are open */
140 ui_add_document_sensitive(demo_item);
141 /* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
142 main_menu_item = demo_item;
144 welcome_text = g_strdup(_("Hello World!"));
146 /* This might seem strange but is a method to get the GeanyPlugin pointer passed to
147 * on_editor_notify(). PluginCallback functions get the same data that was set via
148 * GEANY_PLUING_REGISTER_FULL() or geany_plugin_set_data() by default (unless the data pointer
149 * was set to non-NULL at compile time).
150 * This is really only done for demoing PluginCallback. Actual plugins will use real custom
151 * data and perhaps embed the GeanyPlugin or GeanyData pointer their if they also use
152 * PluginCallback. */
153 geany_plugin_set_data(plugin, plugin, NULL);
154 return TRUE;
158 /* Callback connected in demo_configure(). */
159 static void
160 on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
162 /* catch OK or Apply clicked */
163 if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
165 /* We only have one pref here, but for more you would use a struct for user_data */
166 GtkWidget *entry = GTK_WIDGET(user_data);
168 g_free(welcome_text);
169 welcome_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
170 /* maybe the plugin should write here the settings into a file
171 * (e.g. using GLib's GKeyFile API)
172 * all plugin specific files should be created in:
173 * geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
174 * 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 * demo_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 static GtkWidget *demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
185 GtkWidget *label, *entry, *vbox;
187 /* example configuration dialog */
188 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 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 demo_init(). */
211 static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
213 /* remove the menu item added in demo_init() */
214 gtk_widget_destroy(main_menu_item);
215 /* release other allocated strings and objects */
216 g_free(welcome_text);
219 void geany_load_module(GeanyPlugin *plugin)
221 /* main_locale_init() must be called for your package before any localization can be done */
222 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
223 plugin->info->name = _("Demo");
224 plugin->info->description = _("Example plugin.");
225 plugin->info->version = "0.4";
226 plugin->info->author = _("The Geany developer team");
228 plugin->funcs->init = demo_init;
229 plugin->funcs->configure = demo_configure;
230 plugin->funcs->help = NULL; /* This demo has no help but it is an option */
231 plugin->funcs->cleanup = demo_cleanup;
232 plugin->funcs->callbacks = demo_callbacks;
234 GEANY_PLUGIN_REGISTER(plugin, 225);