1 /** @file
2 * @brief System-wide clipboard management - implementation
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk@o2.pl>
6 * Incorporates some code from selection-chemistry.cpp, see that file for more credits.
7 *
8 * Copyright (C) 2008 authors
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * See the file COPYING for details.
16 */
18 #include "ui/clipboard.h"
20 // TODO: reduce header bloat if possible
22 #include <list>
23 #include <algorithm>
24 #include <gtkmm/clipboard.h>
25 #include <glibmm/ustring.h>
26 #include <glibmm/i18n.h>
27 #include <glib/gstdio.h> // for g_file_set_contents etc., used in _onGet and paste
28 #include "gc-core.h"
29 #include "xml/repr.h"
30 #include "inkscape.h"
31 #include "io/stringstream.h"
32 #include "desktop.h"
33 #include "desktop-handles.h"
34 #include "desktop-style.h" // for sp_desktop_set_style, used in _pasteStyle
35 #include "document.h"
36 #include "document-private.h"
37 #include "selection.h"
38 #include "message-stack.h"
39 #include "context-fns.h"
40 #include "dropper-context.h" // used in copy()
41 #include "style.h"
42 #include "extension/db.h" // extension database
43 #include "extension/input.h"
44 #include "extension/output.h"
45 #include "selection-chemistry.h"
46 #include "libnr/nr-rect.h"
47 #include "libnr/nr-convert2geom.h"
48 #include <2geom/rect.h>
49 #include <2geom/transforms.h>
50 #include "box3d.h"
51 #include "gradient-drag.h"
52 #include "sp-item.h"
53 #include "sp-item-transform.h" // for sp_item_scale_rel, used in _pasteSize
54 #include "sp-path.h"
55 #include "sp-pattern.h"
56 #include "sp-shape.h"
57 #include "sp-gradient.h"
58 #include "sp-gradient-reference.h"
59 #include "sp-gradient-fns.h"
60 #include "sp-linear-gradient-fns.h"
61 #include "sp-radial-gradient-fns.h"
62 #include "sp-clippath.h"
63 #include "sp-mask.h"
64 #include "sp-textpath.h"
65 #include "sp-rect.h"
66 #include "live_effects/lpeobject.h"
67 #include "live_effects/lpeobject-reference.h"
68 #include "live_effects/parameter/path.h"
69 #include "svg/svg.h" // for sp_svg_transform_write, used in _copySelection
70 #include "svg/css-ostringstream.h" // used in _parseColor
71 #include "file.h" // for file_import, used in _pasteImage
72 #include "prefs-utils.h" // for prefs_get_string_attribute, used in _pasteImage
73 #include "text-context.h"
74 #include "text-editing.h"
75 #include "tools-switch.h"
76 #include "path-chemistry.h"
77 #include "id-clash.h"
78 #include "unit-constants.h"
79 #include "helper/png-write.h"
80 #include "svg/svg-color.h"
82 /// @brief Made up mimetype to represent Gdk::Pixbuf clipboard contents
83 #define CLIPBOARD_GDK_PIXBUF_TARGET "image/x-gdk-pixbuf"
85 #define CLIPBOARD_TEXT_TARGET "text/plain"
87 #ifdef WIN32
88 #include <windows.h>
89 // Clipboard Formats: http://msdn.microsoft.com/en-us/library/ms649013(VS.85).aspx
90 // On Windows, most graphical applications can handle CF_DIB/CF_BITMAP and/or CF_ENHMETAFILE
91 // GTK automatically presents an "image/bmp" target as CF_DIB/CF_BITMAP
92 // Presenting "image/x-emf" as CF_ENHMETAFILE must be done by Inkscape ?
93 #define CLIPBOARD_WIN32_EMF_TARGET "CF_ENHMETAFILE"
94 #define CLIPBOARD_WIN32_EMF_MIME "image/x-emf"
95 #endif
97 namespace Inkscape {
98 namespace UI {
101 /**
102 * @brief Default implementation of the clipboard manager
103 */
104 class ClipboardManagerImpl : public ClipboardManager {
105 public:
106 virtual void copy();
107 virtual void copyPathParameter(Inkscape::LivePathEffect::PathParam *);
108 virtual bool paste(bool in_place);
109 virtual bool pasteStyle();
110 virtual bool pasteSize(bool, bool, bool);
111 virtual bool pastePathEffect();
112 virtual Glib::ustring getPathParameter();
113 virtual Glib::ustring getShapeOrTextObjectId();
114 virtual const gchar *getFirstObjectID();
116 ClipboardManagerImpl();
117 ~ClipboardManagerImpl();
119 private:
120 void _copySelection(Inkscape::Selection *);
121 void _copyUsedDefs(SPItem *);
122 void _copyGradient(SPGradient *);
123 void _copyPattern(SPPattern *);
124 void _copyTextPath(SPTextPath *);
125 Inkscape::XML::Node *_copyNode(Inkscape::XML::Node *, Inkscape::XML::Document *, Inkscape::XML::Node *);
127 void _pasteDocument(SPDocument *, bool in_place);
128 void _pasteDefs(SPDocument *);
129 bool _pasteImage();
130 bool _pasteText();
131 SPCSSAttr *_parseColor(const Glib::ustring &);
132 void _applyPathEffect(SPItem *, gchar const *);
133 SPDocument *_retrieveClipboard(Glib::ustring = "");
135 // clipboard callbacks
136 void _onGet(Gtk::SelectionData &, guint);
137 void _onClear();
139 // various helpers
140 void _createInternalClipboard();
141 void _discardInternalClipboard();
142 Inkscape::XML::Node *_createClipNode();
143 NR::scale _getScale(Geom::Point const &, Geom::Point const &, Geom::Rect const &, bool, bool);
144 Glib::ustring _getBestTarget();
145 void _setClipboardTargets();
146 void _setClipboardColor(guint32);
147 void _userWarn(SPDesktop *, char const *);
149 // private properites
150 SPDocument *_clipboardSPDoc; ///< Document that stores the clipboard until someone requests it
151 Inkscape::XML::Node *_defs; ///< Reference to the clipboard document's defs node
152 Inkscape::XML::Node *_root; ///< Reference to the clipboard's root node
153 Inkscape::XML::Node *_clipnode; ///< The node that holds extra information
154 Inkscape::XML::Document *_doc; ///< Reference to the clipboard's Inkscape::XML::Document
156 Glib::RefPtr<Gtk::Clipboard> _clipboard; ///< Handle to the system wide clipboard - for convenience
157 std::list<Glib::ustring> _preferred_targets; ///< List of supported clipboard targets
158 };
161 ClipboardManagerImpl::ClipboardManagerImpl()
162 : _clipboardSPDoc(NULL),
163 _defs(NULL),
164 _root(NULL),
165 _clipnode(NULL),
166 _doc(NULL),
167 _clipboard( Gtk::Clipboard::get() )
168 {
169 // push supported clipboard targets, in order of preference
170 _preferred_targets.push_back("image/x-inkscape-svg");
171 _preferred_targets.push_back("image/svg+xml");
172 _preferred_targets.push_back("image/svg+xml-compressed");
173 #ifdef WIN32
174 _preferred_targets.push_back(CLIPBOARD_WIN32_EMF_MIME);
175 #endif
176 _preferred_targets.push_back("application/pdf");
177 _preferred_targets.push_back("image/x-adobe-illustrator");
178 }
181 ClipboardManagerImpl::~ClipboardManagerImpl() {}
184 /**
185 * @brief Copy selection contents to the clipboard
186 */
187 void ClipboardManagerImpl::copy()
188 {
189 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
190 if ( desktop == NULL ) return;
191 Inkscape::Selection *selection = sp_desktop_selection(desktop);
193 // Special case for when the gradient dragger is active - copies gradient color
194 if (desktop->event_context->get_drag()) {
195 GrDrag *drag = desktop->event_context->get_drag();
196 if (drag->hasSelection()) {
197 _setClipboardColor(drag->getColor());
198 _discardInternalClipboard();
199 return;
200 }
201 }
203 // Special case for when the color picker ("dropper") is active - copies color under cursor
204 if (tools_isactive(desktop, TOOLS_DROPPER)) {
205 _setClipboardColor(sp_dropper_context_get_color(desktop->event_context));
206 _discardInternalClipboard();
207 return;
208 }
210 // Special case for when the text tool is active - if some text is selected, copy plain text,
211 // not the object that holds it
212 if (tools_isactive(desktop, TOOLS_TEXT)) {
213 Glib::ustring selected_text = sp_text_get_selected_text(desktop->event_context);
214 if (!selected_text.empty()) {
215 _clipboard->set_text(selected_text);
216 _discardInternalClipboard();
217 return;
218 }
219 }
221 if (selection->isEmpty()) { // check whether something is selected
222 _userWarn(desktop, _("Nothing was copied."));
223 return;
224 }
225 _discardInternalClipboard();
227 _createInternalClipboard(); // construct a new clipboard document
228 _copySelection(selection); // copy all items in the selection to the internal clipboard
229 fit_canvas_to_drawing(_clipboardSPDoc);
231 _setClipboardTargets();
232 }
235 /**
236 * @brief Copy a Live Path Effect path parameter to the clipboard
237 * @param pp The path parameter to store in the clipboard
238 */
239 void ClipboardManagerImpl::copyPathParameter(Inkscape::LivePathEffect::PathParam *pp)
240 {
241 if ( pp == NULL ) return;
242 gchar *svgd = sp_svg_write_path( pp->get_pathvector() );
243 if ( svgd == NULL || *svgd == '\0' ) return;
245 _discardInternalClipboard();
246 _createInternalClipboard();
248 Inkscape::XML::Node *pathnode = _doc->createElement("svg:path");
249 pathnode->setAttribute("d", svgd);
250 g_free(svgd);
251 _root->appendChild(pathnode);
252 Inkscape::GC::release(pathnode);
254 fit_canvas_to_drawing(_clipboardSPDoc);
255 _setClipboardTargets();
256 }
258 /**
259 * @brief Paste from the system clipboard into the active desktop
260 * @param in_place Whether to put the contents where they were when copied
261 */
262 bool ClipboardManagerImpl::paste(bool in_place)
263 {
264 // do any checking whether we really are able to paste before requesting the contents
265 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
266 if ( desktop == NULL ) return false;
267 if ( Inkscape::have_viable_layer(desktop, desktop->messageStack()) == false ) return false;
269 Glib::ustring target = _getBestTarget();
271 // Special cases of clipboard content handling go here 00ff00
272 // Note that target priority is determined in _getBestTarget.
273 // TODO: Handle x-special/gnome-copied-files and text/uri-list to support pasting files
275 // if there is an image on the clipboard, paste it
276 if ( target == CLIPBOARD_GDK_PIXBUF_TARGET ) return _pasteImage();
277 // if there's only text, paste it into a selected text object or create a new one
278 if ( target == CLIPBOARD_TEXT_TARGET ) return _pasteText();
280 // otherwise, use the import extensions
281 SPDocument *tempdoc = _retrieveClipboard(target);
282 if ( tempdoc == NULL ) {
283 _userWarn(desktop, _("Nothing on the clipboard."));
284 return false;
285 }
287 _pasteDocument(tempdoc, in_place);
288 sp_document_unref(tempdoc);
290 return true;
291 }
293 /**
294 * @brief Returns the id of the first visible copied object
295 */
296 const gchar *ClipboardManagerImpl::getFirstObjectID()
297 {
298 SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
299 if ( tempdoc == NULL ) {
300 return NULL;
301 }
303 Inkscape::XML::Node
304 *root = sp_document_repr_root(tempdoc);
306 if (!root)
307 return NULL;
309 Inkscape::XML::Node *ch = sp_repr_children(root);
310 while (ch != NULL &&
311 strcmp(ch->name(), "svg:g") &&
312 strcmp(ch->name(), "svg:path") &&
313 strcmp(ch->name(), "svg:use") &&
314 strcmp(ch->name(), "svg:text") &&
315 strcmp(ch->name(), "svg:image") &&
316 strcmp(ch->name(), "svg:rect")
317 )
318 ch = ch->next();
320 if (ch) {
321 return ch->attribute("id");
322 }
324 return NULL;
325 }
328 /**
329 * @brief Implements the Paste Style action
330 */
331 bool ClipboardManagerImpl::pasteStyle()
332 {
333 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
334 if (desktop == NULL) return false;
336 // check whether something is selected
337 Inkscape::Selection *selection = sp_desktop_selection(desktop);
338 if (selection->isEmpty()) {
339 _userWarn(desktop, _("Select <b>object(s)</b> to paste style to."));
340 return false;
341 }
343 SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
344 if ( tempdoc == NULL ) {
345 _userWarn(desktop, _("No style on the clipboard."));
346 return false;
347 }
349 Inkscape::XML::Node
350 *root = sp_document_repr_root(tempdoc),
351 *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
353 bool pasted = false;
355 if (clipnode) {
356 _pasteDefs(tempdoc);
357 SPCSSAttr *style = sp_repr_css_attr(clipnode, "style");
358 sp_desktop_set_style(desktop, style);
359 pasted = true;
360 }
361 else {
362 _userWarn(desktop, _("No style on the clipboard."));
363 }
365 sp_document_unref(tempdoc);
366 return pasted;
367 }
370 /**
371 * @brief Resize the selection or each object in the selection to match the clipboard's size
372 * @param separately Whether to scale each object in the selection separately
373 * @param apply_x Whether to scale the width of objects / selection
374 * @param apply_y Whether to scale the height of objects / selection
375 */
376 bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y)
377 {
378 if(!apply_x && !apply_y) return false; // pointless parameters
380 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
381 if ( desktop == NULL ) return false;
382 Inkscape::Selection *selection = sp_desktop_selection(desktop);
383 if (selection->isEmpty()) {
384 _userWarn(desktop, _("Select <b>object(s)</b> to paste size to."));
385 return false;
386 }
388 // FIXME: actually, this should accept arbitrary documents
389 SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
390 if ( tempdoc == NULL ) {
391 _userWarn(desktop, _("No size on the clipboard."));
392 return false;
393 }
395 // retrieve size ifomration from the clipboard
396 Inkscape::XML::Node *root = sp_document_repr_root(tempdoc);
397 Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
398 bool pasted = false;
399 if (clipnode) {
400 Geom::Point min, max;
401 sp_repr_get_point(clipnode, "min", &min);
402 sp_repr_get_point(clipnode, "max", &max);
404 // resize each object in the selection
405 if (separately) {
406 for (GSList *i = const_cast<GSList*>(selection->itemList()) ; i ; i = i->next) {
407 SPItem *item = SP_ITEM(i->data);
408 boost::optional<NR::Rect> obj_size = sp_item_bbox_desktop(item);
409 if ( !obj_size || obj_size->isEmpty() ) continue;
410 sp_item_scale_rel(item, _getScale(min, max, to_2geom(*obj_size), apply_x, apply_y));
411 }
412 }
413 // resize the selection as a whole
414 else {
415 boost::optional<NR::Rect> sel_size = selection->bounds();
416 if ( sel_size && !sel_size->isEmpty() ) {
417 sp_selection_scale_relative(selection, sel_size->midpoint(),
418 _getScale(min, max, to_2geom(*sel_size), apply_x, apply_y));
419 }
420 }
421 pasted = true;
422 }
423 sp_document_unref(tempdoc);
424 return pasted;
425 }
428 /**
429 * @brief Applies a path effect from the clipboard to the selected path
430 */
431 bool ClipboardManagerImpl::pastePathEffect()
432 {
433 /** @todo FIXME: pastePathEffect crashes when moving the path with the applied effect,
434 segfaulting in fork_private_if_necessary(). */
436 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
437 if ( desktop == NULL )
438 return false;
440 Inkscape::Selection *selection = sp_desktop_selection(desktop);
441 if (selection && selection->isEmpty()) {
442 _userWarn(desktop, _("Select <b>object(s)</b> to paste live path effect to."));
443 return false;
444 }
446 SPDocument *tempdoc = _retrieveClipboard("image/x-inkscape-svg");
447 if ( tempdoc ) {
448 Inkscape::XML::Node *root = sp_document_repr_root(tempdoc);
449 Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
450 if ( clipnode ) {
451 gchar const *effect = clipnode->attribute("inkscape:path-effect");
452 if ( effect ) {
453 _pasteDefs(tempdoc);
454 // make sure all selected items are converted to paths first (i.e. rectangles)
455 sp_selected_path_to_curves(false);
456 for (GSList *item = const_cast<GSList *>(selection->itemList()) ; item ; item = item->next) {
457 _applyPathEffect(reinterpret_cast<SPItem*>(item->data), effect);
458 }
460 return true;
461 }
462 }
463 }
465 // no_effect:
466 _userWarn(desktop, _("No effect on the clipboard."));
467 return false;
468 }
471 /**
472 * @brief Get LPE path data from the clipboard
473 * @return The retrieved path data (contents of the d attribute), or "" if no path was found
474 */
475 Glib::ustring ClipboardManagerImpl::getPathParameter()
476 {
477 SPDocument *tempdoc = _retrieveClipboard(); // any target will do here
478 if ( tempdoc == NULL ) {
479 _userWarn(SP_ACTIVE_DESKTOP, _("Nothing on the clipboard."));
480 return "";
481 }
482 Inkscape::XML::Node
483 *root = sp_document_repr_root(tempdoc),
484 *path = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth
485 if ( path == NULL ) {
486 _userWarn(SP_ACTIVE_DESKTOP, _("Clipboard does not contain a path."));
487 sp_document_unref(tempdoc);
488 return "";
489 }
490 gchar const *svgd = path->attribute("d");
491 return svgd;
492 }
495 /**
496 * @brief Get object id of a shape or text item from the clipboard
497 * @return The retrieved id string (contents of the id attribute), or "" if no shape or text item was found
498 */
499 Glib::ustring ClipboardManagerImpl::getShapeOrTextObjectId()
500 {
501 SPDocument *tempdoc = _retrieveClipboard(); // any target will do here
502 if ( tempdoc == NULL ) {
503 _userWarn(SP_ACTIVE_DESKTOP, _("Nothing on the clipboard."));
504 return "";
505 }
506 Inkscape::XML::Node *root = sp_document_repr_root(tempdoc);
508 Inkscape::XML::Node *repr = sp_repr_lookup_name(root, "svg:path", -1); // unlimited search depth
509 if ( repr == NULL )
510 repr = sp_repr_lookup_name(root, "svg:text", -1);
512 if ( repr == NULL ) {
513 _userWarn(SP_ACTIVE_DESKTOP, _("Clipboard does not contain a path."));
514 sp_document_unref(tempdoc);
515 return "";
516 }
517 gchar const *svgd = repr->attribute("id");
518 return svgd;
519 }
522 /**
523 * @brief Iterate over a list of items and copy them to the clipboard.
524 */
525 void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection)
526 {
527 GSList const *items = selection->itemList();
528 // copy the defs used by all items
529 for (GSList *i = const_cast<GSList *>(items) ; i != NULL ; i = i->next) {
530 _copyUsedDefs(SP_ITEM (i->data));
531 }
533 // copy the representation of the items
534 GSList *sorted_items = g_slist_copy(const_cast<GSList *>(items));
535 sorted_items = g_slist_sort(sorted_items, (GCompareFunc) sp_object_compare_position);
537 for (GSList *i = sorted_items ; i ; i = i->next) {
538 if (!SP_IS_ITEM(i->data)) continue;
539 Inkscape::XML::Node *obj = SP_OBJECT_REPR(i->data);
540 Inkscape::XML::Node *obj_copy = _copyNode(obj, _doc, _root);
542 // copy complete inherited style
543 SPCSSAttr *css = sp_repr_css_attr_inherited(obj, "style");
544 sp_repr_css_set(obj_copy, css, "style");
545 sp_repr_css_attr_unref(css);
547 // write the complete accumulated transform passed to us
548 // (we're dealing with unattached representations, so we write to their attributes
549 // instead of using sp_item_set_transform)
550 gchar *transform_str = sp_svg_transform_write(from_2geom(sp_item_i2doc_affine(SP_ITEM(i->data))));
551 obj_copy->setAttribute("transform", transform_str);
552 g_free(transform_str);
553 }
555 // copy style for Paste Style action
556 if (sorted_items) {
557 if(SP_IS_ITEM(sorted_items->data)) {
558 SPCSSAttr *style = take_style_from_item((SPItem *) sorted_items->data);
559 sp_repr_css_set(_clipnode, style, "style");
560 sp_repr_css_attr_unref(style);
561 }
563 // copy path effect from the first path
564 if (SP_IS_OBJECT(sorted_items->data)) {
565 gchar const *effect = SP_OBJECT_REPR(sorted_items->data)->attribute("inkscape:path-effect");
566 if (effect) {
567 _clipnode->setAttribute("inkscape:path-effect", effect);
568 }
569 }
570 }
572 boost::optional<NR::Rect> size = selection->bounds();
573 if (size) {
574 sp_repr_set_point(_clipnode, "min", size->min());
575 sp_repr_set_point(_clipnode, "max", size->max());
576 }
578 g_slist_free(sorted_items);
579 }
582 /**
583 * @brief Recursively copy all the definitions used by a given item to the clipboard defs
584 */
585 void ClipboardManagerImpl::_copyUsedDefs(SPItem *item)
586 {
587 // copy fill and stroke styles (patterns and gradients)
588 SPStyle *style = SP_OBJECT_STYLE(item);
590 if (style && (style->fill.isPaintserver())) {
591 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
592 if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server))
593 _copyGradient(SP_GRADIENT(server));
594 if (SP_IS_PATTERN(server))
595 _copyPattern(SP_PATTERN(server));
596 }
597 if (style && (style->stroke.isPaintserver())) {
598 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
599 if (SP_IS_LINEARGRADIENT(server) || SP_IS_RADIALGRADIENT(server))
600 _copyGradient(SP_GRADIENT(server));
601 if (SP_IS_PATTERN(server))
602 _copyPattern(SP_PATTERN(server));
603 }
605 // For shapes, copy all of the shape's markers
606 if (SP_IS_SHAPE(item)) {
607 SPShape *shape = SP_SHAPE (item);
608 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
609 if (shape->marker[i]) {
610 _copyNode(SP_OBJECT_REPR(SP_OBJECT(shape->marker[i])), _doc, _defs);
611 }
612 }
613 }
614 // For lpe items, copy lpe stack if applicable
615 if (SP_IS_LPE_ITEM(item)) {
616 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
617 if (sp_lpe_item_has_path_effect(lpeitem)) {
618 for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
619 {
620 LivePathEffectObject *lpeobj = (*it)->lpeobject;
621 if (lpeobj)
622 _copyNode(SP_OBJECT_REPR(SP_OBJECT(lpeobj)), _doc, _defs);
623 }
624 }
625 }
626 // For 3D boxes, copy perspectives
627 if (SP_IS_BOX3D(item)) {
628 _copyNode(SP_OBJECT_REPR(SP_OBJECT(box3d_get_perspective(SP_BOX3D(item)))), _doc, _defs);
629 }
630 // Copy text paths
631 if (SP_IS_TEXT_TEXTPATH(item)) {
632 _copyTextPath(SP_TEXTPATH(sp_object_first_child(SP_OBJECT(item))));
633 }
634 // Copy clipping objects
635 if (item->clip_ref->getObject()) {
636 _copyNode(SP_OBJECT_REPR(item->clip_ref->getObject()), _doc, _defs);
637 }
638 // Copy mask objects
639 if (item->mask_ref->getObject()) {
640 SPObject *mask = item->mask_ref->getObject();
641 _copyNode(SP_OBJECT_REPR(mask), _doc, _defs);
642 // recurse into the mask for its gradients etc.
643 for (SPObject *o = SP_OBJECT(mask)->children ; o != NULL ; o = o->next) {
644 if (SP_IS_ITEM(o))
645 _copyUsedDefs(SP_ITEM(o));
646 }
647 }
648 // Copy filters
649 if (style->getFilter()) {
650 SPObject *filter = style->getFilter();
651 if (SP_IS_FILTER(filter)) {
652 _copyNode(SP_OBJECT_REPR(filter), _doc, _defs);
653 }
654 }
656 // recurse
657 for (SPObject *o = SP_OBJECT(item)->children ; o != NULL ; o = o->next) {
658 if (SP_IS_ITEM(o))
659 _copyUsedDefs(SP_ITEM(o));
660 }
661 }
664 /**
665 * @brief Copy a single gradient to the clipboard's defs element
666 */
667 void ClipboardManagerImpl::_copyGradient(SPGradient *gradient)
668 {
669 while (gradient) {
670 // climb up the refs, copying each one in the chain
671 _copyNode(SP_OBJECT_REPR(gradient), _doc, _defs);
672 gradient = gradient->ref->getObject();
673 }
674 }
677 /**
678 * @brief Copy a single pattern to the clipboard document's defs element
679 */
680 void ClipboardManagerImpl::_copyPattern(SPPattern *pattern)
681 {
682 // climb up the references, copying each one in the chain
683 while (pattern) {
684 _copyNode(SP_OBJECT_REPR(pattern), _doc, _defs);
686 // items in the pattern may also use gradients and other patterns, so recurse
687 for (SPObject *child = sp_object_first_child(SP_OBJECT(pattern)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
688 if (!SP_IS_ITEM (child)) continue;
689 _copyUsedDefs(SP_ITEM(child));
690 }
691 pattern = pattern->ref->getObject();
692 }
693 }
696 /**
697 * @brief Copy a text path to the clipboard's defs element
698 */
699 void ClipboardManagerImpl::_copyTextPath(SPTextPath *tp)
700 {
701 SPItem *path = sp_textpath_get_path_item(tp);
702 if(!path) return;
703 Inkscape::XML::Node *path_node = SP_OBJECT_REPR(path);
705 // Do not copy the text path to defs if it's already copied
706 if(sp_repr_lookup_child(_root, "id", path_node->attribute("id"))) return;
707 _copyNode(path_node, _doc, _defs);
708 }
711 /**
712 * @brief Copy a single XML node from one document to another
713 * @param node The node to be copied
714 * @param target_doc The document to which the node is to be copied
715 * @param parent The node in the target document which will become the parent of the copied node
716 * @return Pointer to the copied node
717 */
718 Inkscape::XML::Node *ClipboardManagerImpl::_copyNode(Inkscape::XML::Node *node, Inkscape::XML::Document *target_doc, Inkscape::XML::Node *parent)
719 {
720 Inkscape::XML::Node *dup = node->duplicate(target_doc);
721 parent->appendChild(dup);
722 Inkscape::GC::release(dup);
723 return dup;
724 }
727 /**
728 * @brief Paste the contents of a document into the active desktop
729 * @param clipdoc The document to paste
730 * @param in_place Whether to paste the selection where it was when copied
731 * @pre @c clipdoc is not empty and items can be added to the current layer
732 */
733 void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place)
734 {
735 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
736 SPDocument *target_document = sp_desktop_document(desktop);
737 Inkscape::XML::Node
738 *root = sp_document_repr_root(clipdoc),
739 *target_parent = SP_OBJECT_REPR(desktop->currentLayer());
740 Inkscape::XML::Document *target_xmldoc = sp_document_repr_doc(target_document);
742 // copy definitions
743 _pasteDefs(clipdoc);
745 // copy objects
746 GSList *pasted_objects = NULL;
747 for (Inkscape::XML::Node *obj = root->firstChild() ; obj ; obj = obj->next()) {
748 // Don't copy metadata, defs, named views and internal clipboard contents to the document
749 if (!strcmp(obj->name(), "svg:defs")) continue;
750 if (!strcmp(obj->name(), "svg:metadata")) continue;
751 if (!strcmp(obj->name(), "sodipodi:namedview")) continue;
752 if (!strcmp(obj->name(), "inkscape:clipboard")) continue;
753 Inkscape::XML::Node *obj_copy = _copyNode(obj, target_xmldoc, target_parent);
754 pasted_objects = g_slist_prepend(pasted_objects, (gpointer) obj_copy);
755 }
757 Inkscape::Selection *selection = sp_desktop_selection(desktop);
758 selection->setReprList(pasted_objects);
760 // move the selection to the right position
761 if(in_place)
762 {
763 Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
764 if (clipnode) {
765 Geom::Point min, max;
766 sp_repr_get_point(clipnode, "min", &min);
767 sp_repr_get_point(clipnode, "max", &max);
769 // this formula was discovered empyrically
770 min[Geom::Y] += ((max[Geom::Y] - min[Geom::Y]) - sp_document_height(target_document));
771 sp_selection_move_relative(selection, Geom::Point(min));
772 }
773 }
774 // copied from former sp_selection_paste in selection-chemistry.cpp
775 else {
776 sp_document_ensure_up_to_date(target_document);
777 boost::optional<NR::Rect> sel_size = selection->bounds();
779 Geom::Point m( desktop->point() );
780 if (sel_size) {
781 m -= sel_size->midpoint();
782 }
783 sp_selection_move_relative(selection, m);
784 }
786 g_slist_free(pasted_objects);
787 }
790 /**
791 * @brief Paste SVG defs from the document retrieved from the clipboard into the active document
792 * @param clipdoc The document to paste
793 * @pre @c clipdoc != NULL and pasting into the active document is possible
794 */
795 void ClipboardManagerImpl::_pasteDefs(SPDocument *clipdoc)
796 {
797 // boilerplate vars copied from _pasteDocument
798 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
799 SPDocument *target_document = sp_desktop_document(desktop);
800 Inkscape::XML::Node
801 *root = sp_document_repr_root(clipdoc),
802 *defs = sp_repr_lookup_name(root, "svg:defs", 1),
803 *target_defs = SP_OBJECT_REPR(SP_DOCUMENT_DEFS(target_document));
804 Inkscape::XML::Document *target_xmldoc = sp_document_repr_doc(target_document);
806 prevent_id_clashes(clipdoc, target_document);
808 for (Inkscape::XML::Node *def = defs->firstChild() ; def ; def = def->next()) {
809 _copyNode(def, target_xmldoc, target_defs);
810 }
811 }
814 /**
815 * @brief Retrieve a bitmap image from the clipboard and paste it into the active document
816 */
817 bool ClipboardManagerImpl::_pasteImage()
818 {
819 SPDocument *doc = SP_ACTIVE_DOCUMENT;
820 if ( doc == NULL ) return false;
822 // retrieve image data
823 Glib::RefPtr<Gdk::Pixbuf> img = _clipboard->wait_for_image();
824 if (!img) return false;
826 // Very stupid hack: Write into a file, then import the file into the document.
827 // To avoid using tmpfile and POSIX file handles, make the filename based on current time.
828 // This wasn't my idea, I just copied this from selection-chemistry.cpp
829 // and just can't think of something saner at the moment. Pasting more than
830 // one image per second will overwrite the image.
831 // However, I don't think anyone is able to copy a _different_ image into inkscape
832 // in 1 second.
833 time_t rawtime;
834 char image_filename[128];
835 gchar const *save_folder;
837 time(&rawtime);
838 strftime(image_filename, 128, "inkscape_pasted_image_%Y%m%d_%H%M%S.png", localtime( &rawtime ));
839 save_folder = (gchar const *) prefs_get_string_attribute("dialogs.save_as", "path");
841 gchar *image_path = g_build_filename(save_folder, image_filename, NULL);
842 img->save(image_path, "png");
843 file_import(doc, image_path, NULL);
844 g_free(image_path);
846 return true;
847 }
849 /**
850 * @brief Paste text into the selected text object or create a new one to hold it
851 */
852 bool ClipboardManagerImpl::_pasteText()
853 {
854 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
855 if ( desktop == NULL ) return false;
857 // if the text editing tool is active, paste the text into the active text object
858 if (tools_isactive(desktop, TOOLS_TEXT))
859 return sp_text_paste_inline(desktop->event_context);
861 // try to parse the text as a color and, if successful, apply it as the current style
862 SPCSSAttr *css = _parseColor(_clipboard->wait_for_text());
863 if (css) {
864 sp_desktop_set_style(desktop, css);
865 return true;
866 }
868 return false;
869 }
872 /**
873 * @brief Attempt to parse the passed string as a hexadecimal RGB or RGBA color
874 * @param text The Glib::ustring to parse
875 * @return New CSS style representation if the parsing was successful, NULL otherwise
876 */
877 SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text)
878 {
879 Glib::ustring::size_type len = text.bytes();
880 char *str = const_cast<char *>(text.data());
881 bool attempt_alpha = false;
882 if ( !str || ( *str == '\0' ) ) return NULL; // this is OK due to boolean short-circuit
884 // those conditionals guard against parsing e.g. the string "fab" as "fab000"
885 // (incomplete color) and "45fab71" as "45fab710" (incomplete alpha)
886 if ( *str == '#' ) {
887 if ( len < 7 ) return NULL;
888 if ( len >= 9 ) attempt_alpha = true;
889 } else {
890 if ( len < 6 ) return NULL;
891 if ( len >= 8 ) attempt_alpha = true;
892 }
894 unsigned int color = 0, alpha = 0xff;
896 // skip a leading #, if present
897 if ( *str == '#' ) ++str;
899 // try to parse first 6 digits
900 int res = sscanf(str, "%6x", &color);
901 if ( res && ( res != EOF ) ) {
902 if (attempt_alpha) {// try to parse alpha if there's enough characters
903 sscanf(str + 6, "%2x", &alpha);
904 if ( !res || res == EOF ) alpha = 0xff;
905 }
907 SPCSSAttr *color_css = sp_repr_css_attr_new();
909 // print and set properties
910 gchar color_str[16];
911 g_snprintf(color_str, 16, "#%06x", color);
912 sp_repr_css_set_property(color_css, "fill", color_str);
914 float opacity = static_cast<float>(alpha)/static_cast<float>(0xff);
915 if (opacity > 1.0) opacity = 1.0; // safeguard
916 Inkscape::CSSOStringStream opcss;
917 opcss << opacity;
918 sp_repr_css_set_property(color_css, "fill-opacity", opcss.str().data());
919 return color_css;
920 }
921 return NULL;
922 }
925 /**
926 * @brief Applies a pasted path effect to a given item
927 */
928 void ClipboardManagerImpl::_applyPathEffect(SPItem *item, gchar const *effect)
929 {
930 if ( item == NULL ) return;
931 if ( SP_IS_RECT(item) ) return;
933 if (SP_IS_LPE_ITEM(item))
934 {
935 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
936 SPObject *obj = sp_uri_reference_resolve(_clipboardSPDoc, effect);
937 if (!obj) return;
938 // if the effect is not used by anyone, we might as well take it
939 LivePathEffectObject *lpeobj = LIVEPATHEFFECT(obj)->fork_private_if_necessary(1);
940 sp_lpe_item_add_path_effect(lpeitem, lpeobj);
941 }
942 }
945 /**
946 * @brief Retrieve the clipboard contents as a document
947 * @return Clipboard contents converted to SPDocument, or NULL if no suitable content was present
948 */
949 SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_target)
950 {
951 Glib::ustring best_target;
952 if ( required_target == "" )
953 best_target = _getBestTarget();
954 else
955 best_target = required_target;
957 if ( best_target == "" ) {
958 return NULL;
959 }
961 // FIXME: Temporary hack until we add memory input.
962 // Save the clipboard contents to some file, then read it
963 gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-import", NULL );
965 bool file_saved = false;
966 Glib::ustring target = best_target;
968 #ifdef WIN32
969 if (best_target == CLIPBOARD_WIN32_EMF_TARGET)
970 { // Try to save clipboard data as en emf file (using win32 api)
971 if (OpenClipboard(NULL)) {
972 HGLOBAL hglb = GetClipboardData(CF_ENHMETAFILE);
973 if (hglb) {
974 HENHMETAFILE hemf = CopyEnhMetaFile((HENHMETAFILE) hglb, filename);
975 if (hemf) {
976 file_saved = true;
977 target = CLIPBOARD_WIN32_EMF_MIME;
978 DeleteEnhMetaFile(hemf);
979 }
980 }
981 CloseClipboard();
982 }
983 }
984 #endif
986 if (!file_saved) {
987 if ( !_clipboard->wait_is_target_available(best_target) ) {
988 return NULL;
989 }
991 // doing this synchronously makes better sense
992 // TODO: use another method because this one is badly broken imo.
993 // from documentation: "Returns: A SelectionData object, which will be invalid if retrieving the given target failed."
994 // I don't know how to check whether an object is 'valid' or not, unusable if that's not possible...
995 Gtk::SelectionData sel = _clipboard->wait_for_contents(best_target);
996 target = sel.get_target(); // this can crash if the result was invalid of last function. No way to check for this :(
998 // FIXME: Temporary hack until we add memory input.
999 // Save the clipboard contents to some file, then read it
1000 g_file_set_contents(filename, (const gchar *) sel.get_data(), sel.get_length(), NULL);
1001 }
1003 // there is no specific plain SVG input extension, so if we can paste the Inkscape SVG format,
1004 // we use the image/svg+xml mimetype to look up the input extension
1005 if(target == "image/x-inkscape-svg")
1006 target = "image/svg+xml";
1008 Inkscape::Extension::DB::InputList inlist;
1009 Inkscape::Extension::db.get_input_list(inlist);
1010 Inkscape::Extension::DB::InputList::const_iterator in = inlist.begin();
1011 for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in);
1012 if ( in == inlist.end() )
1013 return NULL; // this shouldn't happen unless _getBestTarget returns something bogus
1015 SPDocument *tempdoc = NULL;
1016 try {
1017 tempdoc = (*in)->open(filename);
1018 } catch (...) {
1019 }
1020 g_unlink(filename);
1021 g_free(filename);
1023 return tempdoc;
1024 }
1027 /**
1028 * @brief Callback called when some other application requests data from Inkscape
1029 *
1030 * Finds a suitable output extension to save the internal clipboard document,
1031 * then saves it to memory and sets the clipboard contents.
1032 */
1033 void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
1034 {
1035 g_assert( _clipboardSPDoc != NULL );
1037 const Glib::ustring target = sel.get_target();
1038 if(target == "") return; // this shouldn't happen
1040 Inkscape::Extension::DB::OutputList outlist;
1041 Inkscape::Extension::db.get_output_list(outlist);
1042 Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin();
1043 for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out);
1044 if ( out == outlist.end() && target != "image/png") return; // this also shouldn't happen
1046 // FIXME: Temporary hack until we add support for memory output.
1047 // Save to a temporary file, read it back and then set the clipboard contents
1048 gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-export", NULL );
1049 gsize len; gchar *data;
1051 try {
1052 if (out == outlist.end() && target == "image/png")
1053 {
1054 NRRect area;
1055 gdouble dpi = PX_PER_IN;
1056 guint32 bgcolor = 0x00000000;
1058 area.x0 = SP_ROOT(_clipboardSPDoc->root)->x.computed;
1059 area.y0 = SP_ROOT(_clipboardSPDoc->root)->y.computed;
1060 area.x1 = area.x0 + sp_document_width (_clipboardSPDoc);
1061 area.y1 = area.y0 + sp_document_height (_clipboardSPDoc);
1063 unsigned long int width = (unsigned long int) ((area.x1 - area.x0) * dpi / PX_PER_IN + 0.5);
1064 unsigned long int height = (unsigned long int) ((area.y1 - area.y0) * dpi / PX_PER_IN + 0.5);
1066 // read from namedview
1067 Inkscape::XML::Node *nv = sp_repr_lookup_name (_clipboardSPDoc->rroot, "sodipodi:namedview");
1068 if (nv && nv->attribute("pagecolor"))
1069 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
1070 if (nv && nv->attribute("inkscape:pageopacity"))
1071 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
1073 sp_export_png_file(_clipboardSPDoc, filename, area.x0, area.y0, area.x1, area.y1, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL);
1074 }
1075 else
1076 {
1077 (*out)->save(_clipboardSPDoc, filename);
1078 }
1079 g_file_get_contents(filename, &data, &len, NULL);
1081 sel.set(8, (guint8 const *) data, len);
1082 } catch (...) {
1083 }
1085 g_unlink(filename); // delete the temporary file
1086 g_free(filename);
1087 }
1090 /**
1091 * @brief Callback when someone else takes the clipboard
1092 *
1093 * When the clipboard owner changes, this callback clears the internal clipboard document
1094 * to reduce memory usage.
1095 */
1096 void ClipboardManagerImpl::_onClear()
1097 {
1098 // why is this called before _onGet???
1099 //_discardInternalClipboard();
1100 }
1103 /**
1104 * @brief Creates an internal clipboard document from scratch
1105 */
1106 void ClipboardManagerImpl::_createInternalClipboard()
1107 {
1108 if ( _clipboardSPDoc == NULL ) {
1109 _clipboardSPDoc = sp_document_new(NULL, false, true);
1110 //g_assert( _clipboardSPDoc != NULL );
1111 _defs = SP_OBJECT_REPR(SP_DOCUMENT_DEFS(_clipboardSPDoc));
1112 _doc = sp_document_repr_doc(_clipboardSPDoc);
1113 _root = sp_document_repr_root(_clipboardSPDoc);
1115 _clipnode = _doc->createElement("inkscape:clipboard");
1116 _root->appendChild(_clipnode);
1117 Inkscape::GC::release(_clipnode);
1118 }
1119 }
1122 /**
1123 * @brief Deletes the internal clipboard document
1124 */
1125 void ClipboardManagerImpl::_discardInternalClipboard()
1126 {
1127 if ( _clipboardSPDoc != NULL ) {
1128 sp_document_unref(_clipboardSPDoc);
1129 _clipboardSPDoc = NULL;
1130 _defs = NULL;
1131 _doc = NULL;
1132 _root = NULL;
1133 _clipnode = NULL;
1134 }
1135 }
1138 /**
1139 * @brief Get the scale to resize an item, based on the command and desktop state
1140 */
1141 NR::scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y)
1142 {
1143 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
1144 double scale_x = 1.0;
1145 double scale_y = 1.0;
1147 if (apply_x) {
1148 scale_x = (max[Geom::X] - min[Geom::X]) / obj_rect[Geom::X].extent();
1149 }
1150 if (apply_y) {
1151 scale_y = (max[Geom::Y] - min[Geom::Y]) / obj_rect[Geom::Y].extent();
1152 }
1153 // If the "lock aspect ratio" button is pressed and we paste only a single coordinate,
1154 // resize the second one by the same ratio too
1155 if (desktop->isToolboxButtonActive("lock")) {
1156 if (apply_x && !apply_y) scale_y = scale_x;
1157 if (apply_y && !apply_x) scale_x = scale_y;
1158 }
1160 return NR::scale(scale_x, scale_y);
1161 }
1164 /**
1165 * @brief Find the most suitable clipboard target
1166 */
1167 Glib::ustring ClipboardManagerImpl::_getBestTarget()
1168 {
1169 std::list<Glib::ustring> targets = _clipboard->wait_for_targets();
1171 // clipboard target debugging snippet
1172 /*
1173 g_debug("Begin clipboard targets");
1174 for ( std::list<Glib::ustring>::iterator x = targets.begin() ; x != targets.end(); ++x )
1175 g_debug("Clipboard target: %s", (*x).data());
1176 g_debug("End clipboard targets\n");
1177 //*/
1179 for(std::list<Glib::ustring>::iterator i = _preferred_targets.begin() ;
1180 i != _preferred_targets.end() ; ++i)
1181 {
1182 if ( std::find(targets.begin(), targets.end(), *i) != targets.end() )
1183 return *i;
1184 }
1185 #ifdef WIN32
1186 if (OpenClipboard(NULL))
1187 { // If both bitmap and metafile are present, pick the one that was exported first.
1188 UINT format = EnumClipboardFormats(0);
1189 while (format) {
1190 if (format == CF_ENHMETAFILE || format == CF_DIB || format == CF_BITMAP)
1191 break;
1192 format = EnumClipboardFormats(format);
1193 }
1194 CloseClipboard();
1196 if (format == CF_ENHMETAFILE)
1197 return CLIPBOARD_WIN32_EMF_TARGET;
1198 if (format == CF_DIB || format == CF_BITMAP)
1199 return CLIPBOARD_GDK_PIXBUF_TARGET;
1200 }
1202 if (IsClipboardFormatAvailable(CF_ENHMETAFILE))
1203 return CLIPBOARD_WIN32_EMF_TARGET;
1204 #endif
1205 if (_clipboard->wait_is_image_available())
1206 return CLIPBOARD_GDK_PIXBUF_TARGET;
1207 if (_clipboard->wait_is_text_available())
1208 return CLIPBOARD_TEXT_TARGET;
1210 return "";
1211 }
1214 /**
1215 * @brief Set the clipboard targets to reflect the mimetypes Inkscape can output
1216 */
1217 void ClipboardManagerImpl::_setClipboardTargets()
1218 {
1219 Inkscape::Extension::DB::OutputList outlist;
1220 Inkscape::Extension::db.get_output_list(outlist);
1221 std::list<Gtk::TargetEntry> target_list;
1222 for (Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin() ; out != outlist.end() ; ++out) {
1223 target_list.push_back(Gtk::TargetEntry( (*out)->get_mimetype() ));
1224 }
1226 // Add PNG export explicitly since there is no extension for this...
1227 // On Windows, GTK will also present this as a CF_DIB/CF_BITMAP
1228 target_list.push_back(Gtk::TargetEntry( "image/png" ));
1230 _clipboard->set(target_list,
1231 sigc::mem_fun(*this, &ClipboardManagerImpl::_onGet),
1232 sigc::mem_fun(*this, &ClipboardManagerImpl::_onClear));
1234 #ifdef WIN32
1235 // If the "image/x-emf" target handled by the emf extension would be
1236 // presented as a CF_ENHMETAFILE automatically (just like an "image/bmp"
1237 // is presented as a CF_BITMAP) this code would not be needed.. ???
1238 // Or maybe there is some other way to achieve the same?
1240 // Note: Metafile is the only format that is rendered and stored in clipboard
1241 // on Copy, all other formats are rendered only when needed by a Paste command.
1243 // FIXME: This should at least be rewritten to use "delayed rendering".
1244 // If possible make it delayed rendering by using GTK API only.
1246 if (OpenClipboard(NULL)) {
1247 if ( _clipboardSPDoc != NULL ) {
1248 const Glib::ustring target = CLIPBOARD_WIN32_EMF_MIME;
1250 Inkscape::Extension::DB::OutputList outlist;
1251 Inkscape::Extension::db.get_output_list(outlist);
1252 Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin();
1253 for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out);
1254 if ( out != outlist.end() ) {
1255 // FIXME: Temporary hack until we add support for memory output.
1256 // Save to a temporary file, read it back and then set the clipboard contents
1257 gchar *filename = g_build_filename( g_get_tmp_dir(), "inkscape-clipboard-export.emf", NULL );
1259 try {
1260 (*out)->save(_clipboardSPDoc, filename);
1261 HENHMETAFILE hemf = GetEnhMetaFileA(filename);
1262 if (hemf) {
1263 SetClipboardData(CF_ENHMETAFILE, hemf);
1264 DeleteEnhMetaFile(hemf);
1265 }
1266 } catch (...) {
1267 }
1268 g_unlink(filename); // delete the temporary file
1269 g_free(filename);
1270 }
1271 }
1272 CloseClipboard();
1273 }
1274 #endif
1275 }
1278 /**
1279 * @brief Set the string representation of a 32-bit RGBA color as the clipboard contents
1280 */
1281 void ClipboardManagerImpl::_setClipboardColor(guint32 color)
1282 {
1283 gchar colorstr[16];
1284 g_snprintf(colorstr, 16, "%08x", color);
1285 _clipboard->set_text(colorstr);
1286 }
1289 /**
1290 * @brief Put a notification on the mesage stack
1291 */
1292 void ClipboardManagerImpl::_userWarn(SPDesktop *desktop, char const *msg)
1293 {
1294 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, msg);
1295 }
1299 /* #######################################
1300 ClipboardManager class
1301 ####################################### */
1303 ClipboardManager *ClipboardManager::_instance = NULL;
1305 ClipboardManager::ClipboardManager() {}
1306 ClipboardManager::~ClipboardManager() {}
1307 ClipboardManager *ClipboardManager::get()
1308 {
1309 if ( _instance == NULL )
1310 _instance = new ClipboardManagerImpl;
1311 return _instance;
1312 }
1314 } // namespace Inkscape
1315 } // namespace IO
1317 /*
1318 Local Variables:
1319 mode:c++
1320 c-file-style:"stroustrup"
1321 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1322 indent-tabs-mode:nil
1323 fill-column:99
1324 End:
1325 */
1326 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :