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