workbook-view.c 40.1 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4
/*
 * workbook-view.c: View functions for the workbook
 *
Jody Goldberg's avatar
Jody Goldberg committed
5
 * Copyright (C) 2000-2006 Jody Goldberg (jody@gnome.org)
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
 * along with this program; if not, write to the Free Software
J.H.M. Dassen (Ray)'s avatar
J.H.M. Dassen (Ray) committed
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
20
 * USA
21
 */
22 23
#include <gnumeric-config.h>
#include "gnumeric.h"
Jody Goldberg's avatar
Jody Goldberg committed
24
#include "workbook-view.h"
25 26

#include "workbook-control-priv.h"
27
#include "workbook-priv.h"
28
#include "application.h"
Jody Goldberg's avatar
Jody Goldberg committed
29
#include "sheet.h"
30
#include "sheet-view.h"
Jody Goldberg's avatar
Jody Goldberg committed
31
#include "sheet-merge.h"
Jody Goldberg's avatar
Jody Goldberg committed
32
#include "sheet-style.h"
33
#include "style-font.h"
34
#include "gnm-format.h"
35
#include "func.h"
36
#include "expr.h"
37
#include "expr-name.h"
38
#include "expr-impl.h"
39
#include "value.h"
Jody Goldberg's avatar
Jody Goldberg committed
40
#include "ranges.h"
41
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
42
#include "mstyle.h"
Jody Goldberg's avatar
Jody Goldberg committed
43 44
#include "validation.h"
#include "validation-combo.h"
Jody Goldberg's avatar
Jody Goldberg committed
45 46
#include "gnm-sheet-slicer.h"
#include "gnm-sheet-slicer-combo.h"
47
#include "position.h"
48
#include "cell.h"
Morten Welinder's avatar
Morten Welinder committed
49
#include "gutils.h"
50
#include "command-context.h"
51
#include "auto-format.h"
Jody Goldberg's avatar
Jody Goldberg committed
52
#include "sheet-object.h"
53
#include "gnumeric-conf.h"
54

55
#include <goffice/goffice.h>
56
#include <gsf/gsf.h>
57
#include <gsf/gsf-meta-names.h>
Jody Goldberg's avatar
Jody Goldberg committed
58
#include <gsf/gsf-impl-utils.h>
59 60
#include <gsf/gsf-output-stdio.h>
#include <gsf/gsf-input.h>
61
#include <glib/gi18n-lib.h>
Morten Welinder's avatar
Morten Welinder committed
62
#include <glib/gstdio.h>
Jody Goldberg's avatar
Jody Goldberg committed
63
#include <string.h>
64
#include <unistd.h>
65
#include <errno.h>
66
#include <stdlib.h>
67
#include "mathfunc.h"
68

69
#ifdef G_OS_WIN32
70
#include <windows.h>
71 72
#endif

73 74 75 76
enum {
	PROP_0,
	PROP_AUTO_EXPR_FUNC,
	PROP_AUTO_EXPR_DESCR,
77
	PROP_AUTO_EXPR_MAX_PRECISION,
78
	PROP_AUTO_EXPR_TEXT,
79
	PROP_AUTO_EXPR_ATTRS,
80 81
	PROP_AUTO_EXPR_CELL,
	PROP_AUTO_EXPR_SHEET,
82 83 84
	PROP_SHOW_HORIZONTAL_SCROLLBAR,
	PROP_SHOW_VERTICAL_SCROLLBAR,
	PROP_SHOW_NOTEBOOK_TABS,
85
	PROP_SHOW_FUNCTION_CELL_MARKERS,
86
	PROP_SHOW_EXTENSION_MARKERS,
87 88 89 90
	PROP_DO_AUTO_COMPLETION,
	PROP_PROTECTED,
	PROP_PREFERRED_WIDTH,
	PROP_PREFERRED_HEIGHT
91
};
92 93 94 95 96 97

/* WorkbookView signals */
enum {
	LAST_SIGNAL
};

98 99 100 101 102 103
/**
 * wb_view_get_workbook :
 * @wbv : #WorkbookView
 *
 * Return the #Workbook assciated with @wbv
 **/
104
Workbook *
105
wb_view_get_workbook (WorkbookView const *wbv)
Jody Goldberg's avatar
Jody Goldberg committed
106
{
107 108
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), NULL);
	return wbv->wb;
109 110
}

111 112 113 114 115 116 117 118 119 120 121 122 123
/**
 * wb_view_get_doc :
 * @wbv : #WorkbookView
 *
 * Return the #Workbook assciated with @wbv cast to a #GODoc
 **/
GODoc *
wb_view_get_doc (WorkbookView const *wbv)
{
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), NULL);
	return GO_DOC (wbv->wb);
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/**
 * wb_view_get_index_in_wb :
 * @wbv : #WorkbookView
 *
 * Returns 0 based index of wbv within workbook, or -1 if there is no workbook.
 **/
int
wb_view_get_index_in_wb (WorkbookView const *wbv)
{
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), -1);
	if (NULL != wbv->wb) {
		unsigned i = wbv->wb->wb_views->len;
		while (i-- > 0)
			if (g_ptr_array_index (wbv->wb->wb_views, i) == wbv)
				return i;
	}
	return -1;
}

143
Sheet *
144
wb_view_cur_sheet (WorkbookView const *wbv)
145
{
146 147 148
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), NULL);
	return wbv->current_sheet;
}
Jody Goldberg's avatar
Jody Goldberg committed
149

150 151 152 153 154 155 156
SheetView *
wb_view_cur_sheet_view (WorkbookView const *wbv)
{
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), NULL);
	return wbv->current_sheet_view;
}

