Code

5b9aa4fc838207a1b42753503b4f5c3abdeae4d4
[inkscape.git] / src / ui / tool / selectable-control-point.cpp
1 /** @file
2  * Desktop-bound selectable control object - implementation
3  */
4 /* Authors:
5  *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
6  *
7  * Copyright (C) 2009 Authors
8  * Released under GNU GPL, read the file 'COPYING' for more information
9  */
11 #include "ui/tool/control-point-selection.h"
12 #include "ui/tool/event-utils.h"
13 #include "ui/tool/selectable-control-point.h"
15 namespace Inkscape {
16 namespace UI {
18 static SelectableControlPoint::ColorSet default_scp_color_set = {
19     {
20         {0xffffff00, 0x01000000}, // normal fill, stroke
21         {0xff0000ff, 0x01000000}, // mouseover fill, stroke
22         {0x0000ffff, 0x01000000}  // clicked fill, stroke
23     },
24     {0x0000ffff, 0x000000ff}, // normal fill, stroke when selected
25     {0xff000000, 0x000000ff}, // mouseover fill, stroke when selected
26     {0xff000000, 0x000000ff}  // clicked fill, stroke when selected
27 };
29 SelectableControlPoint::SelectableControlPoint(SPDesktop *d, Geom::Point const &initial_pos,
30         Gtk::AnchorType anchor, SPCtrlShapeType shape, unsigned int size,
31         ControlPointSelection &sel, ColorSet *cset, SPCanvasGroup *group)
32     : ControlPoint (d, initial_pos, anchor, shape, size,
33         cset ? reinterpret_cast<ControlPoint::ColorSet*>(cset)
34         : reinterpret_cast<ControlPoint::ColorSet*>(&default_scp_color_set), group)
35     , _selection (sel)
36 {
37     _connectHandlers();
38 }
39 SelectableControlPoint::SelectableControlPoint(SPDesktop *d, Geom::Point const &initial_pos,
40         Gtk::AnchorType anchor, Glib::RefPtr<Gdk::Pixbuf> pixbuf,
41         ControlPointSelection &sel, ColorSet *cset, SPCanvasGroup *group)
42     : ControlPoint (d, initial_pos, anchor, pixbuf,
43         cset ? reinterpret_cast<ControlPoint::ColorSet*>(cset)
44         : reinterpret_cast<ControlPoint::ColorSet*>(&default_scp_color_set), group)
45     , _selection (sel)
46 {
47     _connectHandlers();
48 }
50 SelectableControlPoint::~SelectableControlPoint()
51 {
52     _selection.erase(this);
53     _selection.allPoints().erase(this);
54 }
56 void SelectableControlPoint::_connectHandlers()
57 {
58     _selection.allPoints().insert(this);
59     signal_clicked.connect(
60         sigc::mem_fun(*this, &SelectableControlPoint::_clickedHandler));
61     signal_grabbed.connect(
62         sigc::bind_return(
63             sigc::mem_fun(*this, &SelectableControlPoint::_grabbedHandler),
64             false));
65 }
67 void SelectableControlPoint::_grabbedHandler(GdkEventMotion *event)
68 {
69     // if a point is dragged while not selected, it should select itself
70     if (!selected()) {
71         _takeSelection();
72         // HACK!!! invoke the last slot for signal_grabbed (it will be the callback registered
73         // by ControlPointSelection when adding to selection).
74         signal_grabbed.slots().back()(event);
75     }
76 }
77 bool SelectableControlPoint::_clickedHandler(GdkEventButton *event)
78 {
79     if (event->button != 1) return false;
80     if (held_shift(*event)) {
81         if (selected()) {
82             _selection.erase(this);
83         } else {
84             _selection.insert(this);
85         }
86     } else {
87         _takeSelection();
88     }
89     return true;
90 }
92 void SelectableControlPoint::_takeSelection()
93 {
94     _selection.clear();
95     _selection.insert(this);
96 }
98 bool SelectableControlPoint::selected() const
99 {
100     SelectableControlPoint *p = const_cast<SelectableControlPoint*>(this);
101     return _selection.find(p) != _selection.end();
104 void SelectableControlPoint::_setState(State state)
106     if (!selected()) {
107         ControlPoint::_setState(state);
108         return;
109     }
111     ColorSet *cset = reinterpret_cast<ColorSet*>(_cset);
112     ColorEntry current = {0, 0};
113     switch (state) {
114     case STATE_NORMAL:
115         current = cset->selected_normal; break;
116     case STATE_MOUSEOVER:
117         current = cset->selected_mouseover; break;
118     case STATE_CLICKED:
119         current = cset->selected_clicked; break;
120     }
121     _setColors(current);
122     _state = state;
125 } // namespace UI
126 } // namespace Inkscape
128 /*
129   Local Variables:
130   mode:c++
131   c-file-style:"stroustrup"
132   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
133   indent-tabs-mode:nil
134   fill-column:99
135   End:
136 */
137 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :