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 }
55 void SelectableControlPoint::_connectHandlers()
56 {
57 signal_clicked.connect(
58 sigc::mem_fun(*this, &SelectableControlPoint::_clickedHandler));
59 signal_grabbed.connect(
60 sigc::bind_return(
61 sigc::mem_fun(*this, &SelectableControlPoint::_grabbedHandler),
62 false));
63 }
65 void SelectableControlPoint::_grabbedHandler(GdkEventMotion *event)
66 {
67 // if a point is dragged while not selected, it should select itself
68 if (!selected()) {
69 _takeSelection();
70 // HACK!!! invoke the last slot for signal_grabbed (it will be the callback registered
71 // by ControlPointSelection when adding to selection).
72 signal_grabbed.slots().back()(event);
73 }
74 }
75 bool SelectableControlPoint::_clickedHandler(GdkEventButton *event)
76 {
77 if (event->button != 1) return false;
78 if (held_shift(*event)) {
79 if (selected()) {
80 _selection.erase(this);
81 } else {
82 _selection.insert(this);
83 }
84 } else {
85 _takeSelection();
86 }
87 return true;
88 }
90 void SelectableControlPoint::_takeSelection()
91 {
92 _selection.clear();
93 _selection.insert(this);
94 }
96 bool SelectableControlPoint::selected() const
97 {
98 SelectableControlPoint *p = const_cast<SelectableControlPoint*>(this);
99 return _selection.find(p) != _selection.end();
100 }
102 void SelectableControlPoint::_setState(State state)
103 {
104 if (!selected()) {
105 ControlPoint::_setState(state);
106 return;
107 }
109 ColorSet *cset = reinterpret_cast<ColorSet*>(_cset);
110 ColorEntry current = {0, 0};
111 switch (state) {
112 case STATE_NORMAL:
113 current = cset->selected_normal; break;
114 case STATE_MOUSEOVER:
115 current = cset->selected_mouseover; break;
116 case STATE_CLICKED:
117 current = cset->selected_clicked; break;
118 }
119 _setColors(current);
120 _state = state;
121 }
123 } // namespace UI
124 } // namespace Inkscape
126 /*
127 Local Variables:
128 mode:c++
129 c-file-style:"stroustrup"
130 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
131 indent-tabs-mode:nil
132 fill-column:99
133 End:
134 */
135 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :