commands.c 207 KB
Newer Older
1

Jody Goldberg's avatar
Jody Goldberg committed
2
/*
3 4
 * commands.c: Handlers to undo & redo commands
 *
Jody Goldberg's avatar
Jody Goldberg committed
5 6
 * Copyright (C) 1999-2008 Jody Goldberg (jody@gnome.org)
 * Copyright (C) 2002-2008 Morten Welinder (terra@gnome.org)
Jody Goldberg's avatar
Jody Goldberg committed
7
 *
8
 * Contributors : Almer S. Tigelaar (almer@gnome.org)
9
 *                Andreas J. Guelzow (aguelzow@taliesin.ca)
10
 *
11 12 13 14
 * 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.
Jody Goldberg's avatar
Jody Goldberg committed
15
 *
16 17 18 19 20 21 22
 * 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
23
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
24
 * USA
Jody Goldberg's avatar
Jody Goldberg committed
25
 */
26
#include <gnumeric-config.h>
27
#include <glib/gi18n-lib.h>
28 29 30
#include <gnumeric.h>
#include <commands.h>
#include <gnm-command-impl.h>
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
#include <application.h>
#include <sheet.h>
#include <sheet-view.h>
#include <sheet-style.h>
#include <gnm-format.h>
#include <format-template.h>
#include <command-context.h>
#include <workbook-control.h>
#include <workbook-view.h>
#include <workbook-priv.h> /* For the undo/redo queues and the FOREACH */
#include <ranges.h>
#include <sort.h>
#include <dependent.h>
#include <value.h>
#include <expr.h>
#include <func.h>
#include <expr-name.h>
#include <cell.h>
#include <sheet-merge.h>
#include <parse-util.h>
#include <print-info.h>
#include <clipboard.h>
#include <selection.h>
#include <colrow.h>
#include <style-border.h>
#include <tools/auto-correct.h>
#include <sheet-autofill.h>
#include <mstyle.h>
#include <search.h>
#include <gutils.h>
#include <gui-util.h>
#include <sheet-object-cell-comment.h>
#include <sheet-object-widget.h>
#include <sheet-object.h>
#include <sheet-object-component.h>
#include <sheet-object-graph.h>
#include <sheet-control.h>
#include <sheet-control-gui.h>
#include <sheet-utils.h>
#include <style-color.h>
#include <sheet-filter.h>
#include <auto-format.h>
#include <tools/dao.h>
#include <gnumeric-conf.h>
#include <tools/scenarios.h>
#include <tools/data-shuffling.h>
#include <tools/tabulate.h>
#include <wbc-gtk.h>
#include <undo.h>
Jody Goldberg's avatar
Jody Goldberg committed
81

82
#include <goffice/goffice.h>
Jody Goldberg's avatar
Jody Goldberg committed
83 84
#include <gsf/gsf-doc-meta-data.h>
#include <string.h>
85

86 87 88
#define UNICODE_ELLIPSIS "\xe2\x80\xa6"


Jody Goldberg's avatar
Jody Goldberg committed
89
/*
90
 * There are several distinct stages to wrapping each command.
91
 *
Jody Goldberg's avatar
Jody Goldberg committed
92 93 94 95 96 97 98 99 100 101
 * 1) Find the appropriate place(s) in the catch all calls to activations
 * of this logical function.  Be careful.  This should only be called by
 * user initiated actions, not internal commands.
 *
 * 2) Copy the boiler plate code into place and implement the descriptor.
 *
 * 3) Implement the guts of the support functions.
 *
 * That way undo redo just become applications of the old or the new styles.
 *
Morten Welinder's avatar
Morten Welinder committed
102
 * Design thoughts:
103 104 105 106 107 108 109
 * 1) redo : this should be renamed 'exec' and should be the place that the
 *    the actual command executes.  This avoid duplicating the code for
 *    application and re-application.
 *
 * 2) The command objects are responsible for generating recalc and redraw
 *    events.  None of the internal utility routines should do so.  Those are
 *    expensive events and should only be done once per command to avoid
110
 *    duplicating work.  The lower levels can queue redraws if they must, and
111
 *    flag state changes but the call to gnm_app_recalc and sheet_update is
112
 *    by GnmCommand.
113
 *
Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
114
 * FIXME: Filter the list of commands when a sheet is deleted.
115
 *
Jody Goldberg's avatar
Jody Goldberg committed
116 117 118
 * TODO : Possibly clear lists on save.
 *
 * TODO : Reqs for selective undo
119 120 121
 *
 * Future thoughts
 * - undoable preference setting ?  XL does not have this.  Do we want it ?
Jody Goldberg's avatar
Jody Goldberg committed
122 123 124
 */
/******************************************************************/

125 126
#define GNM_COMMAND(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), GNM_COMMAND_TYPE, GnmCommand))
#define GNM_COMMAND_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST ((k), GNM_COMMAND_TYPE, GnmCommandClass))
Morten Welinder's avatar
Morten Welinder committed
127
#define GNM_IS_COMMAND(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), GNM_COMMAND_TYPE))
128
#define CMD_CLASS(o)		GNM_COMMAND_CLASS (G_OBJECT_GET_CLASS(cmd))
Jody Goldberg's avatar
Jody Goldberg committed
129

Jody Goldberg's avatar
Jody Goldberg committed
130
GSF_CLASS (GnmCommand, gnm_command, NULL, NULL, G_TYPE_OBJECT)
Jody Goldberg's avatar
Jody Goldberg committed
131

