clipboard.c 16.3 KB
Newer Older
Arturo Espinosa's avatar
Arturo Espinosa committed
1 2 3 4
/*
 * Clipboard.c: Implements the copy/paste operations
 *
 * Author:
5
 *  Miguel de Icaza (miguel@gnu.org)
Arturo Espinosa's avatar
Arturo Espinosa committed
6 7 8
 */
#include <config.h>
#include <gnome.h>
9 10
#include <locale.h>
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
11
#include "gnumeric.h"
12
#include "gnumeric-util.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
13
#include "clipboard.h"
14
#include "eval.h"
15
#include "selection.h"
16
#include "application.h"
17
#include "render-ascii.h"
18
#include "workbook-view.h"
19

20
/*
21
 * Callback information.
22
 *
23
 * Passed through the workbook structure.
24
 */
25 26 27 28 29 30 31
typedef struct {
	Sheet      *dest_sheet;
	CellRegion *region;
	int         dest_col, dest_row;
	int         paste_flags;
} clipboard_paste_closure_t;

32 33 34 35 36 37 38 39
/**
 * paste_cell:
 * @dest_sheet:  The sheet where the pasting will be done
 * @new_cell:    The cell to paste.
 * @target_col:  Column to put the cell into
 * @target_row:  Row to put the cell into.
 * @paste_flags: Bit mask that describes the paste options.
 *
40 41 42
 * Pastes a cell in the spreadsheet
 */
static int
43 44
paste_cell (Sheet *dest_sheet, Cell *new_cell,
	    int target_col, int target_row,
45
	    int paste_flags)
46
{
47 48
	g_return_val_if_fail (target_col < SHEET_MAX_COLS, 0);
	g_return_val_if_fail (target_row < SHEET_MAX_ROWS, 0);
49

50
	sheet_cell_add (dest_sheet, new_cell, target_col, target_row);
51

52 53 54 55 56 57
	if (!(paste_flags & PASTE_FORMULAS)){
		if (new_cell->parsed_node){
			expr_tree_unref (new_cell->parsed_node);
			new_cell->parsed_node = NULL;
		}
	}
58

59
	if (new_cell->parsed_node){
60
		if (paste_flags & PASTE_FORMULAS){
61
			cell_relocate (new_cell, TRUE);
62 63
			cell_content_changed (new_cell);
		}
64
		else
65 66
			cell_make_value (new_cell);
	}
67

68 69
	if (new_cell->value)
		cell_render_value (new_cell);
70

71 72 73 74 75 76 77 78 79 80 81 82
	sheet_redraw_cell_region (dest_sheet,
				  target_col, target_row,
				  target_col, target_row);

	return new_cell->parsed_node != 0;
}

static int
paste_cell_flags (Sheet *dest_sheet, int target_col, int target_row,
		  CellCopy *c_copy, int paste_flags)
{
	if (!(paste_flags & (PASTE_FORMULAS | PASTE_VALUES))){
83 84 85 86 87 88 89 90 91 92
		Range r;

		r.start.col = target_col;
		r.start.row = target_row;
		r.end.col   = target_col;
		r.end.row   = target_row;
		if (c_copy->u.cell.mstyle) {
			mstyle_ref (c_copy->u.cell.mstyle);
			sheet_style_attach (dest_sheet, r, c_copy->u.cell.mstyle);
		}
93 94
	} else {
		Cell *new_cell;
95

96 97 98 99
		if (c_copy->type != CELL_COPY_TYPE_TEXT) {
			Range r;

			new_cell = cell_copy (c_copy->u.cell.cell);
100

101 102 103 104 105 106 107 108 109
			r.start.col = target_col;
			r.start.row = target_row;
			r.end.col   = target_col;
			r.end.row   = target_row;
			if (c_copy->u.cell.mstyle) {
				mstyle_ref (c_copy->u.cell.mstyle);
				sheet_style_attach (dest_sheet, r,
						    c_copy->u.cell.mstyle);
			}
110

111 112
			return paste_cell (
				dest_sheet, new_cell,
113
				target_col, target_row, paste_flags);
114 115 116 117 118 119 120 121 122 123
		} else {
			new_cell = sheet_cell_new (dest_sheet,
						   target_col, target_row);
			cell_set_text (new_cell, c_copy->u.text);
		}
	}

	return 0;
}

