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()->get_canvasvar_ControlPointFill());
50         _item->set_outline_color (ARDOUR_UI::config()->get_canvasvar_ControlPointOutline());
51         _item->set_data ("control_point", this);
52         _item->Event.connect (sigc::mem_fun (this, &ControlPoint::event_handler));
53
54         hide ();
55         set_visible (false);
56 }
57
58 ControlPoint::ControlPoint (const ControlPoint& other, bool /*dummy_arg_to_force_special_copy_constructor*/)
59         : _line (other._line)
60 {
61         if (&other == this) {
62                 return;
63         }
64
65         _model = other._model;
66         _view_index = other._view_index;
67         _can_slide = other._can_slide;
68         _x = other._x;
69         _y = other._y;
70         _shape = other._shape;
71         _size = other._size;
72
73         _item = new ArdourCanvas::Rectangle (&_line.canvas_group());
74         _item->set_fill (false);
75         _item->set_outline_color (ARDOUR_UI::config()->get_canvasvar_ControlPointOutline());
76
77         /* NOTE: no event handling in copied ControlPoints */
78
79         hide ();
80         set_visible (false);
81 }
82
83 ControlPoint::~ControlPoint ()
84 {
85         CatchDeletion (this); /* EMIT SIGNAL */
86         
87         delete _item;
88 }
89
90 bool
91 ControlPoint::event_handler (GdkEvent* event)
92 {
93         return PublicEditor::instance().canvas_control_point_event (event, _item, this);
94 }
95
96 void
97 ControlPoint::hide ()
98 {
99         _item->hide();
100 }
101
102 void
103 ControlPoint::show()
104 {
105         _item->show();
106 }
107
108 void
109 ControlPoint::set_visible (bool yn)
110 {
111         _item->property_draw() = (gboolean) yn;
112 }
113
114 bool
115 ControlPoint::visible () const
116 {
117         return _item->property_draw ();
118 }
119
120 void
121 ControlPoint::reset (double x, double y, AutomationList::iterator mi, uint32_t vi, ShapeType shape)
122 {
123         _model = mi;
124         _view_index = vi;
125         move_to (x, y, shape);
126 }
127
128 void
129 ControlPoint::set_color ()
130 {
131         uint32_t color = 0;
132
133         if (_selected) {
134                 color = ARDOUR_UI::config()->get_canvasvar_ControlPointSelected();
135         } else {
136                 color = ARDOUR_UI::config()->get_canvasvar_ControlPointOutline();
137         }
138
139         _item->set_outline_color (color);
140         _item->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ControlPointFill());
141 }
142
143 void
144 ControlPoint::set_size (double sz)
145 {
146         _size = sz;
147         move_to (_x, _y, _shape);
148 }
149
150 void
151 ControlPoint::move_to (double x, double y, ShapeType shape)
152 {
153         double x1 = 0;
154         double x2 = 0;
155         double half_size = rint(_size/2.0);
156
157         switch (shape) {
158         case Full:
159                 x1 = x - half_size;
160                 x2 = x + half_size;
161                 break;
162         case Start:
163                 x1 = x;
164                 x2 = x + half_size;
165                 break;
166         case End:
167                 x1 = x - half_size;
168                 x2 = x;
169                 break;
170         }
171
172         _item->set (ArdourCanvas::Rect (x1, y - half_size, x2, y + half_size));
173
174         _x = x;
175         _y = y;
176         _shape = shape;
177 }
178
179 void
180 ControlPoint::i2w (double& x, double& y) const
181 {
182         _item->item_to_canvas (x, y);
183 }