dialog-define-names.c 6.3 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 12 13 14
#include "gnumeric.h"
#include "gnumeric-util.h"
#include "dialogs.h"

15 16
#define LIST_KEY "name_list_data"

Michael Meeks's avatar
Michael Meeks committed
17 18 19 20
typedef struct {
	Workbook  *wb;
	GladeXML  *gui;
	GtkWidget *dia;
21 22 23
	GtkList   *list;
	GtkEntry  *name;
	GtkEntry  *value;
Michael Meeks's avatar
Michael Meeks committed
24 25 26 27
	GList     *expr_names;
	gint       selected;
} state_t;

28 29 30 31 32 33 34 35 36
static void
update_edit (state_t *state)
{
	gint          i = state->selected;
	ExprName     *expr_name;
	Sheet        *sheet;
	EvalPosition  ep;
	char         *txt;
	
37
	sheet = state->wb->current_sheet;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	g_return_if_fail (sheet != NULL);

	eval_pos_init (&ep, sheet, 0, 0);

	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, "");
	
	txt = expr_name_value (expr_name);
	gtk_entry_set_text (state->value, txt);
	g_free (txt);
}

Michael Meeks's avatar
Michael Meeks committed
53 54 55
static void
select_name (GtkWidget *w, state_t *state)
{
56 57 58 59 60 61 62 63 64 65 66 67
	guint     i    = 0;
	GList    *sel  = GTK_LIST(w)->selection;
	GList    *p    = state->expr_names;
	ExprName *name;

	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
68
	while (p) {
69
		if (p->data == name) {
Michael Meeks's avatar
Michael Meeks committed
70
			state->selected = i;
71
			update_edit (state);
Michael Meeks's avatar
Michael Meeks committed
72 73 74 75 76 77 78 79 80 81 82
			return;
		}
		i++;
		p = g_list_next (p);
	}
}

static void
fill_list (state_t *state)
{
	GList *names;
83
	GList *items = NULL;
Michael Meeks's avatar
Michael Meeks committed
84

85 86
	g_return_if_fail (state != NULL);
	g_return_if_fail (state->list != NULL);
Michael Meeks's avatar
Michael Meeks committed
87

88
	state->selected   = -1;
Michael Meeks's avatar
Michael Meeks committed
89

90 91
	/* FIXME: scoping issues here */
	state->expr_names = names = expr_name_list (state->wb, NULL, FALSE);
Michael Meeks's avatar
Michael Meeks committed
92 93 94 95
	
	while (names) {
		ExprName *expr_name = names->data;
		GtkWidget *li = gtk_list_item_new_with_label (expr_name->name->str);
96 97 98
		gtk_object_set_data (GTK_OBJECT (li), LIST_KEY, expr_name);
		gtk_widget_show (GTK_WIDGET (li));
		items = g_list_append (items, li);
Michael Meeks's avatar
Michael Meeks committed
99 100
		names = g_list_next (names);
	}
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	gtk_list_append_items (state->list, items);
}

static void
destroy_state (state_t *state)
{
	if (state->expr_names)
		g_list_free (state->expr_names);
	state->expr_names = NULL;
	state->selected = -1;
}

static void
empty_list (state_t *state)
{
	if (state->list)
		gtk_list_clear_items (state->list, 0, -1);
	state->list = NULL;
	destroy_state (state);
Michael Meeks's avatar
Michael Meeks committed
120 121 122 123 124
}

static void
remove_name (GtkWidget *widget, state_t *state)
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*	gint i;
	g_return_if_fail (state != NULL);
	g_return_if_fail (widget != NULL);

	i = state->selected;
	if (i >= 0) {
		GList *na           = g_list_nth (state->expr_names, i);
		GList *it           = g_list_nth (state->list_items, i);
		GList *l;

		g_return_if_fail (it != NULL && na != NULL);
		g_return_if_fail (it->data != NULL && na->data != NULL);

		expr_name_remove (na->data);
		state->expr_names = g_list_remove (state->expr_names, na->data);

		l = g_list_append (NULL, it->data);
		gtk_list_remove_items (state->list, l);
		g_list_free (l);
		state->list_items = g_list_remove (state->list_items, it->data);

		gtk_entry_set_text (state->name, "");
		gtk_entry_set_text (state->value, "");
		}*/
	g_warning ("Unimplemented, need to sweep sheets to check for usage");
}

static gboolean
grab_text_ok (state_t *state, gboolean update_list)
{
	gchar        *name;
	gchar        *value;
	ExprName     *expr_name;
	char         *error;

	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;
Michael Meeks's avatar
Michael Meeks committed
167
	
168 169
	/* FIXME: we need to be able to select names scope ideally */
	expr_name = expr_name_lookup (state->wb, NULL, name);
170 171 172
	if (expr_name)
		expr_name_remove (expr_name);

173 174
	/* FIXME: and here */
	expr_name = expr_name_create (state->wb, NULL, name,
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
				      value, &error);

	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
destroy_dialog (state_t *state)
{
	if (state->dia)
		gnome_dialog_close (GNOME_DIALOG (state->dia));
	state->dia   = NULL;
	state->list  = NULL;
	state->name  = NULL;
	state->value = NULL;
}

static void
add_name (GtkWidget *widget, state_t *state)
{
	grab_text_ok (state, TRUE);
}

static void
close_name (GtkWidget *widget, state_t *state)
{
	destroy_state  (state);
	destroy_dialog (state);
}

static void
ok_name (GtkWidget *widget, state_t *state)
{
	if (grab_text_ok (state, FALSE))
		close_name (widget, state);
Michael Meeks's avatar
Michael Meeks committed
222 223
}

Miguel de Icaza's avatar
Miguel de Icaza committed
224 225 226
void
dialog_define_names (Workbook *wb)
{
Michael Meeks's avatar
Michael Meeks committed
227 228 229 230 231 232 233 234 235 236 237 238 239
	gint       v;
	state_t    state;
	GtkWidget *w;

	g_return_if_fail (wb != NULL);

	state.wb  = wb;
	state.gui = glade_xml_new (GNUMERIC_GLADEDIR "/names.glade", NULL);
	if (!state.gui) {
		printf ("Could not find names.glade\n");
		return;
	}
	
240 241 242 243 244
	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;
Michael Meeks's avatar
Michael Meeks committed
245

246 247 248
	w = glade_xml_get_widget (state.gui, "ok");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (ok_name), &state);
Michael Meeks's avatar
Michael Meeks committed
249

250 251 252 253 254 255 256 257
	w = glade_xml_get_widget (state.gui, "close");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (close_name), &state);

	w = glade_xml_get_widget (state.gui, "add");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (add_name), &state);
	
Michael Meeks's avatar
Michael Meeks committed
258 259 260
	w = glade_xml_get_widget (state.gui, "delete");
	gtk_signal_connect (GTK_OBJECT (w), "clicked",
			    GTK_SIGNAL_FUNC (remove_name), &state);
Miguel de Icaza's avatar
Miguel de Icaza committed
261
	
Michael Meeks's avatar
Michael Meeks committed
262 263 264 265 266
	state.dia = glade_xml_get_widget (state.gui, "NamesDialog");
	if (!state.dia) {
		printf ("Corrupt file names.glade\n");
		return;
	}
Jody Goldberg's avatar
Jody Goldberg committed
267 268 269 270
 	gnome_dialog_editable_enters(GNOME_DIALOG(state.dia), 
				     GTK_EDITABLE(state.name));
 	gnome_dialog_editable_enters(GNOME_DIALOG(state.dia), 
				     GTK_EDITABLE(state.value));
Michael Meeks's avatar
Michael Meeks committed
271

272 273 274 275
	fill_list (&state);

	gtk_signal_connect (GTK_OBJECT (state.list), "selection_changed",
			    GTK_SIGNAL_FUNC (select_name), &state);
Miguel de Icaza's avatar
Miguel de Icaza committed
276

277
	v = gnumeric_dialog_run (wb, GNOME_DIALOG (state.dia));
278 279 280 281 282
	
	if (v == -1)
		destroy_state (&state);
	else
		destroy_dialog (&state);
Michael Meeks's avatar
Michael Meeks committed
283 284

	gtk_object_unref (GTK_OBJECT (state.gui));
Miguel de Icaza's avatar
Miguel de Icaza committed
285
}