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