Code

* Add "show transform handles" toggle button.
[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_dragged.connect(
65         sigc::mem_fun(*this, &SelectableControlPoint::_draggedHandler));
66     signal_ungrabbed.connect(
67         sigc::hide(
68             sigc::mem_fun(*this, &SelectableControlPoint::_ungrabbedHandler)));
69     signal_clicked.connect(
70         sigc::mem_fun(*this, &SelectableControlPoint::_clickedHandler));
71 }
73 void SelectableControlPoint::_grabbedHandler()
74 {
75     // if a point is dragged while not selected, it should select itself
76     if (!selected()) {
77         _takeSelection();
78         _selection._pointGrabbed();
79     }
80 }
81 void SelectableControlPoint::_draggedHandler(Geom::Point const &old_pos, Geom::Point &new_pos, GdkEventMotion *event)
82 {
83     _selection._pointDragged(old_pos, new_pos, event);
84 }
85 void SelectableControlPoint::_ungrabbedHandler()
86 {
87     _selection._pointUngrabbed();
88 }
89 bool SelectableControlPoint::_clickedHandler(GdkEventButton *event)
90 {
91     if (selected() && _selection._pointClicked(this, event)) return true;
92     if (event->button != 1) return false;
93     if (held_shift(*event)) {
94         if (selected()) {
95             _selection.erase(this);
96         } else {
97             _selection.insert(this);
98         }
99     } else {
100         _takeSelection();
101     }
102     return true;
105 void SelectableControlPoint::_takeSelection()
107     _selection.clear();
108     _selection.insert(this);
111 bool SelectableControlPoint::selected() const
113     SelectableControlPoint *p = const_cast<SelectableControlPoint*>(this);
114     return _selection.find(p) != _selection.end();
117 void SelectableControlPoint::_setState(State state)
119     if (!selected()) {
120         ControlPoint::_setState(state);
121         return;
122     }
124     ColorSet *cset = reinterpret_cast<ColorSet*>(_cset);
125     ColorEntry current = {0, 0};
126     switch (state) {
127     case STATE_NORMAL:
128         current = cset->selected_normal; break;
129     case STATE_MOUSEOVER:
130         current = cset->selected_mouseover; break;
131     case STATE_CLICKED:
132         current = cset->selected_clicked; break;
133     }
134     _setColors(current);
135     _state = state;
138 } // namespace UI
139 } // namespace Inkscape
141 /*
142   Local Variables:
143   mode:c++
144   c-file-style:"stroustrup"
145   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
146   indent-tabs-mode:nil
147   fill-column:99
148   End:
149 */
150 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :