dialog-define-names.c 7.23 KB
Newer Older
Michael Meeks's avatar
Michael Meeks committed
1 2
/**
 * dialog-summary.c:  Implements the summary info stuff
Miguel de Icaza's avatar
Miguel de Icaza committed
3 4
 *
 * Author:
Michael Meeks's avatar
Michael Meeks committed
5
 *        Michael Meeks <michael@imaginator.com>
Miguel de Icaza's avatar
Miguel de Icaza committed
6
 *
Michael Meeks's avatar
Michael Meeks committed
7
 **/
Miguel de Icaza's avatar
Miguel de Icaza committed
8 9
#include <config.h>
#include <gnome.h>
Michael Meeks's avatar
Michael Meeks committed
10
#include <glade/glade.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
11
#include "dialogs.h"
12
#include "expr.h"
13
#include "expr-name.h"
14
#include "workbook.h"
15
#include "workbook-edit.h"
16
#include "gnumeric-util.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
17

18 19
#define LIST_KEY "name_list_data"

Michael Meeks's avatar
Michael Meeks committed
20 21
typedef struct {
	GladeXML  *gui;
22
	GtkWidget *dialog;
23 24 25
	GtkList   *list;
	GtkEntry  *name;
	GtkEntry  *value;
Michael Meeks's avatar
Michael Meeks committed
26 27
	GList     *expr_names;
	gint       selected;
Jody Goldberg's avatar
Jody Goldberg committed
28

29 30 31 32 33
	GtkWidget *ok_button;
	GtkWidget *add_button;
	GtkWidget *close_button;
	GtkWidget *delete_button;

Jody Goldberg's avatar
Jody Goldberg committed
34
	Workbook  *wb;
35
} NameGuruState;
Michael Meeks's avatar
Michael Meeks committed
36

37
static void
38
update_edit (NameGuruState *state)
39 40
{
	gint          i = state->selected;
41
	NamedExpression     *expr_name;
42 43
	Sheet        *sheet;
	char         *txt;
44

45
	sheet = state->wb->current_sheet;
46 47 48 49 50 51 52
	g_return_if_fail (sheet != NULL);

	expr_name = g_list_nth (state->expr_names, i)->data;
	if (expr_name->name && expr_name->name->str)
		gtk_entry_set_text (state->name, expr_name->name->str);
	else
		gtk_entry_set_text (state->name, "");
53

54 55 56 57 58
	txt = expr_name_value (expr_name);
	gtk_entry_set_text (state->value, txt);
	g_free (txt);
}

Michael Meeks's avatar
Michael Meeks committed
59
static void
60
select_name (GtkWidget *w, NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
61
{
62 63 64
	guint     i    = 0;
	GList    *sel  = GTK_LIST(w)->selection;
	GList    *p    = state->expr_names;
65
	NamedExpression *name;
66 67 68 69 70 71 72 73

	if (sel == NULL)
		return;

	g_return_if_fail (sel->data != NULL);

	name = gtk_object_get_data (GTK_OBJECT (sel->data), LIST_KEY);

Michael Meeks's avatar
Michael Meeks committed
74
	while (p) {
75
		if (p->data == name) {
Michael Meeks's avatar
Michael Meeks committed
76
			state->selected = i;
77
			update_edit (state);
Michael Meeks's avatar
Michael Meeks committed
78 79 80 81 82 83 84 85
			return;
		}
		i++;
		p = g_list_next (p);
	}
}

static void
86
fill_list (NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
87 88
{
	GList *names;
Jody Goldberg's avatar
Jody Goldberg committed
89
	GtkContainer *list;
Michael Meeks's avatar
Michael Meeks committed
90

91 92
	g_return_if_fail (state != NULL);
	g_return_if_fail (state->list != NULL);
Michael Meeks's avatar
Michael Meeks committed
93

Jody Goldberg's avatar
Jody Goldberg committed
94 95 96
	list = GTK_CONTAINER (state->list);
	g_return_if_fail (list != NULL);

97
	state->selected   = -1;
Michael Meeks's avatar
Michael Meeks committed
98

99
	/* FIXME: scoping issues here */
Jody Goldberg's avatar
Jody Goldberg committed
100
	names = state->expr_names = expr_name_list (state->wb, NULL, FALSE);
101

Jody Goldberg's avatar
Jody Goldberg committed
102
	for (; names != NULL ; names = g_list_next (names)) {
103
		NamedExpression *expr_name = names->data;
Michael Meeks's avatar
Michael Meeks committed
104
		GtkWidget *li = gtk_list_item_new_with_label (expr_name->name->str);
105
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
Jody Goldberg's avatar
Jody Goldberg committed
106
		gtk_container_add (list, li);
Michael Meeks's avatar
Michael Meeks committed
107
	}
Jody Goldberg's avatar
Jody Goldberg committed
108
	gtk_widget_show_all (GTK_WIDGET (state->list));
109 110 111
}

static void
112
clear_state (NameGuruState *state)
113 114 115 116 117 118 119 120
{
	if (state->expr_names)
		g_list_free (state->expr_names);
	state->expr_names = NULL;
	state->selected = -1;
}

static void
121
empty_list (NameGuruState *state)
122 123 124 125
{
	if (state->list)
		gtk_list_clear_items (state->list, 0, -1);
	state->list = NULL;
126
	clear_state (state);
Michael Meeks's avatar
Michael Meeks committed
127 128 129
}

static void
130
remove_name (GtkWidget *ignored, NameGuruState *state)
Michael Meeks's avatar
Michael Meeks committed
131
{
Jody Goldberg's avatar
Jody Goldberg committed
132
	gint i;
133 134 135 136
	g_return_if_fail (state != NULL);

	i = state->selected;
	if (i >= 0) {
Jody Goldberg's avatar
Jody Goldberg committed
137
		GList *na = g_list_nth (state->expr_names, i);
138

Jody Goldberg's avatar
Jody Goldberg committed
139 140
		g_return_if_fail (na != NULL);
		g_return_if_fail (na->data != NULL);
141 142 143 144 145 146

		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
147
	}
148 149 150
}

static gboolean
151
grab_text_ok (NameGuruState *state, gboolean update_list)
152
{
153
	NamedExpression     *expr_name;
Jody Goldberg's avatar
Jody Goldberg committed
154 155 156
	ExprTree     *expr;
	gchar        *name, *value, *error;
	ParsePos      pos, *pp;
157 158 159 160 161 162 163 164

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

Jody Goldberg's avatar
Jody Goldberg committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/* FIXME : Need to be able to control scope.
	 *        1) sheet
	 *        2) workbook
	 *        3) global
	 *
	 * default to workbook for now.
	 */
	pp = parse_pos_init (&pos, state->wb, NULL, 0, 0);

	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);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

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

	if (update_list) {
		empty_list (state);
		fill_list  (state);
	}

	return TRUE;
}

static void
207
cb_name_guru_clicked (GtkWidget *button, NameGuruState *state)
208
{
209 210 211 212 213 214 215 216 217 218 219 220 221
	if (state->dialog == NULL)
		return;

	if (button == state->delete_button) {
		remove_name (NULL, state);
		return;
	}

	if (button == state->add_button || button == state->ok_button)
		grab_text_ok (state, TRUE);

	if (button == state->close_button || button == state->ok_button)
		gtk_widget_destroy (state->dialog);
222 223
}

224 225
static GtkWidget *
name_guru_init_button (NameGuruState *state, char const *name)
226
{
227 228 229 230 231
	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;
232 233
}

234 235
static gboolean
cb_name_guru_destroy (GtkObject *w, NameGuruState *state)
236
{
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	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;
256 257
}

258 259
static gboolean
name_guru_init (NameGuruState *state)
260
{
261 262 263 264 265 266 267 268 269 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 297 298 299
	state->gui = gnumeric_glade_xml_new (workbook_command_context_gui (state->wb),
					     "names.glade");
        if (state->gui == NULL)
                return TRUE;

	state->dialog = glade_xml_get_widget (state->gui, "NamesDialog");
	state->name  = GTK_ENTRY (glade_xml_get_widget (state->gui, "name"));
	state->value = GTK_ENTRY (glade_xml_get_widget (state->gui, "value"));
	state->list  = GTK_LIST  (glade_xml_get_widget (state->gui, "name_list"));
	state->expr_names = NULL;
	state->selected   = -1;

	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");

 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_EDITABLE(state->name));
 	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
				  GTK_EDITABLE (state->value));

	fill_list (state);

	gtk_signal_connect (GTK_OBJECT (state->list), "selection_changed",
			    GTK_SIGNAL_FUNC (select_name), &state);

	gtk_signal_connect (GTK_OBJECT (state->dialog), "destroy",
			    GTK_SIGNAL_FUNC (cb_name_guru_destroy),
			    state);

	gtk_window_set_transient_for (GTK_WINDOW (state->dialog),
				      GTK_WINDOW (state->wb->toplevel));

	workbook_set_entry (state->wb, state->value);
	workbook_edit_attach_guru (state->wb, state->dialog);
	workbook_edit_select_absolute (state->wb);

	return FALSE;
Michael Meeks's avatar
Michael Meeks committed
300 301
}

Miguel de Icaza's avatar
Miguel de Icaza committed
302 303 304
void
dialog_define_names (Workbook *wb)
{
305
	NameGuruState *state;
Michael Meeks's avatar
Michael Meeks committed
306 307 308

	g_return_if_fail (wb != NULL);

309 310 311 312
	state = g_new (NameGuruState, 1);
	state->wb  = wb;
	if (name_guru_init (state)) {
		g_free (state);
Michael Meeks's avatar
Michael Meeks committed
313 314 315
		return;
	}

316
	gtk_widget_show (state->dialog);
Miguel de Icaza's avatar
Miguel de Icaza committed
317
}