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