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