viewer window playback tweaks
[goodguy/history.git] / cinelerra-5.1 / cinelerra / apatchgui.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "apatchgui.h"
23 #include "atrack.h"
24 #include "autoconf.h"
25 #include "automation.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "floatauto.h"
29 #include "floatautos.h"
30 #include "gwindowgui.h"
31 #include "intauto.h"
32 #include "intautos.h"
33 #include "language.h"
34 #include "localsession.h"
35 #include "mainsession.h"
36 #include "mainundo.h"
37 #include "mwindow.h"
38 #include "mwindowgui.h"
39 #include "panauto.h"
40 #include "panautos.h"
41 #include "patchbay.h"
42 #include "theme.h"
43 #include "trackcanvas.h"
44 #include "zwindow.h"
45
46 APatchGUI::APatchGUI(MWindow *mwindow, PatchBay *patchbay,
47                 ATrack *track, int x, int y)
48  : PatchGUI(mwindow, patchbay, track, x, y)
49 {
50         data_type = TRACK_AUDIO;
51         this->atrack = track;
52         meter = 0;
53         pan = 0;
54         fade = 0;
55 }
56
57 APatchGUI::~APatchGUI()
58 {
59         if( fade ) delete fade;
60         if( meter ) delete meter;
61         if( pan ) delete pan;
62 }
63
64 void APatchGUI::create_objects()
65 {
66         update(x, y);
67 }
68
69 int APatchGUI::reposition(int x, int y)
70 {
71         int y1 = PatchGUI::reposition(x, y);
72
73         if( fade )
74                 fade->reposition_window(fade->get_x(), y1+y);
75         y1 += mwindow->theme->fade_h;
76         if( meter )
77                 meter->reposition_window(meter->get_x(), y1+y, -1, meter->get_w());
78         y1 += mwindow->theme->meter_h;
79         if( mix )
80                 mix->reposition_window(mix->get_x(), y1+y);
81         if( pan )
82                 pan->reposition_window(pan->get_x(), y1+y);
83         if( nudge )
84                 nudge->reposition_window(nudge->get_x(), y1+y);
85         y1 += mwindow->theme->pan_h;
86
87         return y1;
88 }
89
90 int APatchGUI::update(int x, int y)
91 {
92         int h = track->vertical_span(mwindow->theme);
93         int x1 = 0;
94         int y1 = PatchGUI::update(x, y);
95
96         if( fade ) {
97                 if( h - y1 < mwindow->theme->fade_h ) {
98                         delete fade;
99                         fade = 0;
100                 }
101                 else {
102                         FloatAuto *previous = 0, *next = 0;
103                         double unit_position = mwindow->edl->local_session->get_selectionstart(1);
104                         unit_position = mwindow->edl->align_to_frame(unit_position, 0);
105                         unit_position = atrack->to_units(unit_position, 0);
106                         FloatAutos *ptr = (FloatAutos*)atrack->automation->autos[AUTOMATION_FADE];
107                         float value = ptr->get_value((long)unit_position, PLAY_FORWARD, previous, next);
108                         fade->update(fade->get_w(), value,
109                                      mwindow->edl->local_session->automation_mins[AUTOGROUPTYPE_AUDIO_FADE],
110                                      mwindow->edl->local_session->automation_maxs[AUTOGROUPTYPE_AUDIO_FADE]);
111                 }
112         }
113         else
114         if( h - y1 >= mwindow->theme->fade_h ) {
115                 patchbay->add_subwindow(fade = new AFadePatch(mwindow, this, x1+x, y1+y,
116                         patchbay->get_w() - 10));
117         }
118         y1 += mwindow->theme->fade_h;
119
120         if( meter ) {
121                 if( h - y1 < mwindow->theme->meter_h ) {
122                         delete meter;  meter = 0;
123                 }
124         }
125         else
126         if( h - y1 >= mwindow->theme->meter_h ) {
127                 patchbay->add_subwindow(meter = new AMeterPatch(mwindow, this, x1+x, y1+y));
128         }
129         y1 += mwindow->theme->meter_h;
130
131         if( pan ) {
132                 if( h - y1 < mwindow->theme->pan_h ) {
133                         delete mix;    mix = 0;
134                         delete pan;    pan = 0;
135                         delete nudge;  nudge = 0;
136                 }
137                 else {
138                         if( mwindow->session->selected_zwindow >= 0 ) {
139                                 int v = mwindow->mixer_track_active(track);
140                                 mix->update(v);
141                         }
142                         if( pan->get_total_values() != mwindow->edl->session->audio_channels ) {
143                                 pan->change_channels(mwindow->edl->session->audio_channels,
144                                         mwindow->edl->session->achannel_positions);
145                         }
146                         else {
147                                 int handle_x, handle_y;
148                                 PanAuto *previous = 0, *next = 0;
149                                 double unit_position = mwindow->edl->local_session->get_selectionstart(1);
150                                 unit_position = mwindow->edl->align_to_frame(unit_position, 0);
151                                 unit_position = atrack->to_units(unit_position, 0);
152                                 PanAutos *ptr = (PanAutos*)atrack->automation->autos[AUTOMATION_PAN];
153                                 ptr->get_handle(handle_x, handle_y, (long)unit_position,
154                                                 PLAY_FORWARD, previous, next);
155                                 pan->update(handle_x, handle_y);
156                         }
157                         nudge->update();
158                 }
159         }
160         else
161         if( h - y1 >= mwindow->theme->pan_h ) {
162                 patchbay->add_subwindow(mix = new AMixPatch(mwindow, this, x1+x, y1+y+5));
163                 x1 += mix->get_w() + 10;
164                 patchbay->add_subwindow(pan = new APanPatch(mwindow, this, x1+x, y1+y));
165                 x1 += pan->get_w() + 20;
166                 patchbay->add_subwindow(nudge = new NudgePatch(mwindow, this, x1+x, y1+y,
167                                 patchbay->get_w() - x1-x - 10));
168         }
169         y1 += mwindow->theme->pan_h;
170
171         return y1;
172 }
173
174 void APatchGUI::synchronize_fade(float value_change)
175 {
176         if( fade && !change_source ) {
177                 fade->update(fade->get_value() + value_change);
178                 fade->update_edl();
179         }
180 }
181
182
183
184 AFadePatch::AFadePatch(MWindow *mwindow, APatchGUI *patch, int x, int y, int w)
185  : BC_FSlider(x, y, 0, w, w,
186         mwindow->edl->local_session->automation_mins[AUTOGROUPTYPE_AUDIO_FADE],
187         mwindow->edl->local_session->automation_maxs[AUTOGROUPTYPE_AUDIO_FADE],
188         mwindow->get_float_auto(patch, AUTOMATION_FADE)->get_value())
189 {
190         this->mwindow = mwindow;
191         this->patch = patch;
192 }
193
194 float AFadePatch::update_edl()
195 {
196         FloatAuto *current;
197         double position = mwindow->edl->local_session->get_selectionstart(1);
198         Autos *fade_autos = patch->atrack->automation->autos[AUTOMATION_FADE];
199         int need_undo = !fade_autos->auto_exists_for_editing(position);
200
201         mwindow->undo->update_undo_before(_("fade"), need_undo ? 0 : this);
202
203         current = (FloatAuto*)fade_autos->get_auto_for_editing(position);
204
205         float result = get_value() - current->get_value();
206         current->set_value(this->get_value());
207
208         mwindow->undo->update_undo_after(_("fade"), LOAD_AUTOMATION);
209
210         return result;
211 }
212
213
214 int AFadePatch::handle_event()
215 {
216         if( shift_down() ) {
217                 update(0.0);
218                 set_tooltip(get_caption());
219         }
220
221         patch->change_source = 1;
222         float change = update_edl();
223         if( patch->track->gang && patch->track->record )
224                 patch->patchbay->synchronize_faders(change, TRACK_AUDIO, patch->track);
225         patch->change_source = 0;
226
227         mwindow->sync_parameters(CHANGE_PARAMS);
228
229         if( mwindow->edl->session->auto_conf->autos[AUTOMATION_FADE] ) {
230                 mwindow->gui->draw_overlays(1);
231         }
232         return 1;
233 }
234
235 AKeyFadePatch::AKeyFadePatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
236  : BC_SubWindow(x,y, patch->patchbay->get_w(),20,
237         GWindowGUI::auto_colors[AUTOMATION_FADE])
238 {
239         this->mwindow = mwindow;
240         this->patch = patch;
241 }
242
243 void AKeyFadePatch::create_objects()
244 {
245         akey_fade_value = new AKeyFadeValue(this);
246         add_subwindow(akey_fade_value);
247         akey_fade_value->activate();
248         show_window();
249 }
250
251 AKeyFadeValue::AKeyFadeValue(AKeyFadePatch *akey_fade_patch)
252  : AFadePatch(akey_fade_patch->mwindow, akey_fade_patch->patch,
253         0,0, akey_fade_patch->get_w())
254 {
255         this->akey_fade_patch = akey_fade_patch;
256 }
257
258 int AKeyFadeValue::button_release_event()
259 {
260         AFadePatch::button_release_event();
261         return 0;
262 }
263
264 int AKeyFadeValue::handle_event()
265 {
266         APatchGUI *patch = akey_fade_patch->patch;
267         int ret = AFadePatch::handle_event();
268         AFadePatch *fade = patch->fade;
269         if( fade )
270                 fade->update(get_value());
271         return ret;
272 }
273
274
275 APanPatch::APanPatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
276  : BC_Pan(x, y, PAN_RADIUS, MAX_PAN,
277         mwindow->edl->session->audio_channels,
278         mwindow->edl->session->achannel_positions,
279         mwindow->get_pan_auto(patch)->handle_x,
280         mwindow->get_pan_auto(patch)->handle_y,
281         mwindow->get_pan_auto(patch)->values)
282 {
283         this->mwindow = mwindow;
284         this->patch = patch;
285         set_tooltip(_("Pan"));
286 }
287
288 int APanPatch::handle_event()
289 {
290         PanAuto *current;
291         double position = mwindow->edl->local_session->get_selectionstart(1);
292         Autos *pan_autos = patch->atrack->automation->autos[AUTOMATION_PAN];
293         int need_undo = !pan_autos->auto_exists_for_editing(position);
294
295         mwindow->undo->update_undo_before(_("pan"), need_undo ? 0 : this);
296
297         current = (PanAuto*)pan_autos->get_auto_for_editing(position);
298
299         current->handle_x = get_stick_x();
300         current->handle_y = get_stick_y();
301         memcpy(current->values, get_values(), sizeof(float) * mwindow->edl->session->audio_channels);
302
303         mwindow->undo->update_undo_after(_("pan"), LOAD_AUTOMATION);
304
305         mwindow->sync_parameters(CHANGE_PARAMS);
306
307         if( need_undo && mwindow->edl->session->auto_conf->autos[AUTOMATION_PAN] ) {
308                 mwindow->gui->draw_overlays(1);
309         }
310         return 1;
311 }
312
313 AKeyPanPatch::AKeyPanPatch(MWindow *mwindow, APatchGUI *patch)
314  : APanPatch(mwindow, patch, -1,-1)
315 {
316 }
317
318 int AKeyPanPatch::button_release_event()
319 {
320         APanPatch::button_release_event();
321         return 0;
322 }
323
324 int AKeyPanPatch::handle_event()
325 {
326         int ret = APanPatch::handle_event();
327         APanPatch *pan = patch->pan;
328         if( pan )
329                 pan->update(get_stick_x(), get_stick_y());
330         return ret;
331 }
332
333
334 AMeterPatch::AMeterPatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
335  : BC_Meter(x, y, METER_HORIZ, patch->patchbay->get_w() - 10,
336         mwindow->edl->session->min_meter_db, mwindow->edl->session->max_meter_db,
337         mwindow->edl->session->meter_format, 0, -1)
338 {
339         this->mwindow = mwindow;
340         this->patch = patch;
341         set_delays(TRACKING_RATE * 10,
342                         TRACKING_RATE);
343 }
344
345 int AMeterPatch::button_press_event()
346 {
347         if( cursor_inside() && is_event_win() && get_buttonpress() == 1 ) {
348                 mwindow->reset_meters();
349                 return 1;
350         }
351
352         return 0;
353 }
354
355 AMixPatch::AMixPatch(MWindow *mwindow, APatchGUI *patch, int x, int y)
356  : MixPatch(mwindow, patch, x, y)
357 {
358         set_tooltip(_("Mixer"));
359 }
360
361 AMixPatch::~AMixPatch()
362 {
363 }
364