preview-grid.c 11.8 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5 6 7
/*
 * preview-grid.c : Preview Grid Canvas Item
 *
 * Based upon "The Grid Gnome Canvas Item" a.k.a. Item-Grid
 * (item-grid.c) Created by Miguel de Icaza (miguel@kernel.org)
 *
8
 * Author : Almer S. Tigelaar <almer@gnome.org>
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 23
 */

24
#include <gnumeric-config.h>
25
#include "gnumeric.h"
26
#include "preview-grid-impl.h"
27 28

#include "cell.h"
29
#include "sheet.h"
30
#include "cell-draw.h"
31
#include "colrow.h"
32 33
#include "pattern.h"
#include "mstyle.h"
34
#include "rendered-value.h"
35 36 37
#include "sheet-style.h"
#include "style-border.h"
#include "style-color.h"
38
#include "value.h"
Jody Goldberg's avatar
Jody Goldberg committed
39
#include "gnm-marshalers.h"
40

Jody Goldberg's avatar
Jody Goldberg committed
41
#include <gsf/gsf-impl-utils.h>
42

43
static GocItemClass *parent_klass;
44
enum {
45 46 47 48 49 50
	PREVIEW_GRID_PROP_0,
	PREVIEW_GRID_PROP_RENDER_GRIDLINES,
	PREVIEW_GRID_PROP_DEFAULT_COL_WIDTH,
	PREVIEW_GRID_PROP_DEFAULT_ROW_HEIGHT,
	PREVIEW_GRID_PROP_DEFAULT_STYLE,
	PREVIEW_GRID_PROP_DEFAULT_VALUE
51 52
};

53 54
/*****************************************************************************/

Morten Welinder's avatar
Morten Welinder committed
55
static GnmStyle *
56
pg_get_style (GnmPreviewGrid *pg, int col, int row)
57
{
58
	GnmPreviewGridClass *klass = GNM_PREVIEW_GRID_GET_CLASS (pg);
Morten Welinder's avatar
Morten Welinder committed
59
	GnmStyle *style;
60

61 62
	g_return_val_if_fail (col >= 0 && col < gnm_sheet_get_max_cols (pg->sheet), NULL);
	g_return_val_if_fail (row >= 0 && row < gnm_sheet_get_max_rows (pg->sheet), NULL);
63 64
	g_return_val_if_fail (klass != NULL, NULL);

65 66
	if (klass->get_cell_style) {
		style = klass->get_cell_style (pg, col, row);
67 68 69 70 71 72 73
		if (style != NULL)
			return style;
	}

	return pg->defaults.style;
}

74
static GnmCell *
75
pg_fetch_cell (GnmPreviewGrid *pg, int col, int row)
76
{
77
	GnmPreviewGridClass *klass = GNM_PREVIEW_GRID_GET_CLASS (pg);
78
	GnmCell *cell;
79
	GnmValue *v = NULL;
80 81 82

	g_return_val_if_fail (klass != NULL, NULL);
	g_return_val_if_fail (pg != NULL, NULL);
83 84
	g_return_val_if_fail (col >= 0 && col < gnm_sheet_get_max_cols (pg->sheet), NULL);
	g_return_val_if_fail (row >= 0 && row < gnm_sheet_get_max_rows (pg->sheet), NULL);
85

86 87 88 89
	if (NULL != klass->get_cell_value)
		v = (klass->get_cell_value) (pg, col, row);
	if (NULL == v)
		v = value_dup (pg->defaults.value);
90

91
	cell = sheet_cell_fetch (pg->sheet, col, row);
92
	gnm_cell_set_value (cell, v);
93

94
	gnm_cell_render_value (cell, TRUE);
95 96 97 98

	return cell;
}

99 100 101 102 103 104 105 106 107
/**
 * pg_get_row_offset:
 * pg:
 * @y: offset
 * @row_origin: if not null the origin of the row containing pixel @y is put here
 *
 * Return value: Row containing pixel y (and origin in @row_origin)
 **/
static int
108
pg_get_row_offset (GnmPreviewGrid *pg, int const y, int *row_origin)
109 110
{
	int row   = 0;
111
	int pixel = 1;
112
	int const h = pg->defaults.row_height;
113 114

	g_return_val_if_fail (pg != NULL, 0);
115

116
	do {
117
		if (y <= (pixel + h) || h == 0) {
118 119 120 121
			if (row_origin)
				*row_origin = pixel;
			return row;
		}
122
		pixel += h;
123
	} while (++row < gnm_sheet_get_max_rows (pg->sheet));
124 125 126 127

	if (row_origin)
		*row_origin = pixel;

128
	return gnm_sheet_get_last_row (pg->sheet);
129 130 131 132 133 134 135 136 137 138
}

/**
 * pg_get_col_offset:
 * @x: offset
 * @col_origin: if not null the origin of the column containing pixel @x is put here
 *
 * Return value: Column containing pixel x (and origin in @col_origin)
 **/
static int
139
pg_get_col_offset (GnmPreviewGrid *pg, int const x, int *col_origin)
140 141
{
	int col   = 0;
142
	int pixel = 1;
143
	int w;
144 145

	g_return_val_if_fail (pg != NULL, 0);
146

147
	do {
148
		w = pg->defaults.col_width;
149
		if (x <= (pixel + w) || w == 0) {
150 151 152 153
			if (col_origin)
				*col_origin = pixel;
			return col;
		}
154
		pixel += w;
155
	} while (++col < gnm_sheet_get_max_cols (pg->sheet));
156 157 158 159

	if (col_origin)
		*col_origin = pixel;

160
	return gnm_sheet_get_last_col (pg->sheet);
161 162
}

163
static void
164
preview_grid_update_bounds (GocItem *item)
165
{
166 167 168 169
	item->x0 = -2;
	item->y0 = -2;
	item->x1 = INT_MAX/2;	/* FIXME add some num cols/rows abilities */
	item->y1 = INT_MAX/2;	/* FIXME and some flags to decide how to adapt */
170 171 172
}

static void
173
preview_grid_draw_background (cairo_t *cr, GnmPreviewGrid const *pg, GnmStyle const *mstyle,
174 175
			      int col, int row, int x, int y, int w, int h)
{
Morten Welinder's avatar
Morten Welinder committed
176
	if (gnm_pattern_background_set (mstyle, cr, FALSE, NULL)) {
177 178 179 180
		cairo_rectangle (cr, x, y, w+1, h+1);
		cairo_fill (cr);
	}
	gnm_style_border_draw_diag (mstyle, cr, x, y, x+w, y+h);
181 182 183
}

static void
184
pg_style_get_row (GnmPreviewGrid *pg, GnmStyleRow *sr)
Morten Welinder's avatar
Morten Welinder committed
185
{
186 187
	int const row = sr->row;
	int col;
188

189 190 191 192
	for (col = sr->start_col; col <= sr->end_col; col++) {
		GnmStyle const *style = pg_get_style (pg, col, row);
		sheet_style_set_pos (pg->sheet, col, row,
				     gnm_style_dup (style));
193
	}
194 195

	sheet_style_get_row (pg->sheet, sr);
196 197
}

198
/* no spans or merges */
199 200 201
static gboolean
preview_grid_draw_region (GocItem const *item, cairo_t *cr,
			  double x0, double y0, double x1, double y1)
202
{
203
	GnmPreviewGrid *pg = GNM_PREVIEW_GRID (item);
204

205 206 207 208 209 210 211 212
	/* To ensure that far and near borders get drawn we pretend to draw +-2
	 * pixels around the target area which would include the surrounding
	 * borders if necessary */
	/* TODO : there is an opportunity to speed up the redraw loop by only
	 * painting the borders of the edges and not the content.
	 * However, that feels like more hassle that it is worth.  Look into this someday.
	 */
	int x, y, col, row, n;
213
	int const start_col = pg_get_col_offset (pg, x0 - 2, &x);
214
	int end_col         = pg_get_col_offset (pg, x1 + 2, NULL);
215
	int diff_x    = x;
216 217
	int start_row       = pg_get_row_offset (pg, y0 - 2, &y);
	int end_row         = pg_get_row_offset (pg, y1 + 2, NULL);
218
	int diff_y    = y;
219
	int row_height = pg->defaults.row_height;
220

221
	GnmStyleRow sr, next_sr;
Morten Welinder's avatar
Morten Welinder committed
222 223
	GnmStyle const **styles;
	GnmBorder const **borders, **prev_vert;
Morten Welinder's avatar
Morten Welinder committed
224
	GnmBorder const *none = pg->gridlines ? gnm_style_border_none () : NULL;
225
	gpointer *sr_array_data;
226 227 228

	int *colwidths = NULL;

Morten Welinder's avatar
Morten Welinder committed
229
	gnm_style_border_none_set_color (style_color_grid ());
230

231 232
	/*
	 * allocate a single blob of memory for all 8 arrays of pointers.
233 234
	 *	- 6 arrays of n GnmBorder const *
	 *	- 2 arrays of n GnmStyle const *
235 236
	 */
	n = end_col - start_col + 3; /* 1 before, 1 after, 1 fencepost */
237
	sr_array_data = g_new (gpointer, n * 8);
238
	style_row_init (&prev_vert, &sr, &next_sr, start_col, end_col,
239
			sr_array_data, !pg->gridlines);
240 241 242

	/* load up the styles for the first row */
	next_sr.row = sr.row = row = start_row;
243
	pg_style_get_row (pg, &sr);
244

245
	/* Collect the column widths */
246
	colwidths = g_new (int, n);
247 248
	colwidths -= start_col;
	for (col = start_col; col <= end_col; col++)
249
		colwidths[col] = pg->defaults.col_width;
250

251
	/* Fill entire region with default background (even past far edge) */
252 253
	gtk_render_background (goc_item_get_style_context (item),
			       cr, diff_x, diff_y, x1 - x0, y1 - y0);
254 255

	for (y = diff_y; row <= end_row; row = sr.row = next_sr.row) {
256 257
		if (++next_sr.row > end_row) {
			for (col = start_col ; col <= end_col; ++col)
258 259
				next_sr.vertical[col] =
				next_sr.bottom[col] = none;
260
		} else
261
			pg_style_get_row (pg, &next_sr);
262

263
		for (col = start_col, x = diff_x; col <= end_col; col++) {
264 265
			GnmStyle const *style = sr.styles[col];
			GnmCell const *cell = pg_fetch_cell (pg, col, row);
266

267
			preview_grid_draw_background (cr, pg,
268
						      style, col, row, x, y,
269
						      colwidths[col], row_height);
270

271
			if (!gnm_cell_is_empty (cell))
272
				cell_draw (cell, cr,
273 274
					   x, y, colwidths[col], row_height,
					   -1, FALSE);
275

276
			x += colwidths[col];
277
		}
278

279 280 281 282
		gnm_style_borders_row_draw
			(prev_vert, &sr, cr,
			 diff_x, y, y + row_height,
			 colwidths, TRUE, 1 /* cheat dir == 1 for now */);
283 284 285 286 287 288 289

		/* roll the pointers */
		borders = prev_vert; prev_vert = sr.vertical;
		sr.vertical = next_sr.vertical; next_sr.vertical = borders;
		borders = sr.top; sr.top = sr.bottom;
		sr.bottom = next_sr.top = next_sr.bottom; next_sr.bottom = borders;
		styles = sr.styles; sr.styles = next_sr.styles; next_sr.styles = styles;
290

291
		y += row_height;
292
	}
293 294 295

	g_free (sr_array_data);
	g_free (colwidths + start_col); // Offset reverts -= from above
296
	return TRUE;
297 298 299
}

static double
300
preview_grid_distance (GocItem *item, double cx, double cy,
301
		       GocItem **actual_item)
302 303 304 305 306 307
{
	*actual_item = item;
	return 0.0;
}

static void
308 309
preview_grid_set_property (GObject *obj, guint param_id,
			   GValue const *value, GParamSpec *pspec)
310
{
311
	GnmPreviewGrid *pg = GNM_PREVIEW_GRID (obj);
312

313
	switch (param_id){
Morten Welinder's avatar
Morten Welinder committed
314
	case PREVIEW_GRID_PROP_RENDER_GRIDLINES:
315 316
		pg->gridlines = g_value_get_boolean (value);
		break;
Morten Welinder's avatar
Morten Welinder committed
317
	case PREVIEW_GRID_PROP_DEFAULT_COL_WIDTH:
318 319
		pg->defaults.col_width = g_value_get_uint (value);
		break;
Morten Welinder's avatar
Morten Welinder committed
320
	case PREVIEW_GRID_PROP_DEFAULT_ROW_HEIGHT:
321 322
		pg->defaults.row_height = g_value_get_uint (value);
		break;
323 324
	case PREVIEW_GRID_PROP_DEFAULT_STYLE : {
		GnmStyle *style = g_value_dup_boxed (value);
325
		g_return_if_fail (style != NULL);
326
		gnm_style_unref (pg->defaults.style);
327 328 329
		pg->defaults.style = style;
		break;
	}
330 331
	case PREVIEW_GRID_PROP_DEFAULT_VALUE: {
		GnmValue *val = g_value_dup_boxed (value);
332
		g_return_if_fail (val != NULL);
333 334
		value_release (pg->defaults.value);
		pg->defaults.value = val;
335 336 337 338 339
		break;
	}
	default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec);
		return; /* NOTE : RETURN */
	}
