Code

it was a very stupid idea to sort all combos without asking - this broke the blend...
[inkscape.git] / src / ui / clipboard.cpp
index 36ceac0d10012def47ff9c3cf62c2e1de4a17974..081fbba64c100f9b0900d9a47abf89cd17b1b199 100644 (file)
@@ -69,7 +69,7 @@
 #include "svg/svg.h" // for sp_svg_transform_write, used in _copySelection
 #include "svg/css-ostringstream.h" // used in _parseColor
 #include "file.h" // for file_import, used in _pasteImage
-#include "prefs-utils.h" // for prefs_get_string_attribute, used in _pasteImage
+#include "preferences.h" // for used in _pasteImage
 #include "text-context.h"
 #include "text-editing.h"
 #include "tools-switch.h"
@@ -142,7 +142,7 @@ private:
     void _createInternalClipboard();
     void _discardInternalClipboard();
     Inkscape::XML::Node *_createClipNode();
-    NR::scale _getScale(Geom::Point const &, Geom::Point const &, Geom::Rect const &, bool, bool);
+    Geom::Scale _getScale(Geom::Point const &, Geom::Point const &, Geom::Rect const &, bool, bool);
     Glib::ustring _getBestTarget();
     void _setClipboardTargets();
     void _setClipboardColor(guint32);
@@ -270,7 +270,7 @@ bool ClipboardManagerImpl::paste(bool in_place)
 
     Glib::ustring target = _getBestTarget();
 
-    // Special cases of clipboard content handling go here 00ff00
+    // 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
 
@@ -407,17 +407,17 @@ bool ClipboardManagerImpl::pasteSize(bool separately, bool apply_x, bool apply_y
         if (separately) {
             for (GSList *i = const_cast<GSList*>(selection->itemList()) ; i ; i = i->next) {
                 SPItem *item = SP_ITEM(i->data);
-                boost::optional<NR::Rect> obj_size = sp_item_bbox_desktop(item);
-                if ( !obj_size || obj_size->isEmpty() ) continue;
-                sp_item_scale_rel(item, _getScale(min, max, to_2geom(*obj_size), apply_x, apply_y));
+                Geom::OptRect obj_size = sp_item_bbox_desktop(item);
+                if ( !obj_size ) continue;
+                sp_item_scale_rel(item, _getScale(min, max, *obj_size, apply_x, apply_y));
             }
         }
         // resize the selection as a whole
         else {
-            boost::optional<NR::Rect> sel_size = selection->bounds();
-            if ( sel_size && !sel_size->isEmpty() ) {
+            Geom::OptRect sel_size = selection->bounds();
+            if ( sel_size ) {
                 sp_selection_scale_relative(selection, sel_size->midpoint(),
-                    _getScale(min, max, to_2geom(*sel_size), apply_x, apply_y));
+                    _getScale(min, max, *sel_size, apply_x, apply_y));
             }
         }
         pasted = true;
@@ -454,7 +454,7 @@ bool ClipboardManagerImpl::pastePathEffect()
             if ( effect ) {
                 _pasteDefs(tempdoc);
                 // make sure all selected items are converted to paths first (i.e. rectangles)
-                sp_selected_path_to_curves(false);
+                sp_selected_path_to_curves(desktop, false);
                 for (GSList *item = const_cast<GSList *>(selection->itemList()) ; item ; item = item->next) {
                     _applyPathEffect(reinterpret_cast<SPItem*>(item->data), effect);
                 }
@@ -571,7 +571,7 @@ void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection)
         }
     }
 
-    boost::optional<NR::Rect> size = selection->bounds();
+    Geom::OptRect size = selection->bounds();
     if (size) {
         sp_repr_set_point(_clipnode, "min", size->min());
         sp_repr_set_point(_clipnode, "max", size->max());
@@ -755,60 +755,44 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place)
         Inkscape::XML::Node *obj_copy = _copyNode(obj, target_xmldoc, target_parent);
         pasted_objects = g_slist_prepend(pasted_objects, (gpointer) obj_copy);
     }
-    
-    /* The pasted objects are at the origin of the document coordinates (i.e. the upper left corner
-     * of the bounding box of the pasted selection is aligned to the upper left page corner).
-     * Now we will move the pasted objects to where we want them to be, which is either at the
-     * original position ("in place") or at the location of the mouse pointer (optionaly with snapping
-     * to the grid)
-     */ 
-
-    Geom::Point rel_pos_original, rel_pos_mouse;
-    
-    // Calculate the relative location of the original objects
-    Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
-    if (clipnode) {
-        Geom::Point min, max;
-        // Get two bounding box corners of the data in the clipboard (still in it's original position)
-        sp_repr_get_point(clipnode, "min", &min); //In desktop coordinates
-        sp_repr_get_point(clipnode, "max", &max);        
-        // Calculate the upper-left page corner in desktop coordinates (where the pasted objects are located)
-        Geom::Point ul_page_corner = desktop->doc2dt(Geom::Point(0,0)); 
-        // Calculate the upper-left bbox corner of the original objects
-        Geom::Point ul_sel_corner = Geom::Point(min[Geom::X], max[Geom::Y]);
-        // Now calculate how far we would have to move the pasted objects to get them
-        // at the location of the original
-        rel_pos_original = ul_sel_corner - ul_page_corner; // in desktop coordinates
-    }
-    
-    // Calculate the relative location of the mouse pointer
+   
+    // Change the selection to the freshly pasted objects
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
-    selection->setReprList(pasted_objects);         // Change the selection to the freshly pasted objects
-    sp_document_ensure_up_to_date(target_document); // What does this do?
+    selection->setReprList(pasted_objects);
     
-    boost::optional<NR::Rect> sel_bbox = selection->bounds(); //In desktop coordinates
-    // PS: We could also have used the min/max corners calculated above, instead of selection->bounds() because 
-    // we know that after pasting the upper left corner of the selection will be aligend to the corresponding 
-    // page corner. Using the boundingbox of the selection is more foolproof though
-    if (sel_bbox) {
-        Geom::Point pos_mouse = desktop->point(); //Location of mouse pointer in desktop coordinates
-        // Now calculate how far we would have to move the pasted objects to get their
-        // midpoint at the location of the mouse pointer
-        rel_pos_mouse = pos_mouse - to_2geom(sel_bbox->midpoint());
-    }
+    // invers apply parent transform
+    Geom::Matrix doc2parent = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
+    sp_selection_apply_affine(selection, doc2parent);
     
-    // Determine which offset we need to apply to the pasted objects
-    Geom::Point offset;
-    if (in_place) { // Align the pasted objects with their originals
-        offset = rel_pos_original;        
-    } else { // Stick to the grid if snapping is enabled, otherwise paste at mouse position;
-        SnapManager &m = desktop->namedview->snap_manager;
-        m.setup(NULL, NULL); //Don't display the snapindicator
-        offset = rel_pos_original + m.multipleOfGridPitch(rel_pos_mouse - rel_pos_original); 
+    // Update (among other things) all curves in paths, for bounds() to work
+    sp_document_ensure_up_to_date(target_document);
+
+    // move selection either to original position (in_place) or to mouse pointer
+    Geom::OptRect sel_bbox = selection->bounds();
+    if (sel_bbox) {
+        // get offset of selection to original position of copied elements
+        Geom::Point pos_original;
+        Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
+        if (clipnode) {
+            Geom::Point min, max;
+            sp_repr_get_point(clipnode, "min", &min);
+            sp_repr_get_point(clipnode, "max", &max);        
+            pos_original = Geom::Point(min[Geom::X], max[Geom::Y]);
+        }
+        Geom::Point offset = pos_original - sel_bbox->corner(3);
+        
+        if (!in_place) {
+            SnapManager &m = desktop->namedview->snap_manager;
+            m.setup(desktop, false); // Don't display the snapindicator
+            
+            // get offset from mouse pointer to bbox center, snap to grid if enabled
+            Geom::Point mouse_offset = desktop->point() - sel_bbox->midpoint();
+            offset = m.multipleOfGridPitch(mouse_offset - offset) + offset;
+        }
+
+        sp_selection_move_relative(selection, offset);
     }
     
-    // Apply the offset to the pasted objects
-    sp_selection_move_relative(selection, offset);
     g_slist_free(pasted_objects);
 }
 
@@ -858,13 +842,14 @@ bool ClipboardManagerImpl::_pasteImage()
     // in 1 second.
     time_t rawtime;
     char image_filename[128];
-    gchar const *save_folder;
 
     time(&rawtime);
     strftime(image_filename, 128, "inkscape_pasted_image_%Y%m%d_%H%M%S.png", localtime( &rawtime ));
-    save_folder = (gchar const *) prefs_get_string_attribute("dialogs.save_as", "path");
+    /// @todo Check whether the encoding is correct here
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    std::string save_folder = Glib::filename_from_utf8(prefs->getString("/dialogs/save_as/path"));
 
-    gchar *image_path = g_build_filename(save_folder, image_filename, NULL);
+    gchar *image_path = g_build_filename(save_folder.data(), image_filename, NULL);
     img->save(image_path, "png");
     file_import(doc, image_path, NULL);
     g_free(image_path);
@@ -902,6 +887,7 @@ bool ClipboardManagerImpl::_pasteText()
  */
 SPCSSAttr *ClipboardManagerImpl::_parseColor(const Glib::ustring &text)
 {
+// TODO reuse existing code instead of replicating here.
     Glib::ustring::size_type len = text.bytes();
     char *str = const_cast<char *>(text.data());
     bool attempt_alpha = false;
@@ -1034,7 +1020,7 @@ SPDocument *ClipboardManagerImpl::_retrieveClipboard(Glib::ustring required_targ
     Inkscape::Extension::DB::InputList inlist;
     Inkscape::Extension::db.get_input_list(inlist);
     Inkscape::Extension::DB::InputList::const_iterator in = inlist.begin();
-    for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in);
+    for (; in != inlist.end() && target != (*in)->get_mimetype() ; ++in){};
     if ( in == inlist.end() )
         return NULL; // this shouldn't happen unless _getBestTarget returns something bogus
 
@@ -1060,13 +1046,17 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
 {
     g_assert( _clipboardSPDoc != NULL );
 
-    const Glib::ustring target = sel.get_target();
+    Glib::ustring target = sel.get_target();
     if(target == "") return; // this shouldn't happen
 
+    if (target == CLIPBOARD_TEXT_TARGET) {
+        target = "image/x-inkscape-svg";
+    }
+
     Inkscape::Extension::DB::OutputList outlist;
     Inkscape::Extension::db.get_output_list(outlist);
     Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin();
-    for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out);
+    for ( ; out != outlist.end() && target != (*out)->get_mimetype() ; ++out){};
     if ( out == outlist.end() && target != "image/png") return; // this also shouldn't happen
 
     // FIXME: Temporary hack until we add support for memory output.
@@ -1077,17 +1067,14 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
     try {
         if (out == outlist.end() && target == "image/png")
         {
-            NRRect area;
             gdouble dpi = PX_PER_IN;
             guint32 bgcolor = 0x00000000;
 
-            area.x0 = SP_ROOT(_clipboardSPDoc->root)->x.computed;
-            area.y0 = SP_ROOT(_clipboardSPDoc->root)->y.computed;
-            area.x1 = area.x0 + sp_document_width (_clipboardSPDoc);
-            area.y1 = area.y0 + sp_document_height (_clipboardSPDoc);
+            Geom::Point origin (SP_ROOT(_clipboardSPDoc->root)->x.computed, SP_ROOT(_clipboardSPDoc->root)->y.computed);
+            Geom::Rect area = Geom::Rect(origin, origin + sp_document_dimensions(_clipboardSPDoc));
 
-            unsigned long int width = (unsigned long int) ((area.x1 - area.x0) * dpi / PX_PER_IN + 0.5);
-            unsigned long int height = (unsigned long int) ((area.y1 - area.y0) * dpi / PX_PER_IN + 0.5);
+            unsigned long int width = (unsigned long int) (area.width() * dpi / PX_PER_IN + 0.5);
+            unsigned long int height = (unsigned long int) (area.height() * dpi / PX_PER_IN + 0.5);
 
             // read from namedview
             Inkscape::XML::Node *nv = sp_repr_lookup_name (_clipboardSPDoc->rroot, "sodipodi:namedview");
@@ -1096,10 +1083,14 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
             if (nv && nv->attribute("inkscape:pageopacity"))
                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
 
-            sp_export_png_file(_clipboardSPDoc, filename, area.x0, area.y0, area.x1, area.y1, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL);
+            sp_export_png_file(_clipboardSPDoc, filename, area, width, height, dpi, dpi, bgcolor, NULL, NULL, true, NULL);
         }
         else
         {
+            if (!(*out)->loaded()) {
+                // Need to load the extension.
+                (*out)->set_state(Inkscape::Extension::Extension::STATE_LOADED);
+            }
             (*out)->save(_clipboardSPDoc, filename);
         }
         g_file_get_contents(filename, &data, &len, NULL);
@@ -1164,7 +1155,7 @@ void ClipboardManagerImpl::_discardInternalClipboard()
 /**
  * @brief Get the scale to resize an item, based on the command and desktop state
  */
-NR::scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y)
+Geom::Scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point const &max, Geom::Rect const &obj_rect, bool apply_x, bool apply_y)
 {
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
     double scale_x = 1.0;
@@ -1183,7 +1174,7 @@ NR::scale ClipboardManagerImpl::_getScale(Geom::Point const &min, Geom::Point co
         if (apply_y && !apply_x) scale_x = scale_y;
     }
 
-    return NR::scale(scale_x, scale_y);
+    return Geom::Scale(scale_x, scale_y);
 }
 
 
@@ -1245,8 +1236,18 @@ void ClipboardManagerImpl::_setClipboardTargets()
     Inkscape::Extension::DB::OutputList outlist;
     Inkscape::Extension::db.get_output_list(outlist);
     std::list<Gtk::TargetEntry> target_list;
+    bool plaintextSet = false;
     for (Inkscape::Extension::DB::OutputList::const_iterator out = outlist.begin() ; out != outlist.end() ; ++out) {
-        target_list.push_back(Gtk::TargetEntry( (*out)->get_mimetype() ));
+        if ( !(*out)->deactivated() ) {
+            Glib::ustring mime = (*out)->get_mimetype();
+            if (mime != CLIPBOARD_TEXT_TARGET) {
+                if ( !plaintextSet && (mime.find("svg") == Glib::ustring::npos) ) {
+                    target_list.push_back(Gtk::TargetEntry(CLIPBOARD_TEXT_TARGET));
+                    plaintextSet = true;
+                }
+                target_list.push_back(Gtk::TargetEntry(mime));
+            }
+        }
     }
 
     // Add PNG export explicitly since there is no extension for this...