gnumeric-canvas.c 4.92 KB
Newer Older
1
#include <config.h>
2 3

#include <gnome.h>
4 5
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
6 7
#include "gnumeric.h"
#include "gnumeric-sheet.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/* Signals emited by the Gnumeric Sheet widget */
enum {
	GNUMERIC_SHEET_LAST_SIGNAL
};

static guint sheet_signals [GNUMERIC_SHEET_LAST_SIGNAL] = { 0 };

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
24 25
	gsheet = GNUMERIC_SHEET (object);
	
26 27 28 29
	if (GTK_OBJECT_CLASS (sheet_parent_class)->destroy)
		(*GTK_OBJECT_CLASS (sheet_parent_class)->destroy)(object);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
30 31
static GnumericSheet *
gnumeric_sheet_create (Sheet *sheet)
Arturo Espinosa's avatar
Arturo Espinosa committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
{
	GnumericSheet *gsheet;
	GnomeCanvas   *canvas;
	
	gsheet = gtk_type_new (gnumeric_sheet_get_type ());
	canvas = GNOME_CANVAS (gsheet);

	canvas->visual = gtk_widget_get_default_visual ();
	canvas->colormap = gtk_widget_get_default_colormap ();
	canvas->cc   = gdk_color_context_new (canvas->visual, canvas->colormap);
	canvas->root = gnome_canvas_group_new (canvas);
	
	gsheet->sheet   = sheet;
	gsheet->top_col = 0;
	gsheet->top_row = 0;

Arturo Espinosa's avatar
Arturo Espinosa committed
48 49 50
	return gsheet;
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static void
gnumeric_sheet_move_cursor_horizontal (GnumericSheet *sheet, int count)
{
	ItemCursor *item_cursor = sheet->item_cursor;
	int new_left;
	
	new_left = item_cursor->start_col + count;
	
	if (new_left < sheet->top_col){
		g_warning ("scroll grip left");
	}

	if (new_left < 0)
		new_left = 0;
	
	item_cursor_set_bounds (item_cursor,
				new_left, item_cursor->end_col + count,
				item_cursor->start_row, item_cursor->end_row);
}

static void
gnumeric_sheet_move_cursor_vertical (GnumericSheet *sheet, int count)
{
	ItemCursor *item_cursor = sheet->item_cursor;
	int new_top;
		
	new_top = item_cursor->start_row + count;
	if (new_top < sheet->top_row){
		g_warning ("scroll grid up");
	}

	if (new_top < 0)
		new_top = 0;
	
	item_cursor_set_bounds (item_cursor,
				item_cursor->start_col, item_cursor->end_col,
				new_top, item_cursor->end_row + count);
}

static gint
gnumeric_sheet_key (GtkWidget *widget, GdkEventKey *event)
{
	GnumericSheet *sheet = GNUMERIC_SHEET (widget);

	printf ("tecla\n");
	
	switch (event->keyval){
	case GDK_Left:
		gnumeric_sheet_move_cursor_horizontal (sheet, -1);
		break;

	case GDK_Right:
		gnumeric_sheet_move_cursor_horizontal (sheet, 1);
		break;

	case GDK_Up:
		gnumeric_sheet_move_cursor_vertical (sheet, -1);
		break;

	case GDK_Down:
		gnumeric_sheet_move_cursor_vertical (sheet, 1);
		break;

	default:
		return 0;
	}
	return 1;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
120 121 122
GtkWidget *
gnumeric_sheet_new (Sheet *sheet)
{
123
	GnomeCanvasItem *item;
Arturo Espinosa's avatar
Arturo Espinosa committed
124
	GnumericSheet *gsheet;
125 126 127
	GnomeCanvas   *gsheet_canvas;
	GnomeCanvasGroup *gsheet_group;
	GtkWidget *widget;
Arturo Espinosa's avatar
Arturo Espinosa committed
128 129 130
	
	gsheet = gnumeric_sheet_create (sheet);
	gnome_canvas_set_size (GNOME_CANVAS (gsheet), 300, 100);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

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

	/* The cursor */
	item = gnome_canvas_item_new (gsheet_canvas, gsheet_group,
				      item_cursor_get_type (),
				      "ItemCursor::Sheet", sheet,
				      "ItemCursor::Grid", gsheet->item_grid,
				      NULL);
	gsheet->item_cursor = ITEM_CURSOR (item);
Arturo Espinosa's avatar
Arturo Espinosa committed
150
	
151 152 153 154 155 156 157 158 159 160 161
	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);
	printf ("Window ID=%d\n", GDK_WINDOW_XWINDOW (widget->window));
Arturo Espinosa's avatar
Arturo Espinosa committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
}

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;
179 180
	widget_class->key_press_event = gnumeric_sheet_key;
	widget_class->realize = gnumeric_sheet_realize;
Arturo Espinosa's avatar
Arturo Espinosa committed
181 182
}

Arturo Espinosa's avatar
Arturo Espinosa committed
183 184 185 186 187
static void
gnumeric_sheet_init (GnumericSheet *gsheet)
{
	GnomeCanvas *canvas = GNOME_CANVAS (gsheet);

188 189 190
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);
	GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_DEFAULT);

Arturo Espinosa's avatar
Arturo Espinosa committed
191 192 193 194
	printf ("idel_id=%d\n", canvas->idle_id);
	printf ("pixs=%g\n", canvas->pixels_per_unit);
}

195 196 197 198 199 200 201 202 203 204 205
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
206
			(GtkObjectInitFunc) gnumeric_sheet_init,
207 208 209 210 211
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

Arturo Espinosa's avatar
Arturo Espinosa committed
212
		gnumeric_sheet_type = gtk_type_unique (gnome_canvas_get_type (), &gnumeric_sheet_info);
213 214 215 216 217
	}

	return gnumeric_sheet_type;
}