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