sheet.c 31.4 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1 2 3 4 5 6 7 8
/*
 * Sheet.c:  Implements the sheet management and per-sheet storage
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *
 */
9
#include <config.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
10 11 12
#include <gnome.h>
#include "gnumeric.h"
#include "gnumeric-sheet.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "utils.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
14

15 16 17
static void sheet_selection_col_extend_to (Sheet *sheet, int col);
static void sheet_selection_row_extend_to (Sheet *sheet, int row);

Arturo Espinosa's avatar
Arturo Espinosa committed
18
void
Arturo Espinosa's avatar
Arturo Espinosa committed
19 20 21 22 23 24 25
sheet_redraw_all (Sheet *sheet)
{
	gnome_canvas_request_redraw (
		GNOME_CANVAS (sheet->sheet_view),
		0, 0, INT_MAX, INT_MAX);
}

26
static void
Arturo Espinosa's avatar
Arturo Espinosa committed
27 28 29 30 31
sheet_init_default_styles (Sheet *sheet)
{
	/* The default column style */
	sheet->default_col_style.pos        = -1;
	sheet->default_col_style.style      = style_new ();
Arturo Espinosa's avatar
Arturo Espinosa committed
32
	sheet->default_col_style.units      = 80;
Arturo Espinosa's avatar
Arturo Espinosa committed
33
	sheet->default_col_style.pixels     = 0;
34 35
	sheet->default_col_style.margin_a   = 1;
	sheet->default_col_style.margin_b   = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
36
	sheet->default_col_style.selected   = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
37
	sheet->default_col_style.data       = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
38 39 40 41 42 43

	/* The default row style */
	sheet->default_row_style.pos      = -1;
	sheet->default_row_style.style    = style_new ();
	sheet->default_row_style.units    = 20;
	sheet->default_row_style.pixels   = 0;
44 45
	sheet->default_row_style.margin_a = 1;
	sheet->default_row_style.margin_b = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
46
	sheet->default_row_style.selected = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
47
	sheet->default_row_style.data     = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
48 49 50
}

/* Initialize some of the columns and rows, to test the display engine */
51 52 53
static void
sheet_init_dummy_stuff (Sheet *sheet)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
54
	ColRowInfo *cp, *rp;
55 56 57
	int x, y;

	for (x = 0; x < 40; x += 2){
Arturo Espinosa's avatar
Arturo Espinosa committed
58
		cp = sheet_row_new (sheet);
59 60
		cp->pos = x;
		cp->units = (x+1) * 30;
Arturo Espinosa's avatar
Arturo Espinosa committed
61
		sheet_col_add (sheet, cp);
62 63 64
	}

	for (y = 0; y < 6; y += 2){
Arturo Espinosa's avatar
Arturo Espinosa committed
65
		rp = sheet_row_new (sheet);
66 67
		rp->pos = y;
		rp->units = (20 * (y + 1));
68
		rp->selected = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
69
		sheet_row_add (sheet, rp);
70 71 72
	}
}

73 74 75 76 77 78 79
static void
canvas_bar_realized (GtkWidget *widget, gpointer data)
{
	/* MIGUEL: look at this */
	gdk_window_set_back_pixmap (GTK_LAYOUT (widget)->bin_window, NULL, FALSE);
}

80 81 82 83 84 85 86 87
static GtkWidget *
new_canvas_bar (Sheet *sheet, GtkOrientation o, GnomeCanvasItem **itemp)
{
	GnomeCanvasGroup *group;
	GnomeCanvasItem *item;
	GtkWidget *canvas;
	int w, h;
	
88
	canvas = gnome_canvas_new ();
89

90 91 92 93 94 95
	gtk_signal_connect (GTK_OBJECT (canvas), "realize",
			    (GtkSignalFunc) canvas_bar_realized,
			    NULL);

	/* FIXME: need to set the correct scrolling regions.  This will do for now */

96 97 98
	if (o == GTK_ORIENTATION_VERTICAL){
		w = 60;
		h = 1;
99
		gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas), 0, 0, w, 1000000);
100 101 102
	} else {
		w = 1;
		h = 20;
103
		gnome_canvas_set_scroll_region (GNOME_CANVAS (canvas), 0, 0, 1000000, h);
104 105 106
	}
	gnome_canvas_set_size (GNOME_CANVAS (canvas), w, h);
	group = GNOME_CANVAS_GROUP (GNOME_CANVAS (canvas)->root);
107
	item = gnome_canvas_item_new (group,
108 109 110 111 112
				      item_bar_get_type (),
				      "ItemBar::Sheet", sheet,
				      "ItemBar::Orientation", o,
				      "ItemBar::First", 0,
				      NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
113

114 115 116 117 118
	*itemp = GNOME_CANVAS_ITEM (item);
	gtk_widget_show (canvas);
	return canvas;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
119
static void
Arturo Espinosa's avatar
Arturo Espinosa committed
120
sheet_col_selection_changed (ItemBar *item_bar, int column, int reset, Sheet *sheet)
Arturo Espinosa's avatar
Arturo Espinosa committed
121
{
122 123 124 125
	ColRowInfo *ci;

	ci = sheet_col_get (sheet, column);
	
126 127 128 129 130 131 132 133 134 135
	if (reset){
		gnumeric_sheet_cursor_set (GNUMERIC_SHEET (sheet->sheet_view), column, 0);
		sheet_selection_clear (sheet);
		sheet_selection_append_range (sheet,
					      column, 0,
					      column, 0,
					      column, SHEET_MAX_ROWS-1);
		sheet_col_set_selection (sheet, ci, 1);
	} else
		sheet_selection_col_extend_to (sheet, column);
Arturo Espinosa's avatar
Arturo Espinosa committed
136 137 138 139 140 141
}

static void
sheet_col_size_changed (ItemBar *item_bar, int col, int width, Sheet *sheet)
{
	sheet_col_set_width (sheet, col, width);
142
	gnumeric_sheet_compute_visible_ranges (GNUMERIC_SHEET (sheet->sheet_view));
Arturo Espinosa's avatar
Arturo Espinosa committed
143 144 145
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
146
sheet_row_selection_changed (ItemBar *item_bar, int row, int reset, Sheet *sheet)
Arturo Espinosa's avatar
Arturo Espinosa committed
147
{
148 149 150 151
	ColRowInfo *ri;

	ri = sheet_row_get (sheet, row);

152 153 154 155 156 157 158 159 160 161
	if (reset){
		gnumeric_sheet_cursor_set (GNUMERIC_SHEET (sheet->sheet_view), 0, row);
		sheet_selection_clear (sheet);
		sheet_selection_append_range (sheet,
					      0, row,
					      0, row,
					      SHEET_MAX_COLS-1, row);
		sheet_row_set_selection (sheet, ri, 1);
	} else
		sheet_selection_row_extend_to (sheet, row);
Arturo Espinosa's avatar
Arturo Espinosa committed
162 163 164 165 166 167
}

static void
sheet_row_size_changed (ItemBar *item_bar, int row, int height, Sheet *sheet)
{
	sheet_row_set_height (sheet, row, height);
168
	gnumeric_sheet_compute_visible_ranges (GNUMERIC_SHEET (sheet->sheet_view));
Arturo Espinosa's avatar
Arturo Espinosa committed
169 170
}

171 172 173
static guint
cell_hash (gconstpointer key)
{
174
	CellPos *ca = (CellPos *) key;
175 176 177 178 179 180 181

	return (ca->row << 8) | ca->col;
}

static gint
cell_compare (gconstpointer a, gconstpointer b)
{
182
	CellPos *ca, *cb;
183

184 185
	ca = (CellPos *) a;
	cb = (CellPos *) b;
186 187 188 189 190 191 192 193 194

	if (ca->row != cb->row)
		return 0;
	if (ca->col != cb->col)
		return 0;
	
	return 1;
}

195 196 197 198 199 200
static void
button_select_all (GtkWidget *the_button, Sheet *sheet)
{
	sheet_select_all (sheet);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
201 202 203
Sheet *
sheet_new (Workbook *wb, char *name)
{
204
	int rows_shown, cols_shown;
205 206
	GtkWidget *select_all;
	Sheet *sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
207

208 209
	rows_shown = cols_shown = 40;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
210
	sheet = g_new0 (Sheet, 1);
211
	sheet->signature = SHEET_SIGNATURE;
Arturo Espinosa's avatar
Arturo Espinosa committed
212
	sheet->workbook = wb;
Arturo Espinosa's avatar
Arturo Espinosa committed
213
	sheet->name = g_strdup (name);
214 215
	sheet->last_zoom_factor_used = -1.0;
	sheet->toplevel = gtk_table_new (0, 0, 0);
216 217
	sheet->max_col_used = cols_shown;
	sheet->max_row_used = rows_shown;
218

219
	sheet->cell_hash = g_hash_table_new (cell_hash, cell_compare);
Arturo Espinosa's avatar
Arturo Espinosa committed
220 221
	sheet_init_default_styles (sheet);
	
222
	/* Dummy initialization */
Arturo Espinosa's avatar
Arturo Espinosa committed
223 224
	if (0)
		sheet_init_dummy_stuff (sheet);
225

226 227 228 229
	/* Column canvas */
	sheet->col_canvas = new_canvas_bar (sheet, GTK_ORIENTATION_HORIZONTAL, &sheet->col_item);
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->col_canvas,
			  1, 2, 0, 1,
230 231 232
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  GTK_FILL,
			  0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
233 234 235 236 237 238
	gtk_signal_connect (GTK_OBJECT (sheet->col_item), "selection_changed",
			    GTK_SIGNAL_FUNC (sheet_col_selection_changed),
			    sheet);
	gtk_signal_connect (GTK_OBJECT (sheet->col_item), "size_changed",
			    GTK_SIGNAL_FUNC (sheet_col_size_changed),
			    sheet);
239 240 241 242
	/* Row canvas */
	sheet->row_canvas = new_canvas_bar (sheet, GTK_ORIENTATION_VERTICAL, &sheet->row_item);
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->row_canvas,
			  0, 1, 1, 2,
243 244 245
			  GTK_FILL,
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
246 247 248 249 250 251
	gtk_signal_connect (GTK_OBJECT (sheet->row_item), "selection_changed",
			    GTK_SIGNAL_FUNC (sheet_row_selection_changed),
			    sheet);
	gtk_signal_connect (GTK_OBJECT (sheet->row_item), "size_changed",
			    GTK_SIGNAL_FUNC (sheet_row_size_changed),
			    sheet);
252

Arturo Espinosa's avatar
Arturo Espinosa committed
253
	/* Create the gnumeric sheet and set the initial selection */
254 255 256
	sheet->sheet_view = gnumeric_sheet_new (sheet,
						ITEM_BAR (sheet->col_item),
						ITEM_BAR (sheet->row_item));
Arturo Espinosa's avatar
Arturo Espinosa committed
257 258 259 260 261 262 263
	sheet_selection_append (sheet, 0, 0);
	
	gtk_widget_show (sheet->sheet_view);
	gtk_widget_show (sheet->toplevel);
	
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->sheet_view,
			  1, 2, 1, 2,
264 265 266
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
267

268 269 270 271
	/* The select-all button */
	select_all = gtk_button_new ();
	gtk_table_attach (GTK_TABLE (sheet->toplevel), select_all,
			  0, 1, 0, 1,
272 273
			  GTK_FILL,
			  GTK_FILL,
274 275 276 277
			  0, 0);
	gtk_signal_connect (GTK_OBJECT (select_all), "clicked",
			    GTK_SIGNAL_FUNC (button_select_all), sheet);
	
278 279 280 281 282 283 284 285 286 287
	/* Scroll bars and their adjustments */
	sheet->va = gtk_adjustment_new (0.0, 0.0, sheet->max_row_used, 1.0, rows_shown, 1.0);
	sheet->ha = gtk_adjustment_new (0.0, 0.0, sheet->max_col_used, 1.0, cols_shown, 1.0);
	
	sheet->hs = gtk_hscrollbar_new (GTK_ADJUSTMENT (sheet->ha));
	sheet->vs = gtk_vscrollbar_new (GTK_ADJUSTMENT (sheet->va));

	/* Attach the horizontal scroll */
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->hs,
			  1, 2, 2, 3,
288 289 290
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  GTK_FILL,
			  0, 0);
291 292 293 294

	/* Attach the vertical scroll */
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->vs,
			  2, 3, 1, 2,
295 296 297
			  GTK_FILL,
			  GTK_EXPAND | GTK_FILL | GTK_SHRINK,
			  0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
298
	
299
	sheet_set_zoom_factor (sheet, 1.0);
Arturo Espinosa's avatar
Arturo Espinosa committed
300 301 302
	return sheet;
}

303 304 305 306 307 308
static void
cell_hash_free_key (gpointer key, gpointer value, gpointer user_data)
{
	g_free (key);
}
	
309 310 311 312
void
sheet_destroy (Sheet *sheet)
{
	g_assert (sheet != NULL);
313 314
	g_return_if_fail (IS_SHEET (sheet)); 

315 316 317 318 319
	sheet_selection_clear (sheet);
	g_free (sheet->name);
	
	style_destroy (sheet->default_row_style.style);
	style_destroy (sheet->default_col_style.style);
320 321

	g_hash_table_foreach (sheet->cell_hash, cell_hash_free_key, NULL);
322
	gtk_widget_destroy (sheet->toplevel);
323 324
	
	sheet->signature = 0;
325 326 327
	g_free (sheet);
}

328
void
329
sheet_foreach_col (Sheet *sheet, sheet_col_row_callback callback, void *user_data)
330 331 332 333 334 335 336 337 338 339 340 341 342 343
{
	GList *l = sheet->cols_info;

	/* Invoke the callback for the default style */
	(*callback)(sheet, &sheet->default_col_style, user_data);

	/* And then for the rest */
	while (l){
		(*callback)(sheet, l->data, user_data);
		l = l->next;
	}
}

void
344
sheet_foreach_row (Sheet *sheet, sheet_col_row_callback callback, void *user_data)
345 346 347 348 349 350 351 352 353 354 355 356 357 358
{
	GList *l = sheet->rows_info;

	/* Invoke the callback for the default style */
	(callback)(sheet, &sheet->default_row_style, user_data);

	/* And then for the rest */
	while (l){
		(*callback)(sheet, l->data, user_data);
		l = l->next;
	}
}

static void
359
sheet_compute_col_row_new_size (Sheet *sheet, ColRowInfo *ci, void *data)
360 361 362
{
	double pix_per_unit = sheet->last_zoom_factor_used;

363 364
	ci->pixels = (ci->units * pix_per_unit) +
		ci->margin_a + ci->margin_b + 1;
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}

/*
 * Updates the values used for display after a zoom change
 */
static void
sheet_reconfigure_zoom (Sheet *sheet)
{
	GnomeCanvas *gnumeric_canvas = GNOME_CANVAS (sheet->sheet_view);
	double pixels_per_unit = gnumeric_canvas->pixels_per_unit;
	
	if (pixels_per_unit == sheet->last_zoom_factor_used)
		return;

	sheet->last_zoom_factor_used = pixels_per_unit;
380 381
	sheet_foreach_col (sheet, sheet_compute_col_row_new_size, NULL);
	sheet_foreach_row (sheet, sheet_compute_col_row_new_size, NULL);
382
	g_warning ("Need to recompute string lenghts of cells\n");
383 384 385 386 387 388 389 390 391
}

void
sheet_set_zoom_factor (Sheet *sheet, double factor)
{
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->sheet_view), factor);
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->col_canvas), factor);
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->row_canvas), factor);
	sheet_reconfigure_zoom (sheet);
392 393 394
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->sheet_view), 0, 0);
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->col_canvas), 0, 0);
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->row_canvas), 0, 0);
395 396
}

397
ColRowInfo *
Arturo Espinosa's avatar
Arturo Espinosa committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
sheet_duplicate_colrow (ColRowInfo *original)
{
	ColRowInfo *info = g_new (ColRowInfo, 1);

	*info = *original;
	
	info->style = style_duplicate (original->style);
	
	return info;
}

ColRowInfo *
sheet_row_new (Sheet *sheet)
{
	return sheet_duplicate_colrow (&sheet->default_row_style);
}

ColRowInfo *
sheet_col_new (Sheet *sheet)
{
	return sheet_duplicate_colrow (&sheet->default_col_style);
}


Arturo Espinosa's avatar
Arturo Espinosa committed
422 423 424 425 426 427 428 429 430
static gint
CRsort (gconstpointer a, gconstpointer b)
{
	ColRowInfo *ia = (ColRowInfo *) a;
	ColRowInfo *ib = (ColRowInfo *) b;

	return (ia->pos - ib->pos);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
431 432 433
void
sheet_col_add (Sheet *sheet, ColRowInfo *cp)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
434
	sheet->cols_info = g_list_insert_sorted (sheet->cols_info, cp, CRsort);
Arturo Espinosa's avatar
Arturo Espinosa committed
435 436 437 438 439
}

void
sheet_row_add (Sheet *sheet, ColRowInfo *rp)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
440
	sheet->rows_info = g_list_insert_sorted (sheet->rows_info, rp, CRsort);
Arturo Espinosa's avatar
Arturo Espinosa committed
441 442 443 444
}

ColRowInfo *
sheet_col_get_info (Sheet *sheet, int col)
445
{
446
	GList *l = sheet->cols_info;
447

448
	for (; l; l = l->next){
449
		ColRowInfo *ci = l->data;
450

451
		if (ci->pos == col)
452 453 454 455
			return ci;
	}

	return &sheet->default_col_style;
456 457
}

458
ColRowInfo *
Arturo Espinosa's avatar
Arturo Espinosa committed
459
sheet_row_get_info (Sheet *sheet, int row)
460
{
461
	GList *l = sheet->rows_info;
462

463
	for (; l; l = l->next){
464
		ColRowInfo *ri = l->data;
465

466
		if (ri->pos == row)
467 468
			return ri;
	}
469

470
	return &sheet->default_row_style;
471 472
}

Arturo Espinosa's avatar
Arturo Espinosa committed
473 474 475 476 477 478
static void
colrow_set_units (Sheet *sheet,ColRowInfo *info)
{
	double pix = sheet->last_zoom_factor_used;
	
	info->units  = (info->pixels -
479
			(info->margin_a + info->margin_b + 1)) / pix;
Arturo Espinosa's avatar
Arturo Espinosa committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
}

void
sheet_row_set_height (Sheet *sheet, int row, int height)
{
	ColRowInfo *ri;
	int add = 0;
	
	ri = sheet_row_get_info (sheet, row);
	if (ri == &sheet->default_row_style){
		ri = sheet_duplicate_colrow (ri);
		add = 1;
	}

	ri->pos = row;
	ri->pixels = height;
	colrow_set_units (sheet, ri);
	if (add)
498
		sheet_row_add (sheet, ri);
Arturo Espinosa's avatar
Arturo Espinosa committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	sheet_redraw_all (sheet);
}

void
sheet_col_set_width (Sheet *sheet, int col, int width)
{
	ColRowInfo *ci;
	int add = 0;
	
	ci = sheet_col_get_info (sheet, col);
	if (ci == &sheet->default_col_style){
		ci = sheet_duplicate_colrow (ci);
		add = 1;
	}

	ci->pixels = width;
	ci->pos = col;
	colrow_set_units (sheet, ci);
	if (add)
		sheet_col_add (sheet, ci);

	sheet_redraw_all (sheet);
}

523 524 525 526 527 528
/*
 * Return the number of pixels between from_col to to_col
 */
int
sheet_col_get_distance (Sheet *sheet, int from_col, int to_col)
{
529
	ColRowInfo *ci;
530 531
	int pixels = 0, i;

532
	g_assert (sheet);
533
	g_assert (from_col <= to_col);
534 535

	/* This can be optimized, yes, but the implementation
536
	 * of the ColRowInfo sets is going to change anyways
537
	 */
538
	for (i = from_col; i < to_col; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
539
		ci = sheet_col_get_info (sheet, i);
540
		pixels += ci->pixels;
541 542 543 544 545 546 547 548 549 550
	}
	return pixels;
}

/*
 * Return the number of pixels between from_row to to_row
 */
int
sheet_row_get_distance (Sheet *sheet, int from_row, int to_row)
{
551
	ColRowInfo *ri;
552 553 554 555
	int pixels = 0, i;

	g_assert (from_row <= to_row);
	
556 557 558
	/* This can be optimized, yes, but the implementation
	 * of the RowInfo, ColInfo sets is going to change anyways
	 */
559
	for (i = from_row; i < to_row; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
560
		ri = sheet_row_get_info (sheet, i);
561
		pixels += ri->pixels;
562 563 564
	}
	return pixels;
}
565 566 567 568

void
sheet_get_cell_bounds (Sheet *sheet, ColType col, RowType row, int *x, int *y, int *w, int *h)
{
569 570 571
	GnumericSheet *gsheet;

	g_return_if_fail (sheet != NULL);
572
	g_return_if_fail (IS_SHEET (sheet)); 
573
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet->sheet_view));
574
	
575
	gsheet = GNUMERIC_SHEET (sheet->sheet_view);
576 577 578 579 580 581
	*x = sheet_col_get_distance (sheet, gsheet->top_col, col);
	*y = sheet_row_get_distance (sheet, gsheet->top_row, row);

	*w = sheet_col_get_distance (sheet, col, col + 1);
	*h = sheet_row_get_distance (sheet, row, row + 1);
}
582

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
int
sheet_selection_equal (SheetSelection *a, SheetSelection *b)
{
	if (a->start_col != b->start_col)
		return 0;
	if (a->start_row != b->start_row)
		return 0;
	
	if (a->end_col != b->end_col)
		return 0;
	if (a->end_row != b->end_row)
		return 0;
	return 1;
}

598 599
void
sheet_update_auto_expr (Sheet *sheet)
Arturo Espinosa's avatar
Arturo Espinosa committed
600
{
Arturo Espinosa's avatar
Arturo Espinosa committed
601
	Workbook *wb = sheet->workbook;
Arturo Espinosa's avatar
Arturo Espinosa committed
602 603
	Value *v;
	char  *error;
604 605 606 607 608 609 610 611 612

	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));
		
	/* defaults */
	v = NULL;
	error = "ERROR";
	if (wb->auto_expr)
		v = eval_expr (sheet, wb->auto_expr, 0, 0, &error);
Arturo Espinosa's avatar
Arturo Espinosa committed
613 614 615 616 617
	
	if (v){
		char *s;

		s = value_string (v);
618
		workbook_auto_expr_label_set (wb, s);
Arturo Espinosa's avatar
Arturo Espinosa committed
619 620 621
		g_free (s);
		value_release (v);
	} else
622 623 624 625 626 627 628
		workbook_auto_expr_label_set (wb, error);
}

static void
sheet_selection_changed_hook (Sheet *sheet)
{
	sheet_update_auto_expr (sheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
629 630
}

631
void
632 633 634 635
sheet_selection_append_range (Sheet *sheet,
			      int base_col,  int base_row,
			      int start_col, int start_row,
			      int end_col,   int end_row)
636 637 638
{
	SheetSelection *ss;

639
	g_return_if_fail (sheet != NULL);
640
	g_return_if_fail (IS_SHEET (sheet)); 
641
	
642 643
	ss = g_new0 (SheetSelection, 1);

644 645
	ss->base_col  = base_col;
	ss->base_row  = base_row;
646

647 648 649 650
	ss->start_col = start_col;
	ss->end_col   = end_col;
	ss->start_row = start_row;
	ss->end_row   = end_row;
651 652 653
	
	sheet->selections = g_list_prepend (sheet->selections, ss);

Arturo Espinosa's avatar
Arturo Espinosa committed
654 655 656
	gnumeric_sheet_accept_pending_output (GNUMERIC_SHEET (sheet->sheet_view));
	gnumeric_sheet_load_cell_val (GNUMERIC_SHEET (sheet->sheet_view));
	
657 658
	gnumeric_sheet_set_selection (GNUMERIC_SHEET (sheet->sheet_view), ss);
	sheet_redraw_selection (sheet, ss);
Arturo Espinosa's avatar
Arturo Espinosa committed
659 660

	sheet_selection_changed_hook (sheet);
661 662
}

663 664 665 666 667 668
void
sheet_selection_append (Sheet *sheet, int col, int row)
{
	sheet_selection_append_range (sheet, col, row, col, row, col, row);
}

669 670 671 672 673 674 675 676
/*
 * sheet_selection_extend_to
 * @sheet: the sheet
 * @col:   column that gets covered
 * @row:   row that gets covered
 *
 * This extends the selection to cover col, row
 */
677 678 679
void
sheet_selection_extend_to (Sheet *sheet, int col, int row)
{
680 681 682
	SheetSelection *ss, old_selection;

	g_return_if_fail (sheet != NULL);
683 684
	g_return_if_fail (IS_SHEET (sheet));
	
685
	g_assert (sheet->selections);
686

687 688
	ss = (SheetSelection *) sheet->selections->data;

689 690
	old_selection = *ss;
	
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	if (col < ss->base_col){
		ss->start_col = col;
		ss->end_col   = ss->base_col;
	} else {
		ss->start_col = ss->base_col;
		ss->end_col   = col;
	}

	if (row < ss->base_row){
		ss->end_row   = ss->base_row;
		ss->start_row = row;
	} else {
		ss->end_row   = row;
		ss->start_row = ss->base_row;
	}

707
	gnumeric_sheet_set_selection (GNUMERIC_SHEET (sheet->sheet_view), ss);
Arturo Espinosa's avatar
Arturo Espinosa committed
708 709
	sheet_selection_changed_hook (sheet);
	
710 711 712 713
	sheet_redraw_selection (sheet, &old_selection);
	sheet_redraw_selection (sheet, ss);
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
/*
 * sheet_selection_col_extend_to
 * @sheet: the sheet
 * @col:   column that gets covered
 *
 * Special version of sheet_selection_extend_to that
 * is used by the column marking to keep the
 * ColRowInfo->selected flag in sync
 */
void
sheet_selection_col_extend_to (Sheet *sheet, int col)
{
	SheetSelection *ss, old_selection;
	GList *cols;
	int max_col, min_col, state;
		
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

	g_assert (sheet->selections);

	ss = (SheetSelection *) sheet->selections->data;
	old_selection = *ss;

	sheet_selection_extend_to (sheet, col, SHEET_MAX_ROWS-1);

	min_col = MIN (old_selection.start_col, ss->start_col);
	max_col = MAX (old_selection.end_col, ss->end_col);
	
	for (cols = sheet->cols_info; cols; cols = cols->next){
		ColRowInfo *ci = cols->data;

		if (ci->pos < min_col)
			continue;
		if (ci->pos > max_col)
			break;

		if ((ci->pos < ss->start_col) || 
		    (ci->pos > ss->end_col))
			state = 0;
		else
			state = 1;
		sheet_col_set_selection (sheet, ci, state);
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
758
	sheet_selection_changed_hook (sheet);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
}

/*
 * sheet_selection_row_extend_to
 * @sheet: the sheet
 * @row:   row that gets covered
 *
 * Special version of sheet_selection_extend_to that
 * is used by the row marking to keep the ColRowInfo->selected
 * flag in sync. 
 */
static void
sheet_selection_row_extend_to (Sheet *sheet, int row)
{
	SheetSelection *ss, old_selection;
	GList *rows;
	int min_row, max_row, state;
	
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

	g_assert (sheet->selections);

	ss = (SheetSelection *) sheet->selections->data;
	old_selection = *ss;

	sheet_selection_extend_to (sheet, SHEET_MAX_COLS-1, row);

	min_row = MIN (old_selection.start_row, ss->start_row);
	max_row = MAX (old_selection.end_row, ss->end_row);
	
	for (rows = sheet->rows_info; rows; rows = rows->next){
		ColRowInfo *ri = rows->data;

		if (ri->pos < min_row)
			continue;
		if (ri->pos > max_row)
			break;

		if ((ri->pos < ss->start_col) ||
		    (ri->pos > ss->end_col))
			state = 0;
		else
			state = 1;

		sheet_row_set_selection (sheet, ri, state);
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
806
	sheet_selection_changed_hook (sheet);
807 808
}

809 810 811
void
sheet_row_set_selection (Sheet *sheet, ColRowInfo *ri, int value)
{
812 813 814 815
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));
	g_return_if_fail (ri != NULL);
	
816 817 818 819 820 821 822 823 824 825 826
	if (ri->selected == value)
		return;
	
	ri->selected = value;
	gnome_canvas_request_redraw (GNOME_CANVAS (sheet->row_canvas),
		0, 0, INT_MAX, INT_MAX);
}

void
sheet_col_set_selection (Sheet *sheet, ColRowInfo *ci, int value)
{
827 828 829 830
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));
	g_return_if_fail (ci != NULL);
	
831 832 833 834 835 836 837 838
	if (ci->selected == value)
		return;
	
	ci->selected = value;
	gnome_canvas_request_redraw (GNOME_CANVAS (sheet->col_canvas),
		0, 0, INT_MAX, INT_MAX);
}

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/* sheet_select_all
 * Sheet: The sheet
 *
 * Selects all of the cells in the sheet
 */
