Factor out sequencing related things into an independant new library: "evoral".
[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                                            boost::shared_ptr<ARDOUR::AutomationList> list,
30                                            double                                    spu,
31                                            Gdk::Color&                               basic_color)
32         : RegionView(parent, time_axis, region, spu, basic_color)
33
34         if (list) {
35                 _line = boost::shared_ptr<AutomationLine>(new AutomationLine(
36                                         list->parameter().symbol(), time_axis, *group, list));
37                 _line->set_colors();
38                 _line->show();
39                 _line->show_all_control_points();
40         }
41         
42         group->signal_event().connect (mem_fun (this, &AutomationRegionView::canvas_event), false);
43 }
44
45 void
46 AutomationRegionView::init (Gdk::Color& basic_color, bool wfd)
47 {
48         _enable_display = false;
49         
50         RegionView::init(basic_color, false);
51
52         compute_colors (basic_color);
53
54         reset_width_dependent_items ((double) _region->length() / samples_per_unit);
55
56         set_height (trackview.current_height());
57
58         _region->StateChanged.connect (mem_fun(*this, &AutomationRegionView::region_changed));
59
60         set_colors ();
61
62         _enable_display = true;
63 }
64
65 bool
66 AutomationRegionView::canvas_event(GdkEvent* ev)
67 {
68         if (ev->type == GDK_BUTTON_RELEASE) {
69
70                 const nframes_t when = trackview.editor.pixel_to_frame((nframes_t)ev->button.x)
71                         - _region->position();
72                 add_automation_event(ev, when, ev->button.y);
73         }
74
75         return false;
76 }
77
78 void
79 AutomationRegionView::add_automation_event (GdkEvent* event, nframes_t when, double y)
80 {
81         if (!_line) {
82                 cerr << "ERROR: AutomationRegionView::add_automation_event called without line" << endl;
83                 return;
84         }
85
86         double x = 0;
87         AutomationTimeAxisView* const view = automation_view();
88
89         view->canvas_display->w2i (x, y);
90
91         /* compute vertical fractional position */
92
93         const double h = trackview.current_height() - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
94         y = 1.0 - (y / h);
95
96         /* map using line */
97
98         _line->view_to_model_y (y);
99
100         view->session().begin_reversible_command (_("add automation event"));
101         XMLNode& before = _line->the_list()->get_state();
102
103         _line->the_list()->add (when, y);
104
105         XMLNode& after = _line->the_list()->get_state();
106         view->session().commit_reversible_command (new MementoCommand<ARDOUR::AutomationList>(
107                         *_line->the_list(), &before, &after));
108
109         view->session().set_dirty ();
110 }
111
112
113 void
114 AutomationRegionView::set_y_position_and_height (double y, double h)
115 {
116         RegionView::set_y_position_and_height(y, h - 1);
117
118         if (_line)
119                 _line->set_y_position_and_height ((uint32_t)y, (uint32_t) rint (h - NAME_HIGHLIGHT_SIZE));
120 }
121
122 bool
123 AutomationRegionView::set_position (nframes_t pos, void* src, double* ignored)
124 {
125         // Do nothing, region parent will move us
126         //return false;
127         
128         return RegionView::set_position(pos, src, ignored); // FIXME: eventually...
129 }
130
131
132 void
133 AutomationRegionView::reset_width_dependent_items (double pixel_width)
134 {
135         RegionView::reset_width_dependent_items(pixel_width);
136         
137         if (_line)
138                 _line->reset();
139 }
140
141
142 void
143 AutomationRegionView::region_resized (ARDOUR::Change what_changed)
144 {
145         RegionView::region_resized(what_changed);
146
147         if (_line)
148                 _line->reset();
149 }
150
151
152 void
153 AutomationRegionView::entered()
154 {
155         if (_line)
156                 _line->track_entered();
157 }
158
159
160 void
161 AutomationRegionView::exited()
162 {
163         if (_line)
164                 _line->track_exited();
165 }
166