diff options
Diffstat (limited to 'src/ui/clipboard.cpp')
| -rw-r--r-- | src/ui/clipboard.cpp | 406 |
1 files changed, 256 insertions, 150 deletions
diff --git a/src/ui/clipboard.cpp b/src/ui/clipboard.cpp index af0ec0129..dd1f981b5 100644 --- a/src/ui/clipboard.cpp +++ b/src/ui/clipboard.cpp @@ -3,9 +3,11 @@ */ /* Authors: * Krzysztof KosiĆski <tweenk@o2.pl> + * Jon A. Cruz <jon@joncruz.org> * Incorporates some code from selection-chemistry.cpp, see that file for more credits. * * Copyright (C) 2008 authors + * Copyright (C) 2010 Jon A. Cruz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -69,7 +71,6 @@ #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 "preferences.h" // for used in _pasteImage #include "text-context.h" #include "text-editing.h" #include "tools-switch.h" @@ -105,14 +106,14 @@ namespace UI { */ class ClipboardManagerImpl : public ClipboardManager { public: - virtual void copy(); + virtual void copy(SPDesktop *desktop); 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(); - virtual Glib::ustring getShapeOrTextObjectId(); + virtual bool paste(SPDesktop *desktop, bool in_place); + virtual bool pasteStyle(SPDesktop *desktop); + virtual bool pasteSize(SPDesktop *desktop, bool separately, bool apply_x, bool apply_y); + virtual bool pastePathEffect(SPDesktop *desktop); + virtual Glib::ustring getPathParameter(SPDesktop* desktop); + virtual Glib::ustring getShapeOrTextObjectId(SPDesktop *desktop); virtual const gchar *getFirstObjectID(); ClipboardManagerImpl(); @@ -126,10 +127,10 @@ private: 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(); + void _pasteDocument(SPDesktop *desktop, SPDocument *clipdoc, bool in_place); + void _pasteDefs(SPDesktop *desktop, SPDocument *clipdoc); + bool _pasteImage(SPDocument *doc); + bool _pasteText(SPDesktop *desktop); SPCSSAttr *_parseColor(const Glib::ustring &); void _applyPathEffect(SPItem *, gchar const *); SPDocument *_retrieveClipboard(Glib::ustring = ""); @@ -142,7 +143,7 @@ private: void _createInternalClipboard(); void _discardInternalClipboard(); Inkscape::XML::Node *_createClipNode(); - Geom::Scale _getScale(Geom::Point const &, Geom::Point const &, Geom::Rect const &, bool, bool); + Geom::Scale _getScale(SPDesktop *desktop, Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y); Glib::ustring _getBestTarget(); void _setClipboardTargets(); void _setClipboardColor(guint32); @@ -193,10 +194,11 @@ ClipboardManagerImpl::~ClipboardManagerImpl() {} /** * @brief Copy selection contents to the clipboard */ -void ClipboardManagerImpl::copy() +void ClipboardManagerImpl::copy(SPDesktop *desktop) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if ( desktop == NULL ) return; + if ( desktop == NULL ) { + return; + } Inkscape::Selection *selection = sp_desktop_selection(desktop); // Special case for when the gradient dragger is active - copies gradient color @@ -220,7 +222,9 @@ void ClipboardManagerImpl::copy() g_snprintf(color_str, 16, "#%06x", col >> 8); sp_repr_css_set_property(_text_style, "fill", color_str); float opacity = SP_RGBA32_A_F(col); - if (opacity > 1.0) opacity = 1.0; // safeguard + if (opacity > 1.0) { + opacity = 1.0; // safeguard + } Inkscape::CSSOStringStream opcss; opcss << opacity; sp_repr_css_set_property(_text_style, "opacity", opcss.str().data()); @@ -273,9 +277,13 @@ void ClipboardManagerImpl::copy() */ void ClipboardManagerImpl::copyPathParameter(Inkscape::LivePathEffect::PathParam *pp) { - if ( pp == NULL ) return; + if ( pp == NULL ) { + return; + } gchar *svgd = sp_svg_write_path( pp->get_pathvector() ); - if ( svgd == NULL || *svgd == '\0' ) return; + if ( svgd == NULL || *svgd == '\0' ) { + return; + } _discardInternalClipboard(); _createInternalClipboard(); @@ -294,12 +302,15 @@ void ClipboardManagerImpl::copyPathParameter(Inkscape::LivePathEffect::PathParam * @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) +bool ClipboardManagerImpl::paste(SPDesktop *desktop, 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; + if ( desktop == NULL ) { + return false; + } + if ( Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false ) { + return false; + } Glib::ustring target = _getBestTarget(); @@ -308,9 +319,13 @@ bool ClipboardManagerImpl::paste(bool in_place) // 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 ( target == CLIPBOARD_GDK_PIXBUF_TARGET ) { + return _pasteImage(desktop->doc()); + } // if there's only text, paste it into a selected text object or create a new one - if ( target == CLIPBOARD_TEXT_TARGET ) return _pasteText(); + if ( target == CLIPBOARD_TEXT_TARGET ) { + return _pasteText(desktop); + } // otherwise, use the import extensions SPDocument *tempdoc = _retrieveClipboard(target); @@ -319,7 +334,7 @@ bool ClipboardManagerImpl::paste(bool in_place) return false; } - _pasteDocument(tempdoc, in_place); + _pasteDocument(desktop, tempdoc, in_place); sp_document_unref(tempdoc); return true; @@ -338,8 +353,9 @@ const gchar *ClipboardManagerImpl::getFirstObjectID() Inkscape::XML::Node *root = sp_document_repr_root(tempdoc); - if (!root) + if (!root) { return NULL; + } Inkscape::XML::Node *ch = sp_repr_children(root); while (ch != NULL && @@ -349,8 +365,9 @@ const gchar *ClipboardManagerImpl::getFirstObjectID() strcmp(ch->name(), "svg:text") && strcmp(ch->name(), "svg:image") && strcmp(ch->name(), "svg:rect") - ) + ) { ch = ch->next(); + } if (ch) { return ch->attribute("id"); @@ -363,10 +380,11 @@ const gchar *ClipboardManagerImpl::getFirstObjectID() /** * @brief Implements the Paste Style action */ -bool ClipboardManagerImpl::pasteStyle() +bool ClipboardManagerImpl::pasteStyle(SPDesktop *desktop) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop == NULL) return false; + if (desktop == NULL) { + return false; + } // check whether something is selected Inkscape::Selection *selection = sp_desktop_selection(desktop); @@ -394,7 +412,7 @@ bool ClipboardManagerImpl::pasteStyle() bool pasted = false; if (clipnode) { - _pasteDefs(tempdoc); + _pasteDefs(desktop, tempdoc); SPCSSAttr *style = sp_repr_css_attr(clipnode, "style"); sp_desktop_set_style(desktop, style); pasted = true; @@ -414,12 +432,15 @@ bool ClipboardManagerImpl::pasteStyle() * @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) +bool ClipboardManagerImpl::pasteSize(SPDesktop *desktop, bool separately, bool apply_x, bool apply_y) { - if(!apply_x && !apply_y) return false; // pointless parameters + if (!apply_x && !apply_y) { + return false; // pointless parameters + } - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if ( desktop == NULL ) return false; + if ( desktop == NULL ) { + return false; + } Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection->isEmpty()) { _userWarn(desktop, _("Select <b>object(s)</b> to paste size to.")); @@ -447,8 +468,10 @@ bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y for (GSList *i = const_cast<GSList*>(selection->itemList()) ; i ; i = i->next) { SPItem *item = SP_ITEM(i->data); Geom::OptRect obj_size = sp_item_bbox_desktop(item); - if ( !obj_size ) continue; - sp_item_scale_rel(item, _getScale(min, max, *obj_size, apply_x, apply_y)); + if ( !obj_size ) { + continue; + } + sp_item_scale_rel(item, _getScale(desktop, min, max, *obj_size, apply_x, apply_y)); } } // resize the selection as a whole @@ -456,7 +479,7 @@ bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y Geom::OptRect sel_size = selection->bounds(); if ( sel_size ) { sp_selection_scale_relative(selection, sel_size->midpoint(), - _getScale(min, max, *sel_size, apply_x, apply_y)); + _getScale(desktop, min, max, *sel_size, apply_x, apply_y)); } } pasted = true; @@ -469,14 +492,14 @@ bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y /** * @brief Applies a path effect from the clipboard to the selected path */ -bool ClipboardManagerImpl::pastePathEffect() +bool ClipboardManagerImpl::pastePathEffect(SPDesktop *desktop) { /** @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 ) + if ( desktop == NULL ) { return false; + } Inkscape::Selection *selection = sp_desktop_selection(desktop); if (selection && selection->isEmpty()) { @@ -489,13 +512,14 @@ bool ClipboardManagerImpl::pastePathEffect() Inkscape::XML::Node *root = sp_document_repr_root(tempdoc); Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1); if ( clipnode ) { - gchar const *effect = clipnode->attribute("inkscape:path-effect"); - if ( effect ) { - _pasteDefs(tempdoc); + gchar const *effectstack = clipnode->attribute("inkscape:path-effect"); + if ( effectstack ) { + _pasteDefs(desktop, tempdoc); // make sure all selected items are converted to paths first (i.e. rectangles) - sp_selected_path_to_curves(desktop, false); - for (GSList *item = const_cast<GSList *>(selection->itemList()) ; item ; item = item->next) { - _applyPathEffect(reinterpret_cast<SPItem*>(item->data), effect); + sp_selected_to_lpeitems(desktop); + for (GSList *itemptr = const_cast<GSList *>(selection->itemList()) ; itemptr ; itemptr = itemptr->next) { + SPItem *item = reinterpret_cast<SPItem*>(itemptr->data); + _applyPathEffect(item, effectstack); } return true; @@ -513,18 +537,18 @@ bool ClipboardManagerImpl::pastePathEffect() * @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() +Glib::ustring ClipboardManagerImpl::getPathParameter(SPDesktop* desktop) { SPDocument *tempdoc = _retrieveClipboard(); // any target will do here if ( tempdoc == NULL ) { - _userWarn(SP_ACTIVE_DESKTOP, _("Nothing on the clipboard.")); + _userWarn(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.")); + _userWarn(desktop, _("Clipboard does not contain a path.")); sp_document_unref(tempdoc); return ""; } @@ -537,21 +561,22 @@ Glib::ustring ClipboardManagerImpl::getPathParameter() * @brief Get object id of a shape or text item from the clipboard * @return The retrieved id string (contents of the id attribute), or "" if no shape or text item was found */ -Glib::ustring ClipboardManagerImpl::getShapeOrTextObjectId() +Glib::ustring ClipboardManagerImpl::getShapeOrTextObjectId(SPDesktop *desktop) { SPDocument *tempdoc = _retrieveClipboard(); // any target will do here if ( tempdoc == NULL ) { - _userWarn(SP_ACTIVE_DESKTOP, _("Nothing on the clipboard.")); + _userWarn(desktop, _("Nothing on the clipboard.")); return ""; } Inkscape::XML::Node *root = sp_document_repr_root(tempdoc); Inkscape::XML::Node *repr = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth - if ( repr == NULL ) + if ( repr == NULL ) { repr = sp_repr_lookup_name(root, "svg:text", -1); + } if ( repr == NULL ) { - _userWarn(SP_ACTIVE_DESKTOP, _("Clipboard does not contain a path.")); + _userWarn(desktop, _("Clipboard does not contain a path.")); sp_document_unref(tempdoc); return ""; } @@ -576,7 +601,9 @@ void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection) 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; + 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); @@ -595,7 +622,7 @@ void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection) // copy style for Paste Style action if (sorted_items) { - if(SP_IS_ITEM(sorted_items->data)) { + 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); @@ -630,17 +657,21 @@ void ClipboardManagerImpl::_copyUsedDefs(SPItem *item) if (style && (style->fill.isPaintserver())) { SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item); - if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server)) + if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server)) { _copyGradient(SP_GRADIENT(server)); - if (SP_IS_PATTERN(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)) + if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server)) { _copyGradient(SP_GRADIENT(server)); - if (SP_IS_PATTERN(server)) + } + if (SP_IS_PATTERN(server)) { _copyPattern(SP_PATTERN(server)); + } } // For shapes, copy all of the shape's markers @@ -659,8 +690,9 @@ void ClipboardManagerImpl::_copyUsedDefs(SPItem *item) for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it) { LivePathEffectObject *lpeobj = (*it)->lpeobject; - if (lpeobj) + if (lpeobj) { _copyNode(SP_OBJECT_REPR(SP_OBJECT(lpeobj)), _doc, _defs); + } } } } @@ -682,8 +714,9 @@ void ClipboardManagerImpl::_copyUsedDefs(SPItem *item) _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)) + if (SP_IS_ITEM(o)) { _copyUsedDefs(SP_ITEM(o)); + } } } // Copy filters @@ -696,8 +729,9 @@ void ClipboardManagerImpl::_copyUsedDefs(SPItem *item) // recurse for (SPObject *o = SP_OBJECT(item)->children ; o != NULL ; o = o->next) { - if (SP_IS_ITEM(o)) + if (SP_IS_ITEM(o)) { _copyUsedDefs(SP_ITEM(o)); + } } } @@ -726,7 +760,9 @@ void ClipboardManagerImpl::_copyPattern(SPPattern *pattern) // 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; + if (!SP_IS_ITEM (child)) { + continue; + } _copyUsedDefs(SP_ITEM(child)); } pattern = pattern->ref->getObject(); @@ -740,11 +776,15 @@ void ClipboardManagerImpl::_copyPattern(SPPattern *pattern) void ClipboardManagerImpl::_copyTextPath(SPTextPath *tp) { SPItem *path = sp_textpath_get_path_item(tp); - if(!path) return; + 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; + if (sp_repr_lookup_child(_root, "id", path_node->attribute("id"))) { + return; + } _copyNode(path_node, _doc, _defs); } @@ -771,9 +811,8 @@ Inkscape::XML::Node *ClipboardManagerImpl::_copyNode(Inkscape::XML::Node *node, * @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) +void ClipboardManagerImpl::_pasteDocument(SPDesktop *desktop, 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), @@ -781,16 +820,24 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place) Inkscape::XML::Document *target_xmldoc = sp_document_repr_doc(target_document); // copy definitions - _pasteDefs(clipdoc); + _pasteDefs(desktop, 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; + 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); } @@ -822,11 +869,12 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place) if (!in_place) { SnapManager &m = desktop->namedview->snap_manager; - m.setup(desktop, false); // Don't display the snapindicator + m.setup(desktop); + sp_event_context_discard_delayed_snap_event(desktop->event_context); // get offset from mouse pointer to bbox center, snap to grid if enabled Geom::Point mouse_offset = desktop->point() - sel_bbox->midpoint(); - offset = m.multipleOfGridPitch(mouse_offset - offset) + offset; + offset = m.multipleOfGridPitch(mouse_offset - offset, sel_bbox->midpoint() + offset) + offset; } sp_selection_move_relative(selection, offset); @@ -841,10 +889,9 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place) * @param clipdoc The document to paste * @pre @c clipdoc != NULL and pasting into the active document is possible */ -void ClipboardManagerImpl::_pasteDefs(SPDocument *clipdoc) +void ClipboardManagerImpl::_pasteDefs(SPDesktop *desktop, 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), @@ -863,35 +910,38 @@ void ClipboardManagerImpl::_pasteDefs(SPDocument *clipdoc) /** * @brief Retrieve a bitmap image from the clipboard and paste it into the active document */ -bool ClipboardManagerImpl::_pasteImage() +bool ClipboardManagerImpl::_pasteImage(SPDocument *doc) { - SPDocument *doc = SP_ACTIVE_DOCUMENT; - if ( doc == NULL ) return false; + if ( doc == NULL ) { + return false; + } // retrieve image data Glib::RefPtr<Gdk::Pixbuf> 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]; - - time(&rawtime); - strftime(image_filename, 128, "inkscape_pasted_image_%Y%m%d_%H%M%S.png", localtime( &rawtime )); - /// @todo Check whether the encoding is correct here - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - std::string save_folder = Glib::filename_from_utf8(prefs->getString("/dialogs/save_as/path")); - - gchar *image_path = g_build_filename(save_folder.data(), image_filename, NULL); - img->save(image_path, "png"); - file_import(doc, image_path, NULL); - g_free(image_path); + if (!img) { + return false; + } + + // TODO unify with interface.cpp's sp_ui_drag_data_received() + // AARGH stupid + Inkscape::Extension::DB::InputList o; + Inkscape::Extension::db.get_input_list(o); + Inkscape::Extension::DB::InputList::const_iterator i = o.begin(); + while (i != o.end() && strcmp( (*i)->get_mimetype(), "image/png" ) != 0) { + ++i; + } + Inkscape::Extension::Extension *png = *i; + bool save = (strcmp(png->get_param_optiongroup("link"), "embed") == 0); + png->set_param_optiongroup("link", "embed"); + png->set_gui(false); + + gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-import", NULL ); + img->save(filename, "png"); + file_import(doc, filename, png); + g_free(filename); + + png->set_param_optiongroup("link", save ? "embed" : "link"); + png->set_gui(true); return true; } @@ -899,14 +949,16 @@ bool ClipboardManagerImpl::_pasteImage() /** * @brief Paste text into the selected text object or create a new one to hold it */ -bool ClipboardManagerImpl::_pasteText() +bool ClipboardManagerImpl::_pasteText(SPDesktop *desktop) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if ( desktop == NULL ) return false; + 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)) + 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()); @@ -930,29 +982,43 @@ SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text) Glib::ustring::size_type len = text.bytes(); char *str = const_cast<char *>(text.data()); bool attempt_alpha = false; - if ( !str || ( *str == '\0' ) ) return NULL; // this is OK due to boolean short-circuit + 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; + if ( len < 7 ) { + return NULL; + } + if ( len >= 9 ) { + attempt_alpha = true; + } } else { - if ( len < 6 ) return NULL; - if ( len >= 8 ) attempt_alpha = true; + 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; + 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; + if ( !res || res == EOF ) { + alpha = 0xff; + } } SPCSSAttr *color_css = sp_repr_css_attr_new(); @@ -963,7 +1029,9 @@ SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text) sp_repr_css_set_property(color_css, "fill", color_str); float opacity = static_cast<float>(alpha)/static_cast<float>(0xff); - if (opacity > 1.0) opacity = 1.0; // safeguard + 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()); @@ -976,19 +1044,31 @@ SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text) /** * @brief Applies a pasted path effect to a given item */ -void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effect) +void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effectstack) { - if ( item == NULL ) return; - if ( SP_IS_RECT(item) ) return; + if ( item == NULL ) { + return; + } + if ( SP_IS_RECT(item) ) { + return; + } if (SP_IS_LPE_ITEM(item)) { SPLPEItem *lpeitem = SP_LPE_ITEM(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_lpe_item_add_path_effect(lpeitem, lpeobj); + // for each effect in the stack, check if we need to fork it before adding it to the item + std::istringstream iss(effectstack); + std::string href; + while (std::getline(iss, href, ';')) + { + SPObject *obj = sp_uri_reference_resolve(_clipboardSPDoc, href.c_str()); + if (!obj) { + return; + } + // if the effectstack is not used by anyone, we might as well take it + LivePathEffectObject *lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(1); + sp_lpe_item_add_path_effect(lpeitem, lpeobj); + } } } @@ -1000,10 +1080,11 @@ void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effect) SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_target) { Glib::ustring best_target; - if ( required_target == "" ) + if ( required_target == "" ) { best_target = _getBestTarget(); - else + } else { best_target = required_target; + } if ( best_target == "" ) { return NULL; @@ -1053,15 +1134,18 @@ SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_targ // 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") + 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() ) + for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in) { + }; + if ( in == inlist.end() ) { return NULL; // this shouldn't happen unless _getBestTarget returns something bogus + } SPDocument *tempdoc = NULL; try { @@ -1086,7 +1170,9 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/) g_assert( _clipboardSPDoc != NULL ); Glib::ustring target = sel.get_target(); - if(target == "") return; // this shouldn't happen + if (target == "") { + return; // this shouldn't happen + } if (target == CLIPBOARD_TEXT_TARGET) { target = "image/x-inkscape-svg"; @@ -1095,8 +1181,11 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/) 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() && target != "image/png") return; // this also shouldn't happen + for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out) { + }; + if ( out == outlist.end() && target != "image/png") { + 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 @@ -1117,10 +1206,12 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/) // read from namedview Inkscape::XML::Node *nv = sp_repr_lookup_name (_clipboardSPDoc->rroot, "sodipodi:namedview"); - if (nv && nv->attribute("pagecolor")) + if (nv && nv->attribute("pagecolor")) { bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00); - if (nv && nv->attribute("inkscape:pageopacity")) + } + if (nv && nv->attribute("inkscape:pageopacity")) { bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0)); + } sp_export_png_file(_clipboardSPDoc, filename, area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL); } @@ -1200,9 +1291,8 @@ void ClipboardManagerImpl::_discardInternalClipboard() /** * @brief Get the scale to resize an item, based on the command and desktop state */ -Geom::Scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y) +Geom::Scale ClipboardManagerImpl::_getScale(SPDesktop *desktop, Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; double scale_x = 1.0; double scale_y = 1.0; @@ -1215,8 +1305,12 @@ Geom::Scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point // 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; + if (apply_x && !apply_y) { + scale_y = scale_x; + } + if (apply_y && !apply_x) { + scale_x = scale_y; + } } return Geom::Scale(scale_x, scale_y); @@ -1240,36 +1334,43 @@ Glib::ustring ClipboardManagerImpl::_getBestTarget() g_debug("End clipboard targets\n"); //*/ - for(std::list<Glib::ustring>::iterator i = _preferred_targets.begin() ; + for (std::list<Glib::ustring>::iterator i = _preferred_targets.begin() ; i != _preferred_targets.end() ; ++i) { - if ( std::find(targets.begin(), targets.end(), *i) != targets.end() ) + if ( std::find(targets.begin(), targets.end(), *i) != targets.end() ) { return *i; + } } #ifdef WIN32 if (OpenClipboard(NULL)) { // If both bitmap and metafile are present, pick the one that was exported first. UINT format = EnumClipboardFormats(0); while (format) { - if (format == CF_ENHMETAFILE || format == CF_DIB || format == CF_BITMAP) + if (format == CF_ENHMETAFILE || format == CF_DIB || format == CF_BITMAP) { break; + } format = EnumClipboardFormats(format); } CloseClipboard(); - if (format == CF_ENHMETAFILE) + if (format == CF_ENHMETAFILE) { return CLIPBOARD_WIN32_EMF_TARGET; - if (format == CF_DIB || format == CF_BITMAP) + } + if (format == CF_DIB || format == CF_BITMAP) { return CLIPBOARD_GDK_PIXBUF_TARGET; + } } - if (IsClipboardFormatAvailable(CF_ENHMETAFILE)) + if (IsClipboardFormatAvailable(CF_ENHMETAFILE)) { return CLIPBOARD_WIN32_EMF_TARGET; + } #endif - if (_clipboard->wait_is_image_available()) + if (_clipboard->wait_is_image_available()) { return CLIPBOARD_GDK_PIXBUF_TARGET; - if (_clipboard->wait_is_text_available()) + } + if (_clipboard->wait_is_text_available()) { return CLIPBOARD_TEXT_TARGET; + } return ""; } @@ -1324,7 +1425,8 @@ void ClipboardManagerImpl::_setClipboardTargets() 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); + for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out) { + } if ( out != outlist.end() ) { // FIXME: Temporary hack until we add support for memory output. // Save to a temporary file, read it back and then set the clipboard contents @@ -1382,18 +1484,20 @@ void ClipboardManagerImpl::_inkscape_wait_for_targets(std::list<Glib::ustring> & GdkAtom* targets = 0; gint n_targets = 0; gboolean test = gtk_clipboard_wait_for_targets( gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), &targets, &n_targets ); - if(!test) + if (!test) { n_targets = 0; //otherwise it will be -1. + } //Add the targets to the C++ container: - for(int i = 0; i < n_targets; i++) + for (int i = 0; i < n_targets; i++) { //Convert the atom to a string: gchar* const atom_name = gdk_atom_name(targets[i]); Glib::ustring target; - if(atom_name) + if (atom_name) { target = Glib::ScopedPtr<char>(atom_name).get(); //This frees the gchar*. + } listTargets.push_back(target); } @@ -1409,8 +1513,10 @@ ClipboardManager::ClipboardManager() {} ClipboardManager::~ClipboardManager() {} ClipboardManager *ClipboardManager::get() { - if ( _instance == NULL ) + if ( _instance == NULL ) { _instance = new ClipboardManagerImpl; + } + return _instance; } |
