261b2b3ca4070d67c8009fe82a41aa0cb8b1a7d8
[ardour.git] / gtk2_ardour / crossfade_view.cc
1 /*
2     Copyright (C) 2003 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 <algorithm>
21
22 #include <ardour/region.h>
23 #include <gtkmm2ext/doi.h>
24
25 #include "canvas-simplerect.h"
26 #include "canvas-curve.h"
27 #include "crossfade_view.h"
28 #include "rgb_macros.h"
29 #include "audio_time_axis.h"
30 #include "public_editor.h"
31 #include "audio_region_view.h"
32 #include "utils.h"
33 #include "canvas_impl.h"
34
35 using namespace sigc;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace Editing;
39 using namespace Gnome;
40 using namespace Canvas;
41
42 sigc::signal<void,CrossfadeView*> CrossfadeView::GoingAway;
43
44 CrossfadeView::CrossfadeView (ArdourCanvas::Group *parent, 
45                               RouteTimeAxisView &tv, 
46                               boost::shared_ptr<Crossfade> xf, 
47                               double spu,
48                               Gdk::Color& basic_color,
49                               AudioRegionView& lview,
50                               AudioRegionView& rview)
51                               
52
53         : TimeAxisViewItem ("xfade" /*xf.name()*/, *parent, tv, spu, basic_color, xf->position(), 
54                             xf->length(), TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowFrame)),
55           crossfade (xf),
56           left_view (lview),
57           right_view (rview)
58         
59 {
60         _valid = true;
61         _visible = true;
62
63         fade_in = new Line (*group);
64         fade_in->property_fill_color_rgba() = color_map[cCrossfadeLine];
65         fade_in->property_width_pixels() = 1;
66
67         fade_out = new Line (*group);
68         fade_out->property_fill_color_rgba() = color_map[cCrossfadeLine];
69         fade_out->property_width_pixels() = 1;
70         
71         set_y_position_and_height (0, get_time_axis_view().height);
72
73         /* no frame around the xfade or overlap rects */
74
75         frame->property_outline_what() = 0;
76
77         /* never show the vestigial frame */
78
79         vestigial_frame->hide();
80         show_vestigial = false;
81         
82         group->signal_event().connect (bind (mem_fun (tv.editor, &PublicEditor::canvas_crossfade_view_event), group, this));
83         
84         crossfade_changed (Change (~0));
85
86         crossfade->StateChanged.connect (mem_fun(*this, &CrossfadeView::crossfade_changed));
87 }
88
89 CrossfadeView::~CrossfadeView ()
90 {
91          GoingAway (this) ; /* EMIT_SIGNAL */
92 }
93
94 void
95 CrossfadeView::reset_width_dependent_items (double pixel_width)
96 {
97         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
98
99         active_changed ();
100
101         if (pixel_width < 5) {
102                 fade_in->hide();
103                 fade_out->hide();
104         }
105 }
106
107 void
108 CrossfadeView::set_y_position_and_height (double y, double h)
109 {
110         if (h == TimeAxisView::hSmaller || h == TimeAxisView::hSmall) {
111                 TimeAxisViewItem::set_y_position_and_height (y, h - 3 );
112         } else {
113                 TimeAxisViewItem::set_y_position_and_height (y, h - NAME_HIGHLIGHT_SIZE - 3 );
114         }
115
116         _y_position = y;
117         _height = h;
118
119         redraw_curves ();
120 }
121
122 void
123 CrossfadeView::crossfade_changed (Change what_changed)
124 {
125         bool need_redraw_curves = false;
126
127         if (what_changed & BoundsChanged) {
128                 set_position (crossfade->position(), this);
129                 set_duration (crossfade->length(), this);
130                 need_redraw_curves = true;
131         }
132
133         if (what_changed & Crossfade::FollowOverlapChanged) {
134                 need_redraw_curves = true;
135         }
136         
137         if (what_changed & Crossfade::ActiveChanged) {
138                 /* calls redraw_curves */
139                 active_changed ();
140         } else if (need_redraw_curves) {
141                 redraw_curves ();
142         }
143 }
144
145 void
146 CrossfadeView::redraw_curves ()
147 {
148         Points* points; 
149         int32_t npoints;
150         float* vec;
151         double h;
152
153         if (!crossfade->following_overlap()) {
154                 /* curves should not be visible */
155                 fade_in->hide ();
156                 fade_out->hide ();
157                 return;
158         }
159
160         /*
161          At "height - 3.0" the bottom of the crossfade touches the name highlight or the bottom of the track (if the
162          track is either Small or Smaller.
163          */
164         double const tav_height = get_time_axis_view().height;
165         if (tav_height == TimeAxisView::hSmaller || tav_height == TimeAxisView::hSmall) {
166                 h = tav_height - 3.0;
167         } else {
168                 h = tav_height - NAME_HIGHLIGHT_SIZE - 3.0;
169         }
170
171         if (h < 0) {
172                 /* no space allocated yet */
173                 return;
174         }
175
176         npoints = get_time_axis_view().editor.frame_to_pixel (crossfade->length());
177         npoints = std::min (gdk_screen_width(), npoints);
178
179         if (!_visible || !crossfade->active() || npoints < 3) {
180                 fade_in->hide();
181                 fade_out->hide();
182                 return;
183         } else {
184                 fade_in->show();
185                 fade_out->show();
186         } 
187
188         points = get_canvas_points ("xfade edit redraw", npoints);
189         vec = new float[npoints];
190
191         crossfade->fade_in().get_vector (0, crossfade->length(), vec, npoints);
192         for (int i = 0, pci = 0; i < npoints; ++i) {
193                 Art::Point &p = (*points)[pci++];
194                 p.set_x(i);
195                 p.set_y(_y_position + 2.0 + h - (h * vec[i]));
196         }
197         fade_in->property_points() = *points;
198
199         crossfade->fade_out().get_vector (0, crossfade->length(), vec, npoints);
200         for (int i = 0, pci = 0; i < npoints; ++i) {
201                 Art::Point &p = (*points)[pci++];
202                 p.set_x(i);
203                 p.set_y(_y_position + 2.0 + h - (h * vec[i]));
204         }
205         fade_out->property_points() = *points;
206
207         delete [] vec;
208
209         delete points;
210
211         /* XXX this is ugly, but it will have to wait till Crossfades are reimplented
212            as regions. This puts crossfade views on top of a track, above all regions.
213         */
214
215         group->raise_to_top();
216 }
217
218 void
219 CrossfadeView::active_changed ()
220 {
221         if (crossfade->active()) {
222                 frame->property_fill_color_rgba() = color_map[cActiveCrossfade];
223         } else {
224                 frame->property_fill_color_rgba() = color_map[cInactiveCrossfade];
225         }
226
227         redraw_curves ();
228 }
229
230 void
231 CrossfadeView::set_valid (bool yn)
232 {
233         _valid = yn;
234 }
235
236 AudioRegionView&
237 CrossfadeView::upper_regionview () const
238 {
239         if (left_view.region()->layer() > right_view.region()->layer()) {
240                 return left_view;
241         } else {
242                 return right_view;
243         }
244 }
245
246 void
247 CrossfadeView::show ()
248 {
249         group->show();
250         _visible = true;
251 }
252
253 void
254 CrossfadeView::hide ()
255 {
256         group->hide();
257         _visible = false;
258 }
259
260 void
261 CrossfadeView::fake_hide ()
262 {
263         group->hide();
264 }