157 158 159 160 161
void
wb_view_sheet_focus (WorkbookView *wbv, Sheet *sheet)
{
	if (wbv->current_sheet != sheet) {
		/* Make sure the sheet has been attached */
Jody Goldberg's avatar
Jody Goldberg committed
162
		g_return_if_fail (sheet == NULL || sheet->index_in_wb >= 0);
Jody Goldberg's avatar
Jody Goldberg committed
163

Morten Welinder's avatar
Morten Welinder committed
164 165 166 167
#if 0
		g_print ("Focus %s\n", sheet ? sheet->name_quoted : "-");
#endif

168
		wbv->current_sheet = sheet;
Morten Welinder's avatar
Morten Welinder committed
169
		wbv->current_sheet_view = sheet_get_view (sheet, wbv);
170

171 172
		WORKBOOK_VIEW_FOREACH_CONTROL (wbv, control,
			wb_control_sheet_focus (control, sheet););
Jody Goldberg's avatar
Jody Goldberg committed
173

174
		wb_view_selection_desc (wbv, TRUE, NULL);
175
		wb_view_edit_line_set (wbv, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
176
		wb_view_style_feedback (wbv);
177
		wb_view_menus_update (wbv);
178
		wb_view_auto_expr_recalc (wbv);
179
	}
Jody Goldberg's avatar
Jody Goldberg committed
180
}
181

Jody Goldberg's avatar
Jody Goldberg committed
182 183 184
void
wb_view_sheet_add (WorkbookView *wbv, Sheet *new_sheet)
{
185
	SheetView *new_view;
186

Jody Goldberg's avatar
Jody Goldberg committed
187 188
	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

189
	new_view = sheet_view_new (new_sheet, wbv);
Jody Goldberg's avatar
Jody Goldberg committed
190 191

	WORKBOOK_VIEW_FOREACH_CONTROL (wbv, control,
192
		wb_control_sheet_add (control, new_view););
193 194

	g_object_unref (new_view);
Morten Welinder's avatar
Morten Welinder committed
195 196 197

	if (wbv->current_sheet == NULL)
		wb_view_sheet_focus (wbv, new_sheet);
Jody Goldberg's avatar
Jody Goldberg committed
198 199
}

200 201 202 203 204 205 206 207 208
gboolean
wb_view_is_protected (WorkbookView *wbv, gboolean check_sheet)
{
	g_return_val_if_fail (IS_WORKBOOK_VIEW (wbv), FALSE);

	return wbv->is_protected || (check_sheet &&
		wbv->current_sheet != NULL && wbv->current_sheet->is_protected);
}

Jody Goldberg's avatar
Jody Goldberg committed
209
void
210
wb_view_set_attribute (WorkbookView *wbv, char const *name, char const *value)
Jody Goldberg's avatar
Jody Goldberg committed
211
{
Jody Goldberg's avatar
Jody Goldberg committed
212
	gboolean res;
213
	GObject *obj;
214
	const char *tname;
215 216

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));
Jody Goldberg's avatar
Jody Goldberg committed
217 218 219
	g_return_if_fail (name != NULL);
	g_return_if_fail (value != NULL);

220
	obj = G_OBJECT (wbv);
221
	res = !g_ascii_strcasecmp (value, "TRUE");
222

223 224 225 226 227 228 229 230 231
	if (strncmp (name, "WorkbookView::", 14) == 0)
		tname = name + 14;
	else if (strncmp (name, "Workbook::", 10) == 0)
		/* Some old files have this.  */
		tname = name + 10;
	else
		tname = "nope";

	if (!strcmp (tname , "show_horizontal_scrollbar"))
232
		g_object_set (obj, "show_horizontal_scrollbar", res, NULL);
233
	else if (!strcmp (tname , "show_vertical_scrollbar"))
234
		g_object_set (obj, "show_vertical_scrollbar", res, NULL);
235
	else if (!strcmp (tname , "show_notebook_tabs"))
236
		g_object_set (obj, "show_notebook_tabs", res, NULL);
237 238
	else if (!strcmp (tname , "show_function_cell_markers"))
		g_object_set (obj, "show_function_cell_markers", res, NULL);
239 240
	else if (!strcmp (tname , "show_extension_markers"))
		g_object_set (obj, "show_extension_markers", res, NULL);
241
	else if (!strcmp (tname , "do_auto_completion"))
242
		g_object_set (obj, "do_auto_completion", res, NULL);
243
	else if (!strcmp (tname , "is_protected"))
244
		g_object_set (obj, "protected", res, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
245 246
	else
		g_warning ("WorkbookView unknown arg '%s'", name);
247 248 249
}

void
250
wb_view_preferred_size (WorkbookView *wbv, int w, int h)
251
{
252 253
	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

254 255 256 257
	g_object_set (G_OBJECT (wbv),
		      "preferred-width", w,
		      "preferred-height", h,
		      NULL);
258 259 260
}