124
/**
125 126 127 128 129 130 131 132 133 134
 * do_clipboard_paste_cell_region:
 *
 * region:       a Cell Region that contains information to be pasted
 * dest_col:     initial column where cell pasting takes place
 * dest_row:     initial row where cell pasting takes place
 * paste_width:  boundary for pasting (in columns)
 * paste_height: boundar for pasting (in rows)
 * paste_flags:  controls what gets pasted (see clipboard.h for details
 */
static void
135 136
do_clipboard_paste_cell_region (CmdContext *context,
				CellRegion *region, Sheet *dest_sheet,
137 138 139 140 141 142 143
				int dest_col,       int dest_row,
				int paste_width,    int paste_height,
				int paste_flags)
{
	CellCopyList *l;
	GList *deps;
	int formulas = 0;
144
	int col, row, col_inc, row_inc;
145 146 147

	/* clear the region where we will paste */
	if (paste_flags & (PASTE_VALUES | PASTE_FORMULAS))
148
		sheet_clear_region (context, dest_sheet,
149 150
				    dest_col, dest_row,
				    dest_col + paste_width - 1,
151
				    dest_row + paste_height - 1,
152
				    TRUE);
153 154

	/* If no operations are defined, we clear the area */
155
	if (!(paste_flags & PASTE_OPER_MASK))
156 157 158 159
		sheet_redraw_cell_region (dest_sheet,
					  dest_col, dest_row,
					  dest_col + paste_width - 1,
					  dest_row + paste_height - 1);
160

161
	/* Paste each element */
162
	if (paste_flags & PASTE_TRANSPOSE) {
163 164 165 166 167 168 169 170 171
		col_inc = region->rows;
		row_inc = region->cols;
	} else {
		col_inc = region->cols;
		row_inc = region->rows;
	}

	for (col = 0; col < paste_width; col += col_inc){
		for (row = 0; row < paste_height; row += row_inc){
172 173 174 175
			for (l = region->list; l; l = l->next){
				CellCopy *c_copy = l->data;
				int target_col, target_row;

176 177 178 179 180 181 182
				if (paste_flags & PASTE_TRANSPOSE){
					target_col = col + dest_col + c_copy->row_offset;
					target_row = row + dest_row + c_copy->col_offset;
				} else {
					target_col = col + dest_col + c_copy->col_offset;
					target_row = row + dest_row + c_copy->row_offset;
				}
183

184 185 186 187 188 189 190 191 192 193 194 195
				if (target_col > dest_col + paste_width - 1)
					continue;

				if (target_row > dest_row + paste_height - 1)
					continue;

				formulas |= paste_cell_flags (
					dest_sheet, target_col, target_row,
					c_copy, paste_flags);
			}
		}
	}
196

197 198 199 200 201 202
	deps = region_get_dependencies (
		dest_sheet,
		dest_col, dest_row,
		dest_col + paste_width - 1,
		dest_row + paste_height -1);

203
	if (deps) {
Morten Welinder's avatar
Morten Welinder committed
204
		cell_queue_recalc_list (deps, TRUE);
205 206 207 208 209 210 211 212
		formulas = 1;
	}

	/* Trigger a recompute if required */
	if (formulas)
		workbook_recalc (dest_sheet->workbook);
}

213 214 215 216 217 218
static GList *
new_node (GList *list, char *data, char *p, int col, int row)
{
	CellCopy *c_copy;
	char *text;

Miguel de Icaza's avatar
Miguel de Icaza committed
219 220 221
	/* Eliminate spaces */
	while (*data == ' ' && *data)
		data++;
222

223 224 225
	text = g_malloc (p-data+1);
	text = strncpy (text, data, p-data);
	text [p-data] = 0;
226

227 228 229 230 231
	c_copy = g_new (CellCopy, 1);
	c_copy->type = CELL_COPY_TYPE_TEXT;
	c_copy->col_offset = col;
	c_copy->row_offset = row;
	c_copy->u.text = text;
232

233 234 235
	return g_list_prepend (list, c_copy);
}

236 237 238
/**
 * x_selection_to_cell_region:
 * @data: points to an array of chars are received.
239
 * @len:  The length of the @data buffer as received.
240
 *
241 242 243 244 245 246 247 248 249
 * Creates a CellRegion based on the X selection
 *
 * We use \t, ; and "," as cell separators
 * \n is a line separator
 */
static CellRegion *
x_selection_to_cell_region (char *data, int len)
{
	CellRegion *cr;
250
	int cols = 1, cur_col = 0;
251 252 253 254
	int rows = 0;
	GList *list = NULL;
	char *p = data;

255
	for (;len >= 0; len--, p++){
256
		if (*p == '\t' || *p == ';' || *p == ',' || *p == '\n'){
257 258
			if (p != data)
				list = new_node (list, data, p, cur_col, rows);
259

260
			if (*p == '\n'){
261
				rows++;
262 263
				cur_col = 0;
			} else {
264
				cur_col++;
265 266 267 268
				if (cur_col > cols)
					cols = cur_col;
			}

269
			data = p+1;
270 271 272
		}
	}

273
	/* Handle the remainings */
274
	if (p != data) {
275
		list = new_node (list, data, p, cur_col, rows);
276 277 278 279
		cur_col++;
		if (cur_col > cols)
			cols = cur_col;
	}
280

281
	/* Return the CellRegion */
282
	cr = g_new (CellRegion, 1);
283
	cr->list = list;
284 285
	cr->cols = cols ? cols : 1;
	cr->rows = rows + 1;
286 287 288 289

	return cr;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
290
/**
291
 * sheet_paste_selection:
292 293 294
 *
 */
static void
295 296
sheet_paste_selection (CmdContext *context, Sheet *sheet,
		       CellRegion *content, SheetSelection *ss, clipboard_paste_closure_t *pc)
297 298 299
{
	int        paste_height, paste_width;
	int        end_col, end_row;
300

301
	/* Compute the bigger bounding box (selection u clipboard-region) */
302 303
	if (ss->user.end.col - ss->user.start.col + 1 > content->cols)
		paste_width = ss->user.end.col - ss->user.start.col + 1;
304 305 306
	else
		paste_width = content->cols;

307 308
	if (ss->user.end.row - ss->user.start.row + 1 > content->rows)
		paste_height = ss->user.end.row - ss->user.start.row + 1;
309 310 311
	else
		paste_height = content->rows;

312 313 314 315
	if (pc->dest_col + paste_width > SHEET_MAX_COLS)
		paste_width = SHEET_MAX_COLS - pc->dest_col;
	if (pc->dest_row + paste_height > SHEET_MAX_ROWS)
		paste_height = SHEET_MAX_ROWS - pc->dest_row;
316

317 318
	if (pc->paste_flags & PASTE_TRANSPOSE){
		int t;
319

320 321
		end_col = pc->dest_col + paste_height - 1;
		end_row = pc->dest_row + paste_width - 1;
322

323 324 325 326 327 328 329 330
		/* Swap the paste dimensions for transposing */
		t = paste_height;
		paste_height = paste_width;
		paste_width = t;
	} else {
		end_col = pc->dest_col + paste_width - 1;
		end_row = pc->dest_row + paste_height - 1;
	}
331

332
	/* Do the actual paste operation */
333
	do_clipboard_paste_cell_region (context,
334
		content,      sheet,
335 336 337
		pc->dest_col, pc->dest_row,
		paste_width,  paste_height,
		pc->paste_flags);
338

339 340 341 342 343 344 345 346 347
	{
		Range r;
		r.start.col = pc->dest_col;
		r.end.col   = pc->dest_col + paste_width;
		r.start.row = pc->dest_row;
		r.end.row   = pc->dest_row + paste_height;
		sheet_style_optimize (sheet, r);
	}

348
	sheet_cursor_set (pc->dest_sheet,
349
			  pc->dest_col, pc->dest_row,
350 351 352 353 354 355 356
			  pc->dest_col, pc->dest_row,
			  end_col,      end_row);

	sheet_selection_reset_only (pc->dest_sheet);
	sheet_selection_append (pc->dest_sheet, pc->dest_col, pc->dest_row);
	sheet_selection_extend_to (pc->dest_sheet, end_col, end_row);

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
}

/**
 * x_selection_received:
 *
 * Invoked when the selection has been received by our application.
 * This is triggered by a call we do to gtk_selection_convert.
 */
static void
x_selection_received (GtkWidget *widget, GtkSelectionData *sel, guint time, gpointer data)
{
	SheetSelection *ss;
	Workbook       *wb = data;
	clipboard_paste_closure_t *pc = wb->clipboard_paste_callback_data;
	CellRegion *content;

	ss = pc->dest_sheet->selections->data;
374

375 376 377 378 379 380 381 382
	/* Did X provide any selection? */
	if (sel->length < 0){
		content = pc->region;
		if (!content)
			return;
	} else
		content = x_selection_to_cell_region (sel->data, sel->length);

383 384
	sheet_paste_selection (command_context_gui(),
			       pc->dest_sheet, content, ss, pc);
385

386
	/* Release the resources we used */
387
	if (sel->length >= 0)
388 389 390
		clipboard_release (content);

	/* Remove our used resources */
391 392 393 394
	if (wb->clipboard_paste_callback_data != NULL) {
		g_free (wb->clipboard_paste_callback_data);
		wb->clipboard_paste_callback_data = NULL;
	}
395 396
}

Miguel de Icaza's avatar
Miguel de Icaza committed
397
/**
398 399 400 401
 * x_selection_handler:
 *
 * Callback invoked when another application requests we render the selection.
 */
402
static void
Tom Dyas's avatar
Tom Dyas committed
403
x_selection_handler (GtkWidget *widget, GtkSelectionData *selection_data, guint info, guint time, gpointer data)
404
{
405 406
	gboolean content_needs_free = FALSE;
	CellRegion *clipboard = application_clipboard_contents_get ();
407
	char *rendered_selection;
408

409 410 411 412 413 414 415 416 417
	if (clipboard == NULL) {
		Sheet *sheet = application_clipboard_sheet_get ();
		Range const *a = application_clipboard_area_get ();
		content_needs_free = TRUE;
		clipboard =
		    clipboard_copy_cell_range (sheet,
					       a->start.col, a->start.row,
					       a->end.col,   a->end.row);
	}
418

419 420 421
	g_return_if_fail (clipboard != NULL);

	rendered_selection = cell_region_render_ascii (clipboard);
422

423 424 425
	gtk_selection_data_set (
		selection_data, GDK_SELECTION_TYPE_STRING, 8,
		rendered_selection, strlen (rendered_selection));
Morten Welinder's avatar
Morten Welinder committed
426
	g_free (rendered_selection);
427 428 429

	if (content_needs_free)
		clipboard_release (clipboard);
430 431
}

432
/**
433
 * x_selection_clear:
434
 *
435
 * Callback for the "we lost the X selection" signal
436
 */
437 438
static gint
x_selection_clear (GtkWidget *widget, GdkEventSelection *event, Workbook *wb)
439
{
440
	wb->have_x_selection = FALSE;
441

442
	return TRUE;
443 444
}

445 446
/**
 * x_clipboard_bind_workbook:
447 448
 *
 * Binds the signals related to the X selection to the Workbook
449
 * and initialized the clipboard data structures for the Workbook.
450
 */
451 452 453 454
void
x_clipboard_bind_workbook (Workbook *wb)
{
	wb->have_x_selection = FALSE;
455
	wb->clipboard_paste_callback_data = NULL;
456

457 458
	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_clear_event",
Arturo Espinosa's avatar
Arturo Espinosa committed
459
		GTK_SIGNAL_FUNC (x_selection_clear), wb);
460

461 462
	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_received",
Arturo Espinosa's avatar
Arturo Espinosa committed
463
		GTK_SIGNAL_FUNC (x_selection_received), wb);
Tom Dyas's avatar
Tom Dyas committed
464 465 466

	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_get",
Arturo Espinosa's avatar
Arturo Espinosa committed
467
		GTK_SIGNAL_FUNC (x_selection_handler), NULL);
Tom Dyas's avatar
Tom Dyas committed
468 469

	gtk_selection_add_target (
470
		wb->toplevel,
Tom Dyas's avatar
Tom Dyas committed
471
		GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING, 0);
472 473
}

474
/**
475 476 477 478 479
 * clipboard_export_cell_region:
 *
 * This routine exports a CellRegion to the X selection
 */
static void
480
clipboard_export_cell_region (Workbook *wb)
481 482
{
	wb->have_x_selection = gtk_selection_owner_set (
483
		wb->toplevel,
484 485
		GDK_SELECTION_PRIMARY,
		GDK_CURRENT_TIME);
486

487
}
Arturo Espinosa's avatar
Arturo Espinosa committed
488 489 490 491 492 493

typedef struct {
	int        base_col, base_row;
	CellRegion *r;
} append_cell_closure_t;

494
static Value *
495
clipboard_prepend_cell (Sheet *sheet, int col, int row, Cell *cell, void *user_data)
Arturo Espinosa's avatar
Arturo Espinosa committed
496 497 498 499 500 501
{
	append_cell_closure_t *c = user_data;
	CellCopy *copy;

	copy = g_new (CellCopy, 1);

502
	copy->type = CELL_COPY_TYPE_CELL;
503 504 505
	copy->u.cell.cell = cell_copy (cell);
	/* Horrific inefficiency */
	copy->u.cell.mstyle = sheet_style_compute (sheet, col, row);
506 507
	copy->col_offset    = col - c->base_col;
	copy->row_offset    = row - c->base_row;
508

Arturo Espinosa's avatar
Arturo Espinosa committed
509 510
	c->r->list = g_list_prepend (c->r->list, copy);

511
	return NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
512 513
}

514
/**
515 516 517 518
 * clipboard_copy_cell_range:
 *
 * Entry point to the clipboard copy code
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
519
CellRegion *
520 521
clipboard_copy_cell_range (Sheet *sheet,
			   int start_col, int start_row,
522
			   int end_col, int end_row)
Arturo Espinosa's avatar
Arturo Espinosa committed
523 524
{
	append_cell_closure_t c;
525

526 527 528 529
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	g_return_val_if_fail (start_col <= end_col, NULL);
	g_return_val_if_fail (start_row <= end_row, NULL);
530

531
	c.r = g_new0 (CellRegion, 1);
Arturo Espinosa's avatar
Arturo Espinosa committed
532 533 534

	c.base_col = start_col;
	c.base_row = start_row;
535 536
	c.r->cols = end_col - start_col + 1;
	c.r->rows = end_row - start_row + 1;
537

Arturo Espinosa's avatar
Arturo Espinosa committed
538
	sheet_cell_foreach_range (
539
		sheet, TRUE, start_col, start_row, end_col, end_row,
540 541 542 543
		clipboard_prepend_cell, &c);

	/* reverse the list so that upper left corner is first */
	c.r->list = g_list_reverse (c.r->list);
544

545
	clipboard_export_cell_region (sheet->workbook);
546

547
	return c.r;
Arturo Espinosa's avatar
Arturo Espinosa committed
548 549
}

550 551 552 553
static gboolean
workbook_selection_locator (Workbook *wb, gpointer data)
{
	Workbook **target = data;
554

555 556 557 558 559 560 561 562 563 564
	if (wb->have_x_selection)
		*target = wb;

	return TRUE;
}

static Workbook *
find_local_workbook_with_selection (void)
{
	Workbook *result = NULL;
565

566 567 568 569 570
	workbook_foreach (workbook_selection_locator, &result);

	return result;
}

571 572 573 574 575 576 577 578 579
/**
 * clipboard_paste_region:
 * @region:      A cell region
 * @dest_sheet:  Destination sheet
 * @dest_col:    Column where we should paste the region in dest_sheet
 * @dest_row:    Row where we should paste the region in dest_sheet
 * @paste_flags: Paste flags
 * @time:        Time at which the event happened.
 *
580 581
 * Main entry point for the paste code
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
582
void
583 584
clipboard_paste_region (CmdContext *context,
			CellRegion *region, Sheet *dest_sheet,
585 586
			int dest_col,       int dest_row,
			int paste_flags,    guint32 time)
Arturo Espinosa's avatar
Arturo Espinosa committed
587
{
588
	clipboard_paste_closure_t *data;
589
	Workbook *workbook_holding_selection;
590

591 592 593
	g_return_if_fail (dest_sheet != NULL);
	g_return_if_fail (IS_SHEET (dest_sheet));

594
	if (dest_sheet->workbook->clipboard_paste_callback_data != NULL) {
595
		g_free (dest_sheet->workbook->clipboard_paste_callback_data);
596
		dest_sheet->workbook->clipboard_paste_callback_data = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
597 598
	}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	/*
	 * OK, we dont have the X selection, so we try to get it:
	 * we setup all of the parameters for the callback in
	 * case the X selection fails and we have to fallback to
	 * using our internal selection
	 */
	data = g_new (clipboard_paste_closure_t, 1);
	dest_sheet->workbook->clipboard_paste_callback_data = data;

	data->region       = region;
	data->dest_sheet   = dest_sheet;
	data->dest_col     = dest_col;
	data->dest_row     = dest_row;
	data->paste_flags  = paste_flags;

	/*
	 * If we own the selection, there is no need to ask X for
	 * the selection: we do the paste from our internal buffer.
	 *
	 * This is better as we have all sorts of useful information
	 * to paste from in this case (instead of the simplistic text
	 * we get from X
	 */

623 624 625
	workbook_holding_selection = find_local_workbook_with_selection ();
	if (workbook_holding_selection && region){
		CellRegion *content;
626

627 628
		content = region;

629 630
		sheet_paste_selection (context, dest_sheet,
				       content, dest_sheet->selections->data, data);
631

632 633 634 635 636
		/* Check that this has not already been freed */
		if (workbook_holding_selection->clipboard_paste_callback_data != NULL) {
			workbook_holding_selection->clipboard_paste_callback_data = NULL;
			g_free (data);
		}
637
		return;
638
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
639

640 641 642
	/* Now, trigger a grab of the X selection */
	gtk_selection_convert (
		dest_sheet->workbook->toplevel, GDK_SELECTION_PRIMARY,
643
		GDK_TARGET_STRING, time);
644 645
}

646 647 648
/*
 * Destroys the contents of a CellRegion
 */
649 650 651 652 653 654
void
clipboard_release (CellRegion *region)
{
	CellCopyList *l;

	g_return_if_fail (region != NULL);
655

656
	for (l = region->list; l; l = l->next){
657
		CellCopy *this_cell = l->data;
658

659 660
		if (this_cell->type != CELL_COPY_TYPE_TEXT) {
			/* The cell is not really in the rows or columns */
661 662 663 664 665 666
			this_cell->u.cell.cell->sheet = NULL;
			this_cell->u.cell.cell->row = NULL;
			this_cell->u.cell.cell->col = NULL;
			mstyle_unref (this_cell->u.cell.mstyle);
			this_cell->u.cell.mstyle = NULL;
			cell_destroy (this_cell->u.cell.cell);
667
		} else
668
			g_free (this_cell->u.text);
669 670
		g_free (this_cell);
	}
671

672 673
	g_list_free (region->list);
	g_free (region);
Arturo Espinosa's avatar
Arturo Espinosa committed
674
}
675