workbook-control.c 14.8 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3

/*
Jody Goldberg's avatar
Jody Goldberg committed
4
 * workbook-control.c: The base class for the displaying a workbook.
5
 *
6
 * Copyright (C) 2000-2002 Jody Goldberg (jody@gnome.org)
7
 *
8 9 10 11
 * 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) version 3.
12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21 22
 * USA
 */
23

24
#include <gnumeric-config.h>
25
#include "gnm-i18n.h"
26
#include "gnumeric.h"
27
#include "workbook-control-priv.h"
28

29
#include "application.h"
30
#include "workbook-view.h"
31
#include "workbook-priv.h"
32
#include "sheet.h"
33
#include "sheet-view.h"
34
#include "sheet-utils.h"
35
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
36
#include "commands.h"
37 38 39
#include "value.h"
#include "ranges.h"
#include "expr-name.h"
40
#include "expr.h"
41
#include "command-context.h"
42
#include <goffice/goffice.h>
Jody Goldberg's avatar
Jody Goldberg committed
43
#include <gsf/gsf-impl-utils.h>
44

45 46 47 48 49
enum {
	PROP_0,
	PROP_VIEW
};

Morten Welinder's avatar
Morten Welinder committed
50
#define WBC_CLASS(o) GNM_WBC_CLASS (G_OBJECT_GET_CLASS (o))
51 52 53
#define WBC_VIRTUAL_FULL(func, handle, arglist, call)		\
void wb_control_ ## func arglist				\
{								\
Jody Goldberg's avatar
Jody Goldberg committed
54
	WorkbookControlClass *wbc_class = WBC_CLASS (wbc);	\
55
								\
Jody Goldberg's avatar
Jody Goldberg committed
56
	g_return_if_fail (wbc_class != NULL);			\
57 58 59 60 61 62
								\
	if (wbc_class != NULL && wbc_class->handle != NULL)	\
		wbc_class->handle call;				\
}
#define WBC_VIRTUAL(func, arglist, call) WBC_VIRTUAL_FULL(func, func, arglist, call)

63 64 65 66 67
/**
 * workbook_control_new_wrapper:
 * @wbc: #WorkbookControl
 * @wbv: #WorkbookView
 * @wb: #Workbook
Morten Welinder's avatar
Morten Welinder committed
68
 * @extra: (allow-none):
69 70 71
 *
 * Returns: (transfer full): the newly allocated #WorkbookControl.
 **/
Jody Goldberg's avatar
Jody Goldberg committed
72
WorkbookControl *
73
workbook_control_new_wrapper (WorkbookControl *wbc, WorkbookView *wbv, Workbook *wb,
74
			void *extra)
