bc7301dc1cdeffa3d40d09a4d74a1c6f0b00780f
[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 "diamond.h"
22 #include "automation_line.h"
23 #include "ardour_ui.h"
24 #include "public_editor.h"
25
26 #include "i18n.h"
27
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
31 using namespace Gnome; // for Canvas
32
33 ControlPoint::ControlPoint (AutomationLine& al)
34         : _line (al)
35 {
36         _model = al.the_list()->end();
37         _view_index = 0;
38         _can_slide = true;
39         _x = 0;
40         _y = 0;
41         _shape = Full;
42         _size = 4.0;
43
44         _item = new Canvas::SimpleRect (_line.canvas_group());
45         _item->property_draw() = true;
46         _item->property_fill() = false;
47         _item->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ControlPointFill.get();
48         _item->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ControlPointOutline.get();
49         _item->property_outline_pixels() = 1;
50         _item->set_data ("control_point", this);
51         _item->signal_event().connect (sigc::mem_fun (this, &ControlPoint::event_handler));
52
53         hide ();
54         set_visible (false);
55 }
56
57 ControlPoint::ControlPoint (const ControlPoint& other, bool /*dummy_arg_to_force_special_copy_constructor*/)
58         : _line (other._line)
59 {
60         if (&other == this) {
61                 return;
62         }
63
64         _model = other._model;
65         _view_index = other._view_index;
66         _can_slide = other._can_slide;
67         _x = other._x;
68         _y = other._y;
69         _shape = other._shape;
70         _size = other._size;
71
72         _item = new Canvas::SimpleRect (_line.canvas_group());
73         _item->property_fill() = false;
74         _item->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_ControlPointOutline.get();
75         _item->property_outline_pixels() = 1;
76
77         /* NOTE: no event handling in copied ControlPoints */
78
79         hide ();
80         set_visible (false);
81 }
82
83 ControlPoint::~ControlPoint ()
84 {
85         delete _item;
86 }
87
88 bool
89 ControlPoint::event_handler (GdkEvent* event)
90 {
91         return PublicEditor::instance().canvas_control_point_event (event, _item, this);
92 }
93
94 void
95 ControlPoint::hide ()
96 {
97         _item->hide();
98 }
99
100 void
101 ControlPoint::show()
102 {
103         _item->show();
104 }
105
106 void
107 ControlPoint::set_visible (bool yn)
108 {
109         _item->property_draw() = (gboolean) yn;
110 }
111
112 bool
113 ControlPoint::visible () const
114 {
115         return _item->property_draw ();
116 }
117
118 void
119 ControlPoint::reset (double x, double y, AutomationList::iterator mi, uint32_t vi, ShapeType shape)
120 {
121         /* If this is too big, libart will confuse itself and segfault after it casts the bounding box
122            of this automation line to ints.  Sigh.
123         */
124
125         if (x > INT32_MAX) {
126                 x = INT32_MAX;
127         }
128
129         _model = mi;
130         _view_index = vi;
131         move_to (x, y, shape);
132 }
133
134 void
135 ControlPoint::set_color ()
136 {
137         uint32_t color = 0;
138
139         if (_selected) {
140                 color = ARDOUR_UI::config()->canvasvar_ControlPointSelected.get();
141         } else {
142                 color = ARDOUR_UI::config()->canvasvar_ControlPointOutline.get();
143         }
144
145         _item->property_outline_color_rgba() = color;
146         _item->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ControlPointFill.get();
147 }
148
149 void
150 ControlPoint::set_size (double sz)
151 {
152         _size = sz;
153         move_to (_x, _y, _shape);
154 }
155
156 void
157 ControlPoint::move_to (double x, double y, ShapeType shape)
158 {
159         double x1 = 0;
160         double x2 = 0;
161         double half_size = rint(_size/2.0);
162
163         switch (shape) {
164         case Full:
165                 x1 = x - half_size;
166                 x2 = x + half_size;
167                 break;
168         case Start:
169                 x1 = x;
170                 x2 = x + half_size;
171                 break;
172         case End:
173                 x1 = x - half_size;
174                 x2 = x;
175                 break;
176         }
177
178         _item->property_x1() = x1;
179         _item->property_x2() = x2;
180         _item->property_y1() = y - half_size;
181         _item->property_y2() = y + half_size;
182
183         _x = x;
184         _y = y;
185         _shape = shape;
186 }
187
188 void
189 ControlPoint::i2w (double& x, double& y) const
190 {
191         _item->i2w (x, y);
192 }