2007-06-26 16:17:16 +00:00
|
|
|
/*
|
|
|
|
* demoplugin.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2012-06-18 01:13:05 +02:00
|
|
|
* Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2007-06-26 16:17:16 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2012-08-24 19:25:57 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-06-26 16:17:16 +00:00
|
|
|
*/
|
|
|
|
|
2007-10-24 12:04:15 +00:00
|
|
|
/**
|
|
|
|
* Demo plugin - example of a basic plugin for Geany. Adds a menu item to the
|
|
|
|
* Tools menu.
|
|
|
|
*
|
|
|
|
* Note: This is not installed by default, but (on *nix) you can build it as follows:
|
|
|
|
* cd plugins
|
|
|
|
* make demoplugin.so
|
|
|
|
*
|
2008-11-13 14:37:47 +00:00
|
|
|
* Then copy or symlink the plugins/demoplugin.so file to ~/.config/geany/plugins
|
2007-10-24 12:04:15 +00:00
|
|
|
* - it will be loaded at next startup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-07-14 15:06:20 +00:00
|
|
|
#include "geanyplugin.h" /* plugin API, always comes first */
|
2009-01-28 17:55:58 +00:00
|
|
|
#include "Scintilla.h" /* for the SCNotification struct */
|
2007-06-26 16:17:16 +00:00
|
|
|
|
2008-10-14 17:03:44 +00:00
|
|
|
static GtkWidget *main_menu_item = NULL;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* text to be shown in the plugin dialog */
|
2007-11-20 18:15:46 +00:00
|
|
|
static gchar *welcome_text = NULL;
|
2007-06-26 16:17:16 +00:00
|
|
|
|
2007-06-27 15:56:42 +00:00
|
|
|
|
2008-12-17 16:00:18 +00:00
|
|
|
static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
|
|
|
|
SCNotification *nt, gpointer data)
|
|
|
|
{
|
2015-08-23 15:23:33 +02:00
|
|
|
/* data == GeanyPlugin because the data member of PluginCallback was set to NULL
|
|
|
|
* and this plugin has called geany_plugin_set_data() with the GeanyPlugin pointer as
|
|
|
|
* data */
|
|
|
|
GeanyPlugin *plugin = data;
|
|
|
|
GeanyData *geany_data = plugin->geany_data;
|
2015-08-23 15:23:29 +02:00
|
|
|
|
2008-12-17 16:00:18 +00:00
|
|
|
/* For detailed documentation about the SCNotification struct, please see
|
|
|
|
* http://www.scintilla.org/ScintillaDoc.html#Notifications. */
|
|
|
|
switch (nt->nmhdr.code)
|
|
|
|
{
|
|
|
|
case SCN_UPDATEUI:
|
|
|
|
/* This notification is sent very often, you should not do time-consuming tasks here */
|
|
|
|
break;
|
|
|
|
case SCN_CHARADDED:
|
|
|
|
/* For demonstrating purposes simply print the typed character in the status bar */
|
|
|
|
ui_set_statusbar(FALSE, _("Typed character: %c"), nt->ch);
|
|
|
|
break;
|
|
|
|
case SCN_URIDROPPED:
|
|
|
|
{
|
|
|
|
/* Show a message dialog with the dropped URI list when files (i.e. a list of
|
|
|
|
* filenames) is dropped to the editor widget) */
|
|
|
|
if (nt->text != NULL)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new(
|
2015-08-23 15:23:29 +02:00
|
|
|
GTK_WINDOW(geany_data->main_widgets->window),
|
2008-12-17 16:00:18 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_INFO,
|
|
|
|
GTK_BUTTONS_OK,
|
|
|
|
_("The following files were dropped:"));
|
|
|
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
|
|
|
|
"%s", nt->text);
|
|
|
|
|
|
|
|
gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
/* we return TRUE here which prevents Geany from processing the SCN_URIDROPPED
|
|
|
|
* notification, i.e. Geany won't open the passed files */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-23 15:23:29 +02:00
|
|
|
static PluginCallback demo_callbacks[] =
|
2008-12-17 16:00:18 +00:00
|
|
|
{
|
|
|
|
/* Set 'after' (third field) to TRUE to run the callback @a after the default handler.
|
|
|
|
* If 'after' is FALSE, the callback is run @a before the default handler, so the plugin
|
|
|
|
* can prevent Geany from processing the notification. Use this with care. */
|
|
|
|
{ "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
|
|
|
|
{ NULL, NULL, FALSE, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-10-24 12:04:15 +00:00
|
|
|
/* Callback when the menu item is clicked. */
|
2007-06-26 16:17:16 +00:00
|
|
|
static void
|
|
|
|
item_activate(GtkMenuItem *menuitem, gpointer gdata)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
2015-08-23 15:23:29 +02:00
|
|
|
GeanyPlugin *plugin = gdata;
|
|
|
|
GeanyData *geany_data = plugin->geany_data;
|
2007-06-26 16:17:16 +00:00
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new(
|
2015-08-23 15:23:29 +02:00
|
|
|
GTK_WINDOW(geany_data->main_widgets->window),
|
2007-06-26 16:17:16 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_INFO,
|
|
|
|
GTK_BUTTONS_OK,
|
2008-02-20 11:24:23 +00:00
|
|
|
"%s", welcome_text);
|
2007-06-26 16:17:16 +00:00
|
|
|
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
|
2015-08-23 15:23:29 +02:00
|
|
|
_("(From the %s plugin)"), plugin->info->name);
|
2007-06-26 16:17:16 +00:00
|
|
|
|
|
|
|
gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-23 15:23:29 +02:00
|
|
|
/* Called by Geany to initialize the plugin */
|
2015-08-23 15:23:31 +02:00
|
|
|
static gboolean demo_init(GeanyPlugin *plugin, gpointer data)
|
2007-06-26 16:17:16 +00:00
|
|
|
{
|
2007-06-27 15:56:42 +00:00
|
|
|
GtkWidget *demo_item;
|
2015-08-23 15:23:29 +02:00
|
|
|
GeanyData *geany_data = plugin->geany_data;
|
2007-06-27 15:56:42 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Add an item to the Tools menu */
|
2007-06-27 15:56:42 +00:00
|
|
|
demo_item = gtk_menu_item_new_with_mnemonic(_("_Demo Plugin"));
|
|
|
|
gtk_widget_show(demo_item);
|
2015-08-23 15:23:29 +02:00
|
|
|
gtk_container_add(GTK_CONTAINER(geany_data->main_widgets->tools_menu), demo_item);
|
|
|
|
g_signal_connect(demo_item, "activate", G_CALLBACK(item_activate), plugin);
|
2007-06-27 15:56:42 +00:00
|
|
|
|
2008-10-14 17:03:44 +00:00
|
|
|
/* make the menu item sensitive only when documents are open */
|
2008-12-02 17:05:50 +00:00
|
|
|
ui_add_document_sensitive(demo_item);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* keep a pointer to the menu item, so we can remove it when the plugin is unloaded */
|
2008-10-14 17:03:44 +00:00
|
|
|
main_menu_item = demo_item;
|
|
|
|
|
|
|
|
welcome_text = g_strdup(_("Hello World!"));
|
2015-08-23 15:23:29 +02:00
|
|
|
|
2015-08-23 15:23:33 +02:00
|
|
|
/* This might seem strange but is a method to get the GeanyPlugin pointer passed to
|
|
|
|
* on_editor_notify(). PluginCallback functions get the same data that was set via
|
|
|
|
* GEANY_PLUING_REGISTER_FULL() or geany_plugin_set_data() by default (unless the data pointer
|
|
|
|
* was set to non-NULL at compile time).
|
|
|
|
* This is really only done for demoing PluginCallback. Actual plugins will use real custom
|
|
|
|
* data and perhaps embed the GeanyPlugin or GeanyData pointer their if they also use
|
|
|
|
* PluginCallback. */
|
|
|
|
geany_plugin_set_data(plugin, plugin, NULL);
|
2015-08-23 15:23:31 +02:00
|
|
|
return TRUE;
|
2007-06-26 16:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-23 15:23:29 +02:00
|
|
|
/* Callback connected in demo_configure(). */
|
2008-05-28 13:05:09 +00:00
|
|
|
static void
|
|
|
|
on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
|
|
|
|
{
|
|
|
|
/* catch OK or Apply clicked */
|
|
|
|
if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
|
|
|
|
{
|
|
|
|
/* We only have one pref here, but for more you would use a struct for user_data */
|
|
|
|
GtkWidget *entry = GTK_WIDGET(user_data);
|
|
|
|
|
|
|
|
g_free(welcome_text);
|
|
|
|
welcome_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
|
|
|
|
/* maybe the plugin should write here the settings into a file
|
|
|
|
* (e.g. using GLib's GKeyFile API)
|
|
|
|
* all plugin specific files should be created in:
|
2008-07-07 16:16:18 +00:00
|
|
|
* geany->app->configdir G_DIR_SEPARATOR_S plugins G_DIR_SEPARATOR_S pluginname G_DIR_SEPARATOR_S
|
2008-11-13 14:37:47 +00:00
|
|
|
* e.g. this could be: ~/.config/geany/plugins/Demo/, please use geany->app->configdir */
|
2008-05-28 13:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 18:15:46 +00:00
|
|
|
/* Called by Geany to show the plugin's configure dialog. This function is always called after
|
2015-08-23 15:23:29 +02:00
|
|
|
* demo_init() was called.
|
2007-11-20 18:15:46 +00:00
|
|
|
* You can omit this function if the plugin doesn't need to be configured.
|
|
|
|
* Note: parent is the parent window which can be used as the transient window for the created
|
|
|
|
* dialog. */
|
2015-08-23 15:23:29 +02:00
|
|
|
static GtkWidget *demo_configure(GeanyPlugin *plugin, GtkDialog *dialog, gpointer data)
|
2007-11-20 18:15:46 +00:00
|
|
|
{
|
2008-05-28 13:05:09 +00:00
|
|
|
GtkWidget *label, *entry, *vbox;
|
2007-11-20 18:15:46 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* example configuration dialog */
|
2008-05-28 13:05:09 +00:00
|
|
|
vbox = gtk_vbox_new(FALSE, 6);
|
2007-11-20 18:15:46 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* add a label and a text entry to the dialog */
|
|
|
|
label = gtk_label_new(_("Welcome text to show:"));
|
2007-11-20 18:15:46 +00:00
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
|
|
|
|
entry = gtk_entry_new();
|
|
|
|
if (welcome_text != NULL)
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(entry), welcome_text);
|
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), label);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), entry);
|
|
|
|
|
2008-05-28 13:05:09 +00:00
|
|
|
gtk_widget_show_all(vbox);
|
|
|
|
|
|
|
|
/* Connect a callback for when the user clicks a dialog button */
|
|
|
|
g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), entry);
|
|
|
|
return vbox;
|
2007-11-20 18:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-27 15:56:42 +00:00
|
|
|
/* Called by Geany before unloading the plugin.
|
2007-11-20 18:15:46 +00:00
|
|
|
* Here any UI changes should be removed, memory freed and any other finalization done.
|
2015-08-23 15:23:29 +02:00
|
|
|
* Be sure to leave Geany as it was before demo_init(). */
|
|
|
|
static void demo_cleanup(GeanyPlugin *plugin, gpointer data)
|
2007-06-26 16:17:16 +00:00
|
|
|
{
|
2015-08-23 15:23:29 +02:00
|
|
|
/* remove the menu item added in demo_init() */
|
2008-10-14 17:03:44 +00:00
|
|
|
gtk_widget_destroy(main_menu_item);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* release other allocated strings and objects */
|
2007-11-20 18:15:46 +00:00
|
|
|
g_free(welcome_text);
|
2007-06-26 16:17:16 +00:00
|
|
|
}
|
2015-08-23 15:23:29 +02:00
|
|
|
|
2015-08-23 15:23:31 +02:00
|
|
|
void geany_load_module(GeanyPlugin *plugin)
|
2015-08-23 15:23:29 +02:00
|
|
|
{
|
|
|
|
/* main_locale_init() must be called for your package before any localization can be done */
|
|
|
|
main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
|
|
|
|
plugin->info->name = _("Demo");
|
|
|
|
plugin->info->description = _("Example plugin.");
|
|
|
|
plugin->info->version = "0.4";
|
|
|
|
plugin->info->author = _("The Geany developer team");
|
|
|
|
|
|
|
|
plugin->funcs->init = demo_init;
|
|
|
|
plugin->funcs->configure = demo_configure;
|
|
|
|
plugin->funcs->help = NULL; /* This demo has no help but it is an option */
|
|
|
|
plugin->funcs->cleanup = demo_cleanup;
|
|
|
|
plugin->funcs->callbacks = demo_callbacks;
|
|
|
|
|
2015-08-23 15:23:31 +02:00
|
|
|
GEANY_PLUGIN_REGISTER(plugin, 225);
|
2015-08-23 15:23:29 +02:00
|
|
|
}
|