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

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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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
static void
sheet_init_dummy_stuff (Sheet *sheet)
{
	int x, y;
	ColInfo c, *cp;
	RowInfo *rp;

	c.col          = 0;
	c.style        = NULL;
	c.unit_width   = 40;
	c.width        = 0;
	c.left_margin  = 0;
	c.right_margin = 0;

	sheet->default_col_style = c;

	/* Initialize some of the columns */
	for (x = 0; x < 40; x += 2){
		cp = g_new0 (ColInfo, 1);

		*cp = c;
		cp->col = x;
		cp->unit_width = (x+1) * 30;
		sheet->cols_info = g_list_append (sheet->cols_info, cp);
	}

	/* Rows, we keep them consistent for now */
	sheet->default_row_style.row          = 0;
	sheet->default_row_style.style        = NULL;
	sheet->default_row_style.unit_height  = 20;
	sheet->default_row_style.height       = 0;
	sheet->default_row_style.top_margin   = 0;
	sheet->default_row_style.bottom_margin = 0;

	for (y = 0; y < 6; y += 2){
		rp = g_new0 (RowInfo, 1);

		*rp = sheet->default_row_style;
		rp->row = y;
		rp->unit_height = (20 * (y + 1));
		sheet->rows_info = g_list_append (sheet->rows_info, rp);
	}
}

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);
	*itemp = GNOME_CANVAS_ITEM (item);
	gtk_widget_show (canvas);
	return canvas;
}

Arturo Espinosa's avatar
Arturo Espinosa committed
81 82 83 84 85 86 87 88
Sheet *
sheet_new (Workbook *wb, char *name)
{
	Sheet *sheet;

	sheet = g_new0 (Sheet, 1);
	sheet->parent_workbook = wb;
	sheet->name = g_strdup (name);
89 90 91 92 93 94
	sheet->last_zoom_factor_used = -1.0;
	sheet->toplevel = gtk_table_new (0, 0, 0);
		
	/* Dummy initialization */
	sheet_init_dummy_stuff (sheet);
	
Arturo Espinosa's avatar
Arturo Espinosa committed
95 96
	sheet->sheet_view = gnumeric_sheet_new (sheet);
	gtk_widget_show (sheet->sheet_view);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	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);
	
	/* 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
114
	
115
	sheet_set_zoom_factor (sheet, 1.0);
Arturo Espinosa's avatar
Arturo Espinosa committed
116 117 118
	return sheet;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
void
sheet_foreach_col (Sheet *sheet, sheet_col_callback callback, void *user_data)
{
	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
sheet_foreach_row (Sheet *sheet, sheet_row_callback callback, void *user_data)
{
	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
sheet_compute_col_size (Sheet *sheet, ColInfo *ci, void *data)
{
	double pix_per_unit = sheet->last_zoom_factor_used;

	ci->width = (ci->unit_width * pix_per_unit) +
		ci->left_margin + ci->right_margin + 1;
}

static void
sheet_compute_row_size (Sheet *sheet, RowInfo *ri, void *data)
{
	double pix_per_unit = sheet->last_zoom_factor_used;
	
	ri->height = (ri->unit_height * pix_per_unit) +
		ri->top_margin + ri->bottom_margin + 1;
}

/*
 * 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;
	sheet_foreach_col (sheet, sheet_compute_col_size, NULL);
	sheet_foreach_row (sheet, sheet_compute_row_size, NULL);
}

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);
}

196 197 198
ColInfo *
sheet_get_col_info (Sheet *sheet, int col)
{
199
	GList *l = sheet->cols_info;
200

201 202
	for (; l; l = l->next){
		ColInfo *ci = l->data;
203

204 205 206 207 208
		if (ci->col == col)
			return ci;
	}

	return &sheet->default_col_style;
209 210 211 212 213
}

RowInfo *
sheet_get_row_info (Sheet *sheet, int row)
{
214
	GList *l = sheet->rows_info;
215

216 217 218 219 220 221
	for (; l; l = l->next){
		RowInfo *ri = l->data;

		if (ri->row == row)
			return ri;
	}
222

223
	return &sheet->default_row_style;
224 225
}

226 227 228 229 230 231 232 233 234 235
/*
 * Return the number of pixels between from_col to to_col
 */
int
sheet_col_get_distance (Sheet *sheet, int from_col, int to_col)
{
	ColInfo *ci;
	int pixels = 0, i;

	g_assert (from_col <= to_col);
236 237 238 239

	/* This can be optimized, yes, but the implementation
	 * of the RowInfo, ColInfo sets is going to change anyways
	 */
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	for (i = from_col; i < to_col; i++){
		ci = sheet_get_col_info (sheet, i);
		pixels += ci->width;
	}
	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)
{
	RowInfo *ri;
	int pixels = 0, i;

	g_assert (from_row <= to_row);
	
258 259 260
	/* This can be optimized, yes, but the implementation
	 * of the RowInfo, ColInfo sets is going to change anyways
	 */
261 262 263 264 265 266
	for (i = from_row; i < to_row; i++){
		ri = sheet_get_row_info (sheet, i);
		pixels += ri->height;
	}
	return pixels;
}
267 268 269 270 271 272 273 274 275 276 277 278

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);
}