cmd-edit.c 10.4 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>
Zbigniew Chyla's avatar
New  
Zbigniew Chyla committed
23
#include <gnumeric-i18n.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 32 33
#include "sheet.h"
#include "cell.h"
#include "expr.h"
34
#include "expr-impl.h"
35
#include "dependent.h"
36
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
37
#include "parse-util.h"
38 39 40 41
#include "ranges.h"
#include "commands.h"
#include "clipboard.h"

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

/**
 * cmd_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
Jody Goldberg's avatar
Jody Goldberg committed
51
cmd_select_cur_row (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
52
{
Jody Goldberg's avatar
Jody Goldberg committed
53
	Range const *sel = selection_first_range (sv,  NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
54 55
	if (sel != NULL) {
		Range r = *sel;
Jody Goldberg's avatar
Jody Goldberg committed
56 57 58
		sv_selection_reset (sv);
		sv_selection_add_range (sv,
			sv->edit_pos.col, sv->edit_pos.row,
Jody Goldberg's avatar
Jody Goldberg committed
59
			0, r.start.row, SHEET_MAX_COLS-1, r.end.row);
Jody Goldberg's avatar
Jody Goldberg committed
60
		sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
61
	}
Jody Goldberg's avatar
Jody Goldberg committed
62 63 64 65
}

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

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

Jody Goldberg's avatar
Jody Goldberg committed
97 98 99
	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
100 101 102 103

	if (array == NULL)
		return;

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

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

static gint
cb_compare_deps (gconstpointer a, gconstpointer b)
{
	Cell const *cell_a = a;
	Cell const *cell_b = b;
	int tmp;

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

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

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

Jody Goldberg's avatar
Jody Goldberg committed
151
	g_return_if_fail (IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
152

Jody Goldberg's avatar
Jody Goldberg committed
153 154 155
	cur_cell = sheet_cell_get (sv->sheet,
				   sv->edit_pos.col,
				   sv->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
156 157 158
	if (cur_cell == NULL)
		return;

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 167

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

		/* Merge the sorted list of cells into rows */
		for (deps = g_list_sort (deps, &cb_compare_deps) ; deps ; ) {
			Cell *cell = deps->data;

			if (cur == NULL ||
178 179
			    cur->end.row != cell->pos.row ||
			    cur->end.col+1 != cell->pos.col) {
Jody Goldberg's avatar
Jody Goldberg committed
180 181 182
				if (cur)
					ptr = g_list_prepend (ptr, cur);
				cur = g_new (Range, 1);
183 184
				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
185
			} else
186
				cur->end.col = cell->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

			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 ; ) {
			Range *r1 = deps->data;
			GList *fwd;

			for (fwd = deps->next ; fwd ; ) {
				Range *r2 = fwd->data;

				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) {
			Range *r = ptr->data;
Jody Goldberg's avatar
Jody Goldberg committed
219 220 221 222
			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
223 224 225 226
			g_free (ptr->data);
			ptr = g_list_remove (ptr, r);
		}
	}
Jody Goldberg's avatar
Jody Goldberg committed
227
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
228 229 230 231
}

/**
 * cmd_select_cur_inputs :
Jody Goldberg's avatar
Jody Goldberg committed
232
 * @sv: The sheet
Jody Goldberg's avatar
Jody Goldberg committed
233 234 235 236 237
 *
 * Select all cells that are direct potential inputs to the
 * current cell.
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
238
cmd_select_cur_inputs (SheetView *sv)
Jody Goldberg's avatar
Jody Goldberg committed
239 240 241
{
	Cell *cell;

Jody Goldberg's avatar
Jody Goldberg committed
242
	g_return_if_fail (IS_SHEET_VIEW (sv));
Jody Goldberg's avatar
Jody Goldberg committed
243

Jody Goldberg's avatar
Jody Goldberg committed
244 245 246
	cell = sheet_cell_get (sv->sheet,
			       sv->edit_pos.col,
			       sv->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
247 248 249 250 251

	if (cell == NULL || !cell_has_expr (cell))
		return;

	/* TODO : finish this */
Jody Goldberg's avatar
Jody Goldberg committed
252
	sheet_update (sv->sheet);
Jody Goldberg's avatar
Jody Goldberg committed
253
}
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

/**
 * 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
271
cmd_paste (WorkbookControl *wbc, PasteTarget const *pt)
272 273 274 275
{
	CellRegion  *content;
	Range const *src_range;

276
	g_return_if_fail (pt != NULL);
277 278 279 280 281 282 283
	g_return_if_fail (IS_SHEET (pt->sheet));

	src_range = application_clipboard_area_get ();
	content = application_clipboard_contents_get ();

	if (content == NULL && src_range != NULL) {
		/* Pasting a Cut */
284
		GnmExprRelocateInfo rinfo;
285 286 287 288
		Sheet *src_sheet = application_clipboard_sheet_get ();

		/* Validate the size & shape of the target here. */
		int const cols = (src_range->end.col - src_range->start.col);
289
		int const rows = (src_range->end.row - src_range->start.row);
290 291 292 293 294 295 296 297 298

		Range dst = pt->range;

		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
299
			char *msg = g_strdup_printf (
300 301 302 303 304
				_("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);
305 306
			gnumeric_error_invalid (COMMAND_CONTEXT (wbc),
				_("Unable to paste into selection"), msg);
307 308 309 310 311 312 313 314 315 316
			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;

317 318 319 320 321 322 323 324 325
		if (!cmd_paste_cut (wbc, &rinfo, TRUE, NULL))
			application_clipboard_clear (TRUE);

	/* 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
326
		wb_control_paste_from_selection (wbc, pt);
327 328 329 330
}

/**
 * cmd_paste_to_selection :
Jody Goldberg's avatar
Jody Goldberg committed
331
 * @dest_sv: The sheet into which things should be pasted
332 333 334 335 336 337
 * @flags: special paste flags (eg transpose)
 *
 * Using the current selection as a target
 * Full undo support.
 */
void
Jody Goldberg's avatar
Jody Goldberg committed
338
cmd_paste_to_selection (WorkbookControl *wbc, SheetView *dest_sv, int paste_flags)
339
{
340
	Range const *r;
341 342
	PasteTarget pt;

343
	if (!(r = selection_first_range (dest_sv, COMMAND_CONTEXT (wbc), _("Paste"))))
344 345
		return;

346
	g_return_if_fail (r !=NULL);
347

Jody Goldberg's avatar
Jody Goldberg committed
348
	pt.sheet = dest_sv->sheet;
349
	pt.range = *r;
350
	pt.paste_flags = paste_flags;
351
	cmd_paste (wbc, &pt);
352
}
353 354 355

/**
 * cmd_shift_rows:
356
 * @wbc : 	The error context.
357 358 359 360 361 362 363 364 365 366 367 368 369
 * @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
370
cmd_shift_rows (WorkbookControl *wbc, Sheet *sheet,
371 372
		int col, int start_row, int end_row, int count)
{
373
	GnmExprRelocateInfo rinfo;
374
	char *desc;
375 376 377 378 379 380 381 382

	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
383 384
	if (count > 0)
		rinfo.origin.end.col -= count;
385

386 387 388 389
	desc = g_strdup_printf ((start_row != end_row)
				? _("Shift rows %s")
				: _("Shift row %s"),
				rows_name (start_row, end_row));
390
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
391 392 393 394
}

/**
 * cmd_shift_cols:
395 396 397 398 399 400
 * @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
401 402 403 404 405 406 407
 *		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
408
cmd_shift_cols (WorkbookControl *wbc, Sheet *sheet,
409 410
		int start_col, int end_col, int row, int count)
{
411
	GnmExprRelocateInfo rinfo;
412
	char *desc;
413 414 415 416 417 418 419 420

	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
421 422
	if (count > 0)
		rinfo.origin.end.row -= count;
423

424 425 426 427
	desc = g_strdup_printf ((start_col != end_col)
				? _("Shift columns %s")
				: _("Shift column %s"),
				cols_name (start_col, end_col));
428
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
429
}