340

341
	goc_item_invalidate (GOC_ITEM (obj));
342 343 344
}

static void
345
preview_grid_dispose (GObject *obj)
346
{
347
	GnmPreviewGrid *pg = GNM_PREVIEW_GRID (obj);
348

349
	if (pg->defaults.style != NULL) {
350
		gnm_style_unref (pg->defaults.style);
351
		pg->defaults.style = NULL;
352
	}
353 354
	value_release (pg->defaults.value);
	pg->defaults.value = NULL;
355

356
	g_clear_object (&pg->sheet);
357

358
	G_OBJECT_CLASS (parent_klass)->dispose (obj);
359 360 361
}

static void
362
gnm_preview_grid_init (GnmPreviewGrid *pg)
363
{
364 365 366 367
	pg->sheet = g_object_new (GNM_SHEET_TYPE,
				  "rows", 256,
				  "columns", 256,
				  NULL);
368 369
	pg->gridlines = FALSE;
	pg->defaults.col_width = 64;
370
	pg->defaults.row_height = 17;
371 372
	pg->defaults.style = gnm_style_new_default ();
	pg->defaults.value = value_new_empty ();
373 374 375
}

static void
376
gnm_preview_grid_class_init (GObjectClass *gobject_klass)
377
{
378
	GocItemClass *item_klass = (GocItemClass *)gobject_klass;
379 380 381 382

	parent_klass = g_type_class_peek_parent (gobject_klass);

	gobject_klass->set_property = preview_grid_set_property;
383
	gobject_klass->dispose = preview_grid_dispose;
384 385 386 387 388 389 390
	g_object_class_install_property
		(gobject_klass, PREVIEW_GRID_PROP_RENDER_GRIDLINES,
		 g_param_spec_boolean ("render-gridlines", NULL, NULL,
				       FALSE,
				       GSF_PARAM_STATIC | G_PARAM_WRITABLE));
        g_object_class_install_property
		(gobject_klass, PREVIEW_GRID_PROP_DEFAULT_COL_WIDTH,
391
                 g_param_spec_uint ("default-col-width", NULL, NULL,
392 393 394 395
				    0, G_MAXUINT, 0,
				    GSF_PARAM_STATIC | G_PARAM_WRITABLE));
        g_object_class_install_property
		(gobject_klass, PREVIEW_GRID_PROP_DEFAULT_ROW_HEIGHT,
396
                 g_param_spec_uint ("default-row-height", NULL, NULL,
397 398 399 400 401 402 403 404 405 406 407 408
				    0, G_MAXUINT, 0,
				    GSF_PARAM_STATIC | G_PARAM_WRITABLE));
        g_object_class_install_property
		(gobject_klass, PREVIEW_GRID_PROP_DEFAULT_STYLE,
                 g_param_spec_boxed ("default-style", NULL, NULL,
				      gnm_style_get_type (),
				      GSF_PARAM_STATIC | G_PARAM_WRITABLE));
        g_object_class_install_property
		(gobject_klass, PREVIEW_GRID_PROP_DEFAULT_VALUE,
                 g_param_spec_boxed ("default-value", NULL, NULL,
				     gnm_value_get_type (),
				     GSF_PARAM_STATIC | G_PARAM_WRITABLE));
409

410 411 412
	item_klass->update_bounds = preview_grid_update_bounds;
	item_klass->draw_region = preview_grid_draw_region;
	item_klass->distance    = preview_grid_distance;
413 414
}

415 416
GSF_CLASS (GnmPreviewGrid, gnm_preview_grid,
	   gnm_preview_grid_class_init, gnm_preview_grid_init,
417
	   GOC_TYPE_GROUP)