Deep "automation regions" support.
[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         , _line(list->parameter().to_string(), time_axis, *group, list)
34
35         _line.set_colors();
36         _line.show();
37         _line.show_all_control_points();
38         
39         //group->raise_to_top ();
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_y_position_and_height (0, trackview.height);
56
57         _region->StateChanged.connect (mem_fun(*this, &AutomationRegionView::region_changed));
58
59         set_colors ();
60
61         _enable_display = true;
62 }
63
64 bool
65 AutomationRegionView::canvas_event(GdkEvent* ev)
66 {
67         if (ev->type == GDK_BUTTON_RELEASE) {
68
69                 const nframes_t when = trackview.editor.pixel_to_frame((nframes_t)ev->button.x)
70                         - _region->position();
71                 add_automation_event(ev, when, ev->button.y);
72         }
73
74         return false;
75 }
76
77 void
78 AutomationRegionView::add_automation_event (GdkEvent* event, nframes_t when, double y)
79 {
80         double x = 0;
81         AutomationTimeAxisView* const view = automation_view();
82
83         view->canvas_display->w2i (x, y);
84
85         /* compute vertical fractional position */
86
87         const double height = trackview.height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 2;
88         y = 1.0 - (y / height);
89
90         /* map using line */
91
92         _line.view_to_model_y (y);
93
94         view->session().begin_reversible_command (_("add automation event"));
95         XMLNode& before = _line.the_list()->get_state();
96
97         _line.the_list()->add (when, y);
98
99         XMLNode& after = _line.the_list()->get_state();
100         view->session().commit_reversible_command (new MementoCommand<ARDOUR::AutomationList>(
101                         *_line.the_list(), &before, &after));
102
103         view->session().set_dirty ();
104 }
105
106
107 void
108 AutomationRegionView::set_y_position_and_height (double y, double h)
109 {
110         RegionView::set_y_position_and_height(y, h - 1);
111
112         _line.set_y_position_and_height ((uint32_t)y, (uint32_t) rint (h - NAME_HIGHLIGHT_SIZE));
113 }
114
115 bool
116 AutomationRegionView::set_position (nframes_t pos, void* src, double* ignored)
117 {
118         // Do nothing, region parent will move us
119         //return false;
120         
121         return RegionView::set_position(pos, src, ignored); // FIXME: eventually...
122 }
123
124
125 void
126 AutomationRegionView::reset_width_dependent_items (double pixel_width)
127 {
128         RegionView::reset_width_dependent_items(pixel_width);
129         
130         _line.reset();
131 }
132
133
134 void
135 AutomationRegionView::region_resized (ARDOUR::Change what_changed)
136 {
137         RegionView::region_resized(what_changed);
138
139         _line.reset();
140 }
141
142
143 void
144 AutomationRegionView::entered()
145 {
146         _line.track_entered();
147 }
148
149
150 void
151 AutomationRegionView::exited()
152 {
153         _line.track_exited();
154 }
155