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

#include "gnumeric-sheet.h"
11 12 13 14
#include "item-bar.h"
#include "item-cursor.h"
#include "item-edit.h"
#include "item-grid.h"
15
#include "sheet-control-gui.h"
16
#include "gnumeric-util.h"
17
#include "style-color.h"
Jody Goldberg's avatar
Jody Goldberg committed
18
#include "selection.h"
19
#include "parse-util.h"
20
#include "ranges.h"
Jody Goldberg's avatar
Jody Goldberg committed
21
#include "sheet.h"
22
#include "application.h"
23
#include "workbook-view.h"
Miguel de Icaza's avatar
Miguel de Icaza committed
24
#include "workbook-edit.h"
25
#include "workbook-control-gui-priv.h"
26
#include "workbook.h"
27
#include "commands.h"
28 29 30 31

#ifdef ENABLE_BONOBO
#  include "sheet-object-container.h"
#endif
Jody Goldberg's avatar
Jody Goldberg committed
32
#include <gal/widgets/e-cursors.h>
33

34
static GnomeCanvasClass *gsheet_parent_class;
35

36 37 38 39 40 41
static void
gnumeric_sheet_destroy (GtkObject *object)
{
	GnumericSheet *gsheet;

	/* Add shutdown code here */
Arturo Espinosa's avatar
Arturo Espinosa committed
42
	gsheet = GNUMERIC_SHEET (object);
43

44 45
	if (GTK_OBJECT_CLASS (gsheet_parent_class)->destroy)
		(*GTK_OBJECT_CLASS (gsheet_parent_class)->destroy)(object);
46 47
}

48
/*
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
49
 * key press event handler for the gnumeric sheet for the sheet mode
50
 */
51
static gint
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
52
gnumeric_sheet_key_mode_sheet (GnumericSheet *gsheet, GdkEventKey *event)
53
{
54 55
	Sheet *sheet = gsheet->scg->sheet;
	WorkbookControlGUI *wbcg = gsheet->scg->wbcg;
56
	gboolean const jump_to_bounds = event->state & GDK_CONTROL_MASK;
57 58
	void (*movefn) (SheetControlGUI *, int n,
			gboolean jump, gboolean horiz);
59

60 61
	/* Magic : Some of these are accelerators,
	 * we need to catch them before entering because they appear to be printable
62
	 */
63 64 65 66
	if (!wbcg->editing && event->keyval == GDK_space &&
	    (event->state & (GDK_SHIFT_MASK|GDK_CONTROL_MASK)))
		return FALSE;

67 68
	if (scg_rangesel_possible (gsheet->scg)) {
		/* Ignore a few keys to avoid killing range selection cursor */
69
		switch (event->keyval) {
Miguel de Icaza's avatar
Miguel de Icaza committed
70 71 72
		case GDK_Shift_L:   case GDK_Shift_R:
		case GDK_Alt_L:     case GDK_Alt_R:
		case GDK_Control_L: case GDK_Control_R:
73 74
			return 1;
		}
Tristan Tarrant's avatar
Tristan Tarrant committed
75

76 77 78
		movefn = (event->state & GDK_SHIFT_MASK)
			? scg_rangesel_extend
			: scg_rangesel_move;
79
	} else {
80 81 82
		movefn = (event->state & GDK_SHIFT_MASK)
			? scg_cursor_extend
			: scg_cursor_move;
83
	}
Tristan Tarrant's avatar
Tristan Tarrant committed
84

85
	switch (event->keyval) {
Miguel de Icaza's avatar
Miguel de Icaza committed
86
	case GDK_KP_Left:
87
	case GDK_Left:
88
		(*movefn) (gsheet->scg, -1, jump_to_bounds, TRUE);
89 90
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
91
	case GDK_KP_Right:
92
	case GDK_Right:
93
		(*movefn) (gsheet->scg, 1, jump_to_bounds, TRUE);
94 95
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
96
	case GDK_KP_Up:
97
	case GDK_Up:
98
		(*movefn) (gsheet->scg, -1, jump_to_bounds, FALSE);
99 100
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
101
	case GDK_KP_Down:
102
	case GDK_Down:
103
		(*movefn) (gsheet->scg, 1, jump_to_bounds, FALSE);
104 105
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
106
	case GDK_KP_Page_Up:
107
	case GDK_Page_Up:
108
		if ((event->state & GDK_CONTROL_MASK) != 0)
Jody Goldberg's avatar
Jody Goldberg committed
109
			gtk_notebook_prev_page (wbcg->notebook);
110
		else if ((event->state & GDK_MOD1_MASK) == 0)
111 112 113
			(*movefn)( gsheet->scg,
				   -(gsheet->row.last_visible-gsheet->row.first),
				   FALSE, FALSE);
114
		else
115 116 117
			(*movefn)(gsheet->scg,
				  -(gsheet->col.last_visible-gsheet->col.first),
				  FALSE, TRUE);
118 119
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
120
	case GDK_KP_Page_Down:
121
	case GDK_Page_Down:
122
		if ((event->state & GDK_CONTROL_MASK) != 0)
Jody Goldberg's avatar
Jody Goldberg committed
123
			gtk_notebook_next_page (wbcg->notebook);
124
		else if ((event->state & GDK_MOD1_MASK) == 0)
125 126 127
			(*movefn)(gsheet->scg,
				  gsheet->row.last_visible-gsheet->row.first,
				  FALSE, FALSE);
128
		else
129 130 131
			(*movefn)(gsheet->scg,
				  gsheet->col.last_visible-gsheet->col.first,
				  FALSE, TRUE);
132 133
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
134
	case GDK_KP_Home:
135
	case GDK_Home:
136 137 138 139
		/* do the ctrl-home jump to A1 in 2 steps */
		(*movefn)(gsheet->scg, -SHEET_MAX_COLS, FALSE, TRUE);
		if ((event->state & GDK_CONTROL_MASK))
			(*movefn)(gsheet->scg, -SHEET_MAX_ROWS, FALSE, FALSE);
140 141 142
		break;

	case GDK_KP_Delete:
143
	case GDK_Delete:
144
		cmd_clear_selection (WORKBOOK_CONTROL (wbcg), sheet, CLEAR_VALUES);
145 146
		break;

Miguel de Icaza's avatar
Miguel de Icaza committed
147 148
	/*
	 * NOTE : Keep these in sync with the condition
149 150
	 *        for tabs.
	 */
Miguel de Icaza's avatar
Miguel de Icaza committed
151
	case GDK_KP_Enter:
152
	case GDK_Return:
153
		if (wbcg->editing &&
154 155 156 157
		    (event->state == GDK_CONTROL_MASK ||
		     event->state == (GDK_CONTROL_MASK|GDK_SHIFT_MASK) ||
		     event->state == GDK_MOD1_MASK))
			/* Forward the keystroke to the input line */
Miguel de Icaza's avatar
Miguel de Icaza committed
158
			return gtk_widget_event (
159
				GTK_WIDGET (wbcg_get_entry_logical (wbcg)),
Miguel de Icaza's avatar
Miguel de Icaza committed
160
				(GdkEvent *) event);
Miguel de Icaza's avatar
Miguel de Icaza committed
161
		/* fall down */
162

Miguel de Icaza's avatar
Miguel de Icaza committed
163
	case GDK_Tab:
164 165
	case GDK_ISO_Left_Tab:
	case GDK_KP_Tab:
166
	{
Jody Goldberg's avatar
Jody Goldberg committed
167
		/* Be careful to restore the editing sheet if we are editing */
168 169
		if (wbcg->editing)
			sheet = wbcg->editing_sheet;
170
			
171
		if (wbcg_edit_finish (wbcg, TRUE)) {
172 173 174 175 176 177 178
			/* Figure out the direction */
			gboolean const direction = (event->state & GDK_SHIFT_MASK) ? FALSE : TRUE;
			gboolean const horizontal = (event->keyval == GDK_KP_Enter ||
						     event->keyval == GDK_Return) ? FALSE : TRUE;

			sheet_selection_walk_step (sheet, direction, horizontal);
		}
179
		break;
180
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
181 182

	case GDK_Escape:
183
		wbcg_edit_finish (wbcg, FALSE);
184
		application_clipboard_unant ();
Arturo Espinosa's avatar
Arturo Espinosa committed
185
		break;
186

Jody Goldberg's avatar
Jody Goldberg committed
187
	case GDK_F4:
188
		if (wbcg->editing && gsheet->sel_cursor)
189
			wbcg_edit_toggle_absolute (wbcg);
Jody Goldberg's avatar
Jody Goldberg committed
190 191
		break;

Arturo Espinosa's avatar
Arturo Espinosa committed
192
	case GDK_F2:
193
		wbcg_edit_start (wbcg, FALSE, FALSE);
194
		/* fall down */
195

196
	default:
197
		if (!wbcg->editing) {
198
			if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0)
199
				return FALSE;
200

Jody Goldberg's avatar
Jody Goldberg committed
201 202
			/* If the character is not printable do not start editing */
			if (event->length == 0)
203
				return FALSE;
Jody Goldberg's avatar
Jody Goldberg committed
204

205
			wbcg_edit_start (wbcg, TRUE, TRUE);
206
		}
207
		scg_rangesel_stop (gsheet->scg, FALSE);
208

Arturo Espinosa's avatar
Arturo Espinosa committed
209
		/* Forward the keystroke to the input line */
210
		return gtk_widget_event (GTK_WIDGET (wbcg_get_entry_logical (wbcg)),
Miguel de Icaza's avatar
Miguel de Icaza committed
211
					 (GdkEvent *) event);
212
	}
213 214 215 216

	if (wbcg->editing)
		sheet_update_only_grid (sheet);
	else
217
		sheet_update (sheet);
218 219

	return TRUE;
220 221
}

Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
222 223 224
static gint
gnumeric_sheet_key_mode_object (GnumericSheet *gsheet, GdkEventKey *event)
{
225
	SheetControlGUI *scg = gsheet->scg;
226

227
	switch (event->keyval) {
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
228
	case GDK_Escape:
229
		scg_mode_edit (scg);
230
		application_clipboard_unant ();
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
231 232
		break;

233 234
	case GDK_BackSpace: /* Ick! */
	case GDK_KP_Delete:
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
235
	case GDK_Delete:
236
		gtk_object_destroy (GTK_OBJECT (scg->current_object));
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
237 238 239 240 241 242 243 244 245
		break;

	default:
		return FALSE;
	}
	return TRUE;
}

