cmd-edit.c 12.4 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Jody Goldberg's avatar
Jody Goldberg committed
2 3 4
/*
 * cmd-edit.c: Various commands to be used by the edit menu.
 *
5
 * Copyright (C) 2000 Jody Goldberg (jody@gnome.org)
Jody Goldberg's avatar
Jody Goldberg committed
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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
Jody Goldberg's avatar
Jody Goldberg committed
20 21
 * USA
 */
22
#include <gnumeric-config.h>
23
#include <glib/gi18n-lib.h>
24
#include "gnumeric.h"
Jody Goldberg's avatar
Jody Goldberg committed
25
#include "cmd-edit.h"
26

27
#include "application.h"
28 29
#include "command-context.h"
#include "workbook-control.h"
30
#include "workbook.h"
Jody Goldberg's avatar
Jody Goldberg committed
31
#include "sheet.h"
32
#include "sheet-view.h"
Jody Goldberg's avatar
Jody Goldberg committed
33 34
#include "cell.h"
#include "expr.h"
35
#include "dependent.h"
36
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
37
#include "parse-util.h"
38 39 40
#include "ranges.h"
#include "commands.h"
#include "clipboard.h"
Morten Welinder's avatar
Morten Welinder committed
41
#include "value.h"
42
#include "wbc-gtk.h"
43

Jody Goldberg's avatar
Jody Goldberg committed
44
/**
45
 * sv_select_cur_row:
Jody Goldberg's avatar
Jody Goldberg committed
46
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
47 48 49 50
 *
 * Selects an entire row
 */
void
51
sv_select_cur_row (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
52
{
Jody Goldberg's avatar
Jody Goldberg committed
53
	GnmRange const *sel = selection_first_range (sv,  NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
54
	if (sel != NULL) {
Jody Goldberg's avatar
Jody Goldberg committed
55
		GnmRange r = *sel;
Jody Goldberg's avatar
Jody Goldberg committed
56
		sv_selection_reset (sv);
57
		sv_selection_add_full
58 59
			(sv,
			 sv->edit_pos.col, sv->edit_pos.row,
60
			 0, r.start.row, gnm_sheet_get_last_col (sv->sheet), r.end.row,
61
			 GNM_SELECTION_MODE_ADD);
Jody Goldberg's avatar
Jody Goldberg committed
62
		sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
63
	}
Jody Goldberg's avatar
Jody Goldberg committed
64 65 66
}

/**
67
 * sv_select_cur_col:
Jody Goldberg's avatar
Jody Goldberg committed
68
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
69 70 71 72
 *
 * Selects an entire column
 */
void
73
sv_select_cur_col (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
74
{
Jody Goldberg's avatar
Jody Goldberg committed
75
	GnmRange const *sel = selection_first_range (sv,  NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
76
	if (sel != NULL) {
Jody Goldberg's avatar
Jody Goldberg committed
77
		GnmRange r = *sel;
Jody Goldberg's avatar
Jody Goldberg committed
78
		sv_selection_reset (sv);
79
		sv_selection_add_full
80 81
			(sv,
			 sv->edit_pos.col, sv->edit_pos.row,
82
			 r.start.col, 0, r.end.col, gnm_sheet_get_last_row (sv->sheet),
83
			 GNM_SELECTION_MODE_ADD);
Jody Goldberg's avatar
Jody Goldberg committed
84
		sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
85
	}
Jody Goldberg's avatar
Jody Goldberg committed
86 87 88
}

/**
89
 * sv_select_cur_array:
Jody Goldberg's avatar
Jody Goldberg committed
90
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
91
 *
92 93
 * If the editpos is part of an array clear the selection and select the array.
 **/
Jody Goldberg's avatar
Jody Goldberg committed
94
void
95
sv_select_cur_array (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
96
{
97 98 99
	GnmRange a;
	int const c = sv->edit_pos.col;
	int const r = sv->edit_pos.row;
Jody Goldberg's avatar
Jody Goldberg committed
100

101
	if (!gnm_cell_array_bound (sheet_cell_get (sv->sheet, c, r), &a))
Jody Goldberg's avatar
Jody Goldberg committed
102 103
		return;

104
	/* leave the edit pos where it is, select the entire array. */
Jody Goldberg's avatar
Jody Goldberg committed
105
	sv_selection_reset (sv);
106
	sv_selection_add_full (sv, c, r,
107
			       a.start.col, a.start.row, a.end.col, a.end.row,
108
			       GNM_SELECTION_MODE_ADD);
Jody Goldberg's avatar
Jody Goldberg committed
109
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
110 111 112 113 114
}

static gint
cb_compare_deps (gconstpointer a, gconstpointer b)
{
115 116
	GnmCell const *cell_a = a;
	GnmCell const *cell_b = b;
Jody Goldberg's avatar
Jody Goldberg committed
117 118
	int tmp;

119 120 121
	if (cell_a->base.sheet != cell_b->base.sheet)
		return cell_a->base.sheet->index_in_wb - cell_b->base.sheet->index_in_wb;

122
	tmp = cell_a->pos.row - cell_b->pos.row;
Jody Goldberg's avatar
Jody Goldberg committed
123 124
	if (tmp != 0)
		return tmp;
125
	return cell_a->pos.col - cell_b->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
126 127
}

Jody Goldberg's avatar
Jody Goldberg committed
128
static void
129
cb_collect_deps (GnmDependent *dep, gpointer user)
Jody Goldberg's avatar
Jody Goldberg committed
130
{
131
	if (dependent_is_cell (dep)) {
Jody Goldberg's avatar
Jody Goldberg committed
132 133 134 135 136
		GList **list = (GList **)user;
		*list = g_list_prepend (*list, dep);
	}
}

Jody Goldberg's avatar
Jody Goldberg committed
137
/**
138
 * sv_select_cur_depends:
Jody Goldberg's avatar
Jody Goldberg committed
139
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
140 141 142 143
 *
 * Select all cells that depend on the expression in the current cell.
 */
void
144
sv_select_cur_depends (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
145
{
146
	GnmCell  *cur_cell, dummy;
Jody Goldberg's avatar
Jody Goldberg committed
147
	GList *deps = NULL, *ptr = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
148

Morten Welinder's avatar
Morten Welinder committed
149
	g_return_if_fail (GNM_IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
150

Jody Goldberg's avatar
Jody Goldberg committed
151
	cur_cell = sheet_cell_get (sv->sheet,
152 153 154 155 156 157
		sv->edit_pos.col, sv->edit_pos.row);
	if (cur_cell == NULL) {
		dummy.base.sheet = sv_sheet (sv);
		dummy.pos = sv->edit_pos;
		cur_cell = &dummy;
	}
Jody Goldberg's avatar
Jody Goldberg committed
158

Jody Goldberg's avatar
Jody Goldberg committed
159
	cell_foreach_dep (cur_cell, cb_collect_deps, &deps);
Jody Goldberg's avatar
Jody Goldberg committed
160 161 162
	if (deps == NULL)
		return;

Jody Goldberg's avatar
Jody Goldberg committed
163
	sv_selection_reset (sv);
Jody Goldberg's avatar
Jody Goldberg committed
164 165 166

	/* Short circuit */
	if (g_list_length (deps) == 1) {
167
		GnmCell *cell = deps->data;
168
		sv_selection_add_pos (sv, cell->pos.col, cell->pos.row,
169
				      GNM_SELECTION_MODE_ADD);
Jody Goldberg's avatar
Jody Goldberg committed
170
	} else {
Jody Goldberg's avatar
Jody Goldberg committed
171
		GnmRange *cur = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
172 173 174 175
		ptr = NULL;

		/* Merge the sorted list of cells into rows */
		for (deps = g_list_sort (deps, &cb_compare_deps) ; deps ; ) {
176
			GnmCell *cell = deps->data;
Jody Goldberg's avatar
Jody Goldberg committed
177 178

			if (cur == NULL ||
179 180
			    cur->end.row != cell->pos.row ||
			    cur->end.col+1 != cell->pos.col) {
Jody Goldberg's avatar
Jody Goldberg committed
181 182
				if (cur)
					ptr = g_list_prepend (ptr, cur);
Jody Goldberg's avatar
Jody Goldberg committed
183
				cur = g_new (GnmRange, 1);
184 185
				cur->start.row = cur->end.row = cell->pos.row;
				cur->start.col = cur->end.col = cell->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
186
			} else
187
				cur->end.col = cell->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
188 189 190 191 192 193 194 195 196

			deps = g_list_remove (deps, cell);
		}
		if (cur)
			ptr = g_list_prepend (ptr, cur);

		/* Merge the coalesced rows into ranges */
		deps = ptr;
		for (ptr = NULL ; deps ; ) {
Jody Goldberg's avatar
Jody Goldberg committed
197
			GnmRange *r1 = deps->data;
Jody Goldberg's avatar
Jody Goldberg committed
198 199 200
			GList *fwd;

			for (fwd = deps->next ; fwd ; ) {
Jody Goldberg's avatar
Jody Goldberg committed
201
				GnmRange *r2 = fwd->data;
Jody Goldberg's avatar
Jody Goldberg committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

				if (r1->start.col == r2->start.col &&
				    r1->end.col == r2->end.col &&
				    r1->start.row-1 == r2->end.row) {
					r1->start.row = r2->start.row;
					g_free (fwd->data);
					fwd = g_list_remove (fwd, r2);
				} else
					fwd = fwd->next;
			}

			ptr = g_list_prepend (ptr, r1);
			deps = g_list_remove (deps, r1);
		}

		/* now select the ranges */
		while (ptr) {
219
			sv_selection_add_range (sv, ptr->data);
Jody Goldberg's avatar
Jody Goldberg committed
220
			g_free (ptr->data);
221
			ptr = g_list_remove (ptr, ptr->data);
Jody Goldberg's avatar
Jody Goldberg committed
222 223
		}
	}
Jody Goldberg's avatar
Jody Goldberg committed
224
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
225 226 227
}

/**
228
 * sv_select_cur_inputs:
Jody Goldberg's avatar
Jody Goldberg committed
229
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
230 231 232
 *
 * Select all cells that are direct potential inputs to the
 * current cell.
233
 **/
Jody Goldberg's avatar
Jody Goldberg committed
234
void
235
sv_select_cur_inputs (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
236
{
237 238
	GnmCell  *cell;
	GSList   *ranges, *ptr;
Morten Welinder's avatar
Morten Welinder committed
239
	GnmEvalPos ep;
Jody Goldberg's avatar
Jody Goldberg committed
240

Morten Welinder's avatar
Morten Welinder committed
241
	g_return_if_fail (GNM_IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
242

Jody Goldberg's avatar
Jody Goldberg committed
243
	cell = sheet_cell_get (sv->sheet,
244
		sv->edit_pos.col, sv->edit_pos.row);
245
	if (cell == NULL || !gnm_cell_has_expr (cell))
Jody Goldberg's avatar
Jody Goldberg committed
246
		return;
247
	ranges = gnm_expr_top_get_ranges (cell->base.texpr);
248 249 250
	if (ranges == NULL)
		return;

Morten Welinder's avatar
Morten Welinder committed
251 252 253 254
	ep.eval = sv->edit_pos;
	ep.sheet = sv->sheet;
	ep.dep = NULL;

255
	sv_selection_reset (sv);
Morten Welinder's avatar
Morten Welinder committed
256 257 258 259 260
	for (ptr = ranges ; ptr != NULL ; ptr = ptr->next) {
		GnmValue *v = ptr->data;
		GnmRangeRef const *r = value_get_rangeref (v);

#warning "FIXME: What do we do in these 3D cases?"
261
		if ((r->a.sheet == r->b.sheet) &&
262 263 264 265
		    (r->a.sheet == NULL || r->a.sheet == sv->sheet)) {
		      gint row, col;
		      row = gnm_cellref_get_row (&r->a, &ep);
		      col = gnm_cellref_get_col (&r->a, &ep);
266
		      sv_selection_add_full
267 268
			      (sv, col, row, col, row,
			       gnm_cellref_get_col (&r->b, &ep),
269
			       gnm_cellref_get_row (&r->b, &ep),
270 271
			       GNM_SELECTION_MODE_ADD);
		    }
Morten Welinder's avatar
Morten Welinder committed
272 273
		value_release (v);
	}
274
	g_slist_free (ranges);
Jody Goldberg's avatar
Jody Goldberg committed
275

Jody Goldberg's avatar
Jody Goldberg committed
276
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
277
}
278 279

/**
280
 * cmd_paste:
281 282 283 284 285 286 287 288 289
 *
 * Pastes the current cut buffer, copy buffer, or X selection to
 * the destination sheet range.
 *
 * When pasting a cut the destination MUST be the same size as the src.
 *
 * When pasting a copy the destination can be a singleton, or an integer
 * multiple of the size of the source.  This is not tested here.
 * Full undo support.
290
 **/
291
void
Morten Welinder's avatar
Morten Welinder committed
292
cmd_paste (WorkbookControl *wbc, GnmPasteTarget const *pt)
293
{
Morten Welinder's avatar
Morten Welinder committed
294
	GnmCellRegion  *content;
Jody Goldberg's avatar
Jody Goldberg committed
295
	GnmRange const *src_range;
296
	GnmRange dst;
297

298
	g_return_if_fail (pt != NULL);
299 300
	g_return_if_fail (IS_SHEET (pt->sheet));

301 302 303
	dst = pt->range;

	/* Check for locks */
304
	if (cmd_cell_range_is_locked_effective (pt->sheet, &dst, wbc,
305 306 307
						_("Paste")))
		return ;

Jody Goldberg's avatar
Jody Goldberg committed
308 309
	src_range = gnm_app_clipboard_area_get ();
	content = gnm_app_clipboard_contents_get ();
310 311 312

	if (content == NULL && src_range != NULL) {
		/* Pasting a Cut */
313
		GnmExprRelocateInfo rinfo;
Jody Goldberg's avatar
Jody Goldberg committed
314
		Sheet *src_sheet = gnm_app_clipboard_sheet_get ();
315 316 317

		/* Validate the size & shape of the target here. */
		int const cols = (src_range->end.col - src_range->start.col);
318
		int const rows = (src_range->end.row - src_range->start.row);
319 320 321 322 323 324 325

		if (range_is_singleton (&dst)) {
			dst.end.col = dst.start.col + cols;
			dst.end.row = dst.start.row + rows;
		} else if ((dst.end.col - dst.start.col) != cols ||
			   (dst.end.row - dst.start.row) != rows) {

326
			char *msg = g_strdup_printf (
327 328 329 330 331
				_("destination has a different shape (%dRx%dC) than the original (%dRx%dC)\n\n"
				  "Try selecting a single cell or an area of the same shape and size."),
				(dst.end.row - dst.start.row)+1,
				(dst.end.col - dst.start.col)+1,
				rows+1, cols+1);
332
			go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc),
333
				_("Unable to paste into selection"), msg);
334 335 336 337
			g_free (msg);
			return;
		}

338
		rinfo.reloc_type = GNM_EXPR_RELOCATE_MOVE_RANGE;
339 340 341 342 343 344
		rinfo.origin = *src_range;
		rinfo.col_offset = dst.start.col - rinfo.origin.start.col;
		rinfo.row_offset = dst.start.row - rinfo.origin.start.row;
		rinfo.origin_sheet = src_sheet;
		rinfo.target_sheet = pt->sheet;

345
		if (!cmd_paste_cut (wbc, &rinfo, TRUE, NULL))
Jody Goldberg's avatar
Jody Goldberg committed
346
			gnm_app_clipboard_clear (TRUE);
347 348

	/* If this application has marked a selection use it */
349
	} else if (content != NULL) {
350
		cmd_paste_copy (wbc, pt, content);
351 352 353
		/* We don't own the contents, so don't unref it.  */
	} else {
		/* See if the control has access to information to paste */
354
		wb_control_paste_from_selection (wbc, pt);
355
	}
356 357 358
}

/**
359
 * cmd_paste_to_selection:
Jody Goldberg's avatar
Jody Goldberg committed
360
 * @dest_sv: The sheet into which things should be pasted
361
 * @paste_flags: special paste flags (eg transpose)
362 363 364 365 366
 *
 * Using the current selection as a target
 * Full undo support.
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
367
cmd_paste_to_selection (WorkbookControl *wbc, SheetView *dest_sv, int paste_flags)
368
{
Jody Goldberg's avatar
Jody Goldberg committed
369
	GnmRange const *r;
Morten Welinder's avatar
Morten Welinder committed
370
	GnmPasteTarget pt;
371

372 373
	r = selection_first_range (dest_sv, GO_CMD_CONTEXT (wbc), _("Paste"));
	if (!r)
374 375
		return;

Jody Goldberg's avatar
Jody Goldberg committed
376
	pt.sheet = dest_sv->sheet;
377
	pt.range = *r;
378
	pt.paste_flags = paste_flags;
379
	cmd_paste (wbc, &pt);
380
}
381 382 383

/**
 * cmd_shift_rows:
384
 * @wbc:	The error context.
385 386 387 388 389 390 391 392 393 394
 * @sheet	the sheet
 * @col		column marking the start of the shift
 * @start_row	first row
 * @end_row	end row
 * @count	numbers of columns to shift.  negative numbers will
 *		delete count columns, positive number will insert
 *		count columns.
 *
 * Takes the cells in the region (col,start_row):(MAX_COL,end_row)
 * and copies them @count units (possibly negative) to the right.
395
 **/
396
void
397
cmd_shift_rows (WorkbookControl *wbc, Sheet *sheet,
398 399
		int col, int start_row, int end_row, int count)
{
400
	GnmExprRelocateInfo rinfo;
401
	char *desc;
402

403
	rinfo.reloc_type = GNM_EXPR_RELOCATE_MOVE_RANGE;
404 405 406 407 408 409
	rinfo.col_offset = count;
	rinfo.row_offset = 0;
	rinfo.origin_sheet = rinfo.target_sheet = sheet;
	rinfo.origin.start.row = start_row;
	rinfo.origin.start.col = col;
	rinfo.origin.end.row = end_row;
410
	rinfo.origin.end.col = gnm_sheet_get_last_col (sheet);
411 412 413 414 415 416 417


	if (count > 0) {
		GnmRange r = rinfo.origin;
		r.start.col = r.end.col - count + 1;

		if (!sheet_is_region_empty (sheet, &r)) {
Morten Welinder's avatar
Morten Welinder committed
418
			go_gtk_notice_dialog (wbcg_toplevel (WBC_GTK (wbc)), GTK_MESSAGE_ERROR,
419 420 421
					      _("Inserting these cells would push data off the sheet. "
						"Please enlarge the sheet first."));
			return;
Morten Welinder's avatar
Morten Welinder committed
422
		}
Jody Goldberg's avatar
Jody Goldberg committed
423
		rinfo.origin.end.col -= count;
424
	}
425

426 427 428 429
	desc = g_strdup_printf ((start_row != end_row)
				? _("Shift rows %s")
				: _("Shift row %s"),
				rows_name (start_row, end_row));
430
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
431 432 433 434
}

/**
 * cmd_shift_cols:
435
 * @wbc:	The error context.
436 437 438 439 440
 * @sheet:	the sheet
 * @start_col:	first column
 * @end_col:	end column
 * @row:	row marking the start of the shift
 * @count:	numbers of rows to shift.  a negative numbers will
441 442 443 444 445 446 447
 *		delete count rows, positive number will insert
 *		count rows.
 *
 * Takes the cells in the region (start_col,row):(end_col,MAX_ROW)
 * and copies them @count units (possibly negative) downwards.
 */
void
448
cmd_shift_cols (WorkbookControl *wbc, Sheet *sheet,
449 450
		int start_col, int end_col, int row, int count)
{
451
	GnmExprRelocateInfo rinfo;
452
	char *desc;
453

454
	rinfo.reloc_type = GNM_EXPR_RELOCATE_MOVE_RANGE;
455 456 457 458 459 460
	rinfo.col_offset = 0;
	rinfo.row_offset = count;
	rinfo.origin_sheet = rinfo.target_sheet = sheet;
	rinfo.origin.start.col = start_col;
	rinfo.origin.start.row = row;
	rinfo.origin.end.col = end_col;
461
	rinfo.origin.end.row = gnm_sheet_get_last_row (sheet);
462 463 464 465 466
	if (count > 0) {
		GnmRange r = rinfo.origin;
		r.start.row = r.end.row - count + 1;

		if (!sheet_is_region_empty (sheet, &r)) {
Morten Welinder's avatar
Morten Welinder committed
467
			go_gtk_notice_dialog (wbcg_toplevel (WBC_GTK (wbc)), GTK_MESSAGE_ERROR,
468 469 470
					      _("Inserting these cells would push data off the sheet. "
						"Please enlarge the sheet first."));
			return;
Morten Welinder's avatar
Morten Welinder committed
471
		}
Jody Goldberg's avatar
Jody Goldberg committed
472
		rinfo.origin.end.row -= count;
473
	}
474

475 476 477 478
	desc = g_strdup_printf ((start_col != end_col)
				? _("Shift columns %s")
				: _("Shift column %s"),
				cols_name (start_col, end_col));
479
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
480
}