Code

use touchpath selection when rubberbanding with Alt; do move-selected with Alt only...
[inkscape.git] / src / file.cpp
index 9b2b803c6346bef554e9b89138abdf6689fe5780..69d6da610383ec34533bebb3554e9cb750a4e546 100644 (file)
@@ -43,6 +43,7 @@
 #include "style.h"
 #include "print.h"
 #include "file.h"
+#include "message.h"
 #include "message-stack.h"
 #include "ui/dialog/filedialog.h"
 #include "prefs-utils.h"
@@ -106,6 +107,7 @@ sp_file_new(const Glib::ustring &templ)
         sp_create_window(dtw, TRUE);
         dt = static_cast<SPDesktop*>(dtw->view);
         sp_namedview_window_from_document(dt);
+        sp_namedview_update_layers_from_document(dt);
     }
     return dt;
 }
@@ -201,9 +203,8 @@ sp_file_open(const Glib::ustring &uri,
         // everyone who cares now has a reference, get rid of ours
         sp_document_unref(doc);
         // resize the window to match the document properties
-        // (this may be redundant for new windows... if so, move to the "virgin"
-        //  section above)
         sp_namedview_window_from_document(desktop);
+        sp_namedview_update_layers_from_document(desktop);
 
         if (add_to_recent) {
             prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
@@ -282,7 +283,7 @@ void dump_str(gchar const *str, gchar const *prefix)
     }
 
     tmp += "]";
-    g_message(tmp.c_str());
+    g_message("%s", tmp.c_str());
 }
 
 void dump_ustr(Glib::ustring const &ustr)
@@ -343,7 +344,7 @@ void dump_ustr(Glib::ustring const &ustr)
                 tmp += "            ";
             }
 
-            g_message( tmp.c_str() );
+            g_message( "%s", tmp.c_str() );
         }
     } catch (...) {
         g_message("XXXXXXXXXXXXXXXXXX Exception" );
@@ -387,8 +388,13 @@ sp_file_open_dialog(gpointer object, gpointer data)
                  open_path,
                  Inkscape::UI::Dialog::SVG_TYPES,
                  (char const *)_("Select file to open"));
+        // allow easy access to our examples folder             
+        if (Inkscape::IO::file_test(INKSCAPE_EXAMPLESDIR, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
+            dynamic_cast<Gtk::FileChooser *>(openDialogInstance)->add_shortcut_folder(INKSCAPE_EXAMPLESDIR);
+        }
     }
 
+
     //# Show the dialog
     bool const success = openDialogInstance->show();
     if (!success)
@@ -463,7 +469,7 @@ sp_file_vacuum()
     unsigned int diff = vacuum_document (doc);
 
     sp_document_done(doc, SP_VERB_FILE_VACUUM, 
-                     /* TODO: annotate */ "file.cpp:515");
+                     _("Vacuum &lt;defs&gt;"));
 
     SPDesktop *dt = SP_ACTIVE_DESKTOP;
     if (diff > 0) {
@@ -498,7 +504,7 @@ file_save(SPDocument *doc, const Glib::ustring &uri,
 
     try {
         Inkscape::Extension::save(key, doc, uri.c_str(),
-                 saveas && prefs_get_int_attribute("dialogs.save_as", "append_extension", 1),
+                 false,
                  saveas, official); 
     } catch (Inkscape::Extension::Output::no_extension_found &e) {
         gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str());
@@ -528,8 +534,6 @@ file_save(SPDocument *doc, const Glib::ustring &uri,
 
 
 
-static Inkscape::UI::Dialog::FileSaveDialog *saveDialogInstance = NULL;
-
 /**
  *  Display a SaveAs dialog.  Save the document if OK pressed.
  *
@@ -541,7 +545,7 @@ sp_file_save_dialog(SPDocument *doc, bool is_copy)
 
     Inkscape::XML::Node *repr = sp_document_repr_root(doc);
 
-    Inkscape::Extension::Output *extension;
+    Inkscape::Extension::Output *extension = 0;
 
     //# Get the default extension name
     Glib::ustring default_extension;
@@ -589,7 +593,8 @@ sp_file_save_dialog(SPDocument *doc, bool is_copy)
             save_loc.append(formatBuf);
         }
     } else {
-        save_loc = Glib::path_get_dirname(doc->uri);
+        save_loc = Glib::build_filename(Glib::path_get_dirname(doc->uri),
+                                        Glib::path_get_basename(doc->uri));
     }
 
     // convert save_loc from utf-8 to locale
@@ -607,25 +612,35 @@ sp_file_save_dialog(SPDocument *doc, bool is_copy)
     } else {
         dialog_title = (char const *) _("Select file to save to");
     }
-    if (!saveDialogInstance)
-        saveDialogInstance =
-             Inkscape::UI::Dialog::FileSaveDialog::create(
-                 save_loc,
-                 Inkscape::UI::Dialog::SVG_TYPES,
-                 (char const *) _("Select file to save to"),
-                 default_extension
+    Inkscape::UI::Dialog::FileSaveDialog *saveDialog =
+        Inkscape::UI::Dialog::FileSaveDialog::create(
+            save_loc,
+            Inkscape::UI::Dialog::SVG_TYPES,
+            (char const *) _("Select file to save to"),
+            default_extension
             );
-    saveDialogInstance->change_title(dialog_title);
-    
-    bool success = saveDialogInstance->show();
-    if (!success)
-        return success;
 
-    Glib::ustring fileName = saveDialogInstance->getFilename();
+    saveDialog->change_title(dialog_title);
+    saveDialog->setSelectionType(extension);
 
-    Inkscape::Extension::Extension *selectionType =
-        saveDialogInstance->getSelectionType();
+    // allow easy access to the user's own templates folder             
+    gchar *templates = profile_path ("templates");
+    if (Inkscape::IO::file_test(templates, (GFileTest)(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))) {
+        dynamic_cast<Gtk::FileChooser *>(saveDialog)->add_shortcut_folder(templates);
+    }
+    g_free (templates);
 
+    bool success = saveDialog->show();
+    if (!success) {
+        delete saveDialog;
+        return success;
+    }
+
+    Glib::ustring fileName = saveDialog->getFilename();
+    Inkscape::Extension::Extension *selectionType = saveDialog->getSelectionType();
+
+    delete saveDialog;
+    saveDialog = 0;
 
     if (fileName.size() > 0) {
         Glib::ustring newFileName = Glib::filename_to_utf8(fileName);
@@ -640,7 +655,7 @@ sp_file_save_dialog(SPDocument *doc, bool is_copy)
         if (success)
             prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));
 
-        save_path = fileName;
+        save_path = Glib::path_get_dirname(fileName);
         prefs_set_string_attribute("dialogs.save_as", "path", save_path.c_str());
 
         return success;
@@ -690,6 +705,9 @@ sp_file_save(gpointer object, gpointer data)
 {
     if (!SP_ACTIVE_DOCUMENT)
         return false;
+
+    SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Saving document..."));
+
     sp_namedview_document_from_window(SP_ACTIVE_DESKTOP);
     return sp_file_save_document(SP_ACTIVE_DOCUMENT);
 }
@@ -746,26 +764,19 @@ file_import(SPDocument *in_doc, const Glib::ustring &uri,
     }
 
     if (doc != NULL) {
-        // the import extension has passed us a document, now we need to embed it into our document
-        if ( 0 ) {
-//            const gchar *docbase = (sp_repr_document_root( sp_repr_document( repr ))->attribute("sodipodi:docbase" );
-            g_message(" settings  uri  [%s]", doc->uri );
-            g_message("           base [%s]", doc->base );
-            g_message("           name [%s]", doc->name );
-            Inkscape::IO::fixupHrefs( doc, doc->base, TRUE );
-            g_message("        mid-fixup");
-            Inkscape::IO::fixupHrefs( doc, in_doc->base, TRUE );
-        }
-
         // move imported defs to our document's defs
         SPObject *in_defs = SP_DOCUMENT_DEFS(in_doc);
         SPObject *defs = SP_DOCUMENT_DEFS(doc);
+
+        Inkscape::IO::fixupHrefs(doc, in_doc->base, true);
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+
         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(in_defs)->lastChild();
         for (SPObject *child = sp_object_first_child(defs);
              child != NULL; child = SP_OBJECT_NEXT(child))
         {
             // FIXME: in case of id conflict, newly added thing will be re-ided and thus likely break a reference to it from imported stuff
-            SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(), last_def);
+            SP_OBJECT_REPR(in_defs)->addChild(SP_OBJECT_REPR(child)->duplicate(xml_doc), last_def);
         }
 
         guint items_count = 0;
@@ -780,12 +791,13 @@ file_import(SPDocument *in_doc, const Glib::ustring &uri,
 
         if ((style && style->firstChild()) || items_count > 1) {
             // create group
-            Inkscape::XML::Node *newgroup = sp_repr_new("svg:g");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(in_doc);
+            Inkscape::XML::Node *newgroup = xml_doc->createElement("svg:g");
             sp_repr_css_set (newgroup, style, "style");
 
             for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
                 if (SP_IS_ITEM(child)) {
-                    Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate();
+                    Inkscape::XML::Node *newchild = SP_OBJECT_REPR(child)->duplicate(xml_doc);
 
                     // convert layers to groups; FIXME: add "preserve layers" mode where each layer
                     // from impot is copied to the same-named layer in host
@@ -809,7 +821,7 @@ file_import(SPDocument *in_doc, const Glib::ustring &uri,
             // just add one item
             for (SPObject *child = sp_object_first_child(SP_DOCUMENT_ROOT(doc)); child != NULL; child = SP_OBJECT_NEXT(child) ) {
                 if (SP_IS_ITEM(child)) {
-                    Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate();
+                    Inkscape::XML::Node *newitem = SP_OBJECT_REPR(child)->duplicate(xml_doc);
                     newitem->setAttribute("inkscape:groupmode", NULL);
 
                     if (desktop) {
@@ -838,15 +850,18 @@ file_import(SPDocument *in_doc, const Glib::ustring &uri,
                 int const saved_pref = prefs_get_int_attribute("options.transform", "pattern", 1);
                 prefs_set_int_attribute("options.transform", "pattern", 1);
                 sp_document_ensure_up_to_date(sp_desktop_document(desktop));
-                NR::Point m( desktop->point() - selection->bounds().midpoint() );
-                sp_selection_move_relative(selection, m);
+                NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
+                if (sel_bbox) {
+                    NR::Point m( desktop->point() - sel_bbox->midpoint() );
+                    sp_selection_move_relative(selection, m);
+                }
                 prefs_set_int_attribute("options.transform", "pattern", saved_pref);
             }
         }
 
         sp_document_unref(doc);
         sp_document_done(in_doc, SP_VERB_FILE_IMPORT,
-                         /* TODO: annotate */ "file.cpp:900");
+                         _("Import"));
 
     } else {
         gchar *text = g_strdup_printf(_("Failed to load the requested file %s"), uri.c_str());
@@ -1097,7 +1112,7 @@ sp_file_print_preview(gpointer object, gpointer data)
 
 }
 
-void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, bool spns )
+void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, gboolean spns )
 {
     //g_message("Inkscape::IO::fixupHrefs( , [%s], )", base );
 
@@ -1125,7 +1140,7 @@ void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, bool spns )
             try
             {
                 URI uri(things[i]);
-                bool isAbs = g_path_is_absolute( things[i] );
+                gboolean isAbs = g_path_is_absolute( things[i] );
                 gchar *str = uri.toString();
                 g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
                            (int)uri.isRelative(),
@@ -1144,7 +1159,7 @@ void Inkscape::IO::fixupHrefs( SPDocument *doc, const gchar *base, bool spns )
                 if ( redo == NULL )
                 {
                     URI again = URI::fromUtf8( things[i] );
-                    bool isAbs = g_path_is_absolute( things[i] );
+                    gboolean isAbs = g_path_is_absolute( things[i] );
                     gchar *str = again.toString();
                     g_message( "abs:%d  isRel:%d  scheme:[%s]  path:[%s][%s]   uri[%s] / [%s]", (int)isAbs,
                                (int)again.isRelative(),