void
sheet_select_all (Sheet *sheet)
{
	GList *l;
	
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

	sheet_selection_clear_only (sheet);
	gnumeric_sheet_cursor_set (GNUMERIC_SHEET (sheet->sheet_view), 0, 0);
	sheet_selection_append_range (sheet, 0, 0, 0, 0,
		SHEET_MAX_COLS-1, SHEET_MAX_ROWS-1);

	for (l = sheet->cols_info; l; l = l->next){
		ColRowInfo *ci = (ColRowInfo *)l->data;

		sheet_col_set_selection (sheet, ci, 1);
	}

	for (l = sheet->rows_info; l; l = l->next){
		ColRowInfo *ri = (ColRowInfo *)l->data;

		sheet_row_set_selection (sheet, ri, 1);
	}
}

870 871 872 873 874 875 876 877
void
sheet_redraw_cell_region (Sheet *sheet, int start_col, int start_row,
			  int end_col, int end_row)
{
	GnumericSheet *gsheet;
	int x, y, w, h;
	
	g_return_if_fail (sheet != NULL);
878
	g_return_if_fail (IS_SHEET (sheet)); 
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

	gsheet = GNUMERIC_SHEET (sheet->sheet_view);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	
	x = sheet_col_get_distance (sheet, gsheet->top_col, start_col);
	y = sheet_row_get_distance (sheet, gsheet->top_row, start_row);
	w = sheet_col_get_distance (sheet, start_col, end_col+1);
	h = sheet_col_get_distance (sheet, start_col, end_col+1);

	gnome_canvas_request_redraw (GNOME_CANVAS (gsheet), x, y, x+w, y+h);
}

void
sheet_redraw_selection (Sheet *sheet, SheetSelection *ss)
{
	g_return_if_fail (sheet != NULL);
895
	g_return_if_fail (IS_SHEET (sheet)); 
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	g_return_if_fail (ss != NULL);
	
	sheet_redraw_cell_region (sheet,
				  ss->start_col, ss->start_row,
				  ss->end_col, ss->end_row);
}

int
sheet_row_check_bound (int row, int diff)
{
	int new_val = row + diff;

	if (new_val < 0)
		return 0;
	if (new_val >= SHEET_MAX_ROWS)
		return SHEET_MAX_ROWS - 1;

	return new_val;
}

int
sheet_col_check_bound (int col, int diff)
{
	int new_val = col + diff;

	if (new_val < 0)
		return 0;
	if (new_val >= SHEET_MAX_COLS)
		return SHEET_MAX_COLS - 1;

	return new_val;
}

static void
sheet_selection_change (Sheet *sheet, SheetSelection *old, SheetSelection *new)
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (sheet->sheet_view);
	
	if (sheet_selection_equal (old, new))
		return;
		
	gnumeric_sheet_accept_pending_output (gsheet);
	sheet_redraw_selection (sheet, old);
	sheet_redraw_selection (sheet, new);
Arturo Espinosa's avatar
Arturo Espinosa committed
940
	sheet_selection_changed_hook (sheet);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	
	gnumeric_sheet_set_selection (gsheet, new);
}

/*
 * sheet_selection_extend_horizontal
 * @sheet:  The Sheet *
 * @count:  units to extend the selection horizontally
 */
void
sheet_selection_extend_horizontal (Sheet *sheet, int n)
{
	SheetSelection *ss;
	SheetSelection old_selection;

	/* FIXME: right now we only support units (1 or -1)
	 * to fix this we need to account for the fact that
	 * the selection boundary might change and adjust
	 * appropiately
	 */
	 
	g_return_if_fail (sheet != NULL);
963
	g_return_if_fail (IS_SHEET (sheet)); 
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	g_return_if_fail ((n == 1 || n == -1));
	
	ss = (SheetSelection *)sheet->selections->data;
	old_selection = *ss;
	
	if (ss->base_col < ss->end_col)
		ss->end_col = sheet_col_check_bound (ss->end_col, n);
	else if (ss->base_col > ss->start_col)
		ss->start_col = sheet_col_check_bound (ss->start_col, n);
	else {
		if (n > 0)
			ss->end_col = sheet_col_check_bound (ss->end_col, 1);
		else
			ss->start_col = sheet_col_check_bound (ss->start_col, -1);
	}

	sheet_selection_change (sheet, &old_selection, ss);
981 982
}

983 984 985 986 987 988 989 990 991 992 993 994
/*
 * sheet_selection_extend_vertical
 * @sheet:  The Sheet *
 * @n:      units to extend the selection vertically
 */
void
sheet_selection_extend_vertical (Sheet *sheet, int n)
{
	SheetSelection *ss;
	SheetSelection old_selection;
	
	g_return_if_fail (sheet != NULL);
995
	g_return_if_fail (IS_SHEET (sheet)); 
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
	g_return_if_fail ((n == 1 || n == -1));
	
	ss = (SheetSelection *)sheet->selections->data;
	old_selection = *ss;
	
	if (ss->base_row < ss->end_row)
		ss->end_row = sheet_row_check_bound (ss->end_row, n);
	else if (ss->base_row > ss->start_row)
		ss->start_row = sheet_row_check_bound (ss->start_row, n);
	else {
		if (n > 0)
			ss->end_row = sheet_row_check_bound (ss->end_row, 1);
		else
			ss->start_row = sheet_row_check_bound (ss->start_row, -1);
	}

	sheet_selection_change (sheet, &old_selection, ss);
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
/*
 * Clear the selection from the columns or rows
 */
static int
clean_bar_selection (GList *list)
{
	int cleared = 0;
	
	for (; list; list = list->next){
		ColRowInfo *info = (ColRowInfo *)list->data;

		if (info->selected){
			info->selected = 0;
			cleared++;
		}
	}
	return cleared;
}

1034 1035 1036 1037
/*
 * sheet_selection_clear
 * sheet:  The sheet
 *
1038 1039 1040
 * Clears all of the selection ranges.
 * Warning: This does not set a new selection, this should
 * be taken care on the calling routine. 
1041
 */
1042
void
1043
sheet_selection_clear_only (Sheet *sheet)
1044
{
1045
	GnumericSheet *gsheet;
1046 1047
	GList *list = sheet->selections;

1048
	g_return_if_fail (sheet != NULL);
1049
	g_return_if_fail (IS_SHEET (sheet)); 
1050 1051 1052

	gsheet = GNUMERIC_SHEET (sheet->sheet_view);
	
1053 1054 1055
	for (list = sheet->selections; list; list = list->next){
		SheetSelection *ss = list->data;

1056
		sheet_redraw_selection (sheet, ss);
1057 1058 1059 1060
		g_free (ss);
	}
	g_list_free (sheet->selections);
	sheet->selections = NULL;
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

	/* Unselect the column bar */
	if (clean_bar_selection (sheet->cols_info) > 0)
		gnome_canvas_request_redraw (GNOME_CANVAS (sheet->col_canvas),
					     0, 0, INT_MAX, INT_MAX);

	/* Unselect the row bar */
	if (clean_bar_selection (sheet->rows_info) > 0)
		gnome_canvas_request_redraw (GNOME_CANVAS (sheet->row_canvas),
					     0, 0, INT_MAX, INT_MAX);
	
}

/*
 * sheet_selection_clear
 * sheet:  The sheet
 *
 * Clears all of the selection ranges and resets it to a
 * selection that only covers the cursor
 */
void
sheet_selection_clear (Sheet *sheet)
{
	GnumericSheet *gsheet;

	g_return_if_fail (sheet != NULL);
1087
	g_return_if_fail (IS_SHEET (sheet)); 
1088 1089 1090 1091
	
	gsheet = GNUMERIC_SHEET (sheet->sheet_view);
	
	sheet_selection_clear_only (sheet);
1092
	sheet_selection_append (sheet, gsheet->cursor_col, gsheet->cursor_row);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
}

int
sheet_selection_is_cell_selected (Sheet *sheet, int col, int row)
{
	GList *list = sheet->selections;

	for (list = sheet->selections; list; list = list->next){
		SheetSelection *ss = list->data;

		if ((ss->start_col <= col) && (col <= ss->end_col) &&
		    (ss->start_row <= row) && (row <= ss->end_row))
			return 1;
	}
	return 0;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1109 1110 1111 1112 1113 1114 1115

/*
 * Returns an allocated column:  either an existing one, or a fresh copy
 */
ColRowInfo *
sheet_col_get (Sheet *sheet, int pos)
{
1116
	GList *clist;
Arturo Espinosa's avatar
Arturo Espinosa committed
1117
	ColRowInfo *col;
1118

1119
	g_return_val_if_fail (sheet != NULL, NULL);
1120
	g_return_val_if_fail (IS_SHEET (sheet), NULL); 
Arturo Espinosa's avatar
Arturo Espinosa committed
1121
	
1122
	for (clist = sheet->cols_info; clist; clist = clist->next){
Arturo Espinosa's avatar
Arturo Espinosa committed
1123 1124 1125 1126 1127 1128
		col = (ColRowInfo *) clist->data;

		if (col->pos == pos)
			return col;
	}
	col = sheet_col_new (sheet);
1129
	col->pos = pos;
Arturo Espinosa's avatar
Arturo Espinosa committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	sheet_col_add (sheet, col);
	
	return col;
}

/*
 * Returns an allocated column:  either an existing one, or a fresh copy
 */
ColRowInfo *
sheet_row_get (Sheet *sheet, int pos)
{
1141
	GList *rlist;
Arturo Espinosa's avatar
Arturo Espinosa committed
1142
	ColRowInfo *row;
1143

1144
	g_return_val_if_fail (sheet != NULL, NULL);
1145
	g_return_val_if_fail (IS_SHEET (sheet), NULL); 
Arturo Espinosa's avatar
Arturo Espinosa committed
1146
	
1147
	for (rlist = sheet->rows_info; rlist; rlist = rlist->next){
Arturo Espinosa's avatar
Arturo Espinosa committed
1148 1149 1150 1151 1152
		row = (ColRowInfo *) rlist->data;

		if (row->pos == pos)
			return row;
	}
1153 1154 1155
	row = sheet_row_new (sheet);
	row->pos = pos;
	sheet_row_add (sheet, row);
Arturo Espinosa's avatar
Arturo Espinosa committed
1156 1157 1158 1159
	
	return row;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1160
static int
1161 1162 1163 1164 1165 1166
gen_row_blanks (Sheet *sheet, int col, int start_row, int end_row,
		sheet_cell_foreach_callback callback, void *closure)
{
	int row;

	for (row = 0; row < end_row; row++)
Arturo Espinosa's avatar
Arturo Espinosa committed
1167 1168 1169 1170
		if (!(*callback)(sheet, col, row, NULL, closure))
			return FALSE;

	return TRUE;
1171 1172
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1173
static int
1174 1175 1176 1177 1178 1179 1180
gen_col_blanks (Sheet *sheet, int start_col, int end_col,
		int start_row, int end_row,
		sheet_cell_foreach_callback callback, void *closure)
{
	int col;
       
	for (col = 0; col < end_col; col++)
Arturo Espinosa's avatar
Arturo Espinosa committed
1181 1182 1183 1184
		if (!gen_row_blanks (sheet, col, start_row, end_row, callback, closure))
			return FALSE;
	
	return TRUE;
1185 1186 1187 1188 1189
}

Cell *
sheet_cell_get (Sheet *sheet, int col, int row)
{
1190
	Cell *cell;
1191
	CellPos cellref;
1192
	
1193
	g_return_val_if_fail (sheet != NULL, NULL);
1194
	g_return_val_if_fail (IS_SHEET (sheet), NULL); 
1195

1196 1197 1198 1199 1200
	cellref.col = col;
	cellref.row = row;
	cell = g_hash_table_lookup (sheet->cell_hash, &cellref);

	return cell;
1201 1202
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1203 1204
/*
 * For each existing cell in the range specified, invoke the
1205 1206
 * callback routine.  If the only_existing flag is passed, then
 * callbacks are only invoked for existing cells.
Arturo Espinosa's avatar
Arturo Espinosa committed
1207 1208 1209
 *
 * Return value:
 *    FALSE if some invoked routine requested to stop (by returning FALSE). 
Arturo Espinosa's avatar
Arturo Espinosa committed
1210
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
1211
int
1212
sheet_cell_foreach_range (Sheet *sheet, int only_existing,
Arturo Espinosa's avatar
Arturo Espinosa committed
1213 1214 1215 1216 1217
			  int start_col, int start_row,
			  int end_col, int end_row,
			  sheet_cell_foreach_callback callback,
			  void *closure)
{
1218
	GList *col;
Arturo Espinosa's avatar
Arturo Espinosa committed
1219
	GList *row;
1220
	int   last_col_gen = -1, last_row_gen = -1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1221
	int   cont;
1222

Arturo Espinosa's avatar
Arturo Espinosa committed
1223 1224 1225
	g_return_val_if_fail (sheet != NULL, FALSE);
	g_return_val_if_fail (IS_SHEET (sheet), FALSE); 
	g_return_val_if_fail (callback != NULL, FALSE);
1226 1227
	
	col = sheet->cols_info;
Arturo Espinosa's avatar
Arturo Espinosa committed
1228 1229 1230 1231 1232 1233 1234 1235
	for (; col; col = col->next){
		ColRowInfo *ci = col->data;

		if (ci->pos < start_col)
			continue;
		if (ci->pos > end_col)
			break;

1236 1237
		if (!only_existing){
			if ((last_col_gen > 0) && (ci->pos != last_col_gen+1))
Arturo Espinosa's avatar
Arturo Espinosa committed
1238 1239 1240 1241
				if (!gen_col_blanks (sheet, last_col_gen, ci->pos,
						     start_row, end_row, callback,
						     closure))
				    return FALSE;
1242 1243
					
			if (ci->pos > start_col)
Arturo Espinosa's avatar
Arturo Espinosa committed
1244 1245 1246 1247
				if (!gen_col_blanks (sheet, start_col, ci->pos,
						     start_row, end_row, callback,
						     closure))
					return FALSE;