gnumeric-canvas.c 16.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * The Gnumeric Sheet widget.
 *
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *     Miguel de Icaza (miguel@kernel.org)
 */
9
#include <config.h>
10 11

#include <gnome.h>
12 13
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
14
#include <string.h>
15 16
#include "gnumeric.h"
#include "gnumeric-sheet.h"
17 18 19

static GnomeCanvasClass *sheet_parent_class;

20 21
GdkColor gs_white, gs_black, gs_light_gray, gs_dark_gray;

22 23
static void stop_cell_selection (GnumericSheet *gsheet);

24 25 26 27 28 29
static void
gnumeric_sheet_destroy (GtkObject *object)
{
	GnumericSheet *gsheet;

	/* Add shutdown code here */
Arturo Espinosa's avatar
Arturo Espinosa committed
30 31
	gsheet = GNUMERIC_SHEET (object);
	
32 33 34 35
	if (GTK_OBJECT_CLASS (sheet_parent_class)->destroy)
		(*GTK_OBJECT_CLASS (sheet_parent_class)->destroy)(object);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
36
static GnumericSheet *
37
gnumeric_sheet_create (Sheet *sheet, GtkWidget *entry)
Arturo Espinosa's avatar
Arturo Espinosa committed
38 39 40 41 42 43 44
{
	GnumericSheet *gsheet;
	GnomeCanvas   *canvas;
	
	gsheet = gtk_type_new (gnumeric_sheet_get_type ());
	canvas = GNOME_CANVAS (gsheet);

45 46 47
	gnome_canvas_construct (canvas,
				gtk_widget_get_default_visual (),
				gtk_widget_get_default_colormap ());
Arturo Espinosa's avatar
Arturo Espinosa committed
48 49 50 51
	
	gsheet->sheet   = sheet;
	gsheet->top_col = 0;
	gsheet->top_row = 0;
52
	gsheet->entry   = entry;
53
	
Arturo Espinosa's avatar
Arturo Espinosa committed
54 55 56
	return gsheet;
}

57 58 59
void
gnumeric_sheet_cursor_set (GnumericSheet *sheet, int col, int row)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
60 61
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));

62 63 64 65
	sheet->cursor_col = col;
	sheet->cursor_row = row;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
66
void
Arturo Espinosa's avatar
Arturo Espinosa committed
67
gnumeric_sheet_set_current_value (GnumericSheet *sheet)
68
{
69
	Cell *cell;
Arturo Espinosa's avatar
Arturo Espinosa committed
70 71 72 73 74 75 76 77 78 79 80 81
	int  col, row;
	
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));

	col = sheet->cursor_col;
	row = sheet->cursor_row;
	cell = sheet_cell_get (sheet->sheet, col, row);
	if (!cell)
		cell = sheet_cell_new (sheet->sheet, sheet->cursor_col, sheet->cursor_row);
	
	cell_set_text (sheet->sheet, cell, gtk_entry_get_text (GTK_ENTRY (sheet->entry)));
82 83
	if (sheet->selection)
		stop_cell_selection (sheet);
84
	sheet_brute_force_recompute (sheet->sheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
85 86
	sheet_redraw_all (sheet->sheet);
}
87

Arturo Espinosa's avatar
Arturo Espinosa committed
88 89 90
void
gnumeric_sheet_accept_pending_output (GnumericSheet *sheet)
{
91
	g_return_if_fail (sheet != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
92
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));
93

94 95
	if (!sheet->item_editor)
		return;
96

Arturo Espinosa's avatar
Arturo Espinosa committed
97
	gnumeric_sheet_set_current_value (sheet);
98 99 100
	
	gtk_object_destroy (GTK_OBJECT (sheet->item_editor));
	sheet->item_editor = NULL;
101 102
}

Arturo Espinosa's avatar
Arturo Espinosa committed
103 104
void
gnumeric_sheet_load_cell_val (GnumericSheet *gsheet)
105
{
Arturo Espinosa's avatar
Arturo Espinosa committed
106 107 108
	Sheet *sheet; 
	Workbook *wb;
	GtkWidget *entry;
Arturo Espinosa's avatar
Arturo Espinosa committed
109 110
	Cell *cell;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
111 112 113 114 115
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	
	sheet = gsheet->sheet;
	wb = sheet->parent_workbook;
	entry = wb->ea_input;
Arturo Espinosa's avatar
Arturo Espinosa committed
116 117 118 119 120 121

	cell = sheet_cell_get (sheet, gsheet->cursor_col, gsheet->cursor_row);
	if (cell && cell->entered_text){
		gtk_entry_set_text (GTK_ENTRY(entry), cell->entered_text);
	} else
		gtk_entry_set_text (GTK_ENTRY(entry), "");
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/*
 * gnumeric_sheet_set_selection:
 *  @Sheet:     The sheet name
 *  @start_col: The starting column.
 *  @start_row: The starting row
 *  @end_col:   The end column
 *  @end_row:   The end row
 *
 * Set the current selection to cover the inclusive area delimited by
 * start_col, start_row, end_col and end_row.  The actual cursor is
 * placed at start_col, start_row
 */
void
gnumeric_sheet_set_selection (GnumericSheet *sheet, SheetSelection *ss)
{
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (ss != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));

	gnumeric_sheet_cursor_set (sheet, ss->start_col, ss->start_row);
	item_cursor_set_bounds (sheet->item_cursor,
				ss->start_col, ss->start_row,
				ss->end_col, ss->end_row);
}

149
static void
150 151 152 153 154
start_editing_at_cursor (GnumericSheet *sheet, GtkWidget *entry)
{
	GnomeCanvasItem *item;
	GnomeCanvas *canvas = GNOME_CANVAS (sheet);

Arturo Espinosa's avatar
Arturo Espinosa committed
155
	gtk_entry_set_text (GTK_ENTRY(entry), "");
156
	item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(canvas->root),
157 158 159 160 161 162 163 164 165 166 167 168
				      item_edit_get_type (),
				      "ItemEdit::Sheet",    sheet->sheet,
				      "ItemEdit::Grid",     sheet->item_grid,
				      "ItemEdit::Col",      sheet->cursor_col,
				      "ItemEdit::Row",      sheet->cursor_row,
				      "ItemEdit::GtkEntry", entry,
				      NULL);

	sheet->item_editor = ITEM_EDIT (item);
}

/*
169
 * move_cursor:
170 171 172 173 174 175 176 177 178
 *   @Sheet:    The sheet where the cursor is located
 *   @col:      The new column for the cursor.
 *   @row:      The new row for the cursor.
 *
 *   Moves the sheet cursor to a new location, it clears the selection,
 *   accepts any pending output on the editing line and moves the cell
 *   cursor.
 */
static void
179
move_cursor (GnumericSheet *sheet, int col, int row)
180 181
{
	ItemCursor *item_cursor = sheet->item_cursor;
182 183 184

	gnumeric_sheet_accept_pending_output (sheet);
	gnumeric_sheet_cursor_set (sheet, col, row);
185
	sheet_selection_clear (sheet->sheet);
186 187 188 189 190
	item_cursor_set_bounds (item_cursor, col, row, col, row);
	gnumeric_sheet_load_cell_val (sheet);
}

/*
191
 * move_cursor_horizontal:
192 193 194 195 196 197
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor horizontally
 *
 * Moves the cursor count columns
 */
static void
198
move_cursor_horizontal (GnumericSheet *sheet, int count)
199
{
200 201
	int new_left;
	
202
	new_left = sheet->cursor_col + count;
203 204 205

	if (new_left < 0)
		return;
206 207
	
	if (new_left < sheet->top_col){
208 209
		g_warning ("do scroll\n");
		return;
210 211 212 213
	}

	if (new_left < 0)
		new_left = 0;
214

215
	move_cursor (sheet, new_left, sheet->cursor_row);
216 217
}

218
/*
219
 * move_cursor_vertical:
220 221 222 223 224
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor vertically
 *
 * Moves the cursor count rows
 */
225
static void
226
move_cursor_vertical (GnumericSheet *sheet, int count)
227 228
{
	int new_top;
229 230

	new_top = sheet->cursor_row + count;
231 232

	if (new_top < 0)
233 234 235 236 237 238
		return;

	if (new_top < sheet->top_row){
		g_warning ("do scroll\n");
		return;
	}
239

240
	move_cursor (sheet, sheet->cursor_col, new_top);
241 242
}

243
static void
244
move_horizontal_selection (GnumericSheet *gsheet, int count)
245
{
246
	sheet_selection_extend_horizontal (gsheet->sheet, count);
247 248
}

249
static void
250
move_vertical_selection (GnumericSheet *gsheet, int count)
251
{
252
	sheet_selection_extend_vertical (gsheet->sheet, count);
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
/*
 * gnumeric_sheet_can_move_cursor
 *  @gsheet:   the object
 *
 * Returns true if the cursor keys should be used to select
 * a cell range (if the cursor is in a spot in the expression
 * where it makes sense to have a cell reference), false if not.
 */
static int
gnumeric_sheet_can_move_cursor (GnumericSheet *gsheet)
{
	GtkEntry *entry = GTK_ENTRY (gsheet->entry);
	int cursor_pos = GTK_EDITABLE (entry)->current_pos;

	if (gsheet->selecting_cell)
		return TRUE;
	
	if (entry->text [0] != '=')
		return FALSE;
	if (cursor_pos == 0)
		return FALSE;
		
	switch (entry->text [cursor_pos-1]){
	case '=': case '-': case '*': case '/': case '^': 
	case '+': case '&': case '(': case '%': case '!':
		return TRUE;
	}
	
	return FALSE;
}

static void
start_cell_selection (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
	GnomeCanvasGroup *group = GNOME_CANVAS_GROUP (canvas->root);

	g_return_if_fail (gsheet->selecting_cell == FALSE);
	
	gsheet->selecting_cell = TRUE;
	gsheet->selection = ITEM_CURSOR (gnome_canvas_item_new (
		group,
		item_cursor_get_type (),
		"Sheet", gsheet->sheet,
		"Grid",  gsheet->item_grid,
		"Style", ITEM_CURSOR_ANTED, NULL));
301 302 303 304 305 306
	item_cursor_set_bounds (ITEM_CURSOR (gsheet->selection),
				gsheet->item_cursor->start_col,
				gsheet->item_cursor->start_row,
				gsheet->item_cursor->end_col,
				gsheet->item_cursor->end_row);
				
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	gsheet->sel_cursor_pos = GTK_EDITABLE (gsheet->entry)->current_pos;
	gsheet->sel_text_len = 0;
}

static void
stop_cell_selection (GnumericSheet *gsheet)
{
	if (!gsheet->selecting_cell)
		return;
	
	gsheet->selecting_cell = FALSE;
	gtk_object_destroy (GTK_OBJECT (gsheet->selection));
	gsheet->selection = NULL;
}

static void
selection_remove_selection_string (GnumericSheet *gsheet)
{
	gtk_editable_delete_text (GTK_EDITABLE (gsheet->entry),
				  gsheet->sel_cursor_pos,
				  gsheet->sel_cursor_pos+gsheet->sel_text_len);
}

static void
selection_insert_selection_string (GnumericSheet *gsheet)
{
	ItemCursor *sel = gsheet->selection;
	char buffer [20];
	int pos;

	/* Get the new selection string */
	strcpy (buffer, cell_name (sel->start_col, sel->start_row));
	if (!(sel->start_col == sel->end_col && sel->start_row == sel->end_row)){
		strcat (buffer, ":");
		strcat (buffer, cell_name (sel->end_col, sel->end_row));
	}
	gsheet->sel_text_len = strlen (buffer);

	pos = gsheet->sel_cursor_pos;
	gtk_editable_insert_text (GTK_EDITABLE (gsheet->entry),
				  buffer, strlen (buffer),
				  &pos);

	/* Set the cursor at the end.  It looks nicer */
	gtk_editable_set_position (GTK_EDITABLE (gsheet->entry),
				   gsheet->sel_cursor_pos +
				   gsheet->sel_text_len);
}

static void
selection_cursor_move_horizontal (GnumericSheet *gsheet, int dir)
{
	ItemCursor *ic;

	g_return_if_fail (dir == -1 || dir == 1);
	
	if (!gsheet->selecting_cell)
		start_cell_selection (gsheet);

	ic = gsheet->selection;
	if (dir == -1){
		if (ic->start_col == 0)
			return;
	} else {
		if (ic->end_col + 1 > (SHEET_MAX_COLS-1))
			return;
	}
	
	selection_remove_selection_string (gsheet);
	item_cursor_set_bounds (ic,
				ic->start_col + dir,
				ic->start_row, 
				ic->end_col + dir,
				ic->end_row);
	selection_insert_selection_string (gsheet);
}

static void
selection_cursor_move_vertical (GnumericSheet *gsheet, int dir)
{
	ItemCursor *ic;

	g_return_if_fail (dir == -1 || dir == 1);

	if (!gsheet->selecting_cell)
		start_cell_selection (gsheet);

	ic = gsheet->selection;
	if (dir == -1){
		if (ic->start_row == 0)
			return;
	} else {
		if (ic->end_row + 1 > (SHEET_MAX_ROWS-1))
			return;
	}

	if (!gsheet->selecting_cell)
		start_cell_selection (gsheet);
	item_cursor_set_bounds (ic,
				ic->start_col, ic->start_row + dir,
				ic->end_col, ic->end_row + dir);
	selection_remove_selection_string (gsheet);
	selection_insert_selection_string (gsheet);
}

static void
selection_expand_horizontal (GnumericSheet *gsheet, int dir)
{
	ItemCursor *ic;

	g_return_if_fail (dir == -1 || dir == 1);

	if (!gsheet->selecting_cell){
		selection_cursor_move_vertical (gsheet, dir);
		return;
	}

	ic = gsheet->selection;
	if (dir == -1){
		if (ic->start_col == 0)
			return;
	} else {
		if (ic->end_col == SHEET_MAX_COLS-1)
			return;
	}

	selection_remove_selection_string (gsheet);
	item_cursor_set_bounds (ic,
				ic->start_col + (dir == -1 ? dir : 0),
				ic->start_row,
				ic->end_col  +  (dir == 1 ? dir : 0),
				ic->end_row);
	selection_insert_selection_string (gsheet);
}

static void
selection_expand_vertical (GnumericSheet *gsheet, int dir)
{
	ItemCursor *ic;

	g_return_if_fail (dir == -1 || dir == 1);

	if (!gsheet->selecting_cell){
		selection_cursor_move_vertical (gsheet, dir);
		return;
	}

	ic = gsheet->selection;
	
	if (dir == -1){
		if (ic->start_row == 0)
			return;
	} else {
		if (ic->end_row == SHEET_MAX_ROWS-1)
			return;
	}

	selection_remove_selection_string (gsheet);
	item_cursor_set_bounds (ic,
				ic->start_col,
				ic->start_row + (dir == -1 ? dir : 0),
				ic->end_col,
				ic->end_row + (dir == 1 ? dir : 0));	
	selection_insert_selection_string (gsheet);
}

/*
 * key press event handler for the gnumeric sheet
 */
476 477 478 479
static gint
gnumeric_sheet_key (GtkWidget *widget, GdkEventKey *event)
{
	GnumericSheet *sheet = GNUMERIC_SHEET (widget);
Arturo Espinosa's avatar
Arturo Espinosa committed
480
	Workbook *wb = sheet->sheet->parent_workbook;
Arturo Espinosa's avatar
Arturo Espinosa committed
481 482
	void (*movefn_horizontal) (GnumericSheet *, int);
	void (*movefn_vertical)   (GnumericSheet *, int);
483
	int  cursor_move = gnumeric_sheet_can_move_cursor (sheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
484

485
	if ((event->state & GDK_SHIFT_MASK) != 0){
486 487 488 489 490 491 492
		if (cursor_move){
			movefn_horizontal = selection_expand_horizontal;
			movefn_vertical   = selection_expand_vertical;
		} else {
			movefn_horizontal = move_horizontal_selection;
			movefn_vertical   = move_vertical_selection;
		}
493
	} else {
494 495 496 497 498 499 500
		if (cursor_move){
			movefn_horizontal = selection_cursor_move_horizontal;
			movefn_vertical   = selection_cursor_move_vertical;
		} else {
			movefn_horizontal = move_cursor_horizontal;
			movefn_vertical = move_cursor_vertical;
		}
501
	}
502

503 504 505 506 507 508 509 510 511 512 513 514
	/* Ignore a few keys (to avoid the selection cursor to be killed
	 * in some cases
	 */
	if (cursor_move){
		switch (event->keyval){
		case GDK_Shift_L: case GDK_Shift_R:
		case GDK_Alt_L:   case GDK_Alt_R:
		case GDK_Control_L:   case GDK_Control_R:
			return 1;
		}
	}
	
515 516
	switch (event->keyval){
	case GDK_Left:
517
		(*movefn_horizontal)(sheet, -1);
518 519 520
		break;

	case GDK_Right:
521
		(*movefn_horizontal)(sheet, 1);
522 523 524
		break;

	case GDK_Up:
525
		(*movefn_vertical)(sheet, -1);
526 527 528
		break;

	case GDK_Down:
529
		(*movefn_vertical)(sheet, 1);
530 531
		break;

532
	case GDK_Return:
533
		move_cursor (sheet, sheet->cursor_col, sheet->cursor_row);
534 535
		break;
		
Arturo Espinosa's avatar
Arturo Espinosa committed
536 537 538
	case GDK_F2:
		gtk_window_set_focus (GTK_WINDOW (wb->toplevel), wb->ea_input);
		/* fallback */
539

540
	default:
541 542 543 544
		if (!sheet->item_editor){
			if (event->keyval >= 0x20 && event->keyval <= 0xff)
			    start_editing_at_cursor (sheet, wb->ea_input);
		}
545 546
		stop_cell_selection (sheet);
		
Arturo Espinosa's avatar
Arturo Espinosa committed
547 548 549
		/* Forward the keystroke to the input line */
		gtk_widget_event (sheet->entry, (GdkEvent *) event);
		
550 551 552 553
	}
	return 1;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
554 555 556
GtkWidget *
gnumeric_sheet_new (Sheet *sheet)
{
557
	GnomeCanvasItem *item;
Arturo Espinosa's avatar
Arturo Espinosa committed
558
	GnumericSheet *gsheet;
559 560 561
	GnomeCanvas   *gsheet_canvas;
	GnomeCanvasGroup *gsheet_group;
	GtkWidget *widget;
562
	GtkWidget *entry = sheet->parent_workbook->ea_input;
Arturo Espinosa's avatar
Arturo Espinosa committed
563
	
564
	gsheet = gnumeric_sheet_create (sheet, entry);
Arturo Espinosa's avatar
Arturo Espinosa committed
565
	gnome_canvas_set_size (GNOME_CANVAS (gsheet), 300, 100);
566 567 568 569 570 571

	/* handy shortcuts */
	gsheet_canvas = GNOME_CANVAS (gsheet);
	gsheet_group = GNOME_CANVAS_GROUP (gsheet_canvas->root);
	
	/* The grid */
572
	item = gnome_canvas_item_new (gsheet_group,
573 574 575 576 577 578
				      item_grid_get_type (),
				      "ItemGrid::Sheet", sheet,
				      NULL);
	gsheet->item_grid = ITEM_GRID (item);

	/* The cursor */
579
	item = gnome_canvas_item_new (gsheet_group,
580 581 582 583 584
				      item_cursor_get_type (),
				      "ItemCursor::Sheet", sheet,
				      "ItemCursor::Grid", gsheet->item_grid,
				      NULL);
	gsheet->item_cursor = ITEM_CURSOR (item);
585

586 587 588 589 590 591 592 593 594 595
	widget = GTK_WIDGET (gsheet);

	return widget;
}

static void
gnumeric_sheet_realize (GtkWidget *widget)
{
	if (GTK_WIDGET_CLASS (sheet_parent_class)->realize)
		(*GTK_WIDGET_CLASS (sheet_parent_class)->realize)(widget);
Arturo Espinosa's avatar
Arturo Espinosa committed
596 597
}

598 599
void
gnumeric_sheet_compute_visible_ranges (GnumericSheet *gsheet)
600
{
601
	GnomeCanvas   *canvas = GNOME_CANVAS (gsheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
	int pixels, col, row;

	/* Find out the last visible col and the last full visible column */
	pixels = 0;
	col = gsheet->top_col;
	do {
		ColRowInfo *ci;
		int cb;
		
		ci = sheet_col_get_info (gsheet->sheet, col);
		cb = pixels + ci->pixels;
		
		if (cb == canvas->width){
			gsheet->last_visible_col = col;
			gsheet->last_full_col = col;
		} if (cb > canvas->width){
			gsheet->last_visible_col = col;
			if (col == gsheet->top_col)
				gsheet->last_full_col = gsheet->top_col;
			else
				gsheet->last_full_col = col - 1;
		}
		pixels = cb;
		col++;
	} while (pixels < canvas->width);

	/* Find out the last visible row and the last fully visible row */
	pixels = 0;
	row = gsheet->top_row;
	do {
		ColRowInfo *ri;
		int cb;
		
		ri = sheet_row_get_info (gsheet->sheet, row);
		cb = pixels + ri->pixels;
		
		if (cb == canvas->height){
			gsheet->last_visible_row = row;
			gsheet->last_full_row = row;
641
		} if (cb > canvas->height){
Arturo Espinosa's avatar
Arturo Espinosa committed
642 643 644 645 646 647 648 649
			gsheet->last_visible_row = row;
			if (col == gsheet->top_row)
				gsheet->last_full_row = gsheet->top_row;
			else
				gsheet->last_full_row = row - 1;
		}
		pixels = cb;
		row++;
650 651
	} while (pixels < canvas->height);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
652

653 654 655 656 657 658
static void
gnumeric_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
	(*GTK_WIDGET_CLASS (sheet_parent_class)->size_allocate)(widget, allocation);

	gnumeric_sheet_compute_visible_ranges (GNUMERIC_SHEET (widget));
659 660
}

Arturo Espinosa's avatar
Arturo Espinosa committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
static void
gnumeric_sheet_class_init (GnumericSheetClass *class)
{
	GtkObjectClass *object_class;
	GtkWidgetClass *widget_class;
	GnomeCanvasClass *canvas_class;

	object_class = (GtkObjectClass *) class;
	widget_class = (GtkWidgetClass *) class;
	canvas_class = (GnomeCanvasClass *) class;
	
	sheet_parent_class = gtk_type_class (gnome_canvas_get_type());

	/* Method override */
	object_class->destroy = gnumeric_sheet_destroy;
676
	widget_class->realize = gnumeric_sheet_realize;
677
 	widget_class->size_allocate = gnumeric_size_allocate;
678
	widget_class->key_press_event = gnumeric_sheet_key;
Arturo Espinosa's avatar
Arturo Espinosa committed
679 680
}

Arturo Espinosa's avatar
Arturo Espinosa committed
681 682 683 684
static void
gnumeric_sheet_init (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
685
	
686 687
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_DEFAULT);
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

}

void
gnumeric_sheet_color_alloc (GnomeCanvas *canvas)
{
	static int colors_loaded;
	
	if (colors_loaded)
		return;
	
	gdk_color_white (canvas->colormap, &gs_white);
	gdk_color_black (canvas->colormap, &gs_black);
	gnome_canvas_get_color (canvas, "gray60", &gs_light_gray);
	gnome_canvas_get_color (canvas, "gray20", &gs_dark_gray);
	colors_loaded = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
704 705
}

706 707 708 709 710 711 712 713 714 715 716
GtkType
gnumeric_sheet_get_type (void)
{
	static GtkType gnumeric_sheet_type = 0;

	if (!gnumeric_sheet_type){
		GtkTypeInfo gnumeric_sheet_info = {
			"GnumericSheet",
			sizeof (GnumericSheet),
			sizeof (GnumericSheetClass),
			(GtkClassInitFunc) gnumeric_sheet_class_init,
Arturo Espinosa's avatar
Arturo Espinosa committed
717
			(GtkObjectInitFunc) gnumeric_sheet_init,
718 719 720 721 722
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

Arturo Espinosa's avatar
Arturo Espinosa committed
723
		gnumeric_sheet_type = gtk_type_unique (gnome_canvas_get_type (), &gnumeric_sheet_info);
724 725 726 727
	}

	return gnumeric_sheet_type;
}