Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
actions-file.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Gio::Actions for file handling tied to the application and without GUI.
4 *
5 * Copyright (C) 2020 Tavmjong Bah
6 *
7 * The contents of this file may be used under the GNU General Public License Version 2 or later.
8 *
9 */
10
11#include "actions-file.h"
12
13#include <iostream>
14
15#include <giomm.h> // Not <gtkmm.h>! To eventually allow a headless version!
16#include <glibmm/i18n.h>
17
18#include "actions-helper.h"
19#include "document.h"
20#include "document-undo.h"
21#include "inkscape.h" // Inkscape::Application
23
24
25// Actions for file handling (should be integrated with file dialog).
26
27void
28file_open(const Glib::VariantBase& value, InkscapeApplication *app)
29{
30 Glib::Variant<Glib::ustring> s = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring> >(value);
31
32 Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(s.get());
33 if (!file->query_exists()) {
34 show_output(Glib::ustring("file_open: file '") + s.get().raw() + "' does not exist.");
35 return;
36 }
37 auto document = app->document_open(file).first;
38
39 app->set_active_document(document);
40 app->set_active_selection(document->getSelection());
41 app->set_active_desktop(nullptr);
42
43 document->ensureUpToDate();
44}
45
46void
47file_open_with_window(const Glib::VariantBase& value, InkscapeApplication *app)
48{
49 auto window = app->get_active_window();
50 if (!window) {
51 show_output("You cannot run this action without an active window");
52 return;
53 }
54
55 Glib::Variant<Glib::ustring> s = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(value);
56 Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(s.get());
57 if (!file->query_exists()) {
58 show_output(Glib::ustring("file_open: file '") + s.get().raw() + "' does not exist.");
59 return;
60 }
61 app->create_window(file);
62}
63
64
65void
66file_new(const Glib::VariantBase& value, InkscapeApplication *app)
67{
68 Glib::Variant<Glib::ustring> s = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring> >(value);
69
70 auto document = app->document_new(s.get());
71
72 app->set_active_document(document);
73 app->set_active_selection(document->getSelection());
74 app->set_active_desktop(nullptr); // No desktop (yet).
75
76 document->ensureUpToDate();
77}
78
79void
80file_rebase(const Glib::VariantBase& value, InkscapeApplication *app)
81{
82 Glib::Variant<bool> s = Glib::VariantBase::cast_dynamic<Glib::Variant<bool> >(value);
83 SPDocument *document = app->get_active_document();
84 document->rebase(s.get());
85
86 document->ensureUpToDate();
87 Inkscape::DocumentUndo::done(document, _("Replace file contents"), "");
88}
89
90// Need to create a document_revert that doesn't depend on windows.
91// void
92// file_revert(InkscapeApplication *app)
93// {
94// app->document_revert(app->get_current_document());
95// }
96
97// No checks for dataloss are performed. Useful for scripts.
98void
100{
101 SPDocument *document = app->get_active_document();
102 app->document_close(document);
103
104 app->set_active_document(nullptr);
105 app->set_active_selection(nullptr);
106 app->set_active_desktop(nullptr);
107}
108
109const Glib::ustring SECTION = NC_("Action Section", "File");
110
111std::vector<std::vector<Glib::ustring>> raw_data_file =
112{
113 // clang-format off
114 {"app.file-open", N_("File Open"), SECTION, N_("Open file") },
115 {"app.file-new", N_("File New"), SECTION, N_("Open new document using template") },
116 {"app.file-close", N_("File Close"), SECTION, N_("Close active document") },
117 {"app.file-open-window", N_("File Open Window"), SECTION, N_("Open file window") },
118 {"app.file-rebase", N_("File Contents Replace"), SECTION, N_("Replace current document's contents by contents of another file") }
119 // clang-format on
120};
121
122std::vector<std::vector<Glib::ustring>> hint_data_file =
123{
124 // clang-format off
125 {"app.file-open", N_("Enter file name")},
126 {"app.file-new", N_("Enter file name")},
127 {"app.file-open-window", N_("Enter file name")},
128 {"app.file-rebase-from-saved", N_("Namedview; Update=1, Replace=0")}
129 // clang-format on
130};
131
132void
134{
135 Glib::VariantType Bool( Glib::VARIANT_TYPE_BOOL);
136 Glib::VariantType Int( Glib::VARIANT_TYPE_INT32);
137 Glib::VariantType Double(Glib::VARIANT_TYPE_DOUBLE);
138 Glib::VariantType String(Glib::VARIANT_TYPE_STRING);
139 Glib::VariantType BString(Glib::VARIANT_TYPE_BYTESTRING);
140
141 // Debian 9 has 2.50.0
142#if GLIB_CHECK_VERSION(2, 52, 0)
143 auto *gapp = app->gio_app();
144
145 // clang-format off
146 gapp->add_action_with_parameter( "file-open", String, sigc::bind(sigc::ptr_fun(&file_open), app));
147 gapp->add_action_with_parameter( "file-new", String, sigc::bind(sigc::ptr_fun(&file_new), app));
148 gapp->add_action_with_parameter( "file-open-window", String, sigc::bind(sigc::ptr_fun(&file_open_with_window), app));
149 gapp->add_action( "file-close", sigc::bind(sigc::ptr_fun(&file_close), app));
150 gapp->add_action_with_parameter( "file-rebase", Bool, sigc::bind(sigc::ptr_fun(&file_rebase), app));
151 // clang-format on
152#else
153 show_output("add_actions: Some actions require Glibmm 2.52, compiled with: " << glib_major_version << "." << glib_minor_version);
154#endif
155
158}
159
160
161/*
162 Local Variables:
163 mode:c++
164 c-file-style:"stroustrup"
165 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
166 indent-tabs-mode:nil
167 fill-column:99
168 End:
169*/
170// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
void file_open_with_window(const Glib::VariantBase &value, InkscapeApplication *app)
void file_rebase(const Glib::VariantBase &value, InkscapeApplication *app)
std::vector< std::vector< Glib::ustring > > hint_data_file
const Glib::ustring SECTION
std::vector< std::vector< Glib::ustring > > raw_data_file
void file_open(const Glib::VariantBase &value, InkscapeApplication *app)
void add_actions_file(InkscapeApplication *app)
void file_new(const Glib::VariantBase &value, InkscapeApplication *app)
void file_close(InkscapeApplication *app)
void show_output(Glib::ustring const &data, bool const is_cerr)
void add_data(std::vector< std::vector< Glib::ustring > > const &raw_data)
void add_data(std::vector< std::vector< Glib::ustring > > &raw_data)
void set_active_desktop(SPDesktop *desktop)
InkActionExtraData & get_action_extra_data()
InkscapeWindow * get_active_window()
std::pair< SPDocument *, bool > document_open(Glib::RefPtr< Gio::File > const &file)
void set_active_selection(Inkscape::Selection *selection)
Gio::Application * gio_app()
The Gio application instance, never NULL.
void set_active_document(SPDocument *document)
SPDocument * get_active_document()
SPDocument * document_new(std::string const &template_filename={})
InkActionHintData & get_action_hint_data()
void create_window(Glib::RefPtr< Gio::File > const &file={})
Create a window given a Gio::File.
void document_close(SPDocument *document)
Close a document, remove from app.
static void done(SPDocument *document, Glib::ustring const &event_description, Glib::ustring const &undo_icon, unsigned int object_modified_tag=0)
Typed SVG document implementation.
Definition document.h:101
void rebase(Inkscape::XML::Document *new_xmldoc, bool keep_namedview=true)
Definition document.cpp:556
TODO: insert short description here.