cmd-edit.c 10.7 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 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

/*
 * cmd-edit.c: Various commands to be used by the edit menu.
 *
 * Copyright (C) 2000 Jody Goldberg (jgoldberg@home.com)
 *
 * 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
 */
23
#include <config.h>
Jody Goldberg's avatar
Jody Goldberg committed
24
#include "cmd-edit.h"
25
#include "application.h"
26 27
#include "command-context.h"
#include "workbook-control.h"
28
#include "workbook.h"
Jody Goldberg's avatar
Jody Goldberg committed
29 30 31 32
#include "sheet.h"
#include "cell.h"
#include "expr.h"
#include "eval.h"
33
#include "selection.h"
Jody Goldberg's avatar
Jody Goldberg committed
34
#include "parse-util.h"
35 36 37 38 39
#include "ranges.h"
#include "commands.h"
#include "clipboard.h"

#include <gnome.h>
Jody Goldberg's avatar
Jody Goldberg committed
40 41 42 43 44 45 46 47 48 49 50 51 52

/**
 * cmd_select_all:
 * @sheet: The sheet
 *
 * Selects all of the cells in the sheet
 */
void
cmd_select_all (Sheet *sheet)
{
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

53
	sheet_selection_reset (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	sheet_selection_add_range (sheet, 0, 0, 0, 0,
				   SHEET_MAX_COLS-1, SHEET_MAX_ROWS-1);

	sheet_update (sheet);
}

/**
 * cmd_select_cur_row:
 * @sheet: The sheet
 *
 * Selects an entire row
 */
void
cmd_select_cur_row (Sheet *sheet)
{
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

72
	sheet_selection_reset (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
73
	sheet_selection_add_range (sheet,
74 75 76
		sheet->edit_pos.col, sheet->edit_pos.row,
		0, sheet->edit_pos.row,
		SHEET_MAX_COLS-1, sheet->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	sheet_update (sheet);
}

/**
 * cmd_select_cur_col:
 * @sheet: The sheet
 *
 * Selects an entire column
 */
void
cmd_select_cur_col (Sheet *sheet)
{
	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

92
	sheet_selection_reset (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
93
	sheet_selection_add_range (sheet,
94 95 96
		sheet->edit_pos.col, sheet->edit_pos.row,
		sheet->edit_pos.col, 0,
		sheet->edit_pos.col, SHEET_MAX_ROWS-1);
Jody Goldberg's avatar
Jody Goldberg committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	sheet_update (sheet);
}

/**
 * cmd_select_cur_array :
 * @sheet: The sheet
 *
 * if the current cell is part of an array select
 * the entire array.
 */
void
cmd_select_cur_array (Sheet *sheet)
{
	ExprArray const *array;
	int col, row;

	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

116 117
	col = sheet->edit_pos.col;
	row = sheet->edit_pos.row;
Jody Goldberg's avatar
Jody Goldberg committed
118 119 120 121 122
	array = cell_is_array (sheet_cell_get (sheet, col, row));

	if (array == NULL)
		return;

123
	sheet_selection_reset (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	/*
	 * leave the edit cell where it is,
	 * select the entire array.
	 */
	sheet_selection_add_range (sheet, col, row,
				   col - array->x, row - array->y,
				   col - array->x + array->cols,
				   row - array->y + array->rows);

	sheet_update (sheet);
}

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

143
	tmp = cell_a->pos.row - cell_b->pos.row;
Jody Goldberg's avatar
Jody Goldberg committed
144 145
	if (tmp != 0)
		return tmp;
146
	return cell_a->pos.col - cell_b->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

/**
 * cmd_select_cur_depends :
 * @sheet: The sheet
 *
 * Select all cells that depend on the expression in the current cell.
 */
void
cmd_select_cur_depends (Sheet *sheet)
{
	Cell  *cur_cell;
	GList *deps, *ptr = NULL;

	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

164
	cur_cell = sheet_cell_get (sheet,
165 166
				   sheet->edit_pos.col,
				   sheet->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
167 168 169 170
	if (cur_cell == NULL)
		return;

	deps = cell_get_dependencies (cur_cell);
171
	deps = dependent_list_filter (deps, DEPENDENT_CELL);
Jody Goldberg's avatar
Jody Goldberg committed
172 173 174
	if (deps == NULL)
		return;

175
	sheet_selection_reset (sheet);
Jody Goldberg's avatar
Jody Goldberg committed
176 177 178 179

	/* Short circuit */
	if (g_list_length (deps) == 1) {
		Cell *cell = deps->data;
180
		sheet_selection_add (sheet, cell->pos.col, cell->pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
181 182 183 184 185 186 187 188 189
	} 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 ||
190 191
			    cur->end.row != cell->pos.row ||
			    cur->end.col+1 != cell->pos.col) {
Jody Goldberg's avatar
Jody Goldberg committed
192 193 194
				if (cur)
					ptr = g_list_prepend (ptr, cur);
				cur = g_new (Range, 1);
195 196
				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
197
			} else
198
				cur->end.col = cell->pos.col;
Jody Goldberg's avatar
Jody Goldberg committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

			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;
			sheet_selection_add_range (sheet,
						   r->start.col, r->start.row,
						   r->start.col, r->start.row,
						   r->end.col, r->end.row);
			g_free (ptr->data);
			ptr = g_list_remove (ptr, r);
		}
	}
	sheet_update (sheet);
}

/**
 * cmd_select_cur_inputs :
 * @sheet: The sheet
 *
 * Select all cells that are direct potential inputs to the
 * current cell.
 */
void
cmd_select_cur_inputs (Sheet *sheet)
{
	Cell *cell;

	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));

257
	cell = sheet_cell_get (sheet,
258 259
			       sheet->edit_pos.col,
			       sheet->edit_pos.row);
Jody Goldberg's avatar
Jody Goldberg committed
260 261 262 263 264 265 266

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

	/* TODO : finish this */
	sheet_update (sheet);
}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

/**
 * 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
284
cmd_paste (WorkbookControl *wbc, PasteTarget const *pt, guint32 time)
285 286 287 288
{
	CellRegion  *content;
	Range const *src_range;

289
	g_return_if_fail (pt != NULL);
290 291 292 293 294 295 296 297 298 299 300 301 302
	g_return_if_fail (pt->sheet != NULL);
	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 */
		ExprRelocateInfo rinfo;
		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);
303
		int const rows = (src_range->end.row - src_range->start.row);
304 305 306 307 308 309 310 311 312

		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
313
			char *msg = g_strdup_printf (
314 315 316 317 318
				_("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);
319 320
			gnumeric_error_invalid (COMMAND_CONTEXT (wbc),
				_("Unable to paste into selection"), msg);
321 322 323 324 325 326 327 328 329 330
			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;

331 332 333 334 335 336 337 338 339 340
		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
		wb_control_paste_from_selection (wbc, pt, time);
341 342 343 344 345 346 347 348 349 350 351
}

/**
 * cmd_paste_to_selection :
 * @dest_sheet: The sheet into which things should be pasted
 * @flags: special paste flags (eg transpose)
 *
 * Using the current selection as a target
 * Full undo support.
 */
void
352
cmd_paste_to_selection (WorkbookControl *wbc, Sheet *dest_sheet, int paste_flags)
353
{
354
	Range const *r;
355 356
	PasteTarget pt;

357
	if (!(r = selection_first_range (dest_sheet, wbc, _("Paste"))))
358 359
		return;

360
	g_return_if_fail (r !=NULL);
361 362

	pt.sheet = dest_sheet;
363
	pt.range = *r;
364
	pt.paste_flags = paste_flags;
365
	cmd_paste (wbc, &pt, GDK_CURRENT_TIME);
366
}
367 368 369

/**
 * cmd_shift_rows:
370
 * @wbc : 	The error context.
371 372 373 374 375 376 377 378 379 380 381 382 383
 * @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
384
cmd_shift_rows (WorkbookControl *wbc, Sheet *sheet,
385 386 387
		int col, int start_row, int end_row, int count)
{
	ExprRelocateInfo rinfo;
388
	char *desc;
389 390 391 392 393 394 395 396 397

	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;

398 399 400 401
	desc = (start_row != end_row)
		? g_strdup_printf (_("Shift rows %d:%d"), start_row+1, end_row+1)
		: g_strdup_printf (_("Shift row %d"), start_row+1);
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
402 403 404 405
}

/**
 * cmd_shift_cols:
406 407 408 409 410 411
 * @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
412 413 414 415 416 417 418
 *		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
419
cmd_shift_cols (WorkbookControl *wbc, Sheet *sheet,
420 421 422
		int start_col, int end_col, int row, int count)
{
	ExprRelocateInfo rinfo;
423
	char *desc;
424 425 426 427 428 429 430 431 432

	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;

433 434 435 436
	desc = (start_col != end_col)
		? g_strdup_printf (_("Shift columns %s"), cols_name (start_col, end_col))
		: g_strdup_printf (_("Shift column %s"), col_name (start_col));
	cmd_paste_cut (wbc, &rinfo, FALSE, desc);
437 438
}