From: mental Date: Wed, 26 Mar 2008 03:46:52 +0000 (+0000) Subject: system clipboard support (bug #170185) from Chris Kosiński X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=07916b4c23e70df45383ea8348cf817c1d029083;p=inkscape.git system clipboard support (bug #170185) from Chris Kosiński --- diff --git a/src/display/canvas-axonomgrid.cpp b/src/display/canvas-axonomgrid.cpp index f48648d18..78fe6f38b 100644 --- a/src/display/canvas-axonomgrid.cpp +++ b/src/display/canvas-axonomgrid.cpp @@ -411,7 +411,7 @@ _wr.setUpdating (true); "empcolor", "empopacity", _wr, repr, doc)); Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger( - _("_Major grid line every:"), _(""), _("lines"), "empspacing", _wr, repr, doc ) ); + _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc ) ); _rsu_ox->setDigits(4); _rsu_ox->setIncrements(0.1, 1.0); diff --git a/src/display/canvas-grid.cpp b/src/display/canvas-grid.cpp index 6ac2b4f0e..7aebe0095 100644 --- a/src/display/canvas-grid.cpp +++ b/src/display/canvas-grid.cpp @@ -675,7 +675,7 @@ CanvasXYGrid::newSpecificWidget() _wr, repr, doc)); Inkscape::UI::Widget::RegisteredSuffixedInteger *_rsi = Gtk::manage( new Inkscape::UI::Widget::RegisteredSuffixedInteger( - _("_Major grid line every:"), _(""), _("lines"), "empspacing", _wr, repr, doc) ); + _("_Major grid line every:"), "", _("lines"), "empspacing", _wr, repr, doc) ); table->set_spacings(2); diff --git a/src/dropper-context.cpp b/src/dropper-context.cpp index 66e8a40fb..5a1178040 100644 --- a/src/dropper-context.cpp +++ b/src/dropper-context.cpp @@ -141,79 +141,18 @@ static void sp_dropper_context_finish(SPEventContext *ec) /** - * Copies the current context color to the clipboard. + * Returns the current dropper context color. */ -void sp_dropper_context_copy(SPEventContext *ec) +guint32 sp_dropper_context_get_color(SPEventContext *ec) { SPDropperContext *dc = SP_DROPPER_CONTEXT(ec); - - guint32 const c32 = SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B, dc->alpha); - + int pick = prefs_get_int_attribute("tools.dropper", "pick", SP_DROPPER_PICK_VISIBLE); - int setalpha = prefs_get_int_attribute("tools.dropper", "setalpha", 1); - - gchar c[64]; - g_snprintf(c, 64, "%06x%02x", c32 >> 8, - (pick == SP_DROPPER_PICK_ACTUAL && setalpha)? SP_COLOR_F_TO_U(dc->alpha) : 255); - - Glib::ustring text; - text += c; - if (!text.empty()) - { - Glib::RefPtr refClipboard = - Gtk::Clipboard::get(); - refClipboard->set_text(text); - } -} - -/** - * Makes a copy of the current desktop color to the clipboard. - */ -void sp_dropper_c32_color_copy(guint32 c32) -{ - int const pick = prefs_get_int_attribute("tools.dropper", "pick", - SP_DROPPER_PICK_VISIBLE); - - gchar c[64]; - g_snprintf(c, 64, "%06x%02x", c32 >> 8, - pick == SP_DROPPER_PICK_ACTUAL? SP_RGBA32_A_U(c32) : 255); - - Glib::ustring text; - text += c; - if (!text.empty()) { - Glib::RefPtr refClipboard = Gtk::Clipboard::get(); - refClipboard->set_text(text); - } -} - - -/** - * Makes a copy of the current color as a hex value. This should always compute - * the current color without alpha, but the on-screen representation. - */ -void sp_dropper_c32_color_copy_hex(guint32 c32) -{ - /* - int pick = prefs_get_int_attribute ("tools.dropper", "pick", - SP_DROPPER_PICK_VISIBLE); - - if ( pick == SP_DROPPER_PICK_ACTUAL ) - ; // process c32 so that it computes against page - // else just can cut off that last 2 hex digits.... - - */ - - gchar c[48]; - g_snprintf(c, 48, "%06x", c32 >> 8); - - Glib::ustring text; - text += c; - if (!text.empty()) { - Glib::RefPtr refClipboard = Gtk::Clipboard::get(); - refClipboard->set_text(text); - } + + return SP_RGBA32_F_COMPOSE(dc->R, dc->G, dc->B, + (pick == SP_DROPPER_PICK_ACTUAL && setalpha) ? dc->alpha : 1.0); } diff --git a/src/dropper-context.h b/src/dropper-context.h index f6fdd2523..80b25ad26 100644 --- a/src/dropper-context.h +++ b/src/dropper-context.h @@ -49,10 +49,6 @@ struct SPDropperContextClass { GType sp_dropper_context_get_type (void); -void sp_dropper_context_copy (SPEventContext *ec); - -void sp_dropper_c32_color_copy (guint32 c32); - -void sp_dropper_c32_color_copy_hex (guint32 c32); +guint32 sp_dropper_context_get_color(SPEventContext *ec); #endif diff --git a/src/extension/internal/eps-out.cpp b/src/extension/internal/eps-out.cpp index e122ad4ad..4fd5541c2 100644 --- a/src/extension/internal/eps-out.cpp +++ b/src/extension/internal/eps-out.cpp @@ -93,7 +93,7 @@ EpsOutput::init (void) "FALSE\n" "\n" ".eps\n" - "image/x-e-postscript\n" + "image/x-eps\n" "" N_("Encapsulated Postscript (*.eps)") "\n" "" N_("Encapsulated Postscript File") "\n" "\n" diff --git a/src/extension/internal/latex-pstricks-out.cpp b/src/extension/internal/latex-pstricks-out.cpp index eaab462c7..48eb475ba 100644 --- a/src/extension/internal/latex-pstricks-out.cpp +++ b/src/extension/internal/latex-pstricks-out.cpp @@ -107,7 +107,7 @@ LatexOutput::init (void) "org.inkscape.output.latex\n" "\n" ".tex\n" - "text/plain\n" + "text/x-tex\n" "" N_("LaTeX With PSTricks macros (*.tex)") "\n" "" N_("LaTeX PSTricks File") "\n" "\n" diff --git a/src/extension/internal/ps-out.cpp b/src/extension/internal/ps-out.cpp index d59e246b0..8ccd3b593 100644 --- a/src/extension/internal/ps-out.cpp +++ b/src/extension/internal/ps-out.cpp @@ -85,7 +85,7 @@ PsOutput::init (void) "false\n" "\n" ".ps\n" - "image/x-postscript\n" + "application/postscript\n" "" N_("PostScript (*.ps)") "\n" "" N_("PostScript File") "\n" "\n" diff --git a/src/extension/internal/svg.cpp b/src/extension/internal/svg.cpp index 182b6f096..47f1ce31b 100644 --- a/src/extension/internal/svg.cpp +++ b/src/extension/internal/svg.cpp @@ -56,7 +56,7 @@ Svg::init(void) "" SP_MODULE_KEY_INPUT_SVG "\n" "\n" ".svg\n" - "image/x-svg\n" + "image/svg+xml\n" "" N_("Scalable Vector Graphic (*.svg)") "\n" "" N_("Inkscape native file format and W3C standard") "\n" "" SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE "\n" @@ -70,7 +70,7 @@ Svg::init(void) "" SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE "\n" "\n" ".svg\n" - "image/x-svg\n" + "image/x-inkscape-svg\n" "" N_("Inkscape SVG (*.svg)") "\n" "" N_("SVG format with Inkscape extensions") "\n" "FALSE\n" @@ -84,7 +84,7 @@ Svg::init(void) "" SP_MODULE_KEY_OUTPUT_SVG "\n" "\n" ".svg\n" - "image/x-svg\n" + "image/svg+xml\n" "" N_("Plain SVG (*.svg)") "\n" "" N_("Scalable Vector Graphics format as defined by the W3C") "\n" "\n" diff --git a/src/extension/internal/svgz.cpp b/src/extension/internal/svgz.cpp index 49d37f082..98d3fcfb2 100644 --- a/src/extension/internal/svgz.cpp +++ b/src/extension/internal/svgz.cpp @@ -49,7 +49,7 @@ Svgz::init(void) "" SP_MODULE_KEY_INPUT_SVG "\n" "\n" ".svgz\n" - "image/x-svgz\n" + "image/svg+xml-compressed\n" "" N_("Compressed Inkscape SVG (*.svgz)") "\n" "" N_("SVG file format compressed with GZip") "\n" "" SP_MODULE_KEY_OUTPUT_SVGZ_INKSCAPE "\n" @@ -63,7 +63,7 @@ Svgz::init(void) "" SP_MODULE_KEY_OUTPUT_SVGZ_INKSCAPE "\n" "\n" ".svgz\n" - "image/x-svgz\n" + "image/x-inkscape-svg-compressed\n" "" N_("Compressed Inkscape SVG (*.svgz)") "\n" "" N_("Inkscape's native file format compressed with GZip") "\n" "FALSE\n" @@ -77,7 +77,7 @@ Svgz::init(void) "" SP_MODULE_KEY_OUTPUT_SVGZ "\n" "\n" ".svgz\n" - "image/x-svgz\n" + "image/svg+xml-compressed\n" "" N_("Compressed plain SVG (*.svgz)") "\n" "" N_("Scalable Vector Graphics format compressed with GZip") "\n" "\n" diff --git a/src/gradient-drag.cpp b/src/gradient-drag.cpp index 874832f54..6c960ed28 100644 --- a/src/gradient-drag.cpp +++ b/src/gradient-drag.cpp @@ -249,11 +249,9 @@ gr_drag_style_set (const SPCSSAttr *css, gpointer data) return true; } -bool -GrDrag::copy() +guint32 GrDrag::getColor() { - if (!selected) - return false; + if (!selected) return 0; float cf[4]; cf[0] = cf[1] = cf[2] = cf[3] = 0; @@ -282,28 +280,7 @@ GrDrag::copy() cf[3] /= count; } - guint32 const c32 = SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]); - gchar c[64]; - - SPCSSAttr *css = sp_repr_css_attr_new (); - g_snprintf(c, 64, "#%06x", c32 >> 8); - sp_repr_css_set_property (css, "fill", c); - Inkscape::CSSOStringStream os; - os << cf[3]; - sp_repr_css_set_property (css, "opacity", os.str().c_str()); - sp_set_style_clipboard (css); - - g_snprintf(c, 64, "%06x%02x", c32 >> 8, c32 & 0x000000ff); - Glib::ustring text; - text += c; - if (!text.empty()) - { - Glib::RefPtr refClipboard = - Gtk::Clipboard::get(); - refClipboard->set_text(text); - } - - return true; + return SP_RGBA32_F_COMPOSE(cf[0], cf[1], cf[2], cf[3]); } SPStop * diff --git a/src/gradient-drag.h b/src/gradient-drag.h index 2d52da04b..81713e0ee 100644 --- a/src/gradient-drag.h +++ b/src/gradient-drag.h @@ -131,7 +131,7 @@ public: // FIXME: make more of this private! void deleteSelected (bool just_one = false); - bool copy (); + guint32 getColor(); bool keep_selection; diff --git a/src/live_effects/parameter/path.cpp b/src/live_effects/parameter/path.cpp index ad28dcb47..db4102635 100644 --- a/src/live_effects/parameter/path.cpp +++ b/src/live_effects/parameter/path.cpp @@ -32,6 +32,8 @@ #include "desktop-handles.h" #include "selection.h" #include "nodepath.h" +// clipboard support +#include "ui/clipboard.h" namespace Inkscape { @@ -244,38 +246,29 @@ PathParam::on_edit_button_click() void PathParam::on_paste_button_click() { - // check if something is in the clipboard - GSList * clipboard = sp_selection_get_clipboard(); - if (clipboard == NULL || clipboard->data == NULL) { - SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard.")); - return; - } + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + Glib::ustring svgd = cm->getPathParameter(); + + if (svgd == "") return; - Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data; - if (!strcmp (repr->name(), "svg:path")) { - const char * svgd = repr->attribute("d"); - if (svgd) { - if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD - SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE, - _("This effect does not support arcs yet, try to convert to path.") ); - return; - } else { - param_write_to_repr(svgd); - signal_path_pasted.emit(); - sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, - _("Paste path parameter")); - } - } - } else { - SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path.")); + // Temporary hack until 2Geom supports arcs in SVGD + if (svgd.find('A') != Glib::ustring::npos) { + SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE, + _("This effect does not support arcs yet, try to convert to path.") ); return; + } else { + param_write_to_repr(svgd.data()); + signal_path_pasted.emit(); + sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, + _("Paste path parameter")); } } void PathParam::on_copy_button_click() { - sp_selection_copy_lpe_pathparam(this); + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + cm->copyPathParameter(this); } } /* namespace LivePathEffect */ diff --git a/src/selection-chemistry.cpp b/src/selection-chemistry.cpp index e40746061..81c4bc490 100644 --- a/src/selection-chemistry.cpp +++ b/src/selection-chemistry.cpp @@ -1,9 +1,9 @@ #define __SP_SELECTION_CHEMISTRY_C__ -/* - * Miscellanous operations on selected items - * - * Authors: +/** @file + * @brief Miscellanous operations on selected items + */ +/* Authors: * Lauris Kaplinski * Frank Felfe * MenTaLguY @@ -27,7 +27,7 @@ #include "svg/svg.h" #include "inkscape.h" #include "desktop.h" -#include "desktop-style.h" +//#include "desktop-style.h" #include "selection.h" #include "tools-switch.h" #include "desktop-handles.h" @@ -82,8 +82,6 @@ #include "sp-filter-reference.h" #include "gradient-drag.h" #include "uri-references.h" -#include "live_effects/lpeobject.h" -#include "live_effects/parameter/path.h" #include "libnr/nr-convert2geom.h" // For clippath editing @@ -92,17 +90,14 @@ #include "node-context.h" #include "nodepath.h" +#include "ui/clipboard.h" + using NR::X; using NR::Y; -/* fixme: find a better place */ -Inkscape::XML::Document *clipboard_document = NULL; -GSList *clipboard = NULL; -GSList *defs_clipboard = NULL; -SPCSSAttr *style_clipboard = NULL; -NR::Maybe size_clipboard; - -static void sp_copy_stuff_used_by_item(GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc); +/* The clipboard handling is in ui/clipboard.cpp now. There are some legacy functions left here, +because the layer manipulation code uses them. It should be rewritten specifically +for that purpose. */ /** * Copies repr and its inherited css style elements, along with the accumulated transform 'full_t', @@ -127,66 +122,24 @@ void sp_selection_copy_one (Inkscape::XML::Node *repr, NR::Matrix full_t, GSList *clip = g_slist_prepend(*clip, copy); } -void sp_selection_copy_impl (GSList const *items, GSList **clip, GSList **defs_clip, SPCSSAttr **style_clip, Inkscape::XML::Document* xml_doc) +void sp_selection_copy_impl (GSList const *items, GSList **clip, Inkscape::XML::Document* xml_doc) { + // Sort items: + GSList *sorted_items = g_slist_copy ((GSList *) items); + sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position); - // Copy stuff referenced by all items to defs_clip: - if (defs_clip) { - for (GSList *i = (GSList *) items; i != NULL; i = i->next) { - sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (i->data), items, xml_doc); - } - *defs_clip = g_slist_reverse(*defs_clip); - } - - // Store style: - if (style_clip) { - SPItem *item = SP_ITEM (items->data); // take from the first selected item - *style_clip = take_style_from_item (item); + // Copy item reprs: + for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) { + sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc); } - if (clip) { - // Sort items: - GSList *sorted_items = g_slist_copy ((GSList *) items); - sorted_items = g_slist_sort((GSList *) sorted_items, (GCompareFunc) sp_object_compare_position); - - // Copy item reprs: - for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) { - sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc); - } - - *clip = g_slist_reverse(*clip); - g_slist_free ((GSList *) sorted_items); - } -} - -/** - * Add gradients/patterns/markers referenced by copied objects to defs. - * Iterates through 'defs_clip', and for each item it adds the data - * repr into the global defs. - */ -void -paste_defs (GSList **defs_clip, SPDocument *doc) -{ - if (!defs_clip) - return; - - for (GSList *gl = *defs_clip; gl != NULL; gl = gl->next) { - SPDefs *defs= (SPDefs *) SP_DOCUMENT_DEFS(doc); - Inkscape::XML::Node *repr = (Inkscape::XML::Node *) gl->data; - gchar const *id = repr->attribute("id"); - if (!id || !doc->getObjectById(id)) { - Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc); - Inkscape::XML::Node *copy = repr->duplicate(xml_doc); - SP_OBJECT_REPR(defs)->addChild(copy, NULL); - Inkscape::GC::release(copy); - } - } + *clip = g_slist_reverse(*clip); + g_slist_free ((GSList *) sorted_items); } -GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip, GSList **defs_clip) +GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **clip) { Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc); - paste_defs (defs_clip, doc); GSList *copied = NULL; // add objects to document @@ -501,7 +454,7 @@ void sp_selection_group() sp_repr_unparent(current); // paste into topmost_parent (temporarily) - GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip, NULL); + GSList *copied = sp_selection_paste_impl (doc, doc->getObjectByRepr(topmost_parent), &temp_clip); if (temp_clip) g_slist_free (temp_clip); if (copied) { // if success, // take pasted object (now in topmost_parent) @@ -865,146 +818,6 @@ void sp_selection_cut() sp_selection_delete(); } -void sp_copy_gradient (GSList **defs_clip, SPGradient *gradient, Inkscape::XML::Document* xml_doc) -{ - SPGradient *ref = gradient; - - while (ref) { - // climb up the refs, copying each one in the chain - Inkscape::XML::Node *grad_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc); - *defs_clip = g_slist_prepend (*defs_clip, grad_repr); - - ref = ref->ref->getObject(); - } -} - -void sp_copy_pattern (GSList **defs_clip, SPPattern *pattern, Inkscape::XML::Document* xml_doc) -{ - SPPattern *ref = pattern; - - while (ref) { - // climb up the refs, copying each one in the chain - Inkscape::XML::Node *pattern_repr = SP_OBJECT_REPR(ref)->duplicate(xml_doc); - *defs_clip = g_slist_prepend (*defs_clip, pattern_repr); - - // items in the pattern may also use gradients and other patterns, so we need to recurse here as well - for (SPObject *child = sp_object_first_child(SP_OBJECT(ref)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) { - if (!SP_IS_ITEM (child)) - continue; - sp_copy_stuff_used_by_item (defs_clip, (SPItem *) child, NULL, xml_doc); - } - - ref = ref->ref->getObject(); - } -} - -void sp_copy_single (GSList **defs_clip, SPObject *thing, Inkscape::XML::Document* xml_doc) -{ - Inkscape::XML::Node *duplicate_repr = SP_OBJECT_REPR(thing)->duplicate(xml_doc); - *defs_clip = g_slist_prepend (*defs_clip, duplicate_repr); -} - - -void sp_copy_textpath_path (GSList **defs_clip, SPTextPath *tp, GSList const *items, Inkscape::XML::Document* xml_doc) -{ - SPItem *path = sp_textpath_get_path_item (tp); - if (!path) - return; - if (items && g_slist_find ((GSList *) items, path)) // do not copy it to defs if it is already in the list of items copied - return; - Inkscape::XML::Node *repr = SP_OBJECT_REPR(path)->duplicate(xml_doc); - *defs_clip = g_slist_prepend (*defs_clip, repr); -} - -/** - * Copies things like patterns, markers, gradients, etc. - */ -void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, GSList const *items, Inkscape::XML::Document* xml_doc) -{ - SPStyle *style = SP_OBJECT_STYLE (item); - - if (style && (style->fill.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item); - if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server)) - sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc); - if (SP_IS_PATTERN (server)) - sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc); - } - - if (style && (style->stroke.isPaintserver())) { - SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item); - if (SP_IS_LINEARGRADIENT (server) || SP_IS_RADIALGRADIENT (server)) - sp_copy_gradient (defs_clip, SP_GRADIENT(server), xml_doc); - if (SP_IS_PATTERN (server)) - sp_copy_pattern (defs_clip, SP_PATTERN(server), xml_doc); - } - - // For shapes, copy all of the shape's markers into defs_clip - if (SP_IS_SHAPE (item)) { - SPShape *shape = SP_SHAPE (item); - for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) { - if (shape->marker[i]) { - sp_copy_single (defs_clip, SP_OBJECT (shape->marker[i]), xml_doc); - } - } - - // For shapes, also copy liveeffect if applicable - if (sp_shape_has_path_effect(shape)) { - sp_copy_single (defs_clip, SP_OBJECT(sp_shape_get_livepatheffectobject(shape)), xml_doc); - } - } - - // For 3D boxes copy perspectives - if (SP_IS_BOX3D(item)) { - sp_copy_single (defs_clip, SP_OBJECT(box3d_get_perspective(SP_BOX3D(item))), xml_doc); - } - - if (SP_IS_TEXT_TEXTPATH (item)) { - sp_copy_textpath_path (defs_clip, SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))), items, xml_doc); - } - - if (item->clip_ref->getObject()) { - sp_copy_single (defs_clip, item->clip_ref->getObject(), xml_doc); - } - - if (item->mask_ref->getObject()) { - SPObject *mask = item->mask_ref->getObject(); - sp_copy_single (defs_clip, mask, xml_doc); - // recurse into the mask for its gradients etc. - for (SPObject *o = SP_OBJECT(mask)->children; o != NULL; o = o->next) { - if (SP_IS_ITEM(o)) - sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc); - } - } - - if (style->getFilter()) { - SPObject *filter = style->getFilter(); - if (SP_IS_FILTER(filter)) { - sp_copy_single (defs_clip, filter, xml_doc); - } - } - - // recurse - for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) { - if (SP_IS_ITEM(o)) - sp_copy_stuff_used_by_item (defs_clip, SP_ITEM (o), items, xml_doc); - } -} - -void -sp_set_style_clipboard (SPCSSAttr *css) -{ - if (css != NULL) { - // clear style clipboard - if (style_clipboard) { - sp_repr_css_attr_unref (style_clipboard); - style_clipboard = NULL; - } - //sp_repr_css_print (css); - style_clipboard = css; - } -} - /** * \pre item != NULL */ @@ -1048,337 +861,30 @@ take_style_from_item (SPItem *item) void sp_selection_copy() { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) - return; - - if (!clipboard_document) { - clipboard_document = new Inkscape::XML::SimpleDocument(); - } - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - if (tools_isactive (desktop, TOOLS_DROPPER)) { - sp_dropper_context_copy(desktop->event_context); - return; // copied color under cursor, nothing else to do - } - - if (desktop->event_context->get_drag() && desktop->event_context->get_drag()->copy()) { - return; // copied selected stop(s), nothing else to do - } - - // check if something is selected - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing was copied.")); - return; - } - - GSList const *items = g_slist_copy ((GSList *) selection->itemList()); - - // 0. Copy text to system clipboard - // FIXME: for non-texts, put serialized Inkscape::XML as text to the clipboard; - //for this sp_repr_write_stream needs to be rewritten with iostream instead of FILE - Glib::ustring text; - if (tools_isactive (desktop, TOOLS_TEXT)) { - text = sp_text_get_selected_text(desktop->event_context); - } - - if (text.empty()) { - guint texts = 0; - for (GSList *i = (GSList *) items; i; i = i->next) { - SPItem *item = SP_ITEM (i->data); - if (SP_IS_TEXT (item) || SP_IS_FLOWTEXT(item)) { - if (texts > 0) // if more than one text object is copied, separate them by spaces - text += " "; - gchar *this_text = sp_te_get_string_multiline (item); - if (this_text) { - text += this_text; - g_free(this_text); - } - texts++; - } - } - } - if (!text.empty()) { - Glib::RefPtr refClipboard = Gtk::Clipboard::get(); - refClipboard->set_text(text); - } - - // clear old defs clipboard - while (defs_clipboard) { - Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data); - defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data); - } - - // clear style clipboard - if (style_clipboard) { - sp_repr_css_attr_unref (style_clipboard); - style_clipboard = NULL; - } - - //clear main clipboard - while (clipboard) { - Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data); - clipboard = g_slist_remove(clipboard, clipboard->data); - } - - sp_selection_copy_impl (items, &clipboard, &defs_clipboard, &style_clipboard, clipboard_document); - - if (tools_isactive (desktop, TOOLS_TEXT)) { // take style from cursor/text selection, overwriting the style just set by copy_impl - SPStyle *const query = sp_style_new(SP_ACTIVE_DOCUMENT); - if (sp_desktop_query_style_all (desktop, query)) { - SPCSSAttr *css = sp_css_attr_from_style (query, SP_STYLE_FLAG_ALWAYS); - sp_set_style_clipboard (css); - } - sp_style_unref(query); - } - - size_clipboard = selection->bounds(); - - g_slist_free ((GSList *) items); -} - - -void sp_selection_copy_lpe_pathparam(Inkscape::LivePathEffect::PathParam * pathparam) -{ - if (pathparam == NULL) - return; - - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) - return; - - if (!clipboard_document) { - clipboard_document = new Inkscape::XML::SimpleDocument(); - } - - // clear old defs clipboard - while (defs_clipboard) { - Inkscape::GC::release((Inkscape::XML::Node *) defs_clipboard->data); - defs_clipboard = g_slist_remove (defs_clipboard, defs_clipboard->data); - } - - // clear style clipboard - if (style_clipboard) { - sp_repr_css_attr_unref (style_clipboard); - style_clipboard = NULL; - } - - //clear main clipboard - while (clipboard) { - Inkscape::GC::release((Inkscape::XML::Node *) clipboard->data); - clipboard = g_slist_remove(clipboard, clipboard->data); - } - - // make new path node and put svgd as 'd' attribute - Inkscape::XML::Node *newnode = clipboard_document->createElement("svg:path"); - gchar * svgd = pathparam->param_writeSVGValue(); - newnode->setAttribute("d", svgd); - g_free(svgd); - - clipboard = g_slist_prepend(clipboard, newnode); - - Geom::Rect bnds = Geom::bounds_exact(pathparam->get_pwd2()); - size_clipboard = from_2geom(bnds); + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + cm->copy(); } - -//____________________________________________________________________________ - -/** Paste the bitmap in the clipboard if one is in there. - The bitmap is saved to a PNG file then imported into the document - - @return true if a bitmap was detected and pasted; false if no bitmap -*/ -static bool pastedPicFromClipboard() -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - SPDocument *doc = SP_ACTIVE_DOCUMENT; - if ( desktop == NULL || doc == NULL) - return false; - - Glib::RefPtr refClipboard = Gtk::Clipboard::get(); - Glib::RefPtr pic = refClipboard->wait_for_image(); - - // Stop if the system clipboard doesn't have a bitmap. - if ( pic == 0 ) - { - return false; - } //if - else - { - // Write into a file, then import the file into the document. - // Make a file name based on current time; use the current working dir. - time_t rawtime; - char filename[50]; - const char* path; - - time ( &rawtime ); - strftime (filename,50,"pastedpic_%m%d%Y_%H%M%S.png",localtime( &rawtime )); - path = (char *)prefs_get_string_attribute("dialogs.save_as", "path"); - Glib::ustring finalPath = path; - finalPath.append(G_DIR_SEPARATOR_S).append(filename); - pic->save( finalPath, "png" ); - file_import(doc, finalPath, NULL); - - // Clear the clipboard so that the bitmap in there won't always over - // ride the normal inkscape clipboard.This isn't the ideal solution. - refClipboard->set_text(""); - return true; - } //else - - return false; -} //pastedPicFromClipboard - -//____________________________________________________________________________ - void sp_selection_paste(bool in_place) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - - if (desktop == NULL) { - return; - } - - SPDocument *document = sp_desktop_document(desktop); - - if (Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false) { - return; - } - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - if (tools_isactive (desktop, TOOLS_TEXT)) { - if (sp_text_paste_inline(desktop->event_context)) - return; // pasted from system clipboard into text, nothing else to do - } - - // check if something is in the clipboard - - // Stop if successfully pasted a clipboard bitmap. - if ( pastedPicFromClipboard() ) - return; - - - if (clipboard == NULL) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing in the clipboard.")); - return; - } - - GSList *copied = sp_selection_paste_impl(document, desktop->currentLayer(), &clipboard, &defs_clipboard); - // add pasted objects to selection - selection->setReprList((GSList const *) copied); - g_slist_free (copied); - - if (!in_place) { - sp_document_ensure_up_to_date(document); - - NR::Maybe sel_bbox = selection->bounds(); - NR::Point m( desktop->point() ); - if (sel_bbox) { - m -= sel_bbox->midpoint(); - } - - sp_selection_move_relative(selection, m); - } - - sp_document_done(document, SP_VERB_EDIT_PASTE, _("Paste")); + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + if(cm->paste(in_place)) + sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE, _("Paste")); } void sp_selection_paste_style() { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) return; - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - // check if something is in the clipboard - if (style_clipboard == NULL) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the style clipboard.")); - return; - } - - // check if something is selected - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select object(s) to paste style to.")); - return; - } - - paste_defs (&defs_clipboard, sp_desktop_document(desktop)); - - sp_desktop_set_style (desktop, style_clipboard); - - sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_STYLE, - _("Paste style")); -} - -void sp_selection_paste_livepatheffect_impl(SPDocument *doc, SPItem *item, char const *effecturi) -{ - if ( item && SP_IS_SHAPE(item) ) { - SPShape * shape = SP_SHAPE(item); - - // create a private LPE object! - SPObject * obj = sp_uri_reference_resolve(doc, effecturi); - if (!obj) - return; - LivePathEffectObject * lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(0); - - sp_shape_set_path_effect(shape, lpeobj); - - // set inkscape:original-d for paths. the other shapes don't need this. - if ( SP_IS_PATH(item) ) { - Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item); - if ( ! pathrepr->attribute("inkscape:original-d") ) { - pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d")); - } - } - } else if (item && SP_IS_GROUP (item)) { - for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) { - if (!SP_IS_ITEM (child)) - continue; - sp_selection_paste_livepatheffect_impl (doc, SP_ITEM(child), effecturi); - } - } + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + if(cm->pasteStyle()) + sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_STYLE, _("Paste style")); } void sp_selection_paste_livepatheffect() { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) return; - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - // check if something is in the clipboard - if (clipboard == NULL) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard.")); - return; - } - - // check if something is selected - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select object(s) to paste live path effect to.")); - return; - } - - SPDocument *doc = sp_desktop_document(desktop); - paste_defs (&defs_clipboard, doc); - - Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data; - char const *effecturi = repr->attribute("inkscape:path-effect"); - if (!effecturi) { - SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a live path effect.")); - return; - } - - for ( GSList const *itemlist = selection->itemList(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) { - SPItem *item = reinterpret_cast(itemlist->data); - - sp_selection_paste_livepatheffect_impl(doc, item, effecturi); - - } - - sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT, + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + if(cm->pastePathEffect()) + sp_document_done(SP_ACTIVE_DOCUMENT, SP_VERB_EDIT_PASTE_LIVEPATHEFFECT, _("Paste live path effect")); } @@ -1422,78 +928,17 @@ void sp_selection_remove_livepatheffect() void sp_selection_paste_size (bool apply_x, bool apply_y) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) return; - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - // check if something is in the clipboard - if (!size_clipboard) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard.")); - return; - } - - // check if something is selected - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select object(s) to paste size to.")); - return; - } - - NR::Maybe current = selection->bounds(); - if ( !current || current->isEmpty() ) { - return; - } - - double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X); - double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y); - - sp_selection_scale_relative (selection, current->midpoint(), - NR::scale( - apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0), - apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0))); - - sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE, + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + if(cm->pasteSize(false, apply_x, apply_y)) + sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE, _("Paste size")); } void sp_selection_paste_size_separately (bool apply_x, bool apply_y) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) return; - - Inkscape::Selection *selection = sp_desktop_selection(desktop); - - // check if something is in the clipboard - if ( !size_clipboard ) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard.")); - return; - } - - // check if something is selected - if (selection->isEmpty()) { - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select object(s) to paste size to.")); - return; - } - - for (GSList const *l = selection->itemList(); l != NULL; l = l->next) { - SPItem *item = SP_ITEM(l->data); - - NR::Maybe current = sp_item_bbox_desktop(item); - if ( !current || current->isEmpty() ) { - continue; - } - - double scale_x = size_clipboard->extent(NR::X) / current->extent(NR::X); - double scale_y = size_clipboard->extent(NR::Y) / current->extent(NR::Y); - - sp_item_scale_rel (item, - NR::scale( - apply_x? scale_x : (desktop->isToolboxButtonActive ("lock")? scale_y : 1.0), - apply_y? scale_y : (desktop->isToolboxButtonActive ("lock")? scale_x : 1.0))); - - } - - sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY, + Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get(); + if(cm->pasteSize(true, apply_x, apply_y)) + sp_document_done(sp_desktop_document(SP_ACTIVE_DESKTOP), SP_VERB_EDIT_PASTE_SIZE_SEPARATELY, _("Paste size separately")); } @@ -1515,14 +960,14 @@ void sp_selection_to_next_layer () SPObject *next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); if (next) { GSList *temp_clip = NULL; - sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs + sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc())); sp_selection_delete_impl (items, false, false); next=Inkscape::next_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers GSList *copied; if(next) { - copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL); + copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip); } else { - copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL); + copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip); no_more = true; } selection->setReprList((GSList const *) copied); @@ -1560,14 +1005,14 @@ void sp_selection_to_prev_layer () SPObject *next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); if (next) { GSList *temp_clip = NULL; - sp_selection_copy_impl (items, &temp_clip, NULL, NULL, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs + sp_selection_copy_impl (items, &temp_clip, sp_document_repr_doc(dt->doc())); // we're in the same doc, so no need to copy defs sp_selection_delete_impl (items, false, false); next=Inkscape::previous_layer(dt->currentRoot(), dt->currentLayer()); // Fixes bug 1482973: crash while moving layers GSList *copied; if(next) { - copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip, NULL); + copied = sp_selection_paste_impl (sp_desktop_document (dt), next, &temp_clip); } else { - copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip, NULL); + copied = sp_selection_paste_impl (sp_desktop_document (dt), dt->currentLayer(), &temp_clip); no_more = true; } selection->setReprList((GSList const *) copied); @@ -1882,12 +1327,8 @@ void sp_selection_rotate_90_cw() /** - * \brief sp_selection_rotate_90_ccw - * - * This function rotates selected objects 90 degrees counter-clockwise. - * + * @brief Rotates selected objects 90 degrees counter-clockwise. */ - void sp_selection_rotate_90_ccw() { SPDesktop *desktop = SP_ACTIVE_DESKTOP; @@ -3072,9 +2513,7 @@ sp_selection_create_bitmap_copy () } /** - * \brief sp_selection_set_mask - * - * This function creates a mask or clipPath from selection + * \brief Creates a mask or clipPath from selection * Two different modes: * if applyToLayer, all selection is moved to DEFS as mask/clippath * and is applied to current layer @@ -3395,11 +2834,6 @@ void unhide_all_in_all_layers(SPDesktop *dt) { } -GSList * sp_selection_get_clipboard() { - return clipboard; -} - - /* Local Variables: mode:c++ diff --git a/src/selection-chemistry.h b/src/selection-chemistry.h index 86edc9091..9a7567446 100644 --- a/src/selection-chemistry.h +++ b/src/selection-chemistry.h @@ -59,7 +59,6 @@ SPCSSAttr *take_style_from_item (SPItem *item); void sp_selection_cut(); void sp_selection_copy(); -void sp_selection_copy_lpe_pathparam(Inkscape::LivePathEffect::PathParam * pathparam); void sp_selection_paste(bool in_place); void sp_selection_paste_style(); void sp_selection_paste_livepatheffect(); @@ -123,8 +122,6 @@ void unlock_all_in_all_layers(SPDesktop *dt); void unhide_all(SPDesktop *dt); void unhide_all_in_all_layers(SPDesktop *dt); -GSList * sp_selection_get_clipboard(); - /* selection cycling */ typedef enum diff --git a/src/ui/Makefile_insert b/src/ui/Makefile_insert index fc5ebad91..452471cc9 100644 --- a/src/ui/Makefile_insert +++ b/src/ui/Makefile_insert @@ -6,6 +6,8 @@ ui/clean: ui/widget/clean ui/dialog/clean ui/cache/clean rm -f ui/libui.a $(ui_libui_a_OBJECTS) ui_libui_a_SOURCES = \ + ui/clipboard.cpp \ + ui/clipboard.h \ ui/icons.cpp \ ui/icons.h \ ui/previewable.h \ diff --git a/src/ui/clipboard.cpp b/src/ui/clipboard.cpp new file mode 100644 index 000000000..0f9223054 --- /dev/null +++ b/src/ui/clipboard.cpp @@ -0,0 +1,1137 @@ +/** @file + * @brief System-wide clipboard management - implementation + */ +/* Authors: + * Krzysztof Kosiński + * Incorporates some code from selection-chemistry.cpp, see that file for more credits. + * + * Copyright (C) 2008 authors + * + * 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. + * + * See the file COPYING for details. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include "path-prefix.h" + +#include "ui/clipboard.h" + +// TODO: reduce header bloat if possible + +#include +#include +#include +#include +#include +#include // for g_file_set_contents etc., used in _onGet and paste +#include "gc-core.h" +#include "xml/repr.h" +#include "inkscape.h" +#include "io/stringstream.h" +#include "desktop.h" +#include "desktop-handles.h" +#include "desktop-style.h" // for sp_desktop_set_style, used in _pasteStyle +#include "document.h" +#include "document-private.h" +#include "selection.h" +#include "message-stack.h" +#include "context-fns.h" +#include "dropper-context.h" // used in copy() +#include "style.h" +#include "extension/db.h" // extension database +#include "extension/input.h" +#include "extension/output.h" +#include "selection-chemistry.h" +#include "libnr/nr-rect.h" +#include "box3d.h" +#include "gradient-drag.h" +#include "sp-item.h" +#include "sp-item-transform.h" // for sp_item_scale_rel, used in _pasteSize +#include "sp-path.h" +#include "sp-pattern.h" +#include "sp-shape.h" +#include "sp-gradient.h" +#include "sp-gradient-reference.h" +#include "sp-gradient-fns.h" +#include "sp-linear-gradient-fns.h" +#include "sp-radial-gradient-fns.h" +#include "sp-clippath.h" +#include "sp-mask.h" +#include "sp-textpath.h" +#include "live_effects/lpeobject.h" +#include "live_effects/parameter/path.h" +#include "svg/svg.h" // for sp_svg_transform_write, used in _copySelection +#include "svg/css-ostringstream.h" // used in _parseColor +#include "file.h" // for file_import, used in _pasteImage +#include "prefs-utils.h" // for prefs_get_string_attribute, used in _pasteImage +#include "text-context.h" +#include "text-editing.h" +#include "tools-switch.h" + +/// @brief Made up mimetype to represent Gdk::Pixbuf clipboard contents +#define CLIPBOARD_GDK_PIXBUF_TARGET "image/x-gdk-pixbuf" + +#define CLIPBOARD_TEXT_TARGET "text/plain" + +namespace Inkscape { +namespace UI { + + +/** + * @brief Default implementation of the clipboard manager + */ +class ClipboardManagerImpl : public ClipboardManager { +public: + virtual void copy(); + virtual void copyPathParameter(Inkscape::LivePathEffect::PathParam *); + virtual bool paste(bool in_place); + virtual bool pasteStyle(); + virtual bool pasteSize(bool, bool, bool); + virtual bool pastePathEffect(); + virtual Glib::ustring getPathParameter(); + + ClipboardManagerImpl(); + ~ClipboardManagerImpl(); + +private: + void _copySelection(Inkscape::Selection *); + void _copyUsedDefs(SPItem *); + void _copyGradient(SPGradient *); + void _copyPattern(SPPattern *); + void _copyTextPath(SPTextPath *); + Inkscape::XML::Node *_copyNode(Inkscape::XML::Node *, Inkscape::XML::Document *, Inkscape::XML::Node *); + + void _pasteDocument(SPDocument *, bool in_place); + void _pasteDefs(SPDocument *); + bool _pasteImage(); + bool _pasteText(); + SPCSSAttr *_parseColor(const Glib::ustring &); + void _applyPathEffect(SPItem *, gchar const *); + SPDocument *_retrieveClipboard(Glib::ustring = ""); + + // clipboard callbacks + void _onGet(Gtk::SelectionData &, guint); + void _onClear(); + + // various helpers + void _createInternalClipboard(); + void _discardInternalClipboard(); + Inkscape::XML::Node *_createClipNode(); + NR::scale _getScale(Geom::Point &, Geom::Point &, NR::Rect &, bool, bool); + Glib::ustring _getBestTarget(); + void _setClipboardTargets(); + void _setClipboardColor(guint32); + void _userWarn(SPDesktop *, char const *); + + // private properites + SPDocument *_clipboardSPDoc; ///< Document that stores the clipboard until someone requests it + Inkscape::XML::Node *_defs; ///< Reference to the clipboard document's defs node + Inkscape::XML::Node *_root; ///< Reference to the clipboard's root node + Inkscape::XML::Node *_clipnode; ///< The node that holds extra information + Inkscape::XML::Document *_doc; ///< Reference to the clipboard's Inkscape::XML::Document + + Glib::RefPtr _clipboard; ///< Handle to the system wide clipboard - for convenience + std::list _preferred_targets; ///< List of supported clipboard targets +}; + + +ClipboardManagerImpl::ClipboardManagerImpl() + : _clipboardSPDoc(NULL), + _defs(NULL), + _root(NULL), + _clipnode(NULL), + _doc(NULL), + _clipboard( Gtk::Clipboard::get() ) +{ + // push supported clipboard targets, in order of preference + _preferred_targets.push_back("image/x-inkscape-svg"); + _preferred_targets.push_back("image/svg+xml"); + _preferred_targets.push_back("image/svg+xml-compressed"); +#ifdef WIN32 + _preferred_targets.push_back("image/x-emf"); +#endif + _preferred_targets.push_back("application/pdf"); + _preferred_targets.push_back("image/x-adobe-illustrator"); +} + + +ClipboardManagerImpl::~ClipboardManagerImpl() {} + + +/** + * @brief Copy selection contents to the clipboard + */ +void ClipboardManagerImpl::copy() +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if ( desktop == NULL ) return; + Inkscape::Selection *selection = sp_desktop_selection(desktop); + + // Special case for when the gradient dragger is active - copies gradient color + if (desktop->event_context->get_drag()) { + GrDrag *drag = desktop->event_context->get_drag(); + if (drag->hasSelection()) { + _setClipboardColor(drag->getColor()); + _discardInternalClipboard(); + return; + } + } + + // Special case for when the color picker ("dropper") is active - copies color under cursor + if (tools_isactive(desktop, TOOLS_DROPPER)) { + _setClipboardColor(sp_dropper_context_get_color(desktop->event_context)); + _discardInternalClipboard(); + return; + } + + // Special case for when the text tool is active - if some text is selected, copy plain text, + // not the object that holds it + if (tools_isactive(desktop, TOOLS_TEXT)) { + Glib::ustring selected_text = sp_text_get_selected_text(desktop->event_context); + if (!selected_text.empty()) { + _clipboard->set_text(selected_text); + _discardInternalClipboard(); + return; + } + } + + if (selection->isEmpty()) { // check whether something is selected + _userWarn(desktop, _("Nothing was copied.")); + return; + } + _discardInternalClipboard(); + + _createInternalClipboard(); // construct a new clipboard document + _copySelection(selection); // copy all items in the selection to the internal clipboard + fit_canvas_to_drawing(_clipboardSPDoc); + + _setClipboardTargets(); +} + + +/** + * @brief Copy a Live Path Effect path parameter to the clipboard + * @param pp The path parameter to store in the clipboard + */ +void ClipboardManagerImpl::copyPathParameter(Inkscape::LivePathEffect::PathParam *pp) +{ + if ( pp == NULL ) return; + gchar *svgd = pp->param_writeSVGValue(); + if ( svgd == NULL || *svgd == '\0' ) return; + + _discardInternalClipboard(); + _createInternalClipboard(); + + Inkscape::XML::Node *pathnode = _doc->createElement("svg:path"); + pathnode->setAttribute("d", svgd); + g_free(svgd); + _root->appendChild(pathnode); + Inkscape::GC::release(pathnode); + + fit_canvas_to_drawing(_clipboardSPDoc); + _setClipboardTargets(); +} + +/** + * @brief Paste from the system clipboard into the active desktop + * @param in_place Whether to put the contents where they were when copied + */ +bool ClipboardManagerImpl::paste(bool in_place) +{ + // do any checking whether we really are able to paste before requesting the contents + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if ( desktop == NULL ) return false; + if ( Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false ) return false; + + Glib::ustring target = _getBestTarget(); + + // Special cases of clipboard content handling go here 00ff00 + // Note that target priority is determined in _getBestTarget. + // TODO: Handle x-special/gnome-copied-files and text/uri-list to support pasting files + + // if there is an image on the clipboard, paste it + if ( target == CLIPBOARD_GDK_PIXBUF_TARGET ) return _pasteImage(); + // if there's only text, paste it into a selected text object or create a new one + if ( target == CLIPBOARD_TEXT_TARGET ) return _pasteText(); + + // otherwise, use the import extensions + SPDocument *tempdoc = _retrieveClipboard(target); + if ( tempdoc == NULL ) { + _userWarn(desktop, _("Nothing on the clipboard.")); + return false; + } + + _pasteDocument(tempdoc, in_place); + sp_document_unref(tempdoc); + + return true; +} + + +/** + * @brief Implements the Paste Style action + */ +bool ClipboardManagerImpl::pasteStyle() +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if (desktop == NULL) return false; + + // check whether something is selected + Inkscape::Selection *selection = sp_desktop_selection(desktop); + if (selection->isEmpty()) { + _userWarn(desktop, _("Select object(s) to paste style to.")); + return false; + } + + SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg"); + if ( tempdoc == NULL ) { + _userWarn(desktop, _("No style on the clipboard.")); + return false; + } + + Inkscape::XML::Node + *root = sp_document_repr_root(tempdoc), + *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1); + + bool pasted = false; + + if (clipnode) { + _pasteDefs(tempdoc); + SPCSSAttr *style = sp_repr_css_attr(clipnode, "style"); + sp_desktop_set_style(desktop, style); + pasted = true; + } + else { + _userWarn(desktop, _("No style on the clipboard.")); + } + + sp_document_unref(tempdoc); + return pasted; +} + + +/** + * @brief Resize the selection or each object in the selection to match the clipboard's size + * @param separately Whether to scale each object in the selection separately + * @param apply_x Whether to scale the width of objects / selection + * @param apply_y Whether to scale the height of objects / selection + */ +bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y) +{ + if(!apply_x && !apply_y) return false; // pointless parameters + + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if ( desktop == NULL ) return false; + Inkscape::Selection *selection = sp_desktop_selection(desktop); + if (selection->isEmpty()) { + _userWarn(desktop, _("Select object(s) to paste size to.")); + return false; + } + + // FIXME: actually, this should accept arbitrary documents + SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg"); + if ( tempdoc == NULL ) { + _userWarn(desktop, _("No size on the clipboard.")); + return false; + } + + // retrieve size ifomration from the clipboard + Inkscape::XML::Node *root = sp_document_repr_root(tempdoc); + Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1); + bool pasted = false; + if (clipnode) { + Geom::Point min, max; + sp_repr_get_point(clipnode, "min", &min); + sp_repr_get_point(clipnode, "max", &max); + + // resize each object in the selection + if (separately) { + for (GSList *i = const_cast(selection->itemList()) ; i ; i = i->next) { + SPItem *item = SP_ITEM(i->data); + NR::Maybe obj_size = sp_item_bbox_desktop(item); + if ( !obj_size || obj_size->isEmpty() ) continue; + sp_item_scale_rel(item, _getScale(min, max, *obj_size, apply_x, apply_y)); + } + } + // resize the selection as a whole + else { + NR::Maybe sel_size = selection->bounds(); + if ( sel_size && !sel_size->isEmpty() ) { + sp_selection_scale_relative(selection, sel_size->midpoint(), + _getScale(min, max, *sel_size, apply_x, apply_y)); + } + } + pasted = true; + } + sp_document_unref(tempdoc); + return pasted; +} + + +/** + * @brief Applies a path effect from the clipboard to the selected path + */ +bool ClipboardManagerImpl::pastePathEffect() +{ + /** @todo FIXME: pastePathEffect crashes when moving the path with the applied effect, + segfaulting in fork_private_if_necessary(). */ + + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if ( desktop == NULL ) return false; + Inkscape::Selection *selection = sp_desktop_selection(desktop); + if (selection->isEmpty()) { + _userWarn(desktop, _("Select object(s) to paste live path effect to.")); + return false; + } + + Inkscape::XML::Node *root, *clipnode; + gchar const *effect; + SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg"); + if ( tempdoc == NULL ) goto no_effect; + + root = sp_document_repr_root(tempdoc), + clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1); + if ( clipnode == NULL ) goto no_effect; + effect = clipnode->attribute("inkscape:path-effect"); + if ( effect == NULL ) goto no_effect; + + _pasteDefs(tempdoc); + for (GSList *item = const_cast(selection->itemList()) ; item ; item = item->next) { + _applyPathEffect(reinterpret_cast(item->data), effect); + } + return true; + + no_effect: + _userWarn(desktop, _("No effect on the clipboard.")); + return false; +} + + +/** + * @brief Get LPE path data from the clipboard + * @return The retrieved path data (contents of the d attribute), or "" if no path was found + */ +Glib::ustring ClipboardManagerImpl::getPathParameter() +{ + SPDocument *tempdoc = _retrieveClipboard(); // any target will do here + if ( tempdoc == NULL ) { + _userWarn(SP_ACTIVE_DESKTOP, _("Nothing on the clipboard.")); + return ""; + } + Inkscape::XML::Node + *root = sp_document_repr_root(tempdoc), + *path = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth + if ( path == NULL ) { + _userWarn(SP_ACTIVE_DESKTOP, _("Clipboard does not contain a path.")); + sp_document_unref(tempdoc); + return ""; + } + gchar const *svgd = path->attribute("d"); + return svgd; +} + + +/** + * @brief Iterate over a list of items and copy them to the clipboard. + */ +void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection) +{ + GSList const *items = selection->itemList(); + // copy the defs used by all items + for (GSList *i = const_cast(items) ; i != NULL ; i = i->next) { + _copyUsedDefs(SP_ITEM (i->data)); + } + + // copy the representation of the items + GSList *sorted_items = g_slist_copy(const_cast(items)); + sorted_items = g_slist_sort(sorted_items, (GCompareFunc) sp_object_compare_position); + + for (GSList *i = sorted_items ; i ; i = i->next) { + if (!SP_IS_ITEM(i->data)) continue; + Inkscape::XML::Node *obj = SP_OBJECT_REPR(i->data); + Inkscape::XML::Node *obj_copy = _copyNode(obj, _doc, _root); + + // copy complete inherited style + SPCSSAttr *css = sp_repr_css_attr_inherited(obj, "style"); + sp_repr_css_set(obj_copy, css, "style"); + sp_repr_css_attr_unref(css); + + // write the complete accumulated transform passed to us + // (we're dealing with unattached representations, so we write to their attributes + // instead of using sp_item_set_transform) + gchar *transform_str = sp_svg_transform_write(sp_item_i2doc_affine(SP_ITEM(i->data))); + obj_copy->setAttribute("transform", transform_str); + g_free(transform_str); + } + + // copy style for Paste Style action + if (sorted_items) { + if(SP_IS_ITEM(sorted_items->data)) { + SPCSSAttr *style = take_style_from_item((SPItem *) sorted_items->data); + sp_repr_css_set(_clipnode, style, "style"); + sp_repr_css_attr_unref(style); + } + + // copy path effect from the first path + if (SP_IS_OBJECT(sorted_items->data)) { + gchar const *effect = SP_OBJECT_REPR(sorted_items->data)->attribute("inkscape:path-effect"); + if (effect) { + _clipnode->setAttribute("inkscape:path-effect", effect); + } + } + } + + NR::Maybe size = selection->bounds(); + if (size) { + sp_repr_set_point(_clipnode, "min", size->min().to_2geom()); + sp_repr_set_point(_clipnode, "max", size->max().to_2geom()); + } + + g_slist_free(sorted_items); +} + + +/** + * @brief Recursively copy all the definitions used by a given item to the clipboard defs + */ +void ClipboardManagerImpl::_copyUsedDefs(SPItem *item) +{ + // copy fill and stroke styles (patterns and gradients) + SPStyle *style = SP_OBJECT_STYLE(item); + + if (style && (style->fill.isPaintserver())) { + SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item); + if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server)) + _copyGradient(SP_GRADIENT(server)); + if (SP_IS_PATTERN(server)) + _copyPattern(SP_PATTERN(server)); + } + if (style && (style->stroke.isPaintserver())) { + SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item); + if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server)) + _copyGradient(SP_GRADIENT(server)); + if (SP_IS_PATTERN(server)) + _copyPattern(SP_PATTERN(server)); + } + + // For shapes, copy all of the shape's markers + if (SP_IS_SHAPE(item)) { + SPShape *shape = SP_SHAPE (item); + for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) { + if (shape->marker[i]) { + _copyNode(SP_OBJECT_REPR(SP_OBJECT(shape->marker[i])), _doc, _defs); + } + } + // Also copy live effects if applicable + if (sp_shape_has_path_effect(shape)) { + _copyNode(SP_OBJECT_REPR(SP_OBJECT(sp_shape_get_livepatheffectobject(shape))), _doc, _defs); + } + } + // For 3D boxes, copy perspectives + if (SP_IS_BOX3D(item)) { + _copyNode(SP_OBJECT_REPR(SP_OBJECT(box3d_get_perspective(SP_BOX3D(item)))), _doc, _defs); + } + // Copy text paths + if (SP_IS_TEXT_TEXTPATH(item)) { + _copyTextPath(SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item)))); + } + // Copy clipping objects + if (item->clip_ref->getObject()) { + _copyNode(SP_OBJECT_REPR(item->clip_ref->getObject()), _doc, _defs); + } + // Copy mask objects + if (item->mask_ref->getObject()) { + SPObject *mask = item->mask_ref->getObject(); + _copyNode(SP_OBJECT_REPR(mask), _doc, _defs); + // recurse into the mask for its gradients etc. + for (SPObject *o = SP_OBJECT(mask)->children ; o != NULL ; o = o->next) { + if (SP_IS_ITEM(o)) + _copyUsedDefs(SP_ITEM(o)); + } + } + // Copy filters + if (style->getFilter()) { + SPObject *filter = style->getFilter(); + if (SP_IS_FILTER(filter)) { + _copyNode(SP_OBJECT_REPR(filter), _doc, _defs); + } + } + + // recurse + for (SPObject *o = SP_OBJECT(item)->children ; o != NULL ; o = o->next) { + if (SP_IS_ITEM(o)) + _copyUsedDefs(SP_ITEM(o)); + } +} + + +/** + * @brief Copy a single gradient to the clipboard's defs element + */ +void ClipboardManagerImpl::_copyGradient(SPGradient *gradient) +{ + while (gradient) { + // climb up the refs, copying each one in the chain + _copyNode(SP_OBJECT_REPR(gradient), _doc, _defs); + gradient = gradient->ref->getObject(); + } +} + + +/** + * @brief Copy a single pattern to the clipboard document's defs element + */ +void ClipboardManagerImpl::_copyPattern(SPPattern *pattern) +{ + // climb up the references, copying each one in the chain + while (pattern) { + _copyNode(SP_OBJECT_REPR(pattern), _doc, _defs); + + // items in the pattern may also use gradients and other patterns, so recurse + for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) { + if (!SP_IS_ITEM (child)) continue; + _copyUsedDefs(SP_ITEM(child)); + } + pattern = pattern->ref->getObject(); + } +} + + +/** + * @brief Copy a text path to the clipboard's defs element + */ +void ClipboardManagerImpl::_copyTextPath(SPTextPath *tp) +{ + SPItem *path = sp_textpath_get_path_item(tp); + if(!path) return; + Inkscape::XML::Node *path_node = SP_OBJECT_REPR(path); + + // Do not copy the text path to defs if it's already copied + if(sp_repr_lookup_child(_root, "id", path_node->attribute("id"))) return; + _copyNode(path_node, _doc, _defs); +} + + +/** + * @brief Copy a single XML node from one document to another + * @param node The node to be copied + * @param target_doc The document to which the node is to be copied + * @param parent The node in the target document which will become the parent of the copied node + * @return Pointer to the copied node + */ +Inkscape::XML::Node *ClipboardManagerImpl::_copyNode(Inkscape::XML::Node *node, Inkscape::XML::Document *target_doc, Inkscape::XML::Node *parent) +{ + Inkscape::XML::Node *dup = node->duplicate(target_doc); + parent->appendChild(dup); + Inkscape::GC::release(dup); + return dup; +} + + +/** + * @brief Paste the contents of a document into the active desktop + * @param clipdoc The document to paste + * @param in_place Whether to paste the selection where it was when copied + * @pre @c clipdoc is not empty and items can be added to the current layer + */ +void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place) +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + SPDocument *target_document = sp_desktop_document(desktop); + Inkscape::XML::Node + *root = sp_document_repr_root(clipdoc), + *target_parent = SP_OBJECT_REPR(desktop->currentLayer()); + Inkscape::XML::Document *target_xmldoc = sp_document_repr_doc(target_document); + + // copy definitions + _pasteDefs(clipdoc); + + // copy objects + GSList *pasted_objects = NULL; + for (Inkscape::XML::Node *obj = root->firstChild() ; obj ; obj = obj->next()) { + // Don't copy metadata, defs, named views and internal clipboard contents to the document + if (!strcmp(obj->name(), "svg:defs")) continue; + if (!strcmp(obj->name(), "svg:metadata")) continue; + if (!strcmp(obj->name(), "sodipodi:namedview")) continue; + if (!strcmp(obj->name(), "inkscape:clipboard")) continue; + Inkscape::XML::Node *obj_copy = _copyNode(obj, target_xmldoc, target_parent); + pasted_objects = g_slist_prepend(pasted_objects, (gpointer) obj_copy); + } + + Inkscape::Selection *selection = sp_desktop_selection(desktop); + selection->setReprList(pasted_objects); + + // move the selection to the right position + if(in_place) + { + Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1); + if (clipnode) { + Geom::Point min, max; + sp_repr_get_point(clipnode, "min", &min); + sp_repr_get_point(clipnode, "max", &max); + + // this formula was discovered empyrically + min[Geom::Y] += ((max[Geom::Y] - min[Geom::Y]) - sp_document_height(target_document)); + sp_selection_move_relative(selection, NR::Point(min)); + } + } + // copied from former sp_selection_paste in selection-chemistry.cpp + else { + sp_document_ensure_up_to_date(target_document); + NR::Maybe sel_size = selection->bounds(); + + NR::Point m( desktop->point() ); + if (sel_size) { + m -= sel_size->midpoint(); + } + sp_selection_move_relative(selection, m); + } + + g_slist_free(pasted_objects); +} + + +/** + * @brief Paste SVG defs from the document retrieved from the clipboard into the active document + * @param clipdoc The document to paste + * @pre @c clipdoc != NULL and pasting into the active document is possible + */ +void ClipboardManagerImpl::_pasteDefs(SPDocument *clipdoc) +{ + // boilerplate vars copied from _pasteDocument + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + SPDocument *target_document = sp_desktop_document(desktop); + Inkscape::XML::Node + *root = sp_document_repr_root(clipdoc), + *defs = sp_repr_lookup_name(root, "svg:defs", 1), + *target_defs = SP_OBJECT_REPR(SP_DOCUMENT_DEFS(target_document)); + Inkscape::XML::Document *target_xmldoc = sp_document_repr_doc(target_document); + + for (Inkscape::XML::Node *def = defs->firstChild() ; def ; def = def->next()) { + /// @todo TODO: implement def id collision resolution in ClipboardManagerImpl::_pasteDefs() + + /* + // simplistic solution: when a collision occurs, add "a" to id until it's unique + Glib::ustring pasted_id = def->attribute("id"); + if ( pasted_id.empty() ) continue; // defs without id are useless + Glib::ustring pasted_id_original = pasted_id; + + while(sp_repr_lookup_child(target_defs, "id", pasted_id.data())) { + pasted_id.append("a"); + } + + if ( pasted_id != pasted_id_original ) { + def->setAttribute("id", pasted_id.data()); + // Update the id in the rest of the document so there are no dangling references + // How to do that? + _changeIdReferences(clipdoc, pasted_id_original, pasted_id); + } + */ + if (sp_repr_lookup_child(target_defs, "id", def->attribute("id"))) + continue; // skip duplicate defs - temporary non-solution + + _copyNode(def, target_xmldoc, target_defs); + } +} + + +/** + * @brief Retrieve a bitmap image from the clipboard and paste it into the active document + */ +bool ClipboardManagerImpl::_pasteImage() +{ + SPDocument *doc = SP_ACTIVE_DOCUMENT; + if ( doc == NULL ) return false; + + // retrieve image data + Glib::RefPtr img = _clipboard->wait_for_image(); + if (!img) return false; + + // Very stupid hack: Write into a file, then import the file into the document. + // To avoid using tmpfile and POSIX file handles, make the filename based on current time. + // This wasn't my idea, I just copied this from selection-chemistry.cpp + // and just can't think of something saner at the moment. Pasting more than + // one image per second will overwrite the image. + // However, I don't think anyone is able to copy a _different_ image into inkscape + // in 1 second. + time_t rawtime; + char image_filename[128]; + gchar const *save_folder; + + time(&rawtime); + strftime(image_filename, 128, "inkscape_pasted_image_%Y%m%d_%H%M%S.png", localtime( &rawtime )); + save_folder = (gchar const *) prefs_get_string_attribute("dialogs.save_as", "path"); + + gchar *image_path = g_build_filename(save_folder, image_filename, NULL); + img->save(image_path, "png"); + file_import(doc, image_path, NULL); + g_free(image_path); + + return true; +} + +/** + * @brief Paste text into the selected text object or create a new one to hold it + */ +bool ClipboardManagerImpl::_pasteText() +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if ( desktop == NULL ) return false; + + // if the text editing tool is active, paste the text into the active text object + if (tools_isactive(desktop, TOOLS_TEXT)) + return sp_text_paste_inline(desktop->event_context); + + // try to parse the text as a color and, if successful, apply it as the current style + SPCSSAttr *css = _parseColor(_clipboard->wait_for_text()); + if (css) { + sp_desktop_set_style(desktop, css); + return true; + } + + return false; +} + + +/** + * @brief Attempt to parse the passed string as a hexadecimal RGB or RGBA color + * @param text The Glib::ustring to parse + * @return New CSS style representation if the parsing was successful, NULL otherwise + */ +SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text) +{ + Glib::ustring::size_type len = text.bytes(); + char *str = const_cast(text.data()); + bool attempt_alpha = false; + if ( !str || ( *str == '\0' ) ) return NULL; // this is OK due to boolean short-circuit + + // those conditionals guard against parsing e.g. the string "fab" as "fab000" + // (incomplete color) and "45fab71" as "45fab710" (incomplete alpha) + if ( *str == '#' ) { + if ( len < 7 ) return NULL; + if ( len >= 9 ) attempt_alpha = true; + } else { + if ( len < 6 ) return NULL; + if ( len >= 8 ) attempt_alpha = true; + } + + unsigned int color = 0, alpha = 0xff; + + // skip a leading #, if present + if ( *str == '#' ) ++str; + + // try to parse first 6 digits + int res = sscanf(str, "%6x", &color); + if ( res && ( res != EOF ) ) { + if (attempt_alpha) {// try to parse alpha if there's enough characters + sscanf(str + 6, "%2x", &alpha); + if ( !res || res == EOF ) alpha = 0xff; + } + + SPCSSAttr *color_css = sp_repr_css_attr_new(); + + // print and set properties + gchar color_str[16]; + g_snprintf(color_str, 16, "#%06x", color); + sp_repr_css_set_property(color_css, "fill", color_str); + + float opacity = static_cast(alpha)/static_cast(0xff); + if (opacity > 1.0) opacity = 1.0; // safeguard + Inkscape::CSSOStringStream opcss; + opcss << opacity; + sp_repr_css_set_property(color_css, "fill-opacity", opcss.str().data()); + return color_css; + } + return NULL; +} + + +/** + * @brief Applies a pasted path effect to a given item + */ +void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effect) +{ + if ( item == NULL ) return; + + if (SP_IS_SHAPE(item)) + { + SPShape *shape = SP_SHAPE(item); + SPObject *obj = sp_uri_reference_resolve(_clipboardSPDoc, effect); + if (!obj) return; + // if the effect is not used by anyone, we might as well take it + LivePathEffectObject *lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(1); + sp_shape_set_path_effect(shape, lpeobj); + + // set inkscape:original-d for paths. the other shapes don't need this + if (SP_IS_PATH(item)) { + Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item); + if (!pathrepr->attribute("inkscape:original-d")) { + pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d")); + } + } + } + else if (SP_IS_GROUP(item)) + { + for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; + child != NULL ; child = SP_OBJECT_NEXT(child)) + { + if (!SP_IS_ITEM(child)) continue; + _applyPathEffect(SP_ITEM(child), effect); + } + } +} + + +/** + * @brief Retrieve the clipboard contents as a document + * @return Clipboard contents converted to SPDocument, or NULL if no suitable content was present + */ +SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_target) +{ + Glib::ustring best_target; + if ( required_target == "" ) best_target = _getBestTarget(); + else best_target = required_target; + + if ( best_target == "" ) { + return NULL; + } + + // doing this synchronously makes better sense + Gtk::SelectionData sel = _clipboard->wait_for_contents(best_target); + + Glib::ustring target = sel.get_target(); + + // there is no specific plain SVG input extension, so if we can paste the Inkscape SVG format, + // we use the image/svg+xml mimetype to look up the input extension + if(target == "image/x-inkscape-svg") + target = "image/svg+xml"; + + Inkscape::Extension::DB::InputList inlist; + Inkscape::Extension::db.get_input_list(inlist); + Inkscape::Extension::DB::InputList::const_iterator in = inlist.begin(); + for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in); + if ( in == inlist.end() ) return NULL; // this shouldn't happen unless _getBestTarget returns something bogus + + // FIXME: Temporary hack until we add memory input. + // Save the clipboard contents to some file, then read it + gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-import", NULL ); + g_file_set_contents(filename, (const gchar *) sel.get_data(), sel.get_length(), NULL); + + SPDocument *tempdoc = (*in)->open(filename); + g_unlink(filename); + g_free(filename); + + return tempdoc; +} + + +/** + * @brief Callback called when some other application requests data from Inkscape + * + * Finds a suitable output extension to save the internal clipboard document, + * then saves it to memory and sets the clipboard contents. + */ +void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint info) +{ + g_assert( _clipboardSPDoc != NULL ); + + const Glib::ustring target = sel.get_target(); + if(target == "") return; // this shouldn't happen + + Inkscape::Extension::DB::OutputList outlist; + Inkscape::Extension::db.get_output_list(outlist); + Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin(); + for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out); + if ( out == outlist.end() ) return; // this also shouldn't happen + + // FIXME: Temporary hack until we add support for memory output. + // Save to a temporary file, read it back and then set the clipboard contents + gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-export", NULL ); + gsize len; gchar *data; + + (*out)->save(_clipboardSPDoc, filename); + g_file_get_contents(filename, &data, &len, NULL); + g_unlink(filename); // delete the temporary file + g_free(filename); + + sel.set(8, (guint8 const *) data, len); +} + + +/** + * @brief Callback when someone else takes the clipboard + * + * When the clipboard owner changes, this callback clears the internal clipboard document + * to reduce memory usage. + */ +void ClipboardManagerImpl::_onClear() +{ + // why is this called before _onGet??? + //_discardInternalClipboard(); +} + + +/** + * @brief Creates an internal clipboard document from scratch + */ +void ClipboardManagerImpl::_createInternalClipboard() +{ + if ( _clipboardSPDoc == NULL ) { + _clipboardSPDoc = sp_document_new(NULL, false, true); + //g_assert( _clipboardSPDoc != NULL ); + _defs = SP_OBJECT_REPR(SP_DOCUMENT_DEFS(_clipboardSPDoc)); + _doc = sp_document_repr_doc(_clipboardSPDoc); + _root = sp_document_repr_root(_clipboardSPDoc); + + _clipnode = _doc->createElement("inkscape:clipboard"); + _root->appendChild(_clipnode); + Inkscape::GC::release(_clipnode); + } +} + + +/** + * @brief Deletes the internal clipboard document + */ +void ClipboardManagerImpl::_discardInternalClipboard() +{ + if ( _clipboardSPDoc != NULL ) { + sp_document_unref(_clipboardSPDoc); + _clipboardSPDoc = NULL; + _defs = NULL; + _doc = NULL; + _root = NULL; + _clipnode = NULL; + } +} + + +/** + * @brief Get the scale to resize an item, based on the command and desktop state + */ +NR::scale ClipboardManagerImpl::_getScale(Geom::Point &min, Geom::Point &max, NR::Rect &obj_rect, bool apply_x, bool apply_y) +{ + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + double scale_x = 1.0; + double scale_y = 1.0; + + if (apply_x) { + scale_x = (max[Geom::X] - min[Geom::X]) / obj_rect.extent(NR::X); + } + if (apply_y) { + scale_y = (max[Geom::Y] - min[Geom::Y]) / obj_rect.extent(NR::Y); + } + // If the "lock aspect ratio" button is pressed and we paste only a single coordinate, + // resize the second one by the same ratio too + if (desktop->isToolboxButtonActive("lock")) { + if (apply_x && !apply_y) scale_y = scale_x; + if (apply_y && !apply_x) scale_x = scale_y; + } + + return NR::scale(scale_x, scale_y); +} + + +/** + * @brief Find the most suitable clipboard target + */ +Glib::ustring ClipboardManagerImpl::_getBestTarget() +{ + std::list targets = _clipboard->wait_for_targets(); + + // clipboard target debugging snippet + /* + g_debug("Begin clipboard targets"); + for ( std::list::iterator x = targets.begin() ; x != targets.end(); ++x ) + g_debug("Clipboard target: %s", (*x).data()); + g_debug("End clipboard targets\n"); + //*/ + + for(std::list::iterator i = _preferred_targets.begin() ; + i != _preferred_targets.end() ; ++i) + { + if ( std::find(targets.begin(), targets.end(), *i) != targets.end() ) + return *i; + } + if (_clipboard->wait_is_image_available()) + return CLIPBOARD_GDK_PIXBUF_TARGET; + if (_clipboard->wait_is_text_available()) + return CLIPBOARD_TEXT_TARGET; + + return ""; +} + + +/** + * @brief Set the clipboard targets to reflect the mimetypes Inkscape can output + */ +void ClipboardManagerImpl::_setClipboardTargets() +{ + Inkscape::Extension::DB::OutputList outlist; + Inkscape::Extension::db.get_output_list(outlist); + std::list target_list; + for (Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin() ; out != outlist.end() ; ++out) { + target_list.push_back(Gtk::TargetEntry( (*out)->get_mimetype() )); + } + + _clipboard->set(target_list, + sigc::mem_fun(*this, &ClipboardManagerImpl::_onGet), + sigc::mem_fun(*this, &ClipboardManagerImpl::_onClear)); +} + + +/** + * @brief Set the string representation of a 32-bit RGBA color as the clipboard contents + */ +void ClipboardManagerImpl::_setClipboardColor(guint32 color) +{ + gchar colorstr[16]; + g_snprintf(colorstr, 16, "%08x", color); + _clipboard->set_text(colorstr); +} + + +/** + * @brief Put a notification on the mesage stack + */ +void ClipboardManagerImpl::_userWarn(SPDesktop *desktop, char const *msg) +{ + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, msg); +} + + + +/* ####################################### + ClipboardManager class + ####################################### */ + +ClipboardManager *ClipboardManager::_instance = NULL; + +ClipboardManager::ClipboardManager() {} +ClipboardManager::~ClipboardManager() {} +ClipboardManager *ClipboardManager::get() +{ + if ( _instance == NULL ) + _instance = new ClipboardManagerImpl; + return _instance; +} + +} // namespace Inkscape +} // namespace IO + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : diff --git a/src/ui/clipboard.h b/src/ui/clipboard.h new file mode 100644 index 000000000..6db6be715 --- /dev/null +++ b/src/ui/clipboard.h @@ -0,0 +1,74 @@ +#ifndef SEEN_INKSCAPE_CLIPBOARD_H +#define SEEN_INKSCAPE_CLIPBOARD_H + +/** @file + * @brief System-wide clipboard management - class declaration + */ +/* Authors: + * Krzysztof Kosiński + * + * Copyright (C) 2008 authors + * + * 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. + * + * See the file COPYING for details. + */ + +#include + +// forward declarations +class SPDesktop; +namespace Inkscape { +class Selection; +namespace LivePathEffect { class PathParam; } + +namespace UI { + +/** + * @brief System-wide clipboard manager + * + * ClipboardManager takes care of manipulating the system clipboard in response + * to user actions. It holds a complete SPDocument as the contents. This document + * is exported using output extensions when other applications request data. + * Copying to another instance of Inkscape is special-cased, because of the extra + * data required (i.e. style, size, Live Path Effects parameters, etc.) + */ + +class ClipboardManager { +public: + virtual void copy() = 0; + virtual void copyPathParameter(Inkscape::LivePathEffect::PathParam *) = 0; + virtual bool paste(bool in_place = false) = 0; + virtual bool pasteStyle() = 0; + virtual bool pasteSize(bool separately, bool apply_x, bool apply_y) = 0; + virtual bool pastePathEffect() = 0; + virtual Glib::ustring getPathParameter() = 0; + + static ClipboardManager *get(); +protected: + ClipboardManager(); // singleton + virtual ~ClipboardManager(); +private: + ClipboardManager(const ClipboardManager &); ///< no copy + ClipboardManager &operator=(const ClipboardManager &); ///< no assign + + static ClipboardManager *_instance; +}; + +} // namespace IO +} // namespace Inkscape + +#endif +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :