cell.c 2.71 KB
Newer Older
1
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
2
#include <gnome.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
3
4
#include "gnumeric.h"
#include "eval.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
5

Arturo Espinosa's avatar
Arturo Espinosa committed
6
7
static void
cell_formula_link (Cell *cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
8
{
Arturo Espinosa's avatar
Arturo Espinosa committed
9
	Sheet *sheet = cell->sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
10

Arturo Espinosa's avatar
Arturo Espinosa committed
11
12
	sheet->formula_cell_list = g_list_prepend (sheet->formula_cell_list, cell);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
13

Arturo Espinosa's avatar
Arturo Espinosa committed
14
15
16
17
18
19
20
static void
cell_formula_unlink (Cell *cell)
{
	Sheet *sheet = cell->sheet;
	
	sheet->formula_cell_list = g_list_remove (sheet->formula_cell_list, cell);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
21

Arturo Espinosa's avatar
Arturo Espinosa committed
22
23
24
25
void
cell_set_formula (Cell *cell, char *text)
{
	char *error_msg = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
26

Arturo Espinosa's avatar
Arturo Espinosa committed
27
28
29
30
31
32
33
34
35
36
37
38
39
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
	
	cell->parsed_node = expr_parse_string (&text [1],
					       cell->col->pos,
					       cell->row->pos,
					       &error_msg);
	if (cell->parsed_node == NULL){
		if (cell->text)
			string_unref_ptr (&cell->text);
		cell->text = string_get (error_msg);
		return;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
40

Arturo Espinosa's avatar
Arturo Espinosa committed
41
42
43
44
	cell_formula_link (cell);
	cell_add_dependencies (cell);
	cell_queue_recalc (cell);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
45

Arturo Espinosa's avatar
Arturo Espinosa committed
46
47
48
49
50
51
52
53
54
void
cell_set_text (Cell *cell, char *text)
{
	GdkFont *font;
	char    *rendered_text;
	GList   *deps;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
55

Arturo Espinosa's avatar
Arturo Espinosa committed
56
57
58
59
60
61
62
63
	/* The value entered */
	if (cell->entered_text)
		string_unref_ptr (&cell->entered_text);
	cell->entered_text = string_get (text);
	
	if (cell->parsed_node){
		cell_drop_dependencies (cell);
		expr_tree_unref (cell->parsed_node);
Arturo Espinosa's avatar
Arturo Espinosa committed
64
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
65
66
67
68
69
70
71
72
73
74
	
	if (text [0] == '='){
		cell_set_formula (cell, text); 
	} else {
		Value *v = g_new (Value, 1);
		int is_text = 0, is_float = 0;
		char *p;
		
		if (cell->text)
			string_unref_ptr (&cell->text);
Arturo Espinosa's avatar
Arturo Espinosa committed
75

Arturo Espinosa's avatar
Arturo Espinosa committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
		cell->text = string_get (text);

		for (p = text; *p && !is_text; p++){
			switch (*p){
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				break;
				
			case 'E': case 'e': case '+': case ':': case '.':
				is_float = 1;
				break;
			default:
				is_text = 1;
			}
		}
		if (is_text){
			v->type = VALUE_STRING;
			v->v.str = string_get (text);
		} else {
			if (is_float){
				v->type = VALUE_FLOAT;
				float_get_from_range (text, text+strlen(text),
						      &v->v.v_float);
			} else {
				v->type = VALUE_INTEGER;
				int_get_from_range (text, text+strlen (text),
						    &v->v.v_int);
			}
			/* FIXME: */
			/* In this case we need to format the text */
		}
		cell->value = v;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
109

Arturo Espinosa's avatar
Arturo Espinosa committed
110
111
112
113
114
115
	/* Queue all of the dependencies for this cell */
	deps = cell_get_dependencies (cell->sheet,
				      cell->col->pos,
				      cell->row->pos);
	if (deps)
		cell_queue_recalc_list (deps);
Arturo Espinosa's avatar
Arturo Espinosa committed
116

Arturo Espinosa's avatar
Arturo Espinosa committed
117
118
119
	
	/* Finish setting the values for this cell */
	cell->flags = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
120

Arturo Espinosa's avatar
Arturo Espinosa committed
121
	rendered_text = CELL_TEXT_GET (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
122
123
124
	
	font = cell->style->font->font;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
125
126
127
	cell->width = cell->col->margin_a + cell->col->margin_b + 
		gdk_text_width (font, rendered_text, strlen (rendered_text));
	cell->height = font->ascent + font->descent;
Arturo Espinosa's avatar
Arturo Espinosa committed
128
}