cell.c 3.34 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 8 9
void
cell_set_formula (Cell *cell, char *text)
{
	char *error_msg = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
10

Arturo Espinosa's avatar
Arturo Espinosa committed
11 12
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
13

Arturo Espinosa's avatar
Arturo Espinosa committed
14 15 16 17 18 19 20 21 22 23
	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
24

25
	sheet_cell_formula_link (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
26 27 28
	cell_add_dependencies (cell);
	cell_queue_recalc (cell);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
29

Arturo Espinosa's avatar
Arturo Espinosa committed
30
void
31
cell_calc_dimensions (Cell *cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
32 33
{
	char    *rendered_text;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	GdkFont *font;

	g_return_if_fail (cell != NULL);
	
	rendered_text = CELL_TEXT_GET (cell);
	
	font = cell->style->font->font;
	
	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;
}

void
cell_set_text (Cell *cell, char *text)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
50 51 52 53
	GList   *deps;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
54

Arturo Espinosa's avatar
Arturo Espinosa committed
55 56
	/* The value entered */
	if (cell->entered_text)
57 58
		string_unref (cell->entered_text);

Arturo Espinosa's avatar
Arturo Espinosa committed
59 60 61 62
	cell->entered_text = string_get (text);
	
	if (cell->parsed_node){
		cell_drop_dependencies (cell);
63
		sheet_cell_formula_unlink (cell);
64

Arturo Espinosa's avatar
Arturo Espinosa committed
65
		expr_tree_unref (cell->parsed_node);
66
		cell->parsed_node = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
67
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
68 69 70 71 72 73 74 75 76
	
	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)
77
			string_unref (cell->text);
Arturo Espinosa's avatar
Arturo Espinosa committed
78

Arturo Espinosa's avatar
Arturo Espinosa committed
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 109 110
		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;
111
		cell_calc_dimensions (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
112
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
113

Arturo Espinosa's avatar
Arturo Espinosa committed
114 115 116 117 118 119
	/* 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
120

Arturo Espinosa's avatar
Arturo Espinosa committed
121 122 123
	
	/* Finish setting the values for this cell */
	cell->flags = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
124
}
Arturo Espinosa's avatar
Arturo Espinosa committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

/*
 * Makes a copy of a Cell
 */
Cell *
cell_copy (Cell *cell)
{
	Cell *new_cell;

	g_return_val_if_fail (cell != NULL, NULL);

	new_cell = g_new (Cell, 1);

	/* bitmap copy first */
	*new_cell = *cell;

	/* now copy propertly the rest */
	string_ref      (new_cell->entered_text);
143 144
	if (new_cell->parsed_node)
		expr_tree_ref   (new_cell->parsed_node);
Arturo Espinosa's avatar
Arturo Espinosa committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	string_ref      (new_cell->text);
	
	new_cell->style = style_duplicate (new_cell->style);
	new_cell->value = value_duplicate (new_cell->value);

	return new_cell;
}

void
cell_destroy (Cell *cell)
{
	g_return_if_fail (cell != NULL);

	if (cell->parsed_node){
		expr_tree_unref (cell->parsed_node);
	}

	string_unref    (cell->entered_text);
	string_unref    (cell->text);
	style_destroy   (cell->style);
	value_release   (cell->value);
}