summaryrefslogtreecommitdiffstats
path: root/src/inkscape-window.cpp
blob: 8306d039d918f21ea1bda8bfb1b0771970e79887 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 * Inkscape - An SVG editor.
 */
/*
 * 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 "inkscape-window.h"
#include "inkscape.h"   // SP_ACTIVE_DESKTOP
#include "desktop-events.h" // Handle key events
#include "enums.h"      // PREFS_WINDOW_GEOMETRY_NONE

#include "inkscape-application.h"

#include "actions/actions-canvas-mode.h"
#include "actions/actions-canvas-snapping.h"
#include "actions/actions-canvas-transform.h"
#include "actions/actions-dialogs.h"
#include "actions/actions-edit-window.h"
#include "actions/actions-file-window.h"
#include "actions/actions-help-url.h"
#include "actions/actions-layer.h"
#include "actions/actions-node-align.h" // Node alignment.
#include "actions/actions-paths.h"  // TEMP
#include "actions/actions-selection-window.h"
#include "actions/actions-tools.h"
#include "actions/actions-view-mode.h"
#include "actions/actions-view-window.h"

#include "object/sp-namedview.h"  // TODO Remove need for this!

#include "ui/dialog/dialog-container.h"
#include "ui/dialog/dialog-manager.h"
#include "ui/dialog/dialog-window.h"
#include "ui/drag-and-drop.h"  // Move to canvas?
#include "ui/interface.h" // main menu, sp_ui_close_view()

#include "ui/monitor.h" // get_monitor_geometry_at_point()

#include "ui/desktop/menubar.h"
#include "ui/desktop/menu-icon-shift.h"

#include "ui/drag-and-drop.h"

#include "ui/event-debug.h"
#include "ui/shortcuts.h"

#include "widgets/desktop-widget.h"
#include "ui/util.h"
#include "ui/widget/canvas.h"

using Inkscape::UI::Dialog::DialogManager;
using Inkscape::UI::Dialog::DialogContainer;
using Inkscape::UI::Dialog::DialogWindow;

static gboolean _resize_children(Gtk::Window *win)
{
    Inkscape::UI::resize_widget_children(win);
    return false;
}


InkscapeWindow::InkscapeWindow(SPDocument* document)
    : _document(document)
    , _app(nullptr)
{
    if (!_document) {
        std::cerr << "InkscapeWindow::InkscapeWindow: null document!" << std::endl;
        return;
    }

    _app = InkscapeApplication::instance();
    _app->gtk_app()->add_window(*this);

    set_resizable(true);

    // =============== Build interface ===============

    // Main box
    _mainbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
    _mainbox->set_name("DesktopMainBox");
    _mainbox->show();
    add(*_mainbox);

    // Desktop widget (=> MultiPaned)
    _desktop_widget = new SPDesktopWidget(this, _document);
    _desktop_widget->window = this;
    _desktop_widget->show();
    _desktop = _desktop_widget->desktop;

    // =================== Actions ===================

    // After canvas has been constructed.. move to canvas proper.
    add_actions_canvas_mode(this);          // Actions to change canvas display mode.
    add_actions_canvas_snapping(this);      // Actions to toggle on/off snapping modes.
    add_actions_canvas_transform(this);     // Actions to transform canvas view.
    add_actions_dialogs(this);              // Actions to open dialogs.
    add_actions_edit_window(this);          // Actions to edit.
    add_actions_file_window(this);          // Actions for file actions which are desktop dependent.
    add_actions_help_url(this);             // Actions to help url.
    add_actions_layer(this);                // Actions for layer.
    add_actions_node_align(this);           // Actions to align and distribute nodes (requiring Node tool).
    add_actions_path(this);                 // Actions for paths. TEMP
    add_actions_select_window(this);        // Actions with desktop selection
    add_actions_tools(this);                // Actions to switch between tools.
    add_actions_view_mode(this);            // Actions to change how Inkscape canvas is displayed.
    add_actions_view_window(this);          // Actions to add/change window of Inkscape

    // Add document action group to window and export to DBus.
    insert_action_group("doc", document->getActionGroup());

    auto connection = _app->gio_app()->get_dbus_connection();
    if (connection) {
        std::string document_action_group_name = _app->gio_app()->get_dbus_object_path() + "/document/" + std::to_string(get_id());
        connection->export_action_group(document_action_group_name, document->getActionGroup());
    }

    // This is called here (rather than in InkscapeApplication) solely to add win level action
    // tooltips to the menu label-to-tooltip map.
    build_menu();

    // ========== Drag and Drop of Documents =========
    ink_drag_setup(_desktop_widget);

    // Pallet

    // Status bar

    // The main section
    _mainbox->pack_start(*Gtk::manage(_desktop_widget), true, true);

    // ================== Callbacks ==================
    signal_window_state_event().connect(sigc::mem_fun(*_desktop, &SPDesktop::onWindowStateEvent));
    signal_focus_in_event().connect(    sigc::mem_fun(*_desktop_widget, &SPDesktopWidget::onFocusInEvent));


    // ================ Window Options ===============
    setup_view();

    // Show dialogs after the main window, otherwise dialogs may be associated as the main window of the program.
    // Restore short-lived floating dialogs state if this is the first window being opened
    bool include_short_lived = _app->get_number_of_windows() == 0;
    DialogManager::singleton().restore_dialogs_state(_desktop->getContainer(), include_short_lived);

    // This pokes the window to request the right size for the dialogs once loaded.
    g_idle_add(GSourceFunc(&_resize_children), this);

    // ================= Shift Icons =================
    // Note: The menu is defined at the app level but shifting icons requires actual widgets and
    // must be done on the window level.
    for (auto child : get_children()) {
        auto menubar = dynamic_cast<Gtk::MenuBar *>(child);
        if (menubar) {
            shift_icons_recursive(menubar);
        }
    }

    // ========= Update text for Accellerators =======
    Inkscape::Shortcuts::getInstance().update_gui_text_recursive(this);
}

InkscapeWindow::~InkscapeWindow()
{
    g_idle_remove_by_data(this);
}

// Change a document, leaving desktop/view the same. (Eventually move all code here.)
void
InkscapeWindow::change_document(SPDocument* document)
{
    if (!_app) {
        std::cerr << "Inkscapewindow::change_document: app is nullptr!" << std::endl;
        return;
    }

    _document = document;
    _app->set_active_document(_document);
    remove_action_group("doc");
    insert_action_group("doc", document->getActionGroup());

    setup_view();
    update_dialogs();
}

// Sets up the window and view according to user preferences and <namedview> of the just loaded document
void
InkscapeWindow::setup_view()
{
    // Make sure the GdkWindow is fully initialized before resizing/moving
    // (ensures the monitor it'll be shown on is known)
    realize();

    // Resize the window to match the document properties
    sp_namedview_window_from_document(_desktop); // This should probably be a member function here.

    // Must show before setting zoom and view! (crashes otherwise)
    //
    // Showing after resizing/moving allows the window manager to correct an invalid size/position of the window
    // TODO: This does *not* work when called from 'change_document()', i.e. when the window is already visible.
    //       This can result in off-screen windows! We previously worked around this by hiding and re-showing
    //       the window, but a call to hide() causes Inkscape to just exit since the migration to Gtk::Application
    show();
    
    sp_namedview_zoom_and_view_from_document(_desktop);
    sp_namedview_update_layers_from_document(_desktop);

    SPNamedView *nv = _desktop->namedview;
    if (nv && nv->lockguides) {
        nv->setLockGuides(true);
    }
}

bool
InkscapeWindow::on_key_press_event(GdkEventKey* event)
{
#ifdef EVENT_DEBUG
    ui_dump_event(reinterpret_cast<GdkEvent *>(event), "\nInkscapeWindow::on_key_press_event");
#endif

    // Key press and release events are normally sent first to Gtk::Window for processing as
    // accelerators and menomics before bubbling up from the "grab" or "focus" widget (unlike other
    // events which always bubble up). This would means that key combinations used for accelerators
    // won't reach the focus widget (and our tool event handlers). As we use single keys for
    // accelerators, we wouldn't even be able to type text! We can get around this by sending key
    // events first to the focus widget.
    //
    // See https://developer.gnome.org/gtk3/stable/chap-input-handling.html (Event Propagation)

    auto focus = get_focus();
    if (focus) {
        if (focus->event(reinterpret_cast<GdkEvent *>(event))) {
            return true;
        }
    }

    if (Gtk::Window::on_key_press_event(event)) {
        return true;
    }

    // Not handled
    return false;
}

/**
 * If "dialogs on top" is activated in the preferences, set `parent` as the
 * new transient parent for all DialogWindow windows of the application.
 */
static void retransientize_dialogs(Gtk::Window &parent)
{
    assert(!dynamic_cast<DialogWindow *>(&parent));

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool window_above =
        prefs->getInt("/options/transientpolicy/value", PREFS_DIALOGS_WINDOWS_NORMAL) != PREFS_DIALOGS_WINDOWS_NONE;

    for (auto const &window : parent.get_application()->get_windows()) {
        if (auto dialog_window = dynamic_cast<DialogWindow *>(window)) {
            if (window_above) {
                dialog_window->set_transient_for(parent);
            } else {
                dialog_window->unset_transient_for();
            }
        }
    }
}

bool
InkscapeWindow::on_focus_in_event(GdkEventFocus* event)
{
    if (_app) {
        _app->set_active_window(this);
        _app->set_active_document(_document);
        _app->set_active_view(_desktop);
        _app->set_active_selection(_desktop->selection);
        _app->windows_update(_document);
        update_dialogs();
        retransientize_dialogs(*this);
    } else {
        std::cerr << "Inkscapewindow::on_focus_in_event: app is nullptr!" << std::endl;
    }

    return Gtk::ApplicationWindow::on_focus_in_event(event);
}

// Called when a window is closed via the 'X' in the window bar.
bool
InkscapeWindow::on_delete_event(GdkEventAny* event)
{
    if (_app) {
        _app->destroy_window(this);
    }
    return true;
};

/**
 * Configure is called when the widget's size, position or stack changes.
 */
bool InkscapeWindow::on_configure_event(GdkEventConfigure *event)
{
    bool ret = Gtk::ApplicationWindow::on_configure_event(event);
    // Store the desktop widget size on resize.
    if (!_desktop || !get_realized())
        return ret;

    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool maxed = _desktop->is_maximized();
    bool full = _desktop->is_fullscreen();
    prefs->setBool("/desktop/geometry/fullscreen", full);
    prefs->setBool("/desktop/geometry/maximized", maxed);

    // Don't save geom for maximized, fullscreen or iconified windows.
    // It just tells you the current maximized size, which is not
    // as useful as whatever value it had previously.
    if (!_desktop->is_iconified() && !maxed && !full) {

        // Get size is more accurate than frame extends for window size.
        int w,h = 0;
        get_size(w, h);
        prefs->setInt("/desktop/geometry/width", w);
        prefs->setInt("/desktop/geometry/height", h);

        // Frame extends returns real positions, unlike get_position()
        if (Glib::RefPtr<Gdk::Window> gdkw = get_window()) {
            Gdk::Rectangle rect;
            gdkw->get_frame_extents(rect);
            prefs->setInt("/desktop/geometry/x", rect.get_x());
            prefs->setInt("/desktop/geometry/y", rect.get_y());
        }
    }
    return ret;
}

void InkscapeWindow::update_dialogs()
{
    std::vector<Gtk::Window *> windows = _app->gtk_app()->get_windows();
    for (auto const &window : windows) {
        DialogWindow *dialog_window = dynamic_cast<DialogWindow *>(window);
        if (dialog_window) {
            // Update the floating dialogs, reset them to the new desktop.
            dialog_window->set_inkscape_window(this);
        }
    }

    // Update the docked dialogs in this InkscapeWindow
    _desktop->updateDialogs();
}

/*
  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 :