edit group selection and cut/copy/paste/del shortcuts, interview mode tweaks
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / tracks.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include "atrack.h"
22 #include "automation.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "edit.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "file.h"
30 #include "filexml.h"
31 #include "intauto.h"
32 #include "intautos.h"
33 #include "localsession.h"
34 #include "module.h"
35 #include "panauto.h"
36 #include "panautos.h"
37 #include "patchbay.h"
38 #include "mainsession.h"
39 #include "strack.h"
40 #include "theme.h"
41 #include "track.h"
42 #include "trackcanvas.h"
43 #include "tracks.h"
44 #include "transportque.inc"
45 #include "vtrack.h"
46 #include <string.h>
47
48 Tracks::Tracks(EDL *edl)
49  : List<Track>()
50 {
51         this->edl = edl;
52 }
53
54 Tracks::Tracks()
55  : List<Track>()
56 {
57 }
58
59
60 Tracks::~Tracks()
61 {
62         delete_all_tracks();
63 }
64
65
66
67
68
69 void Tracks::equivalent_output(Tracks *tracks, double *result)
70 {
71         if(total_playable_vtracks() != tracks->total_playable_vtracks())
72         {
73                 *result = 0;
74         }
75         else
76         {
77                 Track *current = first;
78                 Track *that_current = tracks->first;
79                 while(current || that_current)
80                 {
81 // Get next video track
82                         while(current && current->data_type != TRACK_VIDEO)
83                                 current = NEXT;
84
85                         while(that_current && that_current->data_type != TRACK_VIDEO)
86                                 that_current = that_current->next;
87
88 // One doesn't exist but the other does
89                         if((!current && that_current) ||
90                                 (current && !that_current))
91                         {
92                                 *result = 0;
93                                 break;
94                         }
95                         else
96 // Both exist
97                         if(current && that_current)
98                         {
99                                 current->equivalent_output(that_current, result);
100                                 current = NEXT;
101                                 that_current = that_current->next;
102                         }
103                 }
104         }
105 }
106
107
108 void Tracks::clear_selected_edits()
109 {
110         for( Track *track=first; track; track=track->next ) {
111                 for( Edit *edit=track->edits->first; edit; edit=edit->next )
112                         edit->is_selected = 0;
113         }
114 }
115
116 void Tracks::select_affected_edits(double position, Track *start_track)
117 {
118         for( Track *track=start_track; track; track=track->next ) {
119                 if( !track->record ) continue;
120                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
121                         double startproject = track->from_units(edit->startproject);
122                         if( edl->equivalent(startproject, position) ) {
123                                 edit->is_selected = 1;
124                                 break;
125                         }
126                 }
127         }
128 }
129
130 void Tracks::get_selected_edits(ArrayList<Edit*> *drag_edits)
131 {
132         drag_edits->remove_all();
133         for( Track *track=first; track; track=track->next ) {
134                 for( Edit *edit=track->edits->first; edit; edit=edit->next ) {
135                         if( !edit->is_selected ) continue;
136                         drag_edits->append(edit);
137                 }
138         }
139 }
140
141 void Tracks::get_automation_extents(float *min,
142         float *max,
143         double start,
144         double end,
145         int autogrouptype)
146 {
147         *min = 0;
148         *max = 0;
149         int coords_undefined = 1;
150         for(Track *current = first; current; current = NEXT)
151         {
152                 if(current->record)
153                 {
154                         current->automation->get_extents(min,
155                                 max,
156                                 &coords_undefined,
157                                 current->to_units(start, 0),
158                                 current->to_units(end, 1),
159                                 autogrouptype);
160                 }
161         }
162 }
163
164
165 void Tracks::copy_from(Tracks *tracks)
166 {
167         Track *new_track = 0;
168
169         delete_all_tracks();
170         for(Track *current = tracks->first; current; current = NEXT)
171         {
172                 switch(current->data_type)
173                 {
174                 case TRACK_AUDIO:
175                         new_track = add_audio_track(0, 0);
176                         break;
177                 case TRACK_VIDEO:
178                         new_track = add_video_track(0, 0);
179                         break;
180                 case TRACK_SUBTITLE:
181                         new_track = add_subttl_track(0, 0);
182                         break;
183                 default:
184                         continue;
185                 }
186                 new_track->copy_from(current);
187         }
188 }
189
190 Tracks& Tracks::operator=(Tracks &tracks)
191 {
192 printf("Tracks::operator= 1\n");
193         copy_from(&tracks);
194         return *this;
195 }
196
197 int Tracks::load(FileXML *xml,
198         int &track_offset,
199         uint32_t load_flags)
200 {
201 // add the appropriate type of track
202         char string[BCTEXTLEN];
203         Track *track = 0;
204         string[0] = 0;
205
206         xml->tag.get_property("TYPE", string);
207
208         if((load_flags & LOAD_ALL) == LOAD_ALL ||
209                 (load_flags & LOAD_EDITS)) {
210                 if(!strcmp(string, "VIDEO")) {
211                         track = add_video_track(0, 0);
212                 }
213                 else if(!strcmp(string, "SUBTTL")) {
214                         track = add_subttl_track(0, 0);
215                 }
216                 else {
217                         track = add_audio_track(0, 0);    // default to audio
218                 }
219         }
220         else {
221                 track = get_item_number(track_offset++);
222         }
223
224 // load it
225         if( track ) track->load(xml, track_offset, load_flags);
226
227         return 0;
228 }
229
230 Track* Tracks::add_audio_track(int above, Track *dst_track)
231 {
232         ATrack* new_track = new ATrack(edl, this);
233         if(!dst_track)
234         {
235                 dst_track = (above ? first : last);
236         }
237
238         if(above)
239         {
240                 insert_before(dst_track, (Track*)new_track);
241         }
242         else
243         {
244                 insert_after(dst_track, (Track*)new_track);
245 // Shift effects referenced below the destination track
246         }
247
248 // Shift effects referenced below the new track
249         for(Track *track = last;
250                 track && track != new_track;
251                 track = track->previous)
252         {
253                 change_modules(number_of(track) - 1, number_of(track), 0);
254         }
255
256
257         new_track->create_objects();
258         new_track->set_default_title();
259
260         int current_pan = 0;
261         for(Track *current = first;
262                 current != (Track*)new_track;
263                 current = NEXT)
264         {
265                 if(current->data_type == TRACK_AUDIO) current_pan++;
266                 if(current_pan >= edl->session->audio_channels) current_pan = 0;
267         }
268
269
270
271         PanAuto* pan_auto =
272                 (PanAuto*)new_track->automation->autos[AUTOMATION_PAN]->default_auto;
273         pan_auto->values[current_pan] = 1.0;
274
275         BC_Pan::calculate_stick_position(edl->session->audio_channels,
276                 edl->session->achannel_positions,
277                 pan_auto->values,
278                 MAX_PAN,
279                 PAN_RADIUS,
280                 pan_auto->handle_x,
281                 pan_auto->handle_y);
282         return new_track;
283 }
284
285 Track* Tracks::add_video_track(int above, Track *dst_track)
286 {
287         VTrack* new_track = new VTrack(edl, this);
288         if(!dst_track)
289                 dst_track = (above ? first : last);
290         if(above)
291                 insert_before(dst_track, (Track*)new_track);
292         else
293                 insert_after(dst_track, (Track*)new_track);
294
295         for(Track *track = last; track && track != new_track; track = track->previous)
296                 change_modules(number_of(track) - 1, number_of(track), 0);
297
298         new_track->create_objects();
299         new_track->set_default_title();
300         return new_track;
301 }
302
303
304 Track* Tracks::add_subttl_track(int above, Track *dst_track)
305 {
306         STrack* new_track = new STrack(edl, this);
307         if(!dst_track)
308                 dst_track = (above ? first : last);
309
310         if(above)
311                 insert_before(dst_track, (Track*)new_track);
312         else
313                 insert_after(dst_track, (Track*)new_track);
314
315         for(Track *track = last; track && track != new_track; track = track->previous)
316                 change_modules(number_of(track) - 1, number_of(track), 0);
317
318         new_track->create_objects();
319         new_track->set_default_title();
320 //      new_track->paste_silence(0,total_length(),0);
321         return new_track;
322 }
323
324
325 int Tracks::delete_track(Track *track)
326 {
327         if (!track)
328                 return 0;
329
330         int old_location = number_of(track);
331         detach_shared_effects(old_location);
332
333 // Shift effects referencing effects below the deleted track
334         for(Track *current = track;
335                 current;
336                 current = NEXT)
337         {
338                 change_modules(number_of(current), number_of(current) - 1, 0);
339         }
340         if(track) delete track;
341
342         return 0;
343 }
344
345 int Tracks::detach_shared_effects(int module)
346 {
347         for(Track *current = first; current; current = NEXT)
348         {
349                 current->detach_shared_effects(module);
350         }
351
352         return 0;
353  }
354
355 int Tracks::total_of(int type)
356 {
357         int result = 0;
358
359         for(Track *current = first; current; current = NEXT)
360         {
361                 long unit_start = current->to_units(edl->local_session->get_selectionstart(1), 0);
362                 Auto *mute_keyframe = 0;
363                 current->automation->autos[AUTOMATION_MUTE]->
364                         get_prev_auto(unit_start, PLAY_FORWARD, mute_keyframe);
365                 IntAuto *mute_auto = (IntAuto *)mute_keyframe;
366
367                 result +=
368                         (current->play && type == PLAY) ||
369                         (current->record && type == RECORD) ||
370                         (current->gang && type == GANG) ||
371                         (current->draw && type == DRAW) ||
372                         (mute_auto->value && type == MUTE) ||
373                         (current->expand_view && type == EXPAND);
374         }
375         return result;
376 }
377
378 int Tracks::recordable_audio_tracks()
379 {
380         int result = 0;
381         for(Track *current = first; current; current = NEXT)
382                 if(current->data_type == TRACK_AUDIO && current->record) result++;
383         return result;
384 }
385
386 int Tracks::recordable_video_tracks()
387 {
388         int result = 0;
389         for(Track *current = first; current; current = NEXT)
390         {
391                 if(current->data_type == TRACK_VIDEO && current->record) result++;
392         }
393         return result;
394 }
395
396
397 int Tracks::playable_audio_tracks()
398 {
399         int result = 0;
400
401         for(Track *current = first; current; current = NEXT)
402         {
403                 if(current->data_type == TRACK_AUDIO && current->play)
404                 {
405                         result++;
406                 }
407         }
408
409         return result;
410 }
411
412 int Tracks::playable_video_tracks()
413 {
414         int result = 0;
415
416         for(Track *current = first; current; current = NEXT)
417         {
418                 if(current->data_type == TRACK_VIDEO && current->play)
419                 {
420                         result++;
421                 }
422         }
423         return result;
424 }
425
426 int Tracks::total_audio_tracks()
427 {
428         int result = 0;
429         for(Track *current = first; current; current = NEXT)
430                 if(current->data_type == TRACK_AUDIO) result++;
431         return result;
432 }
433
434 int Tracks::total_video_tracks()
435 {
436         int result = 0;
437         for(Track *current = first; current; current = NEXT)
438                 if(current->data_type == TRACK_VIDEO) result++;
439         return result;
440 }
441
442 double Tracks::total_playable_length()
443 {
444         double total = 0;
445         for(Track *current = first; current; current = NEXT)
446         {
447                 if( current->play )
448                 {
449                         double length = current->get_length();
450                         if(length > total) total = length;
451                 }
452         }
453         return total;
454 }
455
456 double Tracks::total_recordable_length()
457 {
458         double total = -1;
459         for(Track *current = first; current; current = NEXT)
460         {
461                 if(current->record)
462                 {
463                         double length = current->get_length();
464                         if(length > total) total = length;
465                 }
466         }
467         return total;
468 }
469
470 double Tracks::total_length()
471 {
472         double total = 0;
473         for(Track *current = first; current; current = NEXT)
474         {
475                 double length = current->get_length();
476                 if(length > total) total = length;
477         }
478         return total;
479 }
480
481 double Tracks::total_audio_length()
482 {
483         double total = 0;
484         for(Track *current = first; current; current = NEXT)
485         {
486                 if(current->data_type == TRACK_AUDIO &&
487                         current->get_length() > total) total = current->get_length();
488         }
489         return total;
490 }
491
492 double Tracks::total_video_length()
493 {
494         double total = 0;
495         for(Track *current = first; current; current = NEXT)
496         {
497                 if(current->data_type == TRACK_VIDEO &&
498                         current->get_length() > total) total = current->get_length();
499         }
500         return total;
501 }
502
503 double Tracks::total_length_framealigned(double fps)
504 {
505         if (total_audio_tracks() && total_video_tracks())
506                 return MIN(floor(total_audio_length() * fps), floor(total_video_length() * fps)) / fps;
507
508         if (total_audio_tracks())
509                 return floor(total_audio_length() * fps) / fps;
510
511         if (total_video_tracks())
512                 return floor(total_video_length() * fps) / fps;
513
514         return 0;
515 }
516
517 void Tracks::translate_projector(float offset_x, float offset_y)
518 {
519         for(Track *current = first; current; current = NEXT)
520         {
521                 if(current->data_type == TRACK_VIDEO)
522                 {
523                         ((VTrack*)current)->translate(offset_x, offset_y, 0);
524                 }
525         }
526 }
527
528 void Tracks::update_y_pixels(Theme *theme)
529 {
530 //      int y = -edl->local_session->track_start;
531         int y = 0;
532         for(Track *current = first; current; current = NEXT)
533         {
534 //printf("Tracks::update_y_pixels %d\n", y);
535                 current->y_pixel = y;
536                 y += current->vertical_span(theme);
537         }
538 }
539
540 int Tracks::dump(FILE *fp)
541 {
542         for(Track* current = first; current; current = NEXT)
543         {
544                 fprintf(fp,"  Track: %p\n", current);
545                 current->dump(fp);
546                 fprintf(fp,"\n");
547         }
548         return 0;
549 }
550
551 void Tracks::select_all(int type,
552                 int value)
553 {
554         for(Track* current = first; current; current = NEXT)
555         {
556                 double position = edl->local_session->get_selectionstart(1);
557
558                 if(type == PLAY) current->play = value;
559                 if(type == RECORD) current->record = value;
560                 if(type == GANG) current->gang = value;
561                 if(type == DRAW) current->draw = value;
562
563                 if(type == MUTE)
564                 {
565                         ((IntAuto*)current->automation->autos[AUTOMATION_MUTE]->get_auto_for_editing(position))->value = value;
566                 }
567
568                 if(type == EXPAND) current->expand_view = value;
569         }
570 }
571
572 // ===================================== file operations
573
574 int Tracks::popup_transition(int cursor_x, int cursor_y)
575 {
576         int result = 0;
577         for(Track* current = first; current && !result; current = NEXT)
578         {
579                 result = current->popup_transition(cursor_x, cursor_y);
580         }
581         return result;
582 }
583
584
585 int Tracks::change_channels(int oldchannels, int newchannels)
586 {
587         for(Track *current = first; current; current = NEXT)
588         { current->change_channels(oldchannels, newchannels); }
589         return 0;
590 }
591
592
593
594 int Tracks::totalpixels()
595 {
596         int result = 0;
597         for(Track* current = first; current; current = NEXT)
598         {
599                 result += edl->local_session->zoom_track;
600         }
601         return result;
602 }
603
604 int Tracks::number_of(Track *track)
605 {
606         int i = 0;
607         for(Track *current = first; current && current != track; current = NEXT)
608         {
609                 i++;
610         }
611         return i;
612 }
613
614 Track* Tracks::number(int number)
615 {
616         Track *current;
617         int i = 0;
618         for(current = first; current && i < number; current = NEXT)
619         {
620                 i++;
621         }
622         return current;
623 }
624
625
626 int Tracks::total_playable_vtracks()
627 {
628         int result = 0;
629         for(Track *current = first; current; current = NEXT)
630         {
631                 if(current->data_type == TRACK_VIDEO && current->play) result++;
632         }
633         return result;
634 }
635
636 int Tracks::plugin_exists(Plugin *plugin)
637 {
638         for(Track *track = first; track; track = track->next)
639         {
640                 if(track->plugin_exists(plugin)) return 1;
641         }
642         return 0;
643 }
644
645 int Tracks::track_exists(Track *track)
646 {
647         for(Track *current = first; current; current = NEXT)
648         {
649                 if(current == track) return 1;
650         }
651         return 0;
652 }
653
654
655 Track *Tracks::get(int idx, int data_type)
656 {
657         for(Track *current = first; current; current = NEXT)
658         {
659                 if( current->data_type != data_type ) continue;
660                 if( --idx < 0 ) return current;
661         }
662         return 0;
663 }
664
665