clipboard.cpp revision 51d5d061f1756df3d3865c0d36522a7d8c5c9d22
/** @file
* @brief System-wide clipboard management - implementation
*/
/* Authors:
* Krzysztof KosiĆski <tweenk@o2.pl>
* Incorporates some code from selection-chemistry.cpp, see that file for more credits.
*
* Copyright (C) 2008 authors
*
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* See the file COPYING for details.
*/
#include "ui/clipboard.h"
// TODO: reduce header bloat if possible
#include <list>
#include <algorithm>
#include <gtkmm/clipboard.h>
#include "gc-core.h"
#include "inkscape.h"
#include "io/stringstream.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "desktop-style.h" // for sp_desktop_set_style, used in _pasteStyle
#include "document.h"
#include "document-private.h"
#include "selection.h"
#include "message-stack.h"
#include "context-fns.h"
#include "dropper-context.h" // used in copy()
#include "style.h"
#include "selection-chemistry.h"
#include "libnr/nr-convert2geom.h"
#include "box3d.h"
#include "gradient-drag.h"
#include "sp-item.h"
#include "sp-item-transform.h" // for sp_item_scale_rel, used in _pasteSize
#include "sp-path.h"
#include "sp-pattern.h"
#include "sp-shape.h"
#include "sp-gradient.h"
#include "sp-gradient-reference.h"
#include "sp-gradient-fns.h"
#include "sp-linear-gradient-fns.h"
#include "sp-radial-gradient-fns.h"
#include "sp-clippath.h"
#include "sp-mask.h"
#include "sp-textpath.h"
#include "sp-rect.h"
#include "live_effects/lpeobject.h"
#include "live_effects/lpeobject-reference.h"
#include "live_effects/parameter/path.h"
#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"
#include "path-chemistry.h"
#include "id-clash.h"
#include "unit-constants.h"
#include "helper/png-write.h"
#include "svg/svg-color.h"
#include "sp-namedview.h"
#include "snap.h"
/// @brief Made up mimetype to represent Gdk::Pixbuf clipboard contents
#define CLIPBOARD_GDK_PIXBUF_TARGET "image/x-gdk-pixbuf"
#define CLIPBOARD_TEXT_TARGET "text/plain"
#ifdef WIN32
#include <windows.h>
// Clipboard Formats: http://msdn.microsoft.com/en-us/library/ms649013(VS.85).aspx
#define CLIPBOARD_WIN32_EMF_TARGET "CF_ENHMETAFILE"
#define CLIPBOARD_WIN32_EMF_MIME "image/x-emf"
#endif
namespace Inkscape {
namespace UI {
/**
* @brief Default implementation of the clipboard manager
*/
class ClipboardManagerImpl : public ClipboardManager {
public:
virtual void copy();
virtual bool pasteStyle();
virtual bool pasteSize(bool, bool, bool);
virtual bool pastePathEffect();
virtual const gchar *getFirstObjectID();
private:
void _copyUsedDefs(SPItem *);
void _copyGradient(SPGradient *);
void _copyPattern(SPPattern *);
void _copyTextPath(SPTextPath *);
Inkscape::XML::Node *_copyNode(Inkscape::XML::Node *, Inkscape::XML::Document *, Inkscape::XML::Node *);
void _pasteDefs(SPDocument *);
bool _pasteImage();
bool _pasteText();
// clipboard callbacks
void _onClear();
// various helpers
void _createInternalClipboard();
void _discardInternalClipboard();
void _setClipboardTargets();
void _setClipboardColor(guint32);
// private properites
// we need a way to copy plain text AND remember its style;
// the standard _clipnode is only available in an SVG tree, hence this special storage
};
: _clipboardSPDoc(NULL),
{
// push supported clipboard targets, in order of preference
#ifdef WIN32
#endif
}
/**
* @brief Copy selection contents to the clipboard
*/
void ClipboardManagerImpl::copy()
{
// Special case for when the gradient dragger is active - copies gradient color
if (drag->hasSelection()) {
// set the color as clipboard content (text in RRGGBBAA format)
// create a style with this color on fill and opacity in master opacity, so it can be
// pasted on other stops or objects
if (_text_style) {
_text_style = NULL;
}
// print and set properties
return;
}
}
// Special case for when the color picker ("dropper") is active - copies color under cursor
return;
}
// Special case for when the text tool is active - if some text is selected, copy plain text,
// not the object that holds it; also copy the style at cursor into
if (!selected_text.empty()) {
}
if (_text_style) {
_text_style = NULL;
}
return;
}
return;
}
_createInternalClipboard(); // construct a new clipboard document
}
/**
* @brief Copy a Live Path Effect path parameter to the clipboard
* @param pp The path parameter to store in the clipboard
*/
{
}
/**
* @brief Paste from the system clipboard into the active desktop
* @param in_place Whether to put the contents where they were when copied
*/
{
// do any checking whether we really are able to paste before requesting the contents
// Special cases of clipboard content handling go here
// Note that target priority is determined in _getBestTarget.
// TODO: Handle x-special/gnome-copied-files and text/uri-list to support pasting files
// if there is an image on the clipboard, paste it
// if there's only text, paste it into a selected text object or create a new one
// otherwise, use the import extensions
return false;
}
return true;
}
/**
* @brief Returns the id of the first visible copied object
*/
{
return NULL;
}
if (!root)
return NULL;
)
if (ch) {
}
return NULL;
}
/**
* @brief Implements the Paste Style action
*/
bool ClipboardManagerImpl::pasteStyle()
{
// check whether something is selected
return false;
}
// no document, but we can try _text_style
if (_text_style) {
return true;
} else {
return false;
}
}
bool pasted = false;
if (clipnode) {
pasted = true;
}
else {
}
return pasted;
}
/**
* @brief Resize the selection or each object in the selection to match the clipboard's size
* @param separately Whether to scale each object in the selection separately
* @param apply_x Whether to scale the width of objects / selection
* @param apply_y Whether to scale the height of objects / selection
*/
{
return false;
}
// FIXME: actually, this should accept arbitrary documents
return false;
}
// retrieve size ifomration from the clipboard
bool pasted = false;
if (clipnode) {
// resize each object in the selection
if (separately) {
if ( !obj_size ) continue;
}
}
// resize the selection as a whole
else {
if ( sel_size ) {
}
}
pasted = true;
}
return pasted;
}
/**
* @brief Applies a path effect from the clipboard to the selected path
*/
bool ClipboardManagerImpl::pastePathEffect()
{
/** @todo FIXME: pastePathEffect crashes when moving the path with the applied effect,
segfaulting in fork_private_if_necessary(). */
return false;
return false;
}
if ( tempdoc ) {
if ( clipnode ) {
if ( effect ) {
// make sure all selected items are converted to paths first (i.e. rectangles)
sp_selected_path_to_curves(desktop, false);
}
return true;
}
}
}
// no_effect:
return false;
}
/**
* @brief Get LPE path data from the clipboard
* @return The retrieved path data (contents of the d attribute), or "" if no path was found
*/
{
return "";
}
return "";
}
return svgd;
}
/**
* @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
*/
{
return "";
}
return "";
}
return svgd;
}
/**
* @brief Iterate over a list of items and copy them to the clipboard.
*/
{
// copy the defs used by all items
}
// copy the representation of the items
if (!SP_IS_ITEM(i->data)) continue;
// copy complete inherited style
// write the complete accumulated transform passed to us
// (we're dealing with unattached representations, so we write to their attributes
// instead of using sp_item_set_transform)
}
// copy style for Paste Style action
if (sorted_items) {
}
// copy path effect from the first path
if (effect) {
}
}
}
if (size) {
}
}
/**
* @brief Recursively copy all the definitions used by a given item to the clipboard defs
*/
{
// copy fill and stroke styles (patterns and gradients)
if (SP_IS_PATTERN(server))
}
if (SP_IS_PATTERN(server))
}
// For shapes, copy all of the shape's markers
if (SP_IS_SHAPE(item)) {
for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
}
}
}
// For lpe items, copy lpe stack if applicable
if (SP_IS_LPE_ITEM(item)) {
if (sp_lpe_item_has_path_effect(lpeitem)) {
for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); ++it)
{
if (lpeobj)
}
}
}
// For 3D boxes, copy perspectives
if (SP_IS_BOX3D(item)) {
}
// Copy text paths
if (SP_IS_TEXT_TEXTPATH(item)) {
}
// Copy clipping objects
}
// Copy mask objects
// recurse into the mask for its gradients etc.
if (SP_IS_ITEM(o))
_copyUsedDefs(SP_ITEM(o));
}
}
// Copy filters
if (SP_IS_FILTER(filter)) {
}
}
// recurse
if (SP_IS_ITEM(o))
_copyUsedDefs(SP_ITEM(o));
}
}
/**
* @brief Copy a single gradient to the clipboard's defs element
*/
{
while (gradient) {
// climb up the refs, copying each one in the chain
}
}
/**
* @brief Copy a single pattern to the clipboard document's defs element
*/
{
// climb up the references, copying each one in the chain
while (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;
}
}
}
/**
* @brief Copy a text path to the clipboard's defs element
*/
{
if(!path) return;
// Do not copy the text path to defs if it's already copied
}
/**
* @brief Copy a single XML node from one document to another
* @param node The node to be copied
* @param target_doc The document to which the node is to be copied
* @param parent The node in the target document which will become the parent of the copied node
* @return Pointer to the copied node
*/
Inkscape::XML::Node *ClipboardManagerImpl::_copyNode(Inkscape::XML::Node *node, Inkscape::XML::Document *target_doc, Inkscape::XML::Node *parent)
{
return dup;
}
/**
* @brief Paste the contents of a document into the active desktop
* @param clipdoc The document to paste
* @param in_place Whether to paste the selection where it was when copied
* @pre @c clipdoc is not empty and items can be added to the current layer
*/
{
// copy definitions
// copy objects
// Don't copy metadata, defs, named views and internal clipboard contents to the document
}
// Change the selection to the freshly pasted objects
// invers apply parent transform
sp_selection_apply_affine(selection, desktop->dt2doc() * doc2parent * desktop->doc2dt(), true, false);
// Update (among other things) all curves in paths, for bounds() to work
// move selection either to original position (in_place) or to mouse pointer
if (sel_bbox) {
// get offset of selection to original position of copied elements
if (clipnode) {
}
if (!in_place) {
// get offset from mouse pointer to bbox center, snap to grid if enabled
}
}
}
/**
* @brief Paste SVG defs from the document retrieved from the clipboard into the active document
* @param clipdoc The document to paste
* @pre @c clipdoc != NULL and pasting into the active document is possible
*/
{
// boilerplate vars copied from _pasteDocument
}
}
/**
* @brief Retrieve a bitmap image from the clipboard and paste it into the active document
*/
bool ClipboardManagerImpl::_pasteImage()
{
// retrieve image data
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.
char image_filename[128];
/// @todo Check whether the encoding is correct here
return true;
}
/**
* @brief Paste text into the selected text object or create a new one to hold it
*/
bool ClipboardManagerImpl::_pasteText()
{
// if the text editing tool is active, paste the text into the active text object
// try to parse the text as a color and, if successful, apply it as the current style
if (css) {
return true;
}
return false;
}
/**
* @brief Attempt to parse the passed string as a hexadecimal RGB or RGBA color
* @param text The Glib::ustring to parse
* @return New CSS style representation if the parsing was successful, NULL otherwise
*/
{
// TODO reuse existing code instead of replicating here.
bool attempt_alpha = false;
// those conditionals guard against parsing e.g. the string "fab" as "fab000"
// (incomplete color) and "45fab71" as "45fab710" (incomplete alpha)
if ( *str == '#' ) {
} else {
}
// skip a leading #, if present
// try to parse first 6 digits
if (attempt_alpha) {// try to parse alpha if there's enough characters
}
// print and set properties
return color_css;
}
return NULL;
}
/**
* @brief Applies a pasted path effect to a given item
*/
{
if ( SP_IS_RECT(item) ) return;
if (SP_IS_LPE_ITEM(item))
{
if (!obj) return;
// if the effect is not used by anyone, we might as well take it
}
}
/**
* @brief Retrieve the clipboard contents as a document
* @return Clipboard contents converted to SPDocument, or NULL if no suitable content was present
*/
{
if ( required_target == "" )
else
if ( best_target == "" ) {
return NULL;
}
// FIXME: Temporary hack until we add memory input.
// Save the clipboard contents to some file, then read it
bool file_saved = false;
#ifdef WIN32
{ // Try to save clipboard data as en emf file (using win32 api)
if (OpenClipboard(NULL)) {
if (hglb) {
if (hemf) {
file_saved = true;
}
}
}
}
#endif
if (!file_saved) {
return NULL;
}
// doing this synchronously makes better sense
// TODO: use another method because this one is badly broken imo.
// from documentation: "Returns: A SelectionData object, which will be invalid if retrieving the given target failed."
// I don't know how to check whether an object is 'valid' or not, unusable if that's not possible...
target = sel.get_target(); // this can crash if the result was invalid of last function. No way to check for this :(
// FIXME: Temporary hack until we add memory input.
// Save the clipboard contents to some file, then read it
}
// there is no specific plain SVG input extension, so if we can paste the Inkscape SVG format,
if(target == "image/x-inkscape-svg")
return NULL; // this shouldn't happen unless _getBestTarget returns something bogus
try {
} catch (...) {
}
return tempdoc;
}
/**
* @brief Callback called when some other application requests data from Inkscape
*
* Finds a suitable output extension to save the internal clipboard document,
* then saves it to memory and sets the clipboard contents.
*/
{
if (target == CLIPBOARD_TEXT_TARGET) {
target = "image/x-inkscape-svg";
}
// FIXME: Temporary hack until we add support for memory output.
// Save to a temporary file, read it back and then set the clipboard contents
try {
{
Geom::Point origin (SP_ROOT(_clipboardSPDoc->root)->x.computed, SP_ROOT(_clipboardSPDoc->root)->y.computed);
// read from namedview
sp_export_png_file(_clipboardSPDoc, filename, area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL);
}
else
{
// Need to load the extension.
}
}
} catch (...) {
}
}
/**
* @brief Callback when someone else takes the clipboard
*
* When the clipboard owner changes, this callback clears the internal clipboard document
* to reduce memory usage.
*/
void ClipboardManagerImpl::_onClear()
{
// why is this called before _onGet???
//_discardInternalClipboard();
}
/**
* @brief Creates an internal clipboard document from scratch
*/
{
if ( _clipboardSPDoc == NULL ) {
//g_assert( _clipboardSPDoc != NULL );
// once we create a SVG document, style will be stored in it, so flush _text_style
if (_text_style) {
_text_style = NULL;
}
}
}
/**
* @brief Deletes the internal clipboard document
*/
{
if ( _clipboardSPDoc != NULL ) {
}
}
/**
* @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)
{
double scale_x = 1.0;
double scale_y = 1.0;
if (apply_x) {
}
if (apply_y) {
}
// If the "lock aspect ratio" button is pressed and we paste only a single coordinate,
// resize the second one by the same ratio too
}
}
/**
* @brief Find the most suitable clipboard target
*/
{
// clipboard target debugging snippet
/*
g_debug("Begin clipboard targets");
for ( std::list<Glib::ustring>::iterator x = targets.begin() ; x != targets.end(); ++x )
g_debug("Clipboard target: %s", (*x).data());
g_debug("End clipboard targets\n");
//*/
i != _preferred_targets.end() ; ++i)
{
return *i;
}
#ifdef WIN32
if (OpenClipboard(NULL))
{ // If both bitmap and metafile are present, pick the one that was exported first.
while (format) {
break;
}
if (format == CF_ENHMETAFILE)
return CLIPBOARD_WIN32_EMF_TARGET;
return CLIPBOARD_GDK_PIXBUF_TARGET;
}
return CLIPBOARD_WIN32_EMF_TARGET;
#endif
if (_clipboard->wait_is_image_available())
return CLIPBOARD_GDK_PIXBUF_TARGET;
if (_clipboard->wait_is_text_available())
return CLIPBOARD_TEXT_TARGET;
return "";
}
/**
* @brief Set the clipboard targets to reflect the mimetypes Inkscape can output
*/
{
bool plaintextSet = false;
for (Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin() ; out != outlist.end() ; ++out) {
if ( !(*out)->deactivated() ) {
if (mime != CLIPBOARD_TEXT_TARGET) {
plaintextSet = true;
}
}
}
}
// Add PNG export explicitly since there is no extension for this...
#ifdef WIN32
// is presented as a CF_BITMAP) this code would not be needed.. ???
// Or maybe there is some other way to achieve the same?
// Note: Metafile is the only format that is rendered and stored in clipboard
// on Copy, all other formats are rendered only when needed by a Paste command.
// FIXME: This should at least be rewritten to use "delayed rendering".
// If possible make it delayed rendering by using GTK API only.
if (OpenClipboard(NULL)) {
if ( _clipboardSPDoc != NULL ) {
// FIXME: Temporary hack until we add support for memory output.
// Save to a temporary file, read it back and then set the clipboard contents
try {
if (hemf) {
}
} catch (...) {
}
}
}
}
#endif
}
/**
* @brief Set the string representation of a 32-bit RGBA color as the clipboard contents
*/
{
}
/**
* @brief Put a notification on the mesage stack
*/
{
}
/* #######################################
ClipboardManager class
####################################### */
ClipboardManager::~ClipboardManager() {}
{
_instance = new ClipboardManagerImpl;
return _instance;
}
} // namespace Inkscape
} // namespace IO
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :