1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #ifndef SEEN_INKSCAPE_UI_WIDGET_SCROLLPROTECTED_H
3 #define SEEN_INKSCAPE_UI_WIDGET_SCROLLPROTECTED_H
4 
5 /* Authors:
6  *   Thomas Holder
7  *
8  * Copyright (C) 2020 authors
9  *
10  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11  */
12 
13 #include "scroll-utils.h"
14 
15 namespace Inkscape {
16 namespace UI {
17 namespace Widget {
18 
19 /**
20  * A class decorator which blocks the scroll event if the widget does not have
21  * focus and any ancestor is a scrollable window, and SHIFT is not pressed.
22  *
23  * For custom scroll event handlers, derived classes must implement
24  * on_safe_scroll_event instead of on_scroll_event. Directly connecting to
25  * signal_scroll_event() will bypass the scroll protection.
26  *
27  * @tparam Base A subclass of Gtk::Widget
28  */
29 template <typename Base>
30 class ScrollProtected : public Base
31 {
32 public:
33     using Base::Base;
34 
35 protected:
36     /**
37      * Event handler for "safe" scroll events which are only triggered if:
38      * - the widget has focus
39      * - or the widget has no scrolled window ancestor
40      * - or the Shift key is pressed
41      */
on_safe_scroll_event(GdkEventScroll * event)42     virtual bool on_safe_scroll_event(GdkEventScroll *event)
43     { //
44         return Base::on_scroll_event(event);
45     }
46 
on_scroll_event(GdkEventScroll * event)47     bool on_scroll_event(GdkEventScroll *event) final
48     {
49         if (!scrolling_allowed(this, event)) {
50             return false;
51         }
52         return on_safe_scroll_event(event);
53     }
54 };
55 
56 } // namespace Widget
57 } // namespace UI
58 } // namespace Inkscape
59 
60 #endif
61 // vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :
62