static void paste_object (GnmPasteTarget const *pt, SheetObject const *src, int left, int top) { SheetObject *dst; SheetObjectAnchor tmp; tmp = *sheet_object_get_anchor (src); if (G_OBJECT_TYPE (src) == CELL_COMMENT_TYPE) { if ((pt->paste_flags & PASTE_COMMENTS) && (pt->paste_flags & PASTE_IGNORE_COMMENTS_AT_ORIGIN && tmp.cell_bound.start.col == 0 && tmp.cell_bound.start.row == 0)) return; } else if (!(pt->paste_flags & PASTE_OBJECTS)) return; if (NULL == (dst = sheet_object_dup (src))) return; if (pt->paste_flags & PASTE_TRANSPOSE) { GnmCellPos origin; origin.col = 0; origin.row = 0; range_transpose (&tmp.cell_bound, pt->sheet, &origin); } range_translate (&tmp.cell_bound, pt->sheet, left, top); sheet_object_set_anchor (dst, &tmp); sheet_object_set_sheet (dst, pt->sheet); g_object_unref (dst); }
static void cb_dup_objects (SheetObject const *src, GnmCellRegion *cr) { SheetObject *dst = sheet_object_dup (src); if (dst != NULL) { SheetObjectAnchor tmp = *sheet_object_get_anchor (src); range_translate (&tmp.cell_bound, sheet_object_get_sheet (src), - cr->base.col, - cr->base.row); sheet_object_set_anchor (dst, &tmp); cr->objects = g_slist_prepend (cr->objects, dst); } }
/** * clipboard_copy_obj: * @sheet: #Sheet * @objects: (element-type SheetObject): #GSList * * Returns a cell region with copies of objects in list. Caller is responsible * for cellregion_unref-ing the result. **/ GnmCellRegion * clipboard_copy_obj (Sheet *sheet, GSList *objects) { SheetObjectAnchor tmp_anchor; SheetObjectAnchor const *anchor; GnmCellRegion *cr; GnmRange *r; GSList *ptr; SheetObject *so; double coords [4]; guint w, h; g_return_val_if_fail (IS_SHEET (sheet), NULL); g_return_val_if_fail (objects != NULL, NULL); cr = gnm_cell_region_new (sheet); for (ptr = objects ; ptr != NULL ; ptr = ptr->next) if (NULL != (so = sheet_object_dup (ptr->data))) { anchor = sheet_object_get_anchor (so); #warning FIXME : This is only used in gnm_sog_write_image /* NOTE #1 : It seems necessary to handle pasting an object that has been removed from * the sheet after being added to the clipboard. it seems like we would need * this sort of information for anything that implements SheetObjectImageableIface **/ sheet_object_anchor_to_pts (anchor, sheet, coords); w = fabs (coords[2] - coords[0]) + 1.5; h = fabs (coords[3] - coords[1]) + 1.5; g_object_set_data (G_OBJECT (so), "pt-width-at-copy", GUINT_TO_POINTER (w)); g_object_set_data (G_OBJECT (so), "pt-height-at-copy", GUINT_TO_POINTER (h)); tmp_anchor = *anchor; r = &tmp_anchor.cell_bound; range_translate (r, sheet, -MIN (r->start.col, r->end.col), -MIN (r->start.row, r->end.row)); sheet_object_set_anchor (so, &tmp_anchor); cr->objects = g_slist_prepend (cr->objects, so); } return cr; }
/** * clipboard_copy_range: * * Entry point to the clipboard copy code */ GnmCellRegion * clipboard_copy_range (Sheet *sheet, GnmRange const *r) { GnmCellRegion *cr; GSList *merged, *ptr; GSList *objects; g_return_val_if_fail (IS_SHEET (sheet), NULL); g_return_val_if_fail (range_is_sane (r), NULL); cr = gnm_cell_region_new (sheet); cr->base = r->start; cr->cols = range_width (r); cr->rows = range_height (r); cr->col_state = colrow_get_states (sheet, TRUE, r->start.col, r->end.col); cr->row_state = colrow_get_states (sheet, FALSE, r->start.row, r->end.row); sheet_foreach_cell_in_range ( sheet, CELL_ITER_IGNORE_NONEXISTENT, r->start.col, r->start.row, r->end.col, r->end.row, (CellIterFunc) cb_clipboard_prepend_cell, cr); objects = sheet_objects_get (sheet, r, G_TYPE_NONE); g_slist_foreach (objects, (GFunc)cb_dup_objects, cr); g_slist_free (objects); cr->styles = sheet_style_get_range (sheet, r); merged = gnm_sheet_merge_get_overlap (sheet, r); for (ptr = merged ; ptr != NULL ; ptr = ptr->next) { GnmRange *tmp = gnm_range_dup (ptr->data); range_translate (tmp, sheet, -r->start.col, -r->start.row); cr->merged = g_slist_prepend (cr->merged, tmp); } g_slist_free (merged); return cr; }
/** * clipboard_paste_region: * @cr: The GnmCellRegion to paste. * @pt: Where to paste the values. * @cc: The context for error handling. * * Pastes the supplied GnmCellRegion (@cr) into the supplied * GnmPasteTarget (@pt). This operation is not undoable. It does not auto grow * the destination if the target is a singleton. This is a simple interface to * paste a region. * * returns : TRUE if there was a problem. **/ gboolean clipboard_paste_region (GnmCellRegion const *cr, GnmPasteTarget const *pt, GOCmdContext *cc) { int repeat_horizontal, repeat_vertical, clearFlags; int dst_cols, dst_rows, src_cols, src_rows; int i, j; GSList *ptr; GnmRange const *r; gboolean has_contents, adjust_merges = TRUE; struct paste_cell_data dat; GnmRange const *merge_src; g_return_val_if_fail (pt != NULL, TRUE); g_return_val_if_fail (cr != NULL, TRUE); /* we do not need any of this fancy stuff when pasting a simple object */ if (cr->cell_content == NULL && cr->styles == NULL && cr->merged == NULL && cr->objects != NULL) { if (pt->paste_flags & (PASTE_COMMENTS | PASTE_OBJECTS)) for (ptr = cr->objects; ptr; ptr = ptr->next) paste_object (pt, ptr->data, pt->range.start.col, pt->range.start.row); return FALSE; } r = &pt->range; dst_cols = range_width (r); dst_rows = range_height (r); src_cols = cr->cols; src_rows = cr->rows; /* If the source is a single cell or a single merge */ /* Treat a target of a single merge specially, don't split the merge */ if ((src_cols == 1 && src_rows == 1) || (g_slist_length (cr->merged) == 1 && (NULL != (merge_src = cr->merged->data)) && range_height (merge_src) == cr->rows && range_width (merge_src) == cr->cols)) { GnmRange const *merge = gnm_sheet_merge_is_corner (pt->sheet, &r->start); if (merge != NULL && range_equal (r, merge)) { dst_cols = dst_rows = 1; adjust_merges = FALSE; src_cols = 1; src_rows = 1; } /* Apparently links do not supercede merges */ } else if (pt->paste_flags & PASTE_LINK) adjust_merges = FALSE; has_contents = pt->paste_flags & (PASTE_CONTENTS|PASTE_AS_VALUES|PASTE_LINK); if (pt->paste_flags & PASTE_TRANSPOSE) { int tmp = src_cols; src_cols = src_rows; src_rows = tmp; } if (cr->not_as_contents && (pt->paste_flags & PASTE_CONTENTS)) { go_cmd_context_error_invalid (cc, _("Unable to paste"), _("Contents can only be pasted by value or by link.")); return TRUE; } /* calculate the tiling */ repeat_horizontal = dst_cols/src_cols; if (repeat_horizontal * src_cols != dst_cols) { char *msg = g_strdup_printf ( _("destination does not have an even multiple of source columns (%d vs %d)\n\n" "Try selecting a single cell or an area of the same shape and size."), dst_cols, src_cols); go_cmd_context_error_invalid (cc, _("Unable to paste"), msg); g_free (msg); return TRUE; } repeat_vertical = dst_rows/src_rows; if (repeat_vertical * src_rows != dst_rows) { char *msg = g_strdup_printf ( _("destination does not have an even multiple of source rows (%d vs %d)\n\n" "Try selecting a single cell or an area of the same shape and size."), dst_rows, src_rows); go_cmd_context_error_invalid (cc, _("Unable to paste"), msg); g_free (msg); return TRUE; } if ((pt->range.start.col + dst_cols) > gnm_sheet_get_max_cols (pt->sheet) || (pt->range.start.row + dst_rows) > gnm_sheet_get_max_rows (pt->sheet)) { go_cmd_context_error_invalid (cc, _("Unable to paste"), _("result passes the sheet boundary")); return TRUE; } clearFlags = 0; /* clear the region where we will paste */ if (has_contents) clearFlags = CLEAR_VALUES | CLEAR_NORESPAN; if (pt->paste_flags & PASTE_COMMENTS) clearFlags |= CLEAR_COMMENTS; /* No need to clear the formats. We will paste over top of these. */ /* if (pt->paste_flags & PASTE_FORMATS) clearFlags |= CLEAR_FORMATS; */ if (pt->paste_flags & (PASTE_OPER_MASK | PASTE_SKIP_BLANKS)) clearFlags = 0; /* remove merged regions even for operations, or blanks */ if (has_contents && adjust_merges) clearFlags |= CLEAR_MERGES; if (clearFlags != 0) { int const dst_col = pt->range.start.col; int const dst_row = pt->range.start.row; sheet_clear_region (pt->sheet, dst_col, dst_row, dst_col + dst_cols - 1, dst_row + dst_rows - 1, clearFlags, cc); } dat.translate_dates = cr->date_conv && !go_date_conv_equal (cr->date_conv, workbook_date_conv (pt->sheet->workbook)); for (i = 0; i < repeat_horizontal ; i++) for (j = 0; j < repeat_vertical ; j++) { int const left = i * src_cols + pt->range.start.col; int const top = j * src_rows + pt->range.start.row; dat.top_left.col = left; dat.top_left.row = top; dat.rinfo.reloc_type = GNM_EXPR_RELOCATE_MOVE_RANGE; dat.rinfo.origin_sheet = dat.rinfo.target_sheet = pt->sheet; if (pt->paste_flags & PASTE_EXPR_LOCAL_RELOCATE) { dat.rinfo.origin.start = cr->base; dat.rinfo.origin.end.col = cr->base.col + cr->cols - 1; dat.rinfo.origin.end.row = cr->base.row + cr->rows - 1; dat.rinfo.col_offset = left - cr->base.col; dat.rinfo.row_offset = top - cr->base.row; } else { dat.rinfo.origin = pt->range; dat.rinfo.col_offset = 0; dat.rinfo.row_offset = 0; } /* Move the styles on here so we get correct formats before recalc */ if (pt->paste_flags & PASTE_FORMATS) { if (pt->paste_flags & PASTE_TRANSPOSE) sheet_style_set_list (pt->sheet, &dat.top_left, cr->styles, (sheet_style_set_list_cb_t) range_transpose, &dat.top_left); else if (pt->paste_flags & PASTE_FLIP_H) { int data = 2 * left + src_cols - 1; sheet_style_set_list (pt->sheet, &dat.top_left, cr->styles, (sheet_style_set_list_cb_t) range_flip_h, &data); } else if (pt->paste_flags & PASTE_FLIP_V) { int data = 2 * top + src_rows - 1; sheet_style_set_list (pt->sheet, &dat.top_left, cr->styles, (sheet_style_set_list_cb_t) range_flip_v, &data); } else sheet_style_set_list (pt->sheet, &dat.top_left, cr->styles, NULL, NULL); } if (has_contents && !(pt->paste_flags & PASTE_DONT_MERGE)) { for (ptr = cr->merged; ptr != NULL ; ptr = ptr->next) { GnmRange tmp = *((GnmRange const *)ptr->data); if (pt->paste_flags & PASTE_TRANSPOSE) { int x; x = tmp.start.col; tmp.start.col = tmp.start.row; tmp.start.row = x; x = tmp.end.col; tmp.end.col = tmp.end.row; tmp.end.row = x; } if (!range_translate (&tmp, pt->sheet, left, top)) gnm_sheet_merge_add (pt->sheet, &tmp, TRUE, cc); } } if (has_contents && (pt->paste_flags & PASTE_LINK)) { paste_link (pt, top, left, cr); continue; } if (has_contents && NULL != cr->cell_content) { dat.pt = pt; dat.cr = cr; g_hash_table_foreach (cr->cell_content, (GHFunc)cb_paste_cell, &dat); } if (pt->paste_flags & (PASTE_COMMENTS | PASTE_OBJECTS)) for (ptr = cr->objects; ptr; ptr = ptr->next) paste_object (pt, ptr->data, left, top); } if (!(pt->paste_flags & PASTE_NO_RECALC)) { if (has_contents) { sheet_region_queue_recalc (pt->sheet, r); sheet_flag_status_update_range (pt->sheet, r); } else sheet_flag_style_update_range (pt->sheet, r); sheet_range_calc_spans (pt->sheet, r, (pt->paste_flags & PASTE_FORMATS) ? GNM_SPANCALC_RE_RENDER : GNM_SPANCALC_RENDER); if (pt->paste_flags & PASTE_UPDATE_ROW_HEIGHT) rows_height_update (pt->sheet, &pt->range, FALSE); sheet_redraw_all (pt->sheet, FALSE); } return FALSE; }
/** * stf_text_to_columns: * @wbc: The control making the request * @cc: * * Main routine, handles importing a file including all dialog mumbo-jumbo **/ void stf_text_to_columns (WorkbookControl *wbc, GOCmdContext *cc) { DialogStfResult_t *dialogresult = NULL; SheetView *sv; Sheet *src_sheet, *target_sheet; GnmRange const *src; GnmRange target; GsfOutput *buf; guint8 const *data; size_t data_len; sv = wb_control_cur_sheet_view (wbc); src_sheet = sv_sheet (sv); src = selection_first_range (sv, cc, _("Text to Columns")); if (src == NULL) return; if (range_width (src) > 1) { go_cmd_context_error (cc, g_error_new (go_error_invalid (), 0, _("Only one column of input data can be parsed at a time"))); return; } /* FIXME : how to do this cleanly ? */ if (!GNM_IS_WBC_GTK (wbc)) return; #warning Add UI for this target_sheet = src_sheet; target = *src; range_translate (&target, target_sheet, 1, 0); buf = gsf_output_memory_new (); sheet_foreach_cell_in_range (src_sheet, CELL_ITER_ALL, src->start.col, src->start.row, src->end.col, src->end.row, (CellIterFunc) &cb_get_content, buf); gsf_output_close (buf); data = gsf_output_memory_get_bytes (GSF_OUTPUT_MEMORY (buf)); data_len = (size_t)gsf_output_size (buf); if (data_len == 0) { go_cmd_context_error_import (GO_CMD_CONTEXT (cc), _("There is no data " "to convert")); } else { dialogresult = stf_dialog (WBC_GTK (wbc), NULL, FALSE, NULL, FALSE, _("Text to Columns"), data, data_len); } if (dialogresult != NULL) { GnmCellRegion *cr = stf_parse_region (dialogresult->parseoptions, dialogresult->text, NULL, target_sheet->workbook); if (cr != NULL) { stf_dialog_result_attach_formats_to_cr (dialogresult, cr); target.end.col = target.start.col + cr->cols - 1; target.end.row = target.start.row + cr->rows - 1; } if (cr == NULL || cmd_text_to_columns (wbc, src, src_sheet, &target, target_sheet, cr)) go_cmd_context_error_import (GO_CMD_CONTEXT (cc), _("Error while trying to " "parse data into sheet")); stf_dialog_result_free (dialogresult); } g_object_unref (buf); }