Add test for #3356.
[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 "gui_thread.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 #include "ardour_ui.h"
36
37 using namespace ARDOUR;
38 using namespace PBD;
39 using namespace Editing;
40 using namespace Gnome;
41 using namespace Canvas;
42
43 PBD::Signal1<void,CrossfadeView*> CrossfadeView::CatchDeletion;
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(), false, TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowFrame)),
56           crossfade (xf),
57           left_view (lview),
58           right_view (rview)    
59 {
60         _valid = true;
61         _visible = true;
62
63         fade_in = new Line (*group);
64         fade_in->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CrossfadeLine.get();
65         fade_in->property_width_pixels() = 1;
66
67         fade_out = new Line (*group);
68         fade_out->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_CrossfadeLine.get();
69         fade_out->property_width_pixels() = 1;
70
71         /* no frame around the xfade or overlap rects */
72
73         frame->property_outline_what() = 0;
74
75         /* never show the vestigial frame */
76         vestigial_frame->hide();
77         show_vestigial = false;
78
79         group->signal_event().connect (sigc::bind (sigc::mem_fun (tv.editor(), &PublicEditor::canvas_crossfade_view_event), group, this));
80
81         PropertyChange all_crossfade_properties;
82         all_crossfade_properties.add (ARDOUR::Properties::active);
83         all_crossfade_properties.add (ARDOUR::Properties::follow_overlap);
84         crossfade_changed (all_crossfade_properties);
85
86         crossfade->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&CrossfadeView::crossfade_changed, this, _1), gui_context());
87         crossfade->FadesChanged.connect (*this, invalidator (*this), ui_bind (&CrossfadeView::crossfade_fades_changed, this), gui_context());
88         ColorsChanged.connect (sigc::mem_fun (*this, &CrossfadeView::color_handler));
89 }
90
91 CrossfadeView::~CrossfadeView ()
92 {
93          CatchDeletion (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_height (double h)
111 {
112         if (h > TimeAxisView::preset_height (HeightSmall)) {
113                 h -= NAME_HIGHLIGHT_SIZE;
114         }
115
116         TimeAxisViewItem::set_height (h);
117
118         redraw_curves ();
119 }
120
121 void
122 CrossfadeView::crossfade_changed (const PropertyChange& what_changed)
123 {
124         bool need_redraw_curves = false;
125
126         if (what_changed.contains (ARDOUR::bounds_change)) {
127                 set_position (crossfade->position(), this);
128                 set_duration (crossfade->length(), this);
129
130                 /* set_duration will call reset_width_dependent_items which in turn will call redraw_curves via active_changed,
131                    so no need for us to call it */
132                 need_redraw_curves = false;
133         }
134
135         if (what_changed.contains (ARDOUR::Properties::follow_overlap)) {
136                 need_redraw_curves = true;
137         }
138
139         if (what_changed.contains (ARDOUR::Properties::active)) {
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
154         if (!crossfade->following_overlap()) {
155                 /* curves should not be visible */
156                 fade_in->hide ();
157                 fade_out->hide ();
158                 return;
159         }
160
161         if (_height < 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().curve().get_vector (0, crossfade->length(), vec, npoints);
182
183         for (int i = 0, pci = 0; i < npoints; ++i) {
184                 Art::Point &p = (*points)[pci++];
185                 p.set_x (i + 1);
186                 p.set_y (_height - ((_height - 2) * vec[i]));
187         }
188         
189         fade_in->property_points() = *points;
190
191         crossfade->fade_out().curve().get_vector (0, crossfade->length(), vec, npoints);
192
193         for (int i = 0, pci = 0; i < npoints; ++i) {
194                 Art::Point &p = (*points)[pci++];
195                 p.set_x (i + 1);
196                 p.set_y (_height - ((_height - 2) * vec[i]));
197         }
198         
199         fade_out->property_points() = *points;
200
201         delete [] vec;
202
203         delete points;
204
205         /* XXX this is ugly, but it will have to wait till Crossfades are reimplented
206            as regions. This puts crossfade views on top of a track, above all regions.
207         */
208
209         group->raise_to_top();
210 }
211
212 void
213 CrossfadeView::active_changed ()
214 {
215         if (crossfade->active()) {
216                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
217         } else {
218                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_InactiveCrossfade.get();
219         }
220
221         redraw_curves ();
222 }
223
224 void
225 CrossfadeView::color_handler ()
226 {
227         active_changed ();
228 }
229
230 void
231 CrossfadeView::set_valid (bool yn)
232 {
233         _valid = yn;
234 }
235
236 AudioRegionView&
237 CrossfadeView::upper_regionview () const
238 {
239         if (left_view.region()->layer() > right_view.region()->layer()) {
240                 return left_view;
241         } else {
242                 return right_view;
243         }
244 }
245
246 void
247 CrossfadeView::show ()
248 {
249         group->show();
250         _visible = true;
251 }
252
253 void
254 CrossfadeView::hide ()
255 {
256         group->hide();
257         _visible = false;
258 }
259
260 void
261 CrossfadeView::fake_hide ()
262 {
263         group->hide();
264 }
265
266 void
267 CrossfadeView::crossfade_fades_changed ()
268 {
269         redraw_curves ();
270 }