Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
inkview-application.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/*
7 * Authors:
8 * Tavmjong Bah
9 *
10 * Copyright (C) 2018 Authors
11 *
12 * The contents of this file may be used under the GNU General Public License Version 2 or later.
13 * Read the file 'COPYING' for more information.
14 *
15 */
16
17#ifdef HAVE_CONFIG_H
18# include "config.h" // Defines ENABLE_NLS
19#endif
20
21#include <iostream>
22#include <glibmm/i18n.h> // Internationalization
23#include <glibmm/variantdict.h>
24#include <gtkmm/filechooserdialog.h>
25
26#include "inkview-application.h"
27#include "inkscape.h" // Inkscape::Application
28#include "inkscape-version.h" // Inkscape version
30#include "inkgc/gc-core.h" // Garbage Collecting init
31#include "inkview-window.h"
32#include "ui/dialog-run.h"
33
34#ifdef ENABLE_NLS
35// Native Language Support - shouldn't this always be used?
36#include "helper/gettext.h" // gettext init
37#endif // ENABLE_NLS
38
39// This is a bit confusing as there are two ways to handle command line arguments and files
40// depending on if the Gio::Application::Flags::HANDLES_OPEN and/or Gio::Application::Flags::HANDLES_COMMAND_LINE
41// flags are set. If the open flag is set and the command line not, the all the remainng arguments
42// after calling on_handle_local_options() are assumed to be filenames.
43
45 : Gtk::Application("org.inkscape.Inkview",
46 Gio::Application::Flags::HANDLES_OPEN | // Use default file opening.
47 Gio::Application::Flags::NON_UNIQUE ) // Allows different instances of Inkview to run at same time.
48 , fullscreen(false)
49 , recursive(false)
50 , timer(0)
51 , scale(1.0)
52 , preload(false)
53{
54 // ==================== Initializations =====================
55 // Garbage Collector
57
58#ifdef ENABLE_NLS
59 // Native Language Support (shouldn't this always be used?).
61#endif
62
63 Glib::set_application_name(N_("Inkview - An SVG File Viewer")); // After gettext() init.
64
65#if GLIBMM_CHECK_VERSION(2,56,0)
66 // Additional informational strings for --help output
67 // TODO: Claims to be translated automatically, but seems broken, so pass already translated strings
68 set_option_context_parameter_string(_("path1 [path2 [pathN]]"));
69 set_option_context_summary(_("Open one or more SVG files (or folders containing SVG files) for viewing."));
70#endif
71
72 // Will automatically handle character conversions.
73 // Note: OptionType::FILENAME => std::string, OptionType::STRING => Glib::ustring.
74
75 // clang-format off
76 add_main_option_entry(OptionType::BOOL, "version", 'V', N_("Print Inkview version"), "");
77 add_main_option_entry(OptionType::BOOL, "fullscreen", 'f', N_("Launch in fullscreen mode"), "");
78 add_main_option_entry(OptionType::BOOL, "recursive", 'r', N_("Search folders recursively"), "");
79 add_main_option_entry(OptionType::INT, "timer", 't', N_("Change image every NUMBER seconds"), N_("NUMBER"));
80 add_main_option_entry(OptionType::DOUBLE, "scale", 's', N_("Scale image by factor NUMBER"), N_("NUMBER"));
81 add_main_option_entry(OptionType::BOOL, "preload", 'p', N_("Preload files"), "");
82 // clang-format on
83
84 signal_handle_local_options().connect(sigc::mem_fun(*this, &InkviewApplication::on_handle_local_options), true);
85
86 // This is normally called for us... but after the "handle_local_options" signal is emitted. If
87 // we want to rely on actions for handling options, we need to call it here. This appears to
88 // have no unwanted side-effect. It will also trigger the call to on_startup().
89 register_application();
90}
91
93
94void
96{
97 Gtk::Application::on_startup();
98
99 // Inkscape::Application should disappear!
101}
102
103
104// Open document window with default document. Either this or on_open() is called.
105void
107{
108 // show file chooser dialog if no files/folders are given on the command line
109 // TODO: A FileChooserNative would be preferential, but offers no easy way to allow files AND folders
110 Glib::ustring title = _("Select Files or Folders to view");
111 Gtk::FileChooserDialog file_chooser(title + "…", Gtk::FileChooser::Action::OPEN);
112 file_chooser.add_button(_("Select"), 42); // use custom response ID that is not intercepted by the file chooser
113 // (allows to pick files AND folders)
114 file_chooser.set_select_multiple();
115
116 Glib::RefPtr<Gtk::FileFilter> file_filter = Gtk::FileFilter::create();
117 file_filter->add_pattern("*.svg");
118 file_filter->set_name(_("Scalable Vector Graphics"));
119 file_chooser.add_filter(file_filter);
120
121 int res = Inkscape::UI::dialog_run(file_chooser);
122 if (res == 42) {
123 auto files = file_chooser.get_files2();
124 if (!files.empty()) {
125 on_open(files, "");
126 }
127 }
128}
129
130// Open document window for each file. Either this or on_activate() is called.
131void InkviewApplication::on_open(Gio::Application::type_vec_files const &files, Glib::ustring const &hint)
132{
133 try {
135 } catch (InkviewWindow::NoValidFilesException const &) {
136 std::cerr << _("Error") << ": " << _("No (valid) files to open.") << std::endl;
137 return; // Fixme: Exit with code 1 - see https://gitlab.com/inkscape/inkscape/-/issues/270.
138 }
139
140 window->set_visible(true);
141 add_window(*window);
142}
143
144// ========================= Callbacks ==========================
145
146/*
147 * Handle command line options callback.
148 */
149int
150InkviewApplication::on_handle_local_options(const Glib::RefPtr<Glib::VariantDict>& options)
151{
152 if (!options) {
153 std::cerr << "InkviewApplication::on_handle_local_options: options is null!" << std::endl;
154 return -1; // Keep going
155 }
156
157 if (options->contains("version")) {
158 std::cout << "Inkscape " << Inkscape::version_string << std::endl;
159 return EXIT_SUCCESS;
160 }
161
162 if (options->contains("fullscreen")) {
163 fullscreen = true;
164 }
165
166 if (options->contains("recursive")) {
167 recursive = true;
168 }
169
170 if (options->contains("timer")) {
171 options->lookup_value("timer", timer);
172 }
173
174 if (options->contains("scale")) {
175 options->lookup_value("scale", scale);
176 }
177
178 if (options->contains("preload")) {
179 options->lookup_value("preload", preload);
180 }
181
182 return -1; // Keep going
183}
184
185/*
186 Local Variables:
187 mode:c++
188 c-file-style:"stroustrup"
189 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
190 indent-tabs-mode:nil
191 fill-column:99
192 End:
193*/
194// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
double scale
Definition aa.cpp:228
static void create(bool use_gui)
Creates a new Inkscape::Application global object.
Definition inkscape.cpp:128
int on_handle_local_options(Glib::RefPtr< Glib::VariantDict > const &options) override
void on_open(Gio::Application::type_vec_files const &files, Glib::ustring const &hint) override
InkviewApplication()
Exclusively for the creation of the singleton instance inside main().
~InkviewApplication() override
Wrapper for Boehm GC.
helper functions for gettext
macro for checking glibmm version
Mini static library that contains the version of Inkscape.
Inkview - An SVG file viewer.
Inkview - An SVG file viewer.
Definition desktop.h:50
void init()
Definition gc-core.h:119
int dialog_run(Gtk::Dialog &dialog)
This is a GTK4 porting aid meant to replace the removal of the Gtk::Dialog synchronous API.
char const * version_string
full version string
void initialize_gettext()
does all required gettext initialization and takes care of the respective locale directory paths
Definition gettext.cpp:30