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