Jody Goldberg's avatar
Jody Goldberg committed
132
void
133
gnm_command_finalize (GObject *obj)
Jody Goldberg's avatar
Jody Goldberg committed
134
{
135
	GnmCommand *cmd = GNM_COMMAND (obj);
Jody Goldberg's avatar
Jody Goldberg committed
136
	GObjectClass *parent;
Jody Goldberg's avatar
Jody Goldberg committed
137 138 139

	/* The const was to avoid accidental changes elsewhere */
	g_free ((gchar *)cmd->cmd_descriptor);
Jody Goldberg's avatar
Jody Goldberg committed
140
	cmd->cmd_descriptor = NULL;
141

Jody Goldberg's avatar
Jody Goldberg committed
142 143 144
	parent = g_type_class_peek (g_type_parent(G_TYPE_FROM_INSTANCE (obj)));
	(*parent->finalize) (obj);
}
Jody Goldberg's avatar
Jody Goldberg committed
145 146 147

/******************************************************************/

148 149
GString *
gnm_cmd_trunc_descriptor (GString *src, gboolean *truncated)
150
{
151
	int max_len = gnm_conf_get_undo_max_descriptor_width ();
152 153 154 155 156 157 158 159 160
	glong len;
	char *pos;

	if (max_len < 5)
		max_len = 5;

	while ((pos = strchr(src->str, '\n')) != NULL ||
	       (pos = strchr(src->str, '\r')) != NULL)
		*pos = ' ';
Morten Welinder's avatar
Morten Welinder committed
161

162
	len = g_utf8_strlen (src->str, -1);
163 164 165

	if (truncated)
		*truncated = (len > max_len);
Morten Welinder's avatar
Morten Welinder committed
166

167 168 169 170 171
	if (len > max_len) {
		gchar* last = g_utf8_offset_to_pointer (src->str,
                                                        max_len - 1);
		g_string_truncate (src, last - src->str);
		g_string_append (src, UNICODE_ELLIPSIS);
172
	}
173
	return src;
174 175 176
}


177
/**
178 179 180 181 182 183
 * cmd_cell_range_is_locked_effective:
 * @sheet: #Sheet
 * @range: #GnmRange
 * @wbc: #WorkbookControl
 * @cmd_name: the command name.
 *
Morten Welinder's avatar
Morten Welinder committed
184
 * checks whether the cells are effectively locked
185 186 187 188
 *
 * static gboolean cmd_cell_range_is_locked_effective
 *
 *
189
 * Do not use this function unless the sheet is part of the
Jody Goldberg's avatar
new.  
Jody Goldberg committed
190
 * workbook with the given wbc (otherwise the results may be strange)
Morten Welinder's avatar
Morten Welinder committed
191 192
 *
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
193
 */
194
gboolean
Jody Goldberg's avatar
Jody Goldberg committed
195
cmd_cell_range_is_locked_effective (Sheet *sheet, GnmRange *range,
196
				    WorkbookControl *wbc, char const *cmd_name)
197 198
{
	int i, j;
199
	WorkbookView *wbv = wb_control_view (wbc);
200 201

	if (wbv->is_protected || sheet->is_protected)
202 203
		for (i = range->start.row; i <= range->end.row; i++)
			for (j = range->start.col; j <= range->end.col; j++)
204
				if (gnm_style_get_contents_locked (sheet_style_get (sheet, j, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
205
					char *r = global_range_name (sheet, range);
206 207 208
					char *text = g_strdup_printf (wbv->is_protected  ?
						_("%s is locked. Unprotect the workbook to enable editing.") :
						_("%s is locked. Unprotect the sheet to enable editing."),
Jody Goldberg's avatar
Jody Goldberg committed
209
						r);
210 211
					go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc),
						cmd_name, text);
212
					g_free (text);
Jody Goldberg's avatar
Jody Goldberg committed
213
					g_free (r);
214 215 216 217 218
					return TRUE;
				}
	return FALSE;
}

219
/*
Morten Welinder's avatar
Morten Welinder committed
220
 * checks whether the cells are effectively locked
221 222 223 224
 *
 * static gboolean cmd_dao_is_locked_effective
 *
 *
225 226
 * Do not use this function unless the sheet is part of the
 * workbook with the given wbcg (otherwise the results may be strange)
227
 *
Morten Welinder's avatar
Morten Welinder committed
228
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
229
 */
230
static gboolean
231 232 233
cmd_dao_is_locked_effective (data_analysis_output_t  *dao,
			     WorkbookControl *wbc, char const *cmd_name)
{
Jody Goldberg's avatar
Jody Goldberg committed
234
	GnmRange range;
235 236
	range_init (&range, dao->start_col, dao->start_row,
		    dao->start_col +  dao->cols - 1,  dao->start_row +  dao->rows - 1);
237
	return (dao->type != NewWorkbookOutput &&
238 239 240
		cmd_cell_range_is_locked_effective (dao->sheet, &range, wbc, cmd_name));
}

241
/**
242
 * cmd_selection_is_locked_effective: (skip)
243 244 245 246
 * checks whether the selection is effectively locked
 *
 * static gboolean cmd_selection_is_locked_effective
 *
247 248
 * Do not use this function unless the sheet is part of the
 * workbook with the given wbcg (otherwise the results may be strange)
249
 *
Morten Welinder's avatar
Morten Welinder committed
250
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
251
 */
252
gboolean
253 254
cmd_selection_is_locked_effective (Sheet *sheet, GSList *selection,
				   WorkbookControl *wbc, char const *cmd_name)
255 256
{
	for (; selection; selection = selection->next) {
Morten Welinder's avatar
Morten Welinder committed
257
		GnmRange *range = selection->data;
258
		if (cmd_cell_range_is_locked_effective (sheet, range, wbc, cmd_name))
259 260 261 262 263
			return TRUE;
	}
	return FALSE;
}

264 265 266 267 268 269 270
/*
 * A helper routine to select a range and make sure the top-left
 * is visible.
 */
static void
select_range (Sheet *sheet, const GnmRange *r, WorkbookControl *wbc)
{
271 272 273 274 275 276 277 278 279
	SheetView *sv;

	if (sheet->workbook != wb_control_get_workbook (wbc)) {
		/*
		 * We could try to pick a random wbc for the sheet's
		 * workbook.  But not right now.
		 */
		return;
	}
280 281

	wb_control_sheet_focus (wbc, sheet);
282
	sv = sheet_get_view (sheet, wb_control_view (wbc));
283
	sv_selection_reset (sv);
284
	sv_selection_add_range (sv, r);
Morten Welinder's avatar
Morten Welinder committed
285
	gnm_sheet_view_make_cell_visible (sv, r->start.col, r->start.row, FALSE);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}

/*
 * A helper routine to select a list of ranges and make sure the top-left
 * corner of the last is visible.
 */
static void
select_selection (Sheet *sheet, GSList *selection, WorkbookControl *wbc)
{
	SheetView *sv = sheet_get_view (sheet, wb_control_view (wbc));
	const GnmRange *r0 = NULL;
	GSList *l;

	g_return_if_fail (selection != NULL);

	wb_control_sheet_focus (wbc, sheet);
	sv_selection_reset (sv);
	for (l = selection; l; l = l->next) {
304 305
		GnmRange const *r = l->data;
		sv_selection_add_range (sv, r);
306 307
		r0 = r;
	}
Morten Welinder's avatar
Morten Welinder committed
308
	gnm_sheet_view_make_cell_visible (sv, r0->start.col, r0->start.row, FALSE);
309 310
}

Jody Goldberg's avatar
Jody Goldberg committed
311
/**
312
 * get_menu_label:
Jody Goldberg's avatar
Jody Goldberg committed
313
 *     with a list of commands.
314
 * @cmd_list: The command list to check.
Jody Goldberg's avatar
Jody Goldberg committed
315
 *
316
 * Utility routine to get the descriptor associated
Jody Goldberg's avatar
Jody Goldberg committed
317 318
 * Returns : A static reference to a descriptor.  DO NOT free this.
 */
319
static char const *
Jody Goldberg's avatar
Jody Goldberg committed
320 321 322
get_menu_label (GSList *cmd_list)
{
	if (cmd_list != NULL) {
323
		GnmCommand *cmd = GNM_COMMAND (cmd_list->data);
Jody Goldberg's avatar
Jody Goldberg committed
324 325 326 327 328 329 330
		return cmd->cmd_descriptor;
	}

	return NULL;
}

/**
331 332
 * undo_redo_menu_labels:
 * @wb: The book whose undo/redo queues we are modifying
Jody Goldberg's avatar
Jody Goldberg committed
333
 *
334
 * Another utility to set the menus correctly.
Jody Goldberg's avatar
Jody Goldberg committed
335 336 337 338
 */
static void
undo_redo_menu_labels (Workbook *wb)
{
339 340 341 342 343 344
	char const *undo_label = get_menu_label (wb->undo_commands);
	char const *redo_label = get_menu_label (wb->redo_commands);

	WORKBOOK_FOREACH_CONTROL (wb, view, control,
		wb_control_undo_redo_labels (control, undo_label, redo_label);
	);
Jody Goldberg's avatar
Jody Goldberg committed
345 346
}

Jody Goldberg's avatar
Jody Goldberg committed
347
static void
348
update_after_action (Sheet *sheet, WorkbookControl *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
349
{
350 351
	gnm_app_recalc ();

Jody Goldberg's avatar
Jody Goldberg committed
352 353 354
	if (sheet != NULL) {
		g_return_if_fail (IS_SHEET (sheet));

355
		sheet_mark_dirty (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
356
		sheet_update (sheet);
357

358
		if (sheet->workbook == wb_control_get_workbook (wbc))
359 360
			WORKBOOK_VIEW_FOREACH_CONTROL (wb_control_view (wbc), control,
				  wb_control_sheet_focus (control, sheet););
361 362 363 364 365
	} else if (wbc != NULL) {
		Sheet *sheet = wb_control_cur_sheet (wbc);
		if (sheet)
			sheet_update (sheet);
	}
Jody Goldberg's avatar
Jody Goldberg committed
366 367
}

Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
368

369
/**
370 371
 * command_undo:
 * @wbc: The workbook control which issued the request.
372 373
 *        Any user level errors generated by undoing will be reported
 *        here.
Jody Goldberg's avatar
Jody Goldberg committed
374
 *
375
 * Undo the last command executed.
376
 **/
Jody Goldberg's avatar
Jody Goldberg committed
377
void
378
command_undo (WorkbookControl *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
379
{
380 381
	GnmCommand *cmd;
	GnmCommandClass *klass;
382
	Workbook *wb = wb_control_get_workbook (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
383 384 385 386

	g_return_if_fail (wb != NULL);
	g_return_if_fail (wb->undo_commands != NULL);

387
	cmd = GNM_COMMAND (wb->undo_commands->data);
Jody Goldberg's avatar
Jody Goldberg committed
388 389
	g_return_if_fail (cmd != NULL);

390
	klass = CMD_CLASS (cmd);
391 392
	g_return_if_fail (klass != NULL);

393 394
	g_object_ref (cmd);

395
	/* TRUE indicates a failure to undo.  Leave the command where it is */
396 397
	if (!klass->undo_cmd (cmd, wbc)) {
		gboolean undo_cleared;
398

399
		update_after_action (cmd->sheet, wbc);
400

401
		if (!cmd->workbook_modified_before_do)
402
			go_doc_set_dirty (GO_DOC (wb), FALSE);
403

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
		/*
		 * A few commands clear the undo queue.  For those, we do not
		 * want to stuff the cmd object on the redo queue.
		 */
		undo_cleared = (wb->undo_commands == NULL);

		if (!undo_cleared) {
			wb->undo_commands = g_slist_remove (wb->undo_commands, cmd);
			wb->redo_commands = g_slist_prepend (wb->redo_commands, cmd);

			WORKBOOK_FOREACH_CONTROL (wb, view, control, {
				wb_control_undo_redo_pop (control, TRUE);
				wb_control_undo_redo_push (control, FALSE, cmd->cmd_descriptor, cmd);
			});
			undo_redo_menu_labels (wb);
			/* TODO : Should we mark the workbook as clean or pristine too */
		}
	}

	g_object_unref (cmd);
Jody Goldberg's avatar
Jody Goldberg committed
424 425
}

426
/**
427
 * command_redo:
428
 * @wbc: The workbook control which issued the request.
429 430
 *
 *  Redo the last command that was undone.
431 432
 *        Any user level errors generated by redoing will be reported
 *        here.
433
 **/
Jody Goldberg's avatar
Jody Goldberg committed
434
void
435
command_redo (WorkbookControl *wbc)
Jody Goldberg's avatar
Jody Goldberg committed
436
{
437 438
	GnmCommand *cmd;
	GnmCommandClass *klass;
439
	Workbook *wb = wb_control_get_workbook (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
440 441 442 443

	g_return_if_fail (wb);
	g_return_if_fail (wb->redo_commands);

444
	cmd = GNM_COMMAND (wb->redo_commands->data);
Jody Goldberg's avatar
Jody Goldberg committed
445 446
	g_return_if_fail (cmd != NULL);

447
	klass = CMD_CLASS (cmd);
448 449
	g_return_if_fail (klass != NULL);

450 451
	g_object_ref (cmd);

452
	cmd->workbook_modified_before_do =
453
		go_doc_is_dirty (wb_control_get_doc (wbc));
454

455
	/* TRUE indicates a failure to redo.  Leave the command where it is */
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	if (!klass->redo_cmd (cmd, wbc)) {
		gboolean redo_cleared;

		update_after_action (cmd->sheet, wbc);

		/*
		 * A few commands clear the undo queue.  For those, we do not
		 * want to stuff the cmd object on the redo queue.
		 */
		redo_cleared = (wb->redo_commands == NULL);

		if (!redo_cleared) {
			wb->redo_commands = g_slist_remove (wb->redo_commands, cmd);
			wb->undo_commands = g_slist_prepend (wb->undo_commands, cmd);

			WORKBOOK_FOREACH_CONTROL (wb, view, control, {
				wb_control_undo_redo_push (control, TRUE, cmd->cmd_descriptor, cmd);
				wb_control_undo_redo_pop (control, FALSE);
			});
			undo_redo_menu_labels (wb);
		}
	}

	g_object_unref (cmd);
Jody Goldberg's avatar
Jody Goldberg committed
480 481
}

482
/**
483
 * command_repeat:
484
 * @wbc: The workbook control which issued the request.
485 486 487
 *
 *  Repeat the last command (if possible)
 *
488 489 490 491 492 493 494 495
 *        Any user level errors generated by redoing will be reported
 *        here.
 **/
void
command_repeat (WorkbookControl *wbc)
{
	GnmCommand *cmd;
	GnmCommandClass *klass;
496
	Workbook *wb = wb_control_get_workbook (wbc);
497 498 499 500 501 502 503 504 505 506 507 508 509 510

	g_return_if_fail (wb);
	g_return_if_fail (wb->undo_commands);

	cmd = GNM_COMMAND (wb->undo_commands->data);
	g_return_if_fail (cmd != NULL);

	klass = CMD_CLASS (cmd);
	g_return_if_fail (klass != NULL);

	if (klass->repeat_cmd != NULL)
		(*klass->repeat_cmd) (cmd, wbc);
}

Jody Goldberg's avatar
Jody Goldberg committed
511
/**
Morten Welinder's avatar
Morten Welinder committed
512
 * command_setup_combos:
513
 * @wbc:
Morten Welinder's avatar
Morten Welinder committed
514
 *
Jody Goldberg's avatar
Jody Goldberg committed
515 516 517 518 519 520
 * Initialize the combos to correspond to the current undo/redo state.
 */
void
command_setup_combos (WorkbookControl *wbc)
{
	char const *undo_label = NULL, *redo_label = NULL;
521
	GSList *ptr, *tmp;
522
	Workbook *wb = wb_control_get_workbook (wbc);
Jody Goldberg's avatar
Jody Goldberg committed
523 524 525

	g_return_if_fail (wb);

526
	wb_control_undo_redo_truncate (wbc, 0, TRUE);
Jody Goldberg's avatar
Jody Goldberg committed
527 528 529
	tmp = g_slist_reverse (wb->undo_commands);
	for (ptr = tmp ; ptr != NULL ; ptr = ptr->next) {
		undo_label = get_menu_label (ptr);
Jody Goldberg's avatar
Jody Goldberg committed
530
		wb_control_undo_redo_push (wbc, TRUE, undo_label, ptr->data);
Jody Goldberg's avatar
Jody Goldberg committed
531
	}
532
	if (g_slist_reverse (tmp)) {}	/* ignore, list is in undo_commands */
Jody Goldberg's avatar
Jody Goldberg committed
533

534
	wb_control_undo_redo_truncate (wbc, 0, FALSE);
Jody Goldberg's avatar
Jody Goldberg committed
535 536 537
	tmp = g_slist_reverse (wb->redo_commands);
	for (ptr = tmp ; ptr != NULL ; ptr = ptr->next) {
		redo_label = get_menu_label (ptr);
Jody Goldberg's avatar
Jody Goldberg committed
538
		wb_control_undo_redo_push (wbc, FALSE, redo_label, ptr->data);
Jody Goldberg's avatar
Jody Goldberg committed
539
	}
540
	if (g_slist_reverse (tmp)) {}	/* ignore, list is in redo_commands */
Jody Goldberg's avatar
Jody Goldberg committed
541 542 543 544 545

	/* update the menus too */
	wb_control_undo_redo_labels (wbc, undo_label, redo_label);
}

546 547 548 549
/**
 * command_list_release:
 * @cmds: (element-type GObject): the set of commands to free.
 *
Jody Goldberg's avatar
Jody Goldberg committed
550 551 552
 * command_list_release : utility routine to free the resources associated
 *    with a list of commands.
 *
553
 * NOTE : remember to NULL the list when you are done.
Jody Goldberg's avatar
Jody Goldberg committed
554 555 556 557 558
 */
void
command_list_release (GSList *cmd_list)
{
	while (cmd_list != NULL) {
Jody Goldberg's avatar
Jody Goldberg committed
559
		GObject *cmd = G_OBJECT (cmd_list->data);
560 561 562

		g_return_if_fail (cmd != NULL);

Jody Goldberg's avatar
Jody Goldberg committed
563
		g_object_unref (cmd);
Jody Goldberg's avatar
Jody Goldberg committed
564 565 566 567
		cmd_list = g_slist_remove (cmd_list, cmd_list->data);
	}
}

568 569 570 571 572 573 574 575 576
/*
 * Each undo item has a certain size.  The size of typing a value into
 * a cell is the unit size.  A large autoformat could have a size of
 * hundreds or even thousands.
 *
 * We wish to have the same undo behaviour across platforms, so please
 * don't use sizeof in computing the undo size.
 */

Morten Welinder's avatar
Morten Welinder committed
577 578
#undef DEBUG_TRUNCATE_UNDO

579 580 581 582 583 584 585
/*
 * Truncate the undo list if it is too big.
 *
 * Returns -1 if no truncation was done, or else the number of elements
 * left.
 */
static int
586
truncate_undo_info (Workbook *wb)
587
{
588
	int size_left;
589
	int max_num;
590 591
	int ok_count;
	GSList *l, *prev;
592

593 594
	size_left = gnm_conf_get_undo_size ();
	max_num   = gnm_conf_get_undo_maxnum ();
595

Morten Welinder's avatar
Morten Welinder committed
596
#ifdef DEBUG_TRUNCATE_UNDO
597
	g_printerr ("Undo sizes:");
Morten Welinder's avatar
Morten Welinder committed
598 599
#endif

600 601 602 603
	for (l = wb->undo_commands, prev = NULL, ok_count = 0;
	     l;
	     prev = l, l = l->next, ok_count++) {
		int min_leave;
604
		GnmCommand *cmd = GNM_COMMAND (l->data);
605 606 607 608 609 610 611 612 613 614 615
		int size = cmd->size;

		if (size < 1) {
				/*
				 * We could g_assert, but that would cause data loss.
				 * Instead, just continue.
				 */
			g_warning ("Faulty undo_size_func, please report.");
			size = 1;
		}

Morten Welinder's avatar
Morten Welinder committed
616
#ifdef DEBUG_TRUNCATE_UNDO
617
			g_printerr (" %d", size);
Morten Welinder's avatar
Morten Welinder committed
618 619
#endif

620
		/* Keep at least one undo item.  */
621
		if (ok_count >= max_num || (size > size_left && ok_count >= 1)) {
622 623
			/* Current item is too big; truncate list here.  */
			command_list_release (l);
624 625 626 627
			if (prev)
				prev->next = NULL;
			else
				wb->undo_commands = NULL;
Morten Welinder's avatar
Morten Welinder committed
628
#ifdef DEBUG_TRUNCATE_UNDO
629
			g_printerr ("[trunc]\n");
Morten Welinder's avatar
Morten Welinder committed
630
#endif
631 632 633 634 635 636 637 638 639 640 641
			return ok_count;
		}

		/*
		 * In order to allow a series of useful small items behind
		 * a big item, leave at least 10% of current item's size.
		 */
		min_leave = size / 10;
		size_left = MAX (size_left - size, min_leave);
	}

Morten Welinder's avatar
Morten Welinder committed
642
#ifdef DEBUG_TRUNCATE_UNDO
643
	g_printerr ("\n");
Morten Welinder's avatar
Morten Welinder committed
644
#endif
645 646 647 648
	return -1;
}


649
/**
650
 * command_register_undo:
651 652
 * @wbc: The workbook control that issued the command.
 * @cmd: The new command to add.
653 654 655
 *
 * An internal utility to tack a new command
 *    onto the undo list.
656 657
 */
static void
Jody Goldberg's avatar
Jody Goldberg committed
658
command_register_undo (WorkbookControl *wbc, GObject *obj)
659 660
{
	Workbook *wb;
661
	GnmCommand *cmd;
662 663 664
	int undo_trunc;

	g_return_if_fail (wbc != NULL);
665
	wb = wb_control_get_workbook (wbc);
666

667
	cmd = GNM_COMMAND (obj);
668 669 670 671 672
	g_return_if_fail (cmd != NULL);

	command_list_release (wb->redo_commands);
	wb->redo_commands = NULL;

673
	g_object_ref (obj); /* keep a ref in case it gets truncated away */
674 675 676
	wb->undo_commands = g_slist_prepend (wb->undo_commands, cmd);
	undo_trunc = truncate_undo_info (wb);

677
	WORKBOOK_FOREACH_CONTROL (wb, view, control, {
Jody Goldberg's avatar
Jody Goldberg committed
678
		wb_control_undo_redo_push (control, TRUE, cmd->cmd_descriptor, cmd);
679
		if (undo_trunc >= 0)
680 681
			wb_control_undo_redo_truncate (control, undo_trunc, TRUE);
		wb_control_undo_redo_truncate (control, 0, FALSE);
682 683
	});
	undo_redo_menu_labels (wb);
684
	g_object_unref (obj);
685 686 687
}


Jody Goldberg's avatar
Jody Goldberg committed
688
/**
689
 * gnm_command_push_undo:
690 691
 * @wbc: The workbook control that issued the command.
 * @obj: The new command to add.
692
 *
693 694 695
 *  An internal utility to tack a new command
 *    onto the undo list.
 *
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
 */


/*
 * cmd_set_text_full
 *
 * the caller is expected to have ensured:
 *
 * 1) that no array is being split
 * 2) that the range is not locked.
 *
 * Note:
 * We will free the selection but nothing else.
 *
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
Jody Goldberg's avatar
Jody Goldberg committed
712
 */
Jody Goldberg's avatar
Jody Goldberg committed
713 714
gboolean
gnm_command_push_undo (WorkbookControl *wbc, GObject *obj)
Jody Goldberg's avatar
Jody Goldberg committed
715
{
716
	gboolean trouble;
717 718
	GnmCommand *cmd;
	GnmCommandClass *klass;
719

720
	g_return_val_if_fail (wbc != NULL, TRUE);
721

722
	cmd = GNM_COMMAND (obj);
723
	cmd->workbook_modified_before_do =
724
		go_doc_is_dirty (wb_control_get_doc (wbc));
725

726 727
	g_return_val_if_fail (cmd != NULL, TRUE);

728
	klass = CMD_CLASS (cmd);
729 730 731
	g_return_val_if_fail (klass != NULL, TRUE);

	/* TRUE indicates a failure to do the command */
732
	trouble = klass->redo_cmd (cmd, wbc);
733
	update_after_action (cmd->sheet, wbc);
Jody Goldberg's avatar
Jody Goldberg committed
734

735 736 737
	if (!trouble)
		command_register_undo (wbc, obj);
	else
Jody Goldberg's avatar
Jody Goldberg committed
738
		g_object_unref (obj);
739 740

	return trouble;
Jody Goldberg's avatar
Jody Goldberg committed
741 742
}

743 744 745 746 747 748 749 750 751 752 753 754 755
/*
 * command_undo_sheet_delete deletes the sheet without deleting the current cmd.
 * returns true if is indeed deleted the sheet.
 * Note: only call this for a sheet of your current workbook from the undo procedure
 */

static gboolean
command_undo_sheet_delete (Sheet* sheet)
{
	Workbook *wb = sheet->workbook;

        g_return_val_if_fail (IS_SHEET (sheet), FALSE);

Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
756 757 758 759
	if (wb->redo_commands != NULL) {
		command_list_release (wb->redo_commands);
		wb->redo_commands = NULL;
		WORKBOOK_FOREACH_CONTROL (wb, view, ctl,
760
			wb_control_undo_redo_truncate (ctl, 0, FALSE););
Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
761 762 763
		undo_redo_menu_labels (wb);
	}

764
	workbook_sheet_delete (sheet);
Andreas J. Guelzow's avatar
Andreas J. Guelzow committed
765 766 767 768

	return (TRUE);
}

769 770
/******************************************************************/

771 772 773
static GnmValue *
cmd_set_text_full_check_texpr (GnmCellIter const *iter, GnmExprTop const  *texpr)
{
Morten Welinder's avatar
Morten Welinder committed
774
	if (iter->cell == NULL ||
775 776 777 778
	    !gnm_expr_top_equal (iter->cell->base.texpr, texpr))
		return VALUE_TERMINATE;
	return NULL;
}
779

780 781 782 783 784
static GnmValue *
cmd_set_text_full_check_text (GnmCellIter const *iter, char *text)
{
	char *old_text;
	gboolean same;
785
	gboolean quoted = FALSE;
786 787 788 789 790 791 792

	if (gnm_cell_is_blank (iter->cell))
		return ((text == NULL || text[0] == '\0') ? NULL : VALUE_TERMINATE);

	if (text == NULL || text[0] == '\0')
		return VALUE_TERMINATE;

Morten Welinder's avatar
Morten Welinder committed
793
	old_text = gnm_cell_get_text_for_editing (iter->cell, NULL, &quoted);
794
	same = g_strcmp0 (old_text, text) == 0;
795

796
	if (!same && !quoted && iter->cell->value && VALUE_IS_STRING (iter->cell->value)
797
	    && text[0] == '\'')
798
		same = g_strcmp0 (old_text, text + 1) == 0;
Morten Welinder's avatar
Morten Welinder committed
799

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	g_free (old_text);

	return (same ? NULL : VALUE_TERMINATE);
}

static GnmValue *
cmd_set_text_full_check_markup (GnmCellIter const *iter, PangoAttrList *markup)
{
	PangoAttrList const *old_markup = NULL;
	gboolean same_markup;

	g_return_val_if_fail (iter->cell != NULL, NULL);

	if (iter->cell->value && VALUE_IS_STRING (iter->cell->value)) {
		const GOFormat *fmt = VALUE_FMT (iter->cell->value);
		if (fmt && go_format_is_markup (fmt)) {
			old_markup = go_format_get_markup (fmt);
			if (go_pango_attr_list_is_empty (old_markup))
				old_markup = NULL;
		}
	}

	same_markup = gnm_pango_attr_list_equal (old_markup, markup);

	return same_markup ? NULL : VALUE_TERMINATE;
}

827
/*
828
 * cmd_set_text_full
829 830 831 832 833 834
 *
 * the caller is expected to have ensured:
 *
 * 1) that no array is being split
 * 2) that the range is not locked.
 *
835 836 837
 * Note:
 * We will free the selection but nothing else.
 *
Morten Welinder's avatar
Morten Welinder committed
838
 * Returns: %TRUE if there was a problem, %FALSE otherwise.
839
 */
840 841
static gboolean
cmd_set_text_full (WorkbookControl *wbc, GSList *selection, GnmEvalPos *ep,
Morten Welinder's avatar
Morten Welinder committed
842
		   char const *new_text, PangoAttrList *markup,
843
		   gboolean autocorrect)
Jody Goldberg's avatar
Jody Goldberg committed
844
{
845
	GSList	*l;
846 847 848 849
	char const *expr_txt;
	GnmExprTop const  *texpr = NULL;
	GOUndo *undo = NULL;
	GOUndo *redo = NULL;
850
	gboolean result, autofit_col = FALSE, same_text_and_not_same_markup = FALSE;
851
	char *text = NULL;
852
	char *name;
853 854 855
	Sheet *sheet = ep->sheet;
	GnmParsePos pp;
	ColRowIndexList *cri_col_list = NULL, *cri_row_list = NULL;
856 857
	GOFormat const *format = gnm_style_get_format
		(sheet_style_get (sheet, ep->eval.col, ep->eval.row));
858

859
	g_return_val_if_fail (selection != NULL , TRUE);
Morten Welinder's avatar
Morten Welinder committed
860

861
	parse_pos_init_evalpos (&pp, ep);
862
	name = undo_range_list_name (sheet, selection);
863 864 865 866 867 868 869 870

	if ((format == NULL) || !go_format_is_text (format)) {
		expr_txt = gnm_expr_char_start_p (new_text);
		if (expr_txt != NULL)
			texpr = gnm_expr_parse_str
				(expr_txt, &pp, GNM_EXPR_PARSE_DEFAULT,
				 sheet_get_conventions (sheet), NULL);
	}
Jody Goldberg's avatar
Jody Goldberg committed
871

872
	if (texpr != NULL) {
873
		GOFormat const *sf;
874
		GnmStyle *new_style = NULL;
875 876 877 878 879 880
		gboolean same_texpr = TRUE;

		/* We should check whether we are in fact changing anything: */
		for (l = selection; l != NULL && same_texpr; l = l->next) {
			GnmRange *r = l->data;
			GnmValue *val =
Morten Welinder's avatar
Morten Welinder committed
881
				sheet_foreach_cell_in_range
Morten Welinder's avatar
Morten Welinder committed
882
				(sheet, CELL_ITER_ALL, r,
Morten Welinder's avatar
Morten Welinder committed
883
				 (CellIterFunc) cmd_set_text_full_check_texpr,
884
				 (gpointer) texpr);
Morten Welinder's avatar
Morten Welinder committed
885

886 887 888 889 890 891 892 893
			same_texpr = (val != VALUE_TERMINATE);
			if (val != NULL && same_texpr)
				value_release (val);
		}

		if (same_texpr) {
			gnm_expr_top_unref (texpr);
			g_free (name);
Morten Welinder's avatar
Morten Welinder committed
894
			range_fragment_free (selection);
895 896
			return TRUE;
		}
Jody Goldberg's avatar
Jody Goldberg committed
897

898
		text = g_strdup_printf (_("Inserting expression in %s"), name);
Jody Goldberg's avatar
Jody Goldberg committed
899

900
		if (go_format_is_general (format)) {
Morten Welinder's avatar
Morten Welinder committed
901
			sf = gnm_auto_style_format_suggest (texpr, ep);
902 903 904 905 906
			if (sf != NULL) {
				new_style = gnm_style_new ();
				gnm_style_set_format (new_style, sf);
				go_format_unref (sf);
			}
907
		}
Jody Goldberg's avatar
Jody Goldberg committed
908

909 910
		for (l = selection; l != NULL; l = l->next) {
			GnmSheetRange *sr;
Morten Welinder's avatar
Morten Welinder committed
911
			undo = go_undo_combine
912 913
				(undo, clipboard_copy_range_undo (sheet, l->data));
			sr = gnm_sheet_range_new (sheet, l->data);
Morten Welinder's avatar
Morten Welinder committed
914
			redo = go_undo_combine
915 916 917
				(redo, sheet_range_set_expr_undo (sr, texpr));
			if (new_style) {
				sr = gnm_sheet_range_new (sheet, l->data);
Morten Welinder's avatar
Morten Welinder committed
918
				redo = go_undo_combine
919 920 921 922 923 924
					(redo, sheet_apply_style_undo (sr, new_style));
			}
		}
		if (new_style)
			gnm_style_unref (new_style);
		gnm_expr_top_unref (texpr);
925
		autofit_col = TRUE;
926 927
	} else {
		GString *text_str;
928
		PangoAttrList *adj_markup = NULL;
929
		char *corrected;
930 931
		gboolean same_text = TRUE;
		gboolean same_markup = TRUE;
Morten Welinder's avatar
Morten Welinder committed
932

933 934 935 936 937 938
		if (new_text == NULL)
			corrected = NULL;
		else if (autocorrect)
			corrected = autocorrect_tool (new_text);
		else
			corrected = g_strdup (new_text);
939 940 941 942 943

		if (corrected && (corrected[0] == '\'') && corrected[1] == '\0') {
			g_free (corrected);
			corrected = g_strdup ("");
		}
944

945 946 947 948 949
		/* We should check whether we are in fact changing anything: */
		/* We'll handle */
		for (l = selection; l != NULL && same_text; l = l->next) {
			GnmRange *r = l->data;
			GnmValue *val =
Morten Welinder's avatar
Morten Welinder committed
950
				sheet_foreach_cell_in_range
Morten Welinder's avatar
Morten Welinder committed
951
				(sheet, CELL_ITER_ALL, r,
Morten Welinder's avatar
Morten Welinder committed
952
				 (CellIterFunc) cmd_set_text_full_check_text,
953
				 (gpointer) corrected);
Morten Welinder's avatar
Morten Welinder committed
954

955 956 957 958
			same_text = (val != VALUE_TERMINATE);
			if (val != NULL && same_text)
				value_release (val);
		}
959

960 961
		if (go_pango_attr_list_is_empty (markup))
			markup = NULL;
962
		if (markup && corrected && corrected[0] == '\'') {
963 964 965
			markup = adj_markup = pango_attr_list_copy (markup);
			go_pango_attr_list_erase (adj_markup, 0, 1);
		}
Morten Welinder's avatar
Morten Welinder committed
966

967 968 969 970
		if (same_text) {
			for (l = selection; l != NULL && same_text; l = l->next) {
				GnmRange *r = l->data;
				GnmValue *val =
Morten Welinder's avatar
Morten Welinder committed
971
					sheet_foreach_cell_in_range
Morten Welinder's avatar
Morten Welinder committed
972
					(sheet, CELL_ITER_IGNORE_BLANK, r,
Morten Welinder's avatar
Morten Welinder committed
973
					 (CellIterFunc) cmd_set_text_full_check_markup,
974
					 (gpointer) markup);
Morten Welinder's avatar
Morten Welinder committed
975

976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
				same_markup = (val != VALUE_TERMINATE);
				if (val != NULL && same_markup)
					value_release (val);
			}

			if (same_markup) {
				g_free (corrected);
				g_free (name);
				range_fragment_free (selection);
				if (adj_markup)
					pango_attr_list_unref (adj_markup);
				return TRUE;
			}

			text = g_strdup_printf (_("Editing style of %s"), name);
		} else {
			text_str = gnm_cmd_trunc_descriptor (g_string_new (corrected), NULL);
			text = g_strdup_printf (_("Typing \"%s\" in %s"), text_str->str, name);
			g_string_free (text_str, TRUE);
		}
996

997 998
		for (l = selection; l != NULL; l = l->next) {
			GnmSheetRange *sr;
Morten Welinder's avatar
Morten Welinder committed
999
			undo = go_undo_combine
1000
				(undo, clipboard_copy_range_undo (sheet, l->data));
1001 1002
			if (corrected) {
				sr = gnm_sheet_range_new (sheet, l->data);
Morten Welinder's avatar
Morten Welinder committed
1003 1004
				redo = go_undo_combine
					(redo, sheet_range_set_text_undo
1005 1006
					 (sr, corrected));
			}
1007 1008 1009
			if (markup) {
				sr = gnm_sheet_range_new (sheet, l->data);
				/* Note: order of combination matters!! */
Morten Welinder's avatar
Morten Welinder committed
1010
				redo = go_undo_combine
1011 1012
					(sheet_range_set_markup_undo (sr, markup), redo);
			}
1013
		}
1014

1015 1016
		if (adj_markup)
			pango_attr_list_unref (adj_markup);
1017
		g_free (corrected);
1018 1019

		same_text_and_not_same_markup = (same_text && !same_markup);
1020
	}
1021
	g_free (name);
Morten Welinder's avatar
Morten Welinder committed
1022

1023 1024
	/* We are combining this since we don't want to apply and undo twice.*/
	if (same_text_and_not_same_markup || !autofit_col) {
Morten Welinder's avatar
Morten Welinder committed
1025
		GnmCell *cell = sheet_cell_fetch
1026
			(sheet, ep->eval.col, ep->eval.row);
1027 1028
		gboolean nvis;

1029
		go_undo_undo (redo);
1030 1031 1032 1033 1034 1035 1036
		nvis = !VALUE_IS_STRING (cell->value);
		if (!autofit_col)
			autofit_col = nvis;
		if (same_text_and_not_same_markup)
			/* We only have to do something if at least one cell           */
			/* now contains a string, but they contain all the same thing. */
			same_text_and_not_same_markup = nvis;
1037 1038
		go_undo_undo (undo);
	}
1039
	if (same_text_and_not_same_markup) {
Morten Welinder's avatar
Morten Welinder committed
1040
		/*We had the same text and different markup but we are not entering strings. */
1041 1042
		g_object_unref (undo);
		g_object_unref (redo);
1043 1044 1045 1046
		g_free (text);
		range_fragment_free (selection);
		return TRUE;
	}
1047 1048
	for (l = selection; l != NULL; l = l->next) {
		GnmRange *r = l->data;
1049 1050 1051
		GnmRange *new_r;

		new_r = g_new (GnmRange, 1);
Morten Welinder's avatar
Morten Welinder committed
1052 1053 1054 1055
		*new_r = *r;
		redo  = go_undo_combine
			(go_undo_binary_new
			 (sheet, new_r,
1056
			  (GOUndoBinaryFunc) colrow_autofit_row,
1057 1058
			  NULL, g_free),
			 redo);
Morten Welinder's avatar
Morten Welinder committed
1059
		cri_row_list = colrow_get_index_list
1060 1061 1062 1063
			(r->start.row, r->end.row, cri_row_list);

		if (autofit_col) {
			new_r = g_new (GnmRange, 1);
Morten Welinder's avatar
Morten Welinder committed
1064 1065 1066 1067
			*new_r = *r;
			redo  = go_undo_combine
				(go_undo_binary_new
				 (sheet, new_r,
1068
				  (GOUndoBinaryFunc) colrow_autofit_col,
1069 1070
				  NULL, g_free),
				 redo);
Morten Welinder's avatar
Morten Welinder committed
1071
			cri_col_list = colrow_get_index_list
1072 1073
				(r->start.col, r->end.col, cri_col_list);
		}
1074
	}
1075

1076
	undo = go_undo_combine (undo,
Morten Welinder's avatar
Morten Welinder committed
1077 1078 1079
				gnm_undo_colrow_restore_state_group_new
				(sheet, TRUE,
				 cri_col_list,
1080
				 colrow_get_sizes (sheet, TRUE,
1081
						   cri_col_list, -1)));
1082
	undo = go_undo_combine (undo,
Morten Welinder's avatar
Morten Welinder committed
1083 1084 1085
				gnm_undo_colrow_restore_state_group_new
				(sheet, FALSE,
				 cri_row_list,
1086
				 colrow_get_sizes (sheet, FALSE,