dialog-define-names.c 8.58 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1 2 3 4
/* vim: set sw=8: */

/*
 * dialog-define-name.c: Edit named regions.
Miguel de Icaza's avatar
Miguel de Icaza committed
5 6
 *
 * Author:
Jody Goldberg's avatar
Jody Goldberg committed
7 8 9
 * 	Jody Goldberg <jgoldberg@home.com> 
 *	Michael Meeks <michael@imaginator.com>
 */
Miguel de Icaza's avatar
Miguel de Icaza committed
10 11
#include <config.h>
#include <gnome.h>
Michael Meeks's avatar
Michael Meeks committed
12
#include <glade/glade.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
13
#include "dialogs.h"
14
#include "expr.h"
15
#include "expr-name.h"
16
#include "workbook.h"
17
#include "workbook-edit.h"
18
#include "gnumeric-util.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
19

20 21
#define LIST_KEY "name_list_data"

Michael Meeks's avatar
Michael Meeks committed
22 23
typedef struct {
	GladeXML  *gui;
24
	GtkWidget *dialog;
25 26 27
	GtkList   *list;
	GtkEntry  *name;
	GtkEntry  *value;
28
	GtkCombo  *scope;
Michael Meeks's avatar
Michael Meeks committed
29
	GList     *expr_names;
30
	NamedExpression *cur_name;
Jody Goldberg's avatar
Jody Goldberg committed
31

32 33 34 35 36
	GtkWidget *ok_button;
	GtkWidget *add_button;
	GtkWidget *close_button;
	GtkWidget *delete_button;

Jody Goldberg's avatar
Jody Goldberg committed
37
	Workbook  *wb;
38
	Sheet	  *sheet;
39
} NameGuruState;
Michael Meeks's avatar
Michael Meeks committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void
cb_scope_changed (GtkEntry *entry, NameGuruState *state)
{
	if (state->cur_name != NULL)
		puts("changed");
}

static void
name_guru_init_scope (NameGuruState *state)
{
	NamedExpression *cur = state->cur_name;
	GList *list = NULL;

	if (cur != NULL && cur->sheet) {
		list = g_list_prepend (list, _("Workbook"));
		list = g_list_prepend (list, cur->sheet->name_unquoted);
	} else {
		list = g_list_prepend (list, state->sheet->name_unquoted);
		list = g_list_prepend (list, _("Workbook"));
	}

	state->cur_name = NULL;
	gtk_combo_set_popdown_strings (state->scope, list);
	g_list_free (list);
	state->cur_name = cur;
}

