sequencer add strips now check for overlap by default (option can be disabled for...
[blender-staging.git] / source / blender / editors / space_sequencer / sequencer_add.c
1 /*
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation, 2003-2009, Campbell Barton
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_sequencer/sequencer_add.c
28  *  \ingroup spseq
29  */
30
31
32 #include <stdlib.h>
33 #include <math.h>
34 #include <string.h>
35
36 #ifndef WIN32
37 #include <unistd.h>
38 #else
39 #include <io.h>
40 #endif
41 #include <sys/types.h>
42
43 #include "MEM_guardedalloc.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_storage_types.h"
48 #include "BLI_utildefines.h"
49
50 #include "DNA_scene_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BKE_context.h"
54 #include "BKE_global.h"
55 #include "BKE_main.h"
56 #include "BKE_sequencer.h"
57 #include "BKE_report.h"
58
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "RNA_define.h"
64 #include "RNA_enum_types.h"
65
66 /* for menu/popup icons etc etc*/
67
68 #include "ED_screen.h"
69
70 #include "UI_view2d.h"
71
72 #include "BKE_sound.h"
73
74 #ifdef WITH_AUDASPACE
75 #  include "AUD_C-API.h"
76 #endif
77
78 /* own include */
79 #include "sequencer_intern.h"
80
81 /* Generic functions, reused by add strip operators */
82
83 /* avoid passing multiple args and be more verbose */
84 #define SEQPROP_STARTFRAME      (1<<0)
85 #define SEQPROP_ENDFRAME        (1<<1)
86 #define SEQPROP_FILES           (1<<2)
87 #define SEQPROP_NOPATHS         (1<<3)
88 #define SEQPROP_NOCHAN          (1<<4)
89
90 #define SELECT 1
91
92 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
93 {
94         if(flag & SEQPROP_STARTFRAME)
95                 RNA_def_int(ot->srna, "frame_start", 0, INT_MIN, INT_MAX, "Start Frame", "Start frame of the sequence strip", INT_MIN, INT_MAX);
96         
97         if(flag & SEQPROP_ENDFRAME)
98                 RNA_def_int(ot->srna, "frame_end", 0, INT_MIN, INT_MAX, "End Frame", "End frame for the color strip", INT_MIN, INT_MAX); /* not useual since most strips have a fixed length */
99         
100         RNA_def_int(ot->srna, "channel", 1, 1, MAXSEQ, "Channel", "Channel to place this strip into", 1, MAXSEQ);
101         
102         RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "replace the current selection");
103
104         RNA_def_boolean(ot->srna, "overlap", 0, "Allow Overlap", "Don't correct overlap on new sequence strips");
105
106         if(flag & SEQPROP_FILES)
107                 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
108 }
109
110 static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op, const char *identifier)
111 {
112         if(RNA_struct_find_property(op->ptr, identifier)) {
113                 Scene *scene= CTX_data_scene(C);
114                 Sequence *last_seq= seq_active_get(scene);
115                 if(last_seq && last_seq->strip && SEQ_HAS_PATH(last_seq)) {
116                         char path[sizeof(last_seq->strip->dir)];
117                         BLI_strncpy(path, last_seq->strip->dir, sizeof(path));
118                         BLI_path_abs(path, G.main->name);
119                         RNA_string_set(op->ptr, identifier, path);
120                 }
121         }
122 }
123
124 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
125 {
126         View2D *v2d= UI_view2d_fromcontext(C);
127         
128         float mval_v2d[2];
129         
130         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &mval_v2d[0], &mval_v2d[1]);
131
132         /* effect strips dont need a channel initialized from the mouse */
133         if(!(flag & SEQPROP_NOCHAN)) {
134                 RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
135         }
136
137         RNA_int_set(op->ptr, "frame_start", (int)mval_v2d[0]);
138         
139         if ((flag & SEQPROP_ENDFRAME) && RNA_property_is_set(op->ptr, "frame_end")==0)
140                 RNA_int_set(op->ptr, "frame_end", (int)mval_v2d[0] + 25); // XXX arbitary but ok for now.
141
142         if (!(flag & SEQPROP_NOPATHS)) {
143                 sequencer_generic_invoke_path__internal(C, op, "filepath");
144                 sequencer_generic_invoke_path__internal(C, op, "directory");
145         }
146 }
147
148 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
149 {
150         int relative= RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path");
151         int is_file= -1;
152         memset(seq_load, 0, sizeof(SeqLoadInfo));
153
154         seq_load->start_frame=  RNA_int_get(op->ptr, "frame_start");
155         seq_load->end_frame=    seq_load->start_frame; /* un-set */
156
157         seq_load->channel=              RNA_int_get(op->ptr, "channel");
158         seq_load->len=                  1; // images only, if endframe isnt set!
159
160         if(RNA_struct_find_property(op->ptr, "filepath")) {
161                 RNA_string_get(op->ptr, "filepath", seq_load->path); /* full path, file is set by the caller */
162                 is_file= 1;
163         } else if (RNA_struct_find_property(op->ptr, "directory")) {
164                 RNA_string_get(op->ptr, "directory", seq_load->path); /* full path, file is set by the caller */
165                 is_file= 0;
166         }
167
168         if((is_file != -1) && relative)
169                 BLI_path_rel(seq_load->path, G.main->name);
170
171         
172         if (RNA_struct_find_property(op->ptr, "frame_end")) {
173                 seq_load->end_frame = RNA_int_get(op->ptr, "frame_end");
174         }
175
176         if (RNA_struct_find_property(op->ptr, "replace_sel") && RNA_boolean_get(op->ptr, "replace_sel"))
177                 seq_load->flag |= SEQ_LOAD_REPLACE_SEL;
178
179         if (RNA_struct_find_property(op->ptr, "cache") && RNA_boolean_get(op->ptr, "cache"))
180                 seq_load->flag |= SEQ_LOAD_SOUND_CACHE;
181
182         if (RNA_struct_find_property(op->ptr, "sound") && RNA_boolean_get(op->ptr, "sound"))
183                 seq_load->flag |= SEQ_LOAD_MOVIE_SOUND;
184
185         /* always use this for ops */
186         seq_load->flag |= SEQ_LOAD_FRAME_ADVANCE;
187
188
189         if(is_file==1) {
190                 BLI_strncpy(seq_load->name, BLI_path_basename(seq_load->path), sizeof(seq_load->name));
191         }
192         else if(RNA_struct_find_property(op->ptr, "files")) {
193                 /* used for image strip */
194                 /* best guess, first images name */
195                 RNA_BEGIN(op->ptr, itemptr, "files") {
196                         char *name= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
197                         BLI_strncpy(seq_load->name, name, sizeof(seq_load->name));
198                         MEM_freeN(name);
199                         break;
200                 }
201                 RNA_END;
202         }
203 }
204
205 /* add scene operator */
206 static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
207 {
208         Scene *scene= CTX_data_scene(C);
209         Editing *ed= seq_give_editing(scene, TRUE);
210         
211         Scene *sce_seq;
212
213         Sequence *seq;  /* generic strip vars */
214         Strip *strip;
215         
216         int start_frame, channel; /* operator props */
217         
218         start_frame= RNA_int_get(op->ptr, "frame_start");
219         channel= RNA_int_get(op->ptr, "channel");
220         
221         sce_seq= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
222         
223         if (sce_seq==NULL) {
224                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
225                 return OPERATOR_CANCELLED;
226         }
227         
228         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
229         seq->type= SEQ_SCENE;
230         seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
231
232         seq->scene= sce_seq;
233         seq->sfra= sce_seq->r.sfra;
234         
235         /* basic defaults */
236         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
237         strip->len = seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
238         strip->us= 1;
239         
240         strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
241         
242         strcpy(seq->name+2, sce_seq->id.name+2);
243         seqbase_unique_name_recursive(&ed->seqbase, seq);
244
245         seq->scene_sound = sound_scene_add_scene_sound(scene, seq, start_frame, start_frame + strip->len, 0);
246
247         calc_sequence_disp(scene, seq);
248         sort_seq(scene);
249         
250         if (RNA_boolean_get(op->ptr, "replace_sel")) {
251                 deselect_all_seq(scene);
252                 seq_active_set(scene, seq);
253                 seq->flag |= SELECT;
254         }
255
256         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
257                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
258         }
259
260         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
261         
262         return OPERATOR_FINISHED;
263 }
264
265
266 static int sequencer_add_scene_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
267 {
268         if(!ED_operator_sequencer_active(C)) {
269                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
270                 return OPERATOR_CANCELLED;
271         }
272
273         if(!RNA_property_is_set(op->ptr, "scene"))
274                 return WM_enum_search_invoke(C, op, event);
275
276         sequencer_generic_invoke_xy__internal(C, op, event, 0);
277         return sequencer_add_scene_strip_exec(C, op);
278         // needs a menu
279         // return WM_menu_invoke(C, op, event);
280 }
281
282
283 void SEQUENCER_OT_scene_strip_add(struct wmOperatorType *ot)
284 {
285         PropertyRNA *prop;
286         
287         /* identifiers */
288         ot->name= "Add Scene Strip";
289         ot->idname= "SEQUENCER_OT_scene_strip_add";
290         ot->description= "Add a strip to the sequencer using a blender scene as a source";
291
292         /* api callbacks */
293         ot->invoke= sequencer_add_scene_strip_invoke;
294         ot->exec= sequencer_add_scene_strip_exec;
295
296         ot->poll= ED_operator_scene_editable;
297         
298         /* flags */
299         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
300         
301         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
302         prop= RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
303         RNA_def_enum_funcs(prop, RNA_scene_itemf);
304         ot->prop= prop;
305 }
306
307 static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoadFunc seq_load_func)
308 {
309         Scene *scene= CTX_data_scene(C); /* only for sound */
310         Editing *ed= seq_give_editing(scene, TRUE);
311         SeqLoadInfo seq_load;
312         Sequence *seq;
313         int tot_files;
314         const short overlap= RNA_boolean_get(op->ptr, "overlap");
315
316         seq_load_operator_info(&seq_load, op);
317
318         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
319                 deselect_all_seq(scene);
320
321         tot_files= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
322
323         if(tot_files) {
324                 /* multiple files */
325                 char dir_only[FILE_MAX];
326                 char file_only[FILE_MAX];
327
328                 BLI_split_dirfile(seq_load.path, dir_only, NULL);
329
330                 RNA_BEGIN(op->ptr, itemptr, "files") {
331                         RNA_string_get(&itemptr, "name", file_only);
332                         BLI_join_dirfile(seq_load.path, sizeof(seq_load.path), dir_only, file_only);
333
334                         seq= seq_load_func(C, ed->seqbasep, &seq_load);
335
336                         if(overlap == FALSE) {
337                                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
338                         }
339                 }
340                 RNA_END;
341         }
342         else {
343                 /* single file */
344                 seq= seq_load_func(C, ed->seqbasep, &seq_load);
345
346                 if(overlap == FALSE) {
347                         if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
348                 }
349         }
350
351         if (seq_load.tot_success==0) {
352                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
353                 return OPERATOR_CANCELLED;
354         }
355
356         sort_seq(scene);
357         seq_update_muting(scene, ed);
358
359         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
360
361         return OPERATOR_FINISHED;
362 }
363
364 /* add movie operator */
365 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
366 {
367         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
368 }
369
370
371 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
372 {
373
374         if(!ED_operator_sequencer_active(C)) {
375                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
376                 return OPERATOR_CANCELLED;
377         }
378
379         /* This is for drag and drop */
380         if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath")) {
381                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
382                 return sequencer_add_movie_strip_exec(C, op);
383         }
384         
385         sequencer_generic_invoke_xy__internal(C, op, event, 0);
386         
387         if(!RNA_property_is_set(op->ptr, "relative_path"))
388                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
389         
390         WM_event_add_fileselect(C, op);
391         return OPERATOR_RUNNING_MODAL;
392
393         //return sequencer_add_movie_strip_exec(C, op);
394 }
395
396
397 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
398 {
399         
400         /* identifiers */
401         ot->name= "Add Movie Strip";
402         ot->idname= "SEQUENCER_OT_movie_strip_add";
403         ot->description= "Add a movie strip to the sequencer";
404
405         /* api callbacks */
406         ot->invoke= sequencer_add_movie_strip_invoke;
407         ot->exec= sequencer_add_movie_strip_exec;
408
409         ot->poll= ED_operator_scene_editable;
410         
411         /* flags */
412         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
413         
414         WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
415         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
416         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
417 }
418
419 /* add sound operator */
420
421 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
422 {
423         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
424 }
425
426 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
427 {
428
429         if(!ED_operator_sequencer_active(C)) {
430                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
431                 return OPERATOR_CANCELLED;
432         }
433         
434         /* This is for drag and drop */
435         if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath")) {
436                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
437                 return sequencer_add_sound_strip_exec(C, op);
438         }
439         
440         sequencer_generic_invoke_xy__internal(C, op, event, 0);
441         
442         if(!RNA_property_is_set(op->ptr, "relative_path"))
443                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
444         
445         WM_event_add_fileselect(C, op);
446         return OPERATOR_RUNNING_MODAL;
447
448         //return sequencer_add_sound_strip_exec(C, op);
449 }
450
451
452 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
453 {
454         
455         /* identifiers */
456         ot->name= "Add Sound Strip";
457         ot->idname= "SEQUENCER_OT_sound_strip_add";
458         ot->description= "Add a sound strip to the sequencer";
459
460         /* api callbacks */
461         ot->invoke= sequencer_add_sound_strip_invoke;
462         ot->exec= sequencer_add_sound_strip_exec;
463
464         ot->poll= ED_operator_scene_editable;
465         
466         /* flags */
467         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
468         
469         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
470         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
471         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
472 }
473
474 /* add image operator */
475 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
476 {
477         /* cant use the generic function for this */
478
479         Scene *scene= CTX_data_scene(C); /* only for sound */
480         Editing *ed= seq_give_editing(scene, TRUE);
481         SeqLoadInfo seq_load;
482         Sequence *seq;
483
484         Strip *strip;
485         StripElem *se;
486
487         seq_load_operator_info(&seq_load, op);
488
489         /* images are unique in how they handle this - 1 per strip elem */
490         seq_load.len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
491
492         if(seq_load.len==0)
493                 return OPERATOR_CANCELLED;
494
495         if(seq_load.flag & SEQ_LOAD_REPLACE_SEL)
496                 deselect_all_seq(scene);
497
498
499         /* main adding function */
500         seq= sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
501         strip= seq->strip;
502         se= strip->stripdata;
503
504         RNA_BEGIN(op->ptr, itemptr, "files") {
505                 char *filename= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
506                 BLI_strncpy(se->name, filename, sizeof(se->name));
507                 MEM_freeN(filename);
508                 se++;
509         }
510         RNA_END;
511
512         if(seq_load.len == 1) {
513                 if(seq_load.start_frame < seq_load.end_frame) {
514                         seq->endstill= seq_load.end_frame - seq_load.start_frame;
515                 }
516         }
517         
518         calc_sequence_disp(scene, seq);
519
520         sort_seq(scene);
521
522         /* last active name */
523         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
524
525         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
526                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
527         }
528
529         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
530
531         return OPERATOR_FINISHED;
532 }
533
534 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
535 {
536
537         if(!ED_operator_sequencer_active(C)) {
538                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
539                 return OPERATOR_CANCELLED;
540         }
541
542
543         /* drag drop has set the names */
544         if(RNA_collection_length(op->ptr, "files")) {
545                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME|SEQPROP_NOPATHS);
546                 return sequencer_add_image_strip_exec(C, op);
547         }
548         
549         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
550         
551         if(!RNA_property_is_set(op->ptr, "relative_path"))
552                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
553
554         WM_event_add_fileselect(C, op);
555         return OPERATOR_RUNNING_MODAL;
556 }
557
558
559 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
560 {
561         
562         /* identifiers */
563         ot->name= "Add Image Strip";
564         ot->idname= "SEQUENCER_OT_image_strip_add";
565         ot->description= "Add an image or image sequence to the sequencer";
566
567         /* api callbacks */
568         ot->invoke= sequencer_add_image_strip_invoke;
569         ot->exec= sequencer_add_image_strip_exec;
570
571         ot->poll= ED_operator_scene_editable;
572         
573         /* flags */
574         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
575         
576         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH);
577         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME|SEQPROP_FILES);
578 }
579
580
581 /* add_effect_strip operator */
582 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
583 {
584         Scene *scene= CTX_data_scene(C);
585         Editing *ed= seq_give_editing(scene, TRUE);
586
587         Sequence *seq;  /* generic strip vars */
588         Strip *strip;
589         struct SeqEffectHandle sh;
590
591         int start_frame, end_frame, channel, type; /* operator props */
592         
593         Sequence *seq1, *seq2, *seq3;
594         const char *error_msg;
595
596         start_frame= RNA_int_get(op->ptr, "frame_start");
597         end_frame= RNA_int_get(op->ptr, "frame_end");
598         channel= RNA_int_get(op->ptr, "channel");
599
600         type= RNA_enum_get(op->ptr, "type");
601         
602         // XXX move to invoke
603         if(!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
604                 BKE_report(op->reports, RPT_ERROR, error_msg);
605                 return OPERATOR_CANCELLED;
606         }
607
608         /* If seq1 is NULL and no error was rasied it means the seq is standalone
609          * (like color strips) and we need to check its start and end frames are valid */
610         if (seq1==NULL && end_frame <= start_frame) {
611                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
612                 return OPERATOR_CANCELLED;
613         }
614
615         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
616         seq->type= type;
617
618         BLI_strncpy(seq->name+2, give_seqname(seq), sizeof(seq->name)-2);
619         seqbase_unique_name_recursive(&ed->seqbase, seq);
620
621         sh = get_sequence_effect(seq);
622
623         seq->seq1= seq1;
624         seq->seq2= seq2;
625         seq->seq3= seq3;
626
627         sh.init(seq);
628
629         if (!seq1) { /* effect has no deps */
630                 seq->len= 1;
631                 seq_tx_set_final_right(seq, end_frame);
632         }
633
634         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
635
636         calc_sequence(scene, seq);
637         
638         /* basic defaults */
639         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
640         strip->len = seq->len;
641         strip->us= 1;
642         if(seq->len>0)
643                 strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
644
645         if (seq->type==SEQ_PLUGIN) {
646                 char path[FILE_MAX];
647                 RNA_string_get(op->ptr, "filepath", path);
648
649                 sh.init_plugin(seq, path);
650
651                 if(seq->plugin==NULL) {
652                         BLI_remlink(ed->seqbasep, seq);
653                         seq_free_sequence(scene, seq);
654                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load.", path);
655                         return OPERATOR_CANCELLED;
656                 }
657         } else if (seq->type == SEQ_COLOR) {
658                 SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
659                 RNA_float_get_array(op->ptr, "color", colvars->col);
660                 seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
661
662         } else if (seq->type == SEQ_ADJUSTMENT) {
663                 seq->blend_mode= SEQ_CROSS;
664         }
665
666         /* an unset channel is a special case where we automatically go above
667          * the other strips. */
668         if(!RNA_property_is_set(op->ptr, "channel")) {
669                 if(seq->seq1) {
670                         int chan= MAX3( seq->seq1 ? seq->seq1->machine : 0,
671                                                         seq->seq2 ? seq->seq2->machine : 0,
672                                                         seq->seq3 ? seq->seq3->machine : 0);
673                         if(chan < MAXSEQ)
674                                 seq->machine= chan;
675                 }
676         }
677
678         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
679                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
680         }
681
682         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
683
684
685         /* not sure if this is needed with update_changed_seq_and_deps.
686          * it was NOT called in blender 2.4x, but wont hurt */
687         sort_seq(scene); 
688
689         if (RNA_boolean_get(op->ptr, "replace_sel")) {
690                 deselect_all_seq(scene);
691                 seq_active_set(scene, seq);
692                 seq->flag |= SELECT;
693         }
694
695         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
696
697         return OPERATOR_FINISHED;
698 }
699
700
701 /* add color */
702 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
703 {
704         short is_type_set= RNA_property_is_set(op->ptr, "type");
705         int type= -1;
706         int prop_flag= SEQPROP_ENDFRAME;
707
708         if(!ED_operator_sequencer_active(C)) {
709                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
710                 return OPERATOR_CANCELLED;
711         }
712
713         if(is_type_set) {
714                 type= RNA_enum_get(op->ptr, "type");
715
716                 /* when invoking an effect strip which uses inputs,
717                  * skip initialzing the channel from the mouse.
718                  * Instead leave the property unset so exec() initializes it to be
719                  * above the strips its applied to. */
720                 if(get_sequence_effect_num_inputs(type) != 0) {
721                         prop_flag |= SEQPROP_NOCHAN;
722                 }
723         }
724
725         sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
726
727         if (is_type_set && type==SEQ_PLUGIN) {
728
729                 if(!RNA_property_is_set(op->ptr, "relative_path"))
730                         RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
731
732                 /* only plugins need the file selector */
733                 return WM_operator_filesel(C, op, event);
734         }
735         else {
736                 return sequencer_add_effect_strip_exec(C, op);
737         }
738 }
739
740 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
741 {
742         /* identifiers */
743         ot->name= "Add Effect Strip";
744         ot->idname= "SEQUENCER_OT_effect_strip_add";
745         ot->description= "Add an effect to the sequencer, most are applied on top of existing strips";
746
747         /* api callbacks */
748         ot->invoke= sequencer_add_effect_strip_invoke;
749         ot->exec= sequencer_add_effect_strip_exec;
750
751         ot->poll= ED_operator_scene_editable;
752         
753         /* flags */
754         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
755         
756         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
757         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
758         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
759         RNA_def_float_vector(ot->srna, "color", 3, NULL, 0.0f, 1.0f, "Color", "Initialize the strip with this color (only used when type='COLOR')", 0.0f, 1.0f);
760 }