cell.c 19.9 KB
Newer Older
1
2
3
4
/*
 * cell.c: Cell management of the Gnumeric spreadsheet.
 *
 * Author:
5
 *    Miguel de Icaza 1998, 1999 (miguel@kernel.org)
6
 */
7
#include "config.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
8
#include "gnumeric.h"
9
#include "cell.h"
10
#include "cellspan.h"
11
12
13
14
15
16
#include "cell-comment.h"
#include "expr.h"
#include "eval.h"
#include "sheet.h"
#include "value.h"
#include "rendered-value.h"
17
#include "gnumeric-util.h"
18
#include "parse-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
19
#include "format.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
20

21
22
23
24
25
26
/**
 * cell_dirty : Mark the sheet containing the cell as being dirty.
 * @cell : the dirty cell.
 *
 * INTERNAL.
 */
27
static inline void
28
cell_dirty (Cell *cell)
29
{
30
	Sheet *sheet = cell->sheet;
31
32
33

	/* Cells from the clipboard do not have a sheet attached */
	if (sheet)
34
		sheet_set_dirty(sheet, TRUE);
35
36
}

37
38
39
40
/**
 * cell_formula_changed : Registers the expression with the sheet and
 *     optionally queues a recalc.
 * @cell : the dirty cell.
41
 * @queue_recalc: also queue a recalc for the cell.
42
43
44
45
 *
 * INTERNAL.
 */
static void
46
cell_formula_changed (Cell *cell, gboolean queue_recalc)
47
{
48
	sheet_cell_expr_link (cell);
49
	if (queue_recalc)
Jody Goldberg's avatar
Jody Goldberg committed
50
		eval_queue_cell (cell);
51
}
Morten Welinder's avatar
Morten Welinder committed
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * cell_cleanout :
 *      Empty a cell's
 *      	- value.
 *      	- rendered_value.
 *      	- entered_text/expression.
 *      	- optional format.
 *     
 *      Clears the flags to
 *      	- not queued for recalc.
 *      	- has no expression.
 *     
 *      Does NOT change
 *      	- Comments.
 *      	- Spans.
 *      	- unqueue a previously queued recalc.
 *      	- Mark sheet as dirty.
 */
Jody Goldberg's avatar
typo.    
Jody Goldberg committed
71
static void
72
73
cell_cleanout (Cell *cell)
{
74
75
	/* A cell can have either an expression or entered text */
	if (cell_has_expr (cell)) {
76
		/* Clipboard cells, e.g., are not attached to a sheet.  */
77
78
		if (cell_expr_is_linked (cell))
			sheet_cell_expr_unlink (cell);
79
80
81
82
83
		expr_tree_unref (cell->u.expression);
		cell->u.expression = NULL;
	} else if (cell->u.entered_text) {
		string_unref (cell->u.entered_text);
		cell->u.entered_text = NULL;
84
85
86
87
88
89
	}

	if (cell->value) {
		value_release (cell->value);
		cell->value = NULL;
	}
90
91
92
93
94
95
96
	if (cell->rendered_value) {
		rendered_value_destroy (cell->rendered_value);
		cell->rendered_value = NULL;
	}
	if (cell->format) {
		style_format_unref (cell->format);
		cell->format = NULL;
97
	}
98
99

	cell->cell_flags &= ~(CELL_HAS_EXPRESSION|CELL_QUEUED_FOR_RECALC);
100
101
}

102
103
104
105
106
/**
 * cell_copy:
 * @cell: existing cell to duplicate
 *
 * Makes a copy of a Cell.
Michael Meeks's avatar
Michael Meeks committed
107
 *
108
 * Returns a copy of the cell.
Michael Meeks's avatar
Michael Meeks committed
109
 */
110
Cell *
111
cell_copy (Cell const *cell)
Michael Meeks's avatar
Michael Meeks committed
112
{
113
	Cell *new_cell;
Michael Meeks's avatar
Michael Meeks committed
114

115
	g_return_val_if_fail (cell != NULL, NULL);
116

117
	new_cell = g_new (Cell, 1);
118

119
120
	/* bitmap copy first */
	*new_cell = *cell;
121

122
123
124
	/* The new cell is not linked into any of the major management structures */
	new_cell->sheet = NULL;
	new_cell->cell_flags &= ~(CELL_QUEUED_FOR_RECALC|CELL_IN_SHEET_LIST|CELL_IN_EXPR_LIST);
Morten Welinder's avatar
Morten Welinder committed
125

126
127
128
129
130
	/* now copy properly the rest */
	if (cell_has_expr (new_cell))
		expr_tree_ref (new_cell->u.expression);
	else
		string_ref (new_cell->u.entered_text);
131

132
	new_cell->rendered_value = NULL;
133

134
135
136
	new_cell->value = (new_cell->value)
	    ? value_duplicate (new_cell->value)
	    : value_new_empty ();
Morten Welinder's avatar
Morten Welinder committed
137

138
139
140
	if (cell->format)
		style_format_ref (cell->format);

141
142
143
	if (cell->comment) {
		new_cell->comment = NULL;
		cell_set_comment (new_cell, cell->comment->comment->str);
Morten Welinder's avatar
Morten Welinder committed
144
	}
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
145

146
	return new_cell;
Arturo Espinosa's avatar
Arturo Espinosa committed
147
}
Arturo Espinosa's avatar
Arturo Espinosa committed
148

149
150
151
152
153
154
/**
 * cell_destroy: Frees all resources allocated to the cell's content and marks the
 *     Cell's container as dirty.
 *
 * @cell : The cell to destroy
 */
155
void
156
cell_destroy (Cell *cell)
157
158
159
{
	g_return_if_fail (cell != NULL);

160
161
162
163
	cell_dirty (cell);
	cell_cleanout (cell);
	cell_comment_destroy (cell);
	g_free (cell);
164
}
165

166
167
168
169
170
171
/**
 * cell_content_changed: Queues recalc of all of the cells depends.
 *
 */
void
cell_content_changed (Cell *cell)
172
{
173
	GList   *deps;
174

175
176
	g_return_if_fail (cell != NULL);

177
178
179
	/* Queue all of the dependencies for this cell */
	deps = cell_get_dependencies (cell);
	if (deps)
Jody Goldberg's avatar
Jody Goldberg committed
180
		eval_queue_list (deps, TRUE);
181

182
	sheet_cell_changed (cell);
183
184
}

185
186
/*
 * cell_relocate:
Jody Goldberg's avatar
Jody Goldberg committed
187
188
 * @cell   : The cell that is changing position
 * @rwinfo : An OPTIONAL pointer to allow for bounds checking and relocation
189
190
191
192
193
194
 *
 * This routine is used to move a cell to a different location:
 *
 * Auxiliary items canvas items attached to the cell are moved.
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
195
cell_relocate (Cell *cell, ExprRewriteInfo *rwinfo)
196
{
197
	g_return_if_fail (cell != NULL);
198

199
200
	/* 1. Tag the cell as dirty */
	cell_dirty (cell);
201

202
203
	/* 2. If the cell contains a formula, relocate the formula */
	if (cell_has_expr (cell)) {
Jody Goldberg's avatar
Jody Goldberg committed
204
205
		ExprTree *expr = cell->u.expression;

206
		if (cell_expr_is_linked (cell))
207
			sheet_cell_expr_unlink (cell);
208

209
210
211
212
213
214
215
216
217
		/*
		 * WARNING WARNING WARNING
		 *
		 * This will only work if the new array cell has already
		 * been inserted.
		 *
		 * WARNING WARNING WARNING
		 */
		/* If cell was part of an array, reset the corner pointer */
Jody Goldberg's avatar
Jody Goldberg committed
218
219
220
		if (expr->any.oper == OPER_ARRAY) {
			int const x = expr->array.x;
			int const y = expr->array.y;
221
			if (x != 0 || y != 0)
Jody Goldberg's avatar
Jody Goldberg committed
222
				expr->array.corner.cell =
223
224
225
					sheet_cell_get (cell->sheet,
							cell->col_info->pos - x,
							cell->row_info->pos - y);
226
		}
227

Jody Goldberg's avatar
Jody Goldberg committed
228
229
230
		/* bounds check, and adjust local references from the cell */
		if (rwinfo != NULL) {
			expr = expr_rewrite (expr, rwinfo);
231

232
			if (expr != NULL) {
233
				/* expression was unlinked above */
234
235
236
237
				expr_tree_unref (cell->u.expression);
				cell->u.expression = expr;
			}
		}
238

239
		/* Relink the expression.  */
240
		cell_formula_changed (cell, TRUE);
241
242
	}

243
	/* 3. Move any auxiliary canvas items */
244
	if (cell->comment)
245
		cell_comment_reposition (cell);
246
247
}

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/****************************************************************************/

/*
 * cell_set_text : Stores the supplied text as the entered_text, then parses it
 *      for storage as a value or expression.  It marks the sheet as dirty.
 *
 * If the text is an expression it IS queued for recalc.
 *        the format prefered by the expression is stored for later use.
 * If the text is a value it is rendered but spans are not calculated.
 *        the format that matched the text is stored for later use.
 *
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
 *
 * NOTE : This DOES check for array partitioning.
 */
264
void
265
cell_set_text (Cell *cell, char const *text)
266
{
267
	StyleFormat *format;
268
269
	Value *val;
	ExprTree *expr;
270
	EvalPos pos;
271

272
	g_return_if_fail (cell != NULL);
273
274
	g_return_if_fail (text != NULL);
	g_return_if_fail (!cell_is_partial_array (cell));
275

276
	format = parse_text_value_or_expr (eval_pos_init_cell (&pos, cell),
Jody Goldberg's avatar
Jody Goldberg committed
277
278
					   text, &val, &expr,
					   NULL /* TODO : Use assigned format ? */);
279

280
281
	if (val != NULL) {	/* String was a value */
		cell_cleanout (cell);
282

283
284
285
		cell->cell_flags &= ~CELL_HAS_EXPRESSION;
		cell->value = val;
		cell->u.entered_text = string_get (text);
286
		cell->format = format;
287
288
		cell_render_value (cell);
	} else {		/* String was an expression */
Jody Goldberg's avatar
Jody Goldberg committed
289
		cell_set_expr (cell, expr, format);
290
		if (format) style_format_unref (format);
291
		expr_tree_unref (expr);
292
	}
293
	cell_dirty (cell);
294
295
}

Miguel de Icaza's avatar
Miguel de Icaza committed
296
/*
297
298
299
300
301
302
 * cell_set_text_and_value : Stores the supplied text as the entered_text, then
 *      stores (WITHOUT COPYING) the supplied value.
 *      It marks the sheet as dirty. The text is NOT parsed.
 *
 * The cell is rendered but spans are not calculated.
 * If an optional format is supplied it is stored for later use.
Miguel de Icaza's avatar
Miguel de Icaza committed
303
 *
304
305
306
307
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
 *
 * NOTE : This DOES check for array partitioning.
Miguel de Icaza's avatar
Miguel de Icaza committed
308
 */
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
309
void
310
cell_set_text_and_value (Cell *cell, String *text,
Jody Goldberg's avatar
Jody Goldberg committed
311
			 Value *v, StyleFormat *opt_fmt)
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
312
{
313
314
315
316
	g_return_if_fail (cell);
	g_return_if_fail (text);
	g_return_if_fail (v);
	g_return_if_fail (!cell_is_partial_array (cell));
Arturo Espinosa's avatar
Arturo Espinosa committed
317

Jody Goldberg's avatar
Jody Goldberg committed
318
319
320
	if (opt_fmt != NULL)
		style_format_ref (opt_fmt);

321
322
	cell_dirty (cell);
	cell_cleanout (cell);
323

Jody Goldberg's avatar
Jody Goldberg committed
324
	cell->format = opt_fmt;
325
	cell->u.entered_text = string_ref (text);
326
327
	cell->value = v;
	cell_render_value (cell);
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
328
329
}

Michael Meeks's avatar
Michael Meeks committed
330
/*
331
332
333
334
 * cell_assign_value : Stores (WITHOUT COPYING) the supplied value.
 *    no changes are made to the expression or entered text.  This
 *    is for use by routines that wish to store values directly such
 *    as expression calculation or import for array formulas.
Michael Meeks's avatar
Michael Meeks committed
335
 *
336
 * The value is rendered but spans are not calculated.
Michael Meeks's avatar
Michael Meeks committed
337
 *
338
 * If an optional format is supplied it is stored for later use.
Michael Meeks's avatar
Michael Meeks committed
339
 *
340
341
342
343
344
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, does not calculate spans, does
 *           not mark anything as dirty.
 *
 * NOTE : This DOES NOT check for array partitioning.
Michael Meeks's avatar
Michael Meeks committed
345
346
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
347
cell_assign_value (Cell *cell, Value *v, StyleFormat *opt_fmt)
Michael Meeks's avatar
Michael Meeks committed
348
349
350
351
{
	g_return_if_fail (cell);
	g_return_if_fail (v);

Jody Goldberg's avatar
Jody Goldberg committed
352
353
354
355
356
	if (cell->format)
		style_format_unref (cell->format);
	if (opt_fmt)
		style_format_ref (opt_fmt);
	cell->format = opt_fmt;
357
358
359

	if (cell->value != NULL)
		value_release (cell->value);
Michael Meeks's avatar
Michael Meeks committed
360
361
362
363
364
	cell->value = v;
	cell_render_value (cell);
}

/*
365
366
367
368
369
 * cell_set_value : Stores (WITHOUT COPYING) the supplied value.  It marks the
 *          sheet as dirty.
 *
 * The value is rendered but spans are not calculated, then the rendered string
 * is stored as if that is what the user had entered.
Michael Meeks's avatar
Michael Meeks committed
370
 *
371
372
373
374
375
376
377
378
379
380
381
382
 * If an optional format is supplied it is stored for later use.
 *
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
 *
 * FIXME FIXME FIXME : The current format code only checks against
 *      the list of canned formats.  Therefore the rendered string
 *      MAY NOT BE PARSEABLE!! if the user has assigned a non-std format.
 *      We need to imporove the parser to handle all formats that exist within
 *      the workbook.
 *
 * NOTE : This DOES check for array partitioning.
Michael Meeks's avatar
Michael Meeks committed
383
384
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
385
cell_set_value (Cell *cell, Value *v, StyleFormat *opt_fmt)
Michael Meeks's avatar
Michael Meeks committed
386
387
388
{
	g_return_if_fail (cell);
	g_return_if_fail (v);
389
	g_return_if_fail (!cell_is_partial_array (cell));
390

Jody Goldberg's avatar
Jody Goldberg committed
391
392
393
	if (opt_fmt)
		style_format_ref (opt_fmt);

394
395
	cell_dirty (cell);
	cell_cleanout (cell);
Michael Meeks's avatar
Michael Meeks committed
396

Jody Goldberg's avatar
Jody Goldberg committed
397
	cell->format = opt_fmt;
398
399
	cell->value = v;
	cell_render_value (cell);
400
401
402
403
404
405
406

	/* Be careful that a value passes as a string stays a string */
	if (v->type == VALUE_STRING) {
		/* TODO : add new string routine to avoid the extra copy */
		char *tmp = g_strconcat ("\'", v->v_str.val->str, NULL);
		cell->u.entered_text = string_get (tmp);
		g_free (tmp);
Jody Goldberg's avatar
Jody Goldberg committed
407
408
409
	} else if (opt_fmt) {
		/* If available use the supplied format */
		cell->u.entered_text = string_get (format_value (opt_fmt, v, NULL, NULL));
410
	} else
Jody Goldberg's avatar
Jody Goldberg committed
411
		/* Fall back on using the format applied to the cell */
412
		cell->u.entered_text = string_ref (cell->rendered_value->rendered_text);
Michael Meeks's avatar
Michael Meeks committed
413
414
}

415
/*
416
 * cell_set_expr_and_value : Stores (WITHOUT COPYING) the supplied value, and
417
418
419
 *        references the supplied expression and links it into the expression
 *        list.  It marks the sheet as dirty. It is intended for use by import
 *        routines or operations that do bulk assignment.
420
 *
421
 * If an optional format is supplied it is stored for later use.
422
 *
423
424
 * The cell is rendered but spans are not calculated,  the cell is NOT marked for
 * recalc.
425
 *
426
427
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
Jody Goldberg's avatar
Jody Goldberg committed
428
 *
429
 * NOTE : This DOES check for array partitioning.
430
 */
431
void
432
cell_set_expr_and_value (Cell *cell, ExprTree *expr, Value *v)
433
{
Arturo Espinosa's avatar
Arturo Espinosa committed
434
	g_return_if_fail (cell != NULL);
435
436
437
438
439
	g_return_if_fail (expr != NULL);
	g_return_if_fail (!cell_is_partial_array (cell));

	/* Repeat after me.  Ref before unref. */
	expr_tree_ref (expr);
Arturo Espinosa's avatar
Arturo Espinosa committed
440

441
	cell_dirty (cell);
442
	cell_cleanout (cell);
443

444
445
	cell->u.expression = expr;
	cell->cell_flags |= CELL_HAS_EXPRESSION;
446
	sheet_cell_expr_link (cell);
447
448
449
450
451
452
453
#if 0
	/* TODO : Should we add this for consistancy ? */
	cell->format = fmt;
#endif
	cell->value = v;
	cell_render_value (cell);
}
454

