56fdccfb8f89a9099215fd6d4c78a1a1f5bd650c
[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_rctf_size_x(&v2d->cur) / BLI_rcti_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                         /* pass */
584                 }
585                 else if (seq1 || seq2 || seq3) {
586                         seq->seq1 = (seq1) ? seq1 : (seq2) ? seq2 : seq3;
587                         seq->seq2 = (seq2) ? seq2 : (seq1) ? seq1 : seq3;
588                         seq->seq3 = (seq3) ? seq3 : (seq1) ? seq1 : seq2;
589
590                         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 1, 1);
591                 }
592                 else
593                         seq->flag |= SELECT;  /* mark for delete */
594         }
595
596         if (seq->flag & SELECT) {
597                 if ((seq1 = del_seq_find_replace_recurs(scene, seq->seq1))) return seq1;
598                 if ((seq2 = del_seq_find_replace_recurs(scene, seq->seq2))) return seq2;
599                 if ((seq3 = del_seq_find_replace_recurs(scene, seq->seq3))) return seq3;
600                 else return NULL;
601         }
602         else
603                 return seq;
604 }
605
606 static void recurs_del_seq_flag(Scene *scene, ListBase *lb, short flag, short deleteall)
607 {
608         Sequence *seq, *seqn;
609         Sequence *last_seq = BKE_sequencer_active_get(scene);
610
611         seq = lb->first;
612         while (seq) {
613                 seqn = seq->next;
614                 if ((seq->flag & flag) || deleteall) {
615                         BLI_remlink(lb, seq);
616                         if (seq == last_seq) BKE_sequencer_active_set(scene, NULL);
617                         if (seq->type == SEQ_TYPE_META) recurs_del_seq_flag(scene, &seq->seqbase, flag, 1);
618                         BKE_sequence_free(scene, seq);
619                 }
620                 seq = seqn;
621         }
622 }
623
624
625 static Sequence *cut_seq_hard(Scene *scene, Sequence *seq, int cutframe)
626 {
627         TransSeq ts;
628         Sequence *seqn = NULL;
629         int skip_dup = FALSE;
630
631         /* backup values */
632         ts.start = seq->start;
633         ts.machine = seq->machine;
634         ts.startstill = seq->startstill;
635         ts.endstill = seq->endstill;
636         ts.startdisp = seq->startdisp;
637         ts.enddisp = seq->enddisp;
638         ts.startofs = seq->startofs;
639         ts.endofs = seq->endofs;
640         ts.anim_startofs = seq->anim_startofs;
641         ts.anim_endofs = seq->anim_endofs;
642         ts.len = seq->len;
643         
644         /* First Strip! */
645         /* strips with extended stillfames before */
646         
647         if ((seq->startstill) && (cutframe < seq->start)) {
648                 /* don't do funny things with METAs ... */
649                 if (seq->type == SEQ_TYPE_META) {
650                         skip_dup = TRUE;
651                         seq->startstill = seq->start - cutframe;
652                 }
653                 else {
654                         seq->start = cutframe - 1;
655                         seq->startstill = cutframe - seq->startdisp - 1;
656                         seq->anim_endofs += seq->len - 1;
657                         seq->endstill = 0;
658                 }
659         }
660         /* normal strip */
661         else if ((cutframe >= seq->start) && (cutframe <= (seq->start + seq->len))) {
662                 seq->endofs = 0;
663                 seq->endstill = 0;
664                 seq->anim_endofs += (seq->start + seq->len) - cutframe;
665         }
666         /* strips with extended stillframes after */
667         else if (((seq->start + seq->len) < cutframe) && (seq->endstill)) {
668                 seq->endstill -= seq->enddisp - cutframe;
669                 /* don't do funny things with METAs ... */
670                 if (seq->type == SEQ_TYPE_META) {
671                         skip_dup = TRUE;
672                 }
673         }
674         
675         BKE_sequence_reload_new_file(scene, seq, FALSE);
676         BKE_sequence_calc(scene, seq);
677
678         if (!skip_dup) {
679                 /* Duplicate AFTER the first change */
680                 seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
681         }
682         
683         if (seqn) {
684                 seqn->flag |= SELECT;
685                         
686                 /* Second Strip! */
687                 /* strips with extended stillframes before */
688                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
689                         seqn->start = ts.start;
690                         seqn->startstill = ts.start - cutframe;
691                         seqn->anim_endofs = ts.anim_endofs;
692                         seqn->endstill = ts.endstill;
693                 }
694                 
695                 /* normal strip */
696                 else if ((cutframe >= seqn->start) && (cutframe <= (seqn->start + seqn->len))) {
697                         seqn->start = cutframe;
698                         seqn->startstill = 0;
699                         seqn->startofs = 0;
700                         seqn->endofs = ts.endofs;
701                         seqn->anim_startofs += cutframe - ts.start;
702                         seqn->anim_endofs = ts.anim_endofs;
703                         seqn->endstill = ts.endstill;
704                 }
705                 
706                 /* strips with extended stillframes after */
707                 else if (((seqn->start + seqn->len) < cutframe) && (seqn->endstill)) {
708                         seqn->start = cutframe;
709                         seqn->startofs = 0;
710                         seqn->anim_startofs += ts.len - 1;
711                         seqn->endstill = ts.enddisp - cutframe - 1;
712                         seqn->startstill = 0;
713                 }
714                 
715                 BKE_sequence_reload_new_file(scene, seqn, FALSE);
716                 BKE_sequence_calc(scene, seqn);
717         }
718         return seqn;
719 }
720
721 static Sequence *cut_seq_soft(Scene *scene, Sequence *seq, int cutframe)
722 {
723         TransSeq ts;
724         Sequence *seqn = NULL;
725         int skip_dup = FALSE;
726
727         /* backup values */
728         ts.start = seq->start;
729         ts.machine = seq->machine;
730         ts.startstill = seq->startstill;
731         ts.endstill = seq->endstill;
732         ts.startdisp = seq->startdisp;
733         ts.enddisp = seq->enddisp;
734         ts.startofs = seq->startofs;
735         ts.endofs = seq->endofs;
736         ts.anim_startofs = seq->anim_startofs;
737         ts.anim_endofs = seq->anim_endofs;
738         ts.len = seq->len;
739         
740         /* First Strip! */
741         /* strips with extended stillfames before */
742         
743         if ((seq->startstill) && (cutframe < seq->start)) {
744                 /* don't do funny things with METAs ... */
745                 if (seq->type == SEQ_TYPE_META) {
746                         skip_dup = TRUE;
747                         seq->startstill = seq->start - cutframe;
748                 }
749                 else {
750                         seq->start = cutframe - 1;
751                         seq->startstill = cutframe - seq->startdisp - 1;
752                         seq->endofs = seq->len - 1;
753                         seq->endstill = 0;
754                 }
755         }
756         /* normal strip */
757         else if ((cutframe >= seq->start) && (cutframe <= (seq->start + seq->len))) {
758                 seq->endofs = (seq->start + seq->len) - cutframe;
759         }
760         /* strips with extended stillframes after */
761         else if (((seq->start + seq->len) < cutframe) && (seq->endstill)) {
762                 seq->endstill -= seq->enddisp - cutframe;
763                 /* don't do funny things with METAs ... */
764                 if (seq->type == SEQ_TYPE_META) {
765                         skip_dup = TRUE;
766                 }
767         }
768         
769         BKE_sequence_calc(scene, seq);
770
771         if (!skip_dup) {
772                 /* Duplicate AFTER the first change */
773                 seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM);
774         }
775         
776         if (seqn) {
777                 seqn->flag |= SELECT;
778                         
779                 /* Second Strip! */
780                 /* strips with extended stillframes before */
781                 if ((seqn->startstill) && (cutframe == seqn->start + 1)) {
782                         seqn->start = ts.start;
783                         seqn->startstill = ts.start - cutframe;
784                         seqn->endofs = ts.endofs;
785                         seqn->endstill = ts.endstill;
786                 }
787                 
788                 /* normal strip */
789                 else if ((cutframe >= seqn->start) && (cutframe <= (seqn->start + seqn->len))) {
790                         seqn->startstill = 0;
791                         seqn->startofs = cutframe - ts.start;
792                         seqn->endofs = ts.endofs;
793                         seqn->endstill = ts.endstill;
794                 }
795                 
796                 /* strips with extended stillframes after */
797                 else if (((seqn->start + seqn->len) < cutframe) && (seqn->endstill)) {
798                         seqn->start = cutframe - ts.len + 1;
799                         seqn->startofs = ts.len - 1;
800                         seqn->endstill = ts.enddisp - cutframe - 1;
801                         seqn->startstill = 0;
802                 }
803                 
804                 BKE_sequence_calc(scene, seqn);
805         }
806         return seqn;
807 }
808
809
810 /* like duplicate, but only duplicate and cut overlapping strips,
811  * strips to the left of the cutframe are ignored and strips to the right are moved into the new list */
812 static int cut_seq_list(Scene *scene, ListBase *old, ListBase *new, int cutframe,
813                         Sequence * (*cut_seq)(Scene *, Sequence *, int))
814 {
815         int did_something = FALSE;
816         Sequence *seq, *seq_next_iter;
817         
818         seq = old->first;
819         
820         while (seq) {
821                 seq_next_iter = seq->next; /* we need this because we may remove seq */
822                 
823                 seq->tmp = NULL;
824                 if (seq->flag & SELECT) {
825                         if (cutframe > seq->startdisp && 
826                             cutframe < seq->enddisp)
827                         {
828                                 Sequence *seqn = cut_seq(scene, seq, cutframe);
829                                 if (seqn) {
830                                         BLI_addtail(new, seqn);
831                                 }
832                                 did_something = TRUE;
833                         }
834                         else if (seq->enddisp <= cutframe) {
835                                 /* do nothing */
836                         }
837                         else if (seq->startdisp >= cutframe) {
838                                 /* move into new list */
839                                 BLI_remlink(old, seq);
840                                 BLI_addtail(new, seq);
841                         }
842                 }
843                 seq = seq_next_iter;
844         }
845         return did_something;
846 }
847
848 static int insert_gap(Scene *scene, int gap, int cfra)
849 {
850         Sequence *seq;
851         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
852         int done = FALSE;
853
854         /* all strips >= cfra are shifted */
855         
856         if (ed == NULL) return 0;
857
858         SEQP_BEGIN (ed, seq)
859         {
860                 if (seq->startdisp >= cfra) {
861                         seq->start += gap;
862                         BKE_sequence_calc(scene, seq);
863                         done = TRUE;
864                 }
865         }
866         SEQ_END
867
868         return done;
869 }
870
871 static void UNUSED_FUNCTION(touch_seq_files) (Scene * scene)
872 {
873         Sequence *seq;
874         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
875         char str[256];
876
877         /* touch all strips with movies */
878         
879         if (ed == NULL) return;
880
881         // XXX25 if (okee("Touch and print selected movies")==0) return;
882
883         WM_cursor_wait(1);
884
885         SEQP_BEGIN (ed, seq)
886         {
887                 if (seq->flag & SELECT) {
888                         if (seq->type == SEQ_TYPE_MOVIE) {
889                                 if (seq->strip && seq->strip->stripdata) {
890                                         BLI_make_file_string(G.main->name, str, seq->strip->dir, seq->strip->stripdata->name);
891                                         BLI_file_touch(seq->name);
892                                 }
893                         }
894
895                 }
896         }
897         SEQ_END
898
899         WM_cursor_wait(0);
900 }
901
902 #if 0
903 static void set_filter_seq(Scene *scene)
904 {
905         Sequence *seq;
906         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
907
908         
909         if (ed == NULL) return;
910
911         if (okee("Set Deinterlace") == 0) return;
912
913         SEQP_BEGIN (ed, seq)
914         {
915                 if (seq->flag & SELECT) {
916                         if (seq->type == SEQ_TYPE_MOVIE) {
917                                 seq->flag |= SEQ_FILTERY;
918                                 reload_sequence_new_file(scene, seq, FALSE);
919                                 calc_sequence(scene, seq);
920                         }
921
922                 }
923         }
924         SEQ_END
925 }
926 #endif
927
928 static void UNUSED_FUNCTION(seq_remap_paths) (Scene * scene)
929 {
930         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene);
931         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
932         char from[FILE_MAX], to[FILE_MAX], stripped[FILE_MAX];
933         
934         
935         if (last_seq == NULL)
936                 return;
937         
938         BLI_strncpy(from, last_seq->strip->dir, sizeof(from));
939 // XXX  if (0 == sbutton(from, 0, sizeof(from)-1, "From: "))
940 //              return;
941         
942         BLI_strncpy(to, from, sizeof(to));
943 // XXX  if (0 == sbutton(to, 0, sizeof(to)-1, "To: "))
944 //              return;
945         
946         if (strcmp(to, from) == 0)
947                 return;
948         
949         SEQP_BEGIN (ed, seq)
950         {
951                 if (seq->flag & SELECT) {
952                         if (strncmp(seq->strip->dir, from, strlen(from)) == 0) {
953                                 printf("found %s\n", seq->strip->dir);
954                                 
955                                 /* strip off the beginning */
956                                 stripped[0] = 0;
957                                 BLI_strncpy(stripped, seq->strip->dir + strlen(from), FILE_MAX);
958                                 
959                                 /* new path */
960                                 BLI_snprintf(seq->strip->dir, sizeof(seq->strip->dir), "%s%s", to, stripped);
961                                 printf("new %s\n", seq->strip->dir);
962                         }
963                 }
964         }
965         SEQ_END
966                 
967 }
968
969
970 static void UNUSED_FUNCTION(no_gaps) (Scene * scene)
971 {
972         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
973         int cfra, first = 0, done;
974
975         
976         if (ed == NULL) return;
977
978         for (cfra = CFRA; cfra <= EFRA; cfra++) {
979                 if (first == 0) {
980                         if (BKE_sequencer_evaluate_frame(scene, cfra) ) first = 1;
981                 }
982                 else {
983                         done = TRUE;
984                         while (BKE_sequencer_evaluate_frame(scene, cfra) == 0) {
985                                 done = insert_gap(scene, -1, cfra);
986                                 if (done == 0) break;
987                         }
988                         if (done == 0) break;
989                 }
990         }
991
992 }
993
994 #if 0
995 static int seq_get_snaplimit(View2D *v2d)
996 {
997         /* fake mouse coords to get the snap value
998          * a bit lazy but its only done once pre transform */
999         float xmouse, ymouse, x;
1000         int mval[2] = {24, 0}; /* 24 screen px snap */
1001         
1002         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1003         x = xmouse;
1004         mval[0] = 0;
1005         UI_view2d_region_to_view(v2d, mval[0], mval[1], &xmouse, &ymouse);
1006         return (int)(x - xmouse);
1007 }
1008 #endif
1009
1010 /* Operator functions */
1011 int sequencer_edit_poll(bContext *C)
1012 {
1013         return (BKE_sequencer_editing_get(CTX_data_scene(C), FALSE) != NULL);
1014 }
1015
1016 #if 0 /* UNUSED */
1017 int sequencer_strip_poll(bContext *C)
1018 {
1019         Editing *ed;
1020         return (((ed = BKE_sequencer_editing_get(CTX_data_scene(C), FALSE)) != NULL) && (ed->act_seq != NULL));
1021 }
1022 #endif
1023
1024 int sequencer_strip_has_path_poll(bContext *C)
1025 {
1026         Editing *ed;
1027         Sequence *seq;
1028         return (((ed = BKE_sequencer_editing_get(CTX_data_scene(C), FALSE)) != NULL) && ((seq = ed->act_seq) != NULL) && (SEQ_HAS_PATH(seq)));
1029 }
1030
1031 int sequencer_view_poll(bContext *C)
1032 {
1033         SpaceSeq *sseq = CTX_wm_space_seq(C);
1034         Editing *ed = BKE_sequencer_editing_get(CTX_data_scene(C), FALSE);
1035         if (ed && sseq && (sseq->mainb == SEQ_DRAW_IMG_IMBUF))
1036                 return 1;
1037
1038         return 0;
1039 }
1040
1041 /* snap operator*/
1042 static int sequencer_snap_exec(bContext *C, wmOperator *op)
1043 {
1044         Scene *scene = CTX_data_scene(C);
1045         
1046         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1047         Sequence *seq;
1048         int snap_frame;
1049
1050         snap_frame = RNA_int_get(op->ptr, "frame");
1051
1052         /* also check metas */
1053         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1054                 if (seq->flag & SELECT && !(seq->depth == 0 && seq->flag & SEQ_LOCK) &&
1055                     BKE_sequence_tx_test(seq))
1056                 {
1057                         if ((seq->flag & (SEQ_LEFTSEL + SEQ_RIGHTSEL)) == 0) {
1058                                 /* simple but no anim update */
1059                                 /* seq->start = snap_frame-seq->startofs+seq->startstill; */
1060
1061                                 BKE_sequence_translate(scene, seq, (snap_frame - seq->startofs + seq->startstill) - seq->start);
1062                         }
1063                         else {
1064                                 if (seq->flag & SEQ_LEFTSEL) {
1065                                         BKE_sequence_tx_set_final_left(seq, snap_frame);
1066                                 }
1067                                 else { /* SEQ_RIGHTSEL */
1068                                         BKE_sequence_tx_set_final_right(seq, snap_frame);
1069                                 }
1070                                 BKE_sequence_tx_handle_xlimits(seq, seq->flag & SEQ_LEFTSEL, seq->flag & SEQ_RIGHTSEL);
1071                         }
1072                         BKE_sequence_calc(scene, seq);
1073                 }
1074         }
1075
1076         /* test for effects and overlap
1077          * don't use SEQP_BEGIN since that would be recursive */
1078         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1079                 if (seq->flag & SELECT && !(seq->depth == 0 && seq->flag & SEQ_LOCK)) {
1080                         seq->flag &= ~SEQ_OVERLAP;
1081                         if (BKE_sequence_test_overlap(ed->seqbasep, seq) ) {
1082                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1083                         }
1084                 }
1085                 else if (seq->type & SEQ_TYPE_EFFECT) {
1086                         if (seq->seq1 && (seq->seq1->flag & SELECT)) 
1087                                 BKE_sequence_calc(scene, seq);
1088                         else if (seq->seq2 && (seq->seq2->flag & SELECT)) 
1089                                 BKE_sequence_calc(scene, seq);
1090                         else if (seq->seq3 && (seq->seq3->flag & SELECT)) 
1091                                 BKE_sequence_calc(scene, seq);
1092                 }
1093         }
1094
1095         /* as last: */
1096         BKE_sequencer_sort(scene);
1097         
1098         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1099         
1100         return OPERATOR_FINISHED;
1101 }
1102
1103 static int sequencer_snap_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1104 {
1105         Scene *scene = CTX_data_scene(C);
1106         
1107         int snap_frame;
1108         
1109         snap_frame = CFRA;
1110         
1111         RNA_int_set(op->ptr, "frame", snap_frame);
1112         return sequencer_snap_exec(C, op);
1113 }
1114
1115 void SEQUENCER_OT_snap(struct wmOperatorType *ot)
1116 {
1117         /* identifiers */
1118         ot->name = "Snap strips";
1119         ot->idname = "SEQUENCER_OT_snap";
1120         ot->description = "Frame where selected strips will be snapped";
1121         
1122         /* api callbacks */
1123         ot->invoke = sequencer_snap_invoke;
1124         ot->exec = sequencer_snap_exec;
1125         ot->poll = sequencer_edit_poll;
1126         
1127         /* flags */
1128         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1129         
1130         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be snapped", INT_MIN, INT_MAX);
1131 }
1132
1133 /* mute operator */
1134 static int sequencer_mute_exec(bContext *C, wmOperator *op)
1135 {
1136         Scene *scene = CTX_data_scene(C);
1137         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1138         Sequence *seq;
1139         int selected;
1140
1141         selected = !RNA_boolean_get(op->ptr, "unselected");
1142         
1143         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1144                 if ((seq->flag & SEQ_LOCK) == 0) {
1145                         if (selected) { /* mute unselected */
1146                                 if (seq->flag & SELECT) {
1147                                         seq->flag |= SEQ_MUTE;
1148                                         BKE_sequence_invalidate_dependent(scene, seq);
1149                                 }
1150                         }
1151                         else {
1152                                 if ((seq->flag & SELECT) == 0) {
1153                                         seq->flag |= SEQ_MUTE;
1154                                         BKE_sequence_invalidate_dependent(scene, seq);
1155                                 }
1156                         }
1157                 }
1158         }
1159         
1160         BKE_sequencer_update_muting(ed);
1161         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1162         
1163         return OPERATOR_FINISHED;
1164 }
1165
1166 void SEQUENCER_OT_mute(struct wmOperatorType *ot)
1167 {
1168         /* identifiers */
1169         ot->name = "Mute Strips";
1170         ot->idname = "SEQUENCER_OT_mute";
1171         ot->description = "Mute selected strips";
1172         
1173         /* api callbacks */
1174         ot->exec = sequencer_mute_exec;
1175         ot->poll = sequencer_edit_poll;
1176         
1177         /* flags */
1178         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1179         
1180         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Mute unselected rather than selected strips");
1181 }
1182
1183
1184 /* unmute operator */
1185 static int sequencer_unmute_exec(bContext *C, wmOperator *op)
1186 {
1187         Scene *scene = CTX_data_scene(C);
1188         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1189         Sequence *seq;
1190         int selected;
1191
1192         selected = !RNA_boolean_get(op->ptr, "unselected");
1193         
1194         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1195                 if ((seq->flag & SEQ_LOCK) == 0) {
1196                         if (selected) { /* unmute unselected */
1197                                 if (seq->flag & SELECT) {
1198                                         seq->flag &= ~SEQ_MUTE;
1199                                         BKE_sequence_invalidate_dependent(scene, seq);
1200                                 }
1201                         }
1202                         else {
1203                                 if ((seq->flag & SELECT) == 0) {
1204                                         seq->flag &= ~SEQ_MUTE;
1205                                         BKE_sequence_invalidate_dependent(scene, seq);
1206                                 }
1207                         }
1208                 }
1209         }
1210         
1211         BKE_sequencer_update_muting(ed);
1212         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1213         
1214         return OPERATOR_FINISHED;
1215 }
1216
1217 void SEQUENCER_OT_unmute(struct wmOperatorType *ot)
1218 {
1219         /* identifiers */
1220         ot->name = "Un-Mute Strips";
1221         ot->idname = "SEQUENCER_OT_unmute";
1222         ot->description = "Un-Mute unselected rather than selected strips";
1223         
1224         /* api callbacks */
1225         ot->exec = sequencer_unmute_exec;
1226         ot->poll = sequencer_edit_poll;
1227         
1228         /* flags */
1229         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1230         
1231         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "UnMute unselected rather than selected strips");
1232 }
1233
1234
1235 /* lock operator */
1236 static int sequencer_lock_exec(bContext *C, wmOperator *UNUSED(op))
1237 {
1238         Scene *scene = CTX_data_scene(C);
1239         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1240         Sequence *seq;
1241
1242         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1243                 if (seq->flag & SELECT) {
1244                         seq->flag |= SEQ_LOCK;
1245                 }
1246         }
1247
1248         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1249
1250         return OPERATOR_FINISHED;
1251 }
1252
1253 void SEQUENCER_OT_lock(struct wmOperatorType *ot)
1254 {
1255         /* identifiers */
1256         ot->name = "Lock Strips";
1257         ot->idname = "SEQUENCER_OT_lock";
1258         ot->description = "Lock the active strip so that it can't be transformed";
1259         
1260         /* api callbacks */
1261         ot->exec = sequencer_lock_exec;
1262         ot->poll = sequencer_edit_poll;
1263         
1264         /* flags */
1265         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1266 }
1267
1268 /* unlock operator */
1269 static int sequencer_unlock_exec(bContext *C, wmOperator *UNUSED(op))
1270 {
1271         Scene *scene = CTX_data_scene(C);
1272         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1273         Sequence *seq;
1274
1275         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1276                 if (seq->flag & SELECT) {
1277                         seq->flag &= ~SEQ_LOCK;
1278                 }
1279         }
1280
1281         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1282
1283         return OPERATOR_FINISHED;
1284 }
1285
1286 void SEQUENCER_OT_unlock(struct wmOperatorType *ot)
1287 {
1288         /* identifiers */
1289         ot->name = "UnLock Strips";
1290         ot->idname = "SEQUENCER_OT_unlock";
1291         ot->description = "Unlock the active strip so that it can't be transformed";
1292         
1293         /* api callbacks */
1294         ot->exec = sequencer_unlock_exec;
1295         ot->poll = sequencer_edit_poll;
1296         
1297         /* flags */
1298         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1299 }
1300
1301 /* reload operator */
1302 static int sequencer_reload_exec(bContext *C, wmOperator *op)
1303 {
1304         Scene *scene = CTX_data_scene(C);
1305         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1306         Sequence *seq;
1307         int adjust_length = RNA_boolean_get(op->ptr, "adjust_length");
1308
1309         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1310                 if (seq->flag & SELECT) {
1311                         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
1312                         BKE_sequence_reload_new_file(scene, seq, !adjust_length);
1313
1314                         if (adjust_length) {
1315                                 if (BKE_sequence_test_overlap(ed->seqbasep, seq))
1316                                         BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1317                         }
1318                 }
1319         }
1320
1321         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1322
1323         return OPERATOR_FINISHED;
1324 }
1325
1326 void SEQUENCER_OT_reload(struct wmOperatorType *ot)
1327 {
1328         PropertyRNA *prop;
1329
1330         /* identifiers */
1331         ot->name = "Reload Strips";
1332         ot->idname = "SEQUENCER_OT_reload";
1333         ot->description = "Reload strips in the sequencer";
1334         
1335         /* api callbacks */
1336         ot->exec = sequencer_reload_exec;
1337         ot->poll = sequencer_edit_poll;
1338         
1339         /* flags */
1340         ot->flag = OPTYPE_REGISTER; /* no undo, the data changed is stored outside 'main' */
1341
1342         prop = RNA_def_boolean(ot->srna, "adjust_length", 0, "Adjust Length",
1343                                "Adjust length of strips to their data length");
1344         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1345 }
1346
1347 /* reload operator */
1348 static int sequencer_refresh_all_exec(bContext *C, wmOperator *UNUSED(op))
1349 {
1350         Scene *scene = CTX_data_scene(C);
1351         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1352
1353         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
1354
1355         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1356
1357         return OPERATOR_FINISHED;
1358 }
1359
1360 void SEQUENCER_OT_refresh_all(struct wmOperatorType *ot)
1361 {
1362         /* identifiers */
1363         ot->name = "Refresh Sequencer";
1364         ot->idname = "SEQUENCER_OT_refresh_all";
1365         ot->description = "Refresh the sequencer editor";
1366         
1367         /* api callbacks */
1368         ot->exec = sequencer_refresh_all_exec;
1369         ot->poll = sequencer_edit_poll;
1370 }
1371
1372 static int sequencer_reassign_inputs_exec(bContext *C, wmOperator *op)
1373 {
1374         Scene *scene = CTX_data_scene(C);
1375         Sequence *seq1, *seq2, *seq3, *last_seq = BKE_sequencer_active_get(scene);
1376         const char *error_msg;
1377
1378         if (!seq_effect_find_selected(scene, last_seq, last_seq->type, &seq1, &seq2, &seq3, &error_msg)) {
1379                 BKE_report(op->reports, RPT_ERROR, error_msg);
1380                 return OPERATOR_CANCELLED;
1381         }
1382         /* see reassigning would create a cycle */
1383         if (seq_is_predecessor(seq1, last_seq) ||
1384             seq_is_predecessor(seq2, last_seq) ||
1385             seq_is_predecessor(seq3, last_seq))
1386         {
1387                 BKE_report(op->reports, RPT_ERROR, "Can't reassign inputs: no cycles allowed");
1388                 return OPERATOR_CANCELLED;
1389         }
1390
1391         last_seq->seq1 = seq1;
1392         last_seq->seq2 = seq2;
1393         last_seq->seq3 = seq3;
1394
1395         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
1396
1397         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1398
1399         return OPERATOR_FINISHED;
1400 }
1401
1402 static int sequencer_effect_poll(bContext *C)
1403 {
1404         Scene *scene = CTX_data_scene(C);
1405         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1406
1407         if (ed) {
1408                 Sequence *last_seq = BKE_sequencer_active_get(scene);
1409                 if (last_seq && (last_seq->type & SEQ_TYPE_EFFECT)) {
1410                         return 1;
1411                 }
1412         }
1413
1414         return 0;
1415 }
1416
1417 void SEQUENCER_OT_reassign_inputs(struct wmOperatorType *ot)
1418 {
1419         /* identifiers */
1420         ot->name = "Reassign Inputs";
1421         ot->idname = "SEQUENCER_OT_reassign_inputs";
1422         ot->description = "Reassign the inputs for the effect strip";
1423
1424         /* api callbacks */
1425         ot->exec = sequencer_reassign_inputs_exec;
1426         ot->poll = sequencer_effect_poll;
1427
1428         /* flags */
1429         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1430 }
1431
1432
1433 static int sequencer_swap_inputs_exec(bContext *C, wmOperator *op)
1434 {
1435         Scene *scene = CTX_data_scene(C);
1436         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene);
1437
1438         if (last_seq->seq1 == NULL || last_seq->seq2 == NULL) {
1439                 BKE_report(op->reports, RPT_ERROR, "No valid inputs to swap");
1440                 return OPERATOR_CANCELLED;
1441         }
1442
1443         seq = last_seq->seq1;
1444         last_seq->seq1 = last_seq->seq2;
1445         last_seq->seq2 = seq;
1446
1447         BKE_sequencer_update_changed_seq_and_deps(scene, last_seq, 1, 1);
1448
1449         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1450
1451         return OPERATOR_FINISHED;
1452 }
1453 void SEQUENCER_OT_swap_inputs(struct wmOperatorType *ot)
1454 {
1455         /* identifiers */
1456         ot->name = "Swap Inputs";
1457         ot->idname = "SEQUENCER_OT_swap_inputs";
1458         ot->description = "Swap the first two inputs for the effect strip";
1459
1460         /* api callbacks */
1461         ot->exec = sequencer_swap_inputs_exec;
1462         ot->poll = sequencer_effect_poll;
1463
1464         /* flags */
1465         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1466 }
1467
1468
1469 /* cut operator */
1470 static EnumPropertyItem prop_cut_types[] = {
1471         {SEQ_CUT_SOFT, "SOFT", 0, "Soft", ""},
1472         {SEQ_CUT_HARD, "HARD", 0, "Hard", ""},
1473         {0, NULL, 0, NULL, NULL}
1474 };
1475
1476 static int sequencer_cut_exec(bContext *C, wmOperator *op)
1477 {
1478         Scene *scene = CTX_data_scene(C);
1479         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1480         int cut_side, cut_hard, cut_frame;
1481
1482         ListBase newlist;
1483         int changed;
1484
1485         cut_frame = RNA_int_get(op->ptr, "frame");
1486         cut_hard = RNA_enum_get(op->ptr, "type");
1487         cut_side = RNA_enum_get(op->ptr, "side");
1488         
1489         newlist.first = newlist.last = NULL;
1490
1491         if (cut_hard == SEQ_CUT_HARD) {
1492                 changed = cut_seq_list(scene, ed->seqbasep, &newlist, cut_frame, cut_seq_hard);
1493         }
1494         else {
1495                 changed = cut_seq_list(scene, ed->seqbasep, &newlist, cut_frame, cut_seq_soft);
1496         }
1497         
1498         if (newlist.first) { /* got new strips ? */
1499                 Sequence *seq;
1500                 BLI_movelisttolist(ed->seqbasep, &newlist);
1501
1502                 if (cut_side != SEQ_SIDE_BOTH) {
1503                         SEQP_BEGIN (ed, seq)
1504                         {
1505                                 if (cut_side == SEQ_SIDE_LEFT) {
1506                                         if (seq->startdisp >= cut_frame) {
1507                                                 seq->flag &= ~SEQ_ALLSEL;
1508                                         }
1509                                 }
1510                                 else {
1511                                         if (seq->enddisp <= cut_frame) {
1512                                                 seq->flag &= ~SEQ_ALLSEL;
1513                                         }
1514                                 }
1515                         }
1516                         SEQ_END;
1517                 }
1518                 /* as last: */
1519                 BKE_sequencer_sort(scene);
1520         }
1521
1522         if (changed) {
1523                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1524                 return OPERATOR_FINISHED;
1525         }
1526         else {
1527                 return OPERATOR_CANCELLED;
1528         }
1529 }
1530
1531
1532 static int sequencer_cut_invoke(bContext *C, wmOperator *op, wmEvent *event)
1533 {
1534         Scene *scene = CTX_data_scene(C);
1535         View2D *v2d = UI_view2d_fromcontext(C);
1536
1537         int cut_side = SEQ_SIDE_BOTH;
1538         int cut_frame = CFRA;
1539
1540         if (ED_operator_sequencer_active(C) && v2d)
1541                 cut_side = mouse_frame_side(v2d, event->mval[0], cut_frame);
1542         
1543         RNA_int_set(op->ptr, "frame", cut_frame);
1544         RNA_enum_set(op->ptr, "side", cut_side);
1545         /*RNA_enum_set(op->ptr, "type", cut_hard); */ /*This type is set from the key shortcut */
1546
1547         return sequencer_cut_exec(C, op);
1548 }
1549
1550
1551 void SEQUENCER_OT_cut(struct wmOperatorType *ot)
1552 {
1553         /* identifiers */
1554         ot->name = "Cut Strips";
1555         ot->idname = "SEQUENCER_OT_cut";
1556         ot->description = "Cut the selected strips";
1557         
1558         /* api callbacks */
1559         ot->invoke = sequencer_cut_invoke;
1560         ot->exec = sequencer_cut_exec;
1561         ot->poll = sequencer_edit_poll;
1562         
1563         /* flags */
1564         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1565         
1566         RNA_def_int(ot->srna, "frame", 0, INT_MIN, INT_MAX, "Frame", "Frame where selected strips will be cut", INT_MIN, INT_MAX);
1567         RNA_def_enum(ot->srna, "type", prop_cut_types, SEQ_CUT_SOFT, "Type", "The type of cut operation to perform on strips");
1568         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side that remains selected after cutting");
1569 }
1570
1571 /* duplicate operator */
1572 static int apply_unique_name_cb(Sequence *seq, void *arg_pt)
1573 {
1574         Scene *scene = (Scene *)arg_pt;
1575         char name[sizeof(seq->name) - 2];
1576
1577         BLI_strncpy_utf8(name, seq->name + 2, sizeof(name));
1578         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seq);
1579         BKE_sequencer_dupe_animdata(scene, name, seq->name + 2);
1580         return 1;
1581
1582 }
1583
1584 static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op))
1585 {
1586         Scene *scene = CTX_data_scene(C);
1587         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1588
1589         ListBase nseqbase = {NULL, NULL};
1590
1591         if (ed == NULL)
1592                 return OPERATOR_CANCELLED;
1593
1594         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT);
1595
1596         if (nseqbase.first) {
1597                 Sequence *seq = nseqbase.first;
1598                 /* rely on the nseqbase list being added at the end */
1599                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
1600
1601                 for (; seq; seq = seq->next)
1602                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
1603
1604                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1605                 return OPERATOR_FINISHED;
1606         }
1607
1608         return OPERATOR_CANCELLED;
1609 }
1610
1611 void SEQUENCER_OT_duplicate(wmOperatorType *ot)
1612 {
1613         /* identifiers */
1614         ot->name = "Duplicate Strips";
1615         ot->idname = "SEQUENCER_OT_duplicate";
1616         ot->description = "Duplicate the selected strips";
1617         
1618         /* api callbacks */
1619         ot->exec = sequencer_add_duplicate_exec;
1620         ot->poll = ED_operator_sequencer_active;
1621         
1622         /* flags */
1623         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1624         
1625         /* to give to transform */
1626         RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1627 }
1628
1629 /* delete operator */
1630 static int sequencer_delete_exec(bContext *C, wmOperator *UNUSED(op))
1631 {
1632         Scene *scene = CTX_data_scene(C);
1633         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1634         Sequence *seq;
1635         MetaStack *ms;
1636         int nothingSelected = TRUE;
1637
1638         seq = BKE_sequencer_active_get(scene);
1639         if (seq && seq->flag & SELECT) { /* avoid a loop since this is likely to be selected */
1640                 nothingSelected = FALSE;
1641         }
1642         else {
1643                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1644                         if (seq->flag & SELECT) {
1645                                 nothingSelected = FALSE;
1646                                 break;
1647                         }
1648                 }
1649         }
1650
1651         if (nothingSelected)
1652                 return OPERATOR_FINISHED;
1653
1654         /* for effects, try to find a replacement input */
1655         for (seq = ed->seqbasep->first; seq; seq = seq->next)
1656                 if ((seq->type & SEQ_TYPE_EFFECT) && !(seq->flag & SELECT))
1657                         del_seq_find_replace_recurs(scene, seq);
1658
1659         /* delete all selected strips */
1660         recurs_del_seq_flag(scene, ed->seqbasep, SELECT, 0);
1661
1662         /* updates lengths etc */
1663         seq = ed->seqbasep->first;
1664         while (seq) {
1665                 BKE_sequence_calc(scene, seq);
1666                 seq = seq->next;
1667         }
1668
1669         /* free parent metas */
1670         ms = ed->metastack.last;
1671         while (ms) {
1672                 BKE_sequence_calc(scene, ms->parseq);
1673                 ms = ms->prev;
1674         }
1675
1676         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1677         
1678         return OPERATOR_FINISHED;
1679 }
1680
1681 static int sequencer_delete_invoke(bContext *C, wmOperator *op, wmEvent *event)
1682 {
1683         ARegion *ar = CTX_wm_region(C);
1684
1685         if (ar->regiontype == RGN_TYPE_WINDOW) {
1686                 /* bounding box of 30 pixels is used for markers shortcuts,
1687                  * prevent conflict with markers shortcurts here
1688                  */
1689                 if (event->mval[1] <= 30)
1690                         return OPERATOR_PASS_THROUGH;
1691         }
1692
1693         return WM_operator_confirm(C, op, event);
1694 }
1695
1696 void SEQUENCER_OT_delete(wmOperatorType *ot)
1697 {
1698
1699         /* identifiers */
1700         ot->name = "Erase Strips";
1701         ot->idname = "SEQUENCER_OT_delete";
1702         ot->description = "Erase selected strips from the sequencer";
1703         
1704         /* api callbacks */
1705         ot->invoke = sequencer_delete_invoke;
1706         ot->exec = sequencer_delete_exec;
1707         ot->poll = sequencer_edit_poll;
1708         
1709         /* flags */
1710         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1711 }
1712
1713
1714 /* offset clear operator */
1715 static int sequencer_offset_clear_exec(bContext *C, wmOperator *UNUSED(op))
1716 {
1717         Scene *scene = CTX_data_scene(C);
1718         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1719         Sequence *seq;
1720
1721         /* for effects, try to find a replacement input */
1722         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1723                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1724                         seq->startofs = seq->endofs = seq->startstill = seq->endstill = 0;
1725                 }
1726         }
1727
1728         /* updates lengths etc */
1729         seq = ed->seqbasep->first;
1730         while (seq) {
1731                 BKE_sequence_calc(scene, seq);
1732                 seq = seq->next;
1733         }
1734
1735         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
1736                 if ((seq->type & SEQ_TYPE_EFFECT) == 0 && (seq->flag & SELECT)) {
1737                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
1738                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
1739                         }
1740                 }
1741         }
1742
1743         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1744
1745         return OPERATOR_FINISHED;
1746 }
1747
1748
1749 void SEQUENCER_OT_offset_clear(wmOperatorType *ot)
1750 {
1751
1752         /* identifiers */
1753         ot->name = "Clear Strip Offset";
1754         ot->idname = "SEQUENCER_OT_offset_clear";
1755         ot->description = "Clear strip offsets from the start and end frames";
1756
1757         /* api callbacks */
1758         ot->exec = sequencer_offset_clear_exec;
1759         ot->poll = sequencer_edit_poll;
1760
1761         /* flags */
1762         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1763 }
1764
1765
1766 /* separate_images operator */
1767 static int sequencer_separate_images_exec(bContext *C, wmOperator *op)
1768 {
1769         Scene *scene = CTX_data_scene(C);
1770         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1771         
1772         Sequence *seq, *seq_new;
1773         Strip *strip_new;
1774         StripElem *se, *se_new;
1775         int start_ofs, cfra, frame_end;
1776         int step = RNA_int_get(op->ptr, "length");
1777
1778         seq = ed->seqbasep->first; /* poll checks this is valid */
1779
1780         while (seq) {
1781                 if ((seq->flag & SELECT) && (seq->type == SEQ_TYPE_IMAGE) && (seq->len > 1)) {
1782                         /* remove seq so overlap tests don't conflict,
1783                          * see seq_free_sequence below for the real free'ing */
1784                         BLI_remlink(ed->seqbasep, seq);
1785                         /* if (seq->ipo) seq->ipo->id.us--; */
1786                         /* XXX, remove fcurve and assign to split image strips */
1787
1788                         start_ofs = cfra = BKE_sequence_tx_get_final_left(seq, 0);
1789                         frame_end = BKE_sequence_tx_get_final_right(seq, 0);
1790
1791                         while (cfra < frame_end) {
1792                                 /* new seq */
1793                                 se = BKE_sequencer_give_stripelem(seq, cfra);
1794
1795                                 seq_new = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME);
1796                                 BLI_addtail(ed->seqbasep, seq_new);
1797
1798                                 seq_new->start = start_ofs;
1799                                 seq_new->type = SEQ_TYPE_IMAGE;
1800                                 seq_new->len = 1;
1801                                 seq_new->endstill = step - 1;
1802
1803                                 /* new strip */
1804                                 strip_new = seq_new->strip;
1805                                 strip_new->us = 1;
1806
1807                                 /* new stripdata */
1808                                 se_new = strip_new->stripdata;
1809                                 BLI_strncpy(se_new->name, se->name, sizeof(se_new->name));
1810                                 BKE_sequence_calc(scene, seq_new);
1811
1812                                 if (step > 1) {
1813                                         seq_new->flag &= ~SEQ_OVERLAP;
1814                                         if (BKE_sequence_test_overlap(ed->seqbasep, seq_new)) {
1815                                                 BKE_sequence_base_shuffle(ed->seqbasep, seq_new, scene);
1816                                         }
1817                                 }
1818
1819                                 /* XXX, COPY FCURVES */
1820
1821                                 cfra++;
1822                                 start_ofs += step;
1823                         }
1824
1825                         BKE_sequence_free(scene, seq);
1826                         seq = seq->next;
1827                 }
1828                 else {
1829                         seq = seq->next;
1830                 }
1831         }
1832
1833         /* as last: */
1834         BKE_sequencer_sort(scene);
1835         
1836         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1837
1838         return OPERATOR_FINISHED;
1839 }
1840
1841
1842 void SEQUENCER_OT_images_separate(wmOperatorType *ot)
1843 {
1844         /* identifiers */
1845         ot->name = "Separate Images";
1846         ot->idname = "SEQUENCER_OT_images_separate";
1847         ot->description = "On image sequence strips, it returns a strip for each image";
1848         
1849         /* api callbacks */
1850         ot->exec = sequencer_separate_images_exec;
1851         ot->poll = sequencer_edit_poll;
1852         
1853         /* flags */
1854         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1855
1856         RNA_def_int(ot->srna, "length", 1, 1, INT_MAX, "Length", "Length of each frame", 1, 1000);
1857 }
1858
1859
1860 /* META Operators */
1861
1862 /* separate_meta_toggle operator */
1863 static int sequencer_meta_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1864 {
1865         Scene *scene = CTX_data_scene(C);
1866         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1867         Sequence *last_seq = BKE_sequencer_active_get(scene);
1868         MetaStack *ms;
1869
1870         if (last_seq && last_seq->type == SEQ_TYPE_META && last_seq->flag & SELECT) {
1871                 /* Enter Metastrip */
1872                 ms = MEM_mallocN(sizeof(MetaStack), "metastack");
1873                 BLI_addtail(&ed->metastack, ms);
1874                 ms->parseq = last_seq;
1875                 ms->oldbasep = ed->seqbasep;
1876
1877                 ed->seqbasep = &last_seq->seqbase;
1878
1879                 BKE_sequencer_active_set(scene, NULL);
1880
1881         }
1882         else {
1883                 /* Exit Metastrip (if possible) */
1884
1885                 Sequence *seq;
1886
1887                 if (ed->metastack.first == NULL)
1888                         return OPERATOR_CANCELLED;
1889
1890                 ms = ed->metastack.last;
1891                 BLI_remlink(&ed->metastack, ms);
1892
1893                 ed->seqbasep = ms->oldbasep;
1894
1895                 /* recalc all: the meta can have effects connected to it */
1896                 for (seq = ed->seqbasep->first; seq; seq = seq->next)
1897                         BKE_sequence_calc(scene, seq);
1898
1899                 BKE_sequencer_active_set(scene, ms->parseq);
1900
1901                 ms->parseq->flag |= SELECT;
1902                 recurs_sel_seq(ms->parseq);
1903
1904                 MEM_freeN(ms);
1905
1906         }
1907
1908         BKE_sequencer_update_muting(ed);
1909         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1910
1911         return OPERATOR_FINISHED;
1912 }
1913
1914 void SEQUENCER_OT_meta_toggle(wmOperatorType *ot)
1915 {
1916         /* identifiers */
1917         ot->name = "Toggle Meta Strip";
1918         ot->idname = "SEQUENCER_OT_meta_toggle";
1919         ot->description = "Toggle a metastrip (to edit enclosed strips)";
1920         
1921         /* api callbacks */
1922         ot->exec = sequencer_meta_toggle_exec;
1923         ot->poll = sequencer_edit_poll;
1924         
1925         /* flags */
1926         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1927 }
1928
1929
1930 /* separate_meta_make operator */
1931 static int sequencer_meta_make_exec(bContext *C, wmOperator *op)
1932 {
1933         Scene *scene = CTX_data_scene(C);
1934         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
1935         
1936         Sequence *seq, *seqm, *next, *last_seq = BKE_sequencer_active_get(scene);
1937         int channel_max = 1;
1938
1939         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
1940                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
1941                 return OPERATOR_CANCELLED;
1942         }
1943
1944         /* remove all selected from main list, and put in meta */
1945
1946         seqm = BKE_sequence_alloc(ed->seqbasep, 1, 1); /* channel number set later */
1947         strcpy(seqm->name + 2, "MetaStrip");
1948         seqm->type = SEQ_TYPE_META;
1949         seqm->flag = SELECT;
1950
1951         seq = ed->seqbasep->first;
1952         while (seq) {
1953                 next = seq->next;
1954                 if (seq != seqm && (seq->flag & SELECT)) {
1955                         channel_max = max_ii(seq->machine, channel_max);
1956                         BLI_remlink(ed->seqbasep, seq);
1957                         BLI_addtail(&seqm->seqbase, seq);
1958                 }
1959                 seq = next;
1960         }
1961         seqm->machine = last_seq ? last_seq->machine : channel_max;
1962         BKE_sequence_calc(scene, seqm);
1963
1964         seqm->strip = MEM_callocN(sizeof(Strip), "metastrip");
1965         seqm->strip->us = 1;
1966         
1967         BKE_sequencer_active_set(scene, seqm);
1968
1969         if (BKE_sequence_test_overlap(ed->seqbasep, seqm) ) BKE_sequence_base_shuffle(ed->seqbasep, seqm, scene);
1970
1971         BKE_sequencer_update_muting(ed);
1972
1973         BKE_sequence_base_unique_name_recursive(&scene->ed->seqbase, seqm);
1974
1975         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
1976
1977         return OPERATOR_FINISHED;
1978 }
1979
1980 void SEQUENCER_OT_meta_make(wmOperatorType *ot)
1981 {
1982         /* identifiers */
1983         ot->name = "Make Meta Strip";
1984         ot->idname = "SEQUENCER_OT_meta_make";
1985         ot->description = "Group selected strips into a metastrip";
1986         
1987         /* api callbacks */
1988         ot->invoke = WM_operator_confirm;
1989         ot->exec = sequencer_meta_make_exec;
1990         ot->poll = sequencer_edit_poll;
1991         
1992         /* flags */
1993         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1994 }
1995
1996
1997 static int seq_depends_on_meta(Sequence *seq, Sequence *seqm)
1998 {
1999         if (seq == seqm) return 1;
2000         else if (seq->seq1 && seq_depends_on_meta(seq->seq1, seqm)) return 1;
2001         else if (seq->seq2 && seq_depends_on_meta(seq->seq2, seqm)) return 1;
2002         else if (seq->seq3 && seq_depends_on_meta(seq->seq3, seqm)) return 1;
2003         else return 0;
2004 }
2005
2006 /* separate_meta_make operator */
2007 static int sequencer_meta_separate_exec(bContext *C, wmOperator *UNUSED(op))
2008 {
2009         Scene *scene = CTX_data_scene(C);
2010         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2011
2012         Sequence *seq, *last_seq = BKE_sequencer_active_get(scene); /* last_seq checks (ed == NULL) */
2013
2014         if (last_seq == NULL || last_seq->type != SEQ_TYPE_META)
2015                 return OPERATOR_CANCELLED;
2016
2017         BLI_movelisttolist(ed->seqbasep, &last_seq->seqbase);
2018
2019         last_seq->seqbase.first = NULL;
2020         last_seq->seqbase.last = NULL;
2021
2022         BLI_remlink(ed->seqbasep, last_seq);
2023         BKE_sequence_free(scene, last_seq);
2024
2025         /* emtpy meta strip, delete all effects depending on it */
2026         for (seq = ed->seqbasep->first; seq; seq = seq->next)
2027                 if ((seq->type & SEQ_TYPE_EFFECT) && seq_depends_on_meta(seq, last_seq))
2028                         seq->flag |= SEQ_FLAG_DELETE;
2029
2030         recurs_del_seq_flag(scene, ed->seqbasep, SEQ_FLAG_DELETE, 0);
2031
2032         /* test for effects and overlap
2033          * don't use SEQP_BEGIN since that would be recursive */
2034         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2035                 if (seq->flag & SELECT) {
2036                         seq->flag &= ~SEQ_OVERLAP;
2037                         if (BKE_sequence_test_overlap(ed->seqbasep, seq)) {
2038                                 BKE_sequence_base_shuffle(ed->seqbasep, seq, scene);
2039                         }
2040                 }
2041         }
2042
2043         BKE_sequencer_sort(scene);
2044         BKE_sequencer_update_muting(ed);
2045
2046         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2047
2048         return OPERATOR_FINISHED;
2049 }
2050
2051 void SEQUENCER_OT_meta_separate(wmOperatorType *ot)
2052 {
2053         /* identifiers */
2054         ot->name = "UnMeta Strip";
2055         ot->idname = "SEQUENCER_OT_meta_separate";
2056         ot->description = "Put the contents of a metastrip back in the sequencer";
2057         
2058         /* api callbacks */
2059         ot->invoke = WM_operator_confirm;
2060         ot->exec = sequencer_meta_separate_exec;
2061         ot->poll = sequencer_edit_poll;
2062         
2063         /* flags */
2064         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2065 }
2066
2067 /* view_all operator */
2068 static int sequencer_view_all_exec(bContext *C, wmOperator *UNUSED(op))
2069 {
2070         ARegion *ar = CTX_wm_region(C);
2071         View2D *v2d = UI_view2d_fromcontext(C);
2072
2073         UI_view2d_smooth_view(C, ar, &v2d->tot);
2074         return OPERATOR_FINISHED;
2075 }
2076
2077 void SEQUENCER_OT_view_all(wmOperatorType *ot)
2078 {
2079         /* identifiers */
2080         ot->name = "View All";
2081         ot->idname = "SEQUENCER_OT_view_all";
2082         ot->description = "View all the strips in the sequencer";
2083         
2084         /* api callbacks */
2085         ot->exec = sequencer_view_all_exec;
2086         ot->poll = ED_operator_sequencer_active;
2087         
2088         /* flags */
2089         ot->flag = OPTYPE_REGISTER;
2090 }
2091
2092 /* view_all operator */
2093 static int sequencer_view_all_preview_exec(bContext *C, wmOperator *UNUSED(op))
2094 {
2095         bScreen *sc = CTX_wm_screen(C);
2096         ScrArea *area = CTX_wm_area(C);
2097 #if 0
2098         ARegion *ar = CTX_wm_region(C);
2099         SpaceSeq *sseq = area->spacedata.first;
2100         Scene *scene = CTX_data_scene(C);
2101 #endif
2102         View2D *v2d = UI_view2d_fromcontext(C);
2103
2104         v2d->cur = v2d->tot;
2105         UI_view2d_curRect_validate(v2d);
2106         UI_view2d_sync(sc, area, v2d, V2D_LOCK_COPY);
2107         
2108 #if 0
2109         /* Like zooming on an image view */
2110         float zoomX, zoomY;
2111         int width, height, imgwidth, imgheight;
2112
2113         width = ar->winx;
2114         height = ar->winy;
2115
2116         seq_reset_imageofs(sseq);
2117
2118         imgwidth = (scene->r.size * scene->r.xsch) / 100;
2119         imgheight = (scene->r.size * scene->r.ysch) / 100;
2120
2121         /* Apply aspect, dosnt need to be that accurate */
2122         imgwidth = (int)(imgwidth * (scene->r.xasp / scene->r.yasp));
2123
2124         if (((imgwidth >= width) || (imgheight >= height)) &&
2125             ((width > 0) && (height > 0)))
2126         {
2127                 /* Find the zoom value that will fit the image in the image space */
2128                 zoomX = ((float)width) / ((float)imgwidth);
2129                 zoomY = ((float)height) / ((float)imgheight);
2130                 sseq->zoom = (zoomX < zoomY) ? zoomX : zoomY;
2131
2132                 sseq->zoom = 1.0f / power_of_2(1 / min_ff(zoomX, zoomY));
2133         }
2134         else {
2135                 sseq->zoom = 1.0f;
2136         }
2137 #endif
2138
2139         ED_area_tag_redraw(CTX_wm_area(C));
2140         return OPERATOR_FINISHED;
2141 }
2142
2143 void SEQUENCER_OT_view_all_preview(wmOperatorType *ot)
2144 {
2145         /* identifiers */
2146         ot->name = "View All";
2147         ot->idname = "SEQUENCER_OT_view_all_preview";
2148         ot->description = "Zoom preview to fit in the area";
2149         
2150         /* api callbacks */
2151         ot->exec = sequencer_view_all_preview_exec;
2152         ot->poll = ED_operator_sequencer_active;
2153         
2154         /* flags */
2155         ot->flag = OPTYPE_REGISTER;
2156 }
2157
2158
2159 static int sequencer_view_zoom_ratio_exec(bContext *C, wmOperator *op)
2160 {
2161         RenderData *rd = &CTX_data_scene(C)->r;
2162         View2D *v2d = UI_view2d_fromcontext(C);
2163
2164         float ratio = RNA_float_get(op->ptr, "ratio");
2165
2166         float winx = (int)(rd->size * rd->xsch) / 100;
2167         float winy = (int)(rd->size * rd->ysch) / 100;
2168
2169         float facx = BLI_rcti_size_x(&v2d->mask) / winx;
2170         float facy = BLI_rcti_size_y(&v2d->mask) / winy;
2171
2172         BLI_rctf_resize(&v2d->cur, (int)(winx * facx * ratio) + 1, (int)(winy * facy * ratio) + 1);
2173
2174         ED_region_tag_redraw(CTX_wm_region(C));
2175
2176         return OPERATOR_FINISHED;
2177 }
2178
2179 void SEQUENCER_OT_view_zoom_ratio(wmOperatorType *ot)
2180 {
2181         /* identifiers */
2182         ot->name = "Sequencer View Zoom Ratio";
2183         ot->idname = "SEQUENCER_OT_view_zoom_ratio";
2184         ot->description = "Change zoom ratio of sequencer preview";
2185
2186         /* api callbacks */
2187         ot->exec = sequencer_view_zoom_ratio_exec;
2188         ot->poll = ED_operator_sequencer_active;
2189
2190         /* properties */
2191         RNA_def_float(ot->srna, "ratio", 1.0f, -FLT_MAX, FLT_MAX,
2192                       "Ratio", "Zoom ratio, 1.0 is 1:1, higher is zoomed in, lower is zoomed out", -FLT_MAX, FLT_MAX);
2193 }
2194
2195
2196 #if 0
2197 static EnumPropertyItem view_type_items[] = {
2198         {SEQ_VIEW_SEQUENCE, "SEQUENCER", ICON_SEQ_SEQUENCER, "Sequencer", ""},
2199         {SEQ_VIEW_PREVIEW,  "PREVIEW", ICON_SEQ_PREVIEW, "Image Preview", ""},
2200         {SEQ_VIEW_SEQUENCE_PREVIEW,  "SEQUENCER_PREVIEW", ICON_SEQ_SEQUENCER, "Sequencer and Image Preview", ""},
2201         {0, NULL, 0, NULL, NULL}
2202 };
2203 #endif
2204
2205 /* view_all operator */
2206 static int sequencer_view_toggle_exec(bContext *C, wmOperator *UNUSED(op))
2207 {
2208         SpaceSeq *sseq = (SpaceSeq *)CTX_wm_space_data(C);
2209
2210         sseq->view++;
2211         if (sseq->view > SEQ_VIEW_SEQUENCE_PREVIEW) sseq->view = SEQ_VIEW_SEQUENCE;
2212
2213         ED_area_tag_refresh(CTX_wm_area(C));
2214
2215         return OPERATOR_FINISHED;
2216 }
2217
2218 void SEQUENCER_OT_view_toggle(wmOperatorType *ot)
2219 {
2220         /* identifiers */
2221         ot->name = "View Toggle";
2222         ot->idname = "SEQUENCER_OT_view_toggle";
2223         ot->description = "Toggle between sequencer views (sequence, preview, both)";
2224         
2225         /* api callbacks */
2226         ot->exec = sequencer_view_toggle_exec;
2227         ot->poll = ED_operator_sequencer_active;
2228         
2229         /* flags */
2230         ot->flag = OPTYPE_REGISTER;
2231 }
2232
2233
2234 /* view_selected operator */
2235 static int sequencer_view_selected_exec(bContext *C, wmOperator *UNUSED(op))
2236 {
2237         Scene *scene = CTX_data_scene(C);
2238         View2D *v2d = UI_view2d_fromcontext(C);
2239         ARegion *ar = CTX_wm_region(C);
2240         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2241         Sequence *seq;
2242         rctf cur_new = v2d->cur;
2243
2244         int xmin =  MAXFRAME * 2;
2245         int xmax = -MAXFRAME * 2;
2246         int ymin =  MAXSEQ + 1;
2247         int ymax = 0;
2248         int orig_height;
2249         int ymid;
2250         int ymargin = 1;
2251         int xmargin = FPS;
2252
2253         if (ed == NULL)
2254                 return OPERATOR_CANCELLED;
2255
2256         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2257                 if (seq->flag & SELECT) {
2258                         xmin = min_ii(xmin, seq->startdisp);
2259                         xmax = max_ii(xmax, seq->enddisp);
2260
2261                         ymin = min_ii(ymin, seq->machine);
2262                         ymax = max_ii(ymax, seq->machine);
2263                 }
2264         }
2265
2266         if (ymax != 0) {
2267                 
2268                 xmax += xmargin;
2269                 xmin -= xmargin;
2270                 ymax += ymargin;
2271                 ymin -= ymargin;
2272
2273                 orig_height = BLI_rctf_size_y(&cur_new);
2274
2275                 cur_new.xmin = xmin;
2276                 cur_new.xmax = xmax;
2277
2278                 cur_new.ymin = ymin;
2279                 cur_new.ymax = ymax;
2280
2281                 /* only zoom out vertically */
2282                 if (orig_height > BLI_rctf_size_y(&cur_new)) {
2283                         ymid = BLI_rctf_cent_y(&cur_new);
2284
2285                         cur_new.ymin = ymid - (orig_height / 2);
2286                         cur_new.ymax = ymid + (orig_height / 2);
2287                 }
2288
2289                 UI_view2d_smooth_view(C, ar, &cur_new);
2290
2291                 return OPERATOR_FINISHED;
2292         }
2293         else {
2294                 return OPERATOR_CANCELLED;
2295         }
2296         
2297 }
2298
2299 void SEQUENCER_OT_view_selected(wmOperatorType *ot)
2300 {
2301         /* identifiers */
2302         ot->name = "View Selected";
2303         ot->idname = "SEQUENCER_OT_view_selected";
2304         ot->description = "Zoom the sequencer on the selected strips";
2305         
2306         /* api callbacks */
2307         ot->exec = sequencer_view_selected_exec;
2308         ot->poll = ED_operator_sequencer_active;
2309         
2310         /* flags */
2311         ot->flag = OPTYPE_REGISTER;
2312 }
2313
2314
2315 static int find_next_prev_edit(Scene *scene, int cfra,
2316                                const short side,
2317                                const short do_skip_mute, const short do_center)
2318 {
2319         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2320         Sequence *seq, *best_seq = NULL, *frame_seq = NULL;
2321         
2322         int dist, best_dist;
2323         best_dist = MAXFRAME * 2;
2324
2325         if (ed == NULL) return cfra;
2326         
2327         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2328                 int seq_frame;
2329
2330                 if (do_skip_mute && (seq->flag & SEQ_MUTE)) {
2331                         continue;
2332                 }
2333
2334                 if (do_center) {
2335                         seq_frame = (seq->startdisp + seq->enddisp) / 2;
2336                 }
2337                 else {
2338                         seq_frame = seq->startdisp;
2339                 }
2340
2341                 dist = MAXFRAME * 2;
2342                         
2343                 switch (side) {
2344                         case SEQ_SIDE_LEFT:
2345                                 if (seq_frame < cfra) {
2346                                         dist = cfra - seq_frame;
2347                                 }
2348                                 break;
2349                         case SEQ_SIDE_RIGHT:
2350                                 if (seq_frame > cfra) {
2351                                         dist = seq_frame - cfra;
2352                                 }
2353                                 else if (seq_frame == cfra) {
2354                                         frame_seq = seq;
2355                                 }
2356                                 break;
2357                 }
2358
2359                 if (dist < best_dist) {
2360                         best_dist = dist;
2361                         best_seq = seq;
2362                 }
2363         }
2364
2365         /* if no sequence to the right is found and the
2366          * frame is on the start of the last sequence,
2367          * move to the end of the last sequence */
2368         if (frame_seq) {
2369                 if (do_center) {
2370                         cfra = (frame_seq->startdisp + frame_seq->enddisp) / 2;
2371                 }
2372                 else {
2373                         cfra = frame_seq->enddisp;
2374                 }
2375         }
2376
2377         if (best_seq) {
2378                 if (do_center) {
2379                         cfra = (best_seq->startdisp + best_seq->enddisp) / 2;
2380                 }
2381                 else {
2382                         cfra = best_seq->startdisp;
2383                 }
2384         }
2385
2386         return cfra;
2387 }
2388
2389 static int strip_jump_internal(Scene *scene,
2390                                const short side,
2391                                const short do_skip_mute, const short do_center)
2392 {
2393         int change = FALSE;
2394         int cfra = CFRA;
2395         int nfra = find_next_prev_edit(scene, cfra, side, do_skip_mute, do_center);
2396         
2397         if (nfra != cfra) {
2398                 CFRA = nfra;
2399                 change = TRUE;
2400         }
2401
2402         return change;
2403 }
2404
2405 /* jump frame to edit point operator */
2406 static int sequencer_strip_jump_exec(bContext *C, wmOperator *op)
2407 {
2408         Scene *scene = CTX_data_scene(C);
2409         short next = RNA_boolean_get(op->ptr, "next");
2410         short center = RNA_boolean_get(op->ptr, "center");
2411
2412         /* currently do_skip_mute is always TRUE */
2413         if (!strip_jump_internal(scene, next ? SEQ_SIDE_RIGHT : SEQ_SIDE_LEFT, TRUE, center)) {
2414                 return OPERATOR_CANCELLED;
2415         }
2416
2417         WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene);
2418         
2419         return OPERATOR_FINISHED;
2420 }
2421
2422 void SEQUENCER_OT_strip_jump(wmOperatorType *ot)
2423 {
2424         /* identifiers */
2425         ot->name = "Jump to Strip";
2426         ot->idname = "SEQUENCER_OT_strip_jump";
2427         ot->description = "Move frame to previous edit point";
2428
2429         /* api callbacks */
2430         ot->exec = sequencer_strip_jump_exec;
2431         ot->poll = sequencer_edit_poll;
2432
2433         /* flags */
2434         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2435         
2436         /* properties */
2437         RNA_def_boolean(ot->srna, "next", TRUE, "Next Strip", "");
2438         RNA_def_boolean(ot->srna, "center", TRUE, "Use strip center", "");
2439 }
2440
2441 static void swap_sequence(Scene *scene, Sequence *seqa, Sequence *seqb)
2442 {
2443         int gap = seqb->startdisp - seqa->enddisp;
2444         seqb->start = (seqb->start - seqb->startdisp) + seqa->startdisp;
2445         BKE_sequence_calc(scene, seqb);
2446         seqa->start = (seqa->start - seqa->startdisp) + seqb->enddisp + gap;
2447         BKE_sequence_calc(scene, seqa);
2448 }
2449
2450 #if 0
2451 static Sequence *sequence_find_parent(Scene *scene, Sequence *child)
2452 {
2453         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2454         Sequence *parent = NULL;
2455         Sequence *seq;
2456
2457         if (ed == NULL) return NULL;
2458
2459         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
2460                 if ((seq != child) && seq_is_parent(seq, child)) {
2461                         parent = seq;
2462                         break;
2463                 }
2464         }
2465
2466         return parent;
2467 }
2468 #endif
2469
2470 static int sequencer_swap_exec(bContext *C, wmOperator *op)
2471 {
2472         Scene *scene = CTX_data_scene(C);
2473         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2474         Sequence *active_seq = BKE_sequencer_active_get(scene);
2475         Sequence *seq, *iseq;
2476         int side = RNA_enum_get(op->ptr, "side");
2477
2478         if (active_seq == NULL) return OPERATOR_CANCELLED;
2479
2480         seq = find_next_prev_sequence(scene, active_seq, side, -1);
2481         
2482         if (seq) {
2483                 
2484                 /* disallow effect strips */
2485                 if (BKE_sequence_effect_get_num_inputs(seq->type) >= 1 && (seq->effectdata || seq->seq1 || seq->seq2 || seq->seq3))
2486                         return OPERATOR_CANCELLED;
2487                 if ((BKE_sequence_effect_get_num_inputs(active_seq->type) >= 1) && (active_seq->effectdata || active_seq->seq1 || active_seq->seq2 || active_seq->seq3))
2488                         return OPERATOR_CANCELLED;
2489
2490                 switch (side) {
2491                         case SEQ_SIDE_LEFT: 
2492                                 swap_sequence(scene, seq, active_seq);
2493                                 break;
2494                         case SEQ_SIDE_RIGHT: 
2495                                 swap_sequence(scene, active_seq, seq);
2496                                 break;
2497                 }
2498
2499                 // XXX - should be a generic function
2500                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2501                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2502                                 BKE_sequence_calc(scene, iseq);
2503                         }
2504                 }
2505
2506                 /* do this in a new loop since both effects need to be calculated first */
2507                 for (iseq = scene->ed->seqbasep->first; iseq; iseq = iseq->next) {
2508                         if ((iseq->type & SEQ_TYPE_EFFECT) && (seq_is_parent(iseq, active_seq) || seq_is_parent(iseq, seq))) {
2509                                 /* this may now overlap */
2510                                 if (BKE_sequence_test_overlap(ed->seqbasep, iseq) ) {
2511                                         BKE_sequence_base_shuffle(ed->seqbasep, iseq, scene);
2512                                 }
2513                         }
2514                 }
2515
2516
2517
2518                 BKE_sequencer_sort(scene);
2519
2520                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2521
2522                 return OPERATOR_FINISHED;
2523         }
2524
2525         return OPERATOR_CANCELLED;
2526 }
2527
2528 void SEQUENCER_OT_swap(wmOperatorType *ot)
2529 {
2530         /* identifiers */
2531         ot->name = "Swap Strip";
2532         ot->idname = "SEQUENCER_OT_swap";
2533         ot->description = "Swap active strip with strip to the right or left";
2534         
2535         /* api callbacks */
2536         ot->exec = sequencer_swap_exec;
2537         ot->poll = sequencer_edit_poll;
2538         
2539         /* flags */
2540         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2541         
2542         /* properties */
2543         RNA_def_enum(ot->srna, "side", prop_side_lr_types, SEQ_SIDE_RIGHT, "Side", "Side of the strip to swap");
2544 }
2545
2546 static int sequencer_rendersize_exec(bContext *C, wmOperator *UNUSED(op))
2547 {
2548         int retval = OPERATOR_CANCELLED;
2549         Scene *scene = CTX_data_scene(C);
2550         Sequence *active_seq = BKE_sequencer_active_get(scene);
2551         StripElem *se = NULL;
2552
2553         if (active_seq == NULL)
2554                 return OPERATOR_CANCELLED;
2555
2556
2557         if (active_seq->strip) {
2558                 switch (active_seq->type) {
2559                         case SEQ_TYPE_IMAGE:
2560                                 se = BKE_sequencer_give_stripelem(active_seq, scene->r.cfra);
2561                                 break;
2562                         case SEQ_TYPE_MOVIE:
2563                                 se = active_seq->strip->stripdata;
2564                                 break;
2565                         case SEQ_TYPE_SCENE:
2566                         case SEQ_TYPE_META:
2567                         case SEQ_TYPE_SOUND_RAM:
2568                         case SEQ_TYPE_SOUND_HD:
2569                         default:
2570                                 break;
2571                 }
2572         }
2573
2574         if (se) {
2575                 // prevent setting the render size if sequence values aren't initialized
2576                 if ((se->orig_width > 0) && (se->orig_height > 0)) {
2577                         scene->r.xsch = se->orig_width;
2578                         scene->r.ysch = se->orig_height;
2579                         WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS, scene);
2580                         retval = OPERATOR_FINISHED;
2581                 }
2582         }
2583
2584         return retval;
2585 }
2586
2587 void SEQUENCER_OT_rendersize(wmOperatorType *ot)
2588 {
2589         /* identifiers */
2590         ot->name = "Set Render Size";
2591         ot->idname = "SEQUENCER_OT_rendersize";
2592         ot->description = "Set render size and aspect from active sequence";
2593         
2594         /* api callbacks */
2595         ot->exec = sequencer_rendersize_exec;
2596         ot->poll = sequencer_edit_poll;
2597         
2598         /* flags */
2599         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2600         
2601         /* properties */
2602 }
2603
2604 static void seq_copy_del_sound(Scene *scene, Sequence *seq)
2605 {
2606         if (seq->type == SEQ_TYPE_META) {
2607                 Sequence *iseq;
2608                 for (iseq = seq->seqbase.first; iseq; iseq = iseq->next) {
2609                         seq_copy_del_sound(scene, iseq);
2610                 }
2611         }
2612         else if (seq->scene_sound) {
2613                 sound_remove_scene_sound(scene, seq->scene_sound);
2614                 seq->scene_sound = NULL;
2615         }
2616 }
2617
2618 /* TODO, validate scenes */
2619 static int sequencer_copy_exec(bContext *C, wmOperator *op)
2620 {
2621         Scene *scene = CTX_data_scene(C);
2622         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2623
2624         ListBase nseqbase = {NULL, NULL};
2625
2626         BKE_sequencer_free_clipboard();
2627
2628         if (BKE_sequence_base_isolated_sel_check(ed->seqbasep) == FALSE) {
2629                 BKE_report(op->reports, RPT_ERROR, "Please select all related strips");
2630                 return OPERATOR_CANCELLED;
2631         }
2632
2633         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME);
2634
2635         /* To make sure the copied strips have unique names between each other add
2636          * them temporarily to the end of the original seqbase. (bug 25932)
2637          */
2638         if (nseqbase.first) {
2639                 Sequence *seq, *first_seq = nseqbase.first;
2640                 BLI_movelisttolist(ed->seqbasep, &nseqbase);
2641
2642                 for (seq = first_seq; seq; seq = seq->next)
2643                         BKE_sequencer_recursive_apply(seq, apply_unique_name_cb, scene);
2644
2645                 seqbase_clipboard.first = first_seq;
2646                 seqbase_clipboard.last = ed->seqbasep->last;
2647
2648                 if (first_seq->prev) {
2649                         first_seq->prev->next = NULL;
2650                         ed->seqbasep->last = first_seq->prev;
2651                         first_seq->prev = NULL;
2652                 }
2653         }
2654
2655         seqbase_clipboard_frame = scene->r.cfra;
2656
2657         /* Need to remove anything that references the current scene */
2658         {
2659                 Sequence *seq;
2660                 for (seq = seqbase_clipboard.first; seq; seq = seq->next) {
2661                         seq_copy_del_sound(scene, seq);
2662                 }
2663         }
2664
2665         return OPERATOR_FINISHED;
2666 }
2667
2668 void SEQUENCER_OT_copy(wmOperatorType *ot)
2669 {
2670         /* identifiers */
2671         ot->name = "Copy";
2672         ot->idname = "SEQUENCER_OT_copy";
2673         ot->description = "";
2674
2675         /* api callbacks */
2676         ot->exec = sequencer_copy_exec;
2677         ot->poll = sequencer_edit_poll;
2678
2679         /* flags */
2680         ot->flag = OPTYPE_REGISTER;
2681
2682         /* properties */
2683 }
2684
2685 static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op))
2686 {
2687         Scene *scene = CTX_data_scene(C);
2688         Editing *ed = BKE_sequencer_editing_get(scene, TRUE); /* create if needed */
2689         ListBase nseqbase = {NULL, NULL};
2690         int ofs;
2691         Sequence *iseq;
2692
2693         ED_sequencer_deselect_all(scene);
2694         ofs = scene->r.cfra - seqbase_clipboard_frame;
2695
2696         BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME);
2697
2698         /* transform pasted strips before adding */
2699         if (ofs) {
2700                 for (iseq = nseqbase.first; iseq; iseq = iseq->next) {
2701                         BKE_sequence_translate(scene, iseq, ofs);
2702                         BKE_sequence_sound_init(scene, iseq);
2703                 }
2704         }
2705
2706         iseq = nseqbase.first;
2707
2708         BLI_movelisttolist(ed->seqbasep, &nseqbase);
2709
2710         /* make sure the pasted strips have unique names between them */
2711         for (; iseq; iseq = iseq->next) {
2712                 BKE_sequencer_recursive_apply(iseq, apply_unique_name_cb, scene);
2713         }
2714
2715         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2716
2717         return OPERATOR_FINISHED;
2718 }
2719
2720 void SEQUENCER_OT_paste(wmOperatorType *ot)
2721 {
2722         /* identifiers */
2723         ot->name = "Paste";
2724         ot->idname = "SEQUENCER_OT_paste";
2725         ot->description = "";
2726
2727         /* api callbacks */
2728         ot->exec = sequencer_paste_exec;
2729         ot->poll = ED_operator_sequencer_active;
2730
2731         /* flags */
2732         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2733
2734         /* properties */
2735 }
2736
2737 static int sequencer_swap_data_exec(bContext *C, wmOperator *op)
2738 {
2739         Scene *scene = CTX_data_scene(C);
2740         Sequence *seq_act;
2741         Sequence *seq_other;
2742         const char *error_msg;
2743
2744         if (BKE_sequencer_active_get_pair(scene, &seq_act, &seq_other) == 0) {
2745                 BKE_report(op->reports, RPT_ERROR, "Please select two strips");
2746                 return OPERATOR_CANCELLED;
2747         }
2748
2749         if (BKE_sequence_swap(seq_act, seq_other, &error_msg) == 0) {
2750                 BKE_report(op->reports, RPT_ERROR, error_msg);
2751                 return OPERATOR_CANCELLED;
2752         }
2753
2754         if (seq_act->scene_sound)
2755                 sound_remove_scene_sound(scene, seq_act->scene_sound);
2756
2757         if (seq_other->scene_sound)
2758                 sound_remove_scene_sound(scene, seq_other->scene_sound);
2759
2760         seq_act->scene_sound = NULL;
2761         seq_other->scene_sound = NULL;
2762
2763         BKE_sequence_calc(scene, seq_act);
2764         BKE_sequence_calc(scene, seq_other);
2765
2766         if (seq_act->sound) sound_add_scene_sound_defaults(scene, seq_act);
2767         if (seq_other->sound) sound_add_scene_sound_defaults(scene, seq_other);
2768
2769         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2770
2771         return OPERATOR_FINISHED;
2772 }
2773
2774 void SEQUENCER_OT_swap_data(wmOperatorType *ot)
2775 {
2776         /* identifiers */
2777         ot->name = "Sequencer Swap Data";
2778         ot->idname = "SEQUENCER_OT_swap_data";
2779         ot->description = "Swap 2 sequencer strips";
2780
2781         /* api callbacks */
2782         ot->exec = sequencer_swap_data_exec;
2783         ot->poll = ED_operator_sequencer_active;
2784
2785         /* flags */
2786         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2787
2788         /* properties */
2789 }
2790
2791 /* borderselect operator */
2792 static int view_ghost_border_exec(bContext *C, wmOperator *op)
2793 {
2794         Scene *scene = CTX_data_scene(C);
2795         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2796         View2D *v2d = UI_view2d_fromcontext(C);
2797
2798         rctf rect;
2799
2800         /* convert coordinates of rect to 'tot' rect coordinates */
2801         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
2802         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
2803
2804         if (ed == NULL)
2805                 return OPERATOR_CANCELLED;
2806
2807         rect.xmin /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2808         rect.ymin /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2809
2810         rect.xmax /=  fabsf(BLI_rctf_size_x(&v2d->tot));
2811         rect.ymax /=  fabsf(BLI_rctf_size_y(&v2d->tot));
2812
2813         rect.xmin += 0.5f;
2814         rect.xmax += 0.5f;
2815         rect.ymin += 0.5f;
2816         rect.ymax += 0.5f;
2817
2818         CLAMP(rect.xmin, 0.0f, 1.0f);
2819         CLAMP(rect.ymin, 0.0f, 1.0f);
2820         CLAMP(rect.xmax, 0.0f, 1.0f);
2821         CLAMP(rect.ymax, 0.0f, 1.0f);
2822
2823         scene->ed->over_border = rect;
2824
2825         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2826
2827         return OPERATOR_FINISHED;
2828 }
2829
2830 /* ****** Border Select ****** */
2831 void SEQUENCER_OT_view_ghost_border(wmOperatorType *ot)
2832 {
2833         /* identifiers */
2834         ot->name = "Border Offset View";
2835         ot->idname = "SEQUENCER_OT_view_ghost_border";
2836         ot->description = "Enable border select mode";
2837
2838         /* api callbacks */
2839         ot->invoke = WM_border_select_invoke;
2840         ot->exec = view_ghost_border_exec;
2841         ot->modal = WM_border_select_modal;
2842         ot->poll = sequencer_view_poll;
2843         ot->cancel = WM_border_select_cancel;
2844
2845         /* flags */
2846         ot->flag = 0;
2847
2848         /* rna */
2849         WM_operator_properties_gesture_border(ot, FALSE);
2850 }
2851
2852 /* rebuild_proxy operator */
2853 static int sequencer_rebuild_proxy_exec(bContext *C, wmOperator *UNUSED(op))
2854 {
2855         seq_proxy_build_job(C);
2856
2857         return OPERATOR_FINISHED;
2858 }
2859
2860 void SEQUENCER_OT_rebuild_proxy(wmOperatorType *ot)
2861 {
2862         /* identifiers */
2863         ot->name = "Rebuild Proxy and Timecode Indices";
2864         ot->idname = "SEQUENCER_OT_rebuild_proxy";
2865         ot->description = "Rebuild all selected proxies and timecode indices using the job system";
2866         
2867         /* api callbacks */
2868         ot->exec = sequencer_rebuild_proxy_exec;
2869         ot->poll = ED_operator_sequencer_active;
2870         
2871         /* flags */
2872         ot->flag = OPTYPE_REGISTER;
2873 }
2874
2875 /* change ops */
2876
2877 static EnumPropertyItem prop_change_effect_input_types[] = {
2878         {0, "A_B", 0, "A -> B", ""},
2879         {1, "B_C", 0, "B -> C", ""},
2880         {2, "A_C", 0, "A -> C", ""},
2881         {0, NULL, 0, NULL, NULL}
2882 };
2883
2884 static int sequencer_change_effect_input_exec(bContext *C, wmOperator *op)
2885 {
2886         Scene *scene = CTX_data_scene(C);
2887         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2888         Sequence *seq = BKE_sequencer_active_get(scene);
2889
2890         Sequence **seq_1, **seq_2;
2891
2892         switch (RNA_enum_get(op->ptr, "swap")) {
2893                 case 0:
2894                         seq_1 = &seq->seq1;
2895                         seq_2 = &seq->seq2;
2896                         break;
2897                 case 1:
2898                         seq_1 = &seq->seq2;
2899                         seq_2 = &seq->seq3;
2900                         break;
2901                 default: /* 2 */
2902                         seq_1 = &seq->seq1;
2903                         seq_2 = &seq->seq3;
2904                         break;
2905         }
2906
2907         if (*seq_1 == NULL || *seq_2 == NULL) {
2908                 BKE_report(op->reports, RPT_ERROR, "One of the effect inputs is unset, can't swap");
2909                 return OPERATOR_CANCELLED;
2910         }
2911         else {
2912                 SWAP(Sequence *, *seq_1, *seq_2);
2913         }
2914
2915         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
2916
2917         /* important else we don't get the imbuf cache flushed */
2918         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
2919
2920         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2921
2922         return OPERATOR_FINISHED;
2923 }
2924
2925 void SEQUENCER_OT_change_effect_input(struct wmOperatorType *ot)
2926 {
2927         /* identifiers */
2928         ot->name = "Change Effect Input";
2929         ot->idname = "SEQUENCER_OT_change_effect_input";
2930         ot->description = "";
2931
2932         /* api callbacks */
2933         ot->exec = sequencer_change_effect_input_exec;
2934         ot->poll = sequencer_effect_poll;
2935
2936         /* flags */
2937         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2938
2939         ot->prop = RNA_def_enum(ot->srna, "swap", prop_change_effect_input_types, 0, "Swap", "The effect inputs to swap");
2940 }
2941
2942 static int sequencer_change_effect_type_exec(bContext *C, wmOperator *op)
2943 {
2944         Scene *scene = CTX_data_scene(C);
2945         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
2946         Sequence *seq = BKE_sequencer_active_get(scene);
2947         const int new_type = RNA_enum_get(op->ptr, "type");
2948
2949         /* free previous effect and init new effect */
2950         struct SeqEffectHandle sh;
2951
2952         if ((seq->type & SEQ_TYPE_EFFECT) == 0) {
2953                 return OPERATOR_CANCELLED;
2954         }
2955
2956         /* can someone explain the logic behind only allowing to increase this,
2957          * copied from 2.4x - campbell */
2958         if (BKE_sequence_effect_get_num_inputs(seq->type) <
2959             BKE_sequence_effect_get_num_inputs(new_type))
2960         {
2961                 BKE_report(op->reports, RPT_ERROR, "New effect needs more input strips");
2962                 return OPERATOR_CANCELLED;
2963         }
2964         else {
2965                 sh = BKE_sequence_get_effect(seq);
2966                 sh.free(seq);
2967
2968                 seq->type = new_type;
2969
2970                 sh = BKE_sequence_get_effect(seq);
2971                 sh.init(seq);
2972         }
2973
2974         /* update */
2975         BKE_sequencer_update_changed_seq_and_deps(scene, seq, 0, 1);
2976
2977         /* important else we don't get the imbuf cache flushed */
2978         BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
2979
2980         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
2981
2982         return OPERATOR_FINISHED;
2983 }
2984
2985 void SEQUENCER_OT_change_effect_type(struct wmOperatorType *ot)
2986 {
2987         /* identifiers */
2988         ot->name = "Change Effect Type";
2989         ot->idname = "SEQUENCER_OT_change_effect_type";
2990         ot->description = "";
2991
2992         /* api callbacks */
2993         ot->exec = sequencer_change_effect_type_exec;
2994         ot->poll = sequencer_effect_poll;
2995
2996         /* flags */
2997         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2998
2999         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_TYPE_CROSS, "Type", "Sequencer effect type");
3000 }
3001
3002 static int sequencer_change_path_exec(bContext *C, wmOperator *op)
3003 {
3004         Main *bmain = CTX_data_main(C);
3005         Scene *scene = CTX_data_scene(C);
3006         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
3007         Sequence *seq = BKE_sequencer_active_get(scene);
3008         const int is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
3009
3010         if (seq->type == SEQ_TYPE_IMAGE) {
3011                 char directory[FILE_MAX];
3012                 const int len = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
3013                 StripElem *se;
3014
3015                 if (len == 0)
3016                         return OPERATOR_CANCELLED;
3017
3018                 RNA_string_get(op->ptr, "directory", directory);
3019                 if (is_relative_path) {
3020                         /* TODO, shouldn't this already be relative from the filesel?
3021                          * (as the 'filepath' is) for now just make relative here,
3022                          * but look into changing after 2.60 - campbell */
3023                         BLI_path_rel(directory, bmain->name);
3024                 }
3025                 BLI_strncpy(seq->strip->dir, directory, sizeof(seq->strip->dir));
3026
3027                 if (seq->strip->stripdata) {
3028                         MEM_freeN(seq->strip->stripdata);
3029                 }
3030                 seq->strip->stripdata = se = MEM_callocN(len * sizeof(StripElem), "stripelem");
3031
3032                 RNA_BEGIN (op->ptr, itemptr, "files")
3033                 {
3034                         char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
3035                         BLI_strncpy(se->name, filename, sizeof(se->name));
3036                         MEM_freeN(filename);
3037                         se++;
3038                 }
3039                 RNA_END;
3040
3041                 /* reset these else we wont see all the images */
3042                 seq->anim_startofs = seq->anim_endofs = 0;
3043
3044                 /* correct start/end frames so we don't move
3045                  * important not to set seq->len = len; allow the function to handle it */
3046                 BKE_sequence_reload_new_file(scene, seq, TRUE);
3047
3048                 BKE_sequence_calc(scene, seq);
3049
3050                 /* important else we don't get the imbuf cache flushed */
3051                 BKE_sequencer_free_imbuf(scene, &ed->seqbase, FALSE);
3052         }
3053         else {
3054                 /* lame, set rna filepath */
3055                 PointerRNA seq_ptr;
3056                 PropertyRNA *prop;
3057                 char filepath[FILE_MAX];
3058
3059                 RNA_pointer_create(&scene->id, &RNA_Sequence, seq, &seq_ptr);
3060
3061                 RNA_string_get(op->ptr, "filepath", filepath);
3062                 prop = RNA_struct_find_property(&seq_ptr, "filepath");
3063                 RNA_property_string_set(&seq_ptr, prop, filepath);
3064                 RNA_property_update(C, &seq_ptr, prop);
3065         }
3066
3067         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
3068
3069         return OPERATOR_FINISHED;
3070 }
3071
3072 static int sequencer_change_path_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
3073 {
3074         Scene *scene = CTX_data_scene(C);
3075         Sequence *seq = BKE_sequencer_active_get(scene);
3076
3077         RNA_string_set(op->ptr, "directory", seq->strip->dir);
3078
3079         /* set default display depending on seq type */
3080         if (seq->type == SEQ_TYPE_IMAGE) {
3081                 RNA_boolean_set(op->ptr, "filter_movie", FALSE);
3082         }
3083         else {
3084                 RNA_boolean_set(op->ptr, "filter_image", FALSE);
3085         }
3086
3087         WM_event_add_fileselect(C, op);
3088
3089         return OPERATOR_RUNNING_MODAL;
3090 }
3091
3092 void SEQUENCER_OT_change_path(struct wmOperatorType *ot)
3093 {
3094         /* identifiers */
3095         ot->name = "Change Data/Files";
3096         ot->idname = "SEQUENCER_OT_change_path";
3097         ot->description = "";
3098
3099         /* api callbacks */
3100         ot->exec = sequencer_change_path_exec;
3101         ot->invoke = sequencer_change_path_invoke;
3102         ot->poll = sequencer_strip_has_path_poll;
3103
3104         /* flags */
3105         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3106
3107         WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE,
3108                                        WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH | WM_FILESEL_FILEPATH | WM_FILESEL_FILES,
3109                                        FILE_DEFAULTDISPLAY);
3110 }
3111