Code

From trunk
[inkscape.git] / src / ui / clipboard.cpp
index ce506523a2b6ce9ed60f3a510c91befbb37463d6..e76af958fb86d1f710085286ad40058c0fbbd8f4 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);
+                boost::optional<Geom::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));
+                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();
+            boost::optional<Geom::Rect> sel_size = selection->bounds();
             if ( sel_size && !sel_size->isEmpty() ) {
                 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);
                 }
@@ -549,7 +549,7 @@ void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection)
         // 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)
-        gchar *transform_str = sp_svg_transform_write(from_2geom(sp_item_i2doc_affine(SP_ITEM(i->data))));
+        gchar *transform_str = sp_svg_transform_write(sp_item_i2doc_affine(SP_ITEM(i->data)));
         obj_copy->setAttribute("transform", transform_str);
         g_free(transform_str);
     }
@@ -571,7 +571,7 @@ void ClipboardManagerImpl::_copySelection(Inkscape::Selection *selection)
         }
     }
 
-    boost::optional<NR::Rect> size = selection->bounds();
+    boost::optional<Geom::Rect> size = selection->bounds();
     if (size) {
         sp_repr_set_point(_clipnode, "min", size->min());
         sp_repr_set_point(_clipnode, "max", size->max());
@@ -786,12 +786,12 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place)
     selection->setReprList(pasted_objects);         // Change the selection to the freshly pasted objects
     sp_document_ensure_up_to_date(target_document); // What does this do?
     
-    boost::optional<NR::Rect> sel_bbox = selection->bounds(); //In desktop coordinates
+    boost::optional<Geom::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 = to_2geom(desktop->point()); //Location of mouse pointer in desktop coordinates
+        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());
@@ -803,7 +803,7 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place)
         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
+        m.setup(desktop, false); //Don't display the snapindicator
         offset = rel_pos_original + m.multipleOfGridPitch(rel_pos_mouse - rel_pos_original); 
     }
     
@@ -858,13 +858,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 +903,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;
@@ -1060,9 +1062,13 @@ 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();
@@ -1100,6 +1106,10 @@ void ClipboardManagerImpl::_onGet(Gtk::SelectionData &sel, guint /*info*/)
         }
         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 +1174,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 +1193,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 +1255,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...