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

21 22
#define LIST_KEY "name_list_data"

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

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

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

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

	if (sel == NULL)
		return;

	g_return_if_fail (sel->data != NULL);

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

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

87 88
	state->cur_name = expr_name;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)
235
		cb_name_guru_add (state);
236 237 238

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

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

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

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

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

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

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

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

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

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

	g_return_if_fail (wb != NULL);

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

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