gnumeric-canvas.c 10.4 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 15
#include "gnumeric.h"
#include "gnumeric-sheet.h"
16 17 18 19 20 21 22 23 24

static GnomeCanvasClass *sheet_parent_class;

static void
gnumeric_sheet_destroy (GtkObject *object)
{
	GnumericSheet *gsheet;

	/* Add shutdown code here */
Arturo Espinosa's avatar
Arturo Espinosa committed
25 26
	gsheet = GNUMERIC_SHEET (object);
	
27 28 29 30
	if (GTK_OBJECT_CLASS (sheet_parent_class)->destroy)
		(*GTK_OBJECT_CLASS (sheet_parent_class)->destroy)(object);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
31
static GnumericSheet *
32
gnumeric_sheet_create (Sheet *sheet, GtkWidget *entry)
Arturo Espinosa's avatar
Arturo Espinosa committed
33 34 35 36 37 38 39
{
	GnumericSheet *gsheet;
	GnomeCanvas   *canvas;
	
	gsheet = gtk_type_new (gnumeric_sheet_get_type ());
	canvas = GNOME_CANVAS (gsheet);

40 41 42
	gnome_canvas_construct (canvas,
				gtk_widget_get_default_visual (),
				gtk_widget_get_default_colormap ());
Arturo Espinosa's avatar
Arturo Espinosa committed
43 44 45 46
	
	gsheet->sheet   = sheet;
	gsheet->top_col = 0;
	gsheet->top_row = 0;
47
	gsheet->entry   = entry;
48
	
Arturo Espinosa's avatar
Arturo Espinosa committed
49 50 51
	return gsheet;
}

52 53 54
void
gnumeric_sheet_cursor_set (GnumericSheet *sheet, int col, int row)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
55 56
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));

57 58 59 60
	sheet->cursor_col = col;
	sheet->cursor_row = row;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