68
static void
Jody Goldberg's avatar
Jody Goldberg committed
69
cb_name_guru_select_name (GtkWidget *list, NameGuruState *state)
70
{
Jody Goldberg's avatar
Jody Goldberg committed
71 72 73
	NamedExpression *expr_name;
	GList *sel = GTK_LIST(list)->selection;
	char *txt;
74 75 76 77 78 79

	if (sel == NULL)
		return;

	g_return_if_fail (sel->data != NULL);

Jody Goldberg's avatar
Jody Goldberg committed
80
	expr_name = gtk_object_get_data (GTK_OBJECT (sel->data), LIST_KEY);
81

82
	g_return_if_fail (expr_name != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
83 84 85
	g_return_if_fail (expr_name->name != NULL);
	g_return_if_fail (expr_name->name->str != NULL);

86 87
	state->cur_name = expr_name;

Jody Goldberg's avatar
Jody Goldberg committed
88 89 90 91 92 93 94
	/* Display the name */
	gtk_entry_set_text (state->name, expr_name->name->str);

	/* Display the value */
	txt = expr_name_value (expr_name);
	gtk_entry_set_text (state->value, txt);
	g_free (txt);
95 96 97

	/* Init the scope combo box */
	name_guru_init_scope (state);
Michael Meeks's avatar
Michael Meeks committed
98 99 100
}

static void
Jody Goldberg's avatar
Jody Goldberg committed
101
name_guru_populate_list (NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
102 103
{
	GList *names;
Jody Goldberg's avatar
Jody Goldberg committed
104
	GtkContainer *list;
Michael Meeks's avatar
Michael Meeks committed
105

106 107
	g_return_if_fail (state != NULL);
	g_return_if_fail (state->list != NULL);
Michael Meeks's avatar
Michael Meeks committed
108

109
	state->cur_name = NULL;
110 111
	if (state->expr_names != NULL)
		g_list_free (state->expr_names);
Michael Meeks's avatar
Michael Meeks committed
112

113
	state->expr_names = expr_name_list (state->wb, state->sheet, FALSE);
114

115
	list = GTK_CONTAINER (state->list);
116
	for (names = state->expr_names ; names != NULL ; names = g_list_next (names)) {
117
		NamedExpression *expr_name = names->data;
118 119 120 121 122 123 124 125 126 127
		GtkWidget *li;

		if (expr_name->sheet != NULL) {
			char *name = g_strdup_printf ("%s!%s",
						      expr_name->sheet->name_unquoted,
						      expr_name->name->str);
			li = gtk_list_item_new_with_label (name);
			g_free (name);
		} else
			li = gtk_list_item_new_with_label (expr_name->name->str);
128
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
Jody Goldberg's avatar
Jody Goldberg committed
129
		gtk_container_add (list, li);
Michael Meeks's avatar
Michael Meeks committed
130
	}
Jody Goldberg's avatar
Jody Goldberg committed
131
	gtk_widget_show_all (GTK_WIDGET (state->list));
132 133 134
}

static void
135
cb_name_guru_remove (GtkWidget *ignored, NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
136
{
137 138
	g_return_if_fail (state != NULL);

139 140
	if (state->cur_name != NULL) {
		GList *na = g_list_remove (state->expr_names, state->cur_name);
141

Jody Goldberg's avatar
Jody Goldberg committed
142 143
		g_return_if_fail (na != NULL);
		g_return_if_fail (na->data != NULL);
144 145 146 147 148 149

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

		gtk_entry_set_text (state->name, "");
		gtk_entry_set_text (state->value, "");
Jody Goldberg's avatar
Jody Goldberg committed
150
	}
151 152 153
}

static gboolean
154
cb_name_guru_add (NameGuruState *state)
155
{
156
	NamedExpression     *expr_name;
Jody Goldberg's avatar
Jody Goldberg committed
157 158 159
	ExprTree     *expr;
	gchar        *name, *value, *error;
	ParsePos      pos, *pp;
160 161 162 163 164 165 166 167

	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;
168

Jody Goldberg's avatar
Jody Goldberg committed
169 170 171 172 173 174 175
	/* FIXME : Need to be able to control scope.
	 *        1) sheet
	 *        2) workbook
	 *        3) global
	 *
	 * default to workbook for now.
	 */
176 177 178
	pp = parse_pos_init (&pos, state->wb, state->sheet,
			     state->sheet->cursor.edit_pos.col,
			     state->sheet->cursor.edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

	expr_name = expr_name_lookup (pp, name);

	expr = expr_parse_string (value, pp, NULL, &error);

	/* If name already exists replace the its content */
	if (expr_name) {
		if (!expr_name->builtin) {
			expr_tree_unref (expr_name->t.expr_tree);
			expr_name->t.expr_tree = expr;
		} else
			gnumeric_notice (state->wb, GNOME_MESSAGE_BOX_ERROR,
					 _("You can not redefine a builtin name."));
	} else
		expr_name = expr_name_add (state->wb, NULL, name, expr, &error);
194 195 196 197 198 199 200 201 202

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

203
	gtk_list_clear_items (state->list, 0, -1);
Jody Goldberg's avatar
Jody Goldberg committed
204
	name_guru_populate_list (state);
205 206 207 208

	return TRUE;
}

Jody Goldberg's avatar
Jody Goldberg committed
209 210 211
static void
cb_name_guru_value_focus (GtkWidget *w, GdkEventFocus *ev, NameGuruState *state)
{
Jody Goldberg's avatar
Jody Goldberg committed
212 213
	GtkEntry *entry = GTK_ENTRY (w);
	if (entry == state->value) {
Jody Goldberg's avatar
Jody Goldberg committed
214 215 216 217 218 219
		workbook_set_entry (state->wb, state->value);
		workbook_edit_select_absolute (state->wb);
	} else
		workbook_set_entry (state->wb, NULL);
}

220
static void
221
cb_name_guru_clicked (GtkWidget *button, NameGuruState *state)
222
{
223 224 225
	if (state->dialog == NULL)
		return;

Jody Goldberg's avatar
Jody Goldberg committed
226 227
	workbook_set_entry (state->wb, NULL);

228
	if (button == state->delete_button) {
229
		cb_name_guru_remove (NULL, state);
230 231 232 233
		return;
	}

	if (button == state->add_button || button == state->ok_button)
234
		cb_name_guru_add (state);
235 236 237

	if (button == state->close_button || button == state->ok_button)
		gtk_widget_destroy (state->dialog);
238 239
}

240 241
static GtkWidget *
name_guru_init_button (NameGuruState *state, char const *name)
242
{
243 244 245 246 247
	GtkWidget *tmp = glade_xml_get_widget (state->gui, name);
	gtk_signal_connect (GTK_OBJECT (tmp), "clicked",
			    GTK_SIGNAL_FUNC (cb_name_guru_clicked),
			    state);
	return tmp;
248 249
}

250 251
static gboolean
cb_name_guru_destroy (GtkObject *w, NameGuruState *state)
252
{
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	g_return_val_if_fail (w != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);

	workbook_edit_detach_guru (state->wb);

	if (state->gui != NULL) {
		gtk_object_unref (GTK_OBJECT (state->gui));
		state->gui = NULL;
	}

	/* Handle window manger closing the dialog.
	 * This will be ignored if we are being destroyed differently.
	 */
	workbook_finish_editing (state->wb, FALSE);

	state->dialog = NULL;

	g_free (state);
	return FALSE;
272 273
}

274
static gboolean
275
name_guru_init (NameGuruState *state, Workbook *wb)
276
{
277 278
	state->wb  = wb;
	state->sheet = wb->current_sheet;
279 280 281 282 283
	state->gui = gnumeric_glade_xml_new (workbook_command_context_gui (state->wb),
					     "names.glade");
        if (state->gui == NULL)
                return TRUE;

284
	state->dialog = glade_xml_get_widget (state->gui, "NameGuru");
285 286
	state->name  = GTK_ENTRY (glade_xml_get_widget (state->gui, "name"));
	state->value = GTK_ENTRY (glade_xml_get_widget (state->gui, "value"));
287
	state->scope = GTK_COMBO (glade_xml_get_widget (state->gui, "scope_combo"));
288 289
	state->list  = GTK_LIST  (glade_xml_get_widget (state->gui, "name_list"));
	state->expr_names = NULL;
290 291 292 293 294 295
	state->cur_name   = NULL;

	/* Init the scope combo box */
	name_guru_init_scope (state);
	gtk_signal_connect (GTK_OBJECT (state->scope->entry), "changed",
			    GTK_SIGNAL_FUNC (cb_scope_changed), state);
296 297 298 299 300 301 302

	state->ok_button = name_guru_init_button (state, "ok_button");
	state->close_button = name_guru_init_button (state, "close_button");
	state->add_button = name_guru_init_button (state, "add_button");
	state->delete_button = name_guru_init_button (state, "delete_button");

	gtk_signal_connect (GTK_OBJECT (state->list), "selection_changed",
303
			    GTK_SIGNAL_FUNC (cb_name_guru_select_name), state);
Jody Goldberg's avatar
Jody Goldberg committed
304 305 306 307
	gtk_signal_connect (GTK_OBJECT (state->name), "focus-in-event",
			    GTK_SIGNAL_FUNC (cb_name_guru_value_focus), state);
	gtk_signal_connect (GTK_OBJECT (state->value), "focus-in-event",
			    GTK_SIGNAL_FUNC (cb_name_guru_value_focus), state);
308
	gtk_signal_connect (GTK_OBJECT (state->dialog), "destroy",
Jody Goldberg's avatar
Jody Goldberg committed
309
			    GTK_SIGNAL_FUNC (cb_name_guru_destroy), state);
310

Jody Goldberg's avatar
Jody Goldberg committed
311 312 313 314
 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_EDITABLE(state->name));
 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_EDITABLE (state->value));
315 316
	gnumeric_combo_enters (GTK_WINDOW (state->dialog),
			       state->scope);
317
	gnumeric_non_modal_dialog (state->wb, GTK_DIALOG (state->dialog));
318 319 320 321

	workbook_edit_attach_guru (state->wb, state->dialog);

	return FALSE;
Michael Meeks's avatar
Michael Meeks committed
322 323
}

Miguel de Icaza's avatar
Miguel de Icaza committed
324 325 326
void
dialog_define_names (Workbook *wb)
{
327
	NameGuruState *state;
Michael Meeks's avatar
Michael Meeks committed
328 329 330

	g_return_if_fail (wb != NULL);

331
	state = g_new (NameGuruState, 1);
332
	if (name_guru_init (state, wb)) {
333
		g_free (state);
Michael Meeks's avatar
Michael Meeks committed
334 335 336
		return;
	}

Jody Goldberg's avatar
Jody Goldberg committed
337
	name_guru_populate_list (state);
338
	gtk_widget_show (state->dialog);
Miguel de Icaza's avatar
Miguel de Icaza committed
339
}