Code

9039bd53320f81c4a699673ac0c9533d94ba085e
[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_grabbed.connect(
60         sigc::bind_return(
61             sigc::hide(
62                 sigc::mem_fun(*this, &SelectableControlPoint::_grabbedHandler)),
63             false));
64     signal_clicked.connect(
65         sigc::mem_fun(*this, &SelectableControlPoint::_clickedHandler));
66 }
68 void SelectableControlPoint::_grabbedHandler()
69 {
70     // if a point is dragged while not selected, it should select itself
71     if (!selected()) {
72         _takeSelection();
73     }
74 }
76 bool SelectableControlPoint::_clickedHandler(GdkEventButton *event)
77 {
78     if (event->button != 1) return false;
79     if (held_shift(*event)) {
80         if (selected()) {
81             _selection.erase(this);
82         } else {
83             _selection.insert(this);
84         }
85     } else {
86         _takeSelection();
87     }
88     return true;
89 }
91 void SelectableControlPoint::_takeSelection()
92 {
93     _selection.clear();
94     _selection.insert(this);
95 }
97 bool SelectableControlPoint::selected() const
98 {
99     SelectableControlPoint *p = const_cast<SelectableControlPoint*>(this);
100     return _selection.find(p) != _selection.end();
103 void SelectableControlPoint::_setState(State state)
105     if (!selected()) {
106         ControlPoint::_setState(state);
107         return;
108     }
110     ColorSet *cset = reinterpret_cast<ColorSet*>(_cset);
111     ColorEntry current = {0, 0};
112     switch (state) {
113     case STATE_NORMAL:
114         current = cset->selected_normal; break;
115     case STATE_MOUSEOVER:
116         current = cset->selected_mouseover; break;
117     case STATE_CLICKED:
118         current = cset->selected_clicked; break;
119     }
120     _setColors(current);
121     _state = state;
124 } // namespace UI
125 } // namespace Inkscape
127 /*
128   Local Variables:
129   mode:c++
130   c-file-style:"stroustrup"
131   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
132   indent-tabs-mode:nil
133   fill-column:99
134   End:
135 */
136 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :