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