static gint
246
gnumeric_sheet_key_press (GtkWidget *widget, GdkEventKey *event)
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
247 248
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);
249
	SheetControlGUI *scg = gsheet->scg;
250

251
	if (scg->current_object != NULL || scg->new_object != NULL)
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
252
		return gnumeric_sheet_key_mode_object (gsheet, event);
253
	return gnumeric_sheet_key_mode_sheet (gsheet, event);
Miguel de Icaza's avatar
New:  
Miguel de Icaza committed
254 255
}

256 257 258 259
static gint
gnumeric_sheet_key_release (GtkWidget *widget, GdkEventKey *event)
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);
260
	SheetControlGUI *scg = gsheet->scg;
261 262 263 264 265 266 267 268

	/*
	 * The status_region normally displays the current edit_pos
	 * When we extend the selection it changes to displaying the size of
	 * the selected region while we are selecting.  When the shift key
	 * is released, or the mouse button is release we need to reset
	 * to displaying the edit pos.
	 */
269
	if (scg->current_object == NULL &&
270
	    (event->keyval == GDK_Shift_L || event->keyval == GDK_Shift_R))
271 272
		wb_view_selection_desc (wb_control_view (
			WORKBOOK_CONTROL (gsheet->scg->wbcg)), TRUE, NULL);
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 301 302 303 304 305 306
	return (*GTK_WIDGET_CLASS (gsheet_parent_class)->key_release_event)(widget, event);
}

static gint
gnumeric_sheet_button_release (GtkWidget *widget, GdkEventButton *button)
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);

	if (button->button != 4 && button->button != 5)
		return (*GTK_WIDGET_CLASS (gsheet_parent_class)->button_release_event)(widget, button);

	/* Roll Up or Left */
	/* Roll Down or Right */
	if ((button->state & GDK_MOD1_MASK)) {
		int col = gsheet->col.last_full - gsheet->col.first;
		if (button->button == 4)
			col = MAX (gsheet->col.first - col, 0);
		else if (gsheet->col.last_full < SHEET_MAX_COLS-1)
			col = gsheet->col.last_full;
		else
			return FALSE;
		gnumeric_sheet_set_left_col (gsheet, col);
	} else {
		int row = gsheet->row.last_full - gsheet->row.first;
		if (button->button == 4)
			row = MAX (gsheet->row.first - row, 0);
		else if (gsheet->row.last_full < SHEET_MAX_ROWS-1)
			row = gsheet->row.last_full;
		else
			return FALSE;
		gnumeric_sheet_set_top_row (gsheet, row);
	}
	return TRUE;
307 308
}

309 310 311 312 313 314 315
/* Focus in handler for the canvas */
static gint
gnumeric_sheet_focus_in (GtkWidget *widget, GdkEventFocus *event)
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (widget);
	if (gsheet->ic)
		gdk_im_begin (gsheet->ic, gsheet->canvas.layout.bin_window);
316
	return (*GTK_WIDGET_CLASS (gsheet_parent_class)->focus_in_event)(widget, event);
317 318 319 320 321 322 323
}

/* Focus out handler for the canvas */
static gint
gnumeric_sheet_focus_out (GtkWidget *widget, GdkEventFocus *event)
{
	gdk_im_end ();
324
	return (*GTK_WIDGET_CLASS (gsheet_parent_class)->focus_out_event)(widget, event);
325 326
}

327 328 329 330 331 332 333
static void
gnumeric_sheet_drag_data_get (GtkWidget *widget,
			      GdkDragContext *context,
			      GtkSelectionData *selection_data,
			      guint info,
			      guint time)
{
334
#if 0
335
	BonoboMoniker *moniker;
336
	Sheet *sheet = GNUMERIC_SHEET (widget)->scg->sheet;
337 338
	Workbook *wb = sheet->workbook;
	char *s;
339
	WorkbookControl *wbc = WORKBOOK_CONTROL (gsheet->scg->wbcg);
340

341
	if (wb->filename == NULL)
342
		workbook_save (wbc, wb);
343 344
	if (wb->filename == NULL)
		return;
345

346 347
	moniker = bonobo_moniker_new ();
	bonobo_moniker_set_server (
348 349 350 351
		moniker,
		"IDL:GNOME:Gnumeric:Workbook:1.0",
		wb->filename);

352
	bonobo_moniker_append_item_name (
353
		moniker, "Sheet1");
354
	s = bonobo_moniker_get_as_string (moniker);
355 356 357
	gtk_object_destroy (GTK_OBJECT (moniker));

	gtk_selection_data_set (selection_data, selection_data->target, 8, s, strlen (s)+1);
358
#endif
359 360
}

361 362 363
/*
 * gnumeric_sheet_filenames_dropped :
 */
364 365 366 367 368 369 370 371 372 373 374
static void
gnumeric_sheet_filenames_dropped (GtkWidget        *widget,
				  GdkDragContext   *context,
				  gint              x,
				  gint              y,
				  GtkSelectionData *selection_data,
				  guint             info,
				  guint             time,
				  GnumericSheet    *gsheet)
{
	GList *names, *tmp_list;
375
	WorkbookControl *wbc = WORKBOOK_CONTROL (gsheet->scg->wbcg);
376 377

	names = gnome_uri_list_extract_filenames ((char *)selection_data->data);
378

379 380 381
	for (tmp_list = names; tmp_list != NULL; tmp_list = tmp_list->next) {
		gchar *file_name = tmp_list->data;
		WorkbookView *new_wb = workbook_try_read (wbc, file_name);
382

383 384 385
		if (new_wb != NULL) {
			(void) file_finish_load (wbc, new_wb);
		} else {
386 387 388
#ifdef ENABLE_BONOBO
			/* If it wasn't a workbook, see if we have a control for it */
			SheetObject *so = sheet_object_container_new_file (
389
			                  gsheet->scg->sheet, file_name);
390
			if (so != NULL)
391
				scg_mode_create_object (gsheet->scg, so);
392 393 394 395 396 397 398
#else
			gchar *msg;

			msg = g_strdup_printf (_("File \"%s\" has unknown format."),
			                       file_name);
			gnumeric_error_read (COMMAND_CONTEXT (wbc), msg);
			g_free (msg);
399
#endif
400
		}
401 402 403
	}
}

404 405 406
static void
gnumeric_sheet_realize (GtkWidget *widget)
{
407
	GdkWindow *window;
408
	GnumericSheet *gsheet;
409

410 411
	if (GTK_WIDGET_CLASS (gsheet_parent_class)->realize)
		(*GTK_WIDGET_CLASS (gsheet_parent_class)->realize)(widget);
412

413
	window = widget->window;
414
	gdk_window_set_back_pixmap (GTK_LAYOUT (widget)->bin_window, NULL, FALSE);
415

416
	e_cursor_set (window, E_CURSOR_FAT_CROSS);
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

	gsheet = GNUMERIC_SHEET (widget);
	if (gdk_im_ready () && (gsheet->ic_attr = gdk_ic_attr_new ()) != NULL) {
		GdkEventMask mask;
		GdkICAttr *attr = gsheet->ic_attr;
		GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
		GdkIMStyle style;
		GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
			GDK_IM_PREEDIT_NOTHING |
			GDK_IM_STATUS_NONE |
			GDK_IM_STATUS_NOTHING;

		attr->style = style = gdk_im_decide_style (supported_style);
		attr->client_window = gsheet->canvas.layout.bin_window;

		gsheet->ic = gdk_ic_new (attr, attrmask);
		if (gsheet->ic != NULL) {
			mask = gdk_window_get_events (attr->client_window);
			mask |= gdk_ic_get_events (gsheet->ic);
			gdk_window_set_events (attr->client_window, mask);

			if (GTK_WIDGET_HAS_FOCUS (widget))
				gdk_im_begin (gsheet->ic, attr->client_window);
		} else
			g_warning ("Can't create input context.");
	}
}

static void
gnumeric_sheet_unrealize (GtkWidget *widget)
{
	GnumericSheet *gsheet;

	gsheet = GNUMERIC_SHEET (widget);
	g_return_if_fail (gsheet != NULL);

	if (gsheet->ic) {
		gdk_ic_destroy (gsheet->ic);
		gsheet->ic = NULL;
	}
	if (gsheet->ic_attr) {
		gdk_ic_attr_destroy (gsheet->ic_attr);
		gsheet->ic_attr = NULL;
	}

462
	(*GTK_WIDGET_CLASS (gsheet_parent_class)->unrealize)(widget);
Arturo Espinosa's avatar
Arturo Espinosa committed
463 464
}

465 466
static void
gnumeric_sheet_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
467
{
468
	(*GTK_WIDGET_CLASS (gsheet_parent_class)->size_allocate)(widget, allocation);
Jody Goldberg's avatar
Jody Goldberg committed
469

470 471
	gsheet_compute_visible_region (GNUMERIC_SHEET (widget), FALSE);
}
Jody Goldberg's avatar
Jody Goldberg committed
472

473 474 475
typedef struct {
	GnomeCanvasClass parent_class;
} GnumericSheetClass;
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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 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 758 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 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 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 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
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;

	gsheet_parent_class = gtk_type_class (gnome_canvas_get_type ());

	/* Method override */
	object_class->destroy = gnumeric_sheet_destroy;

	widget_class->realize		   = gnumeric_sheet_realize;
	widget_class->unrealize		   = gnumeric_sheet_unrealize;
 	widget_class->size_allocate	   = gnumeric_sheet_size_allocate;
	widget_class->key_press_event	   = gnumeric_sheet_key_press;
	widget_class->key_release_event	   = gnumeric_sheet_key_release;
	widget_class->button_release_event = gnumeric_sheet_button_release;
	widget_class->focus_in_event	   = gnumeric_sheet_focus_in;
	widget_class->focus_out_event	   = gnumeric_sheet_focus_out;
}

static void
gnumeric_sheet_init (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);

	gsheet->ic = NULL;
	gsheet->ic_attr = NULL;

	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_DEFAULT);
}

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,
			(GtkObjectInitFunc) gnumeric_sheet_init,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

		gnumeric_sheet_type = gtk_type_unique (gnome_canvas_get_type (), &gnumeric_sheet_info);
	}

	return gnumeric_sheet_type;
}

GtkWidget *
gnumeric_sheet_new (SheetControlGUI *scg)
{
	static GtkTargetEntry const drag_types[] = {
		{ "text/uri-list", 0, 0 },
	};
	static gint const n_drag_types = sizeof (drag_types) / sizeof (drag_types [0]);

	GnomeCanvasItem	 *item;
	GnumericSheet	 *gsheet;
	GnomeCanvasGroup *gsheet_group;
	GtkWidget	 *widget;
	Range		  r;

	g_return_val_if_fail (IS_SHEET_CONTROL_GUI (scg), NULL);

	gsheet = gtk_type_new (gnumeric_sheet_get_type ());
	gsheet_group = GNOME_CANVAS_GROUP (GNOME_CANVAS (gsheet)->root);
	widget = GTK_WIDGET (gsheet);

	gsheet->scg = scg;
	gsheet->row.first = gsheet->row.last_full = gsheet->row.last_visible = 0;
	gsheet->col.first = gsheet->col.last_full = gsheet->col.last_visible = 0;
	gsheet->row_offset.first = gsheet->row_offset.last_full = gsheet->row_offset.last_visible = 0;
	gsheet->col_offset.first = gsheet->col_offset.last_full = gsheet->col_offset.last_visible = 0;

	/* FIXME: figure out some real size for the canvas scrolling region */
	gnome_canvas_set_scroll_region (GNOME_CANVAS (gsheet), 0, 0,
					GNUMERIC_SHEET_FACTOR_X, GNUMERIC_SHEET_FACTOR_Y);

	/* The grid */
	item = gnome_canvas_item_new (gsheet_group,
		item_grid_get_type (),
		"ItemGrid::SheetControlGUI", scg,
		NULL);
	gsheet->item_grid = ITEM_GRID (item);

	/* The cursor */
	item = gnome_canvas_item_new (gsheet_group,
		item_cursor_get_type (),
		"ItemCursor::SheetControlGUI", scg,
		NULL);
	gsheet->item_cursor = ITEM_CURSOR (item);
	gnumeric_sheet_cursor_bound (gsheet, range_init (&r, 0, 0, 0, 0)); /* A1 */

	/* Setup a test of Drag and Drop */
	gtk_signal_connect (GTK_OBJECT (widget),
		"drag_data_get",
		GTK_SIGNAL_FUNC (gnumeric_sheet_drag_data_get), NULL);
	gtk_drag_dest_set (widget,
		GTK_DEST_DEFAULT_ALL,
		drag_types, n_drag_types,
		GDK_ACTION_COPY);
	gtk_signal_connect (GTK_OBJECT (widget),
		"drag_data_received",
		GTK_SIGNAL_FUNC (gnumeric_sheet_filenames_dropped),
		widget);

	return widget;
}

static int
gnumeric_sheet_bar_set_top_row (GnumericSheet *gsheet, int new_first_row)
{
	GnomeCanvas *rowc;
	int row_offset;
	int x;

	g_return_val_if_fail (gsheet != NULL, 0);
	g_return_val_if_fail (gsheet->item_grid != NULL, 0);
	g_return_val_if_fail (0 <= new_first_row && new_first_row < SHEET_MAX_ROWS, 0);

	rowc = gsheet->scg->row_item->canvas;
	row_offset = gsheet->row_offset.first +=
		scg_colrow_distance_get (gsheet->scg, FALSE, gsheet->row.first, new_first_row);
	gsheet->row.first = new_first_row;

	/* Scroll the row headers */
	gnome_canvas_get_scroll_offsets (rowc, &x, NULL);
	gnome_canvas_scroll_to (rowc, x, row_offset);

	return row_offset;
}

void
gnumeric_sheet_set_top_row (GnumericSheet *gsheet, int new_first_row)
{
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (0 <= new_first_row && new_first_row < SHEET_MAX_ROWS);

	if (gsheet->row.first != new_first_row) {
		int x;
		GnomeCanvas * const canvas = GNOME_CANVAS(gsheet);
		int const y_offset =
			gnumeric_sheet_bar_set_top_row (gsheet, new_first_row);

		gsheet_compute_visible_region (gsheet, FALSE);

		/* Scroll the cell canvas */
		gnome_canvas_get_scroll_offsets (canvas, &x, NULL);
		gnome_canvas_scroll_to (canvas, x, y_offset);
	}
}

static int
gnumeric_sheet_bar_set_left_col (GnumericSheet *gsheet, int new_first_col)
{
	GnomeCanvas *colc;
	int col_offset;
	int y;

	g_return_val_if_fail (gsheet != NULL, 0);
	g_return_val_if_fail (gsheet->item_grid != NULL, 0);
	g_return_val_if_fail (0 <= new_first_col && new_first_col < SHEET_MAX_COLS, 0);

	colc = gsheet->scg->col_item->canvas;
	col_offset = gsheet->col_offset.first +=
		scg_colrow_distance_get (gsheet->scg, TRUE, gsheet->col.first, new_first_col);
	gsheet->col.first = new_first_col;

	/* Scroll the column headers */
	gnome_canvas_get_scroll_offsets (colc, NULL, &y);
	gnome_canvas_scroll_to (colc, col_offset, y);

	return col_offset;
}