455
456
/**
 * cell_set_expr_internal:
457
 * @cell: the cell to set the formula to
458
 * @expr: an expression tree with the formula
459
 * opt_fmt: an optional format to apply to the cell.
460
461
462
463
464
465
 *
 * A private internal utility to store an expression.
 * Does NOT
 * 	- check for array subdivision
 * 	- queue recalcs.
 * 	- render value, calc dimension, compute spans
466
 * 	- link the expression into the master list.
467
 */
Jody Goldberg's avatar
Jody Goldberg committed
468
static void
Jody Goldberg's avatar
Jody Goldberg committed
469
cell_set_expr_internal (Cell *cell, ExprTree *expr, StyleFormat *opt_fmt)
470
{
Jody Goldberg's avatar
Jody Goldberg committed
471
472
	if (opt_fmt != NULL)
		style_format_ref (opt_fmt);
473

474
	expr_tree_ref (expr);
475

476
477
	cell_dirty (cell);
	cell_cleanout (cell);
478

479
480
481
	if (cell->u.expression)
		expr_tree_unref (cell->u.expression);

Jody Goldberg's avatar
Jody Goldberg committed
482
	cell->format = opt_fmt;
483
484
	cell->u.expression = expr;
	cell->cell_flags |= CELL_HAS_EXPRESSION;
485

486
487
	/* Until the value is recomputed, we put in this value.  */
	cell->value = value_new_error (NULL, gnumeric_err_RECALC);
488
489
490
}

/*
Jody Goldberg's avatar
Jody Goldberg committed
491
492
493
 * cell_set_expr_unsafe : Stores and references the supplied expression.  It
 *         marks the sheet as dirty.  Intented for use by import routines that
 *         do bulk assignment.
494
495
496
497
498
499
500
 *
 * The cell IS marked for recalc.
 *
 * If an optional format is supplied it is stored for later use.
 *
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
Jody Goldberg's avatar
Jody Goldberg committed
501
502
 *           It also DOES NOT CHECK FOR ARRAY DIVISION.  Be very careful
 *           using this.
503
504
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
505
cell_set_expr_unsafe (Cell *cell, ExprTree *expr, StyleFormat *opt_fmt)
506
507
{
	g_return_if_fail (cell != NULL);
508
	g_return_if_fail (expr != NULL);
509

Jody Goldberg's avatar
Jody Goldberg committed
510
	cell_set_expr_internal (cell, expr, opt_fmt);
511
	cell_formula_changed (cell, TRUE);
512
513
}

Jody Goldberg's avatar
Jody Goldberg committed
514
515
516
517
518
/**
 * cell_set_expr : A utility wrapper for cell_set_expr_unsafe.  That adds
 *      checks for array subdivision.
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
519
cell_set_expr (Cell *cell, ExprTree *expr, StyleFormat *opt_fmt)
Jody Goldberg's avatar
Jody Goldberg committed
520
521
522
{
	g_return_if_fail (!cell_is_partial_array (cell));

Jody Goldberg's avatar
Jody Goldberg committed
523
	cell_set_expr_unsafe (cell, expr, opt_fmt);
Jody Goldberg's avatar
Jody Goldberg committed
524
525
}

526
527
528
529
530
531
532
533
/**
 * cell_set_array_formula:
 * @sheet:   The sheet to set the formula to.
 * @row_a:   The top row in the destination region.
 * @col_a:   The left column in the destination region.
 * @row_b:   The bottom row in the destination region.
 * @col_b:   The right column in the destination region.
 * @formula: an expression tree with the formula
534
535
 * @queue_recalc : A flag that if true indicates that the cells should be
 *                 queued for recalc.
536
 *
537
 * Uses cell_set_expr_internal to store the formula as an
538
539
540
 * 'array-formula'.  The supplied expression is wrapped in an array
 * operator for each cell in the range and scheduled for recalc.  The
 * upper left corner is handled as a special case and care is taken to
541
542
 * put it at the head of the recalc queue if recalcs are requested.
 *
Jody Goldberg's avatar
Jody Goldberg committed
543
544
545
 * NOTE : Does not add a reference to the expression.  It takes over the callers
 *        reference.
 *
546
547
548
 * Does not regenerate spans, dimensions or autosize cols/rows.
 *
 * DOES NOT CHECK for array partitioning.
549
550
551
552
 */
void
cell_set_array_formula (Sheet *sheet,
			int row_a, int col_a, int row_b, int col_b,
553
			ExprTree *formula,
554
			gboolean queue_recalc)
555
556
557
558
559
{
	int const num_rows = 1 + row_b - row_a;
	int const num_cols = 1 + col_b - col_a;
	int x, y;
	Cell * const corner = sheet_cell_fetch (sheet, col_a, row_a);
560
	ExprTree *wrapper;
561
562
563
564
565

	g_return_if_fail (num_cols > 0);
	g_return_if_fail (num_rows > 0);
	g_return_if_fail (formula != NULL);
	g_return_if_fail (corner != NULL);
Jody Goldberg's avatar
Jody Goldberg committed
566
567
	g_return_if_fail (col_a <= col_b);
	g_return_if_fail (row_a <= row_b);
568

569
570
571
	wrapper = expr_tree_new_array (0, 0, num_rows, num_cols);
	wrapper->array.corner.func.value = NULL;
	wrapper->array.corner.func.expr = formula;
572
	cell_set_expr_internal (corner, wrapper, NULL);
573
	expr_tree_unref (wrapper);
574
575

	for (x = 0; x < num_cols; ++x)
576
		for (y = 0; y < num_rows; ++y) {
577
578
			Cell *cell;

579
580
			if (x == 0 && y == 0)
				continue;
581

Michael Meeks's avatar
Michael Meeks committed
582
			cell = sheet_cell_fetch (sheet, col_a + x, row_a + y);
583
584
			wrapper = expr_tree_new_array (x, y, num_rows, num_cols);
			wrapper->array.corner.cell = corner;
585
			cell_set_expr_internal (cell, wrapper, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
586
			cell_formula_changed (cell, queue_recalc);
587
			expr_tree_unref (wrapper);
588
589
590
		}

	/* Put the corner at the head of the recalc list */
591
	cell_formula_changed (corner, queue_recalc);
592
593
}

594
/***************************************************************************/
Arturo Espinosa's avatar
Arturo Espinosa committed
595

596
gboolean
597
cell_is_blank (Cell const * cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
598
{
599
600
	return (cell == NULL || cell->value == NULL ||
		cell->value->type == VALUE_EMPTY);
Arturo Espinosa's avatar
Arturo Espinosa committed
601
}
602

603
Value *
604
cell_is_error (Cell const * cell)
605
{
606
607
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->value != NULL, NULL);
608

609
610
611
	if (cell->value->type == VALUE_ERROR)
		return cell->value;
	return NULL;
612
613
}

614
inline gboolean
615
cell_is_number (Cell const *cell)
616
{
617
618
	/* FIXME : This does not handle arrays or ranges */
	return (cell->value && VALUE_IS_NUMBER (cell->value));
619
}
620

621
gboolean
622
cell_is_zero (Cell const *cell)
623
624
625
626
627
{
	Value const * const v = cell->value;
	if (v == NULL)
		return FALSE;
	switch (v->type) {
628
629
	case VALUE_BOOLEAN : return !v->v_bool.val;
	case VALUE_INTEGER : return v->v_int.val == 0;
630
631
	case VALUE_FLOAT :
	{
632
		double const res = v->v_float.val;
633
634
635
636
637
638
639
		return (-1e-10 < res && res < 1e-10);
	}

	default :
		return FALSE;
	}
}
640

641
642
inline ExprArray const *
cell_is_array (Cell const *cell)
643
{
644
	if (cell != NULL && cell_has_expr (cell) &&
645
646
	    cell->u.expression->any.oper == OPER_ARRAY)
		return &cell->u.expression->array;
647
	return NULL;
648
649
}

650
gboolean
651
cell_is_partial_array (Cell const *cell)
652
{
653
	ExprArray const *ref = cell_is_array (cell);
654
	return ref != NULL && (ref->cols > 1 || ref->rows > 1);
655
656
}

657
/***************************************************************************/
658
659

/*
660
661
 * cell_render_value
 * @cell: The cell whose value needs to be rendered
662
 *
663
664
665
666
667
 * TODO :
 * There is no reason currently for this to allocate the rendered value as
 * seperate entity.  However, in the future I'm thinking of referencing them
 * akin to strings.  We need to do some profiling of how frequently things
 * are shared.
668
669
 */
void
670
cell_render_value (Cell *cell)
671
{
672
	RenderedValue *rv;
673
674
	g_return_if_fail (cell != NULL);

675
676
677
678
	rv = rendered_value_new (cell);
	if (cell->rendered_value)
		rendered_value_destroy (cell->rendered_value);
	cell->rendered_value = rv;
679
680
}

681

682
MStyle *
683
cell_get_mstyle (Cell const *cell)
684
{
685
686
687
	return sheet_style_compute (cell->sheet,
				    cell->col_info->pos,
				    cell->row_info->pos);
688
689
690
}

void
691
cell_set_mstyle (Cell const *cell, MStyle *mstyle)
692
693
694
{
	Range         range;

695
696
	range.start.col = cell->col_info->pos;
	range.start.row = cell->row_info->pos;
697
698
699
700
701
	range.end       = range.start;

	sheet_style_attach (cell->sheet, range, mstyle);
}

Morten Welinder's avatar
Morten Welinder committed
702
char *
703
cell_get_format (Cell const *cell)
Morten Welinder's avatar
Morten Welinder committed
704
{
705
706
	char   *result = NULL;
	MStyle *mstyle = cell_get_mstyle (cell);
Morten Welinder's avatar
Morten Welinder committed
707
708

	if (mstyle_is_element_set (mstyle, MSTYLE_FORMAT)) {
709
		char const *format;
Morten Welinder's avatar
Morten Welinder committed
710
		format = mstyle_get_format (mstyle)->format;
711

Morten Welinder's avatar
Morten Welinder committed
712
713
		/* FIXME: we really should distinguish between "not assigned"
		   and "assigned General".  */
714
715
716
717
718
719
720
721
722
723
		if (format) {
			/* If the format is General it may have been over
			 * ridden by the format used to parse the input text.
			 */
			if (strcmp (format, "General") != 0)
				result = g_strdup (format);
			else if (cell->format)
				result = g_strdup (cell->format->format);
		}
	}
Morten Welinder's avatar
Morten Welinder committed
724
725
726
727

	mstyle_unref (mstyle);
	return result;
}
728
729
730
731
732
733

/*
 * cell_set_format:
 *
 * Changes the format for CELL to be FORMAT.  FORMAT should be
 * a number display format as specified on the manual
734
735
 *
 * Does not render, redraw, or respan.
736
737
 */
void
738
cell_set_format (Cell *cell, char const *format)
739
{
Jody Goldberg's avatar
Jody Goldberg committed
740
	MStyle *mstyle = mstyle_new ();
741
742
743

	g_return_if_fail (mstyle != NULL);

744
	mstyle_set_format_text (mstyle, format);
745
746
747
748
	cell_set_mstyle (cell, mstyle);
	cell_dirty (cell);
}

Jody Goldberg's avatar
Jody Goldberg committed
749
750
751
752
753
754
755
756
757
758
759
760
761
/**
 * cell_make_value : drops the expression keeps its value.  Then uses the formatted
 *      result as if that had been entered.
 *
 * NOTE : the cell's expression can not be linked into the expression * list.
 *
 * The cell is rendered but spans are not calculated,  the cell is NOT marked for
 * recalc.
 *
 * WARNING : This is an internal routine that does not queue redraws,
 *           does not auto-resize, and does not calculate spans.
 *
 * NOTE : This DOES NOT check for array partitioning.
762
763
764
765
766
767
768
 */
void
cell_make_value (Cell *cell)
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell_has_expr(cell));

Jody Goldberg's avatar
Jody Goldberg committed
769
770
771
772
773
774
775
776
777
778
	expr_tree_unref (cell->u.expression);
	cell->u.expression = NULL;
	cell->cell_flags &= ~CELL_HAS_EXPRESSION;

	if (cell->rendered_value == NULL)
		cell_render_value (cell);

	g_return_if_fail (cell->rendered_value != NULL);

	cell->u.entered_text = string_ref (cell->rendered_value->rendered_text);
779
780
	cell_dirty (cell);
}