X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ffile.cpp;h=69d6da610383ec34533bebb3554e9cb750a4e546;hb=03edfb301b911c7a3313b58dd50193ba8e7e21ca;hp=6154fb97daac9cf22ae78066802d824927cfbffb;hpb=e34b436837164a85983d798ae08066800b81e889;p=inkscape.git diff --git a/src/file.cpp b/src/file.cpp index 6154fb97d..69d6da610 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -8,9 +8,10 @@ * Chema Celorio * bulia byak * + * Copyright (C) 2006 Johan Engelen * Copyright (C) 1999-2005 Authors - * Copyright (C) 2001-2002 Ximian, Inc. * Copyright (C) 2004 David Turner + * Copyright (C) 2001-2002 Ximian, Inc. * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -42,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" @@ -105,6 +107,7 @@ sp_file_new(const Glib::ustring &templ) sp_create_window(dtw, TRUE); dt = static_cast(dtw->view); sp_namedview_window_from_document(dt); + sp_namedview_update_layers_from_document(dt); } return dt; } @@ -200,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)); @@ -281,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) @@ -342,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" ); @@ -386,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(openDialogInstance)->add_shortcut_folder(INKSCAPE_EXAMPLESDIR); + } } + //# Show the dialog bool const success = openDialogInstance->show(); if (!success) @@ -462,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 <defs>")); SPDesktop *dt = SP_ACTIVE_DESKTOP; if (diff > 0) { @@ -484,18 +491,21 @@ sp_file_vacuum() /** * This 'save' function called by the others below + * + * \param official whether to set :output_module and :modified in the + * document; is true for normal save, false for temporary saves */ static bool file_save(SPDocument *doc, const Glib::ustring &uri, - Inkscape::Extension::Extension *key, bool saveas) + Inkscape::Extension::Extension *key, bool saveas, bool official) { if (!doc || uri.size()<1) //Safety check return false; try { Inkscape::Extension::save(key, doc, uri.c_str(), - saveas && prefs_get_int_attribute("dialogs.save_as", "append_extension", 1), - saveas, TRUE); // save officially, with inkscape: attributes set + false, + saveas, official); } catch (Inkscape::Extension::Output::no_extension_found &e) { gchar *safeUri = Inkscape::IO::sanitizeString(uri.c_str()); gchar *text = g_strdup_printf(_("No Inkscape extension found to save document (%s). This may have been caused by an unknown filename extension."), safeUri); @@ -524,18 +534,18 @@ 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. + * + * \param ascopy (optional) wether to set the documents->uri to the new filename or not */ bool -sp_file_save_dialog(SPDocument *doc) +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; @@ -583,7 +593,8 @@ sp_file_save_dialog(SPDocument *doc) 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 @@ -595,24 +606,41 @@ sp_file_save_dialog(SPDocument *doc) save_loc = save_loc_local; //# Show the SaveAs dialog - if (!saveDialogInstance) - saveDialogInstance = - Inkscape::UI::Dialog::FileSaveDialog::create( - save_loc, - Inkscape::UI::Dialog::SVG_TYPES, - (char const *) _("Select file to save to"), - default_extension + char const * dialog_title; + if (is_copy) { + dialog_title = (char const *) _("Select file to save a copy to"); + } else { + dialog_title = (char const *) _("Select file to save to"); + } + 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 ); - bool success = saveDialogInstance->show(); - if (!success) - return success; + saveDialog->change_title(dialog_title); + saveDialog->setSelectionType(extension); - Glib::ustring fileName = saveDialogInstance->getFilename(); + // 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(saveDialog)->add_shortcut_folder(templates); + } + g_free (templates); - Inkscape::Extension::Extension *selectionType = - saveDialogInstance->getSelectionType(); + 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); @@ -622,12 +650,12 @@ sp_file_save_dialog(SPDocument *doc) else g_warning( "Error converting save filename to UTF-8." ); - success = file_save(doc, fileName, selectionType, TRUE); + success = file_save(doc, fileName, selectionType, TRUE, !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; @@ -650,14 +678,14 @@ sp_file_save_document(SPDocument *doc) gchar const *fn = repr->attribute("sodipodi:modified"); if (fn != NULL) { - if (doc->uri == NULL - || repr->attribute("inkscape:output_extension") == NULL) + if ( doc->uri == NULL + || repr->attribute("inkscape:output_extension") == NULL ) { - return sp_file_save_dialog(doc); + return sp_file_save_dialog(doc, FALSE); } else { fn = g_strdup(doc->uri); gchar const *ext = repr->attribute("inkscape:output_extension"); - success = file_save(doc, fn, Inkscape::Extension::db.get(ext), FALSE); + success = file_save(doc, fn, Inkscape::Extension::db.get(ext), FALSE, TRUE); g_free((void *) fn); } } else { @@ -677,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); } @@ -691,11 +722,23 @@ sp_file_save_as(gpointer object, gpointer data) if (!SP_ACTIVE_DOCUMENT) return false; sp_namedview_document_from_window(SP_ACTIVE_DESKTOP); - return sp_file_save_dialog(SP_ACTIVE_DOCUMENT); + return sp_file_save_dialog(SP_ACTIVE_DOCUMENT, FALSE); } +/** + * Save a copy of a document, always displaying a sort of SaveAs dialog. + */ +bool +sp_file_save_a_copy(gpointer object, gpointer data) +{ + if (!SP_ACTIVE_DOCUMENT) + return false; + sp_namedview_document_from_window(SP_ACTIVE_DESKTOP); + return sp_file_save_dialog(SP_ACTIVE_DOCUMENT, TRUE); +} + /*###################### ## I M P O R T @@ -721,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; @@ -755,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 @@ -784,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) { @@ -813,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 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()); @@ -991,7 +1031,7 @@ sp_file_export_dialog(void *widget) else g_warning( "Error converting save filename to UTF-8." ); - success = file_save(doc, fileName, selectionType, TRUE); + success = file_save(doc, fileName, selectionType, TRUE, FALSE); if (success) prefs_set_recent_file(SP_DOCUMENT_URI(doc), SP_DOCUMENT_NAME(doc));