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