cell.c 33.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 <gnome.h>
Miguel de Icaza's avatar
Miguel de Icaza committed
9
#include <locale.h>
10
#include <ctype.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric-sheet.h"
13
#include "gnumeric-util.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
14
#include "eval.h"
15
#include "format.h"
16
#include "color.h"
17
#include "border.h"
18
#include "cursors.h"
19
#include "utils.h"
20
#include "gnumeric-util.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
21

22
23
static int         redraws_frozen           = 0;
static int         redraws_deep_frozen      = 0;
24
25
static GHashTable *cell_hash_queue;

26
static void
27
28
29
cell_formula_changed (Cell *cell)
{
	g_return_if_fail (cell != NULL);
30

31
	sheet_cell_formula_link (cell);
32

33
34
35
	cell_queue_recalc (cell);
}

36
37
38
static inline void
cell_modified (Cell *cell)
{
39
	Sheet *sheet = cell->sheet;
40
41
42
43

	/* Cells from the clipboard do not have a sheet attached */
	if (sheet)
		sheet->modified = TRUE;
44
45
}

Morten Welinder's avatar
Morten Welinder committed
46

47
/* Empty a cell's value, entered_text, and parsed_node.  */
Jody Goldberg's avatar
typo.    
Jody Goldberg committed
48
static void
49
50
51
cell_cleanout (Cell *cell)
{
	if (cell->parsed_node){
52
53
54
		/* Clipboard cells, e.g., are not attached to a sheet.  */
		if (cell->sheet)
			sheet_cell_formula_unlink (cell);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		expr_tree_unref (cell->parsed_node);
		cell->parsed_node = NULL;
	}

	if (cell->value) {
		value_release (cell->value);
		cell->value = NULL;
	}

	if (cell->entered_text) {
		string_unref (cell->entered_text);
		cell->entered_text = NULL;
	}
}


Jody Goldberg's avatar
typo.    
Jody Goldberg committed
71
void
72
cell_set_formula (Cell *cell, const char *text)
Arturo Espinosa's avatar
Arturo Espinosa committed
73
{
74
	ExprTree *new_expr;
75
	char *error_msg = _("ERROR");
Morten Welinder's avatar
Morten Welinder committed
76
	char *desired_format = NULL;
77
	ParsePosition pp;
Morten Welinder's avatar
Morten Welinder committed
78
	gboolean may_set_format;
79

Arturo Espinosa's avatar
Arturo Espinosa committed
80
81
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
82
	g_return_if_fail (gnumeric_char_start_expr_p (*text));
83

Morten Welinder's avatar
Morten Welinder committed
84
85
	may_set_format = !cell_has_assigned_format (cell);

86
	cell_modified (cell);
87
	new_expr = expr_parse_string (text+1, /* Ignore leading char (=,@,+) */
88
				      parse_pos_cell (&pp, cell),
Morten Welinder's avatar
Morten Welinder committed
89
				      may_set_format ? &desired_format : NULL,
90
				      &error_msg);
91
92
	cell_cleanout (cell);

93
	if (new_expr == NULL) {
94
		cell_set_rendered_text (cell, error_msg);
95
96
97
		cell->entered_text = string_get (text);
		/* FIXME: Supply a proper position?  */
		cell->value = value_new_error (NULL, error_msg);
Arturo Espinosa's avatar
Arturo Espinosa committed
98
		return;
99
100
	}

Morten Welinder's avatar
Morten Welinder committed
101
102
103
	/* Until the value is recomputed, we put in this value.  */
	cell->value = value_new_error (NULL, _("Pending recomputation"));

Morten Welinder's avatar
Morten Welinder committed
104
	if (desired_format) {
105
		cell_set_format (cell, desired_format);
Morten Welinder's avatar
Morten Welinder committed
106
107
		g_free (desired_format);
	}
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
108

109
	if (new_expr->oper == OPER_ARRAY) {
110
		/* The corner sets up the entire array block */
111
		if (new_expr->u.array.x != 0 || new_expr->u.array.y != 0) {
112
113
114
			/* Throw away the expression, including the inner
			   one.  */
			expr_tree_unref (new_expr->u.array.corner.func.expr);
115
116
117
118
			expr_tree_unref (new_expr);
			return;
		}

119
		/*
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
		 * NOTE : The wrapper supplied by the parser will be released
		 *        and recreated.  new_expr will NOT be valid on exit
		 *        from cell_set_array_formula.
		 */
		cell->parsed_node = new_expr;
		cell_set_array_formula (cell->sheet,
					cell->row->pos, cell->col->pos,
					cell->row->pos +
					    new_expr->u.array.rows -1,
					cell->col->pos +
					    new_expr->u.array.cols -1,
					new_expr->u.array.corner.func.expr);
	} else {
		cell->parsed_node = new_expr;
		cell_formula_changed (cell);
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
136
}
Arturo Espinosa's avatar
Arturo Espinosa committed
137

138
139
140
141
142
void
cell_comment_destroy (Cell *cell)
{
	CellComment *comment;
	GList *l;
143

144
145
146
147
148
149
150
151
152
	g_return_if_fail (cell != NULL);

	comment = cell->comment;
	if (!comment)
		return;
	cell->comment = NULL;

	/* Free resources */
	string_unref (comment->comment);
153
154
155
156
157

	if (comment->timer_tag != -1)
		gtk_timeout_remove (comment->timer_tag);

	if (comment->window)
158
		gtk_object_destroy (GTK_OBJECT (comment->window));
159

160
	for (l = comment->realized_list; l; l = l->next)
161
		gtk_object_destroy (l->data);
Morten Welinder's avatar
Morten Welinder committed
162
	g_list_free (comment->realized_list);
163

164
165
166
	g_free (comment);
}

167
168
169
170
171
172
173
static void
cell_comment_cancel_timer (Cell *cell)
{
	if (cell->comment->timer_tag != -1){
		gtk_timeout_remove (cell->comment->timer_tag);
		cell->comment->timer_tag = -1;
	}
174

175
}
176

177
178
179
180
181
static void
cell_display_comment (Cell *cell)
{
	GtkWidget *window, *label;
	int x, y;
182

183
184
	g_return_if_fail (cell != NULL);

185
	cell_comment_cancel_timer (cell);
186

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	window = gtk_window_new (GTK_WINDOW_POPUP);
	label = gtk_label_new (cell->comment->comment->str);
	gtk_container_add (GTK_CONTAINER (window), label);

	gdk_window_get_pointer (NULL, &x, &y, NULL);
	gtk_widget_set_uposition (window, x+10, y+10);

	gtk_widget_show_all (window);

	cell->comment->window = window;
}

static gint
cell_popup_comment (gpointer data)
{
	Cell *cell = data;
203

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	cell->comment->timer_tag = -1;

	cell_display_comment (cell);
	return FALSE;
}

static int
cell_comment_clicked (GnomeCanvasItem *item, GdkEvent *event, Cell *cell)
{
	GnomeCanvas *canvas = item->canvas;

	switch (event->type){
	case GDK_BUTTON_RELEASE:
		if (event->button.button != 1)
			return FALSE;
		if (cell->comment->window)
			return FALSE;
		cell_display_comment (cell);
		break;

	case GDK_BUTTON_PRESS:
		if (event->button.button != 1)
			return FALSE;
		break;
228

229
230
231
232
233
234
	case GDK_ENTER_NOTIFY:
		cell->comment->timer_tag = gtk_timeout_add (1000, cell_popup_comment, cell);
		cursor_set_widget (canvas, GNUMERIC_CURSOR_ARROW);
		break;

	case GDK_LEAVE_NOTIFY:
235
		cell_comment_cancel_timer (cell);
236
		if (cell->comment->window){
237
			gtk_object_destroy (GTK_OBJECT (cell->comment->window));
238
239
240
			cell->comment->window = NULL;
		}
		break;
241

242
243
244
245
246
247
	default:
		return FALSE;
	}
	return TRUE;
}

248
249
250
251
252
253
254
255
static void
cell_comment_realize (Cell *cell)
{
	GList *l;

	g_return_if_fail (cell->comment != NULL);

	sheet_cell_comment_link (cell);
Morten Welinder's avatar
Morten Welinder committed
256
	for (l = cell->sheet->sheet_views; l; l = l->next){
257
258
		SheetView *sheet_view = SHEET_VIEW (l->data);
		GnomeCanvasItem *o;
259

260
		o = sheet_view_comment_create_marker (
261
			sheet_view,
262
			cell->col->pos, cell->row->pos);
263
		gtk_object_ref (GTK_OBJECT (o));
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

		cell->comment->realized_list = g_list_prepend (
			cell->comment->realized_list, o);

		gtk_signal_connect (GTK_OBJECT (o), "event",
				    GTK_SIGNAL_FUNC (cell_comment_clicked), cell);
	}
}

static void
cell_comment_unrealize (Cell *cell)
{
	GList *l;

	g_return_if_fail (cell->comment != NULL);

	sheet_cell_comment_unlink (cell);
	for (l = cell->comment->realized_list; l; l = l->next){
		GnomeCanvasItem *o = l->data;

284
		gtk_object_unref (GTK_OBJECT (o));
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	}
	g_list_free (cell->comment->realized_list);
	cell->comment->realized_list = NULL;
}

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

	if (cell->comment)
		cell_comment_realize (cell);
}

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

	if (cell->comment)
		cell_comment_unrealize (cell);
}

308
void
309
cell_set_comment (Cell *cell, const char *str)
310
311
{
	int had_comments = FALSE;
312

313
314
315
316
317
318
319
320
321
322
323
	g_return_if_fail (cell != NULL);
	g_return_if_fail (str != NULL);

	cell_modified (cell);

	cell_comment_destroy (cell);

	cell->comment = g_new (CellComment, 1);
	cell->comment->realized_list = NULL;
	cell->comment->timer_tag = -1;
	cell->comment->window = NULL;
324

325
326
327
328
329
	cell->comment->comment = string_get (str);

	if (had_comments)
		cell_queue_redraw (cell);

330
331
	if (cell->sheet)
		cell_comment_realize (cell);
332
333
}

Miguel de Icaza's avatar
Miguel de Icaza committed
334
335
336
337
338
339
340
341
/*
 * 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
342
void
343
cell_set_rendered_text (Cell *cell, const char *rendered_text)
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
344
{
345
	String *oldtext;
346

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
347
348
	g_return_if_fail (cell != NULL);
	g_return_if_fail (rendered_text != NULL);
349

350
	cell_modified (cell);
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
351

352
	oldtext = cell->text;
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
353
	cell->text = string_get (rendered_text);
354
355
	if (oldtext)
		string_unref (oldtext);
356

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
357
358
359
	cell_calc_dimensions (cell);
}

360
char *
361
362
363
364
cell_get_formatted_val (Cell *cell, StyleColor **col)
{
	MStyle *mstyle;
	char *txt;
365

366
367
368
369
370
371
372
373
374
375
376
377
378
	mstyle = sheet_style_compute (cell->sheet, cell->col->pos,
				      cell->row->pos);
	if (mstyle_is_element_set (mstyle, MSTYLE_FORMAT))
		txt = format_value (mstyle_get_format (mstyle),
				    cell->value, col);
	else {
		g_warning ("No format: serious error");
		txt = g_strdup ("Error");
	}
	mstyle_unref (mstyle);
	return txt;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
379
380
381
382
/*
 * cell_render_value
 * @cell: The cell whose value needs to be rendered
 *
383
384
385
 * The value of the cell is formated according to the format style, but if
 * formulas are being displayed then use the text of the formula instead of 
 * its value.
Miguel de Icaza's avatar
Miguel de Icaza committed
386
 */
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
387
388
389
void
cell_render_value (Cell *cell)
{
390
	StyleColor *color;
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
391
	char *str;
392

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
393
394
395
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->value != NULL);

Jody Goldberg's avatar
Jody Goldberg committed
396
	if (cell->render_color == (void *)0xdeadbeef) {
Miguel de Icaza's avatar
Miguel de Icaza committed
397
398
		g_error ("This cell is dead!");
	}
399
	if (cell->render_color) {
400
401
402
		style_color_unref (cell->render_color);
		cell->render_color = NULL;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
403

404
405
406
407
408
409
410
411
412
413
	if (cell->sheet->workbook->display_formulas && cell->parsed_node){
		ParsePosition pp;
		char *tmpstr = expr_decode_tree (cell->parsed_node,
						 parse_pos_cell (&pp, cell));
		str = g_strconcat ("=", tmpstr, NULL);
		g_free (tmpstr);
	} else {
		str = cell_get_formatted_val (cell, &color);		
		cell->render_color = color;
	}
414

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
415
416
417
418
	cell_set_rendered_text (cell, str);
	g_free (str);
}

Michael Meeks's avatar
Michael Meeks committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * Sets the value for a cell:
 *
 * This is kind of an internal function and should be only called by
 * routines that know what they are doing.  These are the important
 * differences from cell_set_value:
 *
 *    - It does not queue redraws (so you have to queue the redraw yourself
 *      or queue a full redraw).
 *
 *    - It does not queue any recomputations.  You have to queue the recompute
 *      yourself.
 */
void
Morten Welinder's avatar
Morten Welinder committed
433
cell_set_value_simple (Cell *cell, Value *v)
Michael Meeks's avatar
Michael Meeks committed
434
435
436
437
438
{
	g_return_if_fail (cell);
	g_return_if_fail (v);

	cell_modified (cell);
439
	cell_cleanout (cell);
Michael Meeks's avatar
Michael Meeks committed
440
441
442
443
444
445
446
447
448
449
450

	cell->value = v;
	cell_render_value (cell);
}

/*
 * cell_set_value
 *
 * Changes the value of a cell
 */
void
Morten Welinder's avatar
Morten Welinder committed
451
cell_set_value (Cell *cell, Value *v)
Michael Meeks's avatar
Michael Meeks committed
452
453
454
{
	g_return_if_fail (cell);
	g_return_if_fail (v);
455

Michael Meeks's avatar
Michael Meeks committed
456
457
458
459
460
461
462
463
	cell_queue_redraw (cell);

	cell_set_value_simple (cell, v);
	cell_content_changed (cell);

	cell_queue_redraw (cell);
}

464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * Sets the text for a cell:
 *
 * This is kind of an internal function and should be only called by
 * routines that know what they are doing.  These are the important
 * differences from cell_set_text:
 *
 *    - It does not queue redraws (so you have to queue the redraw yourself
 *      or queue a full redraw).
 *
 *    - It does not queue any recomputations.  You have to queue the recompute
 *      yourself.
 */
477
void
478
cell_set_text_simple (Cell *cell, const char *text)
479
{
Arturo Espinosa's avatar
Arturo Espinosa committed
480
481
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
482

483
	cell_modified (cell);
484
	cell_cleanout (cell);
485

486
	if (gnumeric_char_start_expr_p (*text) && text[1] != '\0')
487
		cell_set_formula (cell, text);
488
	else {
489
490
		char *end;
		long l;
491
		int  set=0;
492

Morten Welinder's avatar
Morten Welinder committed
493
		l = strtol (text, &end, 10);
494
495
		if (l != LONG_MAX && l != LONG_MIN &&
		    text != end && (l == (int)l)) {
496
497
498
			/* Allow and ignore spaces at the end of integers.  */
			while (*end == ' ')
				end++;
499
500
501
502
503
			if (*end == 0) {
				cell->value = value_new_int (l);
				set = 1;
			}
		}
504

505
		if (!set) {
506
			double d;
Morten Welinder's avatar
Morten Welinder committed
507
			d = strtod (text, &end);
508
			if (text != end && *end == 0) {
509
510
				/* It is a floating point number.  */
				cell->value = value_new_float ((float_t)d);
Arturo Espinosa's avatar
Arturo Espinosa committed
511
			} else {
512
513
				/* It is text.  */
				cell->value = value_new_string (text);
Arturo Espinosa's avatar
Arturo Espinosa committed
514
515
			}
		}
516

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
517
		cell_render_value (cell);
Jody Goldberg's avatar
Jody Goldberg committed
518
		cell->entered_text = string_get (text);
519
	}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
}

/*
 * cell_content_changed:
 *
 * Call this routine if you modify the contents of a cell
 * to trigger a recompute on any dependencies
 */
void
cell_content_changed (Cell *cell)
{
	GList   *deps;

	g_return_if_fail (cell != NULL);

Arturo Espinosa's avatar
Arturo Espinosa committed
535
	/* Queue all of the dependencies for this cell */
536
	deps = cell_get_dependencies (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
537
	if (deps)
Morten Welinder's avatar
Morten Welinder committed
538
		cell_queue_recalc_list (deps, TRUE);
539
540
}

541

542
543
544
545
546
547
/*
 * cell_set_text
 *
 * Changes the content of a cell
 */
void
548
cell_set_text (Cell *cell, const char *text)
549
550
551
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (text != NULL);
552

553
	if (cell->parsed_node != NULL && cell->parsed_node->oper == OPER_ARRAY) {
554
555
556
557
		gnumeric_no_modify_array_notice (cell->sheet->workbook);
		return;
	}

558
559
560
561
562
563
564
565
566
567
	if (*text == 0){
		static int warn_shown;

		if (!warn_shown){
			g_warning (
				"Cell value being set to empty string");
			warn_shown = 1;
		}
	}
	
568
569
570
	cell_queue_redraw (cell);

	cell_set_text_simple (cell, text);
571
	cell_content_changed (cell);
572

573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
	cell_queue_redraw (cell);
}

/**
 * cell_set_formula_tree_simple:
 * @cell:    the cell to set the formula to
 * @formula: an expression tree with the formula
 *
 * This is an internal function.  It should be only called by routines that
 * know what they are doing.  These are the important differences from
 * cell_set_formula:
 *
 *   - It does not queue redraws (so you have to queue the redraw yourself
 *     or queue a full redraw).
 */
void
cell_set_formula_tree_simple (Cell *cell, ExprTree *formula)
{
	g_return_if_fail (cell != NULL);
	g_return_if_fail (formula != NULL);

594
	/* Ref before unref.  Repeat after me.  */
Morten Welinder's avatar
Morten Welinder committed
595
596
	expr_tree_ref (formula);

597
598
	cell_modified (cell);
	cell_cleanout (cell);
599
600

	cell->parsed_node = formula;
601
602
	/* Until the value is recomputed, we put in this value.  */
	cell->value = value_new_error (NULL, _("Circular reference"));
603
604
605
	cell_formula_changed (cell);
}