Jody Goldberg's avatar
Jody Goldberg committed
75
{
Jody Goldberg's avatar
Jody Goldberg committed
76
	WorkbookControlClass *wbc_class = WBC_CLASS (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
77

Jody Goldberg's avatar
Jody Goldberg committed
78
	g_return_val_if_fail (wbc_class != NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
79 80

	if (wbc_class != NULL && wbc_class->control_new != NULL)
81
		return wbc_class->control_new (wbc, wbv, wb, extra);
Jody Goldberg's avatar
Jody Goldberg committed
82 83 84
	return NULL;
}

85 86
WBC_VIRTUAL (style_feedback,
	(WorkbookControl *wbc, GnmStyle const *changes), (wbc, changes))
87
WBC_VIRTUAL (edit_line_set,
Jody Goldberg's avatar
Jody Goldberg committed
88
	(WorkbookControl *wbc, char const *text), (wbc, text))
Jody Goldberg's avatar
Jody Goldberg committed
89
WBC_VIRTUAL (selection_descr_set,
Jody Goldberg's avatar
Jody Goldberg committed
90
	(WorkbookControl *wbc, char const *text), (wbc, text))
Jody Goldberg's avatar
Jody Goldberg committed
91

Jody Goldberg's avatar
Jody Goldberg committed
92
WBC_VIRTUAL_FULL (sheet_remove, sheet.remove,
Jody Goldberg's avatar
Jody Goldberg committed
93
	(WorkbookControl *wbc, Sheet *sheet), (wbc, sheet))
Jody Goldberg's avatar
Jody Goldberg committed
94
WBC_VIRTUAL_FULL (sheet_focus, sheet.focus,
Jody Goldberg's avatar
Jody Goldberg committed
95
	(WorkbookControl *wbc, Sheet *sheet), (wbc, sheet))
Jody Goldberg's avatar
Jody Goldberg committed
96
WBC_VIRTUAL_FULL (sheet_remove_all, sheet.remove_all,
Jody Goldberg's avatar
Jody Goldberg committed
97
	(WorkbookControl *wbc), (wbc))
98

99 100
WBC_VIRTUAL_FULL (undo_redo_truncate, undo_redo.truncate,
	(WorkbookControl *wbc, int n, gboolean is_undo), (wbc, n, is_undo))
101 102 103
WBC_VIRTUAL_FULL (undo_redo_pop, undo_redo.pop,
	(WorkbookControl *wbc, gboolean is_undo), (wbc, is_undo))
WBC_VIRTUAL_FULL (undo_redo_push, undo_redo.push,
Jody Goldberg's avatar
Jody Goldberg committed
104 105
	(WorkbookControl *wbc, gboolean is_undo, char const *text, gpointer key),
	(wbc, is_undo, text, key))
106 107 108 109
WBC_VIRTUAL_FULL (undo_redo_labels, undo_redo.labels,
	(WorkbookControl *wbc, char const *undo, char const *redo),
	(wbc, undo, redo))

110
WBC_VIRTUAL_FULL (menu_state_update, menu_state.update,
111 112
        (WorkbookControl *wbc, int flags),
	(wbc, flags))
Jody Goldberg's avatar
Jody Goldberg committed
113

114
WBC_VIRTUAL (paste_from_selection,
Morten Welinder's avatar
Morten Welinder committed
115
	(WorkbookControl *wbc, GnmPasteTarget const *pt), (wbc, pt))
116 117
WBC_VIRTUAL (update_action_sensitivity,
	(WorkbookControl *wbc), (wbc))
118

Jody Goldberg's avatar
Jody Goldberg committed
119
void
120
wb_control_sheet_add (WorkbookControl *wbc, SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
121 122 123
{
	WorkbookControlClass *wbc_class;

Morten Welinder's avatar
Morten Welinder committed
124
	g_return_if_fail (GNM_IS_WBC (wbc));
Jody Goldberg's avatar
Jody Goldberg committed
125 126 127

	wbc_class = WBC_CLASS (wbc);
	if (wbc_class != NULL && wbc_class->sheet.add != NULL) {
128
		Sheet *new_sheet = sv_sheet (sv);
Jody Goldberg's avatar
Jody Goldberg committed
129

130
		wbc_class->sheet.add (wbc, sv);
Jody Goldberg's avatar
Jody Goldberg committed
131 132 133

		/* If this is the current sheet init the display */
		if (new_sheet == wb_control_cur_sheet (wbc)) {
134
			WorkbookView *wbv = wb_control_view (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
135
			wb_control_sheet_focus (wbc, new_sheet);
136 137
			wb_view_selection_desc (wbv, TRUE, wbc);
			wb_view_edit_line_set (wbv, wbc);
138
			wb_control_style_feedback (wbc, NULL);
139
			wb_control_menu_state_update (wbc, MS_ALL);
140
			wb_control_update_action_sensitivity (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
141 142 143 144
		}
	}
}

145 146 147 148 149
gboolean
wb_control_claim_selection (WorkbookControl *wbc)
{
	WorkbookControlClass *wbc_class;

Morten Welinder's avatar
Morten Welinder committed
150
	g_return_val_if_fail (GNM_IS_WBC (wbc), FALSE);
151 152 153 154 155 156 157

	wbc_class = WBC_CLASS (wbc);
	if (wbc_class != NULL && wbc_class->claim_selection != NULL)
		return wbc_class->claim_selection (wbc);
	return TRUE; /* no handler means we always get the selection */
}

Jody Goldberg's avatar
Jody Goldberg committed
158
/**
159
 * wb_control_validation_msg:
160 161
 *	 1 : ignore invalid and accept result
 *	 0 : discard invalid and finish editing
Jody Goldberg's avatar
Jody Goldberg committed
162
 *	-1 : continue editing
163
 **/
Jody Goldberg's avatar
Jody Goldberg committed
164
int
165
wb_control_validation_msg (WorkbookControl *wbc, ValidationStyle v,
Jody Goldberg's avatar
Jody Goldberg committed
166 167 168 169
			   char const *title, char const *msg)
{
	WorkbookControlClass *wbc_class;

Morten Welinder's avatar
Morten Welinder committed
170
	g_return_val_if_fail (GNM_IS_WBC (wbc), 1);
Jody Goldberg's avatar
Jody Goldberg committed
171 172 173 174 175 176 177

	wbc_class = WBC_CLASS (wbc);
	if (wbc_class != NULL && wbc_class->validation_msg != NULL)
		return wbc_class->validation_msg (wbc, v, title, msg);
	return 1; /* no handler, always accept */
}

178 179 180 181 182 183
/**
 * wb_control_view:
 * @wbc: #WorkbookControl
 *
 * Returns: (transfer none): the workbook view.
 **/
Jody Goldberg's avatar
Jody Goldberg committed
184
WorkbookView *
185
wb_control_view (WorkbookControl const *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
186
{
Morten Welinder's avatar
Morten Welinder committed
187
	g_return_val_if_fail (GNM_IS_WBC (wbc), NULL);
Jody Goldberg's avatar
Jody Goldberg committed
188 189 190
	return wbc->wb_view;
}

191 192 193 194 195 196
/**
 * wb_control_get_doc:
 * @wbc: #WorkbookControl
 *
 * Returns: (transfer none): the workbook set as a #GODoc.
 **/
197 198 199 200 201 202
GODoc *
wb_control_get_doc (WorkbookControl const *wbc)
{
	return GO_DOC (wb_control_get_workbook (wbc));
}

203 204 205 206 207 208
/**
 * wb_control_get_workbook:
 * @wbc: #WorkbookControl
 *
 * Returns: (transfer none): the workbook.
 **/
209
Workbook *
210
wb_control_get_workbook (WorkbookControl const *wbc)
211
{
Morten Welinder's avatar
Morten Welinder committed
212
	g_return_val_if_fail (GNM_IS_WBC (wbc), NULL);
213
	return wbc->wb_view ? wb_view_get_workbook (wbc->wb_view) : NULL;
214 215
}

216 217 218 219 220 221
/**
 * wb_control_cur_sheet:
 * @wbc: #WorkbookControl
 *
 * Returns: (transfer none): the current sheet.
 **/
222
Sheet *
223
wb_control_cur_sheet (WorkbookControl const *wbc)
224
{
Morten Welinder's avatar
Morten Welinder committed
225
	g_return_val_if_fail (GNM_IS_WBC (wbc), NULL);
226 227 228
	return wb_view_cur_sheet (wbc->wb_view);
}

229 230 231 232 233 234
/**
 * wb_control_cur_sheet_view:
 * @wbc: #WorkbookControl
 *
 * Returns: (transfer none): the current sheet view.
 **/
235 236 237
SheetView *
wb_control_cur_sheet_view (WorkbookControl const *wbc)
{
Morten Welinder's avatar
Morten Welinder committed
238
	g_return_val_if_fail (GNM_IS_WBC (wbc), NULL);
239 240 241
	return wb_view_cur_sheet_view (wbc->wb_view);
}

Morten Welinder's avatar
Morten Welinder committed
242 243
static void
wb_create_name (WorkbookControl *wbc, char const *text, GnmParsePos *pp)
244
{
Morten Welinder's avatar
Morten Welinder committed
245 246 247
	GnmRange const *r;
	GnmCellRef a, b;
	GnmExpr const *target_range;
Morten Welinder's avatar
Morten Welinder committed
248

Morten Welinder's avatar
Morten Welinder committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	r = selection_first_range (wb_control_cur_sheet_view (wbc),
		                   GO_CMD_CONTEXT (wbc), _("Define Name"));
	if (r != NULL) {
		a.sheet = b.sheet = wb_control_cur_sheet (wbc);
		a.col = r->start.col;
		a.row = r->start.row;
		b.col = r->end.col;
		b.row = r->end.row;
		a.col_relative = a.row_relative = b.col_relative = b.row_relative = FALSE;
		pp->sheet = NULL; /* make it a global name */
		if (gnm_cellref_equal (&a, &b))
			target_range = gnm_expr_new_cellref (&a);
		else
			target_range = gnm_expr_new_constant (
				value_new_cellrange_unsafe (&a, &b));
		cmd_define_name (wbc, text, pp, gnm_expr_top_new (target_range), NULL);
	}
}

/*
 * Select the given range and make the it visible.
 */
271
gboolean
272
wb_control_jump (WorkbookControl *wbc, Sheet *sheet, const GnmRangeRef *r)
Morten Welinder's avatar
Morten Welinder committed
273 274
{
	SheetView *sv;
Jody Goldberg's avatar
Jody Goldberg committed
275
	GnmCellPos tmp;
Morten Welinder's avatar
Morten Welinder committed
276

277 278
	if (r->a.sheet)
		sheet = r->a.sheet;
Morten Welinder's avatar
Morten Welinder committed
279

280 281 282 283 284 285
	if (!sheet_is_visible (sheet)) {
		go_cmd_context_error_invalid
			(GO_CMD_CONTEXT (wbc),
			 _("Cannot jump to an invisible sheet"),
			 sheet->name_unquoted);
		return FALSE;
Morten Welinder's avatar
Morten Welinder committed
286 287
	}

288 289 290 291 292
	sv = sheet_get_view (sheet, wb_control_view (wbc));

	tmp.col = r->a.col;
	tmp.row = r->a.row;
	sv_selection_set (sv, &tmp, r->a.col, r->a.row, r->b.col, r->b.row);
Morten Welinder's avatar
Morten Welinder committed
293 294 295
	gnm_sheet_view_make_cell_visible (sv, r->b.col, r->b.row, FALSE);
	gnm_sheet_view_make_cell_visible (sv, r->a.col, r->a.row, FALSE);
	gnm_sheet_view_update (sv);
Morten Welinder's avatar
Morten Welinder committed
296 297
	if (wb_control_cur_sheet (wbc) != sheet)
		wb_view_sheet_focus (wbc->wb_view, sheet);
298 299

	return TRUE;
Morten Welinder's avatar
Morten Welinder committed
300 301 302 303 304 305 306 307 308 309
}

/*
 * This is called when something is entered in the location entry.
 * We either go there (if the text refers to a cell by address or
 * name), or we try to define a name for the selection.
 */
gboolean
wb_control_parse_and_jump (WorkbookControl *wbc, char const *text)
{
Morten Welinder's avatar
Morten Welinder committed
310 311
	Sheet *sheet = wb_control_cur_sheet (wbc);
	GnmParsePos pp;
312
	GnmEvalPos ep;
Jody Goldberg's avatar
Jody Goldberg committed
313
	GnmValue *target;
314 315
	GnmRangeRef range;
	SheetView *sv;
316 317 318

	if (text == NULL || *text == '\0')
		return FALSE;
319

320 321
	sv = wb_control_cur_sheet_view (wbc);
	parse_pos_init_editpos (&pp, sv);
322 323
	target = value_new_cellrange_parsepos_str (&pp, text,
						   GNM_EXPR_PARSE_DEFAULT);
324 325
	if (target == NULL) {
		GnmExprTop const *texpr;
326

327 328
		texpr = gnm_expr_parse_str
			(text, &pp, GNM_EXPR_PARSE_DEFAULT,
329
			 gnm_conventions_xls_r1c1, NULL);
330 331 332 333
		if (texpr != NULL)  {
			target = gnm_expr_top_get_range (texpr);
			gnm_expr_top_unref (texpr);
		}
334 335 336
	}
	if (target == NULL) {
		GnmExprTop const *texpr;
337

338 339
		texpr = gnm_expr_parse_str
			(text, &pp, GNM_EXPR_PARSE_DEFAULT,
340 341 342 343 344
			 gnm_conventions_default, NULL);
		if (texpr != NULL)  {
			target = gnm_expr_top_get_range (texpr);
			gnm_expr_top_unref (texpr);
		}
345
	}
346

347
	if (target == NULL) {
Morten Welinder's avatar
Morten Welinder committed
348
		/* Not an address; is it a name? */
Morten Welinder's avatar
Morten Welinder committed
349
		GnmParsePos pp;
350
		GnmNamedExpr *nexpr = expr_name_lookup (
351
			parse_pos_init_sheet (&pp, sheet), text);
352

353
		/* If no name, or just a placeholder exists create a name */
354
		if (nexpr == NULL || expr_name_is_placeholder (nexpr)) {
Morten Welinder's avatar
Morten Welinder committed
355
			wb_create_name (wbc, text, &pp);
356 357
			return FALSE;
		} else {
358
			target = gnm_expr_top_get_range (nexpr->texpr);
359
			if (target == NULL) {
360 361
				go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc),
					_("Address"), text);
362 363 364
				return FALSE;
			}
		}
365 366
	}

367 368 369
	eval_pos_init_editpos (&ep, sv);
	gnm_cellref_make_abs (&range.a, &target->v_range.cell.a, &ep);
	gnm_cellref_make_abs (&range.b, &target->v_range.cell.b, &ep);
370
	value_release (target);
371 372

	return wb_control_jump (wbc, sheet, &range);
373 374
}

Morten Welinder's avatar
Morten Welinder committed
375
void
376 377 378 379 380 381 382 383 384 385 386
wb_control_navigate_to_cell (WorkbookControl *wbc, wb_control_navigator_t to)
{
	Sheet *sheet = wb_control_cur_sheet (wbc);
	SheetView *sv = wb_control_cur_sheet_view (wbc);
	GnmRange region;
	GnmRange const *first = selection_first_range (sv, NULL, NULL);
	GnmRangeRef rangeref;

	region = *first;
	gnm_sheet_guess_data_range (sheet, &region);
	range_ensure_sanity (&region, sheet);
Morten Welinder's avatar
Morten Welinder committed
387

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	switch (to) {
	case navigator_top:
		region.end.row = region.start.row;
		region.start.col = first->start.col;
		region.end.col = first->end.col;
		break;
	case navigator_bottom:
		region.start.row = region.end.row;
		region.start.col = first->start.col;
		region.end.col = first->end.col;
		break;
	case navigator_first:
		region.end.col = region.start.col;
		region.start.row = first->start.row;
		region.end.row = first->end.row;
		break;
	case navigator_last:
		region.start.col = region.end.col;
		region.start.row = first->start.row;
		region.end.row = first->end.row;
		break;
	default:
		break;
	}
	gnm_cellref_init (&rangeref.a, sheet,
			  region.start.col, region.start.row, FALSE);
	gnm_cellref_init (&rangeref.b, sheet,
			  region.end.col, region.end.row, FALSE);

	wb_control_jump (wbc, sheet, &rangeref);

	return;
}




425
static void
426
cb_wbc_clipboard_modified (G_GNUC_UNUSED GnmApp *app, WorkbookControl *wbc)
427 428 429 430
{
	wb_control_menu_state_update (wbc, MS_PASTE_SPECIAL);
}

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
static void
wbc_get_property (GObject     *object,
		  guint        property_id,
		  GValue      *value,
		  GParamSpec  *pspec)
{
	WorkbookControl *wbc = (WorkbookControl *)object;

	switch (property_id) {
	case PROP_VIEW:
		g_value_set_object (value, wbc->wb_view);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
wbc_set_property (GObject      *object,
		  guint         property_id,
		  GValue const *value,
		  GParamSpec   *pspec)
{
	WorkbookControl *wbc = (WorkbookControl *)object;

	switch (property_id) {
	case PROP_VIEW:
		wbc->wb_view = g_value_get_object (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

469
static GObjectClass *parent_klass;
470

Jody Goldberg's avatar
Jody Goldberg committed
471
static void
472
wbc_dispose (GObject *obj)
Jody Goldberg's avatar
Jody Goldberg committed
473
{
Morten Welinder's avatar
Morten Welinder committed
474
	WorkbookControl *wbc = GNM_WBC (obj);
475
	if (wbc->clipboard_changed_signal) {
Jody Goldberg's avatar
Jody Goldberg committed
476
		g_signal_handler_disconnect (gnm_app_get_app (),
477
					     wbc->clipboard_changed_signal);
478 479
		wbc->clipboard_changed_signal = 0;
	}
480

Jody Goldberg's avatar
Jody Goldberg committed
481 482
	if (wbc->wb_view != NULL)
		wb_view_detach_control (wbc);
483 484

	parent_klass->dispose (obj);
Jody Goldberg's avatar
Jody Goldberg committed
485 486 487
}

static void
Jody Goldberg's avatar
Jody Goldberg committed
488
workbook_control_class_init (GObjectClass *object_class)
Jody Goldberg's avatar
Jody Goldberg committed
489
{
490
	parent_klass = g_type_class_peek_parent (object_class);
491
	object_class->dispose = wbc_dispose;
492 493 494 495 496 497 498
	object_class->get_property = wbc_get_property;
	object_class->set_property = wbc_set_property;

	g_object_class_install_property
		(object_class,
		 PROP_VIEW,
		 g_param_spec_object ("view",
499 500
				      P_("View"),
				      P_("The workbook view being controlled."),
Morten Welinder's avatar
Morten Welinder committed
501
				      GNM_WORKBOOK_VIEW_TYPE,
502 503
				      GSF_PARAM_STATIC |
				      G_PARAM_READWRITE));
Jody Goldberg's avatar
Jody Goldberg committed
504 505
}

506 507 508
static void
workbook_control_init (GObject *obj)
{
Morten Welinder's avatar
Morten Welinder committed
509
	WorkbookControl *wbc = GNM_WBC (obj);
510 511

	wbc->clipboard_changed_signal = g_signal_connect (
Jody Goldberg's avatar
Jody Goldberg committed
512
		gnm_app_get_app (),
513 514 515 516
		"clipboard_modified",
		G_CALLBACK (cb_wbc_clipboard_modified), wbc);
}

517
static void
518
wbc_cmd_context_init (G_GNUC_UNUSED GOCmdContextClass *iface)
519 520 521 522 523 524
{
#if 0
	iface->get_password	    = ;
	iface->set_sensitive	    = ;
	iface->error.error	    = ;
	iface->error.error_info	    = ;
525
	iface->error.error_info_list= ;
526 527 528 529 530 531 532 533 534 535
	iface->progress_set	    = ;
	iface->progress_message_set = ;
#endif
}

GSF_CLASS_FULL (WorkbookControl, workbook_control, NULL, NULL,
		workbook_control_class_init, NULL, workbook_control_init,
		GO_TYPE_DOC_CONTROL, 0,
		GSF_INTERFACE (wbc_cmd_context_init, GO_TYPE_CMD_CONTEXT))

536 537

void
538
wb_control_set_view (WorkbookControl *wbc,
539
		     WorkbookView *opt_view, Workbook *opt_wb)
540
{
541 542
	WorkbookView *wbv;

Morten Welinder's avatar
Morten Welinder committed
543
	g_return_if_fail (GNM_IS_WBC (wbc));
544 545
	g_return_if_fail (wbc->wb_view == NULL);

546 547
	wbv = (opt_view != NULL) ? opt_view : workbook_view_new (opt_wb);
	wb_view_attach_control (wbv, wbc);
548
	go_doc_control_set_doc (GO_DOC_CONTROL (wbc), GO_DOC (wb_view_get_workbook (wbv)));
Jody Goldberg's avatar
Jody Goldberg committed
549 550 551
}

void
552
wb_control_init_state (WorkbookControl *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
553
{
554
	WorkbookView *wbv;
Jody Goldberg's avatar
Jody Goldberg committed
555 556
	WorkbookControlClass *wbc_class;

Morten Welinder's avatar
Morten Welinder committed
557
	g_return_if_fail (GNM_IS_WBC (wbc));
Jody Goldberg's avatar
Jody Goldberg committed
558 559 560

	/* Setup the undo/redo combos */
	command_setup_combos (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
561

562
	/* Add views for all existing sheets */
563
	wbv = wb_control_view (wbc);
Morten Welinder's avatar
Morten Welinder committed
564
	WORKBOOK_FOREACH_SHEET(wb_control_get_workbook (wbc), sheet, {
565 566 567 568
		SHEET_FOREACH_VIEW (sheet, view, {
			if (sv_wbv (view) == wbv)
				wb_control_sheet_add (wbc, view);
		});
Morten Welinder's avatar
Morten Welinder committed
569
	});
Jody Goldberg's avatar
Jody Goldberg committed
570 571

	wbc_class = WBC_CLASS (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
572
	if (wbc_class != NULL && wbc_class->init_state != NULL)
Jody Goldberg's avatar
Jody Goldberg committed
573
		wbc_class->init_state (wbc);
574
}