summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/scroll-utils.cpp
blob: 5822a156a4bf32d03a15f11e8443ca402f0835fe (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
// SPDX-License-Identifier: GPL-2.0-or-later
/* Authors:
 *   Thomas Holder
 *
 * Copyright (C) 2020 authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "scroll-utils.h"

#include <gtkmm/scrolledwindow.h>

namespace Inkscape {
namespace UI {
namespace Widget {

/**
 * Get the first ancestor which is scrollable.
 */
Gtk::Widget *get_scrollable_ancestor(Gtk::Widget *widget)
{
    auto parent = widget->get_parent();
    if (!parent) {
        return nullptr;
    }
    if (auto scrollable = dynamic_cast<Gtk::ScrolledWindow *>(parent)) {
        return scrollable;
    }
    return get_scrollable_ancestor(parent);
}

/**
 * Return true if scrolling is allowed.
 *
 * Scrolling is allowed for any of:
 * - Shift modifier is pressed
 * - Widget has focus
 * - Widget has no scrollable ancestor
 */
bool scrolling_allowed(Gtk::Widget *widget, GdkEventScroll *event)
{
    bool const shift = event && (event->state & GDK_SHIFT_MASK);
    return shift ||               //
           widget->has_focus() || //
           get_scrollable_ancestor(widget) == nullptr;
}

} // namespace Widget
} // namespace UI
} // namespace Inkscape

// vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :