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