cell.c 19.4 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"
Arturo Espinosa's avatar
Arturo Espinosa committed
19

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

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

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

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * 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
69
static void
70
71
cell_cleanout (Cell *cell)
{
72
73
	/* A cell can have either an expression or entered text */
	if (cell_has_expr (cell)) {
74
75
76
		/* Clipboard cells, e.g., are not attached to a sheet.  */
		if (cell->sheet)
			sheet_cell_formula_unlink (cell);
77
78
79
80
81
		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;
82
83
84
85
86
87
	}

	if (cell->value) {
		value_release (cell->value);
		cell->value = NULL;
	}
88
89
90
91
92
93
94
	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;
95
	}
96
97

	cell->cell_flags &= ~(CELL_HAS_EXPRESSION|CELL_QUEUED_FOR_RECALC);
98
99
}

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

113
	g_return_val_if_fail (cell != NULL, NULL);
114

115
	new_cell = g_new (Cell, 1);
116

117
118
	/* bitmap copy first */
	*new_cell = *cell;
119

120
	new_cell->cell_flags &= ~CELL_QUEUED_FOR_RECALC;
Morten Welinder's avatar
Morten Welinder committed
121

122
123
124
125
126
	/* 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);
127

128
	new_cell->rendered_value = NULL;
129

130
131
132
	new_cell->value = (new_cell->value)
	    ? value_duplicate (new_cell->value)
	    : value_new_empty ();
Morten Welinder's avatar
Morten Welinder committed
133

134
135
136
	if (cell->format)
		style_format_ref (cell->format);

137
138
139
	if (cell->comment) {
		new_cell->comment = NULL;
		cell_set_comment (new_cell, cell->comment->comment->str);
Morten Welinder's avatar
Morten Welinder committed
140
	}
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
141

142
	return new_cell;
Arturo Espinosa's avatar
Arturo Espinosa committed
143
}
Arturo Espinosa's avatar
Arturo Espinosa committed
144

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

156
157
158
159
	cell_dirty (cell);
	cell_cleanout (cell);
	cell_comment_destroy (cell);
	g_free (cell);
160
}
161

162
163
164
165
166
167
/**
 * cell_content_changed: Queues recalc of all of the cells depends.
 *
 */
void
cell_content_changed (Cell *cell)
168
{
169
	GList   *deps;
170

171
172
	g_return_if_fail (cell != NULL);

173
174
175
176
	/* Queue all of the dependencies for this cell */
	deps = cell_get_dependencies (cell);
	if (deps)
		cell_queue_recalc_list (deps, TRUE);
177

178
	sheet_cell_changed (cell);
179
180
}

181
182
183
184
185
186
187
188
189
190
/*
 * cell_relocate:
 * @cell:           The cell that is changing position
 * @check_bonunds : Should expressions be bounds checked.
 *
 * This routine is used to move a cell to a different location:
 *
 * Auxiliary items canvas items attached to the cell are moved.
 */
void
191
cell_relocate (Cell *cell, gboolean check_bounds)
192
{
193
	g_return_if_fail (cell != NULL);
194

195
196
	/* 1. Tag the cell as dirty */
	cell_dirty (cell);
197

198
199
200
	/* 2. If the cell contains a formula, relocate the formula */
	if (cell_has_expr (cell)) {
		sheet_cell_formula_unlink (cell);
201

202
203
204
205
206
207
208
209
210
		/*
		 * 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 */
211
212
213
		if (cell->u.expression->any.oper == OPER_ARRAY) {
			int const x = cell->u.expression->array.x;
			int const y = cell->u.expression->array.y;
214
			if (x != 0 || y != 0)
215
				cell->u.expression->array.corner.cell =
216
217
218
					sheet_cell_get (cell->sheet,
							cell->col_info->pos - x,
							cell->row_info->pos - y);
219
		}
220

221
222
		/*
		 * We do not actually need to change any references
223
224
225
226
		 * the move is from its current location to its current
		 * location.  All the move is doing is a bounds check.
		 */
		if (check_bounds) {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
			ExprRewriteInfo   rwinfo;
			ExprRelocateInfo *rinfo;
			ExprTree    	 *expr = cell->u.expression;

			rwinfo.type = EXPR_REWRITE_RELOCATE;
			rinfo = &rwinfo.u.relocate;
			rinfo->origin.start.col =
				rinfo->origin.end.col = cell->col_info->pos;
			rinfo->origin.start.row =
				rinfo->origin.end.row = cell->row_info->pos;
			rinfo->origin_sheet = rinfo->target_sheet = cell->sheet;
			rinfo->col_offset = 0;
			rinfo->row_offset = 0;
			eval_pos_init_cell (&rinfo->pos, cell);
			expr = expr_rewrite (expr, &rwinfo);
242

243
			if (expr != NULL) {
244
				/* expression was unlinked above */
245
246
247
248
				expr_tree_unref (cell->u.expression);
				cell->u.expression = expr;
			}
		}
249

250
		/* Relink the expression.  */
251
		cell_formula_changed (cell, TRUE);
252
253
	}

254
	/* 3. Move any auxiliary canvas items */
255
	if (cell->comment)
256
		cell_comment_reposition (cell);
257

258
	cell_content_changed (cell);
259
260
}

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/****************************************************************************/

/*
 * 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.
 */
277
void
278
cell_set_text (Cell *cell, char const *text)
279
{
280
281
282
	char const *format;
	Value *val;
	ExprTree *expr;
283
	EvalPos pos;
284

285
	g_return_if_fail (cell != NULL);
286
287
	g_return_if_fail (text != NULL);
	g_return_if_fail (!cell_is_partial_array (cell));
288

289
	format = parse_text_value_or_expr (eval_pos_init_cell (&pos, cell),
290
					   text, &val, &expr);
291

292
293
294
295
	if (val != NULL) {	/* String was a value */
		/* If there was a prefered format remember it */
		StyleFormat * const fmt = (format != NULL)
		    ? style_format_new (format) : NULL;
296

297
		cell_cleanout (cell);
298

299
300
301
302
303
304
305
306
307
		cell->cell_flags &= ~CELL_HAS_EXPRESSION;
		cell->value = val;
		cell->u.entered_text = string_get (text);
		cell->format = fmt;
		cell_render_value (cell);
		cell_content_changed (cell);
	} else {		/* String was an expression */
		cell_set_expr (cell, expr, format);
		expr_tree_unref (expr);
308
	}
309
	cell_dirty (cell);
310
311
}

Miguel de Icaza's avatar
Miguel de Icaza committed
312
/*
313
314
315
316
317
318
 * 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
319
 *
320
321
322
323
 * 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
324
 */
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
325
void
326
327
cell_set_text_and_value (Cell *cell, String *text,
			 Value *v, char const * optional_format)
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
328
{
329
330
331
332
	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
333

334
335
	cell_dirty (cell);
	cell_cleanout (cell);
336

337
338
339
340
341
	/* FIXME : When we remerge the number recognition and the formating
	 * this should be a StyleFormat
	 */
	if (optional_format)
		cell->format = style_format_new (optional_format);
342
	cell->u.entered_text = string_ref (text);
343
344
	cell->value = v;
	cell_render_value (cell);
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
345
346
}

Michael Meeks's avatar
Michael Meeks committed
347
/*
348
349
350
351
 * 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
352
 *
353
 * The value is rendered but spans are not calculated.
Michael Meeks's avatar
Michael Meeks committed
354
 *
355
 * If an optional format is supplied it is stored for later use.
Michael Meeks's avatar
Michael Meeks committed
356
 *
357
358
359
360
361
 * 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
362
363
 */
void
364
cell_assign_value (Cell *cell, Value *v, char const * optional_format)
Michael Meeks's avatar
Michael Meeks committed
365
366
367
368
{
	g_return_if_fail (cell);
	g_return_if_fail (v);

369
370
	if (optional_format)
		cell->format = style_format_new (optional_format);
371
372
373

	if (cell->value != NULL)
		value_release (cell->value);
Michael Meeks's avatar
Michael Meeks committed
374
375
376
377
378
	cell->value = v;
	cell_render_value (cell);
}

/*
379
380
381
382
383
 * 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
384
 *
385
386
387
388
389
390
391
392
393
394
395
396
 * 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
397
398
 */
