Merged with trunk
[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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <ardour/region.h>
24 #include <gtkmm2ext/doi.h>
25
26 #include "canvas-simplerect.h"
27 #include "canvas-curve.h"
28 #include "crossfade_view.h"
29 #include "rgb_macros.h"
30 #include "audio_time_axis.h"
31 #include "public_editor.h"
32 #include "regionview.h"
33 #include "utils.h"
34 #include "canvas_impl.h"
35
36 using namespace sigc;
37 using namespace ARDOUR;
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                               AudioTimeAxisView &tv, 
46                               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.overlap_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_height (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_height (double height)
109 {
110         if (height == TimeAxisView::Smaller ||
111                 height == TimeAxisView::Small)
112                 TimeAxisViewItem::set_height (height - 3 );
113         else
114                 TimeAxisViewItem::set_height (height - NAME_HIGHLIGHT_SIZE - 3 );
115
116         redraw_curves ();
117 }
118
119 void
120 CrossfadeView::crossfade_changed (Change what_changed)
121 {
122         bool need_redraw_curves = false;
123
124         if (what_changed & BoundsChanged) {
125                 set_position (crossfade.position(), this);
126                 set_duration (crossfade.overlap_length(), this);
127                 need_redraw_curves = true;
128         }
129         
130         if (what_changed & Crossfade::ActiveChanged) {
131                 /* calls redraw_curves */
132                 active_changed ();
133         } else if (need_redraw_curves) {
134                 redraw_curves ();
135         }
136 }
137
138 void
139 CrossfadeView::redraw_curves ()
140 {
141         Points* points; 
142         int32_t npoints;
143         float* vec;
144         
145         double h;
146
147         /*
148          At "height - 3.0" the bottom of the crossfade touches the name highlight or the bottom of the track (if the
149          track is either Small or Smaller.
150          */
151         switch(get_time_axis_view().height) {
152                 case TimeAxisView::Smaller:
153                 case TimeAxisView::Small:
154                         h = get_time_axis_view().height - 3.0;
155                         break;
156
157                 default:
158                         h = get_time_axis_view().height - NAME_HIGHLIGHT_SIZE - 3.0;
159         }
160
161         if (h < 0) {
162                 /* no space allocated yet */
163                 return;
164         }
165
166         npoints = get_time_axis_view().editor.frame_to_pixel (crossfade.length());
167         npoints = std::min (gdk_screen_width(), npoints);
168
169         if (!_visible || !crossfade.active() || npoints < 3) {
170                 fade_in->hide();
171                 fade_out->hide();
172                 return;
173         } else {
174                 fade_in->show();
175                 fade_out->show();
176         } 
177
178         points = get_canvas_points ("xfade edit redraw", npoints);
179         vec = new float[npoints];
180
181         crossfade.fade_in().get_vector (0, crossfade.length(), vec, npoints);
182         for (int i = 0, pci = 0; i < npoints; ++i) {
183                 Art::Point &p = (*points)[pci++];
184                 p.set_x(i);
185                 p.set_y(2.0 + h - (h * vec[i]));
186         }
187         fade_in->property_points() = *points;
188
189         crossfade.fade_out().get_vector (0, crossfade.length(), vec, npoints);
190         for (int i = 0, pci = 0; i < npoints; ++i) {
191                 Art::Point &p = (*points)[pci++];
192                 p.set_x(i);
193                 p.set_y(2.0 + h - (h * vec[i]));
194         }
195         fade_out->property_points() = *points;
196
197         delete [] vec;
198
199         delete points;
200
201         /* XXX this is ugly, but it will have to wait till Crossfades are reimplented
202            as regions. This puts crossfade views on top of a track, above all regions.
203         */
204
205         group->raise_to_top();
206 }
207
208 void
209 CrossfadeView::active_changed ()
210 {
211         if (crossfade.active()) {
212                 frame->property_fill_color_rgba() = color_map[cActiveCrossfade];
213         } else {
214                 frame->property_fill_color_rgba() = color_map[cInactiveCrossfade];
215         }
216
217         redraw_curves ();
218 }
219
220 void
221 CrossfadeView::set_valid (bool yn)
222 {
223         _valid = yn;
224 }
225
226 AudioRegionView&
227 CrossfadeView::upper_regionview () const
228 {
229         if (left_view.region.layer() > right_view.region.layer()) {
230                 return left_view;
231         } else {
232                 return right_view;
233         }
234 }
235
236 void
237 CrossfadeView::show ()
238 {
239         group->show();
240         _visible = true;
241 }
242
243 void
244 CrossfadeView::hide ()
245 {
246         group->hide();
247         _visible = false;
248 }
249
250 void
251 CrossfadeView::fake_hide ()
252 {
253         group->hide();
254 }