void
Jody Goldberg's avatar
Jody Goldberg committed
261
wb_view_style_feedback (WorkbookView *wbv)
Jody Goldberg's avatar
Jody Goldberg committed
262
{
Jody Goldberg's avatar
Jody Goldberg committed
263
	SheetView *sv;
Jody Goldberg's avatar
Jody Goldberg committed
264
	GnmStyle const *style;
Jody Goldberg's avatar
Jody Goldberg committed
265 266
	GnmSheetSlicer const *dslicer;
	GODataSlicerField *dsfield;
Jody Goldberg's avatar
Jody Goldberg committed
267
	GnmValidation const *val;
Morten Welinder's avatar
Morten Welinder committed
268
	GOFormat const *fmt_style, *fmt_cell;
269
	GnmCell *cell;
270
	gboolean update_controls = TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
271 272 273

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

Jody Goldberg's avatar
Jody Goldberg committed
274
	sv = wbv->current_sheet_view;
275 276 277
	if (sv == NULL)
		return;

Jody Goldberg's avatar
Jody Goldberg committed
278
	style = sheet_style_get (sv->sheet,
279
		sv->edit_pos.col, sv->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
280 281
	fmt_style = gnm_style_get_format (style);
	if (go_format_is_general (fmt_style) &&
282 283
	    (cell = sheet_cell_get (sv->sheet, sv->edit_pos.col, sv->edit_pos.row)) &&
	    cell->value && VALUE_FMT (cell->value))
Jody Goldberg's avatar
Jody Goldberg committed
284
		fmt_cell = VALUE_FMT (cell->value);
285
	else
Jody Goldberg's avatar
Jody Goldberg committed
286
		fmt_cell = fmt_style;
287

Jody Goldberg's avatar
Jody Goldberg committed
288 289
	if (go_format_eq (fmt_cell, fmt_style)) {
		if (style == wbv->current_style)
290
			update_controls = FALSE;
291
		gnm_style_ref (style);
292
	} else {
Jody Goldberg's avatar
Jody Goldberg committed
293 294 295
		GnmStyle *tmp = gnm_style_dup (style);
		gnm_style_set_format (tmp, fmt_cell);
		style = tmp;
296 297
	}

Jody Goldberg's avatar
Jody Goldberg committed
298 299 300 301
	if (wbv->current_style != NULL)
		gnm_style_unref (wbv->current_style);
	wbv->current_style = style;

Jody Goldberg's avatar
Jody Goldberg committed
302 303 304 305
	if (wbv->in_cell_combo != NULL) {
		sheet_object_clear_sheet (wbv->in_cell_combo);
		g_object_unref (wbv->in_cell_combo);
		wbv->in_cell_combo = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
306 307 308 309
	}

	if (gnm_style_is_element_set (style, MSTYLE_VALIDATION) &&
	    NULL != (val = gnm_style_get_validation (style)) &&
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
310
	    val->type == GNM_VALIDATION_TYPE_IN_LIST &&
Jody Goldberg's avatar
Jody Goldberg committed
311 312 313 314 315 316 317 318 319 320 321
	    val->use_dropdown)
		wbv->in_cell_combo = gnm_validation_combo_new (val, sv);
	else if (NULL != (dslicer = gnm_sheet_slicers_at_pos (sv->sheet, &sv->edit_pos)) &&
		   NULL != (dsfield = gnm_sheet_slicer_field_header_at_pos (dslicer, &sv->edit_pos)))
		wbv->in_cell_combo = g_object_new (gnm_sheet_slicer_combo_get_type (),
						   "sheet-view", sv,
						   "field",	 dsfield,
						   NULL);

	if (NULL != wbv->in_cell_combo)
	{
322
		const double a_offsets [4] = { 0., 0., 1., 1. };
Jody Goldberg's avatar
Jody Goldberg committed
323
		SheetObjectAnchor  anchor;
324 325
		GnmRange corner;
		GnmRange const *r;
Jody Goldberg's avatar
Jody Goldberg committed
326
		if (NULL == (r = gnm_sheet_merge_contains_pos (sv->sheet, &sv->edit_pos)))
Morten Welinder's avatar
Morten Welinder committed
327
			r = range_init_cellpos (&corner, &sv->edit_pos);
Jody Goldberg's avatar
Jody Goldberg committed
328 329 330
		sheet_object_anchor_init (&anchor, r, a_offsets, GOD_ANCHOR_DIR_DOWN_RIGHT);
		sheet_object_set_anchor (wbv->in_cell_combo, &anchor);
		sheet_object_set_sheet (wbv->in_cell_combo, sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
331
	}
332

333 334 335 336
	if (update_controls) {
		WORKBOOK_VIEW_FOREACH_CONTROL(wbv, control,
			wb_control_style_feedback (control, NULL););
	}
Jody Goldberg's avatar
Jody Goldberg committed
337 338
}

339 340 341 342 343 344 345 346 347
void
wb_view_menus_update (WorkbookView *wbv)
{
	Sheet *sheet;

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

	sheet = wbv->current_sheet;
	if (sheet != NULL) {
348 349 350
		WORKBOOK_VIEW_FOREACH_CONTROL (wbv, wbc, {
			wb_control_menu_state_update (wbc, MS_ALL);
			wb_control_update_action_sensitivity (wbc);
351
		});
352 353 354
	}
}

355 356 357 358
void
wb_view_selection_desc (WorkbookView *wbv, gboolean use_pos,
			WorkbookControl *optional_wbc)
{
Jody Goldberg's avatar
Jody Goldberg committed
359
	SheetView *sv;
360 361 362

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

Jody Goldberg's avatar
Jody Goldberg committed
363 364
	sv = wbv->current_sheet_view;
	if (sv != NULL) {
365 366
		char buffer [10 + 2 * 4 * sizeof (int)];
		char const *sel_descr = buffer;
Jody Goldberg's avatar
Jody Goldberg committed
367
		GnmRange const *r, *m;
Jody Goldberg's avatar
Jody Goldberg committed
368

Jody Goldberg's avatar
Jody Goldberg committed
369 370
		g_return_if_fail (IS_SHEET_VIEW (sv));
		g_return_if_fail (sv->selections);
Jody Goldberg's avatar
Jody Goldberg committed
371

372
		r = selection_first_range (sv, NULL, NULL);
373

Jody Goldberg's avatar
Jody Goldberg committed
374
		if (use_pos || range_is_singleton (r) ||
Morten Welinder's avatar
Morten Welinder committed
375
		    (NULL != (m = gnm_sheet_merge_is_corner (sv->sheet, &r->start)) &&
376 377
		     range_equal (r, m))) {
			sel_descr = sheet_names_check (sv->sheet, r);
378 379 380 381 382
			if (sel_descr == NULL) {
				GnmParsePos pp;
				parse_pos_init_editpos (&pp, sv);
				sel_descr = parsepos_as_string (&pp);
			}
383 384 385 386
		} else {
			int rows = r->end.row - r->start.row + 1;
			int cols = r->end.col - r->start.col + 1;

387
			if (rows == gnm_sheet_get_max_rows (sv->sheet))
388
        /* Translators: "%dC" is a very short format to indicate the number of full columns */
389
				snprintf (buffer, sizeof (buffer), _("%dC"), cols);
390
			else if (cols == gnm_sheet_get_max_cols (sv->sheet))
391
        /* Translators: "%dR" is a very short format to indicate the number of full rows */
392 393
				snprintf (buffer, sizeof (buffer), _("%dR"), rows);
			else
394
        /* Translators: "%dR x %dC" is a very short format to indicate the number of rows and columns */
395
				snprintf (buffer, sizeof (buffer), _("%dR x %dC"),
396 397
					  rows, cols);
		}
398

Jody Goldberg's avatar
Jody Goldberg committed
399
		if (optional_wbc == NULL) {
400 401
			WORKBOOK_VIEW_FOREACH_CONTROL (wbv, wbc,
				wb_control_selection_descr_set (wbc, sel_descr););
Jody Goldberg's avatar
Jody Goldberg committed
402 403
		} else
			wb_control_selection_descr_set (optional_wbc, sel_descr);
404 405 406
	}
}

407 408 409 410 411 412 413 414 415 416 417 418
/**
 * Load the edit line with the value of the cell in @sheet's edit_pos.
 *
 * @wbv : The view
 * @wbc : An Optional control
 *
 * Calculate what to display on the edit line then display it either in the
 * control @wbc,  or if that is NULL, in all controls.
 */
void
wb_view_edit_line_set (WorkbookView *wbv, WorkbookControl *optional_wbc)
{
Jody Goldberg's avatar
Jody Goldberg committed
419
	SheetView *sv;
420 421 422

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

Jody Goldberg's avatar
Jody Goldberg committed
423 424
	sv = wbv->current_sheet_view;
	if (sv != NULL) {
425
		char *text;
426 427
		Sheet *sheet = sv->sheet;
		GnmCell const *cell = sheet_cell_get (sheet,
428
			sv->edit_pos.col, sv->edit_pos.row);
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 462 463 464 465 466 467 468
		if (NULL != cell) {
			text = gnm_cell_get_entered_text (cell);

			if (gnm_cell_has_expr (cell)) {
				GnmExprTop const *texpr = cell->base.texpr;
				GnmCell const *corner = NULL;
				int x = 0, y = 0;

				/*
				 * If this is part of an array we add '{' '}'
				 * and size information to the display.  That
				 * is not actually part of the parsable
				 * expression, but it is a useful extension to
				 * the simple '{' '}' that MS excel(tm) uses.
				 */
				if (gnm_expr_top_is_array_corner (texpr))
					corner = cell;
				else if (gnm_expr_top_is_array_elem (texpr, &x, &y)) {
					corner = sheet_cell_get
						(sheet,
						 cell->pos.col - x,
						 cell->pos.row - y);
				}

				if (corner) {
					GnmExprArrayCorner const *ac = gnm_cell_is_array_corner (corner);

					char *tmp = g_strdup_printf
						("{%s}(%d%c%d)[%d][%d]",
						 text,
						 ac->cols,
						 go_locale_get_arg_sep (),
						 ac->rows,
						 x, y);
					g_free (text);
					text = tmp;
				}
			}
		} else
469 470 471 472 473 474 475 476 477 478 479 480
			text = g_strdup ("");

		if (optional_wbc == NULL) {
			WORKBOOK_VIEW_FOREACH_CONTROL (wbv, control,
				wb_control_edit_line_set (control, text););
		} else
			wb_control_edit_line_set (optional_wbc, text);

		g_free (text);
	}
}

481
static void
Jody Goldberg's avatar
Jody Goldberg committed
482
accumulate_regions (SheetView *sv,  GnmRange const *r, gpointer closure)
483 484
{
	GnmExprList	**selection = closure;
Jody Goldberg's avatar
Jody Goldberg committed
485
	GnmCellRef a, b;
486 487 488 489 490 491 492 493 494 495 496 497

	a.sheet = b.sheet = sv_sheet (sv);
	a.col_relative = a.row_relative = b.col_relative = b.row_relative = FALSE;
	a.col = r->start.col;
	a.row = r->start.row;
	b.col = r->end.col;
	b.row = r->end.row;

	*selection = gnm_expr_list_prepend (*selection,
		gnm_expr_new_constant (value_new_cellrange_unsafe (&a, &b)));
}

Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
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
static gboolean
wb_view_darken_foreground_attributes_cb (PangoAttribute *attribute,
					 G_GNUC_UNUSED gpointer data)
{
	if (attribute->klass->type == PANGO_ATTR_FOREGROUND) {
		PangoAttrColor *cat = (PangoAttrColor *) attribute;
		guint total = (guint)cat->color.red + (guint)cat->color.green + (guint)cat->color.blue;
		if (total > 98302) {
			float adj = 98302.5/total;
			cat->color.red = cat->color.red * adj;
			cat->color.green = cat->color.green * adj;
			cat->color.blue = cat->color.blue * adj;
		}
	}
	return FALSE;
}

static void
wb_view_darken_foreground_attributes (PangoAttrList *attrs)
{
	pango_attr_list_unref 
		(pango_attr_list_filter 
		 (attrs,
		  wb_view_darken_foreground_attributes_cb,
		  NULL));
}

525
void
526
wb_view_auto_expr_recalc (WorkbookView *wbv)
527
{
528
	GnmEvalPos      ep;
529
	GnmExprList	*selection = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
530
	GnmValue	*v;
Jody Goldberg's avatar
Jody Goldberg committed
531
	SheetView	*sv;
532
	GnmExprTop const *texpr;
533 534
	GnmRange        r;
	GString *str;
535 536 537

	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

Jody Goldberg's avatar
Jody Goldberg committed
538
	sv = wb_view_cur_sheet_view (wbv);
539 540
	if (wbv->current_sheet == NULL ||
	    sv == NULL)
Jody Goldberg's avatar
Jody Goldberg committed
541 542
		return;

543 544 545 546 547 548 549 550 551 552 553 554 555
	if (wbv->auto_expr_sheet != NULL &&
	    wbv->auto_expr_descr != NULL &&
	    wbv->auto_expr_cell.row >= 0 &&
	    wbv->auto_expr_cell.col >= 0) {
		/* We need to check that wbv->auto_expr_sheet is still valid */
		GSList *sheets = workbook_sheets (wbv->wb);
		if (g_slist_find (sheets, wbv->auto_expr_sheet) == NULL) {
			v = value_new_error_REF (NULL);
			str = g_string_new ("?!");
		} else {
			range_init_cellpos (&r, &wbv->auto_expr_cell);
			v = value_new_cellrange_r (wbv->auto_expr_sheet, &r);
			if (strlen (wbv->auto_expr_sheet->name_unquoted) < 8) {
Jean Bréfort's avatar
Jean Bréfort committed
556
				str = g_string_new
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
					(wbv->auto_expr_sheet->name_unquoted);
				g_string_append_c (str, '!');
			} else
				str = g_string_new ("\342\200\246!");
		}
		texpr = gnm_expr_top_new_constant (v);
		g_string_append (str, wbv->auto_expr_descr);
	} else if (wbv->auto_expr_func != NULL &&
		   wbv->auto_expr_descr != NULL) {
		sv_selection_apply (sv, &accumulate_regions, FALSE, &selection);
		texpr = gnm_expr_top_new
			(gnm_expr_new_funcall (wbv->auto_expr_func, selection));
		str = g_string_new (wbv->auto_expr_descr);
	} else
		return;
572

573 574
	eval_pos_init_sheet (&ep, wbv->current_sheet);
	v = gnm_expr_top_eval (texpr, &ep, GNM_EXPR_EVAL_SCALAR_NON_EMPTY);
Jody Goldberg's avatar
Jody Goldberg committed
575

576
	if (v) {
577
		GOFormat const *format = NULL;
578
		GOFormat const *tmp_format = NULL;
579
		PangoAttrList *attrs = NULL;
580

Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
581
		g_string_append (str, " = ");
Morten Welinder's avatar
Morten Welinder committed
582
		if (!wbv->auto_expr_use_max_precision) {
583
			format = VALUE_FMT (v);
584
			if (!format)
585
				format = tmp_format =
586
					auto_style_format_suggest (texpr, &ep);
587 588 589
		}

		if (format) {
590 591
			PangoContext *context = gnm_pango_context_get ();
			PangoLayout *layout = pango_layout_new (context);
592
			gsize old_len = str->len;
593 594 595
			GOFormatNumberError err =
				format_value_layout (layout, format, v,
						     /* Note that we created a label large enough for */
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
596 597
						     /* "Sumerage = -012345678901234" */
						     27 - g_utf8_strlen (str->str, -1),
598
						     workbook_date_conv (wb_view_get_workbook (wbv)));
599
			go_format_unref (tmp_format);
600 601 602 603 604
			switch (err) {
			case GO_FORMAT_NUMBER_OK:
			case GO_FORMAT_NUMBER_DATE_ERROR: {
				PangoAttrList *atl;

605
				go_pango_translate_layout (layout); /* translating custom attributes */
606 607 608
				g_string_append (str, pango_layout_get_text (layout));
				/* We need to shift the attribute list  */
				atl = pango_attr_list_ref (pango_layout_get_attributes (layout));
609 610 611 612 613 614
				if (atl != NULL) {
					attrs = pango_attr_list_new ();
					pango_attr_list_splice
						(attrs, atl, old_len,
						 str->len - old_len);
					pango_attr_list_unref (atl);
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
615 616 617
					/* The field background is white so we need to ensure that no */
					/* foreground colour is set to white (or close to white)      */
					wb_view_darken_foreground_attributes (attrs);
618
				}
619 620 621 622 623 624 625
				break;
			}
			default:
			case GO_FORMAT_NUMBER_INVALID_FORMAT:
				g_string_append (str,  _("Invalid format"));
				break;
			}
626 627 628
			g_object_unref (layout);
			g_object_unref (context);
		} else
629 630
			g_string_append (str, value_peek_string (v));

631 632 633 634 635
		g_object_set (wbv,
			      "auto-expr-text", str->str,
			      "auto-expr-attrs", attrs,
			      NULL);
		pango_attr_list_unref (attrs);
636
		value_release (v);
637
	} else {
638 639 640 641
		g_object_set (wbv,
			      "auto-expr-text", "Internal ERROR",
			      "auto-expr-attrs", NULL,
			      NULL);
642
	}
643
	g_string_free (str, TRUE);
644
	gnm_expr_top_unref (texpr);
645 646
}

Jody Goldberg's avatar
new.  
Jody Goldberg committed
647 648 649
/* perform whatever initialization of a control that is necessary when it
 * finally gets assigned to a view with a workbook */
static void
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
650
wb_view_init_control (G_GNUC_UNUSED WorkbookControl *wbc)
Jody Goldberg's avatar
new.  
Jody Goldberg committed
651 652 653
{
}

Jody Goldberg's avatar
Jody Goldberg committed
654
void
655
wb_view_attach_control (WorkbookView *wbv, WorkbookControl *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
656
{
657
	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));
658
	g_return_if_fail (IS_WORKBOOK_CONTROL (wbc));
659
	g_return_if_fail (wb_control_view (wbc) == NULL);
660

661 662 663
	if (wbv->wb_controls == NULL)
		wbv->wb_controls = g_ptr_array_new ();
	g_ptr_array_add (wbv->wb_controls, wbc);
664
	g_object_set (G_OBJECT (wbc), "view", wbv, NULL);
665

Jody Goldberg's avatar
new.  
Jody Goldberg committed
666 667
	if (wbv->wb != NULL)
		wb_view_init_control (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
668
}
669 670

void
671
wb_view_detach_control (WorkbookControl *wbc)
672
{
673
	g_return_if_fail (IS_WORKBOOK_CONTROL (wbc));
674
	g_return_if_fail (IS_WORKBOOK_VIEW (wb_control_view (wbc)));
675

676 677 678 679 680
	g_ptr_array_remove (wbc->wb_view->wb_controls, wbc);
	if (wbc->wb_view->wb_controls->len == 0) {
		g_ptr_array_free (wbc->wb_view->wb_controls, TRUE);
		wbc->wb_view->wb_controls = NULL;
	}
681
	g_object_set (G_OBJECT (wbc), "view", NULL, NULL);
682 683
}

Jody Goldberg's avatar
Jody Goldberg committed
684
static GObjectClass *parent_class;
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

static void
wb_view_auto_expr_func (WorkbookView *wbv, GnmFunc *func)
{
	if (wbv->auto_expr_func == func)
		return;

	if (wbv->auto_expr_func)
		gnm_func_unref (wbv->auto_expr_func);

	if (func)
		gnm_func_ref (func);
	wbv->auto_expr_func = func;

	wb_view_auto_expr_recalc (wbv);
}

static void
wb_view_auto_expr_descr (WorkbookView *wbv, const char *descr)
{
	char *s;

	if (go_str_compare (descr, wbv->auto_expr_descr) == 0)
		return;

	s = g_strdup (descr);
	g_free (wbv->auto_expr_descr);
	wbv->auto_expr_descr = s;

	wb_view_auto_expr_recalc (wbv);
}

static void
wb_view_auto_expr_precision (WorkbookView *wbv, gboolean use_max_precision)
{
	use_max_precision = !!use_max_precision;

	if (wbv->auto_expr_use_max_precision == use_max_precision)
		return;

	wbv->auto_expr_use_max_precision = use_max_precision;

	wb_view_auto_expr_recalc (wbv);
}

730 731 732 733 734 735 736 737 738 739 740 741 742
static void
wb_view_auto_expr_text (WorkbookView *wbv, const char *text)
{
	char *s;

	if (go_str_compare (text, wbv->auto_expr_text) == 0)
		return;

	s = g_strdup (text);
	g_free (wbv->auto_expr_text);
	wbv->auto_expr_text = s;
}

743 744 745 746 747 748 749 750 751
static void
wb_view_auto_expr_attrs (WorkbookView *wbv, PangoAttrList *attrs)
{
	if (gnm_pango_attr_list_equal (attrs, wbv->auto_expr_attrs))
		return;

	if (attrs)
		pango_attr_list_ref (attrs);
	if (wbv->auto_expr_attrs)
Andreas J. Guelzow 's avatar
Andreas J. Guelzow committed
752
		pango_attr_list_unref (wbv->auto_expr_attrs);
753 754 755
	wbv->auto_expr_attrs = attrs;
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
static void
wb_view_auto_expr_cell (WorkbookView *wbv, gpointer *cell)
{
	if (cell == NULL) {
		wbv->auto_expr_cell.col = -1;
		wbv->auto_expr_cell.row = -1;
	} else {
		wbv->auto_expr_cell = *((GnmCellPos *)cell);
		wb_view_auto_expr_recalc (wbv);
	}
}

static void
wb_view_auto_expr_sheet (WorkbookView *wbv, gpointer *sheet)
{
	wbv->auto_expr_sheet = (Sheet *)sheet;

	if (sheet != NULL)
		wb_view_auto_expr_recalc (wbv);
}


778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
static void
wb_view_set_property (GObject *object, guint property_id,
		      const GValue *value, GParamSpec *pspec)
{
	WorkbookView *wbv = (WorkbookView *)object;

	switch (property_id) {
	case PROP_AUTO_EXPR_FUNC:
		wb_view_auto_expr_func (wbv, g_value_get_pointer (value));
		break;
	case PROP_AUTO_EXPR_DESCR:
		wb_view_auto_expr_descr (wbv, g_value_get_string (value));
		break;
	case PROP_AUTO_EXPR_MAX_PRECISION:
		wb_view_auto_expr_precision (wbv, g_value_get_boolean (value));
		break;
794 795 796
	case PROP_AUTO_EXPR_TEXT:
		wb_view_auto_expr_text (wbv, g_value_get_string (value));
		break;
797 798 799
	case PROP_AUTO_EXPR_ATTRS:
		wb_view_auto_expr_attrs (wbv, g_value_peek_pointer (value));
		break;
800 801 802 803 804 805
	case PROP_AUTO_EXPR_CELL:
		wb_view_auto_expr_cell (wbv, g_value_get_pointer (value));
		break;
	case PROP_AUTO_EXPR_SHEET:
		wb_view_auto_expr_sheet (wbv, g_value_get_pointer (value));
		break;
806
	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
807
		wbv->show_horizontal_scrollbar = !!g_value_get_boolean (value);
808 809
		break;
	case PROP_SHOW_VERTICAL_SCROLLBAR:
810
		wbv->show_vertical_scrollbar = !!g_value_get_boolean (value);
811 812
		break;
	case PROP_SHOW_NOTEBOOK_TABS:
813
		wbv->show_notebook_tabs = !!g_value_get_boolean (value);
814
		break;
815 816 817 818 819
	case PROP_SHOW_FUNCTION_CELL_MARKERS:
		wbv->show_function_cell_markers = !!g_value_get_boolean (value);
		if (wbv->current_sheet)
			sheet_redraw_all (wbv->current_sheet, FALSE);
		break;
820 821 822 823 824
	case PROP_SHOW_EXTENSION_MARKERS:
		wbv->show_extension_markers = !!g_value_get_boolean (value);
		if (wbv->current_sheet)
			sheet_redraw_all (wbv->current_sheet, FALSE);
		break;
825
	case PROP_DO_AUTO_COMPLETION:
826
		wbv->do_auto_completion = !!g_value_get_boolean (value);
827 828
		break;
	case PROP_PROTECTED:
829
		wbv->is_protected = !!g_value_get_boolean (value);
830 831 832 833 834 835 836
		break;
	case PROP_PREFERRED_WIDTH:
		wbv->preferred_width = g_value_get_int (value);
		break;
	case PROP_PREFERRED_HEIGHT:
		wbv->preferred_height = g_value_get_int (value);
		break;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
wb_view_get_property (GObject *object, guint property_id,
		      GValue *value, GParamSpec *pspec)
{
	WorkbookView *wbv = (WorkbookView *)object;

	switch (property_id) {
	case PROP_AUTO_EXPR_FUNC:
		g_value_set_pointer (value, wbv->auto_expr_func);
		break;
	case PROP_AUTO_EXPR_DESCR:
		g_value_set_string (value, wbv->auto_expr_descr);
		break;
	case PROP_AUTO_EXPR_MAX_PRECISION:
		g_value_set_boolean (value, wbv->auto_expr_use_max_precision);
		break;
859 860 861
	case PROP_AUTO_EXPR_TEXT:
		g_value_set_string (value, wbv->auto_expr_text);
		break;
862 863 864
	case PROP_AUTO_EXPR_ATTRS:
		g_value_set_boxed (value, wbv->auto_expr_attrs);
		break;
865 866 867 868 869 870
	case PROP_AUTO_EXPR_CELL:
		g_value_set_pointer (value, &wbv->auto_expr_cell);
		break;
	case PROP_AUTO_EXPR_SHEET:
		g_value_set_pointer (value, wbv->auto_expr_sheet);
		break;
871 872 873 874 875 876 877 878 879
	case PROP_SHOW_HORIZONTAL_SCROLLBAR:
		g_value_set_boolean (value, wbv->show_horizontal_scrollbar);
		break;
	case PROP_SHOW_VERTICAL_SCROLLBAR:
		g_value_set_boolean (value, wbv->show_vertical_scrollbar);
		break;
	case PROP_SHOW_NOTEBOOK_TABS:
		g_value_set_boolean (value, wbv->show_notebook_tabs);
		break;
880 881 882
	case PROP_SHOW_FUNCTION_CELL_MARKERS:
		g_value_set_boolean (value, wbv->show_function_cell_markers);
		break;
883 884 885
	case PROP_SHOW_EXTENSION_MARKERS:
		g_value_set_boolean (value, wbv->show_extension_markers);
		break;
886 887 888 889 890 891 892 893 894 895 896 897
	case PROP_DO_AUTO_COMPLETION:
		g_value_set_boolean (value, wbv->do_auto_completion);
		break;
	case PROP_PROTECTED:
		g_value_set_boolean (value, wbv->is_protected);
		break;
	case PROP_PREFERRED_WIDTH:
		g_value_set_int (value, wbv->preferred_width);
		break;
	case PROP_PREFERRED_HEIGHT:
		g_value_set_int (value, wbv->preferred_height);
		break;
898 899 900 901 902 903
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

904 905 906 907 908 909 910 911
void
wb_view_detach_from_workbook (WorkbookView *wbv)
{
	g_return_if_fail (IS_WORKBOOK_VIEW (wbv));

	if (wbv->wb) {
		workbook_detach_view (wbv);
		wbv->wb = NULL;
Morten Welinder's avatar
Morten Welinder committed
912
		wbv->current_sheet = NULL;
913 914 915
	}
}

916
static void
917
wb_view_dispose (GObject *object)
918
{
919
	WorkbookView *wbv = WORKBOOK_VIEW (object);
920

921 922 923 924 925 926 927
	if (wbv->wb_controls != NULL) {
		WORKBOOK_VIEW_FOREACH_CONTROL (wbv, control, {
			wb_control_sheet_remove_all (control);
			wb_view_detach_control (control);
			g_object_unref (G_OBJECT (control));
		});
		if (wbv->wb_controls != NULL)
928
			g_warning ("Unexpected left-over controls");
929 930
	}

931
	wb_view_detach_from_workbook (wbv);
932

933 934 935 936 937 938 939 940 941
	parent_class->dispose (object);
}


static void
wb_view_finalize (GObject *object)
{
	WorkbookView *wbv = WORKBOOK_VIEW (object);

942 943 944
	if (wbv->auto_expr_func) {
		gnm_func_unref (wbv->auto_expr_func);
		wbv->auto_expr_func = NULL;
945
	}
946

947 948
	g_free (wbv->auto_expr_descr);
	wbv->auto_expr_descr = NULL;
949

950 951
	g_free (wbv->auto_expr_text);
	wbv->auto_expr_text = NULL;
952

953 954 955 956 957
	if (wbv->auto_expr_attrs) {
		pango_attr_list_unref (wbv->auto_expr_attrs);
		wbv->auto_expr_attrs = NULL;
	}

Jody Goldberg's avatar
Jody Goldberg committed
958 959 960 961
	if (wbv->current_style != NULL) {
		gnm_style_unref (wbv->current_style);
		wbv->current_style = NULL;
	}
Jody Goldberg's avatar
Jody Goldberg committed
962 963 964 965
	if (wbv->in_cell_combo != NULL) {
		sheet_object_clear_sheet (wbv->in_cell_combo);
		g_object_unref (wbv->in_cell_combo);
		wbv->in_cell_combo = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
966 967
	}

968
	parent_class->finalize (object);
969 970
}

971
static void
972
workbook_view_class_init (GObjectClass *gobject_class)
973
{
974 975 976 977 978
	parent_class = g_type_class_peek_parent (gobject_class);

	gobject_class->set_property = wb_view_set_property;
	gobject_class->get_property = wb_view_get_property;
	gobject_class->finalize = wb_view_finalize;
979
	gobject_class->dispose = wb_view_dispose;
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

	/* FIXME?  Make a boxed type.  */
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_FUNC,
		 g_param_spec_pointer ("auto-expr-func",
				       _("Auto-expression function"),
				       _("The automatically computed sheet function."),
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_DESCR,
		 g_param_spec_string ("auto-expr-descr",
				      _("Auto-expression description"),
				      _("Description of the automatically computed sheet function."),
				      NULL,
				      GSF_PARAM_STATIC |
				      G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_MAX_PRECISION,
		 g_param_spec_boolean ("auto-expr-max-precision",
				       _("Auto-expression maximum precision"),
				       _("Use maximum available precision for auto-expressions"),
				       FALSE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
1008 1009 1010 1011 1012 1013 1014 1015 1016
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_TEXT,
		 g_param_spec_string ("auto-expr-text",
				      _("Auto-expression text"),
				      _("Displayed text for the automatically computed sheet function."),
				      NULL,
				      GSF_PARAM_STATIC |
				      G_PARAM_READWRITE));
1017 1018 1019 1020 1021 1022 1023 1024
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_ATTRS,
		 g_param_spec_boxed ("auto-expr-attrs",
				     _("Auto-expression Attributes"),
				     _("Text attributes for the automatically computed sheet function."),
				     PANGO_TYPE_ATTR_LIST,
				     GSF_PARAM_STATIC | G_PARAM_READWRITE));
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_CELL,
		 g_param_spec_pointer ("auto-expr-cell",
				       _("Auto-expression Cell Position"),
				       _("The address of the cell to be shown."),
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_AUTO_EXPR_SHEET,
		 g_param_spec_pointer ("auto-expr-sheet",
				       _("Auto-expression Sheet"),
				       _("The sheet on which the cell resides."),
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
        g_object_class_install_property
		(gobject_class,
		 PROP_SHOW_HORIZONTAL_SCROLLBAR,
		 g_param_spec_boolean ("show-horizontal-scrollbar",
				       _("Show horizontal scrollbar"),
				       _("Show the horizontal scrollbar"),
				       TRUE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_SHOW_VERTICAL_SCROLLBAR,
		 g_param_spec_boolean ("show-vertical-scrollbar",
				       _("Show vertical scrollbar"),
				       _("Show the vertical scrollbar"),
				       TRUE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_SHOW_NOTEBOOK_TABS,
		 g_param_spec_boolean ("show-notebook-tabs",
				       _("Show notebook tabs"),
				       _("Show the notebook tabs for sheets"),
				       TRUE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
1068 1069 1070 1071 1072 1073 1074 1075 1076
        g_object_class_install_property
		(gobject_class,
		 PROP_SHOW_FUNCTION_CELL_MARKERS,
		 g_param_spec_boolean ("show-function-cell-markers",
				       _("Show formula cell markers"),
				       _("Mark each cell containing a formula"),
				       FALSE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
1077
        g_object_class_install_property
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		(gobject_class,
		 PROP_SHOW_EXTENSION_MARKERS,
		 g_param_spec_boolean ("show-extension-markers",
				       _("Show extension markers"),
				       _("Mark each cell that fails to show "
					 "the complete content"),
				       FALSE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
	g_object_class_install_property
1088 1089 1090 1091 1092
		(gobject_class,
		 PROP_DO_AUTO_COMPLETION,
		 g_param_spec_boolean ("do-auto-completion",
				       _("Do auto completion"),
				       _("Auto-complete text"),
1093
				       gnm_conf_get_core_gui_editing_autocomplete (),
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_PROTECTED,
		 g_param_spec_boolean ("protected",
				       _("Protected"),
				       _("Is view protected?"),
				       FALSE,
				       GSF_PARAM_STATIC |
				       G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_PREFERRED_WIDTH,
		 g_param_spec_int ("preferred-width",
				   _("Preferred width"),
				   _("Preferred width"),
				   1, G_MAXINT, 1024,
				   GSF_PARAM_STATIC |
				   G_PARAM_READWRITE));
        g_object_class_install_property
		(gobject_class,
		 PROP_PREFERRED_HEIGHT,
		 g_param_spec_int ("preferred-height",
				   _("Preferred height"),
				   _("Preferred height"),
				   1, G_MAXINT, 768,
				   GSF_PARAM_STATIC |
				   G_PARAM_READWRITE));
1123 1124

	parent_class = g_type_class_peek_parent (gobject_class);
1125 1126
}

Jody Goldberg's avatar
Jody Goldberg committed
1127
GSF_CLASS (WorkbookView, workbook_view,
Jody Goldberg's avatar
Jody Goldberg committed
1128
	   workbook_view_class_init, NULL, G_TYPE_OBJECT)
1129 1130 1131

WorkbookView *
workbook_view_new (Workbook *wb)
1132
{
1133
	WorkbookView *wbv = g_object_new (WORKBOOK_VIEW_TYPE, NULL);
1134
	int i;
1135

1136 1137 1138 1139 1140
	if (wb == NULL)
		wb = workbook_new ();

	g_return_val_if_fail (wb != NULL, NULL);

1141 1142
	wbv->wb = wb;
	workbook_attach_view (wbv);
1143 1144 1145 1146

	wbv->show_horizontal_scrollbar = TRUE;
	wbv->show_vertical_scrollbar = TRUE;
	wbv->show_notebook_tabs = TRUE;
1147 1148
	wbv->show_function_cell_markers = gnm_conf_get_core_gui_cells_function_markers ();
	wbv->show_extension_markers = gnm_conf_get_core_gui_cells_extension_markers ();
1149
	wbv->do_auto_completion = gnm_conf_get_core_gui_editing_autocomplete ();
1150
	wbv->is_protected = FALSE;
1151

Jody Goldberg's avatar
Jody Goldberg committed
1152
	wbv->current_style      = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
1153
	wbv->in_cell_combo      = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
1154 1155

	wbv->current_sheet      = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
1156
	wbv->current_sheet_view = NULL;
1157

1158 1159 1160 1161 1162
	/* Set the default operation to be performed over selections */
	wbv->auto_expr_func = gnm_func_lookup ("sum", NULL);
	if (wbv->auto_expr_func)
		gnm_func_ref (wbv->auto_expr_func);
	wbv->auto_expr_descr = g_strdup (_("Sum"));
1163
	wbv->auto_expr_text = NULL;
1164
	wbv->auto_expr_attrs = NULL;
1165 1166
	wbv->auto_expr_use_max_precision = FALSE;

1167 1168
	for (i = 0 ; i < workbook_sheet_count (wb); i++)
		wb_view_sheet_add (wbv, workbook_sheet_by_index (wb, i));
Jody Goldberg's avatar
Jody Goldberg committed
1169

Jody Goldberg's avatar
new.  
Jody Goldberg committed
1170 1171
	WORKBOOK_VIEW_FOREACH_CONTROL (wbv, wbc,
		wb_view_init_control (wbc););
1172

1173
	return wbv;
1174
}
1175

1176 1177 1178 1179 1180
/**
 * wbv_save_to_output :
 * @wbv : #WorkbookView
 * @fs  : #GOFileSaver
 * @output : #GsfOutput
1181
 * @io_context : #GOIOContext
1182 1183 1184 1185 1186
 *
 * NOTE : Temporary api until we get the new output framework.
 **/
void
wbv_save_to_output (WorkbookView *wbv, GOFileSaver const *fs,
1187
		    GsfOutput *output, GOIOContext *io_context)
1188 1189 1190
{
	GError const *err;
	char const   *msg;
1191
	GODoc *godoc = wb_view_get_doc (wbv);
1192

1193 1194 1195
	if (go_doc_is_dirty (godoc))
	  /* FIXME: we should be using the true modification time */
	  gnm_insert_meta_date (godoc, GSF_META_NAME_DATE_MODIFIED);
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	go_file_saver_save (fs, io_context, wbv, output);

	/* The plugin convention is unclear */
	if (!gsf_output_is_closed (output))
		gsf_output_close (output);

	if (NULL == (err = gsf_output_error (output)))
		return;
	if (NULL == (msg = err->message))
		msg = _("An unexplained error happened while saving.");
	g_printerr ("  ==> %s\n", msg);
1207
	if (!go_io_error_occurred (io_context))
1208 1209 1210
		go_cmd_context_error_export (GO_CMD_CONTEXT (io_context), msg);
}

1211 1212
void
wb_view_save_to_uri (WorkbookView *wbv, GOFileSaver const *fs,
1213
		     char const *uri, GOIOContext *io_context)
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
1214
{
1215
	char   *msg = NULL;
Morten Welinder's avatar
Morten Welinder committed
1216
	GError *err = NULL;
1217
	GsfOutput *output = go_file_create (uri, &err);
1218

Morten Welinder's avatar
Morten Welinder committed
1219
	if (output == NULL) {
1220 1221 1222 1223 1224 1225
		if (NULL != err) {
			msg = g_strdup_printf (_("Can't open '%s' for writing: %s"),
						     uri, err->message);
			g_error_free (err);
		} else
			msg = g_strdup_printf (_("Can't open '%s' for writing"), uri);
Morten Welinder's avatar
Morten Welinder committed
1226

1227 1228 1229
		go_cmd_context_error_export (GO_CMD_CONTEXT (io_context), msg);
		g_free (msg);
	} else {