606
607
608
609
610
611
612
613
614
/**
 * 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
 *
615
 * Uses cell_set_formula_tree_simple to store the formula as an
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
 * '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
 * out it at the head of the recalc queue.
 */
void
cell_set_array_formula (Sheet *sheet,
			int row_a, int col_a, int row_b, int col_b,
			ExprTree *formula)
{
	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);
	Cell * cell = NULL;
631
	ExprTree *wrapper;
632
633
634
635
636
637
638
639
640
641
642

	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);

	wrapper = expr_tree_array_formula (0, 0, num_rows, num_cols);
	wrapper->u.array.corner.func.value = NULL;
	wrapper->u.array.corner.func.expr = formula;
	expr_tree_ref (formula);
	cell_set_formula_tree_simple (corner, wrapper);
643
	expr_tree_unref (wrapper);
644
645
646
647
648

	/* The corner must be 1st on the recalc list, queue it later */
	cell_unqueue_from_recalc (corner);

	for (x = 0; x < num_cols; ++x)
649
		for (y = 0; y < num_rows; ++y) {
650
651
			if (x == 0 && y == 0)
				continue;
Michael Meeks's avatar
Michael Meeks committed
652
			cell = sheet_cell_fetch (sheet, col_a + x, row_a + y);
653
654
655
656
			wrapper = expr_tree_array_formula (x, y,
							   num_rows, num_cols);
			wrapper->u.array.corner.cell = corner;
			cell_set_formula_tree_simple (cell, wrapper);
657
			expr_tree_unref (wrapper);
658
659
660
661
662
663
		}

	/* Put the corner at the head of the recalc list */
	cell_queue_recalc (corner);
}

664
665
666
667
668
669
670
671
void
cell_set_formula_tree (Cell *cell, ExprTree *formula)
{
	g_return_if_fail (cell != NULL);

	cell_queue_redraw (cell);

	cell_set_formula_tree_simple (cell, formula);
672
	cell_content_changed (cell);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
673
674

	cell_queue_redraw (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
675
}
Arturo Espinosa's avatar
Arturo Espinosa committed
676

677
678
679
680
/**
 * cell_copy:
 * @cell: existing cell to duplicate
 *
681
 * Makes a copy of a Cell.
682
 *
683
 * Returns a copy of the cell.
Arturo Espinosa's avatar
Arturo Espinosa committed
684
685
 */
Cell *
686
cell_copy (const Cell *cell)
Arturo Espinosa's avatar
Arturo Espinosa committed
687
688
689
690
691
692
693
694
695
696
{
	Cell *new_cell;

	g_return_val_if_fail (cell != NULL, NULL);

	new_cell = g_new (Cell, 1);

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

Morten Welinder's avatar
Morten Welinder committed
697
	new_cell->flags &= ~CELL_QUEUED_FOR_RECALC;
698

Morten Welinder's avatar
Morten Welinder committed
699
	/* now copy properly the rest */
700
	if (new_cell->parsed_node)
Morten Welinder's avatar
Morten Welinder committed
701
702
703
704
		expr_tree_ref (new_cell->parsed_node);

	if (new_cell->text)
		string_ref (new_cell->text);
705
706
707
708

	if (new_cell->entered_text)
		string_ref (new_cell->entered_text);

709
710
/*	if (new_cell->style)
	new_cell->style = style_duplicate (new_cell->style);*/
Morten Welinder's avatar
Morten Welinder committed
711
712
713

	if (new_cell->render_color)
		style_color_ref (new_cell->render_color);
714
715
716

	if (new_cell->value)
		new_cell->value = value_duplicate (new_cell->value);
Arturo Espinosa's avatar
Arturo Espinosa committed
717

Morten Welinder's avatar
Morten Welinder committed
718
719
	if (cell->comment) {
		new_cell->comment = NULL;
720
		cell_set_comment (new_cell, cell->comment->comment->str);
Morten Welinder's avatar
Morten Welinder committed
721
	}
722

Arturo Espinosa's avatar
Arturo Espinosa committed
723
724
725
726
727
728
729
730
	return new_cell;
}

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

Morten Welinder's avatar
Morten Welinder committed
731
732
733
734
735
736
	if (cell_hash_queue && g_hash_table_lookup (cell_hash_queue, cell)) {
		g_warning ("FIXME: Deleting cell %s which was queued for redraw",
			   cell_name (cell->col->pos, cell->row->pos));
		g_hash_table_remove (cell_hash_queue, cell);
	}

737
	cell_modified (cell);
738
	cell_cleanout (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
739

740
741
	if (cell->render_color)
		style_color_unref (cell->render_color);
Morten Welinder's avatar
Morten Welinder committed
742
	cell->render_color = (void *)0xdeadbeef;
743

744
	cell_comment_destroy (cell);
745
746

	if (cell->text)
747
		string_unref (cell->text);
Morten Welinder's avatar
Morten Welinder committed
748
749
	cell->text = (void *)0xdeadbeef;

750
/*	cell->style = (void *)0xdeadbeef;*/
751

752
	g_free (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
753
}
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
754

755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
void
cell_freeze_redraws (void)
{
	redraws_frozen++;
	if (redraws_frozen == 1)
		cell_hash_queue = g_hash_table_new (g_direct_hash, g_direct_equal);
}

static void
call_cell_queue_redraw (gpointer key, gpointer value, gpointer user_data)
{
	cell_queue_redraw (value);
}

void
cell_thaw_redraws (void)
{
	redraws_frozen--;
773
	if (redraws_frozen < 0) {
774
775
776
		g_warning ("unbalanced freeze/thaw\n");
		return;
	}
777
	if (redraws_frozen == 0) {
778
779
780
781
782
		g_hash_table_foreach (cell_hash_queue, call_cell_queue_redraw, NULL);
		g_hash_table_destroy (cell_hash_queue);
		cell_hash_queue = NULL;
	}
}
Michael Meeks's avatar
Michael Meeks committed
783
784
785
786
787
788
789
790
791
792
793
794
795
void
cell_deep_freeze_redraws (void)
{
	redraws_deep_frozen++;
}

void
cell_deep_thaw_redraws (void)
{
	redraws_deep_frozen--;
	if (redraws_frozen < 0)
		g_warning ("unbalanced deep freeze/thaw\n");
}
796
797
798
799
800
801
802
803
804

static void
queue_cell (Cell *cell)
{
	if (g_hash_table_lookup (cell_hash_queue, cell))
		return;
	g_hash_table_insert (cell_hash_queue, cell, cell);
}

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
805
806
807
void
cell_queue_redraw (Cell *cell)
{
Michael Meeks's avatar
Michael Meeks committed
808
	/* You wake up dead after a deep freeze */
809
	if (redraws_deep_frozen > 0)
Michael Meeks's avatar
Michael Meeks committed
810
811
		return;

812
	g_return_if_fail (cell != NULL);
813

814
	if (redraws_frozen) {
815
816
817
		queue_cell (cell);
		return;
	}
Morten Welinder's avatar
Morten Welinder committed
818

Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
819
820
821
822
823
	sheet_redraw_cell_region (cell->sheet,
				  cell->col->pos, cell->row->pos,
				  cell->col->pos, cell->row->pos);
}

824
825
826
827
828
829
830
831
832
/*
 * cell_set_format_simple:
 *
 * This routine is similar to cell_set_format, but it does not queue
 * any redraws, nor expects the cell to have a value.
 *
 * Make sure you queue a draw in the future for this cell.
 */
void
833
cell_set_format_simple (Cell *cell, const char *format)
834
{
835
	MStyle *mstyle = mstyle_new ();
836

837
	mstyle_set_format (mstyle, format);
838

839
	cell_set_mstyle (cell, mstyle);
840
841
}

842
843
844
845
846
847
/*
 * cell_set_format:
 *
 * Changes the format for CELL to be FORMAT.  FORMAT should be
 * a number display format as specified on the manual
 */
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
848
void
849
cell_set_format (Cell *cell, const char *format)
Miguel de Icaza's avatar
Today:    
Miguel de Icaza committed
850
{
851
	g_return_if_fail (cell != NULL);
852

853
	cell_set_format_simple (cell, format);
854

855
856
857
	/* re-render the cell text */
	cell_render_value (cell);
	cell_queue_redraw (cell);
858
859
}

860
861
862
863
void
cell_comment_reposition (Cell *cell)
{
	GList *l;
864

865
866
	g_return_if_fail (cell != NULL);
	g_return_if_fail (cell->comment != NULL);
867

868
869
870
871
872
873
874
875
	for (l = cell->comment->realized_list; l; l = l->next){
		GnomeCanvasItem *o = l->data;
		SheetView *sheet_view = GNUMERIC_SHEET (o->canvas)->sheet_view;

		sheet_view_comment_relocate (sheet_view, cell->col->pos, cell->row->pos, o);
	}
}

876
/*
877
 * cell_relocate:
878
879
 * @cell:           The cell that is changing position
 * @check_bonunds : Should expressions be bounds checked.
880
881
 *
 * This routine is used to move a cell to a different location:
882
883
 *
 * Auxiliary items canvas items attached to the cell are moved.
884
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
885
void
886
cell_relocate (Cell *cell, gboolean const check_bounds)
Arturo Espinosa's avatar
Arturo Espinosa committed
887
888
{
	g_return_if_fail (cell != NULL);
889
890

	/* 1. Tag the cell as modified */
891
892
	cell_modified (cell);

893
	/* 2. If the cell contains a formula, relocate the formula */
Michael Meeks's avatar
Michael Meeks committed
894
	if (cell->parsed_node) {
895
		sheet_cell_formula_unlink (cell);
896
897
898
899

		/*
		 * WARNING WARNING WARNING
		 *
900
		 * This will only work if the new array cell has already
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
		 * been inserted.
		 *
		 * WARNING WARNING WARNING
		 */
		/* If cell was part of an array, reset the corner pointer */
		if (cell->parsed_node->oper == OPER_ARRAY) {
			int const x = cell->parsed_node->u.array.x;
			int const y = cell->parsed_node->u.array.y;
			if (x != 0 || y != 0)
				cell->parsed_node->u.array.corner.cell =
					sheet_cell_get (cell->sheet,
							cell->col->pos - x,
							cell->row->pos - y);
		}

916
917
918
919
920
		/* We do not actually need to change any references
		 * the move is from its current location to its current
		 * location.  All the move is doing is a bounds check.
		 */
		if (check_bounds) {
921
922
			ExprRelocateInfo	rinfo;
			EvalPosition 		pos;
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
			ExprTree    	*expr = cell->parsed_node;

			rinfo.origin.start.col =
				rinfo.origin.end.col = cell->col->pos;
			rinfo.origin.start.row =
				rinfo.origin.end.row = cell->row->pos;
			rinfo.origin_sheet = rinfo.target_sheet = cell->sheet;
			rinfo.col_offset = 0;
			rinfo.row_offset = 0;
			expr = expr_relocate (expr, eval_pos_cell (&pos, cell), &rinfo);

			if (expr != NULL) {
				expr_tree_unref (cell->parsed_node);
				cell->parsed_node = expr;
			}
		}

940
		/* The following call also relinks the cell.  */
941
		cell_formula_changed (cell);
942
	}
943

944
	/* 3. Move any auxiliary canvas items */
Miguel de Icaza's avatar
Miguel de Icaza committed
945
946
	if (cell->comment)
		cell_comment_reposition (cell);
Michael Meeks's avatar
Michael Meeks committed
947
948
949

	/* 4. Tag the contents as having changed */
	cell_content_changed (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
950
951
952
953
954
955
956
957
958
959
960
}

/*
 * 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);

961
	/* FIXME: does this work at all?  -- MW */
962
	cell_modified (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
963
}
964
965

int
966
cell_get_horizontal_align (const Cell *cell, int align)
967
{
968
	g_return_val_if_fail (cell != NULL, HALIGN_RIGHT);
969

970
971
972
	if (align == HALIGN_GENERAL) {
		if (cell->value) {
			if (cell->value->type == VALUE_FLOAT ||
973
			    cell->value->type == VALUE_INTEGER)
974
				align = HALIGN_RIGHT;
975
			else
976
				align = HALIGN_LEFT;
977
		} else
978
979
980
981
			align = HALIGN_RIGHT;
	}

	return align;
982
983
}

984
int inline
985
cell_is_number (const Cell *cell)
986
{
987
	return cell->value && VALUE_IS_NUMBER (cell->value);
988
989
990
}

static inline int
991
cell_contents_fit_inside_column (const Cell *cell)
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
{
	if (cell->width < COL_INTERNAL_WIDTH (cell->col))
		return TRUE;
	else
		return FALSE;
}

/*
 * cell_get_span:
 * @cell:   The cell we will examine
 * @col1:   return value: the first column used by this cell
 * @col2:   return value: the last column used by this cell
 *
 * This routine returns the column interval used by a Cell.
 */
void
cell_get_span (Cell *cell, int *col1, int *col2)
{
	Sheet *sheet;
	int align, left;
	int row, pos, margin;
1013
	MStyle *mstyle;
1014

1015
1016
	g_return_if_fail (cell != NULL);

1017
1018
1019
1020
        /*
	 * If the cell is a number, or the text fits inside the column, or the
	 * alignment modes are set to "justify", then we report only one
	 * column is used.
1021
	 */
1022

1023
	if (cell_is_number (cell) ||
1024
	    cell_contents_fit_inside_column (cell)) {
1025
1026
1027
1028
1029
		*col1 = *col2 = cell->col->pos;
		return;
	}

	sheet = cell->sheet;
1030
1031
1032
1033
	mstyle = sheet_style_compute (cell->sheet, cell->col->pos,
				      cell->row->pos);
	align = cell_get_horizontal_align (cell,
					   mstyle_get_align_h (mstyle));
1034
1035
	row   = cell->row->pos;

1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
	if (align == HALIGN_JUSTIFY ||
	    align == HALIGN_FILL ||
	    mstyle_get_fit_in_cell (mstyle) ||
	    mstyle_get_align_v (mstyle) == VALIGN_JUSTIFY) {
		*col1 = *col2 = cell->col->pos;
		mstyle_unref (mstyle);
		return;
	}
	mstyle_unref (mstyle);

	switch (align) {
1047
1048
1049
1050
1051
	case HALIGN_LEFT:
		*col1 = *col2 = cell->col->pos;
		pos = cell->col->pos + 1;
		left = cell->width - COL_INTERNAL_WIDTH (cell->col);
		margin = cell->col->margin_b;
1052

1053
1054
1055
1056
1057
1058
		for (; left > 0 && pos < SHEET_MAX_COLS-1; pos++){
			ColRowInfo *ci;
			Cell *sibling;

			sibling = sheet_cell_get (sheet, pos, row);

Arturo Espinosa's avatar
Arturo Espinosa committed
1059
			if (!cell_is_blank (sibling))
1060
1061
1062
				return;

			ci = sheet_col_get_info (sheet, pos);
1063

1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
			/* The space consumed is:
			 *    - The margin_b from the last column
			 *    - The width of the cell
			 */
			left -= COL_INTERNAL_WIDTH (ci) +
				margin + ci->margin_a;
			margin = ci->margin_b;
			(*col2)++;
		}
		return;
1074

1075
1076
1077
1078
1079
	case HALIGN_RIGHT:
		*col1 = *col2 = cell->col->pos;
		pos = cell->col->pos - 1;
		left = cell->width - COL_INTERNAL_WIDTH (cell->col);
		margin = cell->col->margin_a;
1080

1081
1082
1083
1084
1085
1086
		for (; left > 0 && pos >= 0; pos--){
			ColRowInfo *ci;
			Cell *sibling;

			sibling = sheet_cell_get (sheet, pos, row);

Arturo Espinosa's avatar
Arturo Espinosa committed
1087
			if (!cell_is_blank (sibling))
1088
1089
1090
1091
1092
1093
				return;

			ci = sheet_col_get_info (sheet, pos);

			/* The space consumed is:
			 *   - The margin_a from the last column
Jody Goldberg's avatar
Jody Goldberg committed
1094
			 *   - The width of this cell
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
			 */
			left -= COL_INTERNAL_WIDTH (ci) +
				margin + ci->margin_b;
			margin = ci->margin_a;
			(*col1)--;
		}
		return;

	case HALIGN_CENTER: {
		int left_left, left_right;
		int margin_a, margin_b;
1106

1107
1108
		*col1 = *col2 = cell->col->pos;
		left = cell->width -  COL_INTERNAL_WIDTH (cell->col);
1109

1110
1111
1112
1113
		left_left  = left / 2 + (left % 2);
		left_right = left / 2;
		margin_a = cell->col->margin_a;
		margin_b = cell->col->margin_b;
1114

1115
1116
1117
1118
		for (; left_left > 0 || left_right > 0;){
			ColRowInfo *ci;
			Cell *left_sibling, *right_sibling;

Arturo Espinosa's avatar
Arturo Espinosa committed
1119
			if (*col1 - 1 >= 0){
1120
1121
				left_sibling = sheet_cell_get (sheet, *col1 - 1, row);

Arturo Espinosa's avatar
Arturo Espinosa committed
1122
				if (!cell_is_blank (left_sibling))
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
					left_left = 0;
				else {
					ci = sheet_col_get_info (sheet, *col1 - 1);

					left_left -= COL_INTERNAL_WIDTH (ci) +
						margin_a + ci->margin_b;
					margin_a = ci->margin_a;
					(*col1)--;
				}
			} else
				left_left = 0;

			if (*col2 + 1 < SHEET_MAX_COLS-1){
				right_sibling = sheet_cell_get (sheet, *col2 + 1, row);

Arturo Espinosa's avatar
Arturo Espinosa committed
1138
				if (!cell_is_blank (right_sibling))
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
					left_right = 0;
				else {
					ci = sheet_col_get_info (sheet, *col2 + 1);

					left_right -= COL_INTERNAL_WIDTH (ci) +
						margin_b + ci->margin_a;
					margin_b = ci->margin_b;
					(*col2)++;
				}
			} else
				left_right = 0;
1150

1151
		} /* for */
Arturo Espinosa's avatar
Arturo Espinosa committed
1152
		break;
1153