void
399
cell_set_value (Cell *cell, Value *v, char const * optional_format)
Michael Meeks's avatar
Michael Meeks committed
400
401
402
{
	g_return_if_fail (cell);
	g_return_if_fail (v);
403
	g_return_if_fail (!cell_is_partial_array (cell));
404

405
406
	cell_dirty (cell);
	cell_cleanout (cell);
Michael Meeks's avatar
Michael Meeks committed
407

408
409
410
411
	if (optional_format)
		cell->format = style_format_new (optional_format);
	cell->value = v;
	cell_render_value (cell);
412
413
414
415
416
417
418
419
420

	/* 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);
	} else
		cell->u.entered_text = string_ref (cell->rendered_value->rendered_text);
Michael Meeks's avatar
Michael Meeks committed
421
422
}

423
/*
424
 * cell_set_expr_and_value : Stores (WITHOUT COPYING) the supplied value, and
425
426
427
 *        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.
428
 *
429
 * If an optional format is supplied it is stored for later use.
430
 *
431
432
 * The cell is rendered but spans are not calculated,  the cell is NOT marked for
 * recalc.
433
 *
434
435
 * 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
436
 *
437
 * NOTE : This DOES check for array partitioning.
438
 */
439
void
440
cell_set_expr_and_value (Cell *cell, ExprTree *expr, Value *v)
441
{
Arturo Espinosa's avatar
Arturo Espinosa committed
442
	g_return_if_fail (cell != NULL);
443
444
445
446
447
	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
448

449
	cell_dirty (cell);
450
	cell_cleanout (cell);
451

452
453
	cell->u.expression = expr;
	cell->cell_flags |= CELL_HAS_EXPRESSION;
454
	sheet_cell_formula_link (cell);
455
456
457
458
459
460
461
#if 0
	/* TODO : Should we add this for consistancy ? */
	cell->format = fmt;
#endif
	cell->value = v;
	cell_render_value (cell);
}
462

463
464
465
466
467
468
469
470
471
472
/**
 * cell_set_expr_internal:
 * @cell:    the cell to set the formula to
 * @expr: an expression tree with the formula
 *
 * A private internal utility to store an expression.
 * Does NOT
 * 	- check for array subdivision
 * 	- queue recalcs.
 * 	- render value, calc dimension, compute spans
473
 * 	- link the expression into the master list.
474
 */
Jody Goldberg's avatar
Jody Goldberg committed
475
static void
476
477
478
cell_set_expr_internal (Cell *cell, ExprTree *expr, char const *optional_format)
{
	StyleFormat * fmt;
479

480
481
482
	/* Repeat after me.  Ref before unref. */
	expr_tree_ref (expr);
	fmt = (optional_format != NULL) ? style_format_new (optional_format) : NULL;
483

484
485
	cell_dirty (cell);
	cell_cleanout (cell);
486

487
488
	cell->u.expression = expr;
	cell->cell_flags |= CELL_HAS_EXPRESSION;
489

490
491
	/* Until the value is recomputed, we put in this value.  */
	cell->value = value_new_error (NULL, gnumeric_err_RECALC);
492
493
494
}

/*
Jody Goldberg's avatar
Jody Goldberg committed
495
496
497
 * 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.
498
499
500
501
502
503
504
 *
 * 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
505
506
 *           It also DOES NOT CHECK FOR ARRAY DIVISION.  Be very careful
 *           using this.
507
508
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
509
cell_set_expr_unsafe (Cell *cell, ExprTree *expr, char const *optional_format)
510
511
{
	g_return_if_fail (cell != NULL);
512
	g_return_if_fail (expr != NULL);
513

514
515
	cell_set_expr_internal (cell, expr, optional_format);
	cell_formula_changed (cell, TRUE);
516
517
}

Jody Goldberg's avatar
Jody Goldberg committed
518
519
520
521
522
523
524
525
526
527
528
529
/**
 * cell_set_expr : A utility wrapper for cell_set_expr_unsafe.  That adds
 *      checks for array subdivision.
 */
void
cell_set_expr (Cell *cell, ExprTree *expr, char const *optional_format)
{
	g_return_if_fail (!cell_is_partial_array (cell));

	cell_set_expr_unsafe (cell, expr, optional_format);
}

530
531
532
533
534
535
536
537
/**
 * 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
538
539
 * @queue_recalc : A flag that if true indicates that the cells should be
 *                 queued for recalc.
540
 *
541
 * Uses cell_set_expr_internal to store the formula as an
542
543
544
 * '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
545
546
 * put it at the head of the recalc queue if recalcs are requested.
 *
Jody Goldberg's avatar
Jody Goldberg committed
547
548
549
 * NOTE : Does not add a reference to the expression.  It takes over the callers
 *        reference.
 *
550
551
552
 * Does not regenerate spans, dimensions or autosize cols/rows.
 *
 * DOES NOT CHECK for array partitioning.
553
554
555
556
 */
void
cell_set_array_formula (Sheet *sheet,
			int row_a, int col_a, int row_b, int col_b,
557
			ExprTree *formula,
558
			gboolean queue_recalc)
559
560
561
562
563
{
	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);
564
	ExprTree *wrapper;
565
566
567
568
569

	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
570
571
	g_return_if_fail (col_a <= col_b);
	g_return_if_fail (row_a <= row_b);
572

573
574
575
	wrapper = expr_tree_new_array (0, 0, num_rows, num_cols);
	wrapper->array.corner.func.value = NULL;
	wrapper->array.corner.func.expr = formula;
576
	cell_set_expr_internal (corner, wrapper, NULL);
577
	expr_tree_unref (wrapper);
578
579

	for (x = 0; x < num_cols; ++x)
580
		for (y = 0; y < num_rows; ++y) {
581
582
			Cell *cell;

583
584
			if (x == 0 && y == 0)
				continue;
585

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

	/* Put the corner at the head of the recalc list */
595
	cell_formula_changed (corner, queue_recalc);
596
597
}

598
/***************************************************************************/
Arturo Espinosa's avatar
Arturo Espinosa committed
599

600
gboolean
601
cell_is_blank (Cell const * cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
602
{
603
604
	return (cell == NULL || cell->value == NULL ||
		cell->value->type == VALUE_EMPTY);
Arturo Espinosa's avatar
Arturo Espinosa committed
605
}
606

607
Value *
608
cell_is_error (Cell const * cell)
609
{
610
611
	g_return_val_if_fail (cell != NULL, NULL);
	g_return_val_if_fail (cell->value != NULL, NULL);
612

613
614
615
	if (cell->value->type == VALUE_ERROR)
		return cell->value;
	return NULL;
616
617
}

618
inline gboolean
619
cell_is_number (Cell const *cell)
620
{
621
622
	/* FIXME : This does not handle arrays or ranges */
	return (cell->value && VALUE_IS_NUMBER (cell->value));
623
}
624

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

	default :
		return FALSE;
	}
}
644

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

654
gboolean
655
cell_is_partial_array (Cell const *cell)
656
{
657
	ExprArray const *ref = cell_is_array (cell);
658
	return ref != NULL && (ref->cols > 1 || ref->rows > 1);
659
660
}

661
/***************************************************************************/
662
663

/*
664
665
 * cell_render_value
 * @cell: The cell whose value needs to be rendered
666
 *
667
668
669
670
671
 * 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.
672
673
 */
void
674
cell_render_value (Cell *cell)
675
{
676
	RenderedValue *rv;
677
678
	g_return_if_fail (cell != NULL);

679
680
681
682
	rv = rendered_value_new (cell);
	if (cell->rendered_value)
		rendered_value_destroy (cell->rendered_value);
	cell->rendered_value = rv;
683
684
}

685

686
MStyle *
687
cell_get_mstyle (Cell const *cell)
688
{
689
690
691
	return sheet_style_compute (cell->sheet,
				    cell->col_info->pos,
				    cell->row_info->pos);
692
693
694
}

void
695
cell_set_mstyle (Cell const *cell, MStyle *mstyle)
696
697
698
{
	Range         range;

699
700
	range.start.col = cell->col_info->pos;
	range.start.row = cell->row_info->pos;
701
702
703
704
705
	range.end       = range.start;

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

Morten Welinder's avatar
Morten Welinder committed
706
char *
707
cell_get_format (Cell const *cell)
Morten Welinder's avatar
Morten Welinder committed
708
709
710
711
{
	MStyle *mstyle;
	char *result;

712
	mstyle = cell_get_mstyle (cell);
Morten Welinder's avatar
Morten Welinder committed
713
	if (mstyle_is_element_set (mstyle, MSTYLE_FORMAT)) {
714
		char const *format;
Morten Welinder's avatar
Morten Welinder committed
715
716
717
718
719
720
721
722
723
724
725
726
727
		format = mstyle_get_format (mstyle)->format;
		/* FIXME: we really should distinguish between "not assigned"
		   and "assigned General".  */
		if (format && strcmp (format, "General") != 0)
			result = g_strdup (format);
		else
			result = NULL;
	} else
		result = NULL;

	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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761

	g_return_if_fail (mstyle != NULL);

	mstyle_set_format (mstyle, format);
	cell_set_mstyle (cell, mstyle);
	cell_dirty (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_has_expr(cell));

	/* FIXME: does this work at all?  -- MW */
	/* THIS IS CRAP fix soon */
	cell_dirty (cell);
}