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