summaryrefslogtreecommitdiffstats
path: root/src/ui/tool/selector.cpp
blob: fe541e823f68df26c7d9cf115bb35d47c4c52d3a (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
/** @file
 * Selector component (click and rubberband)
 */
/* Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *   Jon A. Cruz <jon@joncruz.org>
 *
 * Copyright (C) 2009 Authors
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "control-point.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "display/sodipodi-ctrlrect.h"
#include "ui/tools/tool-base.h"
#include "preferences.h"
#include "ui/tool/event-utils.h"
#include "ui/tool/selector.h"

#include <gdk/gdkkeysyms.h>

namespace Inkscape {
namespace UI {

/** A hidden control point used for rubberbanding and selection.
 * It uses a clever hack: the canvas item is hidden and only receives events when they
 * are passed to it using Selector's event() function. When left mouse button
 * is pressed, it grabs events and handles drags and clicks in the usual way. */
class SelectorPoint : public ControlPoint {
public:
    SelectorPoint(SPDesktop *d, SPCanvasGroup *group, Selector *s) :
        ControlPoint(d, Geom::Point(0,0), SP_ANCHOR_CENTER,
                     CTRL_TYPE_INVISIPOINT,
                     invisible_cset, group),
        _selector(s),
        _cancel(false)
    {
        setVisible(false);
        _rubber = static_cast<CtrlRect*>(sp_canvas_item_new(_desktop->getControls(),
        SP_TYPE_CTRLRECT, NULL));
        sp_canvas_item_hide(_rubber);
    }

    ~SelectorPoint() {
        sp_canvas_item_destroy(_rubber);
    }

    SPDesktop *desktop() { return _desktop; }

    bool event(Inkscape::UI::Tools::ToolBase *ec, GdkEvent *e) {
        return _eventHandler(ec, e);
    }

protected:
    virtual bool _eventHandler(Inkscape::UI::Tools::ToolBase *event_context, GdkEvent *event) {
        if (event->type == GDK_KEY_PRESS && shortcut_key(event->key) == GDK_KEY_Escape &&
            sp_canvas_item_is_visible(_rubber))
        {
            _cancel = true;
            sp_canvas_item_hide(_rubber);
            return true;
        }
        return ControlPoint::_eventHandler(event_context, event);
    }

private:
    virtual bool grabbed(GdkEventMotion *) {
        _cancel = false;
        _start = position();
        sp_canvas_item_show(_rubber);
        return false;
    }

    virtual void dragged(Geom::Point &new_pos, GdkEventMotion *) {
        if (_cancel) return;
        Geom::Rect sel(_start, new_pos);
        _rubber->setRectangle(sel);
    }

    virtual void ungrabbed(GdkEventButton *event) {
        if (_cancel) return;
        sp_canvas_item_hide(_rubber);
        Geom::Rect sel(_start, position());
        _selector->signal_area.emit(sel, event);
    }

    virtual bool clicked(GdkEventButton *event) {
        if (event->button != 1) return false;
        _selector->signal_point.emit(position(), event);
        return true;
    }

    CtrlRect *_rubber;
    Selector *_selector;
    Geom::Point _start;
    bool _cancel;
};


Selector::Selector(SPDesktop *d)
    : Manipulator(d)
    , _dragger(new SelectorPoint(d, d->getControls(), this))
{
    _dragger->setVisible(false);
}

Selector::~Selector()
{
    delete _dragger;
}

bool Selector::event(Inkscape::UI::Tools::ToolBase *event_context, GdkEvent *event)
{
    // The hidden control point will capture all events after it obtains the grab,
    // but it relies on this function to initiate it. If we pass only first button
    // press events here, it won't interfere with any other event handling.
    switch (event->type) {
    case GDK_BUTTON_PRESS:
        // Do not pass button presses other than left button to the control point.
        // This way middle click and right click can be handled in ToolBase.
        if (event->button.button == 1 && !event_context->space_panning) {
            _dragger->setPosition(_desktop->w2d(event_point(event->motion)));
            return _dragger->event(event_context, event);
        }
        break;
    default: break;
    }
    return false;
}

} // namespace UI
} // namespace Inkscape

/*
  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:fileencoding=utf-8:textwidth=99 :