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