add sliders to cwdw proj/cam tools, edit id tweaks, deltrk shortcut tweak
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / edit.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 "asset.h"
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "edit.h"
27 #include "edits.h"
28 #include "edl.h"
29 #include "edlsession.h"
30 #include "filexml.h"
31 #include "filesystem.h"
32 #include "labels.h"
33 #include "localsession.h"
34 #include "plugin.h"
35 #include "mainsession.h"
36 #include "strack.h"
37 #include "trackcanvas.h"
38 #include "tracks.h"
39 #include "transition.h"
40 #include <string.h>
41
42
43 Edit::Edit()
44 {
45         reset();
46 }
47
48 Edit::Edit(EDL *edl, Track *track)
49 {
50         reset();
51         this->edl = edl;
52         this->track = track;
53         if(track) this->edits = track->edits;
54         id = EDL::next_id();
55         orig_id = id;
56 }
57
58 Edit::Edit(EDL *edl, Edits *edits)
59 {
60         reset();
61         this->edl = edl;
62         this->edits = edits;
63         if(edits) this->track = edits->track;
64         id = EDL::next_id();
65         orig_id = id;
66 }
67
68 Edit::~Edit()
69 {
70 //printf("Edit::~Edit 1\n");
71         if(transition) delete transition;
72 //printf("Edit::~Edit 2\n");
73 }
74
75 void Edit::reset()
76 {
77         edl = 0;
78         track = 0;
79         edits = 0;
80         startsource = 0;
81         startproject = 0;
82         length = 0;
83         asset = 0;
84         transition = 0;
85         channel = 0;
86         user_title[0] = 0;
87         nested_edl = 0;
88         is_plugin = 0;
89         is_selected = 0;
90         hard_left = 0;
91         hard_right = 0;
92         color = 0;
93         group_id = 0;
94 }
95
96 Indexable* Edit::get_source()
97 {
98         if(asset) return asset;
99         if(nested_edl) return nested_edl;
100         return 0;
101 }
102
103 int Edit::copy(int64_t start,
104         int64_t end,
105         FileXML *file,
106         const char *output_path)
107 {
108 // variables
109 //printf("Edit::copy 1\n");
110
111         int64_t endproject = startproject + length;
112         int result;
113
114         if((startproject >= start && startproject <= end) ||  // startproject in range
115                  (endproject <= end && endproject >= start) ||     // endproject in range
116                  (startproject <= start && endproject >= end))    // range in project
117         {
118 // edit is in range
119                 int64_t startproject_in_selection = startproject; // start of edit in selection in project
120                 int64_t startsource_in_selection = startsource; // start of source in selection in source
121                 //int64_t endsource_in_selection = startsource + length; // end of source in selection
122                 int64_t length_in_selection = length;             // length of edit in selection
123 //printf("Edit::copy 2\n");
124
125                 if(startproject < start)
126                 {         // start is after start of edit in project
127                         int64_t length_difference = start - startproject;
128
129                         startsource_in_selection += length_difference;
130                         startproject_in_selection += length_difference;
131                         length_in_selection -= length_difference;
132                 }
133 //printf("Edit::copy 3\n");
134
135                 if(endproject > end)
136                 {         // end is before end of edit in project
137                         length_in_selection = end - startproject_in_selection;
138                 }
139
140 //printf("Edit::copy 4\n");
141                 if(file)    // only if not counting
142                 {
143                         file->tag.set_title("EDIT");
144                         file->tag.set_property("STARTSOURCE", startsource_in_selection);
145                         file->tag.set_property("CHANNEL", (int64_t)channel);
146                         file->tag.set_property("LENGTH", length_in_selection);
147                         file->tag.set_property("HARD_LEFT", hard_left);
148                         file->tag.set_property("HARD_RIGHT", hard_right);
149                         file->tag.set_property("COLOR", color);
150                         file->tag.set_property("GROUP_ID", group_id);
151                         if(user_title[0]) file->tag.set_property("USER_TITLE", user_title);
152 //printf("Edit::copy 5\n");
153
154                         copy_properties_derived(file, length_in_selection);
155
156                         file->append_tag();
157 //                      file->append_newline();
158 //printf("Edit::copy 6\n");
159
160                         if(nested_edl)
161                         {
162                                 file->tag.set_title("NESTED_EDL");
163                                 file->tag.set_property("SRC", nested_edl->path);
164                                 file->append_tag();
165                                 file->tag.set_title("/NESTED_EDL");
166                                 file->append_tag();
167                                 file->append_newline();
168                         }
169
170                         if(asset)
171                         {
172 //printf("Edit::copy 6 %s\n", asset->path);
173                                 char stored_path[BCTEXTLEN];
174                                 char asset_directory[BCTEXTLEN];
175                                 char output_directory[BCTEXTLEN];
176                                 FileSystem fs;
177
178 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
179                                 fs.extract_dir(asset_directory, asset->path);
180 //printf("Edit::copy %d %s\n", __LINE__, asset->path);
181
182                                 if(output_path)
183                                         fs.extract_dir(output_directory, output_path);
184                                 else
185                                         output_directory[0] = 0;
186 //printf("Edit::copy %s, %s %s, %s\n", asset->path, asset_directory, output_path, output_directory);
187
188                                 if(output_path && !strcmp(asset_directory, output_directory))
189                                         fs.extract_name(stored_path, asset->path);
190                                 else
191                                         strcpy(stored_path, asset->path);
192
193                                 file->tag.set_title("FILE");
194                                 file->tag.set_property("SRC", stored_path);
195                                 file->append_tag();
196                                 file->tag.set_title("/FILE");
197                                 file->append_tag();
198                         }
199
200                         if(transition && startsource_in_selection == startsource)
201                         {
202                                 transition->save_xml(file);
203                         }
204
205 //printf("Edit::copy 7\n");
206                         file->tag.set_title("/EDIT");
207                         file->append_tag();
208                         file->append_newline();
209 //printf("Edit::copy 8\n");
210                 }
211 //printf("Edit::copy 9\n");
212                 result = 1;
213         }
214         else
215         {
216                 result = 0;
217         }
218 //printf("Edit::copy 10\n");
219         return result;
220 }
221
222
223 int64_t Edit::get_source_end(int64_t default_)
224 {
225         return default_;
226 }
227
228 void Edit::insert_transition(char *title)
229 {
230 //printf("Edit::insert_transition this=%p title=%p title=%s\n", this, title, title);
231         delete transition;
232         transition = new Transition(edl, this, title,
233                 track->to_units(edl->session->default_transition_length, 1));
234 }
235
236 void Edit::detach_transition()
237 {
238         if(transition) delete transition;
239         transition = 0;
240 }
241
242 int Edit::silence()
243 {
244         return (track->data_type != TRACK_SUBTITLE ?
245                 asset || nested_edl :
246                 *((SEdit *)this)->get_text()) ? 0 : 1;
247 }
248
249 void Edit::set_selected(int v)
250 {
251         if( group_id )
252                 edl->tracks->set_group_selected(group_id, v);
253         else
254                 is_selected = v >= 0 ? v : !is_selected ? 1 : 0;
255 }
256
257 void Edit::copy_from(Edit *edit)
258 {
259         this->orig_id = edit->orig_id;
260         this->nested_edl = edl->nested_edls.get_nested(edit->nested_edl);
261         this->asset = edl->assets->update(edit->asset);
262         this->startsource = edit->startsource;
263         this->startproject = edit->startproject;
264         this->length = edit->length;
265         this->hard_left = edit->hard_left;
266         this->hard_right = edit->hard_right;
267         this->color = edit->color;
268         this->group_id = edit->group_id;
269         strcpy (this->user_title, edit->user_title);
270
271         if(edit->transition)
272         {
273                 if(!transition) transition = new Transition(edl,
274                         this,
275                         edit->transition->title,
276                         edit->transition->length);
277                 *this->transition = *edit->transition;
278         }
279         this->channel = edit->channel;
280 }
281
282 void Edit::clone_from(Edit *edit)
283 {
284         copy_from(edit);
285         edit->orig_id = edit->id;
286 }
287
288 void Edit::equivalent_output(Edit *edit, int64_t *result)
289 {
290 // End of edit changed
291         if(startproject + length != edit->startproject + edit->length)
292         {
293                 int64_t new_length = MIN(startproject + length,
294                         edit->startproject + edit->length);
295                 if(*result < 0 || new_length < *result)
296                         *result = new_length;
297         }
298
299         if(
300 // Different nested EDLs
301                 (edit->nested_edl && !nested_edl) ||
302                 (!edit->nested_edl && nested_edl) ||
303 // Different assets
304                 (edit->asset == 0 && asset != 0) ||
305                 (edit->asset != 0 && asset == 0) ||
306 // different transitions
307                 (edit->transition == 0 && transition != 0) ||
308                 (edit->transition != 0 && transition == 0) ||
309 // Position changed
310                 (startproject != edit->startproject) ||
311                 (startsource != edit->startsource) ||
312 // Transition changed
313                 (transition && edit->transition &&
314                         !transition->identical(edit->transition)) ||
315 // Asset changed
316                 (asset && edit->asset &&
317                         !asset->equivalent(*edit->asset, 1, 1, edl)) ||
318 // Nested EDL changed
319                 (nested_edl && edit->nested_edl &&
320                         strcmp(nested_edl->path, edit->nested_edl->path))
321                 )
322         {
323 // Start of edit changed
324                 if(*result < 0 || startproject < *result) *result = startproject;
325         }
326 }
327
328
329 void Edit::synchronize_params(Edit *edit)
330 {
331         copy_from(edit);
332 }
333
334
335 // Comparison for ResourcePixmap drawing
336 int Edit::identical(Edit &edit)
337 {
338         int result = (this->nested_edl == edit.nested_edl &&
339                 this->asset == edit.asset &&
340                 this->startsource == edit.startsource &&
341                 this->startproject == edit.startproject &&
342                 this->length == edit.length &&
343                 this->transition == edit.transition &&
344                 this->channel == edit.channel);
345         return result;
346 }
347
348 double Edit::frames_per_picon()
349 {
350         return Units::round(picon_w()) / frame_w();
351 }
352
353 double Edit::frame_w()
354 {
355         return track->from_units(1) *
356                 edl->session->sample_rate /
357                 edl->local_session->zoom_sample;
358 }
359
360 double Edit::picon_w()
361 {
362         int w = 0, h = 0;
363         if(asset) {
364                 w = asset->width;
365                 h = asset->height;
366         }
367         else if(nested_edl) {
368                 w = nested_edl->session->output_w;
369                 h = nested_edl->session->output_h;
370         }
371         return w>0 && h>0 ? ((double)edl->local_session->zoom_track*w)/h : 0;
372 }
373
374 int Edit::picon_h()
375 {
376         return edl->local_session->zoom_track;
377 }
378
379
380 int Edit::dump(FILE *fp)
381 {
382         fprintf(fp,"     EDIT %p\n", this); fflush(fp);
383         fprintf(fp,"      id %d, orig_id %d, nested_edl=%p %s asset=%p %s\n",
384                 id, orig_id, nested_edl, nested_edl ? nested_edl->path : "",
385                 asset, asset ? asset->path : "");
386         fflush(fp);
387         fprintf(fp,"      channel %d, color %08x, hard lt/rt %d/%d"
388                 " group_id %d, is_selected %d\n",
389                 channel, color, hard_left, hard_right, group_id, is_selected);
390         if( transition ) {
391                 fprintf(fp,"      TRANSITION %p\n", transition);
392                 transition->dump(fp);
393         }
394         fprintf(fp,"      startsource %jd startproject %jd length %jd\n",
395                 startsource, startproject, length);
396         fflush(fp);
397         return 0;
398 }
399
400 int Edit::load_properties(FileXML *file, int64_t &startproject)
401 {
402         startsource = file->tag.get_property("STARTSOURCE", (int64_t)0);
403         length = file->tag.get_property("LENGTH", (int64_t)0);
404         hard_left = file->tag.get_property("HARD_LEFT", (int64_t)0);
405         hard_right = file->tag.get_property("HARD_RIGHT", (int64_t)0);
406         color = file->tag.get_property("COLOR", 0);
407         group_id = file->tag.get_property("GROUP_ID", group_id);
408         user_title[0] = 0;
409         file->tag.get_property("USER_TITLE", user_title);
410         this->startproject = startproject;
411         load_properties_derived(file);
412         return 0;
413 }
414
415 void Edit::shift(int64_t difference)
416 {
417         startproject += difference;
418 }
419
420 void Edit::trim(int64_t difference)
421 {
422         length += difference;
423         if( startproject < 0 ) {
424                 if( (startsource+=startproject) < 0 ) startsource = 0;
425                 if( (length+=startproject) < 0 ) length = 0;
426                 startproject = 0;
427         }
428         if( startsource < 0 )
429                 startsource = 0;
430         int64_t src_len = get_source_end(INT64_MAX);
431         if( startsource + length > src_len ) {
432                 length = src_len - startsource;
433                 if( length < 0 ) length = 0;
434         }
435         if( length < 0 )
436                 length = 0;
437 }
438
439 int Edit::shift_start(int edit_mode, int64_t newposition, int64_t oldposition,
440         int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
441 {
442         int64_t cut_length = newposition - oldposition;
443         int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
444         if( cut_length > length )
445                 cut_length = length;
446         else if( cut_length < -length )
447                 cut_length = -length;
448
449         int64_t start = startproject, end = start + length;
450         Edit *prev = this->previous, *next = this->next;
451         int edits_moved = 0;
452
453         switch( edit_mode ) {
454         case MOVE_RIPPLE:
455                 edits_moved = 1;
456                 startsource += cut_length;
457                 cut_length = -cut_length;
458                 length += cut_length;
459                 for( Edit *edit=next; edit; edit=edit->next )
460                         edit->startproject += cut_length;
461                 break;
462         case MOVE_ROLL:
463                 if( prev && prev->length + cut_length < 0 )
464                         cut_length = -prev->length;
465                 if( prev ) prev->trim(cut_length);
466                 startproject += cut_length;
467                 startsource += cut_length;
468                 length -= cut_length;
469                 break;
470         case MOVE_SLIP:
471                 edits_moved = 1;
472                 startsource -= cut_length;
473                 break;
474         case MOVE_SLIDE:
475                 edits_moved = 1;
476                 if( prev && prev->length + cut_length < 0 )
477                         cut_length = -prev->length;
478                 if( next && next->length - cut_length < 0 )
479                         cut_length = next->length;
480                 if( prev ) prev->trim(cut_length);
481                 startproject += cut_length;
482                 if( next ) {
483                         next->startproject += cut_length;
484                         next->startsource += cut_length;
485                         next->trim(-cut_length);
486                 }
487                 break;
488         case MOVE_EDGE:
489                 edits_moved = 1;
490                 startsource -= cut_length;
491                 length += cut_length;
492                 for( Edit *edit=next; edit; edit=edit->next )
493                         edit->startproject += cut_length;
494                 break;
495         }
496         trim(0);
497         return follow_edits(start, end, cut_length, edits_moved, rest_moved,
498                 edit_labels, edit_autos, edit_plugins, trim_edits);
499 }
500
501 int Edit::shift_end(int edit_mode, int64_t newposition, int64_t oldposition,
502         int edit_labels, int edit_autos, int edit_plugins, Edits *trim_edits)
503 {
504         int64_t cut_length = newposition - oldposition;
505         int rest_moved = edit_mode == MOVE_RIPPLE || edit_mode == MOVE_EDGE ? 1 : 0;
506         if( cut_length > length ) {
507                  if( !rest_moved ) cut_length = length;
508         }
509         else if( cut_length < -length )
510                 cut_length = -length;
511         int64_t start = startproject, end = start + length;
512         Edit *prev = this->previous, *next = this->next;
513         int edits_moved = 0;
514
515         switch( edit_mode ) {
516         case MOVE_RIPPLE:
517         case MOVE_EDGE:
518                 length += cut_length;
519                 for( Edit *edit=next; edit; edit=edit->next )
520                         edit->startproject += cut_length;
521                 break;
522         case MOVE_ROLL:
523                 if( next && next->length - cut_length < 0 )
524                         cut_length = next->length;
525                 length += cut_length;
526                 if( next ) {
527                         next->startproject += cut_length;
528                         next->startsource += cut_length;
529                         next->trim(-cut_length);
530                 }
531                 break;
532         case MOVE_SLIP:
533                 edits_moved = 1;
534                 startsource -= cut_length;
535                 break;
536         case MOVE_SLIDE:
537                 edits_moved = 1;
538                 if( prev && prev->length + cut_length < 0 )
539                         cut_length = -prev->length;
540                 if( next && next->length - cut_length < 0 )
541                         cut_length = next->length;
542                 if( prev ) prev->trim(cut_length);
543                 startproject += cut_length;
544                 if( next ) {
545                         next->startproject += cut_length;
546                         next->startsource += cut_length;
547                         next->trim(-cut_length);
548                 }
549                 break;
550         }
551         trim(0);
552         return follow_edits(start, end, cut_length, edits_moved, rest_moved,
553                 edit_labels, edit_autos, edit_plugins, trim_edits);
554 }
555
556 int Edit::follow_edits(int64_t start, int64_t end, int64_t cut_length,
557                 int edits_moved, int rest_moved, int edit_labels, int edit_autos,
558                 int edit_plugins, Edits *trim_edits)
559 {
560         if( edits_moved && rest_moved ) {
561                 if( edit_labels ) {
562                         double cut_len = track->from_units(cut_length);
563                         double start_pos = edits->track->from_units(start);
564                         if( cut_len < 0 )
565                                 edits->edl->labels->clear(start_pos + cut_len, start_pos, 1);
566                         else if( cut_len > 0 )
567                                 edits->edl->labels->insert(start_pos, cut_len);
568                 }
569                 if( cut_length < 0 )
570                         track->clear(start + cut_length, start,
571                                 0, 0, edit_autos, edit_plugins, trim_edits);
572                 else if( cut_length > 0 ) {
573                         if( edit_autos )
574                                 track->shift_keyframes(start, cut_length);
575                         if( edit_plugins )
576                                 track->shift_effects(start, cut_length, 1, trim_edits);
577                 }
578         }
579         else if( edits_moved ) {
580                 if( edit_labels ) {
581                         double cut_len = track->from_units(cut_length);
582                         double start_pos = edits->track->from_units(start);
583                         edits->edl->labels->insert(start_pos, cut_len);
584                         double end_pos = edits->track->from_units(end);
585                         edits->edl->labels->insert(end_pos + cut_len, -cut_len);
586                 }
587                 if( edit_autos ) {
588                         if( cut_length > 0 ) {
589                                 track->clear(end, end+cut_length, 0, 0, 0, 1, 0);
590                                 track->shift_keyframes(start, cut_length);
591                         }
592                         else if( cut_length < 0 ) {
593                                 track->clear(start+cut_length, start, 0, 0, 0, 1, 0);
594                                 track->shift_keyframes(end+cut_length, -cut_length);
595                         }
596                 }
597                 if( edit_plugins ) {
598                         if( cut_length > 0 ) {
599                                 track->clear(end, end+cut_length, 0, 0, -1, 0, 0);
600                                 track->shift_effects(start, cut_length, 1, 0);
601                         }
602                         else if( cut_length < 0 ) {
603                                 track->clear(start+cut_length, start, 0, 0, -1, 0, 0);
604                                 track->shift_effects(end+cut_length, -cut_length, 1, 0);
605                         }
606                 }
607         }
608         else if( rest_moved ) {
609                 if( edit_labels ) {
610                         double cut_len = track->from_units(cut_length);
611                         double end_pos = edits->track->from_units(end);
612                         if( cut_len < 0 )
613                                 edits->edl->labels->clear(end_pos + cut_len, end_pos, 1);
614                         else if( cut_len > 0 )
615                                 edits->edl->labels->insert(end_pos, cut_len);
616                 }
617                 if( cut_length < 0 )
618                         track->clear(end + cut_length, end,
619                                 0, 0, edit_autos, edit_plugins, trim_edits);
620                 else if( cut_length > 0 ) {
621                         if( edit_autos )
622                                 track->shift_keyframes(end, cut_length);
623                         if( edit_plugins )
624                                 track->shift_effects(end, cut_length, 1, trim_edits);
625                 }
626         }
627         return 0;
628 }
629
630
631
632 int Edit::popup_transition(float view_start, float zoom_units, int cursor_x, int cursor_y)
633 {
634         int64_t left, right, left_unit, right_unit;
635         if(!transition) return 0;
636         get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
637
638         if(cursor_x > left && cursor_x < right)
639         {
640 //              transition->popup_transition(cursor_x, cursor_y);
641                 return 1;
642         }
643         return 0;
644 }
645
646 int Edit::select_handle(float view_start, float zoom_units, int cursor_x, int cursor_y, int64_t &selection)
647 {
648         int64_t left, right, left_unit, right_unit;
649         get_handle_parameters(left, right, left_unit, right_unit, view_start, zoom_units);
650
651         int64_t pixel1, pixel2;
652         pixel1 = left;
653         pixel2 = pixel1 + xS(10);
654
655 // test left edit
656 // cursor_x is faked in acanvas
657         if(cursor_x >= pixel1 && cursor_x <= pixel2)
658         {
659                 selection = left_unit;
660                 return 1;     // left handle
661         }
662
663         //int64_t endproject = startproject + length;
664         pixel2 = right;
665         pixel1 = pixel2 - xS(10);
666
667 // test right edit
668         if(cursor_x >= pixel1 && cursor_x <= pixel2)
669         {
670                 selection = right_unit;
671                 return 2;     // right handle
672         }
673         return 0;
674 }
675
676 void Edit::get_title(char *title)
677 {
678         if( user_title[0] ) {
679                 strcpy(title, user_title);
680                 return;
681         }
682         Indexable *idxbl = asset ? (Indexable*)asset : (Indexable*)nested_edl;
683         if( !idxbl ) {
684                 title[0] = 0;
685                 return;
686         }
687         FileSystem fs;
688         fs.extract_name(title, idxbl->path);
689         if( asset || track->data_type == TRACK_AUDIO ) {
690                 char number[BCSTRLEN];
691                 sprintf(number, " #%d", channel + 1);
692                 strcat(title, number);
693         }
694 }
695