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