dialog-define-names.c 6.32 KB
Newer Older
Michael Meeks's avatar
Michael Meeks committed
1 2
/**
 * dialog-summary.c:  Implements the summary info stuff
Miguel de Icaza's avatar
Miguel de Icaza committed
3 4
 *
 * Author:
Michael Meeks's avatar
Michael Meeks committed
5
 *        Michael Meeks <michael@imaginator.com>
Miguel de Icaza's avatar
Miguel de Icaza committed
6
 *
Michael Meeks's avatar
Michael Meeks committed
7
 **/
Miguel de Icaza's avatar
Miguel de Icaza committed
8 9
#include <config.h>
#include <gnome.h>
Michael Meeks's avatar
Michael Meeks committed
10
#include <glade/glade.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
11
#include "dialogs.h"
12
#include "expr.h"
13
#include "workbook.h"
14
#include "gnumeric-util.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
15

16 17
#define LIST_KEY "name_list_data"

Michael Meeks's avatar
Michael Meeks committed
18 19 20 21
typedef struct {
	Workbook  *wb;
	GladeXML  *gui;
	GtkWidget *dia;
22 23 24
	GtkList   *list;
	GtkEntry  *name;
	GtkEntry  *value;
Michael Meeks's avatar
Michael Meeks committed
25 26 27 28
	GList     *expr_names;
	gint       selected;
} state_t;

29 30 31 32 33 34 35 36 37
static void
update_edit (state_t *state)
{
	gint          i = state->selected;
	ExprName     *expr_name;
	Sheet        *sheet;
	EvalPosition  ep;
	char         *txt;
	
38
	sheet = state->wb->current_sheet;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	g_return_if_fail (sheet != NULL);

	eval_pos_init (&ep, sheet, 0, 0);

	expr_name = g_list_nth (state->expr_names, i)->data;
	if (expr_name->name && expr_name->name->str)
		gtk_entry_set_text (state->name, expr_name->name->str);
	else
		gtk_entry_set_text (state->name, "");
	
	txt = expr_name_value (expr_name);
	gtk_entry_set_text (state->value, txt);
	g_free (txt);
}

Michael Meeks's avatar
Michael Meeks committed
54 55 56
static void
select_name (GtkWidget *w, state_t *state)
{
57 58 59 60 61 62 63 64 65 66 67 68
	guint     i    = 0;
	GList    *sel  = GTK_LIST(w)->selection;
	GList    *p    = state->expr_names;
	ExprName *name;

	if (sel == NULL)
		return;

	g_return_if_fail (sel->data != NULL);

	name = gtk_object_get_data (GTK_OBJECT (sel->data), LIST_KEY);

Michael Meeks's avatar
Michael Meeks committed
69
	while (p) {
70
		if (p->data == name) {
Michael Meeks's avatar
Michael Meeks committed
71
			state->selected = i;
72
			update_edit (state);
Michael Meeks's avatar
Michael Meeks committed
73 74 75 76 77 78 79 80 81 82 83
			return;
		}
		i++;
		p = g_list_next (p);
	}
}

static void
fill_list (state_t *state)
{
	GList *names;
84
	GList *items = NULL;
Michael Meeks's avatar
Michael Meeks committed
85

86 87
	g_return_if_fail (state != NULL);
	g_return_if_fail (state->list != NULL);
Michael Meeks's avatar
Michael Meeks committed
88

89
	state->selected   = -1;
Michael Meeks's avatar
Michael Meeks committed
90

91 92
	/* FIXME: scoping issues here */
	state->expr_names = names = expr_name_list (state->wb, NULL, FALSE);
Michael Meeks's avatar
Michael Meeks committed
93 94 95 96
	
	while (names) {
		ExprName *expr_name = names->data;
		GtkWidget *li = gtk_list_item_new_with_label (expr_name->name->str);
97 98 99
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
		gtk_widget_show (GTK_WIDGET (li));
		items = g_list_append (items, li);
Michael Meeks's avatar
Michael Meeks committed
100 101
		names = g_list_next (names);
	}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	gtk_list_append_items (state->list, items);
}

static void
destroy_state (state_t *state)
{
	if (state->expr_names)
		g_list_free (state->expr_names);
	state->expr_names = NULL;
	state->selected = -1;
}

static void
empty_list (state_t *state)
{
	if (state->list)
		gtk_list_clear_items (state->list, 0, -1);
	state->list = NULL;
	destroy_state (state);
Michael Meeks's avatar
Michael Meeks committed
121 122 123 124 125
}

static void
remove_name (GtkWidget *widget, state_t *state)
{
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/*	gint i;
	g_return_if_fail (state != NULL);
	g_return_if_fail (widget != NULL);

	i = state->selected;
	if (i >= 0) {
		GList *na           = g_list_nth (state->expr_names, i);
		GList *it           = g_list_nth (state->list_items, i);
		GList *l;

		g_return_if_fail (it != NULL && na != NULL);
		g_return_if_fail (it->data != NULL && na->data != NULL);

		expr_name_remove (na->data);
		state->expr_names = g_list_remove (state->expr_names, na->data);

		l = g_list_append (NULL, it->data);
		gtk_list_remove_items (state->list, l);
		g_list_free (l);
		state->list_items = g_list_remove (state->list_items, it->data);

		gtk_entry_set_text (state->name, "");
		gtk_entry_set_text (state->value, "");
		}*/
	g_warning ("Unimplemented, need to sweep sheets to check for usage");
}

