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