changes to help strp silence
[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 {
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_height (get_time_axis_view().current_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 (sigc::bind (sigc::mem_fun (tv.editor(), &PublicEditor::canvas_crossfade_view_event), group, this));
84
85         PropertyChange all_crossfade_properties;
86         all_crossfade_properties.add (ARDOUR::Properties::active);
87         all_crossfade_properties.add (ARDOUR::Properties::follow_overlap);
88         crossfade_changed (all_crossfade_properties);
89
90         crossfade->PropertyChanged.connect (*this, ui_bind (&CrossfadeView::crossfade_changed, this, _1), gui_context());
91         ColorsChanged.connect (sigc::mem_fun (*this, &CrossfadeView::color_handler));
92 }
93
94 CrossfadeView::~CrossfadeView ()
95 {
96          CatchDeletion (this) ; /* EMIT_SIGNAL */
97 }
98
99 void
100 CrossfadeView::reset_width_dependent_items (double pixel_width)
101 {
102         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
103
104         active_changed ();
105
106         if (pixel_width < 5) {
107                 fade_in->hide();
108                 fade_out->hide();
109         }
110 }
111
112 void
113 CrossfadeView::set_height (double height)
114 {
115         double h = 0;
116         if (height <= TimeAxisView::hSmaller) {
117                 h = height - 3;
118         } else {
119                 h = height - NAME_HIGHLIGHT_SIZE - 3;
120         }
121
122         TimeAxisViewItem::set_height (h);
123
124         _height = h;
125         redraw_curves ();
126 }
127
128 void
129 CrossfadeView::crossfade_changed (const PropertyChange& what_changed)
130 {
131         bool need_redraw_curves = false;
132
133         if (what_changed.contains (ARDOUR::bounds_change)) {
134                 set_position (crossfade->position(), this);
135                 set_duration (crossfade->length(), this);
136
137                 /* set_duration will call reset_width_dependent_items which in turn will call redraw_curves via active_changed,
138                    so no need for us to call it */
139                 need_redraw_curves = false;
140         }
141
142         if (what_changed.contains (ARDOUR::Properties::follow_overlap)) {
143                 need_redraw_curves = true;
144         }
145
146         if (what_changed.contains (ARDOUR::Properties::active)) {
147                 /* calls redraw_curves */
148                 active_changed ();
149         } else if (need_redraw_curves) {
150                 redraw_curves ();
151         }
152 }
153
154 void
155 CrossfadeView::redraw_curves ()
156 {
157         Points* points;
158         int32_t npoints;
159         float* vec;
160
161         if (!crossfade->following_overlap()) {
162                 /* curves should not be visible */
163                 fade_in->hide ();
164                 fade_out->hide ();
165                 return;
166         }
167
168         if (_height < 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().curve().get_vector (0, crossfade->length(), vec, npoints);
189
190         for (int i = 0, pci = 0; i < npoints; ++i) {
191                 Art::Point &p = (*points)[pci++];
192                 p.set_x(i);
193                 p.set_y(2.0 + _height - (_height * vec[i]));
194         }
195         fade_in->property_points() = *points;
196
197         crossfade->fade_out().curve().get_vector (0, crossfade->length(), vec, npoints);
198         for (int i = 0, pci = 0; i < npoints; ++i) {
199                 Art::Point &p = (*points)[pci++];
200                 p.set_x(i);
201                 p.set_y(2.0 + _height - (_height * vec[i]));
202         }
203         fade_out->property_points() = *points;
204
205         delete [] vec;
206
207         delete points;
208
209         /* XXX this is ugly, but it will have to wait till Crossfades are reimplented
210            as regions. This puts crossfade views on top of a track, above all regions.
211         */
212
213         group->raise_to_top();
214 }
215
216 void
217 CrossfadeView::active_changed ()
218 {
219         if (crossfade->active()) {
220                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
221         } else {
222                 frame->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_InactiveCrossfade.get();
223         }
224
225         redraw_curves ();
226 }
227
228 void
229 CrossfadeView::color_handler ()
230 {
231         active_changed ();
232 }
233
234 void
235 CrossfadeView::set_valid (bool yn)
236 {
237         _valid = yn;
238 }
239
240 AudioRegionView&
241 CrossfadeView::upper_regionview () const
242 {
243         if (left_view.region()->layer() > right_view.region()->layer()) {
244                 return left_view;
245         } else {
246                 return right_view;
247         }
248 }
249
250 void
251 CrossfadeView::show ()
252 {
253         group->show();
254         _visible = true;
255 }
256
257 void
258 CrossfadeView::hide ()
259 {
260         group->hide();
261         _visible = false;
262 }
263
264 void
265 CrossfadeView::fake_hide ()
266 {
267         group->hide();
268 }
269