style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / space_sequencer / sequencer_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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 Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_edit.c
27  *  \ingroup spseq
28  */
29
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40 #include <sys/types.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_threads.h"
48
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_context.h"
53 #include "BKE_global.h"
54 #include "BKE_main.h"
55 #include "BKE_sequencer.h"
56 #include "BKE_report.h"
57 #include "BKE_sound.h"
58 #include "BKE_movieclip.h"
59
60 #include "IMB_imbuf.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "RNA_define.h"
66 #include "RNA_enum_types.h"
67
68 /* for menu/popup icons etc etc*/
69
70 #include "ED_screen.h"
71 #include "ED_transform.h"
72 #include "ED_sequencer.h"
73
74 #include "UI_view2d.h"
75
76 /* own include */
77 #include "sequencer_intern.h"
78
79 /* XXX */
80 /* RNA Enums, used in multiple files */
81 EnumPropertyItem sequencer_prop_effect_types[] = {
82         {SEQ_CROSS, "CROSS", 0, "Crossfade", "Crossfade effect strip type"},
83         {SEQ_ADD, "ADD", 0, "Add", "Add effect strip type"},
84         {SEQ_SUB, "SUBTRACT", 0, "Subtract", "Subtract effect strip type"},
85         {SEQ_ALPHAOVER, "ALPHA_OVER", 0, "Alpha Over", "Alpha Over effect strip type"},
86         {SEQ_ALPHAUNDER, "ALPHA_UNDER", 0, "Alpha Under", "Alpha Under effect strip type"},
87         {SEQ_GAMCROSS, "GAMMA_CROSS", 0, "Gamma Cross", "Gamma Cross effect strip type"},
88         {SEQ_MUL, "MULTIPLY", 0, "Multiply", "Multiply effect strip type"},
89         {SEQ_OVERDROP, "OVER_DROP", 0, "Alpha Over Drop", "Alpha Over Drop effect strip type"},
90         {SEQ_PLUGIN, "PLUGIN", 0, "Plugin", "Plugin effect strip type"},
91         {SEQ_WIPE, "WIPE", 0, "Wipe", "Wipe effect strip type"},
92         {SEQ_GLOW, "GLOW", 0, "Glow", "Glow effect strip type"},
93         {SEQ_TRANSFORM, "TRANSFORM", 0, "Transform", "Transform effect strip type"},
94         {SEQ_COLOR, "COLOR", 0, "Color", "Color effect strip type"},
95         {SEQ_SPEED, "SPEED", 0, "Speed", "Color effect strip type"},
96         {SEQ_MULTICAM, "MULTICAM", 0, "Multicam Selector", ""},
97         {SEQ_ADJUSTMENT, "ADJUSTMENT", 0, "Adjustment Layer", ""},
98         {0, NULL, 0, NULL, NULL}
99 };
100
101 /* mute operator */
102
103 EnumPropertyItem prop_side_types[] = {
104         {SEQ_SIDE_LEFT, "LEFT", 0, "Left", ""},
105         {SEQ_SIDE_RIGHT, "RIGHT", 0, "Right", ""},
106         {SEQ_SIDE_BOTH, "BOTH", 0, "Both", ""},
107         {0, NULL, 0, NULL, NULL}
108 };
109
110 static EnumPropertyItem prop_side_lr_types[] = {
111         {SEQ_SIDE_LEFT, "LEFT", 0, "Left", ""},
112         {SEQ_SIDE_RIGHT, "RIGHT", 0, "Right", ""},
113         {0, NULL, 0, NULL, NULL}
114 };
115
116 typedef struct TransSeq {
117         int start, machine;
118         int startstill, endstill;
119         int startdisp, enddisp;
120         int startofs, endofs;
121         int anim_startofs, anim_endofs;
122         /* int final_left, final_right; */ /* UNUSED */
123         int len;
124 } TransSeq;
125
126 /* ********************************************************************** */
127
128 /* ***************** proxy job manager ********************** */
129
130 typedef struct ProxyBuildJob {
131         Scene *scene; 
132         struct Main * main;
133         ListBase queue;
134         int stop;
135 } ProxyJob;
136
137 static void proxy_freejob(void *pjv)
138 {
139         ProxyJob *pj= pjv;
140
141         BLI_freelistN(&pj->queue);
142
143         MEM_freeN(pj);
144 }
145
146 /* only this runs inside thread */
147 static void proxy_startjob(void *pjv, short *stop, short *do_update, float *progress)
148 {
149         ProxyJob *pj = pjv;
150         LinkData *link;
151
152         for (link = pj->queue.first; link; link = link->next) {
153                 struct SeqIndexBuildContext *context = link->data;
154
155                 seq_proxy_rebuild(context, stop, do_update, progress);
156         }
157
158         if (*stop) {
159                 pj->stop = 1;
160                 fprintf(stderr,  "Canceling proxy rebuild on users request...\n");
161         }
162 }
163
164 static void proxy_endjob(void *pjv)
165 {
166         ProxyJob *pj = pjv;
167         Editing *ed = seq_give_editing(pj->scene, FALSE);
168         LinkData *link;
169
170         for (link = pj->queue.first; link; link = link->next) {
171                 seq_proxy_rebuild_finish(link->data, pj->stop);
172         }
173
174         free_imbuf_seq(pj->scene, &ed->seqbase, FALSE, FALSE);
175
176         WM_main_add_notifier(NC_SCENE|ND_SEQUENCER, pj->scene);
177 }
178
179 static void seq_proxy_build_job(const bContext *C)
180 {
181         wmJob * steve;
182         ProxyJob *pj;
183         Scene *scene= CTX_data_scene(C);
184         Editing *ed = seq_give_editing(scene, FALSE);
185         ScrArea * sa= CTX_wm_area(C);
186         struct SeqIndexBuildContext *context;
187         LinkData *link;
188         Sequence * seq;
189
190         steve = WM_jobs_get(CTX_wm_manager(C), CTX_wm_window(C), sa, "Building Proxies", WM_JOB_PROGRESS);
191
192         pj = WM_jobs_get_customdata(steve);
193
194         if (!pj) {
195                 pj = MEM_callocN(sizeof(ProxyJob), "proxy rebuild job");
196         
197                 pj->scene= scene;
198                 pj->main = CTX_data_main(C);
199
200                 WM_jobs_customdata(steve, pj, proxy_freejob);
201                 WM_jobs_timer(steve, 0.1, NC_SCENE|ND_SEQUENCER, NC_SCENE|ND_SEQUENCER);
202                 WM_jobs_callbacks(steve, proxy_startjob, NULL, NULL, proxy_endjob);
203         }
204
205         SEQP_BEGIN(ed, seq) {
206                 if ((seq->flag & SELECT)) {
207                         context = seq_proxy_rebuild_context(pj->main, pj->scene, seq);
208                         link = BLI_genericNodeN(context);
209                         BLI_addtail(&pj->queue, link);
210                 }
211         }
212         SEQ_END
213
214         if (!WM_jobs_is_running(steve)) {
215                 G.afbreek = 0;
216                 WM_jobs_start(CTX_wm_manager(C), steve);
217         }
218
219         ED_area_tag_redraw(CTX_wm_area(C));
220 }
221
222 /* ********************************************************************** */
223
224 void seq_rectf(Sequence *seq, rctf *rectf)
225 {
226         if (seq->startstill) rectf->xmin = seq->start;
227         else rectf->xmin = seq->startdisp;
228         rectf->ymin = seq->machine+SEQ_STRIP_OFSBOTTOM;
229         if (seq->endstill) rectf->xmax = seq->start+seq->len;
230         else rectf->xmax = seq->enddisp;
231         rectf->ymax = seq->machine+SEQ_STRIP_OFSTOP;
232 }
233
234 static void UNUSED_FUNCTION(change_plugin_seq)(Scene *scene, char *str) /* called from fileselect */
235 {
236         Editing *ed= seq_give_editing(scene, FALSE);
237         struct SeqEffectHandle sh;
238         Sequence *last_seq= seq_active_get(scene);
239
240         if (last_seq==NULL || last_seq->type != SEQ_PLUGIN) return;
241
242         sh = get_sequence_effect(last_seq);
243         sh.free(last_seq);
244         sh.init_plugin(last_seq, str);
245
246         last_seq->machine = MAX3(last_seq->seq1->machine,
247                                  last_seq->seq2->machine,
248                                  last_seq->seq3->machine);
249
250         if ( seq_test_overlap(ed->seqbasep, last_seq) ) shuffle_seq(ed->seqbasep, last_seq, scene);
251         
252 }
253
254
255 void boundbox_seq(Scene *scene, rctf *rect)
256 {
257         Sequence *seq;
258         Editing *ed= seq_give_editing(scene, FALSE);
259         float min[2], max[2];
260
261         
262         if (ed==NULL) return;
263
264         min[0]= 0.0;
265         max[0]= EFRA+1;
266         min[1]= 0.0;
267         max[1]= 8.0;
268
269         seq= ed->seqbasep->first;
270         while (seq) {
271
272                 if ( min[0] > seq->startdisp-1) min[0]= seq->startdisp-1;
273                 if ( max[0] < seq->enddisp+1) max[0]= seq->enddisp+1;
274                 if ( max[1] < seq->machine+2) max[1]= seq->machine+2;
275
276                 seq= seq->next;
277         }
278
279         rect->xmin = min[0];
280         rect->xmax = max[0];
281         rect->ymin = min[1];
282         rect->ymax = max[1];
283
284 }
285
286 static int mouse_frame_side(View2D *v2d, short mouse_x, int frame ) 
287 {
288         int mval[2];
289         float mouseloc[2];
290         
291         mval[0]= mouse_x;
292         mval[1]= 0;
293         
294         /* choose the side based on which side of the playhead the mouse is on */
295         UI_view2d_region_to_view(v2d, mval[0], mval[1], &mouseloc[0], &mouseloc[1]);
296         
297         return mouseloc[0] > frame ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT;
298 }
299
300
301 Sequence *find_neighboring_sequence(Scene *scene, Sequence *test, int lr, int sel) 
302 {
303         /* sel - 0==unselected, 1==selected, -1==done care*/
304         Sequence *seq;
305         Editing *ed= seq_give_editing(scene, FALSE);
306
307         if (ed==NULL) return NULL;
308
309         if (sel>0) sel = SELECT;
310         
311         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
312                 if (    (seq!=test) &&
313                         (test->machine==seq->machine) &&
314                         ((sel == -1) || (sel && (seq->flag & SELECT)) || (sel==0 && (seq->flag & SELECT)==0)  ))
315                 {
316                         switch (lr) {
317                         case SEQ_SIDE_LEFT:
318                                 if (test->startdisp == (seq->enddisp)) {
319                                         return seq;
320                                 }
321                                 break;
322                         case SEQ_SIDE_RIGHT:
323                                 if (test->enddisp == (seq->startdisp)) {
324                                         return seq;
325                                 }
326                                 break;
327                         }
328                 }
329         }
330         return NULL;
331 }
332
333 static Sequence *find_next_prev_sequence(Scene *scene, Sequence *test, int lr, int sel) 
334 {
335         /* sel - 0==unselected, 1==selected, -1==done care*/
336         Sequence *seq,*best_seq = NULL;
337         Editing *ed= seq_give_editing(scene, FALSE);
338         
339         int dist, best_dist;
340         best_dist = MAXFRAME*2;
341
342         
343         if (ed==NULL) return NULL;
344
345         seq= ed->seqbasep->first;
346         while (seq) {
347                 if (            (seq!=test) &&
348                                 (test->machine==seq->machine) &&
349                                 (test->depth==seq->depth) &&
350                                 ((sel == -1) || (sel==(seq->flag & SELECT))))
351                 {
352                         dist = MAXFRAME*2;
353                         
354                         switch (lr) {
355                         case SEQ_SIDE_LEFT:
356                                 if (seq->enddisp <= test->startdisp) {
357                                         dist = test->enddisp - seq->startdisp;
358                                 }
359                                 break;
360                         case SEQ_SIDE_RIGHT:
361                                 if (seq->startdisp >= test->enddisp) {
362                                         dist = seq->startdisp - test->enddisp;
363                                 }
364                                 break;
365                         }
366                         
367                         if (dist==0) {
368                                 best_seq = seq;
369                                 break;
370                         }
371                         else if (dist < best_dist) {
372                                 best_dist = dist;
373                                 best_seq = seq;
374                         }
375                 }
376                 seq= seq->next;
377         }
378         return best_seq; /* can be null */
379 }
380
381
382 Sequence *find_nearest_seq(Scene *scene, View2D *v2d, int *hand, const int mval[2])
383 {
384         Sequence *seq;
385         Editing *ed= seq_give_editing(scene, FALSE);
386         float x, y;
387         float pixelx;
388         float handsize;
389         float displen;
390         *hand= SEQ_SIDE_NONE;
391
392         
393         if (ed==NULL) return NULL;
394         
395         pixelx = (v2d->cur.xmax - v2d->cur.xmin)/(v2d->mask.xmax - v2d->mask.xmin);
396
397         UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
398         
399         seq= ed->seqbasep->first;
400         
401         while (seq) {
402                 if (seq->machine == (int)y) {
403                         /* check for both normal strips, and strips that have been flipped horizontally */
404                         if ( ((seq->startdisp < seq->enddisp) && (seq->startdisp<=x && seq->enddisp>=x)) ||
405                                 ((seq->startdisp > seq->enddisp) && (seq->startdisp>=x && seq->enddisp<=x)) )
406                         {
407                                 if (seq_tx_test(seq)) {
408                                         
409                                         /* clamp handles to defined size in pixel space */
410                                         
411                                         handsize = seq->handsize;
412                                         displen = (float)abs(seq->startdisp - seq->enddisp);
413                                         
414                                         if (displen / pixelx > 16) { /* don't even try to grab the handles of small strips */
415                                                 /* Set the max value to handle to 1/3 of the total len when its less then 28.
416                                                  * This is important because otherwise selecting handles happens even when you click in the middle */
417                                                 
418                                                 if ((displen/3) < 30*pixelx) {
419                                                         handsize = displen/3;
420                                                 }
421                                                 else {
422                                                         CLAMP(handsize, 7*pixelx, 30*pixelx);
423                                                 }
424                                                 
425                                                 if ( handsize+seq->startdisp >=x )
426                                                         *hand= SEQ_SIDE_LEFT;
427                                                 else if ( -handsize+seq->enddisp <=x )
428                                                         *hand= SEQ_SIDE_RIGHT;
429                                         }
430                                 }
431                                 return seq;
432                         }
433                 }
434                 seq= seq->next;
435         }
436         return NULL;
437 }
438
439
440 static int seq_is_parent(Sequence *par, Sequence *seq)
441 {
442         return ((par->seq1 == seq) || (par->seq2 == seq) || (par->seq3 == seq));
443 }
444
445 static int seq_is_predecessor(Sequence *pred, Sequence *seq)
446 {
447         if (!pred) return 0;
448         if (pred == seq) return 0;
449         else if (seq_is_parent(pred, seq)) return 1;
450         else if (pred->seq1 && seq_is_predecessor(pred->seq1, seq)) return 1;
451         else if (pred->seq2 && seq_is_predecessor(pred->seq2, seq)) return 1;
452         else if (pred->seq3 && seq_is_predecessor(pred->seq3, seq)) return 1;
453
454         return 0;
455 }
456
457 void deselect_all_seq(Scene *scene)
458 {
459         Sequence *seq;
460         Editing *ed= seq_give_editing(scene, FALSE);
461
462         
463         if (ed==NULL) return;
464
465         SEQP_BEGIN(ed, seq) {
466                 seq->flag &= ~SEQ_ALLSEL;
467         }
468         SEQ_END
469                 
470 }
471
472 void recurs_sel_seq(Sequence *seqm)
473 {
474         Sequence *seq;
475
476         seq= seqm->seqbase.first;
477         while (seq) {
478
479                 if (seqm->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL)) seq->flag &= ~SEQ_ALLSEL;
480                 else if (seqm->flag & SELECT) seq->flag |= SELECT;
481                 else seq->flag &= ~SEQ_ALLSEL;
482
483                 if (seq->seqbase.first) recurs_sel_seq(seq);
484
485                 seq= seq->next;
486         }
487 }
488
489 int seq_effect_find_selected(Scene *scene, Sequence *activeseq, int type, Sequence **selseq1, Sequence **selseq2, Sequence **selseq3, const char **error_str)
490 {
491         Editing *ed = seq_give_editing(scene, FALSE);
492         Sequence *seq1= NULL, *seq2= NULL, *seq3= NULL, *seq;
493         
494         *error_str= NULL;
495
496         if (!activeseq)
497                 seq2= seq_active_get(scene);
498
499         for (seq=ed->seqbasep->first; seq; seq=seq->next) {
500                 if (seq->flag & SELECT) {
501                         if (seq->type == SEQ_SOUND && get_sequence_effect_num_inputs(type) != 0) {
502                                 *error_str= "Can't apply effects to audio sequence strips";
503                                 return 0;
504                         }
505                         if ((seq != activeseq) && (seq != seq2)) {
506                                                                 if (seq2 == NULL) seq2= seq;
507                                                                 else if (seq1 == NULL) seq1= seq;
508                                                                 else if (seq3 == NULL) seq3= seq;
509                                                                 else {
510                                                                         *error_str= "Can't apply effect to more than 3 sequence strips";
511                                                                         return 0;
512                                                                 }
513                         }
514                 }
515         }
516
517         /* make sequence selection a little bit more intuitive
518          * for 3 strips: the last-strip should be sequence3 */
519         if (seq3 != NULL && seq2 != NULL) {
520                 Sequence *tmp = seq2;
521                 seq2 = seq3;
522                 seq3 = tmp;
523         }
524         
525
526         switch(get_sequence_effect_num_inputs(type)) {
527         case 0:
528                 *selseq1 = *selseq2 = *selseq3 = NULL;
529                 return 1; /* succsess */
530         case 1:
531                 if (seq2==NULL) {
532                         *error_str= "Need at least one selected sequence strip";
533                         return 0;
534                 }
535                 if (seq1==NULL) seq1= seq2;
536                 if (seq3==NULL) seq3= seq2;
537         case 2:
538                 if (seq1==NULL || seq2==NULL) {
539                         *error_str= "Need 2 selected sequence strips";
540                         return 0;
541                 }
542                 if (seq3 == NULL) seq3= seq2;
543         }
544         
545         if (seq1==NULL && seq2==NULL && seq3==NULL) {
546                 *error_str= "TODO: in what cases does this happen?";
547                 return 0;
548         }
549         
550         *selseq1= seq1;
551         *selseq2= seq2;
552         *selseq3= seq3;
553
554         return 1;
555 }
556
557 static Sequence *del_seq_find_replace_recurs(Scene *scene, Sequence *seq)
558 {
559         Sequence *seq1, *seq2, *seq3;
560
561         /* try to find a replacement input sequence, and flag for later deletion if
562          * no replacement can be found */
563
564         if (!seq)
565                 return NULL;
566         else if (!(seq->type & SEQ_EFFECT))
567                 return ((seq->flag & SELECT)? NULL: seq);
568         else if (!(seq->flag & SELECT)) {
569                 /* try to find replacement for effect inputs */
570                 seq1= del_seq_find_replace_recurs(scene, seq->seq1);
571                 seq2= del_seq_find_replace_recurs(scene, seq->seq2);
572                 seq3= del_seq_find_replace_recurs(scene, seq->seq3);
573
574                 if (seq1==seq->seq1 && seq2==seq->seq2 && seq3==seq->seq3);
575                 else if (seq1 || seq2 || seq3) {
576                         seq->seq1= (seq1)? seq1: (seq2)? seq2: seq3;
577                         seq->seq2= (seq2)? seq2: (seq1)? seq1: seq3;
578                         seq->seq3= (seq3)? seq3: (seq1)? seq1: seq2;
579
580                         update_changed_seq_and_deps(scene, seq, 1, 1);
581                 }
582                 else
583                         seq->flag |= SELECT; /* mark for delete */
584         }
585
586         if (seq->flag & SELECT) {
587                 if ((seq1 = del_seq_find_replace_recurs(scene, seq->seq1))) return seq1;
588                 if ((seq2 = del_seq_find_replace_recurs(scene, seq->seq2))) return seq2;
589                 if ((seq3 = del_seq_find_replace_recurs(scene, seq->seq3))) return seq3;
590                 else return NULL;
591         }
592         else
593                 return seq;
594 }
595
596 static void recurs_del_seq_flag(Scene *scene, ListBase *lb, short flag, short deleteall)
597 {
598         Sequence *seq, *seqn;
599         Sequence *last_seq = seq_active_get(scene);
600
601         seq= lb->first;
602         while (seq) {
603                 seqn= seq->next;
604                 if ((seq->flag & flag) || deleteall) {
605                         BLI_remlink(lb, seq);
606                         if (seq==last_seq) seq_active_set(scene, NULL);
607                         if (seq->type==SEQ_META) recurs_del_seq_flag(scene, &seq->seqbase, flag, 1);
608                         seq_free_sequence(scene, seq);
609                 }
610                 seq= seqn;
611         }
612 }
613
614
615 static Sequence *cut_seq_hard(Scene *scene, Sequence * seq, int cutframe)
616 {
617         TransSeq ts;
618         Sequence *seqn = NULL;
619         int skip_dup = FALSE;
620
621         /* backup values */
622         ts.start= seq->start;
623         ts.machine= seq->machine;
624         ts.startstill= seq->startstill;
625         ts.endstill= seq->endstill;
626         ts.startdisp= seq->startdisp;
627         ts.enddisp= seq->enddisp;
628         ts.startofs= seq->startofs;
629         ts.endofs= seq->endofs;
630         ts.anim_startofs= seq->anim_startofs;
631         ts.anim_endofs= seq->anim_endofs;
632         ts.len= seq->len;
633         
634         /* First Strip! */
635         /* strips with extended stillfames before */
636         
637         if ((seq->startstill) && (cutframe <seq->start)) {
638                 /* don't do funny things with METAs ... */
639                 if (seq->type == SEQ_META) {
640                         skip_dup = TRUE;
641                         seq->startstill = seq->start - cutframe;
642                 }
643                 else {
644                         seq->start= cutframe -1;
645                         seq->startstill= cutframe -seq->startdisp -1;
646                         seq->anim_endofs += seq->len - 1;
647                         seq->endstill= 0;
648                 }
649         }
650         /* normal strip */
651         else if ((cutframe >=seq->start)&&(cutframe <=(seq->start+seq->len))) {
652                 seq->endofs = 0;
653                 seq->endstill = 0;
654                 seq->anim_endofs += (seq->start+seq->len) - cutframe;
655         }
656         /* strips with extended stillframes after */
657         else if (((seq->start+seq->len) < cutframe) && (seq->endstill)) {
658                 seq->endstill -= seq->enddisp - cutframe;
659                 /* don't do funny things with METAs ... */
660                 if (seq->type == SEQ_META) {
661                         skip_dup = TRUE;
662                 }
663         }
664         
665         reload_sequence_new_file(scene, seq, FALSE);
666         calc_sequence(scene, seq);
667
668         if (!skip_dup) {
669                 /* Duplicate AFTER the first change */
670                 seqn = seq_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
671         }
672         
673         if (seqn) { 
674                 seqn->flag |= SELECT;
675                         
676                 /* Second Strip! */
677                 /* strips with extended stillframes before */
678                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
679                         seqn->start = ts.start;
680                         seqn->startstill= ts.start- cutframe;
681                         seqn->anim_endofs = ts.anim_endofs;
682                         seqn->endstill = ts.endstill;
683                 }
684                 
685                 /* normal strip */
686                 else if ((cutframe>=seqn->start)&&(cutframe<=(seqn->start+seqn->len))) {
687                         seqn->start = cutframe;
688                         seqn->startstill = 0;
689                         seqn->startofs = 0;
690                         seqn->endofs = ts.endofs;
691                         seqn->anim_startofs += cutframe - ts.start;
692                         seqn->anim_endofs = ts.anim_endofs;
693                         seqn->endstill = ts.endstill;
694                 }                               
695                 
696                 /* strips with extended stillframes after */
697                 else if (((seqn->start+seqn->len) < cutframe) && (seqn->endstill)) {
698                         seqn->start = cutframe;
699                         seqn->startofs = 0;
700                         seqn->anim_startofs += ts.len-1;
701                         seqn->endstill = ts.enddisp - cutframe -1;
702                         seqn->startstill = 0;
703                 }
704                 
705                 reload_sequence_new_file(scene, seqn, FALSE);
706                 calc_sequence(scene, seqn);
707         }
708         return seqn;
709 }
710
711 static Sequence *cut_seq_soft(Scene *scene, Sequence * seq, int cutframe)
712 {
713         TransSeq ts;
714         Sequence *seqn = NULL;
715         int skip_dup = FALSE;
716
717         /* backup values */
718         ts.start= seq->start;
719         ts.machine= seq->machine;
720         ts.startstill= seq->startstill;
721         ts.endstill= seq->endstill;
722         ts.startdisp= seq->startdisp;
723         ts.enddisp= seq->enddisp;
724         ts.startofs= seq->startofs;
725         ts.endofs= seq->endofs;
726         ts.anim_startofs= seq->anim_startofs;
727         ts.anim_endofs= seq->anim_endofs;
728         ts.len= seq->len;
729         
730         /* First Strip! */
731         /* strips with extended stillfames before */
732         
733         if ((seq->startstill) && (cutframe <seq->start)) {
734                 /* don't do funny things with METAs ... */
735                 if (seq->type == SEQ_META) {
736                         skip_dup = TRUE;
737                         seq->startstill = seq->start - cutframe;
738                 }
739                 else {
740                         seq->start= cutframe -1;
741                         seq->startstill= cutframe -seq->startdisp -1;
742                         seq->endofs = seq->len - 1;
743                         seq->endstill= 0;
744                 }
745         }
746         /* normal strip */
747         else if ((cutframe >=seq->start)&&(cutframe <=(seq->start+seq->len))) {
748                 seq->endofs = (seq->start+seq->len) - cutframe;
749         }
750         /* strips with extended stillframes after */
751         else if (((seq->start+seq->len) < cutframe) && (seq->endstill)) {
752                 seq->endstill -= seq->enddisp - cutframe;
753                 /* don't do funny things with METAs ... */
754                 if (seq->type == SEQ_META) {
755                         skip_dup = TRUE;
756                 }
757         }
758         
759         calc_sequence(scene, seq);
760
761         if (!skip_dup) {
762                 /* Duplicate AFTER the first change */
763                 seqn = seq_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
764         }
765         
766         if (seqn) { 
767                 seqn->flag |= SELECT;
768                         
769                 /* Second Strip! */
770                 /* strips with extended stillframes before */
771                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
772                         seqn->start = ts.start;
773                         seqn->startstill= ts.start- cutframe;
774                         seqn->endofs = ts.endofs;
775                         seqn->endstill = ts.endstill;
776                 }
777                 
778                 /* normal strip */
779                 else if ((cutframe>=seqn->start)&&(cutframe<=(seqn->start+seqn->len))) {
780                         seqn->startstill = 0;
781                         seqn->startofs = cutframe - ts.start;
782                         seqn->endofs = ts.endofs;
783                         seqn->endstill = ts.endstill;
784                 }                               
785                 
786                 /* strips with extended stillframes after */
787                 else if (((seqn->start+seqn->len) < cutframe) && (seqn->endstill)) {
788                         seqn->start = cutframe - ts.len +1;
789                         seqn->startofs = ts.len-1;
790                         seqn->endstill = ts.enddisp - cutframe -1;
791                         seqn->startstill = 0;
792                 }
793                 
794                 calc_sequence(scene, seqn);
795         }
796         return seqn;
797 }
798
799
800 /* like duplicate, but only duplicate and cut overlapping strips,
801  * strips to the left of the cutframe are ignored and strips to the right are moved into the new list */
802 static int cut_seq_list(Scene *scene, ListBase *old, ListBase *new, int cutframe,
803                         Sequence * (*cut_seq)(Scene *, Sequence *, int))
804 {
805         int did_something = FALSE;
806         Sequence *seq, *seq_next_iter;
807         
808         seq= old->first;
809         
810         while (seq) {
811                 seq_next_iter = seq->next; /* we need this because we may remove seq */
812                 
813                 seq->tmp= NULL;
814                 if (seq->flag & SELECT) {
815                         if (cutframe > seq->startdisp && 
816                            cutframe < seq->enddisp) {
817                                 Sequence * seqn = cut_seq(scene, seq, cutframe);
818                                 if (seqn) {
819                                         BLI_addtail(new, seqn);
820                                 }
821                                 did_something = TRUE;
822                         }
823                         else if (seq->enddisp <= cutframe) {
824                                 /* do nothing */
825                         }
826                         else if (seq->startdisp >= cutframe) {
827                                 /* move into new list */
828                                 BLI_remlink(old, seq);
829                                 BLI_addtail(new, seq);
830                         }
831                 }
832                 seq = seq_next_iter;
833         }
834         return did_something;
835 }
836
837 static int insert_gap(Scene *scene, int gap, int cfra)
838 {
839         Sequence *seq;
840         Editing *ed= seq_give_editing(scene, FALSE);
841         int done=0;
842
843         /* all strips >= cfra are shifted */
844         
845         if (ed==NULL) return 0;
846
847         SEQP_BEGIN(ed, seq) {
848                 if (seq->startdisp >= cfra) {
849                         seq->start+= gap;
850                         calc_sequence(scene, seq);
851                         done= 1;
852                 }
853         }
854         SEQ_END
855
856         return done;
857 }
858
859 static void UNUSED_FUNCTION(touch_seq_files)(Scene *scene)
860 {
861         Sequence *seq;
862         Editing *ed= seq_give_editing(scene, FALSE);
863         char str[256];
864
865         /* touch all strips with movies */
866         
867         if (ed==NULL) return;
868
869         // XXX25 if(okee("Touch and print selected movies")==0) return;
870
871         WM_cursor_wait(1);
872
873         SEQP_BEGIN(ed, seq) {
874                 if (seq->flag & SELECT) {
875                         if (seq->type==SEQ_MOVIE) {
876                                 if (seq->strip && seq->strip->stripdata) {
877                                         BLI_make_file_string(G.main->name, str, seq->strip->dir, seq->strip->stripdata->name);
878                                         BLI_file_touch(seq->name);
879                                 }
880                         }
881
882                 }
883         }
884         SEQ_END
885
886         WM_cursor_wait(0);
887 }
888
889 #if 0
890 static void set_filter_seq(Scene *scene)
891 {
892         Sequence *seq;
893         Editing *ed= seq_give_editing(scene, FALSE);
894
895         
896         if (ed==NULL) return;
897
898         if (okee("Set Deinterlace")==0) return;
899
900         SEQP_BEGIN(ed, seq) {
901                 if (seq->flag & SELECT) {
902                         if (seq->type==SEQ_MOVIE) {
903                                 seq->flag |= SEQ_FILTERY;
904                                 reload_sequence_new_file(scene, seq, FALSE);
905                                 calc_sequence(scene, seq);
906                         }
907
908                 }
909         }
910         SEQ_END
911 }
912 #endif
913
914 static void UNUSED_FUNCTION(seq_remap_paths)(Scene *scene)
915 {
916         Sequence *seq, *last_seq = seq_active_get(scene);
917         Editing *ed= seq_give_editing(scene, FALSE);
918         char from[FILE_MAX], to[FILE_MAX], stripped[FILE_MAX];
919         
920         
921         if (last_seq==NULL) 
922                 return;
923         
924         BLI_strncpy(from, last_seq->strip->dir, sizeof(from));
925 // XXX  if (0==sbutton(from, 0, sizeof(from)-1, "From: "))
926 //              return;
927         
928         BLI_strncpy(to, from, sizeof(to));
929 // XXX  if (0==sbutton(to, 0, sizeof(to)-1, "To: "))
930 //              return;
931         
932         if (strcmp(to, from)==0)
933                 return;
934         
935         SEQP_BEGIN(ed, seq) {
936                 if (seq->flag & SELECT) {
937                         if (strncmp(seq->strip->dir, from, strlen(from))==0) {
938                                 printf("found %s\n", seq->strip->dir);
939                                 
940                                 /* strip off the beginning */
941                                 stripped[0]= 0;
942                                 BLI_strncpy(stripped, seq->strip->dir + strlen(from), FILE_MAX);
943                                 
944                                 /* new path */
945                                 BLI_snprintf(seq->strip->dir, sizeof(seq->strip->dir), "%s%s", to, stripped);
946                                 printf("new %s\n", seq->strip->dir);
947                         }
948                 }
949         }
950         SEQ_END
951                 
952 }
953
954
955 static void UNUSED_FUNCTION(no_gaps)(Scene *scene)
956 {
957         Editing *ed= seq_give_editing(scene, FALSE);
958         int cfra, first= 0, done;
959
960         
961         if (ed==NULL) return;
962
963         for (cfra= CFRA; cfra<=EFRA; cfra++) {
964                 if (first==0) {
965                         if ( evaluate_seq_frame(scene, cfra) ) first= 1;
966                 }
967                 else {
968                         done= 1;
969                         while ( evaluate_seq_frame(scene, cfra) == 0) {
970                                 done= insert_gap(scene, -1, cfra);
971                                 if (done==0) break;
972                         }
973                         if (done==0) break;
974                 }
975         }
976
977 }
978
979 #if 0
980 static int seq_get_snaplimit(View2D *v2d)
981 {
982         /* fake mouse coords to get the snap value
983          * a bit lazy but its only done once pre transform */
984         float xmouse, ymouse, x;
985         int mval[2] = {24, 0}; /* 24 screen px snap */
986         
987         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
988         x = xmouse;
989         mval[0] = 0;
990         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
991         return (int)(x - xmouse);
992 }
993 #endif
994
995 /* Operator functions */
996 int sequencer_edit_poll(bContext *C)
997 {
998         return (seq_give_editing(CTX_data_scene(C), FALSE) != NULL);
999 }
1000
1001 int sequencer_strip_poll(bContext *C)
1002 {
1003         Editing *ed;
1004         return (((ed= seq_give_editing(CTX_data_scene(C), FALSE)) != NULL) && (ed->act_seq != NULL));
1005 }
1006
1007 int sequencer_strip_has_path_poll(bContext *C)
1008 {
1009         Editing *ed;
1010         Sequence *seq;
1011         return (((ed= seq_give_editing(CTX_data_scene(C), FALSE)) != NULL) && ((seq= ed->act_seq) != NULL) && (SEQ_HAS_PATH(seq)));
1012 }
1013
1014 int sequencer_view_poll(bContext *C)
1015 {
1016         SpaceSeq *sseq= CTX_wm_space_seq(C);
1017         Editing *ed= seq_give_editing(CTX_data_scene(C), FALSE);
1018         if (ed && sseq && (sseq->mainb == SEQ_DRAW_IMG_IMBUF))
1019                 return 1;
1020
1021         return 0;
1022 }
1023
1024 /* snap operator*/
1025 static int sequencer_snap_exec(bContext *C, wmOperator *op)
1026 {
1027         Scene *scene= CTX_data_scene(C);
1028         
1029         Editing *ed= seq_give_editing(scene, FALSE);
1030         Sequence *seq;
1031         int snap_frame;
1032
1033         snap_frame= RNA_int_get(op->ptr, "frame");
1034
1035         /* also check metas */
1036         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1037                 if (seq->flag & SELECT && !(seq->depth==0 && seq->flag & SEQ_LOCK) &&
1038                         seq_tx_test(seq)) {
1039                         if ((seq->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL))==0) {
1040                                 /* simple but no anim update */
1041                                 /* seq->start= snap_frame-seq->startofs+seq->startstill; */
1042
1043                                 seq_translate(scene, seq, (snap_frame-seq->startofs+seq->startstill) - seq->start);
1044                         }
1045                         else {
1046                                 if (seq->flag & SEQ_LEFTSEL) {
1047                                         seq_tx_set_final_left(seq, snap_frame);
1048                                 }
1049                                 else { /* SEQ_RIGHTSEL */
1050                                         seq_tx_set_final_right(seq, snap_frame);
1051                                 }
1052                                 seq_tx_handle_xlimits(seq, seq->flag & SEQ_LEFTSEL, seq->flag & SEQ_RIGHTSEL);
1053                         }
1054                         calc_sequence(scene, seq);
1055                 }
1056         }
1057
1058         /* test for effects and overlap
1059          * don't use SEQP_BEGIN since that would be recursive */
1060         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1061                 if (seq->flag & SELECT && !(seq->depth==0 && seq->flag & SEQ_LOCK)) {
1062                         seq->flag &= ~SEQ_OVERLAP;
1063                         if ( seq_test_overlap(ed->seqbasep, seq) ) {
1064                                 shuffle_seq(ed->seqbasep, seq, scene);
1065                         }
1066                 }
1067                 else if (seq->type & SEQ_EFFECT) {
1068                         if (seq->seq1 && (seq->seq1->flag & SELECT)) 
1069                                 calc_sequence(scene, seq);
1070                         else if (seq->seq2 && (seq->seq2->flag & SELECT)) 
1071                                 calc_sequence(scene, seq);
1072                         else if (seq->seq3 && (seq->seq3->flag & SELECT)) 
1073                                 calc_sequence(scene, seq);
1074                 }
1075         }
1076
1077         /* as last: */
1078         sort_seq(scene);
1079         
1080         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1081         
1082         return OPERATOR_FINISHED;
1083 }
1084
1085 static int sequencer_snap_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1086 {
1087         Scene *scene = CTX_data_scene(C);
1088         
1089         int snap_frame;
1090         
1091         snap_frame= CFRA;
1092         
1093         RNA_int_set(op->ptr, "frame", snap_frame);
1094         return sequencer_snap_exec(C, op);
1095 }
1096
1097 void SEQUENCER_OT_snap(struct wmOperatorType *ot)
1098 {
1099         /* identifiers */
1100         ot->name = "Snap strips";
1101         ot->idname = "SEQUENCER_OT_snap";
1102         ot->description = "Frame where selected strips will be snapped";
1103         
1104         /* api callbacks */
1105         ot->invoke = sequencer_snap_invoke;
1106         ot->exec = sequencer_snap_exec;
1107         ot->poll = sequencer_edit_poll;
1108         
1109         /* flags */
1110         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1111         
1112         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be snapped", INT_MIN, INT_MAX);
1113 }
1114
1115 /* mute operator */
1116 static int sequencer_mute_exec(bContext *C, wmOperator *op)
1117 {
1118         Scene *scene= CTX_data_scene(C);
1119         Editing *ed= seq_give_editing(scene, FALSE);
1120         Sequence *seq;
1121         int selected;
1122
1123         selected= !RNA_boolean_get(op->ptr, "unselected");
1124         
1125         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1126                 if ((seq->flag & SEQ_LOCK)==0) {
1127                         if (selected) { /* mute unselected */
1128                                 if (seq->flag & SELECT)
1129                                         seq->flag |= SEQ_MUTE;
1130                         }
1131                         else {
1132                                 if ((seq->flag & SELECT)==0)
1133                                         seq->flag |= SEQ_MUTE;
1134                         }
1135                 }
1136         }
1137         
1138         seq_update_muting(ed);
1139         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1140         
1141         return OPERATOR_FINISHED;
1142 }
1143
1144 void SEQUENCER_OT_mute(struct wmOperatorType *ot)
1145 {
1146         /* identifiers */
1147         ot->name = "Mute Strips";
1148         ot->idname = "SEQUENCER_OT_mute";
1149         ot->description = "Mute selected strips";
1150         
1151         /* api callbacks */
1152         ot->exec = sequencer_mute_exec;
1153         ot->poll = sequencer_edit_poll;
1154         
1155         /* flags */
1156         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1157         
1158         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Mute unselected rather than selected strips");
1159 }
1160
1161
1162 /* unmute operator */
1163 static int sequencer_unmute_exec(bContext *C, wmOperator *op)
1164 {
1165         Scene *scene= CTX_data_scene(C);
1166         Editing *ed= seq_give_editing(scene, FALSE);
1167         Sequence *seq;
1168         int selected;
1169
1170         selected= !RNA_boolean_get(op->ptr, "unselected");
1171         
1172         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1173                 if ((seq->flag & SEQ_LOCK)==0) {
1174                         if (selected) { /* unmute unselected */
1175                                 if (seq->flag & SELECT)
1176                                         seq->flag &= ~SEQ_MUTE;
1177                         }
1178                         else {
1179                                 if ((seq->flag & SELECT)==0)
1180                                         seq->flag &= ~SEQ_MUTE;
1181                         }
1182                 }
1183         }
1184         
1185         seq_update_muting(ed);
1186         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1187         
1188         return OPERATOR_FINISHED;
1189 }
1190
1191 void SEQUENCER_OT_unmute(struct wmOperatorType *ot)
1192 {
1193         /* identifiers */
1194         ot->name = "Un-Mute Strips";
1195         ot->idname = "SEQUENCER_OT_unmute";
1196         ot->description = "Un-Mute unselected rather than selected strips";
1197         
1198         /* api callbacks */
1199         ot->exec = sequencer_unmute_exec;
1200         ot->poll = sequencer_edit_poll;
1201         
1202         /* flags */
1203         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1204         
1205         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "UnMute unselected rather than selected strips");
1206 }
1207
1208
1209 /* lock operator */
1210 static int sequencer_lock_exec(bContext *C, wmOperator *UNUSED(op))
1211 {
1212         Scene *scene= CTX_data_scene(C);
1213         Editing *ed= seq_give_editing(scene, FALSE);
1214         Sequence *seq;
1215
1216         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1217                 if (seq->flag & SELECT) {
1218                         seq->flag |= SEQ_LOCK;
1219                 }
1220         }
1221
1222         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1223
1224         return OPERATOR_FINISHED;
1225 }
1226
1227 void SEQUENCER_OT_lock(struct wmOperatorType *ot)
1228 {
1229         /* identifiers */
1230         ot->name = "Lock Strips";
1231         ot->idname = "SEQUENCER_OT_lock";
1232         ot->description = "Lock the active strip so that it can't be transformed";
1233         
1234         /* api callbacks */
1235         ot->exec = sequencer_lock_exec;
1236         ot->poll = sequencer_edit_poll;
1237         
1238         /* flags */
1239         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1240 }
1241
1242 /* unlock operator */
1243 static int sequencer_unlock_exec(bContext *C, wmOperator *UNUSED(op))
1244 {
1245         Scene *scene= CTX_data_scene(C);
1246         Editing *ed= seq_give_editing(scene, FALSE);
1247         Sequence *seq;
1248
1249         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1250                 if (seq->flag & SELECT) {
1251                         seq->flag &= ~SEQ_LOCK;
1252                 }
1253         }
1254
1255         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1256
1257         return OPERATOR_FINISHED;
1258 }
1259
1260 void SEQUENCER_OT_unlock(struct wmOperatorType *ot)
1261 {
1262         /* identifiers */
1263         ot->name = "UnLock Strips";
1264         ot->idname = "SEQUENCER_OT_unlock";
1265         ot->description = "Unlock the active strip so that it can't be transformed";
1266         
1267         /* api callbacks */
1268         ot->exec = sequencer_unlock_exec;
1269         ot->poll = sequencer_edit_poll;
1270         
1271         /* flags */
1272         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1273 }
1274
1275 /* reload operator */
1276 static int sequencer_reload_exec(bContext *C, wmOperator *UNUSED(op))
1277 {
1278         Scene *scene= CTX_data_scene(C);
1279         Editing *ed= seq_give_editing(scene, FALSE);
1280         Sequence *seq;
1281
1282         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1283                 if (seq->flag & SELECT) {
1284                         update_changed_seq_and_deps(scene, seq, 0, 1);
1285                 }
1286         }
1287
1288         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1289
1290         return OPERATOR_FINISHED;
1291 }
1292
1293 void SEQUENCER_OT_reload(struct wmOperatorType *ot)
1294 {
1295         /* identifiers */
1296         ot->name = "Reload Strips";
1297         ot->idname = "SEQUENCER_OT_reload";
1298         ot->description = "Reload strips in the sequencer";
1299         
1300         /* api callbacks */
1301         ot->exec = sequencer_reload_exec;
1302         ot->poll = sequencer_edit_poll;
1303         
1304         /* flags */
1305         ot->flag = OPTYPE_REGISTER; /* no undo, the data changed is stored outside 'main' */
1306 }
1307
1308 /* reload operator */
1309 static int sequencer_refresh_all_exec(bContext *C, wmOperator *UNUSED(op))
1310 {
1311         Scene *scene= CTX_data_scene(C);
1312         Editing *ed= seq_give_editing(scene, FALSE);
1313
1314         free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
1315
1316         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1317
1318         return OPERATOR_FINISHED;
1319 }
1320
1321 void SEQUENCER_OT_refresh_all(struct wmOperatorType *ot)
1322 {
1323         /* identifiers */
1324         ot->name = "Refresh Sequencer";
1325         ot->idname = "SEQUENCER_OT_refresh_all";
1326         ot->description = "Refresh the sequencer editor";
1327         
1328         /* api callbacks */
1329         ot->exec = sequencer_refresh_all_exec;
1330         ot->poll = sequencer_edit_poll;
1331 }
1332
1333 static int sequencer_reassign_inputs_exec(bContext *C, wmOperator *op)
1334 {
1335         Scene *scene= CTX_data_scene(C);
1336         Sequence *seq1, *seq2, *seq3, *last_seq = seq_active_get(scene);
1337         const char *error_msg;
1338
1339         if (!seq_effect_find_selected(scene, last_seq, last_seq->type, &seq1, &seq2, &seq3, &error_msg)) {
1340                 BKE_report(op->reports, RPT_ERROR, error_msg);
1341                 return OPERATOR_CANCELLED;
1342         }
1343         /* see reassigning would create a cycle */
1344         if (    seq_is_predecessor(seq1, last_seq) ||
1345                 seq_is_predecessor(seq2, last_seq) ||
1346                 seq_is_predecessor(seq3, last_seq)
1347         ) {
1348                 BKE_report(op->reports, RPT_ERROR, "Can't reassign inputs: no cycles allowed");
1349                 return OPERATOR_CANCELLED;
1350         }
1351
1352         last_seq->seq1 = seq1;
1353         last_seq->seq2 = seq2;
1354         last_seq->seq3 = seq3;
1355
1356         update_changed_seq_and_deps(scene, last_seq, 1, 1);
1357
1358         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1359
1360         return OPERATOR_FINISHED;
1361 }
1362
1363 static int sequencer_effect_poll(bContext *C)
1364 {
1365         Scene *scene= CTX_data_scene(C);
1366         Editing *ed= seq_give_editing(scene, FALSE);
1367
1368         if (ed) {
1369                 Sequence *last_seq= seq_active_get(scene);
1370                 if (last_seq && (last_seq->type & SEQ_EFFECT)) {
1371                         return 1;
1372                 }
1373         }
1374
1375         return 0;
1376 }
1377
1378 void SEQUENCER_OT_reassign_inputs(struct wmOperatorType *ot)
1379 {
1380         /* identifiers */
1381         ot->name = "Reassign Inputs";
1382         ot->idname = "SEQUENCER_OT_reassign_inputs";
1383         ot->description = "Reassign the inputs for the effect strip";
1384
1385         /* api callbacks */
1386         ot->exec = sequencer_reassign_inputs_exec;
1387         ot->poll = sequencer_effect_poll;
1388
1389         /* flags */
1390         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1391 }
1392
1393
1394 static int sequencer_swap_inputs_exec(bContext *C, wmOperator *op)
1395 {
1396         Scene *scene= CTX_data_scene(C);
1397         Sequence *seq, *last_seq = seq_active_get(scene);
1398
1399         if (last_seq->seq1==NULL || last_seq->seq2 == NULL) {
1400                 BKE_report(op->reports, RPT_ERROR, "No valid inputs to swap");
1401                 return OPERATOR_CANCELLED;
1402         }
1403
1404         seq = last_seq->seq1;
1405         last_seq->seq1 = last_seq->seq2;
1406         last_seq->seq2 = seq;
1407
1408         update_changed_seq_and_deps(scene, last_seq, 1, 1);
1409
1410         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1411
1412         return OPERATOR_FINISHED;
1413 }
1414 void SEQUENCER_OT_swap_inputs(struct wmOperatorType *ot)
1415 {
1416         /* identifiers */
1417         ot->name = "Swap Inputs";
1418         ot->idname = "SEQUENCER_OT_swap_inputs";
1419         ot->description = "Swap the first two inputs for the effect strip";
1420
1421         /* api callbacks */
1422         ot->exec = sequencer_swap_inputs_exec;
1423         ot->poll = sequencer_effect_poll;
1424
1425         /* flags */
1426         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1427 }
1428
1429
1430 /* cut operator */
1431 static EnumPropertyItem prop_cut_types[] = {
1432         {SEQ_CUT_SOFT, "SOFT", 0, "Soft", ""},
1433         {SEQ_CUT_HARD, "HARD", 0, "Hard", ""},
1434         {0, NULL, 0, NULL, NULL}
1435 };
1436
1437 static int sequencer_cut_exec(bContext *C, wmOperator *op)
1438 {
1439         Scene *scene= CTX_data_scene(C);
1440         Editing *ed= seq_give_editing(scene, FALSE);
1441         int cut_side, cut_hard, cut_frame;
1442
1443         ListBase newlist;
1444         int changed;
1445
1446         cut_frame= RNA_int_get(op->ptr, "frame");
1447         cut_hard= RNA_enum_get(op->ptr, "type");
1448         cut_side= RNA_enum_get(op->ptr, "side");
1449         
1450         newlist.first= newlist.last= NULL;
1451
1452         if (cut_hard==SEQ_CUT_HARD) {
1453                 changed = cut_seq_list(scene, ed->seqbasep, &newlist, cut_frame, cut_seq_hard);
1454         }
1455         else {
1456                 changed = cut_seq_list(scene, ed->seqbasep, &newlist, cut_frame, cut_seq_soft);
1457         }
1458         
1459         if (newlist.first) { /* got new strips ? */
1460                 Sequence *seq;
1461                 BLI_movelisttolist(ed->seqbasep, &newlist);
1462
1463                 if (cut_side != SEQ_SIDE_BOTH) {
1464                         SEQP_BEGIN(ed, seq) {
1465                                 if (cut_side==SEQ_SIDE_LEFT) {
1466                                         if ( seq->startdisp >= cut_frame ) {
1467                                                 seq->flag &= ~SEQ_ALLSEL;
1468                                         }
1469                                 }
1470                                 else {
1471                                         if ( seq->enddisp <= cut_frame ) {
1472                                                 seq->flag &= ~SEQ_ALLSEL;
1473                                         }
1474                                 }
1475                         }
1476                         SEQ_END;
1477                 }
1478                 /* as last: */
1479                 sort_seq(scene);
1480         }
1481
1482         if (changed) {
1483                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1484                 return OPERATOR_FINISHED;
1485         }
1486         else {
1487                 return OPERATOR_CANCELLED;
1488         }
1489 }
1490
1491
1492 static int sequencer_cut_invoke(bContext *C, wmOperator *op, wmEvent *event)
1493 {
1494         Scene *scene = CTX_data_scene(C);
1495         View2D *v2d= UI_view2d_fromcontext(C);
1496
1497         int cut_side= SEQ_SIDE_BOTH;
1498         int cut_frame= CFRA;
1499
1500         if (ED_operator_sequencer_active(C) && v2d)
1501                 cut_side= mouse_frame_side(v2d, event->mval[0], cut_frame);
1502         
1503         RNA_int_set(op->ptr, "frame", cut_frame);
1504         RNA_enum_set(op->ptr, "side", cut_side);
1505         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
1506
1507         return sequencer_cut_exec(C, op);
1508 }
1509
1510
1511 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
1512 {
1513         /* identifiers */
1514         ot->name = "Cut Strips";
1515         ot->idname = "SEQUENCER_OT_cut";
1516         ot->description = "Cut the selected strips";
1517         
1518         /* api callbacks */
1519         ot->invoke = sequencer_cut_invoke;
1520         ot->exec = sequencer_cut_exec;
1521         ot->poll = sequencer_edit_poll;
1522         
1523         /* flags */
1524         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1525         
1526         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
1527         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "The type of cut operation to perform on strips");
1528         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
1529 }
1530
1531 /* duplicate operator */
1532 static int apply_unique_name_cb(Sequence *seq, void *arg_pt)
1533 {
1534         Scene *scene= (Scene *)arg_pt;
1535         char name[sizeof(seq->name)-2];
1536
1537         strcpy(name, seq->name+2);
1538         seqbase_unique_name_recursive(&scene->ed->seqbase, seq);
1539         seq_dupe_animdata(scene, name, seq->name+2);
1540         return 1;
1541
1542 }
1543
1544 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
1545 {
1546         Scene *scene= CTX_data_scene(C);
1547         Editing *ed= seq_give_editing(scene, FALSE);
1548
1549         ListBase nseqbase= {NULL, NULL};
1550
1551         if (ed==NULL)
1552                 return OPERATOR_CANCELLED;
1553
1554         seqbase_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT);
1555
1556         if (nseqbase.first) {
1557                 Sequence * seq= nseqbase.first;
1558                 /* rely on the nseqbase list being added at the end */
1559                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
1560
1561                 for ( ; seq; seq= seq->next)
1562                         seq_recursive_apply(seq, apply_unique_name_cb, scene);
1563
1564                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1565                 return OPERATOR_FINISHED;
1566         }
1567
1568         return OPERATOR_CANCELLED;
1569 }
1570
1571 static int sequencer_add_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1572 {
1573         sequencer_add_duplicate_exec(C, op);
1574
1575         RNA_enum_set(op->ptr, "mode", TFM_TRANSLATION);
1576         WM_operator_name_call(C, "TRANSFORM_OT_transform", WM_OP_INVOKE_REGION_WIN, op->ptr);
1577
1578         return OPERATOR_FINISHED;
1579 }
1580
1581 void SEQUENCER_OT_duplicate(wmOperatorType *ot)
1582 {
1583         /* identifiers */
1584         ot->name = "Duplicate Strips";
1585         ot->idname = "SEQUENCER_OT_duplicate";
1586         ot->description = "Duplicate the selected strips";
1587         
1588         /* api callbacks */
1589         ot->invoke = sequencer_add_duplicate_invoke;
1590         ot->exec = sequencer_add_duplicate_exec;
1591         ot->poll = ED_operator_sequencer_active;
1592         
1593         /* flags */
1594         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1595         
1596         /* to give to transform */
1597         RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1598 }
1599
1600 /* delete operator */
1601 static int sequencer_delete_exec(bContext *C, wmOperator *UNUSED(op))
1602 {
1603         Scene *scene= CTX_data_scene(C);
1604         Editing *ed= seq_give_editing(scene, FALSE);
1605         Sequence *seq;
1606         MetaStack *ms;
1607         int nothingSelected = TRUE;
1608
1609         seq=seq_active_get(scene);
1610         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
1611                 nothingSelected = FALSE;
1612         }
1613         else {
1614                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1615                         if (seq->flag & SELECT) {
1616                                 nothingSelected = FALSE;
1617                                 break;
1618                         }
1619                 }
1620         }
1621
1622         if (nothingSelected)
1623                 return OPERATOR_FINISHED;
1624
1625         /* for effects, try to find a replacement input */
1626         for (seq=ed->seqbasep->first; seq; seq=seq->next)
1627                 if ((seq->type & SEQ_EFFECT) && !(seq->flag & SELECT))
1628                         del_seq_find_replace_recurs(scene, seq);
1629
1630         /* delete all selected strips */
1631         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
1632
1633         /* updates lengths etc */
1634         seq= ed->seqbasep->first;
1635         while (seq) {
1636                 calc_sequence(scene, seq);
1637                 seq= seq->next;
1638         }
1639
1640         /* free parent metas */
1641         ms= ed->metastack.last;
1642         while (ms) {
1643                 calc_sequence(scene, ms->parseq);
1644                 ms= ms->prev;
1645         }
1646
1647         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1648         
1649         return OPERATOR_FINISHED;
1650 }
1651
1652
1653 void SEQUENCER_OT_delete(wmOperatorType *ot)
1654 {
1655
1656         /* identifiers */
1657         ot->name = "Erase Strips";
1658         ot->idname = "SEQUENCER_OT_delete";
1659         ot->description = "Erase selected strips from the sequencer";
1660         
1661         /* api callbacks */
1662         ot->invoke = WM_operator_confirm;
1663         ot->exec = sequencer_delete_exec;
1664         ot->poll = sequencer_edit_poll;
1665         
1666         /* flags */
1667         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1668 }
1669
1670
1671 /* offset clear operator */
1672 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
1673 {
1674         Scene *scene= CTX_data_scene(C);
1675         Editing *ed= seq_give_editing(scene, FALSE);
1676         Sequence *seq;
1677
1678         /* for effects, try to find a replacement input */
1679         for (seq=ed->seqbasep->first; seq; seq=seq->next) {
1680                 if ((seq->type & SEQ_EFFECT)==0 && (seq->flag & SELECT)) {
1681                         seq->startofs= seq->endofs= seq->startstill= seq->endstill= 0;
1682                 }
1683         }
1684
1685         /* updates lengths etc */
1686         seq= ed->seqbasep->first;
1687         while (seq) {
1688                 calc_sequence(scene, seq);
1689                 seq= seq->next;
1690         }
1691
1692         for (seq=ed->seqbasep->first; seq; seq=seq->next) {
1693                 if ((seq->type & SEQ_EFFECT)==0 && (seq->flag & SELECT)) {
1694                         if (seq_test_overlap(ed->seqbasep, seq)) {
1695                                 shuffle_seq(ed->seqbasep, seq, scene);
1696                         }
1697                 }
1698         }
1699
1700         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1701
1702         return OPERATOR_FINISHED;
1703 }
1704
1705
1706 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
1707 {
1708
1709         /* identifiers */
1710         ot->name = "Clear Strip Offset";
1711         ot->idname = "SEQUENCER_OT_offset_clear";
1712         ot->description = "Clear strip offsets from the start and end frames";
1713
1714         /* api callbacks */
1715         ot->exec = sequencer_offset_clear_exec;
1716         ot->poll = sequencer_edit_poll;
1717
1718         /* flags */
1719         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1720 }
1721
1722
1723 /* separate_images operator */
1724 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1725 {
1726         Scene *scene= CTX_data_scene(C);
1727         Editing *ed= seq_give_editing(scene, FALSE);
1728         
1729         Sequence *seq, *seq_new;
1730         Strip *strip_new;
1731         StripElem *se, *se_new;
1732         int start_ofs, cfra, frame_end;
1733         int step= RNA_int_get(op->ptr, "length");
1734
1735         seq= ed->seqbasep->first; /* poll checks this is valid */
1736
1737         while (seq) {
1738                 if ((seq->flag & SELECT) && (seq->type == SEQ_IMAGE) && (seq->len > 1)) {
1739                         /* remove seq so overlap tests don't conflict,
1740                          * see seq_free_sequence below for the real free'ing */
1741                         BLI_remlink(ed->seqbasep, seq);
1742                         /* if(seq->ipo) seq->ipo->id.us--; */
1743                         /* XXX, remove fcurve and assign to split image strips */
1744
1745                         start_ofs = cfra = seq_tx_get_final_left(seq, 0);
1746                         frame_end = seq_tx_get_final_right(seq, 0);
1747
1748                         while (cfra < frame_end) {
1749                                 /* new seq */
1750                                 se = give_stripelem(seq, cfra);
1751
1752                                 seq_new= seq_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
1753                                 BLI_addtail(ed->seqbasep, seq_new);
1754
1755                                 seq_new->start= start_ofs;
1756                                 seq_new->type= SEQ_IMAGE;
1757                                 seq_new->len = 1;
1758                                 seq_new->endstill = step-1;
1759
1760                                 /* new strip */
1761                                 strip_new= seq_new->strip;
1762                                 strip_new->us= 1;
1763
1764                                 /* new stripdata */
1765                                 se_new= strip_new->stripdata;
1766                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
1767                                 calc_sequence(scene, seq_new);
1768
1769                                 if (step > 1) {
1770                                         seq_new->flag &= ~SEQ_OVERLAP;
1771                                         if (seq_test_overlap(ed->seqbasep, seq_new)) {
1772                                                 shuffle_seq(ed->seqbasep, seq_new, scene);
1773                                         }
1774                                 }
1775
1776                                 /* XXX, COPY FCURVES */
1777
1778                                 cfra++;
1779                                 start_ofs += step;
1780                         }
1781
1782                         seq_free_sequence(scene, seq);
1783                         seq = seq->next;
1784                 }
1785                 else {
1786                         seq = seq->next;
1787                 }
1788         }
1789
1790         /* as last: */
1791         sort_seq(scene);
1792         
1793         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1794
1795         return OPERATOR_FINISHED;
1796 }
1797
1798
1799 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
1800 {
1801         /* identifiers */
1802         ot->name = "Separate Images";
1803         ot->idname = "SEQUENCER_OT_images_separate";
1804         ot->description = "On image sequence strips, it returns a strip for each image";
1805         
1806         /* api callbacks */
1807         ot->exec = sequencer_separate_images_exec;
1808         ot->poll = sequencer_edit_poll;
1809         
1810         /* flags */
1811         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1812
1813         RNA_def_int(ot->srna, "length", 1, 1, 1000, "Length", "Length of each frame", 1, INT_MAX);
1814 }
1815
1816
1817 /* META Operators */
1818
1819 /* separate_meta_toggle operator */
1820 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1821 {
1822         Scene *scene= CTX_data_scene(C);
1823         Editing *ed= seq_give_editing(scene, FALSE);
1824         Sequence *last_seq= seq_active_get(scene);
1825         MetaStack *ms;
1826
1827         if (last_seq && last_seq->type==SEQ_META && last_seq->flag & SELECT) {
1828                 /* Enter Metastrip */
1829                 ms= MEM_mallocN(sizeof(MetaStack), "metastack");
1830                 BLI_addtail(&ed->metastack, ms);
1831                 ms->parseq= last_seq;
1832                 ms->oldbasep= ed->seqbasep;
1833
1834                 ed->seqbasep= &last_seq->seqbase;
1835
1836                 seq_active_set(scene, NULL);
1837
1838         }
1839         else {
1840                 /* Exit Metastrip (if possible) */
1841
1842                 Sequence *seq;
1843
1844                 if (ed->metastack.first==NULL)
1845                         return OPERATOR_CANCELLED;
1846
1847                 ms= ed->metastack.last;
1848                 BLI_remlink(&ed->metastack, ms);
1849
1850                 ed->seqbasep= ms->oldbasep;
1851
1852                 /* recalc all: the meta can have effects connected to it */
1853                 for (seq= ed->seqbasep->first; seq; seq= seq->next)
1854                         calc_sequence(scene, seq);
1855
1856                 seq_active_set(scene, ms->parseq);
1857
1858                 ms->parseq->flag |= SELECT;
1859                 recurs_sel_seq(ms->parseq);
1860
1861                 MEM_freeN(ms);
1862
1863         }
1864
1865         seq_update_muting(ed);
1866         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1867
1868         return OPERATOR_FINISHED;
1869 }
1870
1871 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
1872 {
1873         /* identifiers */
1874         ot->name = "Toggle Meta Strip";
1875         ot->idname = "SEQUENCER_OT_meta_toggle";
1876         ot->description = "Toggle a metastrip (to edit enclosed strips)";
1877         
1878         /* api callbacks */
1879         ot->exec = sequencer_meta_toggle_exec;
1880         ot->poll = sequencer_edit_poll;
1881         
1882         /* flags */
1883         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1884 }
1885
1886
1887 /* separate_meta_make operator */
1888 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
1889 {
1890         Scene *scene= CTX_data_scene(C);
1891         Editing *ed= seq_give_editing(scene, FALSE);
1892         
1893         Sequence *seq, *seqm, *next, *last_seq = seq_active_get(scene);
1894         int channel_max= 1;
1895
1896         if (seqbase_isolated_sel_check(ed->seqbasep)==FALSE) {
1897                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
1898                 return OPERATOR_CANCELLED;
1899         }
1900
1901         /* remove all selected from main list, and put in meta */
1902
1903         seqm= alloc_sequence(ed->seqbasep, 1, 1); /* channel number set later */
1904         strcpy(seqm->name+2, "MetaStrip");
1905         seqm->type= SEQ_META;
1906         seqm->flag= SELECT;
1907
1908         seq= ed->seqbasep->first;
1909         while (seq) {
1910                 next= seq->next;
1911                 if (seq!=seqm && (seq->flag & SELECT)) {
1912                         channel_max= MAX2(seq->machine, channel_max);
1913                         BLI_remlink(ed->seqbasep, seq);
1914                         BLI_addtail(&seqm->seqbase, seq);
1915                 }
1916                 seq= next;
1917         }
1918         seqm->machine= last_seq ? last_seq->machine : channel_max;
1919         calc_sequence(scene, seqm);
1920
1921         seqm->strip= MEM_callocN(sizeof(Strip), "metastrip");
1922         seqm->strip->us= 1;
1923         
1924         seq_active_set(scene, seqm);
1925
1926         if ( seq_test_overlap(ed->seqbasep, seqm) ) shuffle_seq(ed->seqbasep, seqm, scene);
1927
1928         seq_update_muting(ed);
1929
1930         seqbase_unique_name_recursive(&scene->ed->seqbase, seqm);
1931
1932         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
1933
1934         return OPERATOR_FINISHED;
1935 }
1936
1937 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
1938 {
1939         /* identifiers */
1940         ot->name = "Make Meta Strip";
1941         ot->idname = "SEQUENCER_OT_meta_make";
1942         ot->description = "Group selected strips into a metastrip";
1943         
1944         /* api callbacks */
1945         ot->invoke = WM_operator_confirm;
1946         ot->exec = sequencer_meta_make_exec;
1947         ot->poll = sequencer_edit_poll;
1948         
1949         /* flags */
1950         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1951 }
1952
1953
1954 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
1955 {
1956         if (seq == seqm) return 1;
1957         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
1958         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
1959         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
1960         else return 0;
1961 }
1962
1963 /* separate_meta_make operator */
1964 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
1965 {
1966         Scene *scene= CTX_data_scene(C);
1967         Editing *ed= seq_give_editing(scene, FALSE);
1968
1969         Sequence *seq, *last_seq = seq_active_get(scene); /* last_seq checks ed==NULL */
1970
1971         if (last_seq==NULL || last_seq->type!=SEQ_META)
1972                 return OPERATOR_CANCELLED;
1973
1974         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
1975
1976         last_seq->seqbase.first= NULL;
1977         last_seq->seqbase.last= NULL;
1978
1979         BLI_remlink(ed->seqbasep, last_seq);
1980         seq_free_sequence(scene, last_seq);
1981
1982         /* emtpy meta strip, delete all effects depending on it */
1983         for (seq=ed->seqbasep->first; seq; seq=seq->next)
1984                 if ((seq->type & SEQ_EFFECT) && seq_depends_on_meta(seq, last_seq))
1985                         seq->flag |= SEQ_FLAG_DELETE;
1986
1987         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
1988
1989         /* test for effects and overlap
1990          * don't use SEQP_BEGIN since that would be recursive */
1991         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
1992                 if (seq->flag & SELECT) {
1993                         seq->flag &= ~SEQ_OVERLAP;
1994                         if (seq_test_overlap(ed->seqbasep, seq)) {
1995                                 shuffle_seq(ed->seqbasep, seq, scene);
1996                         }
1997                 }
1998         }
1999
2000         sort_seq(scene);
2001         seq_update_muting(ed);
2002
2003         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2004
2005         return OPERATOR_FINISHED;
2006 }
2007
2008 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2009 {
2010         /* identifiers */
2011         ot->name = "UnMeta Strip";
2012         ot->idname = "SEQUENCER_OT_meta_separate";
2013         ot->description = "Put the contents of a metastrip back in the sequencer";
2014         
2015         /* api callbacks */
2016         ot->invoke = WM_operator_confirm;
2017         ot->exec = sequencer_meta_separate_exec;
2018         ot->poll = sequencer_edit_poll;
2019         
2020         /* flags */
2021         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2022 }
2023
2024 /* view_all operator */
2025 static int sequencer_view_all_exec(bContext *C, wmOperator *UNUSED(op))
2026 {
2027         //Scene *scene= CTX_data_scene(C);
2028         bScreen *sc= CTX_wm_screen(C);
2029         ScrArea *area= CTX_wm_area(C);
2030         //ARegion *ar= CTX_wm_region(C);
2031         View2D *v2d= UI_view2d_fromcontext(C);
2032
2033         v2d->cur= v2d->tot;
2034         UI_view2d_curRect_validate(v2d);
2035         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2036         
2037         ED_area_tag_redraw(CTX_wm_area(C));
2038         return OPERATOR_FINISHED;
2039 }
2040
2041 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2042 {
2043         /* identifiers */
2044         ot->name = "View All";
2045         ot->idname = "SEQUENCER_OT_view_all";
2046         ot->description = "View all the strips in the sequencer";
2047         
2048         /* api callbacks */
2049         ot->exec = sequencer_view_all_exec;
2050         ot->poll = ED_operator_sequencer_active;
2051         
2052         /* flags */
2053         ot->flag = OPTYPE_REGISTER;
2054 }
2055
2056 /* view_all operator */
2057 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2058 {
2059         bScreen *sc= CTX_wm_screen(C);
2060         ScrArea *area= CTX_wm_area(C);
2061 #if 0
2062         ARegion *ar= CTX_wm_region(C);
2063         SpaceSeq *sseq= area->spacedata.first;
2064         Scene *scene= CTX_data_scene(C);
2065 #endif
2066         View2D *v2d= UI_view2d_fromcontext(C);
2067
2068         v2d->cur= v2d->tot;
2069         UI_view2d_curRect_validate(v2d);
2070         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2071         
2072 #if 0
2073         /* Like zooming on an image view */
2074         float zoomX, zoomY;
2075         int width, height, imgwidth, imgheight;
2076
2077         width = ar->winx;
2078         height = ar->winy;
2079
2080         seq_reset_imageofs(sseq);
2081
2082         imgwidth= (scene->r.size*scene->r.xsch)/100;
2083         imgheight= (scene->r.size*scene->r.ysch)/100;
2084
2085         /* Apply aspect, dosnt need to be that accurate */
2086         imgwidth= (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2087
2088         if (((imgwidth >= width) || (imgheight >= height)) &&
2089                 ((width > 0) && (height > 0))) {
2090
2091                 /* Find the zoom value that will fit the image in the image space */
2092                 zoomX = ((float)width) / ((float)imgwidth);
2093                 zoomY = ((float)height) / ((float)imgheight);
2094                 sseq->zoom= (zoomX < zoomY) ? zoomX : zoomY;
2095
2096                 sseq->zoom = 1.0f / power_of_2(1/ MIN2(zoomX, zoomY) );
2097         }
2098         else {
2099                 sseq->zoom= 1.0f;
2100         }
2101 #endif
2102
2103         ED_area_tag_redraw(CTX_wm_area(C));
2104         return OPERATOR_FINISHED;
2105 }
2106
2107 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2108 {
2109         /* identifiers */
2110         ot->name = "View All";
2111         ot->idname = "SEQUENCER_OT_view_all_preview";
2112         ot->description = "Zoom preview to fit in the area";
2113         
2114         /* api callbacks */
2115         ot->exec = sequencer_view_all_preview_exec;
2116         ot->poll = ED_operator_sequencer_active;
2117         
2118         /* flags */
2119         ot->flag = OPTYPE_REGISTER;
2120 }
2121
2122
2123 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2124 {
2125         RenderData *r= &CTX_data_scene(C)->r;
2126         View2D *v2d= UI_view2d_fromcontext(C);
2127
2128         float ratio= RNA_float_get(op->ptr, "ratio");
2129
2130         float winx= (int)(r->size * r->xsch)/100;
2131         float winy= (int)(r->size * r->ysch)/100;
2132
2133         float facx= (v2d->mask.xmax - v2d->mask.xmin) / winx;
2134         float facy= (v2d->mask.ymax - v2d->mask.ymin) / winy;
2135
2136         BLI_resize_rctf(&v2d->cur, (int)(winx*facx*ratio) + 1, (int)(winy*facy*ratio) + 1);
2137
2138         ED_region_tag_redraw(CTX_wm_region(C));
2139
2140         return OPERATOR_FINISHED;
2141 }
2142
2143 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2144 {
2145         /* identifiers */
2146         ot->name = "Sequencer View Zoom Ratio";
2147         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2148         ot->description = "Change zoom ratio of sequencer preview";
2149
2150         /* api callbacks */
2151         ot->exec = sequencer_view_zoom_ratio_exec;
2152         ot->poll = ED_operator_sequencer_active;
2153
2154         /* properties */
2155         RNA_def_float(ot->srna, "ratio", 1.0f, 0.0f, FLT_MAX,
2156                 "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2157 }
2158
2159
2160 #if 0
2161 static EnumPropertyItem view_type_items[] = {
2162                 {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2163                 {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2164                 {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2165                 {0, NULL, 0, NULL, NULL}};
2166 #endif
2167
2168 /* view_all operator */
2169 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2170 {
2171         SpaceSeq *sseq= (SpaceSeq *)CTX_wm_space_data(C);
2172
2173         sseq->view++;
2174         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2175
2176         ED_area_tag_refresh(CTX_wm_area(C));
2177
2178         return OPERATOR_FINISHED;
2179 }
2180
2181 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2182 {
2183         /* identifiers */
2184         ot->name = "View Toggle";
2185         ot->idname = "SEQUENCER_OT_view_toggle";
2186         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2187         
2188         /* api callbacks */
2189         ot->exec = sequencer_view_toggle_exec;
2190         ot->poll = ED_operator_sequencer_active;
2191         
2192         /* flags */
2193         ot->flag = OPTYPE_REGISTER;
2194 }
2195
2196
2197 /* view_selected operator */
2198 static int sequencer_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
2199 {
2200         Scene *scene= CTX_data_scene(C);
2201         View2D *v2d= UI_view2d_fromcontext(C);
2202         ScrArea *area= CTX_wm_area(C);
2203         bScreen *sc= CTX_wm_screen(C);
2204         Editing *ed= seq_give_editing(scene, FALSE);
2205         Sequence *seq;
2206
2207         int xmin=  MAXFRAME*2;
2208         int xmax= -MAXFRAME*2;
2209         int ymin=  MAXSEQ+1;
2210         int ymax= 0;
2211         int orig_height;
2212         int ymid;
2213         int ymargin= 1;
2214         int xmargin= FPS;
2215
2216         if (ed==NULL)
2217                 return OPERATOR_CANCELLED;
2218
2219         for (seq=ed->seqbasep->first; seq; seq=seq->next) {
2220                 if (seq->flag & SELECT) {
2221                         xmin= MIN2(xmin, seq->startdisp);
2222                         xmax= MAX2(xmax, seq->enddisp);
2223
2224                         ymin= MIN2(ymin, seq->machine);
2225                         ymax= MAX2(ymax, seq->machine);
2226                 }
2227         }
2228
2229         if (ymax != 0) {
2230                 
2231                 xmax += xmargin;
2232                 xmin -= xmargin;
2233                 ymax += ymargin;
2234                 ymin -= ymargin;
2235
2236                 orig_height= v2d->cur.ymax - v2d->cur.ymin;
2237
2238                 v2d->cur.xmin = xmin;
2239                 v2d->cur.xmax = xmax;
2240
2241                 v2d->cur.ymin = ymin;
2242                 v2d->cur.ymax = ymax;
2243
2244                 /* only zoom out vertically */
2245                 if (orig_height > v2d->cur.ymax - v2d->cur.ymin) {
2246                         ymid= (v2d->cur.ymax + v2d->cur.ymin) / 2;
2247
2248                         v2d->cur.ymin = ymid - (orig_height/2);
2249                         v2d->cur.ymax = ymid + (orig_height/2);
2250                 }
2251
2252                 UI_view2d_curRect_validate(v2d);
2253                 UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2254
2255                 ED_area_tag_redraw(CTX_wm_area(C));
2256         }
2257         
2258         return OPERATOR_FINISHED;
2259 }
2260
2261 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2262 {
2263         /* identifiers */
2264         ot->name = "View Selected";
2265         ot->idname = "SEQUENCER_OT_view_selected";
2266         ot->description = "Zoom the sequencer on the selected strips";
2267         
2268         /* api callbacks */
2269         ot->exec = sequencer_view_selected_exec;
2270         ot->poll = ED_operator_sequencer_active;
2271         
2272         /* flags */
2273         ot->flag = OPTYPE_REGISTER;
2274 }
2275
2276
2277 static int find_next_prev_edit(Scene *scene, int cfra, int side)
2278 {
2279         Editing *ed= seq_give_editing(scene, FALSE);
2280         Sequence *seq,*best_seq = NULL,*frame_seq = NULL;
2281         
2282         int dist, best_dist;
2283         best_dist = MAXFRAME*2;
2284
2285         if (ed==NULL) return cfra;
2286         
2287         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
2288                 dist = MAXFRAME*2;
2289                         
2290                 switch (side) {
2291                         case SEQ_SIDE_LEFT:
2292                                 if (seq->startdisp < cfra) {
2293                                         dist = cfra - seq->startdisp;
2294                                 }
2295                                 break;
2296                         case SEQ_SIDE_RIGHT:
2297                                 if (seq->startdisp > cfra) {
2298                                         dist = seq->startdisp - cfra;
2299                                 }
2300                                 else if (seq->startdisp == cfra) {
2301                                         frame_seq=seq;
2302                                 }
2303                                 break;
2304                 }
2305
2306                 if (dist < best_dist) {
2307                         best_dist = dist;
2308                         best_seq = seq;
2309                 }
2310         }
2311
2312         /* if no sequence to the right is found and the
2313          * frame is on the start of the last sequence,
2314          * move to the end of the last sequence */
2315         if (frame_seq) cfra = frame_seq->enddisp;
2316
2317         return best_seq ? best_seq->startdisp : cfra;
2318 }
2319
2320 static int next_prev_edit_internal(Scene *scene, int side)
2321 {
2322         int change=0;
2323         int cfra = CFRA;
2324         int nfra= find_next_prev_edit(scene, cfra, side);
2325         
2326         if (nfra != cfra) {
2327                 CFRA = nfra;
2328                 change= 1;
2329         }
2330
2331         return change;
2332 }
2333
2334 /* move frame to next edit point operator */
2335 static int sequencer_next_edit_exec(bContext *C, wmOperator *UNUSED(op))
2336 {
2337         Scene *scene= CTX_data_scene(C);
2338         
2339         if (!next_prev_edit_internal(scene, SEQ_SIDE_RIGHT))
2340                 return OPERATOR_CANCELLED;
2341
2342         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
2343
2344         return OPERATOR_FINISHED;
2345 }
2346
2347 void SEQUENCER_OT_next_edit(wmOperatorType *ot)
2348 {
2349         /* identifiers */
2350         ot->name = "Next Edit";
2351         ot->idname = "SEQUENCER_OT_next_edit";
2352         ot->description = "Move frame to next edit point";
2353         
2354         /* api callbacks */
2355         ot->exec = sequencer_next_edit_exec;
2356         ot->poll = sequencer_edit_poll;
2357         
2358         /* flags */
2359         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2360         
2361         /* properties */
2362 }
2363
2364 /* move frame to previous edit point operator */
2365 static int sequencer_previous_edit_exec(bContext *C, wmOperator *UNUSED(op))
2366 {
2367         Scene *scene= CTX_data_scene(C);
2368         
2369         if (!next_prev_edit_internal(scene, SEQ_SIDE_LEFT))
2370                 return OPERATOR_CANCELLED;
2371
2372         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
2373         
2374         return OPERATOR_FINISHED;
2375 }
2376
2377 void SEQUENCER_OT_previous_edit(wmOperatorType *ot)
2378 {
2379         /* identifiers */
2380         ot->name = "Previous Edit";
2381         ot->idname = "SEQUENCER_OT_previous_edit";
2382         ot->description = "Move frame to previous edit point";
2383         
2384         /* api callbacks */
2385         ot->exec = sequencer_previous_edit_exec;
2386         ot->poll = sequencer_edit_poll;
2387         
2388         /* flags */
2389         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2390         
2391         /* properties */
2392 }
2393
2394 static void swap_sequence(Scene* scene, Sequence* seqa, Sequence* seqb)
2395 {
2396         int gap = seqb->startdisp - seqa->enddisp;
2397         seqb->start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2398         calc_sequence(scene, seqb);
2399         seqa->start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2400         calc_sequence(scene, seqa);
2401 }
2402
2403 #if 0
2404 static Sequence* sequence_find_parent(Scene* scene, Sequence* child)
2405 {
2406         Editing *ed= seq_give_editing(scene, FALSE);
2407         Sequence *parent= NULL;
2408         Sequence *seq;
2409
2410         if (ed==NULL) return NULL;
2411
2412         for (seq= ed->seqbasep->first; seq; seq= seq->next) {
2413                 if ( (seq != child) && seq_is_parent(seq, child) ) {
2414                         parent = seq;
2415                         break;
2416                 }
2417         }
2418
2419         return parent;
2420 }
2421 #endif
2422
2423 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2424 {
2425         Scene *scene= CTX_data_scene(C);
2426         Editing *ed= seq_give_editing(scene, FALSE);
2427         Sequence *active_seq = seq_active_get(scene);
2428         Sequence *seq, *iseq;
2429         int side= RNA_enum_get(op->ptr, "side");
2430
2431         if (active_seq==NULL) return OPERATOR_CANCELLED;
2432
2433         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2434         
2435         if (seq) {
2436                 
2437                 /* disallow effect strips */
2438                 if (get_sequence_effect_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2439                         return OPERATOR_CANCELLED;
2440                 if ((get_sequence_effect_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2441                         return OPERATOR_CANCELLED;
2442
2443                 switch (side) {
2444                         case SEQ_SIDE_LEFT: 
2445                                 swap_sequence(scene, seq, active_seq);
2446                                 break;
2447                         case SEQ_SIDE_RIGHT: 
2448                                 swap_sequence(scene, active_seq, seq);
2449                                 break;
2450                 }
2451
2452                 // XXX - should be a generic function
2453                 for (iseq= scene->ed->seqbasep->first; iseq; iseq= iseq->next) {
2454                         if ((iseq->type & SEQ_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2455                                 calc_sequence(scene, iseq);
2456                         }
2457                 }
2458
2459                 /* do this in a new loop since both effects need to be calculated first */
2460                 for (iseq= scene->ed->seqbasep->first; iseq; iseq= iseq->next) {
2461                         if ((iseq->type & SEQ_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2462                                 /* this may now overlap */
2463                                 if ( seq_test_overlap(ed->seqbasep, iseq) ) {
2464                                         shuffle_seq(ed->seqbasep, iseq, scene);
2465                                 }
2466                         }
2467                 }
2468
2469
2470
2471                 sort_seq(scene);
2472
2473                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2474
2475                 return OPERATOR_FINISHED;
2476         }
2477
2478         return OPERATOR_CANCELLED;
2479 }
2480
2481 void SEQUENCER_OT_swap(wmOperatorType *ot)
2482 {
2483         /* identifiers */
2484         ot->name = "Swap Strip";
2485         ot->idname = "SEQUENCER_OT_swap";
2486         ot->description = "Swap active strip with strip to the right or left";
2487         
2488         /* api callbacks */
2489         ot->exec = sequencer_swap_exec;
2490         ot->poll = sequencer_edit_poll;
2491         
2492         /* flags */
2493         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2494         
2495         /* properties */
2496         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2497 }
2498
2499 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2500 {
2501         int retval = OPERATOR_CANCELLED;
2502         Scene *scene= CTX_data_scene(C);
2503         Sequence *active_seq = seq_active_get(scene);
2504         StripElem *se = NULL;
2505
2506         if (active_seq==NULL)
2507                 return OPERATOR_CANCELLED;
2508
2509
2510         if (active_seq->strip) {
2511                 switch (active_seq->type) {
2512                 case SEQ_IMAGE:
2513                         se = give_stripelem(active_seq, scene->r.cfra);
2514                         break;
2515                 case SEQ_MOVIE:
2516                         se = active_seq->strip->stripdata;
2517                         break;
2518                 case SEQ_SCENE:
2519                 case SEQ_META:
2520                 case SEQ_RAM_SOUND:
2521                 case SEQ_HD_SOUND:
2522                 default:
2523                         break;
2524                 }
2525         }
2526
2527         if (se) {
2528                 // prevent setting the render size if sequence values aren't initialized
2529                 if ( (se->orig_width > 0) && (se->orig_height > 0) ) {
2530                         scene->r.xsch= se->orig_width;
2531                         scene->r.ysch= se->orig_height;
2532                         WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, scene);
2533                         retval = OPERATOR_FINISHED;
2534                 }
2535         }
2536
2537         return retval;
2538 }
2539
2540 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2541 {
2542         /* identifiers */
2543         ot->name = "Set Render Size";
2544         ot->idname = "SEQUENCER_OT_rendersize";
2545         ot->description = "Set render size and aspect from active sequence";
2546         
2547         /* api callbacks */
2548         ot->exec = sequencer_rendersize_exec;
2549         ot->poll = sequencer_edit_poll;
2550         
2551         /* flags */
2552         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2553         
2554         /* properties */
2555 }
2556
2557 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
2558 {
2559         if (seq->type == SEQ_META) {
2560                 Sequence *iseq;
2561                 for (iseq= seq->seqbase.first; iseq; iseq= iseq->next) {
2562                         seq_copy_del_sound(scene, iseq);
2563                 }
2564         }
2565         else if (seq->scene_sound) {
2566                 sound_remove_scene_sound(scene, seq->scene_sound);
2567                 seq->scene_sound = NULL;
2568         }
2569 }
2570
2571 /* TODO, validate scenes */
2572 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2573 {
2574         Scene *scene= CTX_data_scene(C);
2575         Editing *ed= seq_give_editing(scene, FALSE);
2576         Sequence *seq;
2577
2578         ListBase nseqbase= {NULL, NULL};
2579
2580         seq_free_clipboard();
2581
2582         if (seqbase_isolated_sel_check(ed->seqbasep)==FALSE) {
2583                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2584                 return OPERATOR_CANCELLED;
2585         }
2586
2587         seqbase_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2588
2589         /* To make sure the copied strips have unique names between each other add
2590          * them temporarily to the end of the original seqbase. (bug 25932)
2591          */
2592         if (nseqbase.first) {
2593                 Sequence *seq, *first_seq = nseqbase.first;
2594                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2595
2596                 for (seq=first_seq; seq; seq=seq->next)
2597                         seq_recursive_apply(seq, apply_unique_name_cb, scene);
2598
2599                 seqbase_clipboard.first = first_seq;
2600                 seqbase_clipboard.last = ed->seqbasep->last;
2601
2602                 if (first_seq->prev) {
2603                         first_seq->prev->next = NULL;
2604                         ed->seqbasep->last = first_seq->prev;
2605                         first_seq->prev = NULL;
2606                 }
2607         }
2608
2609         seqbase_clipboard_frame= scene->r.cfra;
2610
2611         /* Need to remove anything that references the current scene */
2612         for (seq= seqbase_clipboard.first; seq; seq= seq->next) {
2613                 seq_copy_del_sound(scene, seq);
2614         }
2615
2616         return OPERATOR_FINISHED;
2617 }
2618
2619 void SEQUENCER_OT_copy(wmOperatorType *ot)
2620 {
2621         /* identifiers */
2622         ot->name = "Copy";
2623         ot->idname = "SEQUENCER_OT_copy";
2624         ot->description = "";
2625
2626         /* api callbacks */
2627         ot->exec = sequencer_copy_exec;
2628         ot->poll = sequencer_edit_poll;
2629
2630         /* flags */
2631         ot->flag = OPTYPE_REGISTER;
2632
2633         /* properties */
2634 }
2635
2636 static void seq_paste_add_sound(Scene *scene, Sequence *seq)
2637 {
2638         if (seq->type == SEQ_META) {
2639                 Sequence *iseq;
2640                 for (iseq= seq->seqbase.first; iseq; iseq= iseq->next) {
2641                         seq_paste_add_sound(scene, iseq);
2642                 }
2643         }
2644         else if (seq->type == SEQ_SOUND) {
2645                 seq->scene_sound = sound_add_scene_sound_defaults(scene, seq);
2646         }
2647 }
2648
2649 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2650 {
2651         Scene *scene= CTX_data_scene(C);
2652         Editing *ed= seq_give_editing(scene, TRUE); /* create if needed */
2653         ListBase nseqbase = {NULL, NULL};
2654         int ofs;
2655         Sequence *iseq;
2656
2657         deselect_all_seq(scene);
2658         ofs = scene->r.cfra - seqbase_clipboard_frame;
2659
2660         seqbase_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2661
2662         /* transform pasted strips before adding */
2663         if (ofs) {
2664                 for (iseq= nseqbase.first; iseq; iseq= iseq->next) {
2665                         seq_translate(scene, iseq, ofs);
2666                         seq_sound_init(scene, iseq);
2667                 }
2668         }
2669
2670         iseq = nseqbase.first;
2671
2672         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2673
2674         /* make sure the pasted strips have unique names between them */
2675         for (; iseq; iseq=iseq->next) {
2676                 seq_recursive_apply(iseq, apply_unique_name_cb, scene);
2677
2678                 /* restore valid sound_scene for newly added strips */
2679                 seq_paste_add_sound(scene, iseq);
2680         }
2681
2682         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2683
2684         return OPERATOR_FINISHED;
2685 }
2686
2687 void SEQUENCER_OT_paste(wmOperatorType *ot)
2688 {
2689         /* identifiers */
2690         ot->name = "Paste";
2691         ot->idname = "SEQUENCER_OT_paste";
2692         ot->description = "";
2693
2694         /* api callbacks */
2695         ot->exec = sequencer_paste_exec;
2696         ot->poll = ED_operator_sequencer_active;
2697
2698         /* flags */
2699         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2700
2701         /* properties */
2702 }
2703
2704 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2705 {
2706         Scene *scene= CTX_data_scene(C);
2707         Sequence *seq_act;
2708         Sequence *seq_other;
2709         const char *error_msg;
2710
2711         if (seq_active_pair_get(scene, &seq_act, &seq_other) == 0) {
2712                 BKE_report(op->reports, RPT_ERROR, "Must select 2 strips");
2713                 return OPERATOR_CANCELLED;
2714         }
2715
2716         if (seq_swap(seq_act, seq_other, &error_msg) == 0) {
2717                 BKE_report(op->reports, RPT_ERROR, error_msg);
2718                 return OPERATOR_CANCELLED;
2719         }
2720
2721         sound_remove_scene_sound(scene, seq_act->scene_sound);
2722         sound_remove_scene_sound(scene, seq_other->scene_sound);
2723
2724         seq_act->scene_sound= NULL;
2725         seq_other->scene_sound= NULL;
2726
2727         calc_sequence(scene, seq_act);
2728         calc_sequence(scene, seq_other);
2729
2730         if (seq_act->sound)             sound_add_scene_sound_defaults(scene, seq_act);
2731         if (seq_other->sound)   sound_add_scene_sound_defaults(scene, seq_other);
2732
2733         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2734
2735         return OPERATOR_FINISHED;
2736 }
2737
2738 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2739 {
2740         /* identifiers */
2741         ot->name = "Sequencer Swap Data";
2742         ot->idname = "SEQUENCER_OT_swap_data";
2743         ot->description = "Swap 2 sequencer strips";
2744
2745         /* api callbacks */
2746         ot->exec = sequencer_swap_data_exec;
2747         ot->poll = ED_operator_sequencer_active;
2748
2749         /* flags */
2750         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2751
2752         /* properties */
2753 }
2754
2755 /* borderselect operator */
2756 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2757 {
2758         Scene *scene= CTX_data_scene(C);
2759         Editing *ed= seq_give_editing(scene, FALSE);
2760         View2D *v2d= UI_view2d_fromcontext(C);
2761
2762         rctf rect;
2763
2764         /* convert coordinates of rect to 'tot' rect coordinates */
2765         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2766         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2767
2768         if (ed==NULL)
2769                 return OPERATOR_CANCELLED;
2770
2771         rect.xmin /=  (float)(ABS(v2d->tot.xmax - v2d->tot.xmin));
2772         rect.ymin /=  (float)(ABS(v2d->tot.ymax - v2d->tot.ymin));
2773
2774         rect.xmax /=  (float)(ABS(v2d->tot.xmax - v2d->tot.xmin));
2775         rect.ymax /=  (float)(ABS(v2d->tot.ymax - v2d->tot.ymin));
2776
2777         rect.xmin+=0.5f;
2778         rect.xmax+=0.5f;
2779         rect.ymin+=0.5f;
2780         rect.ymax+=0.5f;
2781
2782         CLAMP(rect.xmin, 0.0f, 1.0f);
2783         CLAMP(rect.ymin, 0.0f, 1.0f);
2784         CLAMP(rect.xmax, 0.0f, 1.0f);
2785         CLAMP(rect.ymax, 0.0f, 1.0f);
2786
2787         scene->ed->over_border= rect;
2788
2789         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2790
2791         return OPERATOR_FINISHED;
2792 }
2793
2794 /* ****** Border Select ****** */
2795 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2796 {
2797         /* identifiers */
2798         ot->name = "Border Offset View";
2799         ot->idname = "SEQUENCER_OT_view_ghost_border";
2800         ot->description = "Enable border select mode";
2801
2802         /* api callbacks */
2803         ot->invoke = WM_border_select_invoke;
2804         ot->exec = view_ghost_border_exec;
2805         ot->modal = WM_border_select_modal;
2806         ot->poll = sequencer_view_poll;
2807         ot->cancel = WM_border_select_cancel;
2808
2809         /* flags */
2810         ot->flag = 0;
2811
2812         /* rna */
2813         WM_operator_properties_gesture_border(ot, FALSE);
2814 }
2815
2816 /* rebuild_proxy operator */
2817 static int sequencer_rebuild_proxy_exec(bContext *C, wmOperator *UNUSED(op))
2818 {
2819         seq_proxy_build_job(C);
2820
2821         return OPERATOR_FINISHED;
2822 }
2823
2824 void SEQUENCER_OT_rebuild_proxy(wmOperatorType *ot)
2825 {
2826         /* identifiers */
2827         ot->name = "Rebuild Proxy and Timecode Indices";
2828         ot->idname = "SEQUENCER_OT_rebuild_proxy";
2829         ot->description = "Rebuild all selected proxies and timecode indeces using the job system";
2830         
2831         /* api callbacks */
2832         ot->exec = sequencer_rebuild_proxy_exec;
2833         ot->poll = ED_operator_sequencer_active;
2834         
2835         /* flags */
2836         ot->flag = OPTYPE_REGISTER;
2837 }
2838
2839 /* change ops */
2840
2841 static EnumPropertyItem prop_change_effect_input_types[] = {
2842         {0, "A_B", 0, "A -> B", ""},
2843         {1, "B_C", 0, "B -> C", ""},
2844         {2, "A_C", 0, "A -> C", ""},
2845         {0, NULL, 0, NULL, NULL}
2846 };
2847
2848 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
2849 {
2850         Scene *scene= CTX_data_scene(C);
2851         Editing *ed= seq_give_editing(scene, FALSE);
2852         Sequence *seq= seq_active_get(scene);
2853
2854         Sequence **seq_1, **seq_2;
2855
2856         switch(RNA_enum_get(op->ptr, "swap")) {
2857                 case 0:
2858                         seq_1= &seq->seq1;
2859                         seq_2= &seq->seq2;
2860                         break;
2861                 case 1:
2862                         seq_1= &seq->seq2;
2863                         seq_2= &seq->seq3;
2864                         break;
2865                 default: /* 2 */
2866                         seq_1= &seq->seq1;
2867                         seq_2= &seq->seq3;
2868                         break;
2869         }
2870
2871         if (*seq_1 == NULL || *seq_2 == NULL) {
2872                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, can't swap");
2873                 return OPERATOR_CANCELLED;
2874         }
2875         else {
2876                 SWAP(Sequence *, *seq_1, *seq_2);
2877         }
2878
2879         update_changed_seq_and_deps(scene, seq, 0, 1);
2880
2881         /* important else we don't get the imbuf cache flushed */
2882         free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
2883
2884         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2885
2886         return OPERATOR_FINISHED;
2887 }
2888
2889 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
2890 {
2891         /* identifiers */
2892         ot->name = "Change Effect Input";
2893         ot->idname = "SEQUENCER_OT_change_effect_input";
2894         ot->description = "";
2895
2896         /* api callbacks */
2897         ot->exec = sequencer_change_effect_input_exec;
2898         ot->poll = sequencer_effect_poll;
2899
2900         /* flags */
2901         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2902
2903         ot->prop = RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
2904 }
2905
2906 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
2907 {
2908         Scene *scene= CTX_data_scene(C);
2909         Editing *ed= seq_give_editing(scene, FALSE);
2910         Sequence *seq= seq_active_get(scene);
2911         const int new_type= RNA_enum_get(op->ptr, "type");
2912
2913         /* free previous effect and init new effect */
2914         struct SeqEffectHandle sh;
2915
2916         if ((seq->type & SEQ_EFFECT) == 0) {
2917                 return OPERATOR_CANCELLED;
2918         }
2919
2920         /* can someone explain the logic behind only allowing to increase this,
2921          * copied from 2.4x - campbell */
2922         if (get_sequence_effect_num_inputs(seq->type) <
2923                 get_sequence_effect_num_inputs(new_type)
2924         ) {
2925                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
2926                 return OPERATOR_CANCELLED;
2927         }
2928         else {
2929                 sh = get_sequence_effect(seq);
2930                 sh.free(seq);
2931
2932                 seq->type= new_type;
2933
2934                 sh = get_sequence_effect(seq);
2935                 sh.init(seq);
2936         }
2937
2938         /* update */
2939         update_changed_seq_and_deps(scene, seq, 0, 1);
2940
2941         /* important else we don't get the imbuf cache flushed */
2942         free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
2943
2944         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
2945
2946         return OPERATOR_FINISHED;
2947 }
2948
2949 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
2950 {
2951         /* identifiers */
2952         ot->name = "Change Effect Type";
2953         ot->idname = "SEQUENCER_OT_change_effect_type";
2954         ot->description = "";
2955
2956         /* api callbacks */
2957         ot->exec = sequencer_change_effect_type_exec;
2958         ot->poll = sequencer_effect_poll;
2959
2960         /* flags */
2961         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
2962
2963         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
2964 }
2965
2966 static int sequencer_change_path_exec(bContext *C, wmOperator *op)
2967 {
2968         Main *bmain= CTX_data_main(C);
2969         Scene *scene= CTX_data_scene(C);
2970         Editing *ed= seq_give_editing(scene, FALSE);
2971         Sequence *seq= seq_active_get(scene);
2972         const int is_relative_path= RNA_boolean_get(op->ptr, "relative_path");
2973
2974         if (seq->type == SEQ_IMAGE) {
2975                 char directory[FILE_MAX];
2976                 const int len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
2977                 StripElem *se;
2978
2979                 if (len==0)
2980                         return OPERATOR_CANCELLED;
2981
2982                 RNA_string_get(op->ptr, "directory", directory);
2983                 if (is_relative_path) {
2984                         /* TODO, shouldn't this already be relative from the filesel?
2985                          * (as the 'filepath' is) for now just make relative here,
2986                          * but look into changing after 2.60 - campbell */
2987                         BLI_path_rel(directory, bmain->name);
2988                 }
2989                 BLI_strncpy(seq->strip->dir, directory, sizeof(seq->strip->dir));
2990
2991                 if (seq->strip->stripdata) {
2992                         MEM_freeN(seq->strip->stripdata);
2993                 }
2994                 seq->strip->stripdata= se= MEM_callocN(len*sizeof(StripElem), "stripelem");
2995
2996                 RNA_BEGIN(op->ptr, itemptr, "files") {
2997                         char *filename= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
2998                         BLI_strncpy(se->name, filename, sizeof(se->name));
2999                         MEM_freeN(filename);
3000                         se++;
3001                 }
3002                 RNA_END;
3003
3004                 /* reset these else we wont see all the images */
3005                 seq->anim_startofs= seq->anim_endofs= 0;
3006
3007                 /* correct start/end frames so we don't move
3008                  * important not to set seq->len= len; allow the function to handle it */
3009                 reload_sequence_new_file(scene, seq, TRUE);
3010
3011                 calc_sequence(scene, seq);
3012
3013                 /* important else we don't get the imbuf cache flushed */
3014                 free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
3015         }
3016         else {
3017                 /* lame, set rna filepath */
3018                 PointerRNA seq_ptr;
3019                 PropertyRNA *prop;
3020                 char filepath[FILE_MAX];
3021
3022                 RNA_pointer_create(&scene->id, &RNA_Sequence, seq, &seq_ptr);
3023
3024                 RNA_string_get(op->ptr, "filepath", filepath);
3025                 prop= RNA_struct_find_property(&seq_ptr, "filepath");
3026                 RNA_property_string_set(&seq_ptr, prop, filepath);
3027                 RNA_property_update(C, &seq_ptr, prop);
3028         }
3029
3030         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
3031
3032         return OPERATOR_FINISHED;
3033 }
3034
3035 static int sequencer_change_path_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
3036 {
3037         Scene *scene= CTX_data_scene(C);
3038         Sequence *seq= seq_active_get(scene);
3039
3040         RNA_string_set(op->ptr, "directory", seq->strip->dir);
3041
3042         /* set default display depending on seq type */
3043         if (seq->type == SEQ_IMAGE) {
3044                 RNA_boolean_set(op->ptr, "filter_movie", FALSE);
3045         }
3046         else {
3047                 RNA_boolean_set(op->ptr, "filter_image", FALSE);
3048         }
3049
3050         WM_event_add_fileselect(C, op);
3051
3052         return OPERATOR_RUNNING_MODAL;
3053 }
3054
3055 void SEQUENCER_OT_change_path(struct wmOperatorType *ot)
3056 {
3057         /* identifiers */
3058         ot->name = "Change Data/Files";
3059         ot->idname = "SEQUENCER_OT_change_path";
3060         ot->description = "";
3061
3062         /* api callbacks */
3063         ot->exec = sequencer_change_path_exec;
3064         ot->invoke = sequencer_change_path_invoke;
3065         ot->poll = sequencer_strip_has_path_poll;
3066
3067         /* flags */
3068         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
3069
3070         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH|WM_FILESEL_FILEPATH|WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
3071 }
3072
3073 static int sequencer_update_strip_length_exec(bContext *C, wmOperator *UNUSED(op))
3074 {
3075         Scene *scene = CTX_data_scene(C);
3076         Editing *ed = seq_give_editing(scene, FALSE);
3077         Sequence *seq;
3078         int update = FALSE;
3079
3080         SEQP_BEGIN(ed, seq) {
3081                 if ((seq->flag & SELECT)) {
3082                         int changed = FALSE;
3083
3084                         switch (seq->type) {
3085                                 case SEQ_SCENE:
3086                                         seq->len = seq->scene->r.efra - seq->scene->r.sfra + 1;
3087                                         changed = TRUE;
3088                                         break;
3089                                 case SEQ_MOVIECLIP:
3090                                         seq->len = BKE_movieclip_get_duration(seq->clip);
3091                                         changed = TRUE;
3092                                         break;
3093                                 case SEQ_MOVIE:
3094                                         seq->len = IMB_anim_get_duration(seq->anim, IMB_TC_RECORD_RUN);
3095                                         changed = TRUE;
3096                                         break;
3097                         }
3098
3099                         if (changed) {
3100                                 calc_sequence_disp(scene, seq);
3101
3102                                 if (seq_test_overlap(ed->seqbasep, seq))
3103                                         shuffle_seq(ed->seqbasep, seq, scene);
3104
3105                                 update = TRUE;
3106                         }
3107                 }
3108         }
3109         SEQ_END
3110
3111         if (update) {
3112                 free_imbuf_seq(scene, &ed->seqbase, FALSE, FALSE);
3113
3114                 WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
3115         }
3116
3117         return OPERATOR_FINISHED;
3118 }
3119
3120 void SEQUENCER_OT_update_strip_length(wmOperatorType *ot)
3121 {
3122         /* identifiers */
3123         ot->name = "Update Strip Length";
3124         ot->idname = "SEQUENCER_OT_update_strip_length";
3125         ot->description = "Update actual content length for selected strips";
3126
3127         /* api callbacks */
3128         ot->exec = sequencer_update_strip_length_exec;
3129         ot->poll = ED_operator_sequencer_active;
3130
3131         /* flags */
3132         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
3133 }