Merge libs/ardour and gtk2_ardour with 2.0-ongoing R2837.
[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 #include "ardour_ui.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() = ARDOUR_UI::config()->canvasvar_CrossfadeLine.get();
66         fade_in->property_width_pixels() = 1;
67
68         fade_out = new Line (*group);
69         fade_out->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CrossfadeLine.get();
70         fade_out->property_width_pixels() = 1;
71         
72         set_y_position_and_height (0, 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         ColorsChanged.connect (mem_fun (*this, &CrossfadeView::color_handler));
89 }
90
91 CrossfadeView::~CrossfadeView ()
92 {
93          GoingAway (this) ; /* EMIT_SIGNAL */
94 }
95
96 void
97 CrossfadeView::reset_width_dependent_items (double pixel_width)
98 {
99         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
100
101         active_changed ();
102
103         if (pixel_width < 5) {
104                 fade_in->hide();
105                 fade_out->hide();
106         }
107 }
108
109 void
110 CrossfadeView::set_y_position_and_height (double y, double h)
111 {
112         if (h == TimeAxisView::hSmaller || h == TimeAxisView::hSmall) {
113                 TimeAxisViewItem::set_y_position_and_height (y, h - 3 );
114         } else {
115                 TimeAxisViewItem::set_y_position_and_height (y, h - NAME_HIGHLIGHT_SIZE - 3 );
116         }
117
118         _y_position = y;
119         _height = h;
120
121         redraw_curves ();
122 }
123
124 void
125 CrossfadeView::crossfade_changed (Change what_changed)
126 {
127         bool need_redraw_curves = false;
128
129         if (what_changed & BoundsChanged) {
130                 set_position (crossfade->position(), this);
131                 set_duration (crossfade->length(), this);
132                 need_redraw_curves = true;
133         }
134
135         if (what_changed & Crossfade::FollowOverlapChanged) {
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         Points* points; 
151         int32_t npoints;
152         float* vec;
153         double h;
154
155         if (!crossfade->following_overlap()) {
156                 /* curves should not be visible */
157                 fade_in->hide ();
158                 fade_out->hide ();
159                 return;
160         }
161
162         /*
163          At "height - 3.0" the bottom of the crossfade touches the name highlight or the bottom of the track (if the
164          track is either Small or Smaller.
165          */
166         double const tav_height = get_time_axis_view().height;
167         if (tav_height == TimeAxisView::hSmaller || tav_height == TimeAxisView::hSmall) {
168                 h = tav_height - 3.0;
169         } else {
170                 h = tav_height - NAME_HIGHLIGHT_SIZE - 3.0;
171         }
172
173         if (h < 0) {
174                 /* no space allocated yet */
175                 return;
176         }
177
178         npoints = get_time_axis_view().editor.frame_to_pixel (crossfade->length());
179         npoints = std::min (gdk_screen_width(), npoints);
180
181         if (!_visible || !crossfade->active() || npoints < 3) {
182                 fade_in->hide();
183                 fade_out->hide();
184                 return;
185         } else {
186                 fade_in->show();
187                 fade_out->show();
188         } 
189
190         points = get_canvas_points ("xfade edit redraw", npoints);
191         vec = new float[npoints];
192
193         crossfade->fade_in().curve().get_vector (0, crossfade->length(), vec, npoints);
194         for (int i = 0, pci = 0; i < npoints; ++i) {
195                 Art::Point &p = (*points)[pci++];
196                 p.set_x(i);
197                 p.set_y(_y_position + 2.0 + h - (h * vec[i]));
198         }
199         fade_in->property_points() = *points;
200
201         crossfade->fade_out().curve().get_vector (0, crossfade->length(), vec, npoints);
202         for (int i = 0, pci = 0; i < npoints; ++i) {
203                 Art::Point &p = (*points)[pci++];
204                 p.set_x(i);
205                 p.set_y(_y_position + 2.0 + h - (h * vec[i]));
206         }
207         fade_out->property_points() = *points;
208
209         delete [] vec;
210
211         delete points;
212
213         /* XXX this is ugly, but it will have to wait till Crossfades are reimplented
214            as regions. This puts crossfade views on top of a track, above all regions.
215         */
216
217         group->raise_to_top();
218 }
219
220 void
221 CrossfadeView::active_changed ()
222 {
223         if (crossfade->active()) {
224                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
225         } else {
226                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_InactiveCrossfade.get();
227         }
228
229         redraw_curves ();
230 }
231
232 void
233 CrossfadeView::color_handler ()
234 {
235         active_changed ();
236 }
237
238 void
239 CrossfadeView::set_valid (bool yn)
240 {
241         _valid = yn;
242 }
243
244 AudioRegionView&
245 CrossfadeView::upper_regionview () const
246 {
247         if (left_view.region()->layer() > right_view.region()->layer()) {
248                 return left_view;
249         } else {
250                 return right_view;
251         }
252 }
253
254 void
255 CrossfadeView::show ()
256 {
257         group->show();
258         _visible = true;
259 }
260
261 void
262 CrossfadeView::hide ()
263 {
264         group->hide();
265         _visible = false;
266 }
267
268 void
269 CrossfadeView::fake_hide ()
270 {
271         group->hide();
272 }
273