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

#include <gnome.h>
10
11
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
12
#include <string.h>
13
14
#include "gnumeric.h"
#include "gnumeric-sheet.h"
15
#include "color.h"
16
#include "cursors.h"
17

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
18
19
20
#define CURSOR_COL(gsheet) gsheet->sheet_view->sheet->cursor_col
#define CURSOR_ROW(gsheet) gsheet->sheet_view->sheet->cursor_row

21
/* Public colors: shared by all of our items in Gnumeric */
22

23
24
GdkColor gs_white, gs_black, gs_light_gray, gs_dark_gray;

25

26
static GnomeCanvasClass *sheet_parent_class;
27
28
static void stop_cell_selection (GnumericSheet *gsheet);

29

30
31
32
33
34
35
static void
gnumeric_sheet_destroy (GtkObject *object)
{
	GnumericSheet *gsheet;

	/* Add shutdown code here */
Arturo Espinosa's avatar
Arturo Espinosa committed
36
37
	gsheet = GNUMERIC_SHEET (object);
	
38
39
40
41
	if (GTK_OBJECT_CLASS (sheet_parent_class)->destroy)
		(*GTK_OBJECT_CLASS (sheet_parent_class)->destroy)(object);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
42
static GnumericSheet *
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
43
gnumeric_sheet_create (SheetView *sheet_view, GtkWidget *entry)
Arturo Espinosa's avatar
Arturo Espinosa committed
44
45
46
47
48
49
50
{
	GnumericSheet *gsheet;
	GnomeCanvas   *canvas;
	
	gsheet = gtk_type_new (gnumeric_sheet_get_type ());
	canvas = GNOME_CANVAS (gsheet);

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
51
	gsheet->sheet_view = sheet_view;
Arturo Espinosa's avatar
Arturo Espinosa committed
52
53
	gsheet->top_col = 0;
	gsheet->top_row = 0;
54
	gsheet->entry   = entry;
55
	
Arturo Espinosa's avatar
Arturo Espinosa committed
56
57
58
	return gsheet;
}

59
void
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
60
gnumeric_sheet_get_cell_bounds (GnumericSheet *gsheet, int col, int row, int *x, int *y, int *w, int *h)
61
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
62
63
	Sheet *sheet;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
64
	g_return_if_fail (gsheet != NULL);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
65
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet)); 
66

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
67
	sheet = gsheet->sheet_view->sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
68
	
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
69
70
	*x = sheet_col_get_distance (sheet, gsheet->top_col, col);
	*y = sheet_row_get_distance (sheet, gsheet->top_row, row);
Arturo Espinosa's avatar
Arturo Espinosa committed
71

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
72
73
	*w = sheet_col_get_distance (sheet, col, col + 1);
	*h = sheet_row_get_distance (sheet, row, row + 1);
Arturo Espinosa's avatar
Arturo Espinosa committed
74
75
}

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
76
77
78
79
80
81
82
83
84
/*
 * gnumeric_sheet_cursor_set
 *  @gsheet: The sheet
 *  @col:    the column
 *  @row:    the row
 *
 * This informs the GnumericSheet of the cursor position.  It is
 * used to sync the contents of the scrollbars with our position
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
85
void
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
86
gnumeric_sheet_cursor_set (GnumericSheet *gsheet, int col, int row)
Arturo Espinosa's avatar
Arturo Espinosa committed
87
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
88
89
90
	GtkAdjustment *ha, *va;
	SheetView *sheet_view;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
91
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
92

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
93
	sheet_view = gsheet->sheet_view;
94

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
95
96
97
98
99
100
101
102
103
	if (sheet_view->ha){
		ha = GTK_ADJUSTMENT (sheet_view->ha);
		va = GTK_ADJUSTMENT (sheet_view->va);
		ha->value = col;
		va->value = row;
		
		gtk_adjustment_value_changed (ha);
		gtk_adjustment_value_changed (va);
	}
104
105
}

106
107
/*
 * gnumeric_sheet_set_selection:
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
108
 *  @gsheet:    The sheet name
109
110
111
112
113
114
115
116
117
118
 *  @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
Arturo Espinosa's avatar
Arturo Espinosa committed
119
gnumeric_sheet_set_selection (GnumericSheet *gsheet, SheetSelection *ss)
120
{
Arturo Espinosa's avatar
Arturo Espinosa committed
121
	g_return_if_fail (gsheet != NULL);
122
	g_return_if_fail (ss != NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
123
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
124

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
125
126
127
128
	sheet_cursor_set (
		gsheet->sheet_view->sheet,
		ss->start_col, ss->start_row,
		ss->end_col, ss->end_row);
129
130
131
}

/*
132
 * move_cursor:
133
134
135
 *   @Sheet:    The sheet where the cursor is located
 *   @col:      The new column for the cursor.
 *   @row:      The new row for the cursor.
136
 *   @clear_selection: If set, clear the selection before moving
137
138
139
140
141
 *   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
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
142
move_cursor (GnumericSheet *gsheet, int col, int row, gboolean clear_selection)
143
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
144
	Sheet *sheet = gsheet->sheet_view->sheet;
145
146

	if (clear_selection)
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
147
		sheet_selection_reset_only (sheet);
148

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
149
	sheet_cursor_set (sheet, col, row, col, row);
150

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
151
152
	if (clear_selection)
		sheet_selection_append (sheet, col, row);
153
154
}

155
156
157
158
159
160
161
162
163
void
gnumeric_sheet_move_cursor (GnumericSheet *gsheet, int col, int row)
{
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));

	move_cursor (gsheet, col, row, 1);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
164
165
166
167
168
169
170
171
void
gnumeric_sheet_set_cursor_bounds (GnumericSheet *gsheet,
				  int start_col, int start_row,
				  int end_col,   int end_row)
{
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
172
173
174
175
	item_cursor_set_bounds (
		gsheet->item_cursor,
		start_col, start_row,
		end_col, end_row);
Arturo Espinosa's avatar
Arturo Espinosa committed
176
177
}

178
/*
179
 * move_cursor_horizontal:
180
181
182
183
184
185
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor horizontally
 *
 * Moves the cursor count columns
 */
static void
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
186
move_cursor_horizontal (GnumericSheet *gsheet, int count)
187
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
188
	Sheet *sheet = gsheet->sheet_view->sheet;
189
190
	int new_left;
	
191
	new_left = sheet->cursor_col + count;
192
193

	if (new_left < 0)
194
		new_left = 0;
195
	
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
196
	move_cursor (gsheet, new_left, sheet->cursor_row, TRUE);
197
198
}

199
/*
200
 * move_cursor_vertical:
201
202
203
204
205
 *  @Sheet:  The sheet name
 *  @count:  number of units to move the cursor vertically
 *
 * Moves the cursor count rows
 */
206
static void
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
207
move_cursor_vertical (GnumericSheet *gsheet, int count)
208
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
209
	Sheet *sheet = gsheet->sheet_view->sheet;
210
	int new_top;
211

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
212
	new_top = CURSOR_ROW (gsheet) + count;
213
214

	if (new_top < 0)
215
		new_top = 0;
216

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
217
	move_cursor (gsheet, sheet->cursor_col, new_top, TRUE);
218
219
}

220
static void
221
move_horizontal_selection (GnumericSheet *gsheet, int count)
222
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
223
	sheet_selection_extend_horizontal (gsheet->sheet_view->sheet, count);
224
225
}

226
static void
227
move_vertical_selection (GnumericSheet *gsheet, int count)
228
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
229
	sheet_selection_extend_vertical (gsheet->sheet_view->sheet, count);
230
231
}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * 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 '!':
257
	case ':': case ',':
258
259
260
261
262
263
264
265
266
267
268
		return TRUE;
	}
	
	return FALSE;
}

static void
start_cell_selection (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
	GnomeCanvasGroup *group = GNOME_CANVAS_GROUP (canvas->root);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
269
270
	Sheet *sheet = gsheet->sheet_view->sheet;
	
271
272
273
274
275
276
	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 (),
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
277
		"Sheet", gsheet->sheet_view->sheet,
278
279
		"Grid",  gsheet->item_grid,
		"Style", ITEM_CURSOR_ANTED, NULL));
280
	item_cursor_set_bounds (ITEM_CURSOR (gsheet->selection),
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
281
282
283
284
				sheet->cursor_col,
				sheet->cursor_row,
				sheet->cursor_col,
				sheet->cursor_row);
285
				
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	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;
}

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
void
gnumeric_sheet_create_editing_cursor (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
	GnomeCanvasItem *item;
	Sheet *sheet;
	int col, row;

	sheet = gsheet->sheet_view->sheet;
	col = sheet->cursor_col;
	row = sheet->cursor_row;
	
	item = gnome_canvas_item_new (GNOME_CANVAS_GROUP(canvas->root),
				      item_edit_get_type (),
				      "ItemEdit::Sheet",    sheet,
				      "ItemEdit::Grid",     gsheet->item_grid,
				      "ItemEdit::Col",      col, 
				      "ItemEdit::Row",      row,
				      "ItemEdit::GtkEntry", sheet->workbook->ea_input,
				      NULL);

	gsheet->item_editor = ITEM_EDIT (item);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
325
static void
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
326
327
328
329
330
331
332
333
334
335
destroy_item_editor (GnumericSheet *gsheet)
{
	g_return_if_fail (gsheet->item_editor);
	
	gtk_object_destroy (GTK_OBJECT (gsheet->item_editor));
	gsheet->item_editor = NULL;
}

void
gnumeric_sheet_destroy_editing_cursor (GnumericSheet *gsheet)
Arturo Espinosa's avatar
Arturo Espinosa committed
336
{
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
337
338
339
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	
Arturo Espinosa's avatar
Arturo Espinosa committed
340
341
	stop_cell_selection (gsheet);
	
342
343
344
	if (!gsheet->item_editor)
		return;

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
345
346
347
348
349
350
351
352
353
354
355
356
	destroy_item_editor (gsheet);
}

void
gnumeric_sheet_stop_editing (GnumericSheet *gsheet)
{
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));

	if (!gsheet->item_editor)
		return;

357
	destroy_item_editor (gsheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
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
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);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
414
	item_cursor_set_bounds (ic, 
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
				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,
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
444
445
446
447
				ic->start_col,
				ic->start_row + dir,
				ic->end_col,
				ic->end_row + dir);
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
	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
 */
516
517
518
static gint
gnumeric_sheet_key (GtkWidget *widget, GdkEventKey *event)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
519
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
520
521
	Sheet *sheet = gsheet->sheet_view->sheet;
	Workbook *wb = sheet->workbook;
Arturo Espinosa's avatar
Arturo Espinosa committed
522
523
	void (*movefn_horizontal) (GnumericSheet *, int);
	void (*movefn_vertical)   (GnumericSheet *, int);
Arturo Espinosa's avatar
Arturo Espinosa committed
524
	int  cursor_move = gnumeric_sheet_can_move_cursor (gsheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
525

526
	if ((event->state & GDK_SHIFT_MASK) != 0){
527
528
529
530
531
532
533
		if (cursor_move){
			movefn_horizontal = selection_expand_horizontal;
			movefn_vertical   = selection_expand_vertical;
		} else {
			movefn_horizontal = move_horizontal_selection;
			movefn_vertical   = move_vertical_selection;
		}
534
	} else {
535
536
537
538
539
540
541
		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;
		}
542
	}
543

544
545
546
547
548
549
550
551
552
553
554
	/* 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;
		}
	}
Tristan Tarrant's avatar
Tristan Tarrant committed
555

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
	/*
	 * The following sequences do not trigger an editor-start
	 * but if the editor is running we forward the events to it.
	 */
	if (!gsheet->item_editor){
		
		if ((event->state & GDK_CONTROL_MASK) != 0) {

			switch (event->keyval) {

				/* Select all */
			case GDK_a:
				sheet_select_all (sheet);
				sheet_redraw_all (sheet);
				return 1;

			case GDK_space:
				sheet_selection_reset_only (sheet);
				sheet_selection_append_range (
					sheet,
					sheet->cursor_col, sheet->cursor_row,
					sheet->cursor_col, 0,
					sheet->cursor_col, SHEET_MAX_ROWS-1);
				sheet_redraw_all (sheet);
				return 1;
			}
Tristan Tarrant's avatar
Tristan Tarrant committed
582
		}
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

		if ((event->state & GDK_SHIFT_MASK) != 0){

			switch (event->keyval) {
				/* select row */
			case GDK_space:
				sheet_selection_reset_only (sheet);
				sheet_selection_append_range (
					sheet,
					sheet->cursor_col, sheet->cursor_row,
					0, sheet->cursor_row,
					SHEET_MAX_COLS-1, sheet->cursor_row);
				sheet_redraw_all (sheet);
				return 1;
			}
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
599

Tristan Tarrant's avatar
Tristan Tarrant committed
600
601
	}

602
603
	switch (event->keyval){
	case GDK_Left:
Arturo Espinosa's avatar
Arturo Espinosa committed
604
		(*movefn_horizontal)(gsheet, -1);
605
606
607
		break;

	case GDK_Right:
Arturo Espinosa's avatar
Arturo Espinosa committed
608
		(*movefn_horizontal)(gsheet, 1);
609
610
611
		break;

	case GDK_Up:
Arturo Espinosa's avatar
Arturo Espinosa committed
612
		(*movefn_vertical)(gsheet, -1);
613
614
615
		break;

	case GDK_Down:
Arturo Espinosa's avatar
Arturo Espinosa committed
616
		(*movefn_vertical)(gsheet, 1);
617
618
		break;

619
620
621
622
623
624
625
626
627
628
629
630
631
632
	case GDK_Page_Up:
	        (*movefn_vertical)(gsheet, -(gsheet->last_visible_row-gsheet->top_row));
		break;

	case GDK_Page_Down:
	        (*movefn_vertical)(gsheet, gsheet->last_visible_row-gsheet->top_row);
		break;

	case GDK_Home:
	        (*movefn_horizontal)(gsheet, -CURSOR_COL(gsheet));
		break;

	case GDK_KP_Delete:
	case GDK_Delete: 
Miguel de Icaza's avatar
Miguel de Icaza committed
633
		sheet_selection_clear (sheet);
634
635
		break;

636
	case GDK_Return:
Miguel de Icaza's avatar
Miguel de Icaza committed
637
638
639
640
		if ((event->state & GDK_CONTROL_MASK) != 0){
			if (gsheet->item_editor){
				Cell *cell;

641
				sheet_accept_pending_input (sheet);
Miguel de Icaza's avatar
Miguel de Icaza committed
642
643
644
645
				cell = sheet_cell_get (sheet,
						       sheet->cursor_col,
						       sheet->cursor_row);

646
				/* I am assuming sheet_accept_pending_input
Miguel de Icaza's avatar
Miguel de Icaza committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
				 * will always create the cell with the given
				 * input (based on the fact that we had an
				 * gsheet->item_editor when we entered this
				 * part of the code
				 */
				g_return_val_if_fail (cell != NULL, 1);
				g_return_val_if_fail (cell->entered_text != NULL, 1);
				sheet_fill_selection_with (
					sheet, cell->entered_text->str);
			}
			return 1;
		}
		/* fall down */
		
	case GDK_Tab:
662
663
664
665
666
667
668
669
670
671
	{
		int col, row;
		int walking_selection;
		int direction, horizontal;

		/* Figure out the direction */
		direction = (event->state & GDK_SHIFT_MASK) ? 0 : 1;
		horizontal = (event->keyval == GDK_Tab) ? 1 : 0;
		
		walking_selection = sheet_selection_walk_step (
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
672
673
			sheet, direction, horizontal,
			sheet->cursor_col, sheet->cursor_row,
674
675
			&col, &row);
		move_cursor (gsheet, col, row, walking_selection == 0);
676
		break;
677
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
678
679

	case GDK_Escape:
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
680
		sheet_cancel_pending_input (sheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
681
		break;
682
		
Arturo Espinosa's avatar
Arturo Espinosa committed
683
684
	case GDK_F2:
		gtk_window_set_focus (GTK_WINDOW (wb->toplevel), wb->ea_input);
685
		/* fall down */
686

687
	default:
Arturo Espinosa's avatar
Arturo Espinosa committed
688
		if (!gsheet->item_editor){
Arturo Espinosa's avatar
Arturo Espinosa committed
689
690
691
			if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
				return 0;
			
692
			if (event->keyval >= 0x20 && event->keyval <= 0xff)
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
693
			    sheet_start_editing_at_cursor (sheet);
694
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
695
		stop_cell_selection (gsheet);
696
		
Arturo Espinosa's avatar
Arturo Espinosa committed
697
		/* Forward the keystroke to the input line */
Arturo Espinosa's avatar
Arturo Espinosa committed
698
		gtk_widget_event (gsheet->entry, (GdkEvent *) event);
Arturo Espinosa's avatar
Arturo Espinosa committed
699
		
700
701
702
703
	}
	return 1;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
704
GtkWidget *
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
705
gnumeric_sheet_new (SheetView *sheet_view, ItemBar *colbar, ItemBar *rowbar)
Arturo Espinosa's avatar
Arturo Espinosa committed
706
{
707
	GnomeCanvasItem *item;
Arturo Espinosa's avatar
Arturo Espinosa committed
708
	GnumericSheet *gsheet;
709
710
711
	GnomeCanvas   *gsheet_canvas;
	GnomeCanvasGroup *gsheet_group;
	GtkWidget *widget;
712
	GtkWidget *entry;
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
713
714
715
716
717
	Sheet     *sheet;
	Workbook  *workbook;
	
	g_return_val_if_fail (sheet_view  != NULL, NULL);
	g_return_val_if_fail (IS_SHEET_VIEW (sheet_view), NULL);
718
719
720
721
722
	g_return_val_if_fail (colbar != NULL, NULL);
	g_return_val_if_fail (rowbar != NULL, NULL);
	g_return_val_if_fail (IS_ITEM_BAR (colbar), NULL);
	g_return_val_if_fail (IS_ITEM_BAR (rowbar), NULL);

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
723
724
725
726
727
	sheet = sheet_view->sheet;
	workbook = sheet->workbook;
	
	entry = workbook->ea_input;
	gsheet = gnumeric_sheet_create (sheet_view, entry);
Arturo Espinosa's avatar
Arturo Espinosa committed
728
	gnome_canvas_set_size (GNOME_CANVAS (gsheet), 300, 100);
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
729
	
730
731
	/* FIXME: figure out some real size for the canvas scrolling region */
	gnome_canvas_set_scroll_region (GNOME_CANVAS (gsheet), 0, 0, 1000000, 1000000);
732
733
734
735

	/* handy shortcuts */
	gsheet_canvas = GNOME_CANVAS (gsheet);
	gsheet_group = GNOME_CANVAS_GROUP (gsheet_canvas->root);
736
737
738

	gsheet->colbar = colbar;
	gsheet->rowbar = rowbar;
739
740
	
	/* The grid */
741
	item = gnome_canvas_item_new (gsheet_group,
742
				      item_grid_get_type (),
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
743
				      "ItemGrid::SheetView", sheet_view,
744
745
746
747
				      NULL);
	gsheet->item_grid = ITEM_GRID (item);

	/* The cursor */
748
	item = gnome_canvas_item_new (gsheet_group,
749
750
751
752
753
				      item_cursor_get_type (),
				      "ItemCursor::Sheet", sheet,
				      "ItemCursor::Grid", gsheet->item_grid,
				      NULL);
	gsheet->item_cursor = ITEM_CURSOR (item);
754
755
	item_cursor_set_bounds (gsheet->item_cursor, 0, 0, 1, 1);
		
756
757
758
759
760
	widget = GTK_WIDGET (gsheet);

	return widget;
}

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
gnumeric_sheet_pattern_t gnumeric_sheet_patterns [GNUMERIC_SHEET_PATTERNS] = {
	{ N_("75%"),
	  { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb } },

	{ N_("50%"),
	  { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 } },

	{ N_("25%"),
	  { 0x11, 0x44, 0x11, 0x44, 0x11, 0x44, 0x11, 0x44 } },

	{ N_("12%"),
	  { 0x01, 0x08, 0x40, 0x02, 0x10, 0x80, 0x04, 0x20 } },
	
	{ N_("6%"),
	  { 0x80, 0x00, 0x04, 0x00, 0x80, 0x00, 0x04, 0x00 } },

	{ N_("Horizontal lines"),
	  { 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00 } },

	{ N_("Vertical lines"),
	  { 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa } },

	{ N_("Diagonal lines"),
	  { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 } },

	{ N_("Diagonal lines"),
	  { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 } },

	{ N_("Diagonal lines"),
	  { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 } },

	{ N_("Diagonal lines"),
	  { 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11 } },

	{ N_("Crossed diagonals"),
	  { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } },

	{ N_("Bricks"),
	  { 0x80, 0x80, 0x80, 0xff, 0x04, 0x04, 0x04, 0xff } },
};

802

803
804
805
static void
gnumeric_sheet_realize (GtkWidget *widget)
{
806
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);
807
	GdkWindow *window;
808
809
	int i;
	
810
811
	if (GTK_WIDGET_CLASS (sheet_parent_class)->realize)
		(*GTK_WIDGET_CLASS (sheet_parent_class)->realize)(widget);
812

813
	window = widget->window;
814
	gdk_window_set_back_pixmap (GTK_LAYOUT (widget)->bin_window, NULL, FALSE);
815
816
817
818

	for (i = 0; i < GNUMERIC_SHEET_PATTERNS; i++)
		gsheet->patterns [i] = gdk_bitmap_create_from_data (
			window, gnumeric_sheet_patterns [i].pattern, 8, 8);
819

Arturo Espinosa's avatar
Arturo Espinosa committed
820
	cursor_set (window, GNUMERIC_CURSOR_FAT_CROSS);
Arturo Espinosa's avatar
Arturo Espinosa committed
821
822
}

823
824
void
gnumeric_sheet_compute_visible_ranges (GnumericSheet *gsheet)
825
{
826
	GnomeCanvas   *canvas = GNOME_CANVAS (gsheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
827
828
829
830
831
832
833
834
835
	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;
		
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
836
		ci = sheet_col_get_info (gsheet->sheet_view->sheet, col);
Arturo Espinosa's avatar
Arturo Espinosa committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
		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;
		
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
860
		ri = sheet_row_get_info (gsheet->sheet_view->sheet, row);
Arturo Espinosa's avatar
Arturo Espinosa committed
861
862
863
864
865
		cb = pixels + ri->pixels;
		
		if (cb == canvas->height){
			gsheet->last_visible_row = row;
			gsheet->last_full_row = row;
866
		} if (cb > canvas->height){
Arturo Espinosa's avatar
Arturo Espinosa committed
867
868
869
870
871
872
873
874
			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++;
875
876
	} while (pixels < canvas->height);
}
Arturo Espinosa's avatar
Arturo Espinosa committed
877

878
879
880
881
static int
gnumeric_sheet_set_top_row (GnumericSheet *gsheet, int new_top_row)
{
	GnomeCanvas *rowc = GNOME_CANVAS_ITEM (gsheet->rowbar)->canvas;
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
882
	Sheet *sheet = gsheet->sheet_view->sheet;
883
	int row_distance;
884
	int x;
885
886
887
	
	gsheet->top_row = new_top_row;
	row_distance = sheet_row_get_distance (sheet, 0, gsheet->top_row);
888
889
890

	gnome_canvas_get_scroll_offsets (rowc, &x, NULL);
	gnome_canvas_scroll_to (rowc, x, row_distance);
891
892
893
894
895
896
897
898
	
	return row_distance;
}

static int
gnumeric_sheet_set_top_col (GnumericSheet *gsheet, int new_top_col)
{
	GnomeCanvas *colc = GNOME_CANVAS_ITEM (gsheet->colbar)->canvas;
Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
899
	Sheet *sheet = gsheet->sheet_view->sheet;
900
	int col_distance;
901
	int y;
902
903
904

	gsheet->top_col = new_top_col;
	col_distance = sheet_col_get_distance (sheet, 0, gsheet->top_col);
905
906
907

	gnome_canvas_get_scroll_offsets (colc, NULL, &y);
	gnome_canvas_scroll_to (colc, col_distance, y);
908
909
910
911
	
	return col_distance;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
912
913
914
915
916
void
gnumeric_sheet_make_cell_visible (GnumericSheet *gsheet, int col, int row)
{
	GnomeCanvas *canvas;
	Sheet *sheet;
917
918
919
	int   did_change = 0;
	int   new_top_col, new_top_row;
	int   col_distance, row_distance;
Arturo Espinosa's avatar
Arturo Espinosa committed
920
921
922
923
924
925
926
927
	
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	g_return_if_fail (col >= 0);
	g_return_if_fail (row >= 0);
	g_return_if_fail (col < SHEET_MAX_COLS);
	g_return_if_fail (row < SHEET_MAX_ROWS);

Arturo Espinosa's avatar
Today:    
Arturo Espinosa committed
928
	sheet = gsheet->sheet_view->sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
929
930
	canvas = GNOME_CANVAS (gsheet);

931
	/* Find the new gsheet->top_col */
Arturo Espinosa's avatar
Arturo Espinosa committed
932
	if (col < gsheet->top_col){
933
		new_top_col = col;
934
	} else if (col > gsheet->last_full_col){
Arturo Espinosa's avatar
Arturo Espinosa committed
935
936
937
		ColRowInfo *ci;
		int width = canvas->width;
		int allocated = 0;
938
		int first_col;
Arturo Espinosa's avatar
Arturo Espinosa committed
939

940
		for (first_col = col; first_col > 0; first_col--){
Arturo Espinosa's avatar
Arturo Espinosa committed
941
			ci = sheet_col_get_info (sheet, first_col);
942
943
944

			if (allocated + ci->pixels > width)
				break;
Arturo Espinosa's avatar
Arturo Espinosa committed
945
			allocated += ci->pixels;
946
		}
947
948
949
950
951
952
953
		new_top_col = first_col+1;
	} else
		new_top_col = gsheet->top_col;

	/* Find the new gsheet->top_row */
	if (row < gsheet->top_row){
		new_top_row = row;
954
	} else if (row > gsheet->last_full_row){
955
		ColRowInfo *ri;
956
		int height = canvas->height;
957
		int allocated = 0;
958
		int first_row;
959

960
		for (first_row = row; first_row > 0; first_row--){
961
			ri = sheet_row_get_info (sheet, first_row);
962
963
964
			
			if (allocated + ri->pixels > height)
				break;
965
			allocated += ri->pixels;
966
		}
967
968
969
970
971
		new_top_row = first_row+1;
	} else
		new_top_row = gsheet->top_row;

	/* Determine if scrolling is required */
972
973

	gnome_canvas_get_scroll_offsets (GNOME_CANVAS (gsheet), &col_distance, &row_distance);
974
975
976
977
	
	if (gsheet->top_col != new_top_col){
		col_distance = gnumeric_sheet_set_top_col (gsheet, new_top_col);
		did_change = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
978
979
	}

980
981
982
983
984
985
986
987
	if (gsheet->top_row != new_top_row){
		row_distance = gnumeric_sheet_set_top_row (gsheet, new_top_row);
		did_change = 1;
	}

	if (!did_change)
		return;
	
Arturo Espinosa's avatar
Arturo Espinosa committed
988
	gnumeric_sheet_compute_visible_ranges (gsheet);
989

990
	gnome_canvas_scroll_to (GNOME_CANVAS (gsheet), col_distance, row_distance);
Arturo Espinosa's avatar
Arturo Espinosa committed
991
992
}

993
994
995
996
997
998
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));
999
1000
}

1001
1002
1003
1004
1005
1006
1007
1008
1009
static gint
gnumeric_button_press (GtkWidget *widget, GdkEventButton *event)
{
	if (!GTK_WIDGET_HAS_FOCUS (widget))
		gtk_widget_grab_focus (widget);

	return FALSE;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
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;
1025
	
1026
1027
1028
	widget_class->realize              = gnumeric_sheet_realize;
 	widget_class->size_allocate        = gnumeric_size_allocate;
	widget_class->key_press_event      = gnumeric_sheet_key;
Arturo Espinosa's avatar
Arturo Espinosa committed
1029
1030
}

Arturo Espinosa's avatar
Arturo Espinosa committed
1031
1032
1033
1034
static void
gnumeric_sheet_init (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
1035
	
1036
1037
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_DEFAULT);
1038
1039
}

1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
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
1051
			(GtkObjectInitFunc) gnumeric_sheet_init,
1052
1053
1054
1055
1056
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

Arturo Espinosa's avatar
Arturo Espinosa committed
1057
		gnumeric_sheet_type = gtk_type_unique (gnome_canvas_get_type (), &gnumeric_sheet_info);
1058
1059
1060
1061
	}

	return gnumeric_sheet_type;
}
Arturo Espinosa's avatar
Arturo Espinosa committed
1062