dialog-define-names.c 16.6 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Jody Goldberg's avatar
Jody Goldberg committed
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:
7
 * 	Jody Goldberg <jgoldberg@home.com>
Jody Goldberg's avatar
Jody Goldberg committed
8
 *	Michael Meeks <michael@imaginator.com>
9
 *	Chema Celorio <chema@celorio.com>
Jody Goldberg's avatar
Jody Goldberg committed
10
 */
Miguel de Icaza's avatar
Miguel de Icaza committed
11
#include <config.h>
12 13 14
#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
Michael Meeks's avatar
Michael Meeks committed
15
#include <glade/glade.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
16
#include "dialogs.h"
17
#include "expr.h"
18
#include "str.h"
19
#include "expr-name.h"
Jody Goldberg's avatar
Jody Goldberg committed
20
#include "sheet.h"
21
#include "workbook.h"
22
#include "workbook-control.h"
23
#include "workbook-edit.h"
24
#include "gui-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
25
#include "parse-util.h"
26
#include "widgets/gnumeric-expr-entry.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
27

28
#define LIST_KEY "name_list_data"
29
#define DEFINE_NAMES_KEY "define-names-dialog"
30

31 32 33 34 35
typedef enum {
	NAME_GURU_SCOPE_SHEET,
	NAME_GURU_SCOPE_WORKBOOK,
} NameGuruScope;

Michael Meeks's avatar
Michael Meeks committed
36 37
typedef struct {
	GladeXML  *gui;
38
	GtkWidget *dialog;
39 40
	GtkList   *list;
	GtkEntry  *name;
41
	GnumericExprEntry *expr_text;
42
	GtkCombo  *scope;
Michael Meeks's avatar
Michael Meeks committed
43
	GList     *expr_names;
44
	NamedExpression *cur_name;
Jody Goldberg's avatar
Jody Goldberg committed
45

46 47 48 49
	GtkWidget *ok_button;
	GtkWidget *add_button;
	GtkWidget *close_button;
	GtkWidget *delete_button;
50
	GtkWidget *update_button;
51

52
	Sheet	  *sheet;
53 54
	Workbook  *wb;
	WorkbookControlGUI  *wbcg;
55 56

	gboolean updating;
57
} NameGuruState;
Michael Meeks's avatar
Michael Meeks committed
58

59 60
/**
 * name_guru_warned_if_used:
61 62
 * @state:
 *
63 64
 * If the expresion that is about to be deleted is beeing used,
 * warn the user about it. Ask if we should procede or not
65
 *
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 * Return Value: TRUE if users confirms deletion, FALSE otherwise
 **/
static gboolean
name_guru_warn (NameGuruState *state)
{
	static gboolean warned = FALSE;
	if (!warned)
		g_warning ("Implement me !. name_guru_warned_if_used\n");
	warned = TRUE;

	return TRUE;
}

/**
 * name_guru_scope_change:
81 82 83 84
 * @state:
 * @scope:
 *
 * Change the scope of state->cur_name. Ask the user if we want to procede with the
85 86 87 88 89 90 91 92 93 94 95 96 97
 * change if we are going to invalidate expressions in the sheet.
 *
 * Return Value: FALSE, if the user cancels the scope change
 **/
static gboolean
name_guru_scope_change (NameGuruState *state, NameGuruScope scope)
{
	NamedExpression *expression;

	g_return_val_if_fail (state != NULL, FALSE);
	expression = state->cur_name;
	if (expression == NULL)
		return TRUE;
98

99 100 101 102 103 104 105 106
	/* get the current values for the expression */
	if (scope == NAME_GURU_SCOPE_WORKBOOK) {
		expr_name_sheet2wb (expression);
	}

	if (scope == NAME_GURU_SCOPE_SHEET) {
		if (!name_guru_warn (state))
			return FALSE;
107

108 109
		expr_name_wb2sheet (expression, state->sheet);
	}
110

111 112
	return TRUE;
}
113

114 115 116
static void
cb_scope_changed (GtkEntry *entry, NameGuruState *state)
{
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	NamedExpression *expression;

	if (state->updating)
		return;

	expression = state->cur_name;
	if (expression == NULL)
		return;

	if (!name_guru_scope_change (state,
				     (expression->sheet == NULL) ?
				     NAME_GURU_SCOPE_SHEET :
				     NAME_GURU_SCOPE_WORKBOOK))
		g_print ("Here we toggle the scope back to what it was\n"
			 "The user cancelled the scope change.\n");
132

133 134 135 136 137
}

static void
name_guru_init_scope (NameGuruState *state)
{
138
	NamedExpression *expression;
139 140
	GList *list = NULL;

141 142 143
	expression  = state->cur_name;
	if (expression != NULL && expression->wb) {
		list = g_list_prepend (list, state->sheet->name_unquoted);
144 145 146
		list = g_list_prepend (list, _("Workbook"));
	} else {
		list = g_list_prepend (list, _("Workbook"));
147
		list = g_list_prepend (list, state->sheet->name_unquoted);
148 149 150
	}

	state->cur_name = NULL;
151
	state->updating = TRUE;
152 153
	gtk_combo_set_popdown_strings (state->scope, list);
	g_list_free (list);
154 155
	state->updating = FALSE;
	state->cur_name = expression;
156 157
}

158 159 160 161 162

static void cb_name_guru_select_name (GtkWidget *list, NameGuruState *state);

/**
 * name_guru_set_expr:
163
 * @state:
164
 * @expr_name: Expression to set in the entries, NULL to clear entries
165
 *
166 167 168 169 170 171 172
 * Set the entries in the dialog from an NamedExpression
 **/
static void
name_guru_set_expr (NameGuruState *state, NamedExpression *expr_name)
{
	g_return_if_fail (state != NULL);

173
	/* Don't recur.  */
174
	state->updating = TRUE;
175

176
	if (expr_name) {
177 178
		gchar *txt;

179 180 181
		/* Display the name */
		gtk_entry_set_text (state->name, expr_name->name->str);

Jody Goldberg's avatar
Jody Goldberg committed
182
		/* Display the expr_text */
183
		txt = expr_name_value (expr_name);
184
		gtk_entry_set_text (GTK_ENTRY (state->expr_text), txt);
185 186 187
		g_free (txt);
	} else {
		gtk_entry_set_text (state->name, "");
188
		gtk_entry_set_text (GTK_ENTRY (state->expr_text), "");
189 190 191 192 193 194 195 196 197 198 199 200
	}

	/* unblock them */
	state->updating = FALSE;

	/* Init the scope combo box */
	name_guru_init_scope (state);

}

/**
 * name_guru_clear_selection:
201 202
 * @state:
 *
203 204 205 206 207 208 209 210 211 212 213 214 215 216
 * Clear the selection of the gtklist
 **/
static void
name_guru_clear_selection (NameGuruState *state)
{
	g_return_if_fail (state != NULL);

	state->updating = TRUE;
	gtk_list_unselect_all (state->list);
	state->updating = FALSE;
}

/**
 * name_guru_in_list:
217
 * @name:
218
 * @state:
219
 *
220
 * Given a name, it searches for it inside the list of Names
221
 *
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
 * Return Value: TRUE if name is already defined, FALSE otherwise
 **/
static gboolean
name_guru_in_list (const gchar *name, NameGuruState *state)
{
	NamedExpression *expression;
	GList *list;

	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);

	for (list = state->expr_names; list; list = list->next) {
		expression = (NamedExpression *) list->data;
		g_return_val_if_fail (expression != NULL, FALSE);
		g_return_val_if_fail (expression->name != NULL, FALSE);
		g_return_val_if_fail (expression->name->str != NULL, FALSE);
		if (strcasecmp (name, expression->name->str) == 0) {
			return TRUE;
		}
	}

	return FALSE;
}



/**
 * name_guru_update_sensitivity:
250 251 252
 * @state:
 * @update_entries:
 *
253 254 255 256 257 258 259 260 261
 * Update the dialog widgets sensitivity
 **/
static void
name_guru_update_sensitivity (NameGuruState *state, gboolean update_entries)
{
	gboolean selection;
	gboolean update;
	gboolean add;
	gboolean in_list = FALSE;
Jody Goldberg's avatar
Jody Goldberg committed
262 263
	char const *expr_text;
	char const *name;
264 265 266 267 268

	g_return_if_fail (state->list != NULL);

	if (state->updating)
		return;
269

270
	name  = gtk_entry_get_text (state->name);
271
	expr_text = gtk_entry_get_text (GTK_ENTRY (state->expr_text));
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

	/** Add is active if :
	 *  - We have a name in the entry to add
	 *  - Either we don't have a current Name or if we have a current
	 *     name, the name is different than what we are going to add
	 **/
	add = (name != NULL &&
	       name[0] != '\0' &&
	       !(in_list = name_guru_in_list (name, state)));
	selection = (g_list_length (state->list->selection) > 0);
	update = (name && *name && !add);

	gtk_widget_set_sensitive (state->delete_button, selection && in_list);
	gtk_widget_set_sensitive (state->add_button,    add);
	gtk_widget_set_sensitive (state->update_button, update);

	if (!selection && update_entries)
		name_guru_set_expr (state, NULL);

	if (selection && !in_list)
		name_guru_clear_selection (state);
}



/**
 * name_guru_update_sensitivity_cb:
299 300 301
 * @dummy:
 * @state:
 *
302 303 304 305 306 307 308 309 310
 **/
static void
name_guru_update_sensitivity_cb (GtkWidget *dummy, NameGuruState *state)
{
	name_guru_update_sensitivity (state, FALSE);
}

/**
 * cb_name_guru_select_name:
311 312 313
 * @list:
 * @state:
 *
314 315
 * Set the expression from the selected row in the gtklist
 **/
316
static void
Jody Goldberg's avatar
Jody Goldberg committed
317
cb_name_guru_select_name (GtkWidget *list, NameGuruState *state)
318
{
Jody Goldberg's avatar
Jody Goldberg committed
319 320
	NamedExpression *expr_name;
	GList *sel = GTK_LIST(list)->selection;
321

322
	if (sel == NULL || state->updating)
323 324 325 326
		return;

	g_return_if_fail (sel->data != NULL);

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

329
	g_return_if_fail (expr_name != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
330 331 332
	g_return_if_fail (expr_name->name != NULL);
	g_return_if_fail (expr_name->name->str != NULL);

333 334
	state->cur_name = expr_name;

335 336
	name_guru_set_expr (state, expr_name);
	name_guru_update_sensitivity (state, FALSE);
Jody Goldberg's avatar
Jody Goldberg committed
337

Michael Meeks's avatar
Michael Meeks committed
338 339
}

340

Michael Meeks's avatar
Michael Meeks committed
341
static void
Jody Goldberg's avatar
Jody Goldberg committed
342
name_guru_populate_list (NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
343 344
{
	GList *names;
Jody Goldberg's avatar
Jody Goldberg committed
345
	GtkContainer *list;
Michael Meeks's avatar
Michael Meeks committed
346

347 348
	g_return_if_fail (state != NULL);
	g_return_if_fail (state->list != NULL);
Michael Meeks's avatar
Michael Meeks committed
349

350
	state->cur_name = NULL;
Michael Meeks's avatar
Michael Meeks committed
351

Morten Welinder's avatar
Morten Welinder committed
352
	g_list_free (state->expr_names);
353
	state->expr_names = expr_name_list (state->wb, state->sheet, FALSE);
354

355
	list = GTK_CONTAINER (state->list);
356
	for (names = state->expr_names ; names != NULL ; names = g_list_next (names)) {
357
		NamedExpression *expr_name = names->data;
358 359 360 361 362 363 364 365 366
		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);
367
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
Jody Goldberg's avatar
Jody Goldberg committed
368
		gtk_container_add (list, li);
Michael Meeks's avatar
Michael Meeks committed
369
	}
Jody Goldberg's avatar
Jody Goldberg committed
370
	gtk_widget_show_all (GTK_WIDGET (state->list));
371
	name_guru_update_sensitivity (state, TRUE);
372 373
}

374 375
/**
 * name_guru_scope_get:
376 377
 * @state:
 *
378
 * Get the selected Scope from the combo box
379 380
 *
 * Return Value:
381 382 383 384 385 386 387 388 389
 **/
static NameGuruScope
name_guru_scope_get (NameGuruState *state)
{
	gchar *text;

	text = gtk_entry_get_text (GTK_ENTRY (state->scope->entry));

	g_return_val_if_fail (text != NULL, NAME_GURU_SCOPE_WORKBOOK);
390

391 392 393 394 395 396 397 398 399
	if (strcmp (text, _("Workbook"))==0)
	    return NAME_GURU_SCOPE_WORKBOOK;
	else
	    return NAME_GURU_SCOPE_SHEET;
}


/**
 * cb_name_guru_remove:
400 401 402
 * @ignored:
 * @state:
 *
403 404
 * Remove the state->cur_name
 **/
405
static void
406
cb_name_guru_remove (GtkWidget *ignored, NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
407
{
408 409
	g_return_if_fail (state != NULL);

410
	if (state->cur_name != NULL) {
411 412 413 414 415
		if (!name_guru_warn (state))
			return;
		state->expr_names = g_list_remove (state->expr_names, state->cur_name);
		expr_name_remove (state->cur_name);
		state->cur_name = NULL;
416

417 418 419 420
		gtk_list_clear_items (state->list, 0, -1);
		name_guru_populate_list (state);
	} else {
		g_warning ("Why is the delete button sensitive ? ...\n");
Jody Goldberg's avatar
Jody Goldberg committed
421
	}
422

423 424
}

425 426 427

/**
 * cb_name_guru_add:
428 429
 * @state:
 *
430
 * Update or add a NamedExpression from the values in the gtkentries.
431
 *
432 433
 * Return Value: FALSE if the expression was invalid, TRUE otherwise
 **/
434
static gboolean
435
cb_name_guru_add (NameGuruState *state)
436
{
437
	NamedExpression *expr_name;
Jody Goldberg's avatar
Jody Goldberg committed
438
	ParsePos      pos, *pp;
439
	ParseError    perr;
440
	ExprTree *expr;
Jody Goldberg's avatar
Jody Goldberg committed
441
	char const *name, *expr_text, *tmp;
442
	gchar *error;
443 444 445

	g_return_val_if_fail (state != NULL, FALSE);

446
	expr_text = gtk_entry_get_text (GTK_ENTRY (state->expr_text));
447 448 449 450
	name  = gtk_entry_get_text (state->name);

	if (!name || (name[0] == '\0'))
		return TRUE;
451

452 453 454 455
	pp = parse_pos_init (&pos, state->wb, state->sheet, 0, 0);
	/*
	 * expr name uses 0,0 for now.  Figure out what to do with this
	 * eventually.
456 457
			     state->sheet->edit_pos.col,
			     state->sheet->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
458
         */
Jody Goldberg's avatar
Jody Goldberg committed
459 460 461

	expr_name = expr_name_lookup (pp, name);

Jody Goldberg's avatar
Jody Goldberg committed
462 463
	if (NULL != (tmp = gnumeric_char_start_expr_p (expr_text)))
		expr_text = tmp;
464
	expr = expr_parse_string (expr_text, pp, NULL, &perr);
Jody Goldberg's avatar
Jody Goldberg committed
465

Jody Goldberg's avatar
Jody Goldberg committed
466 467
	/* If the expression is invalid */
	if (expr == NULL) {
468
		gnumeric_notice (state->wbcg, GNOME_MESSAGE_BOX_ERROR, perr.message);
Jody Goldberg's avatar
Jody Goldberg committed
469
		gtk_widget_grab_focus (GTK_WIDGET (state->expr_text));
Jody Goldberg's avatar
Jody Goldberg committed
470 471
		return FALSE;
	} else if (expr_name) {
Jody Goldberg's avatar
Jody Goldberg committed
472
		if (!expr_name->builtin) {
473 474
			/* This means that the expresion was updated updated.
			 * FIXME: if the scope has been changed too, call scope
475
			 * chaned first.
476
			 */
Jody Goldberg's avatar
Jody Goldberg committed
477 478
			expr_tree_unref (expr_name->t.expr_tree);
			expr_name->t.expr_tree = expr;
479
		} else
480
			gnumeric_notice (state->wbcg, GNOME_MESSAGE_BOX_ERROR,
Morten Welinder's avatar
Morten Welinder committed
481
					 _("You cannot redefine a builtin name."));
482 483 484 485 486 487
	} else {
		if (name_guru_scope_get (state) == NAME_GURU_SCOPE_WORKBOOK)
			expr_name = expr_name_add (state->wb, NULL, name, expr, &error);
		else
			expr_name = expr_name_add (NULL, state->sheet, name, expr, &error);
	}
488

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

491
	gtk_list_clear_items (state->list, 0, -1);
Jody Goldberg's avatar
Jody Goldberg committed
492
	name_guru_populate_list (state);
493
	gtk_widget_grab_focus (GTK_WIDGET (state->name));
494 495 496 497 498

	return TRUE;
}

static void
499
cb_name_guru_clicked (GtkWidget *button, NameGuruState *state)
500
{
501 502 503
	if (state->dialog == NULL)
		return;

504
	wbcg_set_entry (state->wbcg, NULL);
505

506
	if (button == state->delete_button) {
507
		cb_name_guru_remove (NULL, state);
508 509 510
		return;
	}

511 512 513 514 515
	if (button == state->add_button ||
	    button == state->update_button ||
	    button == state->ok_button) {
		/* If adding the name failed, do not exit */
		if (!cb_name_guru_add (state)) {
Jody Goldberg's avatar
Jody Goldberg committed
516
			return;
517 518
		}
	}
519

520
	if (button == state->close_button || button == state->ok_button) {
521
		gtk_widget_destroy (state->dialog);
522 523
		return;
	}
524

525 526
}

527 528
static GtkWidget *
name_guru_init_button (NameGuruState *state, char const *name)
529
{
530
	GtkWidget *tmp = glade_xml_get_widget (state->gui, name);
531 532

	g_return_val_if_fail (tmp != NULL, NULL);
533

534 535 536 537
	gtk_signal_connect (GTK_OBJECT (tmp), "clicked",
			    GTK_SIGNAL_FUNC (cb_name_guru_clicked),
			    state);
	return tmp;
538 539
}

540 541
static gboolean
cb_name_guru_destroy (GtkObject *w, NameGuruState *state)
542
{
543 544 545
	g_return_val_if_fail (w != NULL, FALSE);
	g_return_val_if_fail (state != NULL, FALSE);

546
	wbcg_edit_detach_guru (state->wbcg);
547 548 549 550 551 552

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

553
	wbcg_edit_finish (state->wbcg, FALSE);
554 555 556

	state->dialog = NULL;

Morten Welinder's avatar
Morten Welinder committed
557 558 559
	g_list_free (state->expr_names);
	state->expr_names = NULL;

560
	g_free (state);
561

562
	return FALSE;
563 564
}

565 566
static void
cb_name_guru_set_focus (GtkWidget *window, GtkWidget *focus_widget,
567
			NameGuruState *state)
568
{
Jody Goldberg's avatar
Jody Goldberg committed
569
	if (IS_GNUMERIC_EXPR_ENTRY (focus_widget)) {
570
		wbcg_set_entry (state->wbcg,
571 572 573
				    GNUMERIC_EXPR_ENTRY (focus_widget));
		gnumeric_expr_entry_set_absolute (state->expr_text);
	} else
574
		wbcg_set_entry (state->wbcg, NULL);
575 576
}

577
static gboolean
578
name_guru_init (NameGuruState *state, WorkbookControlGUI *wbcg)
579
{
580
	Workbook *wb = wb_control_workbook (WORKBOOK_CONTROL (wbcg));
581
	GtkTable *table2;
582 583 584 585 586

	state->wbcg  = wbcg;
	state->wb   = wb;
	state->sheet = wb_control_cur_sheet (WORKBOOK_CONTROL (wbcg));
	state->gui = gnumeric_glade_xml_new (state->wbcg, "names.glade");
587 588 589
        if (state->gui == NULL)
                return TRUE;

590
	state->dialog = glade_xml_get_widget (state->gui, "NameGuru");
591
	table2 = GTK_TABLE (glade_xml_get_widget (state->gui, "table2"));
592
	state->name  = GTK_ENTRY (glade_xml_get_widget (state->gui, "name"));
Jody Goldberg's avatar
Jody Goldberg committed
593
	state->expr_text = GNUMERIC_EXPR_ENTRY (gnumeric_expr_entry_new (state->wbcg));
594 595 596 597 598
	gtk_table_attach (table2, GTK_WIDGET (state->expr_text),
			  1, 2, 1, 2,
			  GTK_EXPAND | GTK_FILL, 0,
			  0, 0);
	gtk_widget_show (GTK_WIDGET (state->expr_text));
599
	state->scope = GTK_COMBO (glade_xml_get_widget (state->gui, "scope_combo"));
600 601
	state->list  = GTK_LIST  (glade_xml_get_widget (state->gui, "name_list"));
	state->expr_names = NULL;
602
	state->cur_name   = NULL;
603
	state->updating   = FALSE;
604 605 606 607 608

	/* 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);
609

610 611 612
	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");
613
	state->delete_button = name_guru_init_button (state, "delete_button");
614
	state->update_button = name_guru_init_button (state, "update_button");
615 616

	gtk_signal_connect (GTK_OBJECT (state->list), "selection_changed",
617
			    GTK_SIGNAL_FUNC (cb_name_guru_select_name), state);
618 619
	gtk_signal_connect (GTK_OBJECT (state->dialog), "set-focus",
			    GTK_SIGNAL_FUNC (cb_name_guru_set_focus), state);
620
	gtk_signal_connect (GTK_OBJECT (state->dialog), "destroy",
Jody Goldberg's avatar
Jody Goldberg committed
621
			    GTK_SIGNAL_FUNC (cb_name_guru_destroy), state);
622 623
	gtk_signal_connect (GTK_OBJECT (state->name), "changed",
			    GTK_SIGNAL_FUNC (name_guru_update_sensitivity_cb), state);
624 625 626
	/* We need to connect after because this is an expresion, and it will
	 * be changed by the mouse selecting a range, update after the entry
	 * is updated with the new text.
627
	 */
Jody Goldberg's avatar
Jody Goldberg committed
628
	gtk_signal_connect_after (GTK_OBJECT (state->expr_text), "changed",
629
				  GTK_SIGNAL_FUNC (name_guru_update_sensitivity_cb), state);
630

Jody Goldberg's avatar
Jody Goldberg committed
631 632 633
 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_EDITABLE(state->name));
 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
Jody Goldberg's avatar
Jody Goldberg committed
634
				  GTK_EDITABLE (state->expr_text));
635 636
	gnumeric_combo_enters (GTK_WINDOW (state->dialog),
			       state->scope);
637 638
	gnumeric_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
			       DEFINE_NAMES_KEY);
639

640 641
	gnumeric_expr_entry_set_scg (state->expr_text,
				     wb_control_gui_cur_sheet (wbcg));
642
	wbcg_edit_attach_guru (state->wbcg, state->dialog);
643 644

	return FALSE;
Michael Meeks's avatar
Michael Meeks committed
645 646
}

647 648
/**
 * dialog_define_names:
649 650
 * @wbcg:
 *
651 652
 * Create and show the define names dialog.
 **/
Miguel de Icaza's avatar
Miguel de Icaza committed
653
void
654
dialog_define_names (WorkbookControlGUI *wbcg)
Miguel de Icaza's avatar
Miguel de Icaza committed
655
{
656
	NameGuruState *state;
Michael Meeks's avatar
Michael Meeks committed
657

658
	g_return_if_fail (wbcg != NULL);
Michael Meeks's avatar
Michael Meeks committed
659

660
	/* Only one guru per workbook. */
661
	if (wbcg_edit_has_guru (wbcg))
662 663
		return;

664 665 666 667
	/* Only pop up one copy per workbook */
	if (gnumeric_dialog_raise_if_exists (wbcg, DEFINE_NAMES_KEY))
		return;

668
	state = g_new (NameGuruState, 1);
669
	if (name_guru_init (state, wbcg)) {
670 671
		gnumeric_notice (wbcg, GNOME_MESSAGE_BOX_ERROR,
				 _("Could not create the Name Guru."));
672
		g_free (state);
Michael Meeks's avatar
Michael Meeks committed
673 674 675
		return;
	}

Jody Goldberg's avatar
Jody Goldberg committed
676
	name_guru_populate_list (state);
677
	gtk_widget_show (state->dialog);
Miguel de Icaza's avatar
Miguel de Icaza committed
678
}