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