cca92198f5e7ecc408a562878ccdef03bb95b8ca
[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
39 sigc::signal<void,CrossfadeView*> CrossfadeView::GoingAway;
40
41 CrossfadeView::CrossfadeView (Gnome::Canvas::Group *parent, 
42                               AudioTimeAxisView &tv, 
43                               Crossfade& xf, 
44                               double spu,
45                               Gdk::Color& basic_color,
46                               AudioRegionView& lview,
47                               AudioRegionView& rview)
48                               
49
50         : TimeAxisViewItem ("xf.name()", *parent, tv, spu, basic_color, xf.position(), 
51                             xf.overlap_length(), TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowFrame)),
52           crossfade (xf),
53           left_view (lview),
54           right_view (rview)
55         
56 {
57         _valid = true;
58         _visible = true;
59
60         fade_in = gnome_canvas_item_new (GNOME_CANVAS_GROUP(group),
61                                        gnome_canvas_line_get_type(),
62                                        "fill_color_rgba", color_map[cCrossfadeLine],
63                                         "width_pixels", (guint) 1,
64                                        NULL);
65
66         fade_out = gnome_canvas_item_new (GNOME_CANVAS_GROUP(group),
67                                         gnome_canvas_line_get_type(),
68                                         "fill_color_rgba", color_map[cCrossfadeLine],
69                                         "width_pixels", (guint) 1,
70                                         NULL);
71         
72         set_height (get_time_axis_view().height);
73
74         /* no frame around the xfade or overlap rects */
75
76         frame->set_property ("outline_what", 0);
77
78         /* never show the vestigial frame */
79
80         vestigial_frame->hide();
81         show_vestigial = false;
82
83         // GTK2FIX
84         // group->signal_event.connect (bind (mem_fun (tv.editor, &PublicEditor::canvas_crossfade_view_event), group, this));
85         
86         crossfade_changed (Change (~0));
87
88         crossfade.StateChanged.connect (mem_fun(*this, &CrossfadeView::crossfade_changed));
89 }
90
91 CrossfadeView::~CrossfadeView ()
92 {
93          GoingAway (this) ; /* EMIT_SIGNAL */
94 }
95
96 std::string
97 CrossfadeView::get_item_name ()
98 {
99         return "xfade";
100 //      return crossfade.name();
101 }
102
103 void
104 CrossfadeView::reset_width_dependent_items (double pixel_width)
105 {
106         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
107
108         active_changed ();
109
110         if (pixel_width < 5) {
111                 gnome_canvas_item_hide (fade_in);
112                 gnome_canvas_item_hide (fade_out);
113         }
114 }
115
116 void
117 CrossfadeView::set_height (double height)
118 {
119         if (height == TimeAxisView::Smaller ||
120                 height == TimeAxisView::Small)
121                 TimeAxisViewItem::set_height (height - 3 );
122         else
123                 TimeAxisViewItem::set_height (height - NAME_HIGHLIGHT_SIZE - 3 );
124
125         redraw_curves ();
126 }
127
128 void
129 CrossfadeView::crossfade_changed (Change what_changed)
130 {
131         bool need_redraw_curves = false;
132
133         if (what_changed & BoundsChanged) {
134                 set_position (crossfade.position(), this);
135                 set_duration (crossfade.overlap_length(), this);
136                 need_redraw_curves = true;
137         }
138         
139         if (what_changed & Crossfade::ActiveChanged) {
140                 /* calls redraw_curves */
141                 active_changed ();
142         } else if (need_redraw_curves) {
143                 redraw_curves ();
144         }
145 }
146
147 void
148 CrossfadeView::redraw_curves ()
149 {
150         GnomeCanvasPoints* points; 
151         int32_t npoints;
152         float* vec;
153         
154         double h;
155
156         /*
157          At "height - 3.0" the bottom of the crossfade touches the name highlight or the bottom of the track (if the
158          track is either Small or Smaller.
159          */
160         switch(get_time_axis_view().height) {
161                 case TimeAxisView::Smaller:
162                 case TimeAxisView::Small:
163                         h = get_time_axis_view().height - 3.0;
164                         break;
165
166                 default:
167                         h = get_time_axis_view().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                 gnome_canvas_item_hide (fade_in);
180                 gnome_canvas_item_hide (fade_out);
181                 return;
182         } else {
183                 gnome_canvas_item_show (fade_in);
184                 gnome_canvas_item_show (fade_out);
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                 points->coords[pci++] = i;
193                 points->coords[pci++] = 2.0 + h - (h * vec[i]);
194         }
195         gnome_canvas_item_set (fade_in, "points", points, NULL);
196
197         crossfade.fade_out().get_vector (0, crossfade.length(), vec, npoints);
198         for (int i = 0, pci = 0; i < npoints; ++i) {
199                 points->coords[pci++] = i;
200                 points->coords[pci++] = 2.0 + h - (h * vec[i]);
201         }
202         gnome_canvas_item_set (fade_out, "points", points, NULL);
203
204         delete [] vec;
205
206         gnome_canvas_points_unref (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 }