sheet.c 9.38 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1 2 3 4
#include <gnome.h>
#include "gnumeric.h"
#include "gnumeric-sheet.h"

Arturo Espinosa's avatar
Arturo Espinosa committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
static void
sheet_redraw_all (Sheet *sheet)
{
	gnome_canvas_request_redraw (
		GNOME_CANVAS (sheet->sheet_view),
		0, 0, INT_MAX, INT_MAX);
}

static void
sheet_init_default_styles (Sheet *sheet)
{
	/* The default column style */
	sheet->default_col_style.pos        = -1;
	sheet->default_col_style.style      = style_new ();
	sheet->default_col_style.units      = 40;
	sheet->default_col_style.pixels     = 0;
	sheet->default_col_style.margin_a   = 0;
	sheet->default_col_style.margin_b   = 0;
	sheet->default_col_style.selected   = 0;

	/* The default row style */
	sheet->default_row_style.pos      = -1;
	sheet->default_row_style.style    = style_new ();
	sheet->default_row_style.units    = 20;
	sheet->default_row_style.pixels   = 0;
	sheet->default_row_style.margin_a = 0;
	sheet->default_row_style.margin_b = 0;
	sheet->default_row_style.selected = 0;
}

/* Initialize some of the columns and rows, to test the display engine */
36 37 38
static void
sheet_init_dummy_stuff (Sheet *sheet)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
39
	ColRowInfo *cp, *rp;
40 41 42
	int x, y;

	for (x = 0; x < 40; x += 2){
Arturo Espinosa's avatar
Arturo Espinosa committed
43
		cp = sheet_row_new (sheet);
44 45
		cp->pos = x;
		cp->units = (x+1) * 30;
Arturo Espinosa's avatar
Arturo Espinosa committed
46
		sheet_col_add (sheet, cp);
47 48 49
	}

	for (y = 0; y < 6; y += 2){
Arturo Espinosa's avatar
Arturo Espinosa committed
50
		rp = sheet_row_new (sheet);
51 52
		rp->pos = y;
		rp->units = (20 * (y + 1));
53
		rp->selected = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
54
		sheet_row_add (sheet, rp);
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
	}
}

static GtkWidget *
new_canvas_bar (Sheet *sheet, GtkOrientation o, GnomeCanvasItem **itemp)
{
	GnomeCanvasGroup *group;
	GnomeCanvasItem *item;
	GtkWidget *canvas;
	int w, h;
	
	canvas = gnome_canvas_new (
		gtk_widget_get_default_visual (),
		gtk_widget_get_default_colormap ());

	if (o == GTK_ORIENTATION_VERTICAL){
		w = 60;
		h = 1;
	} else {
		w = 1;
		h = 20;
	}
	gnome_canvas_set_size (GNOME_CANVAS (canvas), w, h);
	group = GNOME_CANVAS_GROUP (GNOME_CANVAS (canvas)->root);
	item = gnome_canvas_item_new (GNOME_CANVAS (canvas), group,
				      item_bar_get_type (),
				      "ItemBar::Sheet", sheet,
				      "ItemBar::Orientation", o,
				      "ItemBar::First", 0,
				      NULL);
Arturo Espinosa's avatar
Arturo Espinosa committed
85

86 87 88 89 90
	*itemp = GNOME_CANVAS_ITEM (item);
	gtk_widget_show (canvas);
	return canvas;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static void
sheet_col_selection_changed (ItemBar *item_bar, int column, Sheet *sheet)
{
	printf ("Sheet signal: Column %d selection changed\n", column);
}

static void
sheet_col_size_changed (ItemBar *item_bar, int col, int width, Sheet *sheet)
{
	sheet_col_set_width (sheet, col, width);
}

static void
sheet_row_selection_changed (ItemBar *item_bar, int row, Sheet *sheet)
{
	printf ("Sheet signal: Row %d selection changed\n", row);
}

static void
sheet_row_size_changed (ItemBar *item_bar, int row, int height, Sheet *sheet)
{
	sheet_row_set_height (sheet, row, height);
}

Arturo Espinosa's avatar
Arturo Espinosa committed
115 116 117 118 119 120 121 122
Sheet *
sheet_new (Workbook *wb, char *name)
{
	Sheet *sheet;

	sheet = g_new0 (Sheet, 1);
	sheet->parent_workbook = wb;
	sheet->name = g_strdup (name);
123 124
	sheet->last_zoom_factor_used = -1.0;
	sheet->toplevel = gtk_table_new (0, 0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
125 126 127

	sheet_init_default_styles (sheet);
	
128 129 130
	/* Dummy initialization */
	sheet_init_dummy_stuff (sheet);
	
Arturo Espinosa's avatar
Arturo Espinosa committed
131 132
	sheet->sheet_view = gnumeric_sheet_new (sheet);
	gtk_widget_show (sheet->sheet_view);
133 134 135 136 137 138 139 140 141 142 143
	gtk_widget_show (sheet->toplevel);
	
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->sheet_view,
			  1, 2, 1, 2,
			  GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);

	/* Column canvas */
	sheet->col_canvas = new_canvas_bar (sheet, GTK_ORIENTATION_HORIZONTAL, &sheet->col_item);
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->col_canvas,
			  1, 2, 0, 1,
			  GTK_FILL | GTK_EXPAND, 0, 0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
144 145 146 147 148 149
	gtk_signal_connect (GTK_OBJECT (sheet->col_item), "selection_changed",
			    GTK_SIGNAL_FUNC (sheet_col_selection_changed),
			    sheet);
	gtk_signal_connect (GTK_OBJECT (sheet->col_item), "size_changed",
			    GTK_SIGNAL_FUNC (sheet_col_size_changed),
			    sheet);
150 151 152 153 154
	/* Row canvas */
	sheet->row_canvas = new_canvas_bar (sheet, GTK_ORIENTATION_VERTICAL, &sheet->row_item);
	gtk_table_attach (GTK_TABLE (sheet->toplevel), sheet->row_canvas,
			  0, 1, 1, 2,
			  0, GTK_FILL | GTK_EXPAND, 0, 0);
Arturo Espinosa's avatar
Arturo Espinosa committed
155 156 157 158 159 160
	gtk_signal_connect (GTK_OBJECT (sheet->row_item), "selection_changed",
			    GTK_SIGNAL_FUNC (sheet_row_selection_changed),
			    sheet);
	gtk_signal_connect (GTK_OBJECT (sheet->row_item), "size_changed",
			    GTK_SIGNAL_FUNC (sheet_row_size_changed),
			    sheet);
Arturo Espinosa's avatar
Arturo Espinosa committed
161
	
162
	sheet_set_zoom_factor (sheet, 1.0);
Arturo Espinosa's avatar
Arturo Espinosa committed
163 164 165
	return sheet;
}

166
void
167
sheet_foreach_col (Sheet *sheet, sheet_col_row_callback callback, void *user_data)
168 169 170 171 172 173 174 175 176 177 178 179 180 181
{
	GList *l = sheet->cols_info;

	/* Invoke the callback for the default style */
	(*callback)(sheet, &sheet->default_col_style, user_data);

	/* And then for the rest */
	while (l){
		(*callback)(sheet, l->data, user_data);
		l = l->next;
	}
}

void
182
sheet_foreach_row (Sheet *sheet, sheet_col_row_callback callback, void *user_data)
183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
	GList *l = sheet->rows_info;

	/* Invoke the callback for the default style */
	(callback)(sheet, &sheet->default_row_style, user_data);

	/* And then for the rest */
	while (l){
		(*callback)(sheet, l->data, user_data);
		l = l->next;
	}
}

static void
197
sheet_compute_col_row_new_size (Sheet *sheet, ColRowInfo *ci, void *data)
198 199 200
{
	double pix_per_unit = sheet->last_zoom_factor_used;

201 202
	ci->pixels = (ci->units * pix_per_unit) +
		ci->margin_a + ci->margin_b + 1;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
}

/*
 * Updates the values used for display after a zoom change
 */
static void
sheet_reconfigure_zoom (Sheet *sheet)
{
	GnomeCanvas *gnumeric_canvas = GNOME_CANVAS (sheet->sheet_view);
	double pixels_per_unit = gnumeric_canvas->pixels_per_unit;
	
	if (pixels_per_unit == sheet->last_zoom_factor_used)
		return;

	sheet->last_zoom_factor_used = pixels_per_unit;
218 219
	sheet_foreach_col (sheet, sheet_compute_col_row_new_size, NULL);
	sheet_foreach_row (sheet, sheet_compute_col_row_new_size, NULL);
220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

void
sheet_set_zoom_factor (Sheet *sheet, double factor)
{
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->sheet_view), factor);
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->col_canvas), factor);
	gnome_canvas_set_pixels_per_unit (GNOME_CANVAS (sheet->row_canvas), factor);
	sheet_reconfigure_zoom (sheet);
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->sheet_view), 0, 0);
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->col_canvas), 0, 0);
	gnome_canvas_scroll_to (GNOME_CANVAS (sheet->row_canvas), 0, 0);
}

234
ColRowInfo *
Arturo Espinosa's avatar
Arturo Espinosa committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
sheet_duplicate_colrow (ColRowInfo *original)
{
	ColRowInfo *info = g_new (ColRowInfo, 1);

	*info = *original;
	
	info->style = style_duplicate (original->style);
	
	return info;
}

