08c4cff332f0cd6c34b601390fdf487e4d1717cc
[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 static int sequencer_delete_invoke(bContext *C, wmOperator *op, wmEvent *event)
1672 {
1673         ARegion *ar = CTX_wm_region(C);
1674
1675         if (ar->regiontype == RGN_TYPE_WINDOW) {
1676                 /* bounding box of 30 pixels is used for markers shortcuts,
1677                  * prevent conflict with markers shortcurts here
1678                  */
1679                 if (event->mval[1] <= 30)
1680                         return OPERATOR_PASS_THROUGH;
1681         }
1682
1683         return WM_operator_confirm(C, op, event);
1684 }
1685
1686 void SEQUENCER_OT_delete(wmOperatorType *ot)
1687 {
1688
1689         /* identifiers */
1690         ot->name = "Erase Strips";
1691         ot->idname = "SEQUENCER_OT_delete";
1692         ot->description = "Erase selected strips from the sequencer";
1693         
1694         /* api callbacks */
1695         ot->invoke = sequencer_delete_invoke;
1696         ot->exec = sequencer_delete_exec;
1697         ot->poll = sequencer_edit_poll;
1698         
1699         /* flags */
1700         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1701 }
1702
1703
1704 /* offset clear operator */
1705 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
1706 {
1707         Scene *scene = CTX_data_scene(C);
1708         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1709         Sequence *seq;
1710
1711         /* for effects, try to find a replacement input */
1712         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1713                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1714                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
1715                 }
1716         }
1717
1718         /* updates lengths etc */
1719         seq = ed->seqbasep->first;
1720         while (seq) {
1721                 BKE_sequence_calc(scene, seq);
1722                 seq = seq->next;
1723         }
1724
1725         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1726                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1727                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
1728                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1729                         }
1730                 }
1731         }
1732
1733         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1734
1735         return OPERATOR_FINISHED;
1736 }
1737
1738
1739 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
1740 {
1741
1742         /* identifiers */
1743         ot->name = "Clear Strip Offset";
1744         ot->idname = "SEQUENCER_OT_offset_clear";
1745         ot->description = "Clear strip offsets from the start and end frames";
1746
1747         /* api callbacks */
1748         ot->exec = sequencer_offset_clear_exec;
1749         ot->poll = sequencer_edit_poll;
1750
1751         /* flags */
1752         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1753 }
1754
1755
1756 /* separate_images operator */
1757 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1758 {
1759         Scene *scene = CTX_data_scene(C);
1760         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1761         
1762         Sequence *seq, *seq_new;
1763         Strip *strip_new;
1764         StripElem *se, *se_new;
1765         int start_ofs, cfra, frame_end;
1766         int step = RNA_int_get(op->ptr, "length");
1767
1768         seq = ed->seqbasep->first; /* poll checks this is valid */
1769
1770         while (seq) {
1771                 if ((seq->flag & SELECT) && (seq->type == SEQ_TYPE_IMAGE) && (seq->len > 1)) {
1772                         /* remove seq so overlap tests don't conflict,
1773                          * see seq_free_sequence below for the real free'ing */
1774                         BLI_remlink(ed->seqbasep, seq);
1775                         /* if (seq->ipo) seq->ipo->id.us--; */
1776                         /* XXX, remove fcurve and assign to split image strips */
1777
1778                         start_ofs = cfra = BKE_sequence_tx_get_final_left(seq, 0);
1779                         frame_end = BKE_sequence_tx_get_final_right(seq, 0);
1780
1781                         while (cfra < frame_end) {
1782                                 /* new seq */
1783                                 se = BKE_sequencer_give_stripelem(seq, cfra);
1784
1785                                 seq_new = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
1786                                 BLI_addtail(ed->seqbasep, seq_new);
1787
1788                                 seq_new->start = start_ofs;
1789                                 seq_new->type = SEQ_TYPE_IMAGE;
1790                                 seq_new->len = 1;
1791                                 seq_new->endstill = step - 1;
1792
1793                                 /* new strip */
1794                                 strip_new = seq_new->strip;
1795                                 strip_new->us = 1;
1796
1797                                 /* new stripdata */
1798                                 se_new = strip_new->stripdata;
1799                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
1800                                 BKE_sequence_calc(scene, seq_new);
1801
1802                                 if (step > 1) {
1803                                         seq_new->flag &= ~SEQ_OVERLAP;
1804                                         if (BKE_sequence_test_overlap(ed->seqbasep, seq_new)) {
1805                                                 BKE_sequence_base_shuffle(ed->seqbasep, seq_new, scene);
1806                                         }
1807                                 }
1808
1809                                 /* XXX, COPY FCURVES */
1810
1811                                 cfra++;
1812                                 start_ofs += step;
1813                         }
1814
1815                         BKE_sequence_free(scene, seq);
1816                         seq = seq->next;
1817                 }
1818                 else {
1819                         seq = seq->next;
1820                 }
1821         }
1822
1823         /* as last: */
1824         BKE_sequencer_sort(scene);
1825         
1826         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1827
1828         return OPERATOR_FINISHED;
1829 }
1830
1831
1832 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
1833 {
1834         /* identifiers */
1835         ot->name = "Separate Images";
1836         ot->idname = "SEQUENCER_OT_images_separate";
1837         ot->description = "On image sequence strips, it returns a strip for each image";
1838         
1839         /* api callbacks */
1840         ot->exec = sequencer_separate_images_exec;
1841         ot->poll = sequencer_edit_poll;
1842         
1843         /* flags */
1844         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1845
1846         RNA_def_int(ot->srna, "length", 1, 1, 1000, "Length", "Length of each frame", 1, INT_MAX);
1847 }
1848
1849
1850 /* META Operators */
1851
1852 /* separate_meta_toggle operator */
1853 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1854 {
1855         Scene *scene = CTX_data_scene(C);
1856         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1857         Sequence *last_seq = BKE_sequencer_active_get(scene);
1858         MetaStack *ms;
1859
1860         if (last_seq && last_seq->type == SEQ_TYPE_META && last_seq->flag & SELECT) {
1861                 /* Enter Metastrip */
1862                 ms = MEM_mallocN(sizeof(MetaStack), "metastack");
1863                 BLI_addtail(&ed->metastack, ms);
1864                 ms->parseq = last_seq;
1865                 ms->oldbasep = ed->seqbasep;
1866
1867                 ed->seqbasep = &last_seq->seqbase;
1868
1869                 BKE_sequencer_active_set(scene, NULL);
1870
1871         }
1872         else {
1873                 /* Exit Metastrip (if possible) */
1874
1875                 Sequence *seq;
1876
1877                 if (ed->metastack.first == NULL)
1878                         return OPERATOR_CANCELLED;
1879
1880                 ms = ed->metastack.last;
1881                 BLI_remlink(&ed->metastack, ms);
1882
1883                 ed->seqbasep = ms->oldbasep;
1884
1885                 /* recalc all: the meta can have effects connected to it */
1886                 for (seq = ed->seqbasep->first; seq; seq = seq->next)
1887                         BKE_sequence_calc(scene, seq);
1888
1889                 BKE_sequencer_active_set(scene, ms->parseq);
1890
1891                 ms->parseq->flag |= SELECT;
1892                 recurs_sel_seq(ms->parseq);
1893
1894                 MEM_freeN(ms);
1895
1896         }
1897
1898         BKE_sequencer_update_muting(ed);
1899         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1900
1901         return OPERATOR_FINISHED;
1902 }
1903
1904 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
1905 {
1906         /* identifiers */
1907         ot->name = "Toggle Meta Strip";
1908         ot->idname = "SEQUENCER_OT_meta_toggle";
1909         ot->description = "Toggle a metastrip (to edit enclosed strips)";
1910         
1911         /* api callbacks */
1912         ot->exec = sequencer_meta_toggle_exec;
1913         ot->poll = sequencer_edit_poll;
1914         
1915         /* flags */
1916         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1917 }
1918
1919
1920 /* separate_meta_make operator */
1921 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
1922 {
1923         Scene *scene = CTX_data_scene(C);
1924         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1925         
1926         Sequence *seq, *seqm, *next, *last_seq = BKE_sequencer_active_get(scene);
1927         int channel_max = 1;
1928
1929         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
1930                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
1931                 return OPERATOR_CANCELLED;
1932         }
1933
1934         /* remove all selected from main list, and put in meta */
1935
1936         seqm = BKE_sequence_alloc(ed->seqbasep, 1, 1); /* channel number set later */
1937         strcpy(seqm->name + 2, "MetaStrip");
1938         seqm->type = SEQ_TYPE_META;
1939         seqm->flag = SELECT;
1940
1941         seq = ed->seqbasep->first;
1942         while (seq) {
1943                 next = seq->next;
1944                 if (seq != seqm && (seq->flag & SELECT)) {
1945                         channel_max = MAX2(seq->machine, channel_max);
1946                         BLI_remlink(ed->seqbasep, seq);
1947                         BLI_addtail(&seqm->seqbase, seq);
1948                 }
1949                 seq = next;
1950         }
1951         seqm->machine = last_seq ? last_seq->machine : channel_max;
1952         BKE_sequence_calc(scene, seqm);
1953
1954         seqm->strip = MEM_callocN(sizeof(Strip), "metastrip");
1955         seqm->strip->us = 1;
1956         
1957         BKE_sequencer_active_set(scene, seqm);
1958
1959         if (BKE_sequence_test_overlap(ed->seqbasep, seqm) ) BKE_sequence_base_shuffle(ed->seqbasep, seqm, scene);
1960
1961         BKE_sequencer_update_muting(ed);
1962
1963         BKE_seqence_base_unique_name_recursive(&scene->ed->seqbase, seqm);
1964
1965         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1966
1967         return OPERATOR_FINISHED;
1968 }
1969
1970 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
1971 {
1972         /* identifiers */
1973         ot->name = "Make Meta Strip";
1974         ot->idname = "SEQUENCER_OT_meta_make";
1975         ot->description = "Group selected strips into a metastrip";
1976         
1977         /* api callbacks */
1978         ot->invoke = WM_operator_confirm;
1979         ot->exec = sequencer_meta_make_exec;
1980         ot->poll = sequencer_edit_poll;
1981         
1982         /* flags */
1983         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1984 }
1985
1986
1987 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
1988 {
1989         if (seq == seqm) return 1;
1990         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
1991         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
1992         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
1993         else return 0;
1994 }
1995
1996 /* separate_meta_make operator */
1997 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
1998 {
1999         Scene *scene = CTX_data_scene(C);
2000         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2001
2002         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene); /* last_seq checks (ed == NULL) */
2003
2004         if (last_seq == NULL || last_seq->type != SEQ_TYPE_META)
2005                 return OPERATOR_CANCELLED;
2006
2007         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
2008
2009         last_seq->seqbase.first = NULL;
2010         last_seq->seqbase.last = NULL;
2011
2012         BLI_remlink(ed->seqbasep, last_seq);
2013         BKE_sequence_free(scene, last_seq);
2014
2015         /* emtpy meta strip, delete all effects depending on it */
2016         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2017                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2018                         seq->flag |= SEQ_FLAG_DELETE;
2019
2020         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2021
2022         /* test for effects and overlap
2023          * don't use SEQP_BEGIN since that would be recursive */
2024         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2025                 if (seq->flag & SELECT) {
2026                         seq->flag &= ~SEQ_OVERLAP;
2027                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2028                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2029                         }
2030                 }
2031         }
2032
2033         BKE_sequencer_sort(scene);
2034         BKE_sequencer_update_muting(ed);
2035
2036         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2037
2038         return OPERATOR_FINISHED;
2039 }
2040
2041 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2042 {
2043         /* identifiers */
2044         ot->name = "UnMeta Strip";
2045         ot->idname = "SEQUENCER_OT_meta_separate";
2046         ot->description = "Put the contents of a metastrip back in the sequencer";
2047         
2048         /* api callbacks */
2049         ot->invoke = WM_operator_confirm;
2050         ot->exec = sequencer_meta_separate_exec;
2051         ot->poll = sequencer_edit_poll;
2052         
2053         /* flags */
2054         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2055 }
2056
2057 /* view_all operator */
2058 static int sequencer_view_all_exec(bContext *C, wmOperator *UNUSED(op))
2059 {
2060         ARegion *ar = CTX_wm_region(C);
2061         View2D *v2d = UI_view2d_fromcontext(C);
2062
2063         UI_view2d_smooth_view(C, ar, &v2d->tot);
2064         return OPERATOR_FINISHED;
2065 }
2066
2067 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2068 {
2069         /* identifiers */
2070         ot->name = "View All";
2071         ot->idname = "SEQUENCER_OT_view_all";
2072         ot->description = "View all the strips in the sequencer";
2073         
2074         /* api callbacks */
2075         ot->exec = sequencer_view_all_exec;
2076         ot->poll = ED_operator_sequencer_active;
2077         
2078         /* flags */
2079         ot->flag = OPTYPE_REGISTER;
2080 }
2081
2082 /* view_all operator */
2083 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2084 {
2085         bScreen *sc = CTX_wm_screen(C);
2086         ScrArea *area = CTX_wm_area(C);
2087 #if 0
2088         ARegion *ar = CTX_wm_region(C);
2089         SpaceSeq *sseq = area->spacedata.first;
2090         Scene *scene = CTX_data_scene(C);
2091 #endif
2092         View2D *v2d = UI_view2d_fromcontext(C);
2093
2094         v2d->cur = v2d->tot;
2095         UI_view2d_curRect_validate(v2d);
2096         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2097         
2098 #if 0
2099         /* Like zooming on an image view */
2100         float zoomX, zoomY;
2101         int width, height, imgwidth, imgheight;
2102
2103         width = ar->winx;
2104         height = ar->winy;
2105
2106         seq_reset_imageofs(sseq);
2107
2108         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2109         imgheight = (scene->r.size * scene->r.ysch) / 100;
2110
2111         /* Apply aspect, dosnt need to be that accurate */
2112         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2113
2114         if (((imgwidth >= width) || (imgheight >= height)) &&
2115             ((width > 0) && (height > 0)))
2116         {
2117                 /* Find the zoom value that will fit the image in the image space */
2118                 zoomX = ((float)width) / ((float)imgwidth);
2119                 zoomY = ((float)height) / ((float)imgheight);
2120                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2121
2122                 sseq->zoom = 1.0f / power_of_2(1 / minf(zoomX, zoomY));
2123         }
2124         else {
2125                 sseq->zoom = 1.0f;
2126         }
2127 #endif
2128
2129         ED_area_tag_redraw(CTX_wm_area(C));
2130         return OPERATOR_FINISHED;
2131 }
2132
2133 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2134 {
2135         /* identifiers */
2136         ot->name = "View All";
2137         ot->idname = "SEQUENCER_OT_view_all_preview";
2138         ot->description = "Zoom preview to fit in the area";
2139         
2140         /* api callbacks */
2141         ot->exec = sequencer_view_all_preview_exec;
2142         ot->poll = ED_operator_sequencer_active;
2143         
2144         /* flags */
2145         ot->flag = OPTYPE_REGISTER;
2146 }
2147
2148
2149 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2150 {
2151         RenderData *rd = &CTX_data_scene(C)->r;
2152         View2D *v2d = UI_view2d_fromcontext(C);
2153
2154         float ratio = RNA_float_get(op->ptr, "ratio");
2155
2156         float winx = (int)(rd->size * rd->xsch) / 100;
2157         float winy = (int)(rd->size * rd->ysch) / 100;
2158
2159         float facx = BLI_RCT_SIZE_X(&v2d->mask) / winx;
2160         float facy = BLI_RCT_SIZE_Y(&v2d->mask) / winy;
2161
2162         BLI_rctf_resize(&v2d->cur, (int)(winx * facx * ratio) + 1, (int)(winy * facy * ratio) + 1);
2163
2164         ED_region_tag_redraw(CTX_wm_region(C));
2165
2166         return OPERATOR_FINISHED;
2167 }
2168
2169 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2170 {
2171         /* identifiers */
2172         ot->name = "Sequencer View Zoom Ratio";
2173         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2174         ot->description = "Change zoom ratio of sequencer preview";
2175
2176         /* api callbacks */
2177         ot->exec = sequencer_view_zoom_ratio_exec;
2178         ot->poll = ED_operator_sequencer_active;
2179
2180         /* properties */
2181         RNA_def_float(ot->srna, "ratio", 1.0f, 0.0f, FLT_MAX,
2182                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2183 }
2184
2185
2186 #if 0
2187 static EnumPropertyItem view_type_items[] = {
2188         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2189         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2190         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2191         {0, NULL, 0, NULL, NULL}
2192 };
2193 #endif
2194
2195 /* view_all operator */
2196 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2197 {
2198         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2199
2200         sseq->view++;
2201         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2202
2203         ED_area_tag_refresh(CTX_wm_area(C));
2204
2205         return OPERATOR_FINISHED;
2206 }
2207
2208 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2209 {
2210         /* identifiers */
2211         ot->name = "View Toggle";
2212         ot->idname = "SEQUENCER_OT_view_toggle";
2213         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2214         
2215         /* api callbacks */
2216         ot->exec = sequencer_view_toggle_exec;
2217         ot->poll = ED_operator_sequencer_active;
2218         
2219         /* flags */
2220         ot->flag = OPTYPE_REGISTER;
2221 }
2222
2223
2224 /* view_selected operator */
2225 static int sequencer_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
2226 {
2227         Scene *scene = CTX_data_scene(C);
2228         View2D *v2d = UI_view2d_fromcontext(C);
2229         ARegion *ar = CTX_wm_region(C);
2230         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2231         Sequence *seq;
2232         rctf cur_new = v2d->cur;
2233
2234         int xmin =  MAXFRAME * 2;
2235         int xmax = -MAXFRAME * 2;
2236         int ymin =  MAXSEQ + 1;
2237         int ymax = 0;
2238         int orig_height;
2239         int ymid;
2240         int ymargin = 1;
2241         int xmargin = FPS;
2242
2243         if (ed == NULL)
2244                 return OPERATOR_CANCELLED;
2245
2246         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2247                 if (seq->flag & SELECT) {
2248                         xmin = MIN2(xmin, seq->startdisp);
2249                         xmax = MAX2(xmax, seq->enddisp);
2250
2251                         ymin = MIN2(ymin, seq->machine);
2252                         ymax = MAX2(ymax, seq->machine);
2253                 }
2254         }
2255
2256         if (ymax != 0) {
2257                 
2258                 xmax += xmargin;
2259                 xmin -= xmargin;
2260                 ymax += ymargin;
2261                 ymin -= ymargin;
2262
2263                 orig_height = BLI_RCT_SIZE_Y(&cur_new);
2264
2265                 cur_new.xmin = xmin;
2266                 cur_new.xmax = xmax;
2267
2268                 cur_new.ymin = ymin;
2269                 cur_new.ymax = ymax;
2270
2271                 /* only zoom out vertically */
2272                 if (orig_height > BLI_RCT_SIZE_Y(&cur_new)) {
2273                         ymid = BLI_RCT_CENTER_Y(&cur_new);
2274
2275                         cur_new.ymin = ymid - (orig_height / 2);
2276                         cur_new.ymax = ymid + (orig_height / 2);
2277                 }
2278
2279                 UI_view2d_smooth_view(C, ar, &cur_new);
2280
2281                 return OPERATOR_FINISHED;
2282         }
2283         else {
2284                 return OPERATOR_CANCELLED;
2285         }
2286         
2287 }
2288
2289 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2290 {
2291         /* identifiers */
2292         ot->name = "View Selected";
2293         ot->idname = "SEQUENCER_OT_view_selected";
2294         ot->description = "Zoom the sequencer on the selected strips";
2295         
2296         /* api callbacks */
2297         ot->exec = sequencer_view_selected_exec;
2298         ot->poll = ED_operator_sequencer_active;
2299         
2300         /* flags */
2301         ot->flag = OPTYPE_REGISTER;
2302 }
2303
2304
2305 static int find_next_prev_edit(Scene *scene, int cfra,
2306                                const short side,
2307                                const short do_skip_mute, const short do_center)
2308 {
2309         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2310         Sequence *seq, *best_seq = NULL, *frame_seq = NULL;
2311         
2312         int dist, best_dist;
2313         best_dist = MAXFRAME * 2;
2314
2315         if (ed == NULL) return cfra;
2316         
2317         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2318                 int seq_frame;
2319
2320                 if (do_skip_mute && (seq->flag & SEQ_MUTE)) {
2321                         continue;
2322                 }
2323
2324                 if (do_center) {
2325                         seq_frame = (seq->startdisp + seq->enddisp) / 2;
2326                 }
2327                 else {
2328                         seq_frame = seq->startdisp;
2329                 }
2330
2331                 dist = MAXFRAME * 2;
2332                         
2333                 switch (side) {
2334                         case SEQ_SIDE_LEFT:
2335                                 if (seq_frame < cfra) {
2336                                         dist = cfra - seq_frame;
2337                                 }
2338                                 break;
2339                         case SEQ_SIDE_RIGHT:
2340                                 if (seq_frame > cfra) {
2341                                         dist = seq_frame - cfra;
2342                                 }
2343                                 else if (seq_frame == cfra) {
2344                                         frame_seq = seq;
2345                                 }
2346                                 break;
2347                 }
2348
2349                 if (dist < best_dist) {
2350                         best_dist = dist;
2351                         best_seq = seq;
2352                 }
2353         }
2354
2355         /* if no sequence to the right is found and the
2356          * frame is on the start of the last sequence,
2357          * move to the end of the last sequence */
2358         if (frame_seq) {
2359                 if (do_center) {
2360                         cfra = (frame_seq->startdisp + frame_seq->enddisp) / 2;
2361                 }
2362                 else {
2363                         cfra = frame_seq->enddisp;
2364                 }
2365         }
2366
2367         if (best_seq) {
2368                 if (do_center) {
2369                         cfra = (best_seq->startdisp + best_seq->enddisp) / 2;
2370                 }
2371                 else {
2372                         cfra = best_seq->startdisp;
2373                 }
2374         }
2375
2376         return cfra;
2377 }
2378
2379 static int strip_jump_internal(Scene *scene,
2380                                const short side,
2381                                const short do_skip_mute, const short do_center)
2382 {
2383         int change = FALSE;
2384         int cfra = CFRA;
2385         int nfra = find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center);
2386         
2387         if (nfra != cfra) {
2388                 CFRA = nfra;
2389                 change = TRUE;
2390         }
2391
2392         return change;
2393 }
2394
2395 /* jump frame to edit point operator */
2396 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2397 {
2398         Scene *scene = CTX_data_scene(C);
2399         short next = RNA_boolean_get(op->ptr, "next");
2400         short center = RNA_boolean_get(op->ptr, "center");
2401
2402         /* currently do_skip_mute is always TRUE */
2403         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, TRUE, center)) {
2404                 return OPERATOR_CANCELLED;
2405         }
2406
2407         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2408         
2409         return OPERATOR_FINISHED;
2410 }
2411
2412 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2413 {
2414         /* identifiers */
2415         ot->name = "Jump to Strip";
2416         ot->idname = "SEQUENCER_OT_strip_jump";
2417         ot->description = "Move frame to previous edit point";
2418
2419         /* api callbacks */
2420         ot->exec = sequencer_strip_jump_exec;
2421         ot->poll = sequencer_edit_poll;
2422
2423         /* flags */
2424         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2425         
2426         /* properties */
2427         RNA_def_boolean(ot->srna, "next", TRUE, "Next Strip", "");
2428         RNA_def_boolean(ot->srna, "center", TRUE, "Use strip center", "");
2429 }
2430
2431 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2432 {
2433         int gap = seqb->startdisp - seqa->enddisp;
2434         seqb->start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2435         BKE_sequence_calc(scene, seqb);
2436         seqa->start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2437         BKE_sequence_calc(scene, seqa);
2438 }
2439
2440 #if 0
2441 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
2442 {
2443         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2444         Sequence *parent = NULL;
2445         Sequence *seq;
2446
2447         if (ed == NULL) return NULL;
2448
2449         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2450                 if ((seq != child) && seq_is_parent(seq, child)) {
2451                         parent = seq;
2452                         break;
2453                 }
2454         }
2455
2456         return parent;
2457 }
2458 #endif
2459
2460 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2461 {
2462         Scene *scene = CTX_data_scene(C);
2463         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2464         Sequence *active_seq = BKE_sequencer_active_get(scene);
2465         Sequence *seq, *iseq;
2466         int side = RNA_enum_get(op->ptr, "side");
2467
2468         if (active_seq == NULL) return OPERATOR_CANCELLED;
2469
2470         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2471         
2472         if (seq) {
2473                 
2474                 /* disallow effect strips */
2475                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2476                         return OPERATOR_CANCELLED;
2477                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2478                         return OPERATOR_CANCELLED;
2479
2480                 switch (side) {
2481                         case SEQ_SIDE_LEFT: 
2482                                 swap_sequence(scene, seq, active_seq);
2483                                 break;
2484                         case SEQ_SIDE_RIGHT: 
2485                                 swap_sequence(scene, active_seq, seq);
2486                                 break;
2487                 }
2488
2489                 // XXX - should be a generic function
2490                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2491                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2492                                 BKE_sequence_calc(scene, iseq);
2493                         }
2494                 }
2495
2496                 /* do this in a new loop since both effects need to be calculated first */
2497                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2498                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2499                                 /* this may now overlap */
2500                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
2501                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2502                                 }
2503                         }
2504                 }
2505
2506
2507
2508                 BKE_sequencer_sort(scene);
2509
2510                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2511
2512                 return OPERATOR_FINISHED;
2513         }
2514
2515         return OPERATOR_CANCELLED;
2516 }
2517
2518 void SEQUENCER_OT_swap(wmOperatorType *ot)
2519 {
2520         /* identifiers */
2521         ot->name = "Swap Strip";
2522         ot->idname = "SEQUENCER_OT_swap";
2523         ot->description = "Swap active strip with strip to the right or left";
2524         
2525         /* api callbacks */
2526         ot->exec = sequencer_swap_exec;
2527         ot->poll = sequencer_edit_poll;
2528         
2529         /* flags */
2530         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2531         
2532         /* properties */
2533         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2534 }
2535
2536 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2537 {
2538         int retval = OPERATOR_CANCELLED;
2539         Scene *scene = CTX_data_scene(C);
2540         Sequence *active_seq = BKE_sequencer_active_get(scene);
2541         StripElem *se = NULL;
2542
2543         if (active_seq == NULL)
2544                 return OPERATOR_CANCELLED;
2545
2546
2547         if (active_seq->strip) {
2548                 switch (active_seq->type) {
2549                         case SEQ_TYPE_IMAGE:
2550                                 se = BKE_sequencer_give_stripelem(active_seq, scene->r.cfra);
2551                                 break;
2552                         case SEQ_TYPE_MOVIE:
2553                                 se = active_seq->strip->stripdata;
2554                                 break;
2555                         case SEQ_TYPE_SCENE:
2556                         case SEQ_TYPE_META:
2557                         case SEQ_TYPE_SOUND_RAM:
2558                         case SEQ_TYPE_SOUND_HD:
2559                         default:
2560                                 break;
2561                 }
2562         }
2563
2564         if (se) {
2565                 // prevent setting the render size if sequence values aren't initialized
2566                 if ((se->orig_width > 0) && (se->orig_height > 0)) {
2567                         scene->r.xsch = se->orig_width;
2568                         scene->r.ysch = se->orig_height;
2569                         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
2570                         retval = OPERATOR_FINISHED;
2571                 }
2572         }
2573
2574         return retval;
2575 }
2576
2577 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2578 {
2579         /* identifiers */
2580         ot->name = "Set Render Size";
2581         ot->idname = "SEQUENCER_OT_rendersize";
2582         ot->description = "Set render size and aspect from active sequence";
2583         
2584         /* api callbacks */
2585         ot->exec = sequencer_rendersize_exec;
2586         ot->poll = sequencer_edit_poll;
2587         
2588         /* flags */
2589         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2590         
2591         /* properties */
2592 }
2593
2594 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
2595 {
2596         if (seq->type == SEQ_TYPE_META) {
2597                 Sequence *iseq;
2598                 for (iseq = seq->seqbase.first; iseq; iseq = iseq->next) {
2599                         seq_copy_del_sound(scene, iseq);
2600                 }
2601         }
2602         else if (seq->scene_sound) {
2603                 sound_remove_scene_sound(scene, seq->scene_sound);
2604                 seq->scene_sound = NULL;
2605         }
2606 }
2607
2608 /* TODO, validate scenes */
2609 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2610 {
2611         Scene *scene = CTX_data_scene(C);
2612         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2613         Sequence *seq;
2614
2615         ListBase nseqbase = {NULL, NULL};
2616
2617         BKE_sequencer_free_clipboard();
2618
2619         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
2620                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2621                 return OPERATOR_CANCELLED;
2622         }
2623
2624         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2625
2626         /* To make sure the copied strips have unique names between each other add
2627          * them temporarily to the end of the original seqbase. (bug 25932)
2628          */
2629         if (nseqbase.first) {
2630                 Sequence *seq, *first_seq = nseqbase.first;
2631                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2632
2633                 for (seq = first_seq; seq; seq = seq->next)
2634                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2635
2636                 seqbase_clipboard.first = first_seq;
2637                 seqbase_clipboard.last = ed->seqbasep->last;
2638
2639                 if (first_seq->prev) {
2640                         first_seq->prev->next = NULL;
2641                         ed->seqbasep->last = first_seq->prev;
2642                         first_seq->prev = NULL;
2643                 }
2644         }
2645
2646         seqbase_clipboard_frame = scene->r.cfra;
2647
2648         /* Need to remove anything that references the current scene */
2649         for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2650                 seq_copy_del_sound(scene, seq);
2651         }
2652
2653         return OPERATOR_FINISHED;
2654 }
2655
2656 void SEQUENCER_OT_copy(wmOperatorType *ot)
2657 {
2658         /* identifiers */
2659         ot->name = "Copy";
2660         ot->idname = "SEQUENCER_OT_copy";
2661         ot->description = "";
2662
2663         /* api callbacks */
2664         ot->exec = sequencer_copy_exec;
2665         ot->poll = sequencer_edit_poll;
2666
2667         /* flags */
2668         ot->flag = OPTYPE_REGISTER;
2669
2670         /* properties */
2671 }
2672
2673 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2674 {
2675         Scene *scene = CTX_data_scene(C);
2676         Editing *ed = BKE_sequencer_editing_get(scene, TRUE); /* create if needed */
2677         ListBase nseqbase = {NULL, NULL};
2678         int ofs;
2679         Sequence *iseq;
2680
2681         ED_sequencer_deselect_all(scene);
2682         ofs = scene->r.cfra - seqbase_clipboard_frame;
2683
2684         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2685
2686         /* transform pasted strips before adding */
2687         if (ofs) {
2688                 for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2689                         BKE_sequence_translate(scene, iseq, ofs);
2690                         BKE_sequence_sound_init(scene, iseq);
2691                 }
2692         }
2693
2694         iseq = nseqbase.first;
2695
2696         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2697
2698         /* make sure the pasted strips have unique names between them */
2699         for (; iseq; iseq = iseq->next) {
2700                 BKE_sequencer_recursive_apply(iseq, apply_unique_name_cb, scene);
2701         }
2702
2703         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2704
2705         return OPERATOR_FINISHED;
2706 }
2707
2708 void SEQUENCER_OT_paste(wmOperatorType *ot)
2709 {
2710         /* identifiers */
2711         ot->name = "Paste";
2712         ot->idname = "SEQUENCER_OT_paste";
2713         ot->description = "";
2714
2715         /* api callbacks */
2716         ot->exec = sequencer_paste_exec;
2717         ot->poll = ED_operator_sequencer_active;
2718
2719         /* flags */
2720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2721
2722         /* properties */
2723 }
2724
2725 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2726 {
2727         Scene *scene = CTX_data_scene(C);
2728         Sequence *seq_act;
2729         Sequence *seq_other;
2730         const char *error_msg;
2731
2732         if (BKE_sequencer_active_get_pair(scene, &seq_act, &seq_other) == 0) {
2733                 BKE_report(op->reports, RPT_ERROR, "Must select 2 strips");
2734                 return OPERATOR_CANCELLED;
2735         }
2736
2737         if (BKE_sequence_swap(seq_act, seq_other, &error_msg) == 0) {
2738                 BKE_report(op->reports, RPT_ERROR, error_msg);
2739                 return OPERATOR_CANCELLED;
2740         }
2741
2742         if (seq_act->scene_sound)
2743                 sound_remove_scene_sound(scene, seq_act->scene_sound);
2744
2745         if (seq_other->scene_sound)
2746                 sound_remove_scene_sound(scene, seq_other->scene_sound);
2747
2748         seq_act->scene_sound = NULL;
2749         seq_other->scene_sound = NULL;
2750
2751         BKE_sequence_calc(scene, seq_act);
2752         BKE_sequence_calc(scene, seq_other);
2753
2754         if (seq_act->sound) sound_add_scene_sound_defaults(scene, seq_act);
2755         if (seq_other->sound) sound_add_scene_sound_defaults(scene, seq_other);
2756
2757         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2758
2759         return OPERATOR_FINISHED;
2760 }
2761
2762 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2763 {
2764         /* identifiers */
2765         ot->name = "Sequencer Swap Data";
2766         ot->idname = "SEQUENCER_OT_swap_data";
2767         ot->description = "Swap 2 sequencer strips";
2768
2769         /* api callbacks */
2770         ot->exec = sequencer_swap_data_exec;
2771         ot->poll = ED_operator_sequencer_active;
2772
2773         /* flags */
2774         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2775
2776         /* properties */
2777 }
2778
2779 /* borderselect operator */
2780 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2781 {
2782         Scene *scene = CTX_data_scene(C);
2783         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2784         View2D *v2d = UI_view2d_fromcontext(C);
2785
2786         rctf rect;
2787
2788         /* convert coordinates of rect to 'tot' rect coordinates */
2789         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2790         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2791
2792         if (ed == NULL)
2793                 return OPERATOR_CANCELLED;
2794
2795         rect.xmin /=  (float)(ABS(BLI_RCT_SIZE_X(&v2d->tot)));
2796         rect.ymin /=  (float)(ABS(BLI_RCT_SIZE_Y(&v2d->tot)));
2797
2798         rect.xmax /=  (float)(ABS(BLI_RCT_SIZE_X(&v2d->tot)));
2799         rect.ymax /=  (float)(ABS(BLI_RCT_SIZE_Y(&v2d->tot)));
2800
2801         rect.xmin += 0.5f;
2802         rect.xmax += 0.5f;
2803         rect.ymin += 0.5f;
2804         rect.ymax += 0.5f;
2805
2806         CLAMP(rect.xmin, 0.0f, 1.0f);
2807         CLAMP(rect.ymin, 0.0f, 1.0f);
2808         CLAMP(rect.xmax, 0.0f, 1.0f);
2809         CLAMP(rect.ymax, 0.0f, 1.0f);
2810
2811         scene->ed->over_border = rect;
2812
2813         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2814
2815         return OPERATOR_FINISHED;
2816 }
2817
2818 /* ****** Border Select ****** */
2819 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2820 {
2821         /* identifiers */
2822         ot->name = "Border Offset View";
2823         ot->idname = "SEQUENCER_OT_view_ghost_border";
2824         ot->description = "Enable border select mode";
2825
2826         /* api callbacks */
2827         ot->invoke = WM_border_select_invoke;
2828         ot->exec = view_ghost_border_exec;
2829         ot->modal = WM_border_select_modal;
2830         ot->poll = sequencer_view_poll;
2831         ot->cancel = WM_border_select_cancel;
2832
2833         /* flags */
2834         ot->flag = 0;
2835
2836         /* rna */
2837         WM_operator_properties_gesture_border(ot, FALSE);
2838 }
2839
2840 /* rebuild_proxy operator */
2841 static int sequencer_rebuild_proxy_exec(bContext *C, wmOperator *UNUSED(op))
2842 {
2843         seq_proxy_build_job(C);
2844
2845         return OPERATOR_FINISHED;
2846 }
2847
2848 void SEQUENCER_OT_rebuild_proxy(wmOperatorType *ot)
2849 {
2850         /* identifiers */
2851         ot->name = "Rebuild Proxy and Timecode Indices";
2852         ot->idname = "SEQUENCER_OT_rebuild_proxy";
2853         ot->description = "Rebuild all selected proxies and timecode indices using the job system";
2854         
2855         /* api callbacks */
2856         ot->exec = sequencer_rebuild_proxy_exec;
2857         ot->poll = ED_operator_sequencer_active;
2858         
2859         /* flags */
2860         ot->flag = OPTYPE_REGISTER;
2861 }
2862
2863 /* change ops */
2864
2865 static EnumPropertyItem prop_change_effect_input_types[] = {
2866         {0, "A_B", 0, "A -> B", ""},
2867         {1, "B_C", 0, "B -> C", ""},
2868         {2, "A_C", 0, "A -> C", ""},
2869         {0, NULL, 0, NULL, NULL}
2870 };
2871
2872 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
2873 {
2874         Scene *scene = CTX_data_scene(C);
2875         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2876         Sequence *seq = BKE_sequencer_active_get(scene);
2877
2878         Sequence **seq_1, **seq_2;
2879
2880         switch (RNA_enum_get(op->ptr, "swap")) {
2881                 case 0:
2882                         seq_1 = &seq->seq1;
2883                         seq_2 = &seq->seq2;
2884                         break;
2885                 case 1:
2886                         seq_1 = &seq->seq2;
2887                         seq_2 = &seq->seq3;
2888                         break;
2889                 default: /* 2 */
2890                         seq_1 = &seq->seq1;
2891                         seq_2 = &seq->seq3;
2892                         break;
2893         }
2894
2895         if (*seq_1 == NULL || *seq_2 == NULL) {
2896                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, can't swap");
2897                 return OPERATOR_CANCELLED;
2898         }
2899         else {
2900                 SWAP(Sequence *, *seq_1, *seq_2);
2901         }
2902
2903         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
2904
2905         /* important else we don't get the imbuf cache flushed */
2906         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
2907
2908         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2909
2910         return OPERATOR_FINISHED;
2911 }
2912
2913 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
2914 {
2915         /* identifiers */
2916         ot->name = "Change Effect Input";
2917         ot->idname = "SEQUENCER_OT_change_effect_input";
2918         ot->description = "";
2919
2920         /* api callbacks */
2921         ot->exec = sequencer_change_effect_input_exec;
2922         ot->poll = sequencer_effect_poll;
2923
2924         /* flags */
2925         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2926
2927         ot->prop = RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
2928 }
2929
2930 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
2931 {
2932         Scene *scene = CTX_data_scene(C);
2933         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2934         Sequence *seq = BKE_sequencer_active_get(scene);
2935         const int new_type = RNA_enum_get(op->ptr, "type");
2936
2937         /* free previous effect and init new effect */
2938         struct SeqEffectHandle sh;
2939
2940         if ((seq->type & SEQ_TYPE_EFFECT) == 0) {
2941                 return OPERATOR_CANCELLED;
2942         }
2943
2944         /* can someone explain the logic behind only allowing to increase this,
2945          * copied from 2.4x - campbell */
2946         if (BKE_sequence_effect_get_num_inputs(seq->type) <
2947             BKE_sequence_effect_get_num_inputs(new_type))
2948         {
2949                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
2950                 return OPERATOR_CANCELLED;
2951         }
2952         else {
2953                 sh = BKE_sequence_get_effect(seq);
2954                 sh.free(seq);
2955
2956                 seq->type = new_type;
2957
2958                 sh = BKE_sequence_get_effect(seq);
2959                 sh.init(seq);
2960         }
2961
2962         /* update */
2963         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
2964
2965         /* important else we don't get the imbuf cache flushed */
2966         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
2967
2968         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2969
2970         return OPERATOR_FINISHED;
2971 }
2972
2973 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
2974 {
2975         /* identifiers */
2976         ot->name = "Change Effect Type";
2977         ot->idname = "SEQUENCER_OT_change_effect_type";
2978         ot->description = "";
2979
2980         /* api callbacks */
2981         ot->exec = sequencer_change_effect_type_exec;
2982         ot->poll = sequencer_effect_poll;
2983
2984         /* flags */
2985         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2986
2987         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_TYPE_CROSS, "Type", "Sequencer effect type");
2988 }
2989
2990 static int sequencer_change_path_exec(bContext *C, wmOperator *op)
2991 {
2992         Main *bmain = CTX_data_main(C);
2993         Scene *scene = CTX_data_scene(C);
2994         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2995         Sequence *seq = BKE_sequencer_active_get(scene);
2996         const int is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
2997
2998         if (seq->type == SEQ_TYPE_IMAGE) {
2999                 char directory[FILE_MAX];
3000                 const int len = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
3001                 StripElem *se;
3002
3003                 if (len == 0)
3004                         return OPERATOR_CANCELLED;
3005
3006                 RNA_string_get(op->ptr, "directory", directory);
3007                 if (is_relative_path) {
3008                         /* TODO, shouldn't this already be relative from the filesel?
3009                          * (as the 'filepath' is) for now just make relative here,
3010                          * but look into changing after 2.60 - campbell */
3011                         BLI_path_rel(directory, bmain->name);
3012                 }
3013                 BLI_strncpy(seq->strip->dir, directory, sizeof(seq->strip->dir));
3014
3015                 if (seq->strip->stripdata) {
3016                         MEM_freeN(seq->strip->stripdata);
3017                 }
3018                 seq->strip->stripdata = se = MEM_callocN(len * sizeof(StripElem), "stripelem");
3019
3020                 RNA_BEGIN (op->ptr, itemptr, "files")
3021                 {
3022                         char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
3023                         BLI_strncpy(se->name, filename, sizeof(se->name));
3024                         MEM_freeN(filename);
3025                         se++;
3026                 }
3027                 RNA_END;
3028
3029                 /* reset these else we wont see all the images */
3030                 seq->anim_startofs = seq->anim_endofs = 0;
3031
3032                 /* correct start/end frames so we don't move
3033                  * important not to set seq->len= len; allow the function to handle it */
3034                 BKE_sequence_reload_new_file(scene, seq, TRUE);
3035
3036                 BKE_sequence_calc(scene, seq);
3037
3038                 /* important else we don't get the imbuf cache flushed */
3039                 BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3040         }
3041         else {
3042                 /* lame, set rna filepath */
3043                 PointerRNA seq_ptr;
3044                 PropertyRNA *prop;
3045                 char filepath[FILE_MAX];
3046
3047                 RNA_pointer_create(&scene->id, &RNA_Sequence, seq, &seq_ptr);
3048
3049                 RNA_string_get(op->ptr, "filepath", filepath);
3050                 prop = RNA_struct_find_property(&seq_ptr, "filepath");
3051                 RNA_property_string_set(&seq_ptr, prop, filepath);
3052                 RNA_property_update(C, &seq_ptr, prop);
3053         }
3054
3055         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3056
3057         return OPERATOR_FINISHED;
3058 }
3059
3060 static int sequencer_change_path_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
3061 {
3062         Scene *scene = CTX_data_scene(C);
3063         Sequence *seq = BKE_sequencer_active_get(scene);
3064
3065         RNA_string_set(op->ptr, "directory", seq->strip->dir);
3066
3067         /* set default display depending on seq type */
3068         if (seq->type == SEQ_TYPE_IMAGE) {
3069                 RNA_boolean_set(op->ptr, "filter_movie", FALSE);
3070         }
3071         else {
3072                 RNA_boolean_set(op->ptr, "filter_image", FALSE);
3073         }
3074
3075         WM_event_add_fileselect(C, op);
3076
3077         return OPERATOR_RUNNING_MODAL;
3078 }
3079
3080 void SEQUENCER_OT_change_path(struct wmOperatorType *ot)
3081 {
3082         /* identifiers */
3083         ot->name = "Change Data/Files";
3084         ot->idname = "SEQUENCER_OT_change_path";
3085         ot->description = "";
3086
3087         /* api callbacks */
3088         ot->exec = sequencer_change_path_exec;
3089         ot->invoke = sequencer_change_path_invoke;
3090         ot->poll = sequencer_strip_has_path_poll;
3091
3092         /* flags */
3093         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3094
3095         WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
3096                                        WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH | WM_FILESEL_FILEPATH | WM_FILESEL_FILES,
3097                                        FILE_DEFAULTDISPLAY);
3098 }
3099