61
void
62 63
gnumeric_sheet_accept_pending_output (GnumericSheet *sheet)
{
64 65
	Cell *cell;

66
	g_return_if_fail (sheet != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
67
	g_return_if_fail (GNUMERIC_IS_SHEET (sheet));
68

69 70
	if (!sheet->item_editor)
		return;
71

72 73 74 75 76 77 78 79
	cell = sheet_cell_get (sheet->sheet, sheet->cursor_col, sheet->cursor_row);
	if (!cell)
		cell = sheet_cell_new (sheet->sheet, sheet->cursor_col, sheet->cursor_row);
	
	cell_set_text (cell, gtk_entry_get_text (GTK_ENTRY (sheet->entry)));

	gtk_object_destroy (GTK_OBJECT (sheet->item_editor));
	sheet->item_editor = NULL;
80 81
}

Arturo Espinosa's avatar
Arturo Espinosa committed
82 83
void
gnumeric_sheet_load_cell_val (GnumericSheet *gsheet)
84
{
Arturo Espinosa's avatar
Arturo Espinosa committed
85 86 87
	Sheet *sheet; 
	Workbook *wb;
	GtkWidget *entry;
88

Arturo Espinosa's avatar
Arturo Espinosa committed
89 90 91 92 93 94
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	
	sheet = gsheet->sheet;
	wb = sheet->parent_workbook;
	entry = wb->ea_input;
	
95 96 97
	gtk_entry_set_text (GTK_ENTRY(entry), "");
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/*
 * 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);
}

123
static void
124 125 126 127 128
start_editing_at_cursor (GnumericSheet *sheet, GtkWidget *entry)
{
	GnomeCanvasItem *item;
	GnomeCanvas *canvas = GNOME_CANVAS (sheet);

129
	item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(canvas->root),
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
				      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);
}

/*
 * gnumeric_sheet_move_cursor:
 *   @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
gnumeric_sheet_move_cursor (GnumericSheet *sheet, int col, int row)
153 154
{
	ItemCursor *item_cursor = sheet->item_cursor;
155 156 157

	gnumeric_sheet_accept_pending_output (sheet);
	gnumeric_sheet_cursor_set (sheet, col, row);
158
	sheet_selection_clear (sheet->sheet);
159 160 161 162 163 164 165 166 167 168 169 170 171 172
	item_cursor_set_bounds (item_cursor, col, row, col, row);
	gnumeric_sheet_load_cell_val (sheet);
}

/*
 * gnumeric_sheet_move_cursor_horizontal:
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor horizontally
 *
 * Moves the cursor count columns
 */
static void
gnumeric_sheet_move_cursor_horizontal (GnumericSheet *sheet, int count)
{
173 174
	int new_left;
	
175
	new_left = sheet->cursor_col + count;
176 177 178

	if (new_left < 0)
		return;
179 180
	
	if (new_left < sheet->top_col){
181 182
		g_warning ("do scroll\n");
		return;
183 184 185 186
	}

	if (new_left < 0)
		new_left = 0;
187

188
	gnumeric_sheet_move_cursor (sheet, new_left, sheet->cursor_row);
189 190
}

191 192 193 194 195 196 197
/*
 * gnumeric_sheet_move_cursor_vertical:
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor vertically
 *
 * Moves the cursor count rows
 */
198 199 200 201
static void
gnumeric_sheet_move_cursor_vertical (GnumericSheet *sheet, int count)
{
	int new_top;
202 203

	new_top = sheet->cursor_row + count;
204 205

	if (new_top < 0)
206 207 208 209 210 211
		return;

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

213
	gnumeric_sheet_move_cursor (sheet, sheet->cursor_col, new_top);
214 215
}

216 217
static void
gnumeric_sheet_move_horizontal_selection (GnumericSheet *gsheet, int count)
218
{
219
	sheet_selection_extend_horizontal (gsheet->sheet, count);
220 221
}

222
static void
223
gnumeric_sheet_move_vertical_selection (GnumericSheet *gsheet, int count)
224
{
225
	sheet_selection_extend_vertical (gsheet->sheet, count);
226 227
}

228 229 230 231
static gint
gnumeric_sheet_key (GtkWidget *widget, GdkEventKey *event)
{
	GnumericSheet *sheet = GNUMERIC_SHEET (widget);
Arturo Espinosa's avatar
Arturo Espinosa committed
232
	Workbook *wb = sheet->sheet->parent_workbook;
233 234 235 236 237 238 239 240 241 242 243
	void (*movefn_horizontal)(GnumericSheet *, int);
	void (*movefn_vertical)(GnumericSheet *, int);
	
	if ((event->state & GDK_SHIFT_MASK) != 0){
		movefn_horizontal = gnumeric_sheet_move_horizontal_selection;
		movefn_vertical   = gnumeric_sheet_move_vertical_selection;
	} else {
		movefn_horizontal = gnumeric_sheet_move_cursor_horizontal;
		movefn_vertical = gnumeric_sheet_move_cursor_vertical;
		
	}
244 245 246

	switch (event->keyval){
	case GDK_Left:
247
		(*movefn_horizontal)(sheet, -1);
248 249 250
		break;

	case GDK_Right:
251
		(*movefn_horizontal)(sheet, 1);
252 253 254
		break;

	case GDK_Up:
255
		(*movefn_vertical)(sheet, -1);
256 257 258
		break;

	case GDK_Down:
259
		(*movefn_vertical)(sheet, 1);
260 261
		break;

262 263 264 265
	case GDK_Return:
		gnumeric_sheet_move_cursor (sheet, sheet->cursor_col, sheet->cursor_row);
		break;
		
Arturo Espinosa's avatar
Arturo Espinosa committed
266 267 268
	case GDK_F2:
		gtk_window_set_focus (GTK_WINDOW (wb->toplevel), wb->ea_input);
		/* fallback */
269

270
	default:
271 272 273 274
		if (!sheet->item_editor){
			if (event->keyval >= 0x20 && event->keyval <= 0xff)
			    start_editing_at_cursor (sheet, wb->ea_input);
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
275 276 277 278

		/* Forward the keystroke to the input line */
		gtk_widget_event (sheet->entry, (GdkEvent *) event);
		
279 280 281 282
	}
	return 1;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
283 284 285
GtkWidget *
gnumeric_sheet_new (Sheet *sheet)
{
286
	GnomeCanvasItem *item;
Arturo Espinosa's avatar
Arturo Espinosa committed
287
	GnumericSheet *gsheet;
288 289 290
	GnomeCanvas   *gsheet_canvas;
	GnomeCanvasGroup *gsheet_group;
	GtkWidget *widget;
291
	GtkWidget *entry = sheet->parent_workbook->ea_input;
Arturo Espinosa's avatar
Arturo Espinosa committed
292
	
293
	gsheet = gnumeric_sheet_create (sheet, entry);
Arturo Espinosa's avatar
Arturo Espinosa committed
294
	gnome_canvas_set_size (GNOME_CANVAS (gsheet), 300, 100);
295 296 297 298 299 300

	/* handy shortcuts */
	gsheet_canvas = GNOME_CANVAS (gsheet);
	gsheet_group = GNOME_CANVAS_GROUP (gsheet_canvas->root);
	
	/* The grid */
301
	item = gnome_canvas_item_new (gsheet_group,
302 303 304 305 306 307
				      item_grid_get_type (),
				      "ItemGrid::Sheet", sheet,
				      NULL);
	gsheet->item_grid = ITEM_GRID (item);

	/* The cursor */
308
	item = gnome_canvas_item_new (gsheet_group,
309 310 311 312 313
				      item_cursor_get_type (),
				      "ItemCursor::Sheet", sheet,
				      "ItemCursor::Grid", gsheet->item_grid,
				      NULL);
	gsheet->item_cursor = ITEM_CURSOR (item);
314

315 316 317 318 319 320 321 322 323 324
	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
325 326
}

327 328
void
gnumeric_sheet_compute_visible_ranges (GnumericSheet *gsheet)
329
{
330
	GnomeCanvas   *canvas = GNOME_CANVAS (gsheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
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
	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;
370
		} if (cb > canvas->height){
Arturo Espinosa's avatar
Arturo Espinosa committed
371 372 373 374 375 376 377 378
			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++;
379 380
	} while (pixels < canvas->height);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
381

382 383 384 385 386 387
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));
388 389
}

Arturo Espinosa's avatar
Arturo Espinosa committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
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;
405
	widget_class->realize = gnumeric_sheet_realize;
406
 	widget_class->size_allocate = gnumeric_size_allocate;
407
	widget_class->key_press_event = gnumeric_sheet_key;
Arturo Espinosa's avatar
Arturo Espinosa committed
408 409
}

Arturo Espinosa's avatar
Arturo Espinosa committed
410 411 412 413 414
static void
gnumeric_sheet_init (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);

415 416
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_DEFAULT);
Arturo Espinosa's avatar
Arturo Espinosa committed
417 418
}

419 420 421 422 423 424 425 426 427 428 429
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
430
			(GtkObjectInitFunc) gnumeric_sheet_init,
431 432 433 434 435
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

Arturo Espinosa's avatar
Arturo Espinosa committed
436
		gnumeric_sheet_type = gtk_type_unique (gnome_canvas_get_type (), &gnumeric_sheet_info);
437 438 439 440
	}

	return gnumeric_sheet_type;
}