cmd-edit.c 11.5 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1
/* vim: set sw=8: */
Jody Goldberg's avatar
Jody Goldberg committed
2 3 4
/*
 * cmd-edit.c: Various commands to be used by the edit menu.
 *
Jody Goldberg's avatar
Jody Goldberg committed
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 19 20 21
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
22
#include <gnumeric-config.h>
23
#include <glib/gi18n.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 "expr-impl.h"
36
#include "dependent.h"
37
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
38
#include "parse-util.h"
39 40 41
#include "ranges.h"
#include "commands.h"
#include "clipboard.h"
Morten Welinder's avatar
Morten Welinder committed
42
#include "value.h"
43

Jody Goldberg's avatar
Jody Goldberg committed
44
#include <gtk/gtk.h>
Jody Goldberg's avatar
Jody Goldberg committed
45 46

/**
47
 * sv_select_cur_row:
Jody Goldberg's avatar
Jody Goldberg committed
48
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
49 50 51 52
 *
 * Selects an entire row
 */
void
53
sv_select_cur_row (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
54
{
Jody Goldberg's avatar
Jody Goldberg committed
55
	GnmRange const *sel = selection_first_range (sv,  NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
56
	if (sel != NULL) {
Jody Goldberg's avatar
Jody Goldberg committed
57
		GnmRange r = *sel;
Jody Goldberg's avatar
Jody Goldberg committed
58 59 60
		sv_selection_reset (sv);
		sv_selection_add_range (sv,
			sv->edit_pos.col, sv->edit_pos.row,
Jody Goldberg's avatar
Jody Goldberg committed
61
			0, r.start.row, SHEET_MAX_COLS-1, r.end.row);
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 79 80
		sv_selection_reset (sv);
		sv_selection_add_range (sv,
			sv->edit_pos.col, sv->edit_pos.row,
Jody Goldberg's avatar
Jody Goldberg committed
81
			r.start.col, 0, r.end.col, SHEET_MAX_ROWS-1);
Jody Goldberg's avatar
Jody Goldberg committed
82
		sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
83
	}
Jody Goldberg's avatar
Jody Goldberg committed
84 85 86
}

/**
87
 * sv_select_cur_array :
Jody Goldberg's avatar
Jody Goldberg committed
88
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
89 90 91 92 93
 *
 * if the current cell is part of an array select
 * the entire array.
 */
void
94
sv_select_cur_array (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
95
{
96
	GnmExprArray const *array;
Jody Goldberg's avatar
Jody Goldberg committed
97 98
	int col, row;

Jody Goldberg's avatar
Jody Goldberg committed
99 100 101
	col = sv->edit_pos.col;
	row = sv->edit_pos.row;
	array = cell_is_array (sheet_cell_get (sv->sheet, col, row));
Jody Goldberg's avatar
Jody Goldberg committed
102 103 104 105

	if (array == NULL)
		return;

Jody Goldberg's avatar
Jody Goldberg committed
106
	sv_selection_reset (sv);
Jody Goldberg's avatar
Jody Goldberg committed
107 108 109 110
	/*
	 * leave the edit cell where it is,
	 * select the entire array.
	 */
Jody Goldberg's avatar
Jody Goldberg committed
111
	sv_selection_add_range (sv, col, row,
Jody Goldberg's avatar
Jody Goldberg committed
112
				   col - array->x, row - array->y,
Jody Goldberg's avatar
Jody Goldberg committed
113 114
				   col - array->x + array->cols - 1,
				   row - array->y + array->rows - 1);
Jody Goldberg's avatar
Jody Goldberg committed
115

Jody Goldberg's avatar
Jody Goldberg committed
116
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
117 118 119 120 121
}

static gint
cb_compare_deps (gconstpointer a, gconstpointer b)
{
122 123
	GnmCell const *cell_a = a;
	GnmCell const *cell_b = b;
Jody Goldberg's avatar
Jody Goldberg committed
124 125
	int tmp;

126 127 128
	if (cell_a->base.sheet != cell_b->base.sheet)
		return cell_a->base.sheet->index_in_wb - cell_b->base.sheet->index_in_wb;

129
	tmp = cell_a->pos.row - cell_b->pos.row;
Jody Goldberg's avatar
Jody Goldberg committed
130 131
	if (tmp != 0)
		return tmp;
132
	return cell_a->pos.col - cell_b->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
133 134
}

Jody Goldberg's avatar
Jody Goldberg committed
135
static void
136
cb_collect_deps (GnmDependent *dep, gpointer user)
Jody Goldberg's avatar
Jody Goldberg committed
137
{
138
	if (dependent_is_cell (dep)) {
Jody Goldberg's avatar
Jody Goldberg committed
139 140 141 142 143
		GList **list = (GList **)user;
		*list = g_list_prepend (*list, dep);
	}
}

Jody Goldberg's avatar
Jody Goldberg committed
144
/**
145
 * sv_select_cur_depends :
Jody Goldberg's avatar
Jody Goldberg committed
146
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
147 148 149 150
 *
 * Select all cells that depend on the expression in the current cell.
 */
void
151
sv_select_cur_depends (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
152
{
153
	GnmCell  *cur_cell, dummy;
Jody Goldberg's avatar
Jody Goldberg committed
154
	GList *deps = NULL, *ptr = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
155

Jody Goldberg's avatar
Jody Goldberg committed
156
	g_return_if_fail (IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
157

Jody Goldberg's avatar
Jody Goldberg committed
158
	cur_cell = sheet_cell_get (sv->sheet,
159 160 161 162 163 164
		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
165

Jody Goldberg's avatar
Jody Goldberg committed
166
	cell_foreach_dep (cur_cell, cb_collect_deps, &deps);
Jody Goldberg's avatar
Jody Goldberg committed
167 168 169
	if (deps == NULL)
		return;

Jody Goldberg's avatar
Jody Goldberg committed
170
	sv_selection_reset (sv);
Jody Goldberg's avatar
Jody Goldberg committed
171 172 173

	/* Short circuit */
	if (g_list_length (deps) == 1) {
174
		GnmCell *cell = deps->data;
Jody Goldberg's avatar
Jody Goldberg committed
175
		sv_selection_add_pos (sv, cell->pos.col, cell->pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
176
	} else {
Jody Goldberg's avatar
Jody Goldberg committed
177
		GnmRange *cur = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
178 179 180 181
		ptr = NULL;

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

			if (cur == NULL ||
185 186
			    cur->end.row != cell->pos.row ||
			    cur->end.col+1 != cell->pos.col) {
Jody Goldberg's avatar
Jody Goldberg committed
187 188
				if (cur)
					ptr = g_list_prepend (ptr, cur);
Jody Goldberg's avatar
Jody Goldberg committed
189
				cur = g_new (GnmRange, 1);
190 191
				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
192
			} else
193
				cur->end.col = cell->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
194 195 196 197 198 199 200 201 202

			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
203
			GnmRange *r1 = deps->data;
Jody Goldberg's avatar
Jody Goldberg committed
204 205 206
			GList *fwd;

			for (fwd = deps->next ; fwd ; ) {
Jody Goldberg's avatar
Jody Goldberg committed
207
				GnmRange *r2 = fwd->data;
Jody Goldberg's avatar
Jody Goldberg committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

				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) {
Jody Goldberg's avatar
Jody Goldberg committed
225
			GnmRange *r = ptr->data;
Jody Goldberg's avatar
Jody Goldberg committed
226 227 228 229
			sv_selection_add_range (sv,
						r->start.col, r->start.row,
						r->start.col, r->start.row,
						r->end.col, r->end.row);
Jody Goldberg's avatar
Jody Goldberg committed
230 231 232 233
			g_free (ptr->data);
			ptr = g_list_remove (ptr, r);
		}
	}
Jody Goldberg's avatar
Jody Goldberg committed
234
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
235 236 237
}

/**
238
 * sv_select_cur_inputs :
Jody Goldberg's avatar
Jody Goldberg committed
239
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
240 241 242
 *
 * Select all cells that are direct potential inputs to the
 * current cell.
243
 **/
Jody Goldberg's avatar
Jody Goldberg committed
244
void
245
sv_select_cur_inputs (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
246
{
247 248
	GnmCell  *cell;
	GSList   *ranges, *ptr;
Morten Welinder's avatar
Morten Welinder committed
249
	GnmEvalPos ep;
Jody Goldberg's avatar
Jody Goldberg committed
250

Jody Goldberg's avatar
Jody Goldberg committed
251
	g_return_if_fail (IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
252

Jody Goldberg's avatar
Jody Goldberg committed
253
	cell = sheet_cell_get (sv->sheet,
254
		sv->edit_pos.col, sv->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
255 256
	if (cell == NULL || !cell_has_expr (cell))
		return;
257 258 259 260
	ranges = gnm_expr_get_ranges (cell->base.expression);
	if (ranges == NULL)
		return;

Morten Welinder's avatar
Morten Welinder committed
261 262 263 264
	ep.eval = sv->edit_pos;
	ep.sheet = sv->sheet;
	ep.dep = NULL;

265
	sv_selection_reset (sv);
Morten Welinder's avatar
Morten Welinder committed
266 267 268 269 270 271 272 273 274 275
	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?"
		if (r->a.sheet != r->b.sheet)
			continue;
		if (r->a.sheet != NULL && r->a.sheet != sv->sheet)
			continue;

276
		sv_selection_add_range (sv,
Morten Welinder's avatar
Morten Welinder committed
277 278 279 280 281 282 283 284
					cellref_get_abs_col (&r->a, &ep),
					cellref_get_abs_row (&r->a, &ep),
					cellref_get_abs_col (&r->a, &ep),
					cellref_get_abs_row (&r->a, &ep),
					cellref_get_abs_col (&r->b, &ep),
					cellref_get_abs_row (&r->b, &ep));
		value_release (v);
	}
285
	g_slist_free (ranges);
Jody Goldberg's avatar
Jody Goldberg committed
286

Jody Goldberg's avatar
Jody Goldberg committed
287
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
288
}
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

/**
 * cmd_paste :
 * @sheet: The destination sheet
 * @range : The range to paste to within the destination sheet.
 * @flags: Any paste special flags.
 *
 * 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.
 */
void
Morten Welinder's avatar
Morten Welinder committed
306
cmd_paste (WorkbookControl *wbc, GnmPasteTarget const *pt)
307
{
Morten Welinder's avatar
Morten Welinder committed
308
	GnmCellRegion  *content;
Jody Goldberg's avatar
Jody Goldberg committed
309
	GnmRange const *src_range;
310

311
	g_return_if_fail (pt != NULL);
312 313
	g_return_if_fail (IS_SHEET (pt->sheet));

Jody Goldberg's avatar
Jody Goldberg committed
314 315
	src_range = gnm_app_clipboard_area_get ();
	content = gnm_app_clipboard_contents_get ();
316 317 318

	if (content == NULL && src_range != NULL) {
		/* Pasting a Cut */
319
		GnmExprRelocateInfo rinfo;
Jody Goldberg's avatar
Jody Goldberg committed
320
		Sheet *src_sheet = gnm_app_clipboard_sheet_get ();
321 322 323

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

Jody Goldberg's avatar
Jody Goldberg committed
326
		GnmRange dst = pt->range;
327 328 329 330 331 332 333

		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) {

Miguel de Icaza's avatar
Miguel de Icaza committed
334
			char *msg = g_strdup_printf (
335 336 337 338 339
				_("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);
340
			go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc),
341
				_("Unable to paste into selection"), msg);
342 343 344 345 346 347 348 349 350 351
			g_free (msg);
			return;
		}

		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;

352
		if (!cmd_paste_cut (wbc, &rinfo, TRUE, NULL))
Jody Goldberg's avatar
Jody Goldberg committed
353
			gnm_app_clipboard_clear (TRUE);
354 355 356 357 358 359 360

	/* If this application has marked a selection use it */
	} else if (content != NULL)
		cmd_paste_copy (wbc, pt, content);

	/* See if the control has access to information to paste */
	else
361
		wb_control_paste_from_selection (wbc, pt);
362 363 364 365
}

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

378
	if (!(r = selection_first_range (dest_sv, GO_CMD_CONTEXT (wbc), _("Paste"))))
379 380
		return;

381
	g_return_if_fail (r !=NULL);
382

Jody Goldberg's avatar
Jody Goldberg committed
383
	pt.sheet = dest_sv->sheet;
384
	pt.range = *r;
385
	pt.paste_flags = paste_flags;
386
	cmd_paste (wbc, &pt);
387
}
388 389 390

/**
 * cmd_shift_rows:
391
 * @wbc : 	The error context.
392 393 394 395 396 397 398 399 400 401 402 403 404
 * @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.
 */

void
405
cmd_shift_rows (WorkbookControl *wbc, Sheet *sheet,
406 407
		int col, int start_row, int end_row, int count)
{
408
	GnmExprRelocateInfo rinfo;
409
	char *desc;
410 411 412 413 414 415 416 417

	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;
	rinfo.origin.end.col = SHEET_MAX_COLS-1;
Jody Goldberg's avatar
typo.  
Jody Goldberg committed
418 419
	if (count > 0)
		rinfo.origin.end.col -= count;
420

421 422 423 424
	desc = g_strdup_printf ((start_row != end_row)
				? _("Shift rows %s")
				: _("Shift row %s"),
				rows_name (start_row, end_row));
425
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
426 427 428 429
}

/**
 * cmd_shift_cols:
430 431 432 433 434 435
 * @wbc: 	The error context.
 * @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
436 437 438 439 440 441 442
 *		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
443
cmd_shift_cols (WorkbookControl *wbc, Sheet *sheet,
444 445
		int start_col, int end_col, int row, int count)
{
446
	GnmExprRelocateInfo rinfo;
447
	char *desc;
448 449 450 451 452 453 454 455

	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;
	rinfo.origin.end.row = SHEET_MAX_ROWS-1;
Jody Goldberg's avatar
typo.  
Jody Goldberg committed
456 457
	if (count > 0)
		rinfo.origin.end.row -= count;
458

459 460 461 462
	desc = g_strdup_printf ((start_col != end_col)
				? _("Shift columns %s")
				: _("Shift column %s"),
				cols_name (start_col, end_col));
463
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
464
}