Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
document-check.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Check for data loss when closing a document window.
4 *
5 * Copyright (C) 2004-2021 Authors
6 *
7 * The contents of this file may be used under the GNU General Public License Version 2 or later.
8 *
9 */
10
11/* Authors:
12 * MenTaLguY
13 * Link Mauve
14 * Thomas Holder
15 * Tavmjong Bah
16 */
17
18#include "document-check.h"
19
20#include <glibmm/i18n.h> // Internationalization
21#include <glibmm/ustring.h>
22#include <gtkmm/messagedialog.h>
23
24#include "desktop.h"
25#include "document.h"
26#include "file.h"
27#include "inkscape-window.h"
28#include "extension/system.h" // Inkscape::Extension::FILE...
29#include "object/sp-namedview.h"
30#include "ui/dialog-run.h"
31#include "ui/util.h"
32#include "xml/node.h"
33
34static int run_dialog(Gtk::Window &window, char const * const save_text,
35 char const * const format, char const * const document_name)
36{
37 auto fmt = '\n' + Glib::ustring(format);
38 auto const message = g_markup_printf_escaped(fmt.c_str(), document_name);
39 auto dialog = Gtk::MessageDialog{window, message, true, Gtk::MessageType::WARNING,
40 Gtk::ButtonsType::NONE};
41 g_free(message);
42
43 dialog.property_destroy_with_parent() = true;
44
45 // Don't allow text to be selected (via tabbing).
46 auto const ma = dialog.get_message_area();
47 g_assert(ma);
48 auto const ma_labels = Inkscape::UI::get_children(*ma);
49 ma_labels.at(0)->set_focusable(false);
50
51 dialog.set_title(_("Save Document"));
52 dialog.add_button(_("Close _without saving"), Gtk::ResponseType::NO);
53 dialog.add_button(_("_Cancel"), Gtk::ResponseType::CANCEL);
54 dialog.add_button(_(save_text), Gtk::ResponseType::YES);
55 dialog.set_default_response(Gtk::ResponseType::YES);
56
57 return Inkscape::UI::dialog_run(dialog);
58}
59
66{
67 g_assert(desktop);
68 auto document = desktop->getDocument();
69 auto window = desktop->getInkscapeWindow();
70
71 if (document->isModifiedSinceSave()) {
72 // Document has been modified!
73
74 int const response = run_dialog(*window, _("_Save"),
75 _("<span weight=\"bold\" size=\"larger\">Save changes to document \"%s\" before closing?</span>\n\n"
76 "If you close without saving, your changes will be discarded."),
77 document->getDocumentName());
78
79 switch (response) {
80 case GTK_RESPONSE_YES:
81 {
82 // Save document
83 sp_namedview_document_from_window(desktop); // Save window geometry in document.
84 if (!sp_file_save_document(*window, document)) {
85 // Save dialog cancelled or save failed.
86 return true;
87 }
88 break;
89 }
90 case GTK_RESPONSE_NO:
91 break;
92 default: // cancel pressed, or dialog was closed
93 return true;
94 break;
95 }
96 }
97
98 // Check for data loss due to saving in lossy format.
99 bool allow_data_loss = false;
100 while (document->getReprRoot()->attribute("inkscape:dataloss") != nullptr && allow_data_loss == false) {
101 // This loop catches if the user saves to a lossy format when in the loop.
102
103 int const response = run_dialog(*window, _("_Save as Inkscape SVG"),
104 _("<span weight=\"bold\" size=\"larger\">The file \"%s\" was saved with a format that may cause data loss!</span>\n\n"
105 "Do you want to save this file as Inkscape SVG?"),
106 document->getDocumentName() ? document->getDocumentName() : "Unnamed");
107
108 switch (response) {
109 case GTK_RESPONSE_YES:
110 {
112 // Save dialog cancelled or save failed.
113 return TRUE;
114 }
115
116 break;
117 }
118 case GTK_RESPONSE_NO:
119 allow_data_loss = true;
120 break;
121 default: // cancel pressed, or dialog was closed
122 return true;
123 break;
124 }
125 }
126
127 return false;
128}
129
130/*
131 Local Variables:
132 mode:c++
133 c-file-style:"stroustrup"
134 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135 indent-tabs-mode:nil
136 fill-column:99
137 End:
138*/
139// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
To do: update description of desktop.
Definition desktop.h:149
SPDocument * getDocument() const
Definition desktop.h:189
InkscapeWindow const * getInkscapeWindow() const
Definition desktop.cpp:975
Editable view implementation.
bool document_check_for_data_loss(SPDesktop *desktop)
Check if closing document associated with window will cause data loss, and if so opens a dialog that ...
static int run_dialog(Gtk::Window &window, char const *const save_text, char const *const format, char const *const document_name)
bool sp_file_save_dialog(Gtk::Window &parentWindow, SPDocument *doc, Inkscape::Extension::FileSaveMethod save_method)
Display a SaveAs dialog.
Definition file.cpp:255
bool sp_file_save_document(Gtk::Window &parentWindow, SPDocument *doc)
Save a document, displaying a SaveAs dialog if necessary.
Definition file.cpp:364
Inkscape - An SVG editor.
@ FILE_SAVE_METHOD_INKSCAPE_SVG
Definition system.h:40
std::vector< Gtk::Widget * > get_children(Gtk::Widget &widget)
Get a vector of the widgetʼs children, from get_first_child() through each get_next_sibling().
Definition util.cpp:141
int dialog_run(Gtk::Dialog &dialog)
This is a GTK4 porting aid meant to replace the removal of the Gtk::Dialog synchronous API.
int const char * fmt
Definition safe-printf.h:18
void sp_namedview_document_from_window(SPDesktop *desktop)
SPDesktop * desktop
Interface for XML nodes.