clipboard.c 12.8 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
 *
Miguel de Icaza's avatar
Miguel de Icaza committed
7 8 9
 * FIXME:
 *    When the new GTK+ and gnome-libs is released remove all of the
 *    compatibility code
Arturo Espinosa's avatar
Arturo Espinosa committed
10 11 12
 */
#include <config.h>
#include <gnome.h>
13 14
#include <locale.h>
#include <string.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
15
#include "gnumeric.h"
16
#include "gnumeric-util.h"
Arturo Espinosa's avatar
Arturo Espinosa committed
17
#include "clipboard.h"
18
#include "eval.h"
19 20
#include "render-ascii.h"

21
/*
22
 * Callback information.
23
 *
24
 * Passed trough the workbook structure.
25
 */
26 27 28 29 30 31 32 33 34 35 36 37
typedef struct {
	Sheet      *dest_sheet;
	CellRegion *region;
	int         dest_col, dest_row;
	int         paste_flags;
} clipboard_paste_closure_t;

/*
 * Pastes a cell in the spreadsheet
 */
static int
paste_cell (Sheet *dest_sheet, Cell *new_cell, int target_col, int target_row, int paste_flags)
38
{
39 40 41 42 43 44 45 46 47 48 49
	sheet_cell_add (dest_sheet, new_cell, target_col, target_row);
	
	if (!(paste_flags & PASTE_FORMULAS)){
		if (new_cell->parsed_node){
			expr_tree_unref (new_cell->parsed_node);
			new_cell->parsed_node = NULL;
		}
	}
	
	if (new_cell->parsed_node){
		if (paste_flags & PASTE_FORMULAS)
50
			cell_relocate (new_cell, target_col, target_row);
51 52 53
		else 
			cell_make_value (new_cell);
	}
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	cell_render_value (new_cell);
	
	if (!(paste_flags & PASTE_FORMULAS)){
		string_unref (new_cell->entered_text);
		new_cell->entered_text = string_ref (new_cell->text);
	}

	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))){
		Cell *cell;
		
		cell = sheet_cell_get (dest_sheet,
				       target_col,
				       target_row);
		if (cell && c_copy->u.cell)
			cell_set_style (cell, c_copy->u.cell->style);
	} else {
		Cell *new_cell;
		
		if (c_copy->type == CELL_COPY_TYPE_CELL){
			new_cell = cell_copy (c_copy->u.cell);
			
			return paste_cell (
				dest_sheet, new_cell,
				target_col, target_row, paste_flags);
		} else {
			new_cell = sheet_cell_new (dest_sheet,
						   target_col, target_row);
			cell_set_text (new_cell, c_copy->u.text);
		}
	}

	return 0;
}

/*
 * 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
do_clipboard_paste_cell_region (CellRegion *region, Sheet *dest_sheet,
				int dest_col,       int dest_row,
				int paste_width,    int paste_height,
				int paste_flags)
{
	CellCopyList *l;
	GList *deps;
	int formulas = 0;
	int col, row;

	/* clear the region where we will paste */
	if (paste_flags & (PASTE_VALUES | PASTE_FORMULAS))
		sheet_clear_region (dest_sheet,
				    dest_col, dest_row,
				    dest_col + paste_width - 1,
				    dest_row + paste_height - 1);

	/* If no operations are defined, we clear the area */
129
	if (!(paste_flags & PASTE_OPER_MASK))
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		sheet_redraw_cell_region (dest_sheet,
					  dest_col, dest_row,
					  dest_col + paste_width - 1,
					  dest_row + paste_height - 1);
	
	/* Paste each element */
	for (col = 0; col < paste_width; col += region->cols){
		for (row = 0; row < paste_height; row += region->rows){
			for (l = region->list; l; l = l->next){
				CellCopy *c_copy = l->data;
				int target_col, target_row;
				
				target_col = col + dest_col + c_copy->col_offset;
				target_row = row + dest_row + c_copy->row_offset;

				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);
			}
		}
	}
	
	deps = region_get_dependencies (
		dest_sheet,
		dest_col, dest_row,
		dest_col + paste_width - 1,
		dest_row + paste_height -1);

	if (deps){
		cell_queue_recalc_list (deps);
		formulas = 1;
	}

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

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static GList *
new_node (GList *list, char *data, char *p, int col, int row)
{
	CellCopy *c_copy;
	char *text;

	text = g_malloc (p-data+1);
	text = strncpy (text, data, p-data);
	text [p-data] = 0;
	
	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;
	
	return g_list_prepend (list, c_copy);
}

193 194 195 196 197 198 199 200 201 202
/*
 * 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;
203
	int cols = 1, cur_col = 0;
204 205 206 207
	int rows = 0;
	GList *list = NULL;
	char *p = data;

208
	for (;len >= 0; len--, p++){
209
		if (*p == '\t' || *p == ';' || *p == ',' || *p == '\n'){
210 211
			if (p != data)
				list = new_node (list, data, p, cur_col, rows);
212
			
213
			if (*p == '\n'){
214
				rows++;
215 216
				cur_col = 0;
			} else {
217 218 219 220 221
				if (cur_col > cols)
					cols = cur_col;
				cur_col++;
			}

222
			data = p+1;
223 224 225
		}
	}

226 227 228 229
	/* Handle the remainings */
	if (p != data)
		list = new_node (list, data, p, cur_col, rows);

230 231 232
	/* Return the CellRegion */
	cr = g_new (CellRegion, 1);	
	cr->list = list;
233 234
	cr->cols = cols ? cols : 1;
	cr->rows = rows + 1;
235 236 237 238

	return cr;
}

Miguel de Icaza's avatar
Miguel de Icaza committed
239 240
/**
 * x_selection_received:
241 242 243 244 245
 *
 * Invoked when the selection has been received by our application.
 * This is triggered by a call we do to gtk_selection_convert.
 */
static void
Miguel de Icaza's avatar
Miguel de Icaza committed
246
#ifdef HAVE_GTK_SELECTION_ADD_TARGET
Tom Dyas's avatar
Tom Dyas committed
247
x_selection_received (GtkWidget *widget, GtkSelectionData *sel, guint time, gpointer data)
Miguel de Icaza's avatar
Miguel de Icaza committed
248 249 250
#else
x_selection_received (GtkWidget *widget, GtkSelectionData *sel, gpointer data)
#endif
251 252 253 254 255 256 257
{
	SheetSelection *ss;
	Workbook       *wb = data;
	clipboard_paste_closure_t *pc = wb->clipboard_paste_callback_data;
	CellRegion *content;
	int        paste_height, paste_width;
	int        end_col, end_row;
258

259 260 261
	ss = pc->dest_sheet->selections->data;
	
	/* Did X provide any selection? */
262
	if (sel->length < 0){
263
		content = pc->region;
264 265 266
		if (!content)
			return;
	} else
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		content = x_selection_to_cell_region (sel->data, sel->length);

	/* Compute the bigger bounding box (selection u clipboard-region) */
	if (ss->end_col - ss->start_col + 1 > content->cols)
		paste_width = ss->end_col - ss->start_col + 1;
	else
		paste_width = content->cols;

	if (ss->end_row - ss->start_row + 1 > content->rows)
		paste_height = ss->end_row - ss->start_row + 1;
	else
		paste_height = content->rows;

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

	/* Do the actual paste operation */
	do_clipboard_paste_cell_region (
		content,      pc->dest_sheet,
		pc->dest_col, pc->dest_row,
		paste_width,  paste_height,
		pc->paste_flags);
	
	sheet_cursor_set (pc->dest_sheet,
291
			  pc->dest_col, pc->dest_row,
292 293 294 295 296 297 298 299
			  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);

	/* Release the resources we used */
300
	if (sel->length >= 0)
301 302 303 304 305
		clipboard_release (content);

	/* Remove our used resources */
	g_free (wb->clipboard_paste_callback_data);
	wb->clipboard_paste_callback_data = NULL;
306 307
}

Miguel de Icaza's avatar
Miguel de Icaza committed
308
/**
309 310 311 312
 * x_selection_handler:
 *
 * Callback invoked when another application requests we render the selection.
 */
313
static void
Miguel de Icaza's avatar
Miguel de Icaza committed
314
#ifdef HAVE_GTK_SELECTION_ADD_TARGET
Tom Dyas's avatar
Tom Dyas committed
315
x_selection_handler (GtkWidget *widget, GtkSelectionData *selection_data, guint info, guint time, gpointer data)
Miguel de Icaza's avatar
Miguel de Icaza committed
316 317 318
#else
x_selection_handler (GtkWidget *widget, GtkSelectionData *selection_data, gpointer data)
#endif
319
{
320
	Workbook *wb = (Workbook *) data;
321 322 323 324 325 326 327 328 329 330 331
	char *rendered_selection;
	
	g_assert (wb->clipboard_contents);

	rendered_selection = cell_region_render_ascii (wb->clipboard_contents);
	
	gtk_selection_data_set (
		selection_data, GDK_SELECTION_TYPE_STRING, 8,
		rendered_selection, strlen (rendered_selection));
}

332
/*
333
 * x_selection_clear:
334
 *
335
 * Callback for the "we lost the X selection" signal
336
 */
337 338
static gint
x_selection_clear (GtkWidget *widget, GdkEventSelection *event, Workbook *wb)
339
{
340
	wb->have_x_selection = FALSE;
341

342
	return TRUE;
343 344 345 346 347 348 349
}

/*
 * x_clipboard_bind_workbook
 *
 * Binds the signals related to the X selection to the Workbook
 */
350 351 352 353 354 355 356 357 358
void
x_clipboard_bind_workbook (Workbook *wb)
{
	wb->have_x_selection = FALSE;
	
	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_clear_event",
		GTK_SIGNAL_FUNC(x_selection_clear), wb);

359 360 361
	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_received",
		GTK_SIGNAL_FUNC(x_selection_received), wb);
Tom Dyas's avatar
Tom Dyas committed
362 363 364 365 366

	gtk_signal_connect (
		GTK_OBJECT (wb->toplevel), "selection_get",
		GTK_SIGNAL_FUNC(x_selection_handler), wb);

Miguel de Icaza's avatar
Miguel de Icaza committed
367
#ifdef HAVE_GTK_SELECTION_ADD_TARGET
Tom Dyas's avatar
Tom Dyas committed
368
	gtk_selection_add_target (
369
		wb->toplevel,
Tom Dyas's avatar
Tom Dyas committed
370
		GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING, 0);
Miguel de Icaza's avatar
Miguel de Icaza committed
371 372 373 374 375 376
#else
	gtk_selection_add_handler (
		wb->toplevel,
		GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING,
		x_selection_handler, wb);
#endif
377 378 379 380 381 382 383 384
}

/*
 * clipboard_export_cell_region:
 *
 * This routine exports a CellRegion to the X selection
 */
static void
385
clipboard_export_cell_region (Workbook *wb)
386 387
{
	wb->have_x_selection = gtk_selection_owner_set (
388
		wb->toplevel,
389 390
		GDK_SELECTION_PRIMARY,
		GDK_CURRENT_TIME);
391

392
}
Arturo Espinosa's avatar
Arturo Espinosa committed
393 394 395 396 397 398

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

399
static int
Arturo Espinosa's avatar
Arturo Espinosa committed
400 401 402 403 404 405 406
clipboard_append_cell (Sheet *sheet, int col, int row, Cell *cell, void *user_data)
{
	append_cell_closure_t *c = user_data;
	CellCopy *copy;

	copy = g_new (CellCopy, 1);

407 408
	copy->type = CELL_COPY_TYPE_CELL;
	copy->u.cell = cell_copy (cell);
Arturo Espinosa's avatar
Arturo Espinosa committed
409
	copy->col_offset  = col - c->base_col;
410
	copy->row_offset  = row - c->base_row;
Arturo Espinosa's avatar
Arturo Espinosa committed
411 412
	
	/* Now clear the traces and dependencies on the copied Cell */
413 414 415
	copy->u.cell->col   = NULL;
	copy->u.cell->row   = NULL;
	copy->u.cell->sheet = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
416 417 418 419 420 421
	
	c->r->list = g_list_prepend (c->r->list, copy);

	return TRUE;
}

422 423 424 425 426
/*
 * clipboard_copy_cell_range:
 *
 * Entry point to the clipboard copy code
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
427 428 429 430
CellRegion *
clipboard_copy_cell_range (Sheet *sheet, int start_col, int start_row, int end_col, int end_row)
{
	append_cell_closure_t c;
431
	
432 433 434 435
	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);
436

437
	c.r = g_new0 (CellRegion, 1);
Arturo Espinosa's avatar
Arturo Espinosa committed
438 439 440

	c.base_col = start_col;
	c.base_row = start_row;
441 442
	c.r->cols = end_col - start_col + 1;
	c.r->rows = end_row - start_row + 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
443 444 445
	
	sheet_cell_foreach_range (
		sheet, 1, start_col, start_row, end_col, end_row,
446 447
		clipboard_append_cell, &c);

448
	clipboard_export_cell_region (sheet->workbook);
449
	
450
	return c.r;
Arturo Espinosa's avatar
Arturo Espinosa committed
451 452
}

453 454 455
/*
 * Main entry point for the paste code
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
456
void
Arturo Espinosa's avatar
Arturo Espinosa committed
457
clipboard_paste_region (CellRegion *region, Sheet *dest_sheet,
458 459
			int dest_col,       int dest_row,
			int paste_flags,    guint32 time)
Arturo Espinosa's avatar
Arturo Espinosa committed
460
{
461
	clipboard_paste_closure_t *data;
462
	
463 464 465
	g_return_if_fail (dest_sheet != NULL);
	g_return_if_fail (IS_SHEET (dest_sheet));

466 467 468
	if (dest_sheet->workbook->clipboard_paste_callback_data){
		g_free (dest_sheet->workbook->clipboard_paste_callback_data);	
		dest_sheet->workbook->clipboard_paste_callback_data = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
469 470
	}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	/*
	 * 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
	 */
	if (dest_sheet->workbook->have_x_selection){
		GtkSelectionData sel;

		sel.length = -1;
Miguel de Icaza's avatar
Miguel de Icaza committed
498
#ifdef HAVE_GTK_SELECTION_ADD_TARGET
499
		x_selection_received (dest_sheet->workbook->toplevel, &sel,
Tom Dyas's avatar
Tom Dyas committed
500
				      0, dest_sheet->workbook);
Miguel de Icaza's avatar
Miguel de Icaza committed
501 502 503 504
#else
                x_selection_received (dest_sheet->workbook->toplevel, &sel,
				      dest_sheet->workbook);
#endif
505
		return;
506
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
507

508 509 510
	/* Now, trigger a grab of the X selection */
	gtk_selection_convert (
		dest_sheet->workbook->toplevel, GDK_SELECTION_PRIMARY,
511
		GDK_TARGET_STRING, time);
512 513
}

514 515 516
/*
 * Destroys the contents of a CellRegion
 */
517 518 519 520 521 522 523
void
clipboard_release (CellRegion *region)
{
	CellCopyList *l;

	g_return_if_fail (region != NULL);
	
524
	for (l = region->list; l; l = l->next){
525
		CellCopy *this_cell = l->data;
526 527 528 529 530
		
		if (this_cell->type == CELL_COPY_TYPE_CELL)
			cell_destroy (this_cell->u.cell);
		else
			g_free (this_cell->u.text);
531 532
		g_free (this_cell);
	}
533

534 535
	g_list_free (region->list);
	g_free (region);
Arturo Espinosa's avatar
Arturo Espinosa committed
536
}
537

538