summaryrefslogtreecommitdiffstats
path: root/src/inkview-window.cpp
blob: 57e7f4727e404049302232450a241dad226608e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 * Inkview - An SVG file viewer.
 */
/*
 * Authors:
 *   Tavmjong Bah
 *
 * Copyright (C) 2018 Authors
 *
 * The contents of this file may be used under the GNU General Public License Version 2 or later.
 * Read the file 'COPYING' for more information.
 *
 */


#include "inkview-window.h"

#include <iostream>

#include "document.h"

#include "ui/monitor.h"
#include "ui/view/svg-view-widget.h"

#include "util/units.h"

InkviewWindow::InkviewWindow(const Gio::Application::type_vec_files files,
                             bool fullscreen,
                             bool recursive,
                             int timer,
                             double scale,
                             bool preload
    )
    : _files(files)
    , _fullscreen(fullscreen)
    , _recursive(recursive)
    , _timer(timer)
    , _scale(scale)
    , _preload(preload)
    , _index(-1)
    , _view(nullptr)
    , _controlwindow(nullptr)
{
    _files = create_file_list(_files);

    if (_preload) {
        preload_documents();
    }

    if (_files.empty()) {
        throw NoValidFilesException();
    }

    _documents.resize( _files.size(), nullptr); // We keep _documents and _files in sync.

    // Callbacks
    signal_key_press_event().connect(sigc::mem_fun(*this, &InkviewWindow::key_press), false);

    if (_timer) {
        Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &InkviewWindow::on_timer), _timer);
    }

    // Actions
    add_action( "show_first", sigc::mem_fun(*this, &InkviewWindow::show_first) );
    add_action( "show_prev",  sigc::mem_fun(*this, &InkviewWindow::show_prev)  );
    add_action( "show_next",  sigc::mem_fun(*this, &InkviewWindow::show_next)  );
    add_action( "show_last",  sigc::mem_fun(*this, &InkviewWindow::show_last)  );

    // ToDo: Add Pause, Resume.

    if (_fullscreen) {
        Gtk::Window::fullscreen();
    }

    // Show first file
    activate_action( "show_first" );
}

std::vector<Glib::RefPtr<Gio::File> >
InkviewWindow::create_file_list(const std::vector<Glib::RefPtr<Gio::File > >& files)
{
    std::vector<Glib::RefPtr<Gio::File> > valid_files;

    static bool first = true;

    for (auto file : files) {
        Gio::FileType type = file->query_file_type();
        switch (type) {
            case Gio::FILE_TYPE_NOT_KNOWN:
                std::cerr << "InkviewWindow: File or directory does not exist: "
                          << file->get_basename() << std::endl;
                break;

            case Gio::FILE_TYPE_REGULAR:
            {
                // Only look at SVG and SVGZ files.
                std::string basename = file->get_basename();
                std::string extension = basename.substr(basename.find_last_of(".") + 1);
                if (extension == "svg" || extension == "svgz") {
                    valid_files.push_back(file);
                }
                break;
            }

            case Gio::FILE_TYPE_DIRECTORY:
            {
                if (_recursive || first) {
                    // No easy way to get children of directory!
                    Glib::RefPtr<Gio::FileEnumerator> children = file->enumerate_children();
                    Glib::RefPtr<Gio::FileInfo> info;
                    std::vector<Glib::RefPtr<Gio::File> > input;
                    while ((info = children->next_file())) {
                        input.push_back(children->get_child(info));
                    }
                    auto new_files = create_file_list(input);
                    valid_files.insert(valid_files.end(), new_files.begin(), new_files.end());
                }
                break;
            }
            default:
                std::cerr << "InkviewWindow: Unknown file type: " << type << std::endl;
        }
        first = false;
    }

    return valid_files;
}

void
InkviewWindow::update_title()
{
    Glib::ustring title(_documents[_index]->getDocumentName());

    if (_documents.size() > 1) {
        title += Glib::ustring::compose("  (%1/%2)", _index+1, _documents.size());
    }

    set_title(title);
}

// Returns true if successfully shows document.
bool
InkviewWindow::show_document(SPDocument* document)
{
    document->ensureUpToDate();  // Crashes on some documents if this isn't called!

    // Resize window:  (Might be better to use get_monitor_geometry_at_window(this))
    Gdk::Rectangle monitor_geometry = Inkscape::UI::get_monitor_geometry_primary();
    int width  = MIN((int)document->getWidth().value("px")  * _scale,  monitor_geometry.get_width());
    int height = MIN((int)document->getHeight().value("px") * _scale,  monitor_geometry.get_height());
    resize (width, height);

    if (_view) {
        _view->setDocument(document);
    } else {
        _view = Gtk::manage(new Inkscape::UI::View::SVGViewWidget(document));
        add (*_view);
    }

    update_title();

    return true;
}


// Load document, if fail, remove entry from lists.
SPDocument*
InkviewWindow::load_document()
{
    SPDocument* document = _documents[_index];

    if (!document) {
        // We need to load document. ToDo: Pass Gio::File. Is get_base_name() better?
        document = SPDocument::createNewDoc (_files[_index]->get_parse_name().c_str(), true, false);
        if (document) {
            // We've successfully loaded it!
            _documents[_index] = document;
        }
    }

    if (!document) {
        // Failed to load document, remove from vectors.
        _documents.erase(std::next(_documents.begin(), _index));
        _files.erase(    std::next(    _files.begin(), _index));
    }

    return document;
}



void
InkviewWindow::preload_documents()
{
    for (auto it =_files.begin(); it != _files.end(); ) {

        SPDocument* document =
            SPDocument::createNewDoc ((*it)->get_parse_name().c_str(), true, false);
        if (document) {
            _documents.push_back(document);
            ++it;
        } else {
            it = _files.erase(it);
        }
    }
}

static std::string window_markup = R"(
<interface>
  <object class="GtkWindow" id="ControlWindow">
    <child>
      <object class="GtkButtonBox">
        <child>
          <object class="GtkButton" id="show-first">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <child>
              <object class="GtkImage">
                <property name="visible">True</property>
                <property name="icon_name">go-first</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkButton" id="show-prev">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <child>
              <object class="GtkImage">
                <property name="visible">True</property>
                <property name="icon_name">go-previous</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkButton" id="show-next">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkImage">
                <property name="visible">True</property>
                <property name="icon_name">go-next</property>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkButton" id="show-last">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkImage">
                <property name="visible">True</property>
                <property name="icon_name">go-last</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>
)";

void
InkviewWindow::show_control()
{
    if (!_controlwindow) {

        auto builder = Gtk::Builder::create();
        try
        {
            builder->add_from_string(window_markup);
        }
        catch (const Glib::Error& err)
        {
            std::cerr << "InkviewWindow::show_control: builder failed: " << err.what() << std::endl;
            return;
        }


        builder->get_widget("ControlWindow", _controlwindow);
        if (!_controlwindow) {
            std::cerr << "InkviewWindow::show_control: Control Window not found!" << std::endl;
            return;
        }

        // Need to give control window access to viewer window's actions.
        Glib::RefPtr<Gio::ActionGroup> viewer = get_action_group("win");
        if (viewer) {
            _controlwindow->insert_action_group("viewer", viewer);
        }

        // Gtk::Button not derived from Gtk::Actionable due to ABI issues. Must use Gtk.
        // Fixed in Gtk4. In Gtk4 this can be replaced by setting the action in the interface.
        Gtk::Button* button;
        builder->get_widget("show-first", button);
        gtk_actionable_set_action_name( GTK_ACTIONABLE(button->gobj()), "viewer.show_first");
        builder->get_widget("show-prev", button);
        gtk_actionable_set_action_name( GTK_ACTIONABLE(button->gobj()), "viewer.show_prev");
        builder->get_widget("show-next", button);
        gtk_actionable_set_action_name( GTK_ACTIONABLE(button->gobj()), "viewer.show_next");
        builder->get_widget("show-last", button);
        gtk_actionable_set_action_name( GTK_ACTIONABLE(button->gobj()), "viewer.show_last");

        _controlwindow->set_resizable(false);
        _controlwindow->set_transient_for(*this);
        _controlwindow->show_all();

    } else {
        _controlwindow->present();
    }
}

// Next document
void
InkviewWindow::show_next()
{
    ++_index;

    SPDocument* document = nullptr;

    while (_index < _documents.size() && !document) {
        document = load_document();
    }

    if (document) {
        // Show new document
        show_document(document);
    } else {
        // Failed to load new document, keep current.
        --_index;
    }
}

// Previous document
void
InkviewWindow::show_prev()
{
    SPDocument* document = nullptr;
    int old_index = _index;

    while (_index > 0 && !document) {
        --_index;
        document = load_document();
    }

    if (document) {
        // Show new document
        show_document(document);
    } else {
        // Failed to load new document, keep current.
        _index = old_index;
    }
}

// Show first document
void
InkviewWindow::show_first()
{
    _index = -1;
    show_next();
}

// Show last document
void
InkviewWindow::show_last()
{
    _index = _documents.size();
    show_prev();
}

bool
InkviewWindow::key_press(GdkEventKey* event)
{
    switch (event->keyval) {
        case GDK_KEY_Up:
        case GDK_KEY_Home:
            show_first();
            break;

        case GDK_KEY_Down:
        case GDK_KEY_End:
            show_last();
            break;

        case GDK_KEY_F11:
            if (_fullscreen) {
                unfullscreen();
                _fullscreen = false;
            } else {
                fullscreen();
                _fullscreen = true;
            }
            break;

        case GDK_KEY_Return:
            show_control();
            break;

        case GDK_KEY_KP_Page_Down:
        case GDK_KEY_Page_Down:
        case GDK_KEY_Right:
        case GDK_KEY_space:
            show_next();
            break;

        case GDK_KEY_KP_Page_Up:
        case GDK_KEY_Page_Up:
        case GDK_KEY_Left:
        case GDK_KEY_BackSpace:
            show_prev();
            break;

        case GDK_KEY_Escape:
        case GDK_KEY_q:
        case GDK_KEY_Q:
            close();
            break;

        default:
            break;
    }
    return false;
}

bool
InkviewWindow::on_timer()
{
    show_next();

    // Stop if at end.
    if (_index >= _documents.size() - 1) {
        return false;
    }
    return true;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :