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