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