dialog-define-names.c 8.7 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 "str.h"
16
#include "expr-name.h"
Jody Goldberg's avatar
Jody Goldberg committed
17
#include "sheet.h"
18
#include "workbook.h"
19
#include "workbook-edit.h"
20
#include "gnumeric-util.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
21

22 23
#define LIST_KEY "name_list_data"

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

34 35 36 37 38
	GtkWidget *ok_button;
	GtkWidget *add_button;
	GtkWidget *close_button;
	GtkWidget *delete_button;

Jody Goldberg's avatar
Jody Goldberg committed
39
	Workbook  *wb;
40
	Sheet	  *sheet;
41
} NameGuruState;
Michael Meeks's avatar
Michael Meeks committed
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 68 69
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;
}

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

	if (sel == NULL)
		return;

	g_return_if_fail (sel->data != NULL);

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

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

88 89
	state->cur_name = expr_name;

Jody Goldberg's avatar
Jody Goldberg committed
90 91 92 93 94 95 96
	/* 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);
97 98 99

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

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

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

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

115
	state->expr_names = expr_name_list (state->wb, state->sheet, FALSE);
116

117
	list = GTK_CONTAINER (state->list);
118
	for (names = state->expr_names ; names != NULL ; names = g_list_next (names)) {
119
		NamedExpression *expr_name = names->data;
120 121 122 123 124 125 126 127 128 129
		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);
130
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
Jody Goldberg's avatar
Jody Goldberg committed
131
		gtk_container_add (list, li);
Michael Meeks's avatar
Michael Meeks committed
132
	}
Jody Goldberg's avatar
Jody Goldberg committed
133
	gtk_widget_show_all (GTK_WIDGET (state->list));
134 135 136
}

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

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

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

		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
152
	}
153 154 155
}

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

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

Jody Goldberg's avatar
Jody Goldberg committed
171 172 173 174 175 176 177
	/* FIXME : Need to be able to control scope.
	 *        1) sheet
	 *        2) workbook
	 *        3) global
	 *
	 * default to workbook for now.
	 */
178 179 180
	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
181 182 183 184 185

	expr_name = expr_name_lookup (pp, name);

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

Jody Goldberg's avatar
Jody Goldberg committed
186 187 188 189 190
	/* If the expression is invalid */
	if (expr == NULL) {
		gnumeric_notice (state->wb, GNOME_MESSAGE_BOX_ERROR, error);
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
191
	/* If name already exists replace the its content */
Jody Goldberg's avatar
Jody Goldberg committed
192
	} else if (expr_name) {
Jody Goldberg's avatar
Jody Goldberg committed
193 194 195 196 197
		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,
Morten Welinder's avatar
Morten Welinder committed
198
					 _("You cannot redefine a builtin name."));
Jody Goldberg's avatar
Jody Goldberg committed
199 200
	} else
		expr_name = expr_name_add (state->wb, NULL, name, expr, &error);
201

Jody Goldberg's avatar
Jody Goldberg committed
202
	g_return_val_if_fail (expr_name != NULL, FALSE);
203

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

	return TRUE;
}

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

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

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

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

	if (button == state->add_button || button == state->ok_button)
Jody Goldberg's avatar
Jody Goldberg committed
235 236 237
		/* If adding the name failed do not exit */
		if (!cb_name_guru_add (state))
			return;
238 239 240

	if (button == state->close_button || button == state->ok_button)
		gtk_widget_destroy (state->dialog);
241 242
}

243 244
static GtkWidget *
name_guru_init_button (NameGuruState *state, char const *name)
245
{
246 247 248 249 250
	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;
251 252
}

253 254
static gboolean
cb_name_guru_destroy (GtkObject *w, NameGuruState *state)
255
{
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	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;
275 276
}

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

287
	state->dialog = glade_xml_get_widget (state->gui, "NameGuru");
288 289
	state->name  = GTK_ENTRY (glade_xml_get_widget (state->gui, "name"));
	state->value = GTK_ENTRY (glade_xml_get_widget (state->gui, "value"));
290
	state->scope = GTK_COMBO (glade_xml_get_widget (state->gui, "scope_combo"));
291 292
	state->list  = GTK_LIST  (glade_xml_get_widget (state->gui, "name_list"));
	state->expr_names = NULL;
293 294 295 296 297 298
	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);
299 300 301 302 303 304 305

	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",
306
			    GTK_SIGNAL_FUNC (cb_name_guru_select_name), state);
Jody Goldberg's avatar
Jody Goldberg committed
307 308 309 310
	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);
311
	gtk_signal_connect (GTK_OBJECT (state->dialog), "destroy",
Jody Goldberg's avatar
Jody Goldberg committed
312
			    GTK_SIGNAL_FUNC (cb_name_guru_destroy), state);
313

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

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

	return FALSE;
Michael Meeks's avatar
Michael Meeks committed
325 326
}

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

	g_return_if_fail (wb != NULL);

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

Jody Goldberg's avatar
Jody Goldberg committed
340
	name_guru_populate_list (state);
341
	gtk_widget_show (state->dialog);
Miguel de Icaza's avatar
Miguel de Icaza committed
342
}