plugin-manager.c 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * plugin-manager.c: Dialog used to load plugins into the Gnumeric
 * spreadsheet
 *
 * Author:
 *   Tom Dyas (tdyas@romulus.rutgers.edu)
 */
#include <glib.h>
#include <gnome.h>
#include "gnumeric.h"
#include "dialogs.h"
#include "plugin.h"

typedef struct 
{
	GtkWidget *dialog;
	GtkWidget *hbox;
	GtkWidget *vbbox;
	GtkWidget *clist;
	GtkWidget *button_add;
	GtkWidget *button_remove;
	GtkWidget *button_close;
} PluginManager;

static void
add_to_clist (PluginData *pd, GtkWidget *clist)
{
	gchar *data[2];
	gint row;
	
	data [0] = pd->title;
	data [1] = g_module_name (pd->handle);
	
	row = gtk_clist_append (GTK_CLIST (clist), data);
	gtk_clist_set_row_data (GTK_CLIST (clist), row, pd);
}

static void
39
populate_clist (PluginManager *pm)
40
{
41 42 43 44 45
	gtk_widget_set_sensitive (pm->button_remove, 0);
	gtk_clist_freeze (GTK_CLIST (pm->clist));
	gtk_clist_clear (GTK_CLIST (pm->clist));
	g_list_foreach (plugin_list, (GFunc) add_to_clist, pm->clist);
	gtk_clist_thaw (GTK_CLIST (pm->clist));
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

static void
close_cb (GtkWidget *button, PluginManager *pm)
{
	gtk_widget_destroy (pm->dialog);
}

static void
add_cb (GtkWidget *button, PluginManager *pm)
{
	char *modfile = dialog_query_load_file ();
	PluginData *pd;
	
	if (!modfile)
		return;
	
	pd = plugin_load (modfile);
64
	populate_clist (pm);
65 66 67 68 69 70 71 72 73 74
}

static void
remove_cb (GtkWidget *button, PluginManager *pm)
{
	GList *selection = GTK_CLIST (pm->clist)->selection;
	gint row = (gint) g_list_nth_data (selection, 0);
	PluginData *pd = gtk_clist_get_row_data (GTK_CLIST (pm->clist), row);
	
	plugin_unload (pd);
75
	populate_clist (pm);
76 77 78
}

static void
79
row_cb (GtkWidget * clist, gint row, gint col,
80 81
	       GdkEvent *event,  PluginManager *pm)
{
82 83 84 85
	if (GTK_CLIST(clist)->selection != NULL)
		gtk_widget_set_sensitive (pm->button_remove, 1);
	else
		gtk_widget_set_sensitive (pm->button_remove, 0);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

GtkWidget *
plugin_manager_new (void)
{
	PluginManager *pm;
	gchar *titles[2] = { "Name", "File" };
	
	pm = g_new0 (PluginManager, 1);
	if (!pm)
		return NULL;
	
	pm->dialog = gtk_window_new (GTK_WINDOW_DIALOG);
	
	pm->hbox = gtk_hbox_new (0, 0);
	gtk_container_add (GTK_CONTAINER (pm->dialog), pm->hbox);
	
	pm->clist = gtk_clist_new_with_titles (2, titles);
104
	gtk_widget_set_usize (pm->clist, 300, 120);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	gtk_box_pack_start (GTK_BOX (pm->hbox), pm->clist, 1, 1, 5);
	
	pm->vbbox = gtk_vbutton_box_new ();
	gtk_button_box_set_layout (GTK_BUTTON_BOX (pm->vbbox), GTK_BUTTONBOX_START);
	gtk_box_pack_start (GTK_BOX (pm->hbox), pm->vbbox, 0, 0, 5);
	
	pm->button_close = gtk_button_new_with_label ("Close");
	gtk_container_add (GTK_CONTAINER(pm->vbbox), pm->button_close);
	
	pm->button_add = gtk_button_new_with_label ("Add...");
	gtk_container_add (GTK_CONTAINER (pm->vbbox), pm->button_add);
	
	pm->button_remove = gtk_button_new_with_label ("Remove");
	gtk_container_add (GTK_CONTAINER (pm->vbbox), pm->button_remove);
	
	gtk_widget_realize (pm->clist);
121
	populate_clist (pm);
122 123 124 125 126 127 128 129 130 131 132
	
	gtk_signal_connect (GTK_OBJECT (pm->button_close), "clicked",
			    GTK_SIGNAL_FUNC (close_cb), pm);
	
	gtk_signal_connect(GTK_OBJECT (pm->button_add), "clicked",
			   GTK_SIGNAL_FUNC (add_cb), pm);
	
	gtk_signal_connect (GTK_OBJECT (pm->button_remove), "clicked",
			   GTK_SIGNAL_FUNC (remove_cb), pm);
	
	gtk_signal_connect(GTK_OBJECT (pm->clist), "select_row",
133
			   GTK_SIGNAL_FUNC (row_cb), pm);
134 135
	
	gtk_signal_connect(GTK_OBJECT (pm->clist), "unselect_row",
136
			   GTK_SIGNAL_FUNC (row_cb), pm);
137 138 139 140 141
	
	gtk_widget_show_all (pm->hbox);
	
	return pm->dialog;
}