summaryrefslogtreecommitdiffstats
path: root/src/ui/tool/selectable-control-point.cpp
blob: df2410dc29c5831cb719de33bb88342d3b0907c9 (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
/** @file
 * Desktop-bound selectable control object - implementation
 */
/* Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *
 * Copyright (C) 2009 Authors
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include "ui/tool/control-point-selection.h"
#include "ui/tool/event-utils.h"
#include "ui/tool/selectable-control-point.h"

namespace Inkscape {
namespace UI {

static SelectableControlPoint::ColorSet default_scp_color_set = {
    {
        {0xffffff00, 0x01000000}, // normal fill, stroke
        {0xff0000ff, 0x01000000}, // mouseover fill, stroke
        {0x0000ffff, 0x01000000}  // clicked fill, stroke
    },
    {0x0000ffff, 0x000000ff}, // normal fill, stroke when selected
    {0xff000000, 0x000000ff}, // mouseover fill, stroke when selected
    {0xff000000, 0x000000ff}  // clicked fill, stroke when selected
};

SelectableControlPoint::SelectableControlPoint(SPDesktop *d, Geom::Point const &initial_pos,
        Gtk::AnchorType anchor, SPCtrlShapeType shape, unsigned int size,
        ControlPointSelection &sel, ColorSet *cset, SPCanvasGroup *group)
    : ControlPoint (d, initial_pos, anchor, shape, size,
        cset ? reinterpret_cast<ControlPoint::ColorSet*>(cset)
        : reinterpret_cast<ControlPoint::ColorSet*>(&default_scp_color_set), group)
    , _selection (sel)
{
    _connectHandlers();
}
SelectableControlPoint::SelectableControlPoint(SPDesktop *d, Geom::Point const &initial_pos,
        Gtk::AnchorType anchor, Glib::RefPtr<Gdk::Pixbuf> pixbuf,
        ControlPointSelection &sel, ColorSet *cset, SPCanvasGroup *group)
    : ControlPoint (d, initial_pos, anchor, pixbuf,
        cset ? reinterpret_cast<ControlPoint::ColorSet*>(cset)
        : reinterpret_cast<ControlPoint::ColorSet*>(&default_scp_color_set), group)
    , _selection (sel)
{
    _connectHandlers();
}

SelectableControlPoint::~SelectableControlPoint()
{
    _selection.erase(this);
    _selection.allPoints().erase(this);
}

void SelectableControlPoint::_connectHandlers()
{
    _selection.allPoints().insert(this);
    signal_grabbed.connect(
        sigc::bind_return(
            sigc::hide(
                sigc::mem_fun(*this, &SelectableControlPoint::_grabbedHandler)),
            false));
    signal_dragged.connect(
        sigc::mem_fun(*this, &SelectableControlPoint::_draggedHandler));
    signal_ungrabbed.connect(
        sigc::hide(
            sigc::mem_fun(*this, &SelectableControlPoint::_ungrabbedHandler)));
    signal_clicked.connect(
        sigc::mem_fun(*this, &SelectableControlPoint::_clickedHandler));
}

void SelectableControlPoint::_grabbedHandler()
{
    // if a point is dragged while not selected, it should select itself
    if (!selected()) {
        _takeSelection();
        _selection._pointGrabbed();
    }
}
void SelectableControlPoint::_draggedHandler(Geom::Point const &old_pos, Geom::Point &new_pos, GdkEventMotion *event)
{
    _selection._pointDragged(old_pos, new_pos, event);
}
void SelectableControlPoint::_ungrabbedHandler()
{
    _selection._pointUngrabbed();
}
bool SelectableControlPoint::_clickedHandler(GdkEventButton *event)
{
    if (selected() && _selection._pointClicked(this, event)) return true;
    if (event->button != 1) return false;
    if (held_shift(*event)) {
        if (selected()) {
            _selection.erase(this);
        } else {
            _selection.insert(this);
        }
    } else {
        _takeSelection();
    }
    return true;
}

void SelectableControlPoint::_takeSelection()
{
    _selection.clear();
    _selection.insert(this);
}

bool SelectableControlPoint::selected() const
{
    SelectableControlPoint *p = const_cast<SelectableControlPoint*>(this);
    return _selection.find(p) != _selection.end();
}

void SelectableControlPoint::_setState(State state)
{
    if (!selected()) {
        ControlPoint::_setState(state);
        return;
    }

    ColorSet *cset = reinterpret_cast<ColorSet*>(_cset);
    ColorEntry current = {0, 0};
    switch (state) {
    case STATE_NORMAL:
        current = cset->selected_normal; break;
    case STATE_MOUSEOVER:
        current = cset->selected_mouseover; break;
    case STATE_CLICKED:
        current = cset->selected_clicked; break;
    }
    _setColors(current);
    _state = state;
}

} // 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:encoding=utf-8:textwidth=99 :