cell.c 7.12 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"
5
#include "format.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
6

7 8 9 10 11 12 13 14 15
void
cell_formula_changed (Cell *cell)
{
	g_return_if_fail (cell != NULL);
	
	sheet_cell_formula_link (cell);
	cell_queue_recalc (cell);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
16 17 18 19
void
cell_set_formula (Cell *cell, char *text)
{
	char *error_msg = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
20

Arturo Espinosa's avatar
Arturo Espinosa committed
21 22
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
23

Arturo Espinosa's avatar
Arturo Espinosa committed
24 25 26 27 28 29 30 31 32 33
	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;
	}
34
	cell_formula_changed (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
35
}
Arturo Espinosa's avatar
Arturo Espinosa committed
36

Miguel de Icaza's avatar
Miguel de Icaza committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * cell_set_alignment:
 *
 * @cell: the cell to change the alignment of
 * @halign: the horizontal alignemnt
 * @valign: the vertical alignemnt
 * @orient: the text orientation
 *
 * This routine changes the alignment of a cell to those specified.
 */
void
cell_set_alignment (Cell *cell, int halign, int valign, int orient)
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->style != NULL);

	if ((cell->style->halign == halign) &&
	    (cell->style->halign == valign) &&
	    (cell->style->orientation == orient))
		return;

	cell->style->halign = halign;
	cell->style->valign = valign;
	cell->style->orientation = orient;

	cell_queue_redraw (cell);
}

65 66 67 68 69 70 71 72 73 74 75 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
void
cell_set_font_from_style (Cell *cell, StyleFont *style_font)
{
	GdkFont *font;
	int height;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (style_font != NULL);

	style_font_unref (cell->style->font);
	style_font_ref (style_font);
	
	cell->style->font = style_font;

	font = style_font->font;
	
	height = font->ascent + font->descent;
	
	if (!cell->row->hard_size)
		sheet_row_set_internal_height (cell->sheet, cell->row, height);
	
	cell_queue_redraw (cell);
}

void
cell_set_font (Cell *cell, char *font_name)
{
	StyleFont *style_font;

	g_return_if_fail (cell != NULL);
	g_return_if_fail (font_name != NULL);

	style_font = style_font_new (font_name, 1);

	if (style_font)
		cell_set_font_from_style (cell, style_font);
}

Miguel de Icaza's avatar
Miguel de Icaza committed
103 104 105 106 107 108
/*
 * cell_calc_dimensions
 * @cell:  The cell
 *
 * This routine updates the dimensions of the the rendered text of a cell
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
109
void
110
cell_calc_dimensions (Cell *cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
111 112
{
	char    *rendered_text;
113 114 115
	GdkFont *font;

	g_return_if_fail (cell != NULL);
116 117 118 119 120

	if (cell->text){
		rendered_text = CELL_TEXT_GET (cell);

		font = cell->style->font->font;
121
	
122 123 124 125 126
		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;
	} else
		cell->width = cell->col->margin_a + cell->col->margin_b;
127 128
}

Miguel de Icaza's avatar
Miguel de Icaza committed
129 130 131 132 133 134 135 136
/*
 * cell_set_rendered_text
 * @cell:          the cell we will modify
 * @rendered_text: the text we will display
 *
 * This routine sets the rendered text field of the cell
 * it recomputes the bounding box for the cell as well
 */
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
137 138 139 140 141 142 143 144 145 146 147 148 149
void
cell_set_rendered_text (Cell *cell, char *rendered_text)
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (rendered_text != NULL);
	
	if (cell->text)
		string_unref (cell->text);

	cell->text = string_get (rendered_text);
	cell_calc_dimensions (cell);
}

Miguel de Icaza's avatar
Miguel de Icaza committed
150 151 152 153 154 155
/*
 * cell_render_value
 * @cell: The cell whose value needs to be rendered
 *
 * The value of the cell is formated according to the format style
 */
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
156 157 158 159 160 161 162 163
void
cell_render_value (Cell *cell)
{
	char *str;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->value != NULL);

164
	str = format_value (cell->style->format, cell->value, NULL);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
165 166 167 168
	cell_set_rendered_text (cell, str);
	g_free (str);
}

169 170 171
void
cell_set_text (Cell *cell, char *text)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
172 173 174 175
	GList   *deps;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
176

Arturo Espinosa's avatar
Arturo Espinosa committed
177 178
	/* The value entered */
	if (cell->entered_text)
179 180
		string_unref (cell->entered_text);

Arturo Espinosa's avatar
Arturo Espinosa committed
181 182 183
	cell->entered_text = string_get (text);
	
	if (cell->parsed_node){
184
		sheet_cell_formula_unlink (cell);
185

Arturo Espinosa's avatar
Arturo Espinosa committed
186
		expr_tree_unref (cell->parsed_node);
187
		cell->parsed_node = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
188
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
189
	
Miguel de Icaza's avatar
Miguel de Icaza committed
190
 	if (text [0] == '='){
Arturo Espinosa's avatar
Arturo Espinosa committed
191 192 193 194 195 196 197 198 199 200 201
		cell_set_formula (cell, text); 
	} else {
		Value *v = g_new (Value, 1);
		int is_text = 0, is_float = 0;
		char *p;
		
		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;
202 203 204 205 206

			case '-':
				if (p == text)
					break;
				/* falldown */
Arturo Espinosa's avatar
Arturo Espinosa committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
				
			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);
			}
		}
		cell->value = v;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
230 231
		
		cell_render_value (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
232
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
233

Arturo Espinosa's avatar
Arturo Espinosa committed
234 235 236 237 238 239
	/* 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
Today:  
Arturo Espinosa committed
240 241

	cell_queue_redraw (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
242
}
Arturo Espinosa's avatar
Arturo Espinosa committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

/*
 * 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);
261 262
	if (new_cell->parsed_node)
		expr_tree_ref   (new_cell->parsed_node);
Arturo Espinosa's avatar
Arturo Espinosa committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	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);
}
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
285 286 287 288

void
cell_queue_redraw (Cell *cell)
{
289 290
	g_return_if_fail (cell != NULL);
	
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
291 292 293 294 295 296 297 298
	sheet_redraw_cell_region (cell->sheet,
				  cell->col->pos, cell->row->pos,
				  cell->col->pos, cell->row->pos);
}

void
cell_set_format (Cell *cell, char *format)
{
299 300 301
	g_return_if_fail (cell != NULL);
	g_return_if_fail (format != NULL);
	
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
302 303 304 305 306 307 308 309 310
	if (strcmp (format, cell->style->format->format) == 0)
		return;

	/* Change the format */
	style_format_unref (cell->style->format);
	cell->style->format = style_format_new (format);

	/* re-render the cell text */
	cell_render_value (cell);
311
	cell_queue_redraw (cell);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
312
}
313

Arturo Espinosa's avatar
Arturo Espinosa committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void
cell_formula_relocate (Cell *cell, int target_col, int target_row)
{
	char *text, *formula;
	
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->entered_text);
	
	string_unref (cell->entered_text);
	
	text = expr_decode_tree (cell->parsed_node, target_col, target_row);
	
	formula = g_copy_strings ("=", text, NULL);
	cell->entered_text = string_get (formula);

Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
329 330
	cell_set_formula (cell, formula);
	
Arturo Espinosa's avatar
Arturo Espinosa committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	g_free (formula);
	g_free (text);
	
	cell_formula_changed (cell);
}

/*
 * This routine drops the formula and just keeps the value
 */
void
cell_make_value (Cell *cell)
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->parsed_node != NULL);

	expr_tree_unref (cell->parsed_node);
	cell->parsed_node = NULL;
	string_unref (cell->entered_text);
	cell->entered_text = string_ref (cell->text);
}