Merge branch 'master' into cairocanvas
[ardour.git] / gtk2_ardour / control_point.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "control_point.h"
21 #include "automation_line.h"
22 #include "ardour_ui.h"
23 #include "public_editor.h"
24
25 #include "canvas/rectangle.h"
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 PBD::Signal1<void, ControlPoint *> ControlPoint::CatchDeletion;
34
35 ControlPoint::ControlPoint (AutomationLine& al)
36         : _line (al)
37 {
38         _model = al.the_list()->end();
39         _view_index = 0;
40         _can_slide = true;
41         _x = 0;
42         _y = 0;
43         _shape = Full;
44         _size = 4.0;
45
46         _item = new ArdourCanvas::Rectangle (&_line.canvas_group());
47         _item->property_draw() = true;
48         _item->set_fill (false);
49         _item->set_fill_color (ARDOUR_UI::config()->canvasvar_ControlPointFill.get());
50         _item->set_outline_color (ARDOUR_UI::config()->canvasvar_ControlPointOutline.get());
51         _item->set_outline_width (1);
52         _item->set_data ("control_point", this);
53         _item->Event.connect (sigc::mem_fun (this, &ControlPoint::event_handler));
54
55         hide ();
56         set_visible (false);
57 }
58
59 ControlPoint::ControlPoint (const ControlPoint& other, bool /*dummy_arg_to_force_special_copy_constructor*/)
60         : _line (other._line)
61 {
62         if (&other == this) {
63                 return;
64         }
65
66         _model = other._model;
67         _view_index = other._view_index;
68         _can_slide = other._can_slide;
69         _x = other._x;
70         _y = other._y;
71         _shape = other._shape;
72         _size = other._size;
73
74         _item = new ArdourCanvas::Rectangle (&_line.canvas_group());
75         _item->set_fill (false);
76         _item->set_outline_color (ARDOUR_UI::config()->canvasvar_ControlPointOutline.get());
77         _item->set_outline_width (1);
78
79         /* NOTE: no event handling in copied ControlPoints */
80
81         hide ();
82         set_visible (false);
83 }
84
85 ControlPoint::~ControlPoint ()
86 {
87         CatchDeletion (this); /* EMIT SIGNAL */
88         
89         delete _item;
90 }
91
92 bool
93 ControlPoint::event_handler (GdkEvent* event)
94 {
95         return PublicEditor::instance().canvas_control_point_event (event, _item, this);
96 }
97
98 void
99 ControlPoint::hide ()
100 {
101         _item->hide();
102 }
103
104 void
105 ControlPoint::show()
106 {
107         _item->show();
108 }
109
110 void
111 ControlPoint::set_visible (bool yn)
112 {
113         _item->property_draw() = (gboolean) yn;
114 }
115
116 bool
117 ControlPoint::visible () const
118 {
119         return _item->property_draw ();
120 }
121
122 void
123 ControlPoint::reset (double x, double y, AutomationList::iterator mi, uint32_t vi, ShapeType shape)
124 {
125         /* If this is too big, libart will confuse itself and segfault after it casts the bounding box
126            of this automation line to ints.  Sigh.
127         */
128
129         if (x > INT32_MAX) {
130                 x = INT32_MAX;
131         }
132
133         _model = mi;
134         _view_index = vi;
135         move_to (x, y, shape);
136 }
137
138 void
139 ControlPoint::set_color ()
140 {
141         uint32_t color = 0;
142
143         if (_selected) {
144                 color = ARDOUR_UI::config()->canvasvar_ControlPointSelected.get();
145         } else {
146                 color = ARDOUR_UI::config()->canvasvar_ControlPointOutline.get();
147         }
148
149         _item->set_outline_color (color);
150         _item->set_fill_color (ARDOUR_UI::config()->canvasvar_ControlPointFill.get());
151 }
152
153 void
154 ControlPoint::set_size (double sz)
155 {
156         _size = sz;
157         move_to (_x, _y, _shape);
158 }
159
160 void
161 ControlPoint::move_to (double x, double y, ShapeType shape)
162 {
163         double x1 = 0;
164         double x2 = 0;
165         double half_size = rint(_size/2.0);
166
167         switch (shape) {
168         case Full:
169                 x1 = x - half_size;
170                 x2 = x + half_size;
171                 break;
172         case Start:
173                 x1 = x;
174                 x2 = x + half_size;
175                 break;
176         case End:
177                 x1 = x - half_size;
178                 x2 = x;
179                 break;
180         }
181
182         _item->set (ArdourCanvas::Rect (x1, y - half_size, x2, y + half_size));
183
184         _x = x;
185         _y = y;
186         _shape = shape;
187 }
188
189 void
190 ControlPoint::i2w (double& x, double& y) const
191 {
192         _item->item_to_canvas (x, y);
193 }