ColRowInfo *
sheet_row_new (Sheet *sheet)
{
	return sheet_duplicate_colrow (&sheet->default_row_style);
}

ColRowInfo *
sheet_col_new (Sheet *sheet)
{
	return sheet_duplicate_colrow (&sheet->default_col_style);
}


void
sheet_col_add (Sheet *sheet, ColRowInfo *cp)
{
	sheet->cols_info = g_list_append (sheet->cols_info, cp);
}

void
sheet_row_add (Sheet *sheet, ColRowInfo *rp)
{
	sheet->rows_info = g_list_append (sheet->rows_info, rp);
}

ColRowInfo *
sheet_col_get_info (Sheet *sheet, int col)
273
{
274
	GList *l = sheet->cols_info;
275

276
	for (; l; l = l->next){
277
		ColRowInfo *ci = l->data;
278

279
		if (ci->pos == col)
280 281 282 283
			return ci;
	}

	return &sheet->default_col_style;
284 285
}

286
ColRowInfo *
Arturo Espinosa's avatar
Arturo Espinosa committed
287
sheet_row_get_info (Sheet *sheet, int row)
288
{
289
	GList *l = sheet->rows_info;
290

291
	for (; l; l = l->next){
292
		ColRowInfo *ri = l->data;
293

294
		if (ri->pos == row)
295 296
			return ri;
	}
297

298
	return &sheet->default_row_style;
299 300
}

Arturo Espinosa's avatar
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static void
colrow_set_units (Sheet *sheet,ColRowInfo *info)
{
	double pix = sheet->last_zoom_factor_used;
	
	info->units  = (info->pixels -
			(info->margin_a + info->margin_b + 1))
		/ pix;
}

void
sheet_row_set_height (Sheet *sheet, int row, int height)
{
	ColRowInfo *ri;
	int add = 0;
	
	ri = sheet_row_get_info (sheet, row);
	if (ri == &sheet->default_row_style){
		ri = sheet_duplicate_colrow (ri);
		add = 1;
	}

	ri->pos = row;
	ri->pixels = height;
	colrow_set_units (sheet, ri);
	if (add)
		sheet_col_add (sheet, ri);
	sheet_redraw_all (sheet);
}

void
sheet_col_set_width (Sheet *sheet, int col, int width)
{
	ColRowInfo *ci;
	int add = 0;
	
	ci = sheet_col_get_info (sheet, col);
	if (ci == &sheet->default_col_style){
		ci = sheet_duplicate_colrow (ci);
		add = 1;
	}

	ci->pixels = width;
	ci->pos = col;
	colrow_set_units (sheet, ci);
	if (add)
		sheet_col_add (sheet, ci);

	sheet_redraw_all (sheet);
}

352 353 354 355 356 357
/*
 * Return the number of pixels between from_col to to_col
 */
int
sheet_col_get_distance (Sheet *sheet, int from_col, int to_col)
{
358
	ColRowInfo *ci;
359 360 361
	int pixels = 0, i;

	g_assert (from_col <= to_col);
362 363

	/* This can be optimized, yes, but the implementation
364
	 * of the ColRowInfo sets is going to change anyways
365
	 */
366
	for (i = from_col; i < to_col; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
367
		ci = sheet_col_get_info (sheet, i);
368
		pixels += ci->pixels;
369 370 371 372 373 374 375 376 377 378
	}
	return pixels;
}

/*
 * Return the number of pixels between from_row to to_row
 */
int
sheet_row_get_distance (Sheet *sheet, int from_row, int to_row)
{
379
	ColRowInfo *ri;
380 381 382 383
	int pixels = 0, i;

	g_assert (from_row <= to_row);
	
384 385 386
	/* This can be optimized, yes, but the implementation
	 * of the RowInfo, ColInfo sets is going to change anyways
	 */
387
	for (i = from_row; i < to_row; i++){
Arturo Espinosa's avatar
Arturo Espinosa committed
388
		ri = sheet_row_get_info (sheet, i);
389
		pixels += ri->pixels;
390 391 392
	}
	return pixels;
}
393 394 395 396 397 398 399 400 401 402 403 404

void
sheet_get_cell_bounds (Sheet *sheet, ColType col, RowType row, int *x, int *y, int *w, int *h)
{
	GnumericSheet *gsheet = GNUMERIC_SHEET (sheet->sheet_view);
	
	*x = sheet_col_get_distance (sheet, gsheet->top_col, col);
	*y = sheet_row_get_distance (sheet, gsheet->top_row, row);

	*w = sheet_col_get_distance (sheet, col, col + 1);
	*h = sheet_row_get_distance (sheet, row, row + 1);
}