7e4d02036ef316666505607c822fcc90f9b02db9
[blender.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         BLI_strncpy(seq->name+2, sce_seq->id.name+2, sizeof(seq->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, sizeof(dir_only), 0);
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         WM_event_add_fileselect(C, op);
384         return OPERATOR_RUNNING_MODAL;
385
386         //return sequencer_add_movie_strip_exec(C, op);
387 }
388
389
390 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
391 {
392         
393         /* identifiers */
394         ot->name= "Add Movie Strip";
395         ot->idname= "SEQUENCER_OT_movie_strip_add";
396         ot->description= "Add a movie strip to the sequencer";
397
398         /* api callbacks */
399         ot->invoke= sequencer_add_movie_strip_invoke;
400         ot->exec= sequencer_add_movie_strip_exec;
401
402         ot->poll= ED_operator_scene_editable;
403         
404         /* flags */
405         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
406         
407         WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
408         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
409         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
410 }
411
412 /* add sound operator */
413
414 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
415 {
416         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
417 }
418
419 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
420 {
421
422         if(!ED_operator_sequencer_active(C)) {
423                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
424                 return OPERATOR_CANCELLED;
425         }
426         
427         /* This is for drag and drop */
428         if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath")) {
429                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
430                 return sequencer_add_sound_strip_exec(C, op);
431         }
432         
433         sequencer_generic_invoke_xy__internal(C, op, event, 0);
434
435         WM_event_add_fileselect(C, op);
436         return OPERATOR_RUNNING_MODAL;
437
438         //return sequencer_add_sound_strip_exec(C, op);
439 }
440
441
442 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
443 {
444         
445         /* identifiers */
446         ot->name= "Add Sound Strip";
447         ot->idname= "SEQUENCER_OT_sound_strip_add";
448         ot->description= "Add a sound strip to the sequencer";
449
450         /* api callbacks */
451         ot->invoke= sequencer_add_sound_strip_invoke;
452         ot->exec= sequencer_add_sound_strip_exec;
453
454         ot->poll= ED_operator_scene_editable;
455         
456         /* flags */
457         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
458         
459         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
460         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
461         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
462 }
463
464 /* add image operator */
465 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
466 {
467         /* cant use the generic function for this */
468
469         Scene *scene= CTX_data_scene(C); /* only for sound */
470         Editing *ed= seq_give_editing(scene, TRUE);
471         SeqLoadInfo seq_load;
472         Sequence *seq;
473
474         Strip *strip;
475         StripElem *se;
476
477         seq_load_operator_info(&seq_load, op);
478
479         /* images are unique in how they handle this - 1 per strip elem */
480         seq_load.len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
481
482         if(seq_load.len==0)
483                 return OPERATOR_CANCELLED;
484
485         if(seq_load.flag & SEQ_LOAD_REPLACE_SEL)
486                 deselect_all_seq(scene);
487
488
489         /* main adding function */
490         seq= sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
491         strip= seq->strip;
492         se= strip->stripdata;
493
494         RNA_BEGIN(op->ptr, itemptr, "files") {
495                 char *filename= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
496                 BLI_strncpy(se->name, filename, sizeof(se->name));
497                 MEM_freeN(filename);
498                 se++;
499         }
500         RNA_END;
501
502         if(seq_load.len == 1) {
503                 if(seq_load.start_frame < seq_load.end_frame) {
504                         seq->endstill= seq_load.end_frame - seq_load.start_frame;
505                 }
506         }
507         
508         calc_sequence_disp(scene, seq);
509
510         sort_seq(scene);
511
512         /* last active name */
513         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
514
515         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
516                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
517         }
518
519         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
520
521         return OPERATOR_FINISHED;
522 }
523
524 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
525 {
526
527         if(!ED_operator_sequencer_active(C)) {
528                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
529                 return OPERATOR_CANCELLED;
530         }
531
532
533         /* drag drop has set the names */
534         if(RNA_collection_length(op->ptr, "files")) {
535                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME|SEQPROP_NOPATHS);
536                 return sequencer_add_image_strip_exec(C, op);
537         }
538         
539         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
540
541         WM_event_add_fileselect(C, op);
542         return OPERATOR_RUNNING_MODAL;
543 }
544
545
546 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
547 {
548         
549         /* identifiers */
550         ot->name= "Add Image Strip";
551         ot->idname= "SEQUENCER_OT_image_strip_add";
552         ot->description= "Add an image or image sequence to the sequencer";
553
554         /* api callbacks */
555         ot->invoke= sequencer_add_image_strip_invoke;
556         ot->exec= sequencer_add_image_strip_exec;
557
558         ot->poll= ED_operator_scene_editable;
559         
560         /* flags */
561         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
562         
563         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
564         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
565 }
566
567
568 /* add_effect_strip operator */
569 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
570 {
571         Scene *scene= CTX_data_scene(C);
572         Editing *ed= seq_give_editing(scene, TRUE);
573
574         Sequence *seq;  /* generic strip vars */
575         Strip *strip;
576         struct SeqEffectHandle sh;
577
578         int start_frame, end_frame, channel, type; /* operator props */
579         
580         Sequence *seq1, *seq2, *seq3;
581         const char *error_msg;
582
583         start_frame= RNA_int_get(op->ptr, "frame_start");
584         end_frame= RNA_int_get(op->ptr, "frame_end");
585         channel= RNA_int_get(op->ptr, "channel");
586
587         type= RNA_enum_get(op->ptr, "type");
588         
589         // XXX move to invoke
590         if(!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
591                 BKE_report(op->reports, RPT_ERROR, error_msg);
592                 return OPERATOR_CANCELLED;
593         }
594
595         /* If seq1 is NULL and no error was rasied it means the seq is standalone
596          * (like color strips) and we need to check its start and end frames are valid */
597         if (seq1==NULL && end_frame <= start_frame) {
598                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
599                 return OPERATOR_CANCELLED;
600         }
601
602         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
603         seq->type= type;
604
605         BLI_strncpy(seq->name+2, give_seqname(seq), sizeof(seq->name)-2);
606         seqbase_unique_name_recursive(&ed->seqbase, seq);
607
608         sh = get_sequence_effect(seq);
609
610         seq->seq1= seq1;
611         seq->seq2= seq2;
612         seq->seq3= seq3;
613
614         sh.init(seq);
615
616         if (!seq1) { /* effect has no deps */
617                 seq->len= 1;
618                 seq_tx_set_final_right(seq, end_frame);
619         }
620
621         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
622
623         calc_sequence(scene, seq);
624         
625         /* basic defaults */
626         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
627         strip->len = seq->len;
628         strip->us= 1;
629         if(seq->len>0)
630                 strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
631
632         if (seq->type==SEQ_PLUGIN) {
633                 char path[FILE_MAX];
634                 RNA_string_get(op->ptr, "filepath", path);
635
636                 sh.init_plugin(seq, path);
637
638                 if(seq->plugin==NULL) {
639                         BLI_remlink(ed->seqbasep, seq);
640                         seq_free_sequence(scene, seq);
641                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load", path);
642                         return OPERATOR_CANCELLED;
643                 }
644         } else if (seq->type == SEQ_COLOR) {
645                 SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
646                 RNA_float_get_array(op->ptr, "color", colvars->col);
647                 seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
648
649         } else if (seq->type == SEQ_ADJUSTMENT) {
650                 seq->blend_mode= SEQ_CROSS;
651         }
652
653         /* an unset channel is a special case where we automatically go above
654          * the other strips. */
655         if(!RNA_property_is_set(op->ptr, "channel")) {
656                 if(seq->seq1) {
657                         int chan= MAX3( seq->seq1 ? seq->seq1->machine : 0,
658                                                         seq->seq2 ? seq->seq2->machine : 0,
659                                                         seq->seq3 ? seq->seq3->machine : 0);
660                         if(chan < MAXSEQ)
661                                 seq->machine= chan;
662                 }
663         }
664
665         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
666                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
667         }
668
669         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
670
671
672         /* not sure if this is needed with update_changed_seq_and_deps.
673          * it was NOT called in blender 2.4x, but wont hurt */
674         sort_seq(scene); 
675
676         if (RNA_boolean_get(op->ptr, "replace_sel")) {
677                 deselect_all_seq(scene);
678                 seq_active_set(scene, seq);
679                 seq->flag |= SELECT;
680         }
681
682         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
683
684         return OPERATOR_FINISHED;
685 }
686
687
688 /* add color */
689 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
690 {
691         short is_type_set= RNA_property_is_set(op->ptr, "type");
692         int type= -1;
693         int prop_flag= SEQPROP_ENDFRAME;
694
695         if(!ED_operator_sequencer_active(C)) {
696                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
697                 return OPERATOR_CANCELLED;
698         }
699
700         if(is_type_set) {
701                 type= RNA_enum_get(op->ptr, "type");
702
703                 /* when invoking an effect strip which uses inputs,
704                  * skip initialzing the channel from the mouse.
705                  * Instead leave the property unset so exec() initializes it to be
706                  * above the strips its applied to. */
707                 if(get_sequence_effect_num_inputs(type) != 0) {
708                         prop_flag |= SEQPROP_NOCHAN;
709                 }
710         }
711
712         sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
713
714         if (is_type_set && type==SEQ_PLUGIN) {
715                 /* only plugins need the file selector */
716                 return WM_operator_filesel(C, op, event);
717         }
718         else {
719                 return sequencer_add_effect_strip_exec(C, op);
720         }
721 }
722
723 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
724 {
725         /* identifiers */
726         ot->name= "Add Effect Strip";
727         ot->idname= "SEQUENCER_OT_effect_strip_add";
728         ot->description= "Add an effect to the sequencer, most are applied on top of existing strips";
729
730         /* api callbacks */
731         ot->invoke= sequencer_add_effect_strip_invoke;
732         ot->exec= sequencer_add_effect_strip_exec;
733
734         ot->poll= ED_operator_scene_editable;
735         
736         /* flags */
737         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
738         
739         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
740         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
741         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
742         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);
743 }