void
gnumeric_sheet_set_left_col (GnumericSheet *gsheet, int new_first_col)
{
	g_return_if_fail (gsheet != NULL);
	g_return_if_fail (0 <= new_first_col && new_first_col < SHEET_MAX_COLS);

	if (gsheet->col.first != new_first_col) {
		int y;
		GnomeCanvas * const canvas = GNOME_CANVAS(gsheet);
		int const x_offset =
			gnumeric_sheet_bar_set_left_col (gsheet, new_first_col);

		gsheet_compute_visible_region (gsheet, FALSE);

		/* Scroll the cell canvas */
		gnome_canvas_get_scroll_offsets (canvas, NULL, &y);
		gnome_canvas_scroll_to (canvas, x_offset, y);
	}
}

/*
 * gnumeric_sheet_find_col: return the column containing pixel x
 */
int
gnumeric_sheet_find_col (GnumericSheet *gsheet, int x, int *col_origin)
{
	Sheet *sheet = gsheet->scg->sheet;
	int col   = gsheet->col.first;
	int pixel = gsheet->col_offset.first;

	if (x < pixel) {
		while (col > 0) {
			ColRowInfo *ci = sheet_col_get_info (sheet, --col);
			if (ci->visible) {
				pixel -= ci->size_pixels;
				if (x >= pixel) {
					if (col_origin)
						*col_origin = pixel;
					return col;
				}
			}
		}
		if (col_origin)
			*col_origin = 0;
		return 0;
	}

	do {
		ColRowInfo *ci = sheet_col_get_info (sheet, col);
		if (ci->visible) {
			int const tmp = ci->size_pixels;
			if (x <= pixel + tmp) {
				if (col_origin)
					*col_origin = pixel;
				return col;
			}
			pixel += tmp;
		}
	} while (++col < SHEET_MAX_COLS-1);
	if (col_origin)
		*col_origin = pixel;
	return SHEET_MAX_COLS-1;
}

/*
 * gnumeric_sheet_find_row: return the row where y belongs to
 */
int
gnumeric_sheet_find_row (GnumericSheet *gsheet, int y, int *row_origin)
{
	Sheet *sheet = gsheet->scg->sheet;
	int row   = gsheet->row.first;
	int pixel = gsheet->row_offset.first;

	if (y < pixel) {
		while (row > 0) {
			ColRowInfo *ri = sheet_row_get_info (sheet, --row);
			if (ri->visible) {
				pixel -= ri->size_pixels;
				if (y >= pixel) {
					if (row_origin)
						*row_origin = pixel;
					return row;
				}
			}
		}
		if (row_origin)
			*row_origin = 0;
		return 0;
	}

	do {
		ColRowInfo *ri = sheet_row_get_info (sheet, row);
		if (ri->visible) {
			int const tmp = ri->size_pixels;
			if (pixel <= y && y <= pixel + tmp) {
				if (row_origin)
					*row_origin = pixel;
				return row;
			}
			pixel += tmp;
		}
	} while (++row < SHEET_MAX_ROWS-1);
	if (row_origin)
		*row_origin = pixel;
	return SHEET_MAX_ROWS-1;
}

void
gnumeric_sheet_create_editor (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
	GnomeCanvasItem *item;

	g_return_if_fail (gsheet->item_editor == NULL);

	item = gnome_canvas_item_new (GNOME_CANVAS_GROUP (canvas->root),
				      item_edit_get_type (),
				      "ItemEdit::SheetControlGUI",     gsheet->scg,
				      NULL);

	gsheet->item_editor = ITEM_EDIT (item);
}

void
gnumeric_sheet_stop_editing (GnumericSheet *gsheet)
{
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));

	if (gsheet->item_editor != NULL) {
		gtk_object_destroy (GTK_OBJECT (gsheet->item_editor));
		gsheet->item_editor = NULL;
	}
}

void
gnumeric_sheet_cursor_bound (GnumericSheet *gsheet, Range const *r)
{
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	item_cursor_set_bounds (gsheet->item_cursor, r);
}

void
gnumeric_sheet_rangesel_bound (GnumericSheet *gsheet, Range const *r)
{
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	item_cursor_set_bounds (gsheet->sel_cursor, r);
}

void
gnumeric_sheet_rangesel_start (GnumericSheet *gsheet, int col, int row)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);
	GnomeCanvasItem *tmp;
	GnomeCanvasGroup *group = GNOME_CANVAS_GROUP (canvas->root);
	Sheet *sheet = gsheet->scg->sheet;
	WorkbookControlGUI *wbcg = gsheet->scg->wbcg;
	Range r;

	g_return_if_fail (gsheet->sel_cursor == NULL);

	/* Hide the primary cursor while the range selection cursor is visible
	 * and we are selecting on a different sheet than the expr being edited
	 */
	if (sheet != wb_control_cur_sheet (WORKBOOK_CONTROL (wbcg)))
		item_cursor_set_visibility (gsheet->item_cursor, FALSE);

	tmp = gnome_canvas_item_new (group,
		item_cursor_get_type (),
		"SheetControlGUI", gsheet->scg,
		"Style", ITEM_CURSOR_ANTED, NULL);
	gsheet->sel_cursor = ITEM_CURSOR (tmp);
	item_cursor_set_bounds (gsheet->sel_cursor,
		range_init (&r, col, row, col, row));

	/* If we are selecting a range on a different sheet this may be NULL */
	if (gsheet->item_editor)
		item_edit_disable_highlight (ITEM_EDIT (gsheet->item_editor));
}

void
gnumeric_sheet_rangesel_stop (GnumericSheet *gsheet)
{
	g_return_if_fail (GNUMERIC_IS_SHEET (gsheet));
	g_return_if_fail (gsheet->sel_cursor != NULL);

	gtk_object_destroy (GTK_OBJECT (gsheet->sel_cursor));
	gsheet->sel_cursor = NULL;

	/* If we are selecting a range on a different sheet this may be NULL */
	if (gsheet->item_editor)
		item_edit_enable_highlight (ITEM_EDIT (gsheet->item_editor));

	/* Make the primary cursor visible again */
	item_cursor_set_visibility (gsheet->item_cursor, TRUE);
}

/*
 * gsheet_compute_visible_region : Keeps the top left col/row the same and
 *     recalculates the visible boundaries.
 *
 * @full_recompute :
 *       if TRUE recompute the pixel offsets of the top left row/col
 *       else assumes that the pixel offsets of the top left have not changed.
 */
void
gsheet_compute_visible_region (GnumericSheet *gsheet,
			       gboolean const full_recompute)
{
	SheetControlGUI const * const scg = gsheet->scg;
	Sheet const * const sheet = scg->sheet;
	GnomeCanvas   *canvas = GNOME_CANVAS (gsheet);
	int pixels, col, row, width, height;

	/* When col/row sizes change we need to do a full recompute */
	if (full_recompute) {
		gsheet->col_offset.first =
			scg_colrow_distance_get (scg, TRUE, 0, gsheet->col.first);
		gnome_canvas_scroll_to (scg->col_item->canvas,
			gsheet->col_offset.first, 0);
		gsheet->row_offset.first =
			scg_colrow_distance_get (scg, FALSE, 0, gsheet->row.first);
		gnome_canvas_scroll_to (scg->row_item->canvas,
			0, gsheet->row_offset.first);

		gnome_canvas_scroll_to (GNOME_CANVAS (gsheet),
					gsheet->col_offset.first,
					gsheet->row_offset.first);
	}

	/* Find out the last visible col and the last full visible column */
	pixels = 0;
	col = gsheet->col.first;
	width = GTK_WIDGET (canvas)->allocation.width;

	do {
Jody Goldberg's avatar
Jody Goldberg committed
901
		ColRowInfo const * const ci = sheet_col_get_info (sheet, col);
902 903
		if (ci->visible) {
			int const bound = pixels + ci->size_pixels;
904

905
			if (bound == width) {
906 907
				gsheet->col.last_visible = col;
				gsheet->col.last_full = col;
908
				break;
909
			}
910
			if (bound > width) {
911 912 913
				gsheet->col.last_visible = col;
				if (col == gsheet->col.first)
					gsheet->col.last_full = gsheet->col.first;
914
				else
915
					gsheet->col.last_full = col - 1;
916 917
				break;
			}
918
			pixels = bound;
Arturo Espinosa's avatar
Arturo Espinosa committed
919
		}
920 921 922 923
		++col;
	} while (pixels < width && col < SHEET_MAX_COLS);

	if (col >= SHEET_MAX_COLS) {
924 925
		gsheet->col.last_visible = SHEET_MAX_COLS-1;
		gsheet->col.last_full = SHEET_MAX_COLS-1;
926
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
927 928 929

	/* Find out the last visible row and the last fully visible row */
	pixels = 0;
930
	row = gsheet->row.first;
931
	height = GTK_WIDGET (canvas)->allocation.height;
Arturo Espinosa's avatar
Arturo Espinosa committed
932
	do {
Jody Goldberg's avatar
Jody Goldberg committed
933
		ColRowInfo const * const ri = sheet_row_get_info (sheet, row);
934 935
		if (ri->visible) {
			int const bound = pixels + ri->size_pixels;
936

937
			if (bound == height) {
938 939
				gsheet->row.last_visible = row;
				gsheet->row.last_full = row;
940
				break;
941
			}
942
			if (bound > height) {
943 944 945
				gsheet->row.last_visible = row;
				if (row == gsheet->row.first)
					gsheet->row.last_full = gsheet->row.first;
946
				else
947
					gsheet->row.last_full = row - 1;
948 949
				break;
			}
950
			pixels = bound;
Arturo Espinosa's avatar
Arturo Espinosa committed
951
		}
952
		++row;
953 954
	} while (pixels < height && row < SHEET_MAX_ROWS);

Miguel de Icaza's avatar
Miguel de Icaza committed
955
	if (row >= SHEET_MAX_ROWS) {
956 957
		gsheet->row.last_visible = SHEET_MAX_ROWS-1;
		gsheet->row.last_full = SHEET_MAX_ROWS-1;
958
	}
Jody Goldberg's avatar
Jody Goldberg committed
959 960

	/* Update the scrollbar sizes */
961
	scg_scrollbar_config (scg);
962 963

	/* Force the cursor to update its bounds relative to the new visible region */
964
	item_cursor_reposition (gsheet->item_cursor);
965
}
Arturo Espinosa's avatar
Arturo Espinosa committed
966

967
/**
968 969 970 971 972 973 974 975
 * gnumeric_sheet_make_cell_visible
 * @gsheet        sheet widget
 * @col           column
 * @row           row
 * @force_scroll  force a scroll
 *
 * Ensure that cell (col, row) is visible.
 * Sheet is scrolled if cell is outside viewport.
976
 *
977 978 979 980
 * Avoid calling this before the canvas is realized:
 * We do not know the visible area, and would unconditionally scroll the cell
 * to the top left of the viewport.
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
981
void
982 983
gnumeric_sheet_make_cell_visible (GnumericSheet *gsheet, int col, int row,
				  gboolean const force_scroll)
Arturo Espinosa's avatar
Arturo Espinosa committed
984 985 986
{
	GnomeCanvas *canvas;
	Sheet *sheet;
987
	int   did_change = 0;
988
	int   new_first_col, new_first_row;
989
	int   col_offset, row_offset;
990

Arturo Espinosa's avatar
Arturo Espinosa committed
991 992 993 994 995 996
	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);

997
	sheet = gsheet->scg->sheet;
Arturo Espinosa's avatar
Arturo Espinosa committed
998 999
	canvas = GNOME_CANVAS (gsheet);

1000
	/* Find the new gsheet->col.first */
1001
	if (col < gsheet->col.first) {
1002
		new_first_col = col;
1003
	} else if (col > gsheet->col.last_full) {
1004
		int width = GTK_WIDGET (canvas)->allocation.width;
Jody Goldberg's avatar
typo.  
Jody Goldberg committed
1005 1006
		int first_col = (gsheet->col.last_visible == gsheet->col.first)
			? gsheet->col.first : col;
Arturo Espinosa's avatar
Arturo Espinosa committed
1007

Jody Goldberg's avatar
typo.  
Jody Goldberg committed
1008
		for (; first_col > 0; --first_col) {
1009
			ColRowInfo const * const ci = sheet_col_get_info (sheet, first_col);
1010
			if (ci->visible) {
1011 1012
				width -= ci->size_pixels;
				if (width < 0)
1013 1014
					break;
			}
1015
		}
1016
		new_first_col = first_col+1;
1017
	} else
1018
		new_first_col = gsheet->col.first;
1019

1020
	/* Find the new gsheet->row.first */
1021
	if (row < gsheet->row.first) {
1022
		new_first_row = row;
1023
	} else if (row > gsheet->row.last_full) {
1024
		int height = GTK_WIDGET (canvas)->allocation.height;
Jody Goldberg's avatar
typo.  
Jody Goldberg committed
1025 1026
		int first_row = (gsheet->row.last_visible == gsheet->row.first)
			? gsheet->row.first : row;
1027

Jody Goldberg's avatar
typo.  
Jody Goldberg committed
1028
		for (; first_row > 0; --first_row) {
1029
			ColRowInfo const * const ri = sheet_row_get_info (sheet, first_row);
1030
			if (ri->visible) {
1031 1032
				height -= ri->size_pixels;
				if (height < 0)
1033 1034
					break;
			}
1035
		}
1036
		new_first_row = first_row+1;
1037
	} else
1038
		new_first_row = gsheet->row.first;
1039 1040

	/* Determine if scrolling is required */
1041
	gnome_canvas_get_scroll_offsets (GNOME_CANVAS (gsheet), &col_offset, &row_offset);
1042

1043
	if (gsheet->col.first != new_first_col || force_scroll) {
Jody Goldberg's avatar
Jody Goldberg committed
1044 1045 1046 1047 1048
		if (force_scroll) {
			/* Clear the offsets in case col/row size changed */
			gsheet->col_offset.first = 0;
			gsheet->col.first = 0;
		}
1049
		col_offset = gnumeric_sheet_bar_set_left_col (gsheet, new_first_col);
1050
		did_change = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1051 1052
	}

1053
	if (gsheet->row.first != new_first_row || force_scroll) {
Jody Goldberg's avatar
Jody Goldberg committed
1054 1055 1056 1057 1058
		if (force_scroll) {
			/* Clear the offsets in case col/row size changed */
			gsheet->row_offset.first = 0;
			gsheet->row.first = 0;
		}
1059
		row_offset = gnumeric_sheet_bar_set_top_row (gsheet, new_first_row);
1060 1061 1062
		did_change = 1;
	}

1063
	if (!did_change && !force_scroll)
1064
		return;
1065

1066
	gsheet_compute_visible_region (gsheet, FALSE);
1067

1068
	gnome_canvas_scroll_to (GNOME_CANVAS (gsheet), col_offset, row_offset);
Arturo Espinosa's avatar
Arturo Espinosa committed
1069 1070
}