Fix parameter range stuff and automation time axis height (somewhat...).
[ardour.git] / gtk2_ardour / automation_region_view.cc
1 /*
2     Copyright (C) 2007 Paul Davis 
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <pbd/memento_command.h>
21 #include "automation_region_view.h"
22 #include "public_editor.h"
23
24 #include "i18n.h"
25
26 AutomationRegionView::AutomationRegionView(ArdourCanvas::Group*                      parent,
27                                            AutomationTimeAxisView&                   time_axis,
28                                            boost::shared_ptr<ARDOUR::Region>         region,
29                                            const ARDOUR::Parameter&                  param,
30                                            boost::shared_ptr<ARDOUR::AutomationList> list,
31                                            double                                    spu,
32                                            Gdk::Color&                               basic_color)
33         : RegionView(parent, time_axis, region, spu, basic_color)
34         , _parameter(param)
35
36         if (list) {
37                 assert(list->parameter() == param);
38                 create_line(list);
39         }
40         
41         group->signal_event().connect (mem_fun (this, &AutomationRegionView::canvas_event), false);
42 }
43
44 void
45 AutomationRegionView::init (Gdk::Color& basic_color, bool wfd)
46 {
47         _enable_display = false;
48         
49         RegionView::init(basic_color, false);
50
51         compute_colors (basic_color);
52
53         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
54
55         set_height (trackview.current_height());
56
57         _region->StateChanged.connect (mem_fun(*this, &AutomationRegionView::region_changed));
58
59         set_colors ();
60
61         _enable_display = true;
62 }
63
64 void
65 AutomationRegionView::create_line (boost::shared_ptr<ARDOUR::AutomationList> list)
66 {
67         _line = boost::shared_ptr<AutomationLine>(new AutomationLine(
68                                 list->parameter().symbol(), trackview, *get_canvas_group(), list));
69         _line->set_colors();
70         _line->show();
71         _line->show_all_control_points();
72         _line->set_y_position_and_height (trackview.y_position,
73                 (uint32_t)rint(trackview.current_height() - NAME_HIGHLIGHT_SIZE));
74 }
75
76 bool
77 AutomationRegionView::canvas_event(GdkEvent* ev)
78 {
79         if (ev->type == GDK_BUTTON_RELEASE) {
80
81                 const nframes_t when = trackview.editor.pixel_to_frame((nframes_t)ev->button.x)
82                         - _region->position();
83                 add_automation_event(ev, when, ev->button.y);
84         }
85
86         return false;
87 }
88
89 void
90 AutomationRegionView::add_automation_event (GdkEvent* event, nframes_t when, double y)
91 {
92         if (!_line) {
93                 boost::shared_ptr<Evoral::Control> c = _region->control(_parameter, true);
94                 boost::shared_ptr<ARDOUR::AutomationControl> ac
95                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(c);
96                 assert(ac);
97                 create_line(ac->alist());
98         }
99         assert(_line);
100
101         double x = 0;
102         AutomationTimeAxisView* const view = automation_view();
103
104         view->canvas_display->w2i (x, y);
105
106         /* compute vertical fractional position */
107
108         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
109         y = 1.0 - (y / h);
110
111         /* map using line */
112
113         _line->view_to_model_y (y);
114
115         view->session().begin_reversible_command (_("add automation event"));
116         XMLNode& before = _line->the_list()->get_state();
117
118         _line->the_list()->add (when, y);
119
120         XMLNode& after = _line->the_list()->get_state();
121         view->session().commit_reversible_command (new MementoCommand<ARDOUR::AutomationList>(
122                         *_line->the_list(), &before, &after));
123
124         view->session().set_dirty ();
125 }
126
127 void
128 AutomationRegionView::set_y_position_and_height (double y, double h)
129 {
130         cout << "ARV SET Y POSITION AND HEIGHT: " << y << ", " << h << endl;
131         RegionView::set_y_position_and_height(y, h - 1);
132
133         if (_line)
134                 _line->set_y_position_and_height (y, h - NAME_HIGHLIGHT_SIZE);
135 }
136
137 void
138 AutomationRegionView::set_height (double h)
139 {
140         cout << "ARV SET HEIGHT: " << h << endl;
141         RegionView::set_height(h);
142         if (_line)
143                 _line->set_y_position_and_height (trackview.y_position - h,
144                                 (uint32_t)rint(h - NAME_HIGHLIGHT_SIZE));
145 }
146
147 bool
148 AutomationRegionView::set_position (nframes_t pos, void* src, double* ignored)
149 {
150         // Do nothing, region parent will move us
151         //return false;
152         
153         return RegionView::set_position(pos, src, ignored); // FIXME: eventually...
154 }
155
156
157 void
158 AutomationRegionView::reset_width_dependent_items (double pixel_width)
159 {
160         RegionView::reset_width_dependent_items(pixel_width);
161         
162         if (_line)
163                 _line->reset();
164 }
165
166
167 void
168 AutomationRegionView::region_resized (ARDOUR::Change what_changed)
169 {
170         RegionView::region_resized(what_changed);
171
172         if (_line)
173                 _line->reset();
174 }
175
176
177 void
178 AutomationRegionView::entered()
179 {
180         if (_line)
181                 _line->track_entered();
182 }
183
184
185 void
186 AutomationRegionView::exited()
187 {
188         if (_line)
189                 _line->track_exited();
190 }
191