dialog-define-names.c 8.62 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 186 187 188 189 190 191 192 193 194 195

	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);
196 197 198 199 200 201 202 203 204

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

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

	return TRUE;
}

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

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

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

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

	if (button == state->add_button || button == state->ok_button)
236
		cb_name_guru_add (state);
237 238 239

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

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

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

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

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

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

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

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

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

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

	g_return_if_fail (wb != NULL);

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

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