b581cb9a9da2312a91eb035c76bddb48a0f0c2a2
[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_height ((uint32_t)rint(trackview.current_height() - NAME_HIGHLIGHT_SIZE));
73 }
74
75 bool
76 AutomationRegionView::canvas_event(GdkEvent* ev)
77 {
78         if (ev->type == GDK_BUTTON_RELEASE) {
79
80                 const nframes_t when = trackview.editor.pixel_to_frame((nframes_t)ev->button.x)
81                         - _region->position();
82                 add_automation_event(ev, when, ev->button.y);
83         }
84
85         return false;
86 }
87
88 void
89 AutomationRegionView::add_automation_event (GdkEvent* event, nframes_t when, double y)
90 {
91         if (!_line) {
92                 boost::shared_ptr<Evoral::Control> c = _region->control(_parameter, true);
93                 boost::shared_ptr<ARDOUR::AutomationControl> ac
94                                 = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(c);
95                 assert(ac);
96                 create_line(ac->alist());
97         }
98         assert(_line);
99
100         double x = 0;
101         AutomationTimeAxisView* const view = automation_view();
102
103         view->canvas_display->w2i (x, y);
104
105         /* compute vertical fractional position */
106
107         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
108         y = 1.0 - (y / h);
109
110         /* map using line */
111
112         _line->view_to_model_y (y);
113
114         view->session().begin_reversible_command (_("add automation event"));
115         XMLNode& before = _line->the_list()->get_state();
116
117         _line->the_list()->add (when, y);
118
119         boost::shared_ptr<ARDOUR::MidiRegion> mr = boost::dynamic_pointer_cast<ARDOUR::MidiRegion>(_region);
120         if (mr) {
121                 cout << "ADD TO LIST: " << _line->the_list().get() << " ON " << _region
122                         << " (model " << mr->midi_source(0)->model() << ")" << endl;
123         }
124
125         XMLNode& after = _line->the_list()->get_state();
126         view->session().commit_reversible_command (new MementoCommand<ARDOUR::AutomationList>(
127                         *_line->the_list(), &before, &after));
128
129         view->session().set_dirty ();
130 }
131
132 void
133 AutomationRegionView::set_height (double h)
134 {
135         RegionView::set_height(h);
136
137         if (_line)
138                 _line->set_height ((uint32_t)rint(h - NAME_HIGHLIGHT_SIZE));
139 }
140
141 bool
142 AutomationRegionView::set_position (nframes_t pos, void* src, double* ignored)
143 {
144         // Do nothing, region parent will move us
145         //return false;
146         
147         return RegionView::set_position(pos, src, ignored); // FIXME: eventually...
148 }
149
150
151 void
152 AutomationRegionView::reset_width_dependent_items (double pixel_width)
153 {
154         RegionView::reset_width_dependent_items(pixel_width);
155         
156         if (_line)
157                 _line->reset();
158 }
159
160
161 void
162 AutomationRegionView::region_resized (ARDOUR::Change what_changed)
163 {
164         RegionView::region_resized(what_changed);
165
166         if (_line)
167                 _line->reset();
168 }
169
170
171 void
172 AutomationRegionView::entered()
173 {
174         if (_line)
175                 _line->track_entered();
176 }
177
178
179 void
180 AutomationRegionView::exited()
181 {
182         if (_line)
183                 _line->track_exited();
184 }
185