dialog-define-names.c 16.5 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 12
#include <config.h>
#include <gnome.h>
Michael Meeks's avatar
Michael Meeks committed
13
#include <glade/glade.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
14
#include "dialogs.h"
15
#include "expr.h"
16
#include "str.h"
17
#include "expr-name.h"
Jody Goldberg's avatar
Jody Goldberg committed
18
#include "sheet.h"
19
#include "workbook.h"
20
#include "workbook-control.h"
21
#include "workbook-edit.h"
22
#include "gnumeric-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
23
#include "parse-util.h"
24
#include "widgets/gnumeric-expr-entry.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
25

26
#define LIST_KEY "name_list_data"
27
#define DEFINE_NAMES_KEY "define-names-dialog"
28

29 30 31 32 33
typedef enum {
	NAME_GURU_SCOPE_SHEET,
	NAME_GURU_SCOPE_WORKBOOK,
} NameGuruScope;

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

44 45 46 47
	GtkWidget *ok_button;
	GtkWidget *add_button;
	GtkWidget *close_button;
	GtkWidget *delete_button;
48
	GtkWidget *update_button;
49

50
	Sheet	  *sheet;
51 52
	Workbook  *wb;
	WorkbookControlGUI  *wbcg;
53 54

	gboolean updating;
55
} NameGuruState;
Michael Meeks's avatar
Michael Meeks committed
56

57 58
/**
 * name_guru_warned_if_used:
59 60
 * @state:
 *
61 62
 * If the expresion that is about to be deleted is beeing used,
 * warn the user about it. Ask if we should procede or not
63
 *
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 * 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:
79 80 81 82
 * @state:
 * @scope:
 *
 * Change the scope of state->cur_name. Ask the user if we want to procede with the
83 84 85 86 87 88 89 90 91 92 93 94 95
 * 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;
96

97 98 99 100 101 102 103 104
	/* 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;
105

106 107
		expr_name_wb2sheet (expression, state->sheet);
	}
108

109 110
	return TRUE;
}
111

112 113 114
static void
cb_scope_changed (GtkEntry *entry, NameGuruState *state)
{
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	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");
130

131 132 133 134 135
}

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

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

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

156 157 158 159 160

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

/**
 * name_guru_set_expr:
161
 * @state:
162
 * @expr_name: Expression to set in the entries, NULL to clear entries
163
 *
164 165 166 167 168 169 170
 * 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);

171
	/* Don't recur.  */
172
	state->updating = TRUE;
173

174
	if (expr_name) {
175 176
		gchar *txt;

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

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

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

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

}

/**
 * name_guru_clear_selection:
199 200
 * @state:
 *
201 202 203 204 205 206 207 208 209 210 211 212 213 214
 * 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:
215
 * @name:
216
 * @state:
217
 *
218
 * Given a name, it searches for it inside the list of Names
219
 *
220 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
 * 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:
248 249 250
 * @state:
 * @update_entries:
 *
251 252 253 254 255 256 257 258 259
 * 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
260 261
	char const *expr_text;
	char const *name;
262 263 264 265 266

	g_return_if_fail (state->list != NULL);

	if (state->updating)
		return;
267

268
	name  = gtk_entry_get_text (state->name);
269
	expr_text = gtk_entry_get_text (GTK_ENTRY (state->expr_text));
270 271 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

	/** 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:
297 298 299
 * @dummy:
 * @state:
 *
300 301 302 303 304 305 306 307 308
 **/
static void
name_guru_update_sensitivity_cb (GtkWidget *dummy, NameGuruState *state)
{
	name_guru_update_sensitivity (state, FALSE);
}

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

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

	g_return_if_fail (sel->data != NULL);

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

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

331 332
	state->cur_name = expr_name;

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

Michael Meeks's avatar
Michael Meeks committed
336 337
}

338

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

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

348
	state->cur_name = NULL;
Michael Meeks's avatar
Michael Meeks committed
349

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

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

372 373
/**
 * name_guru_scope_get:
374 375
 * @state:
 *
376
 * Get the selected Scope from the combo box
377 378
 *
 * Return Value:
379 380 381 382 383 384 385 386 387
 **/
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);
388

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


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

408
	if (state->cur_name != NULL) {
409 410 411 412 413
		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;
414

415 416 417 418
		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
419
	}
420

421 422
}

423 424 425

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

	g_return_val_if_fail (state != NULL, FALSE);

444
	expr_text = gtk_entry_get_text (GTK_ENTRY (state->expr_text));
445 446 447 448
	name  = gtk_entry_get_text (state->name);

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

450 451 452 453
	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.
454 455
			     state->sheet->edit_pos.col,
			     state->sheet->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
456
         */
Jody Goldberg's avatar
Jody Goldberg committed
457 458 459

	expr_name = expr_name_lookup (pp, name);

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

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

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

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

	return TRUE;
}

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

502
	wbcg_set_entry (state->wbcg, NULL);
503

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

509 510 511 512 513
	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
514
			return;
515 516
		}
	}
517

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

523 524
}

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

	g_return_val_if_fail (tmp != NULL, NULL);
531

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

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

544
	wbcg_edit_detach_guru (state->wbcg);
545 546 547 548 549 550

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

551
	wbcg_edit_finish (state->wbcg, FALSE);
552 553 554

	state->dialog = NULL;

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

558
	g_free (state);
559

560
	return FALSE;
561 562
}

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

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

	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");
585 586 587
        if (state->gui == NULL)
                return TRUE;

588
	state->dialog = glade_xml_get_widget (state->gui, "NameGuru");
589
	table2 = GTK_TABLE (glade_xml_get_widget (state->gui, "table2"));
590
	state->name  = GTK_ENTRY (glade_xml_get_widget (state->gui, "name"));
Jody Goldberg's avatar
Jody Goldberg committed
591
	state->expr_text = GNUMERIC_EXPR_ENTRY (gnumeric_expr_entry_new (state->wbcg));
592 593 594 595 596
	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));
597
	state->scope = GTK_COMBO (glade_xml_get_widget (state->gui, "scope_combo"));
598 599
	state->list  = GTK_LIST  (glade_xml_get_widget (state->gui, "name_list"));
	state->expr_names = NULL;
600
	state->cur_name   = NULL;
601
	state->updating   = FALSE;
602 603 604 605 606

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

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

	gtk_signal_connect (GTK_OBJECT (state->list), "selection_changed",
615
			    GTK_SIGNAL_FUNC (cb_name_guru_select_name), state);
616 617
	gtk_signal_connect (GTK_OBJECT (state->dialog), "set-focus",
			    GTK_SIGNAL_FUNC (cb_name_guru_set_focus), state);
618
	gtk_signal_connect (GTK_OBJECT (state->dialog), "destroy",
Jody Goldberg's avatar
Jody Goldberg committed
619
			    GTK_SIGNAL_FUNC (cb_name_guru_destroy), state);
620 621
	gtk_signal_connect (GTK_OBJECT (state->name), "changed",
			    GTK_SIGNAL_FUNC (name_guru_update_sensitivity_cb), state);
622 623 624
	/* 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.
625
	 */
Jody Goldberg's avatar
Jody Goldberg committed
626
	gtk_signal_connect_after (GTK_OBJECT (state->expr_text), "changed",
627
				  GTK_SIGNAL_FUNC (name_guru_update_sensitivity_cb), state);
628

Jody Goldberg's avatar
Jody Goldberg committed
629 630 631
 	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
632
				  GTK_EDITABLE (state->expr_text));
633 634
	gnumeric_combo_enters (GTK_WINDOW (state->dialog),
			       state->scope);
635 636
	gnumeric_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
			       DEFINE_NAMES_KEY);
637

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

	return FALSE;
Michael Meeks's avatar
Michael Meeks committed
643 644
}

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

656
	g_return_if_fail (wbcg != NULL);
Michael Meeks's avatar
Michael Meeks committed
657

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

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

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

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