static gboolean
grab_text_ok (state_t *state, gboolean update_list)
{
	gchar        *name;
	gchar        *value;
	ExprName     *expr_name;
	char         *error;

	g_return_val_if_fail (state != NULL, FALSE);

	value = gtk_entry_get_text (state->value);
	name  = gtk_entry_get_text (state->name);

	if (!name || (name[0] == '\0'))
		return TRUE;
Michael Meeks's avatar
Michael Meeks committed
168
	
169 170
	/* FIXME: we need to be able to select names scope ideally */
	expr_name = expr_name_lookup (state->wb, NULL, name);
171 172 173
	if (expr_name)
		expr_name_remove (expr_name);

174 175
	/* FIXME: and here */
	expr_name = expr_name_create (state->wb, NULL, name,
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
				      value, &error);

	if (expr_name == NULL) {
		if (error)
			gnumeric_notice (state->wb, GNOME_MESSAGE_BOX_ERROR, error);
		else
			g_warning ("serious name error");
		return FALSE;
	}

	if (update_list) {
		empty_list (state);
		fill_list  (state);
	}

	return TRUE;
}

static void
destroy_dialog (state_t *state)
{
	if (state->dia)
		gnome_dialog_close (GNOME_DIALOG (state->dia));
	state->dia   = NULL;
	state->list  = NULL;
	state->name  = NULL;
	state->value = NULL;
}

static void
add_name (GtkWidget *widget, state_t *state)
{
	grab_text_ok (state, TRUE);
}

static void
close_name (GtkWidget *widget, state_t *state)
{
	destroy_state  (state);
	destroy_dialog (state);
}

static void
ok_name (GtkWidget *widget, state_t *state)
{
	if (grab_text_ok (state, FALSE))
		close_name (widget, state);
Michael Meeks's avatar
Michael Meeks committed
223 224
}

Miguel de Icaza's avatar
Miguel de Icaza committed
225 226 227
void
dialog_define_names (Workbook *wb)
{
Michael Meeks's avatar
Michael Meeks committed
228 229 230 231 232 233 234 235 236 237 238 239 240
	gint       v;
	state_t    state;
	GtkWidget *w;

	g_return_if_fail (wb != NULL);

	state.wb  = wb;
	state.gui = glade_xml_new (GNUMERIC_GLADEDIR "/names.glade", NULL);
	if (!state.gui) {
		printf ("Could not find names.glade\n");
		return;
	}
	
241 242 243 244 245
	state.name  = GTK_ENTRY (glade_xml_get_widget (state.gui, "name"));
	state.value = GTK_ENTRY (glade_xml_get_widget (state.gui, "value"));
	state.list  = GTK_LIST  (glade_xml_get_widget (state.gui, "name_list"));
	state.expr_names = NULL;
	state.selected   = -1;
Michael Meeks's avatar
Michael Meeks committed
246

247 248 249
	w = glade_xml_get_widget (state.gui, "ok");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (ok_name), &state);
Michael Meeks's avatar
Michael Meeks committed
250

251 252 253 254 255 256 257 258
	w = glade_xml_get_widget (state.gui, "close");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (close_name), &state);

	w = glade_xml_get_widget (state.gui, "add");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (add_name), &state);
	
Michael Meeks's avatar
Michael Meeks committed
259 260 261
	w = glade_xml_get_widget (state.gui, "delete");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (remove_name), &state);
Miguel de Icaza's avatar
Miguel de Icaza committed
262
	
Michael Meeks's avatar
Michael Meeks committed
263 264 265 266 267
	state.dia = glade_xml_get_widget (state.gui, "NamesDialog");
	if (!state.dia) {
		printf ("Corrupt file names.glade\n");
		return;
	}
Jody Goldberg's avatar
Jody Goldberg committed
268 269 270 271
 	gnome_dialog_editable_enters(GNOME_DIALOG(state.dia), 
				     GTK_EDITABLE(state.name));
 	gnome_dialog_editable_enters(GNOME_DIALOG(state.dia), 
				     GTK_EDITABLE(state.value));
Michael Meeks's avatar
Michael Meeks committed
272

273 274 275 276
	fill_list (&state);

	gtk_signal_connect (GTK_OBJECT (state.list), "selection_changed",
			    GTK_SIGNAL_FUNC (select_name), &state);
Miguel de Icaza's avatar
Miguel de Icaza committed
277

278
	v = gnumeric_dialog_run (wb, GNOME_DIALOG (state.dia));
279 280 281 282 283
	
	if (v == -1)
		destroy_state (&state);
	else
		destroy_dialog (&state);
Michael Meeks's avatar
Michael Meeks committed
284 285

	gtk_object_unref (GTK_OBJECT (state.gui));
Miguel de Icaza's avatar
Miguel de Icaza committed
286
}