Merging r46111 through r46136 from trunk into soc-2011-tomato
[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_library.h"
54 #include "BKE_main.h"
55 #include "BKE_sequencer.h"
56 #include "BKE_movieclip.h"
57 #include "BKE_report.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "RNA_define.h"
63 #include "RNA_enum_types.h"
64
65 /* for menu/popup icons etc etc*/
66
67 #include "ED_screen.h"
68
69 #include "UI_view2d.h"
70
71 #include "BKE_sound.h"
72
73 #ifdef WITH_AUDASPACE
74 #  include "AUD_C-API.h"
75 #endif
76
77 /* own include */
78 #include "sequencer_intern.h"
79
80 /* Generic functions, reused by add strip operators */
81
82 /* avoid passing multiple args and be more verbose */
83 #define SEQPROP_STARTFRAME  (1 << 0)
84 #define SEQPROP_ENDFRAME    (1 << 1)
85 #define SEQPROP_NOPATHS     (1 << 2)
86 #define SEQPROP_NOCHAN      (1 << 3)
87
88 #define SELECT 1
89
90 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
91 {
92         if (flag & SEQPROP_STARTFRAME)
93                 RNA_def_int(ot->srna, "frame_start", 0, INT_MIN, INT_MAX, "Start Frame", "Start frame of the sequence strip", INT_MIN, INT_MAX);
94         
95         if (flag & SEQPROP_ENDFRAME)
96                 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 */
97         
98         RNA_def_int(ot->srna, "channel", 1, 1, MAXSEQ, "Channel", "Channel to place this strip into", 1, MAXSEQ);
99         
100         RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "Replace the current selection");
101
102         RNA_def_boolean(ot->srna, "overlap", 0, "Allow Overlap", "Don't correct overlap on new sequence strips");
103 }
104
105 static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op, const char *identifier)
106 {
107         if (RNA_struct_find_property(op->ptr, identifier)) {
108                 Scene *scene = CTX_data_scene(C);
109                 Sequence *last_seq = seq_active_get(scene);
110                 if (last_seq && last_seq->strip && SEQ_HAS_PATH(last_seq)) {
111                         char path[sizeof(last_seq->strip->dir)];
112                         BLI_strncpy(path, last_seq->strip->dir, sizeof(path));
113                         BLI_path_abs(path, G.main->name);
114                         RNA_string_set(op->ptr, identifier, path);
115                 }
116         }
117 }
118
119 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
120 {
121         View2D *v2d = UI_view2d_fromcontext(C);
122         
123         float mval_v2d[2];
124         
125         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &mval_v2d[0], &mval_v2d[1]);
126
127         /* effect strips don't need a channel initialized from the mouse */
128         if (!(flag & SEQPROP_NOCHAN)) {
129                 RNA_int_set(op->ptr, "channel", (int)mval_v2d[1] + 0.5f);
130         }
131
132         RNA_int_set(op->ptr, "frame_start", (int)mval_v2d[0]);
133         
134         if ((flag & SEQPROP_ENDFRAME) && RNA_struct_property_is_set(op->ptr, "frame_end") == 0)
135                 RNA_int_set(op->ptr, "frame_end", (int)mval_v2d[0] + 25);  // XXX arbitary but ok for now.
136
137         if (!(flag & SEQPROP_NOPATHS)) {
138                 sequencer_generic_invoke_path__internal(C, op, "filepath");
139                 sequencer_generic_invoke_path__internal(C, op, "directory");
140         }
141 }
142
143 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
144 {
145         int relative = RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path");
146         int is_file = -1;
147         memset(seq_load, 0, sizeof(SeqLoadInfo));
148
149         seq_load->start_frame =  RNA_int_get(op->ptr, "frame_start");
150         seq_load->end_frame =    seq_load->start_frame; /* un-set */
151
152         seq_load->channel =      RNA_int_get(op->ptr, "channel");
153         seq_load->len =          1; // images only, if endframe isn't set!
154
155         if (RNA_struct_find_property(op->ptr, "filepath")) {
156                 RNA_string_get(op->ptr, "filepath", seq_load->path); /* full path, file is set by the caller */
157                 is_file = 1;
158         }
159         else if (RNA_struct_find_property(op->ptr, "directory")) {
160                 RNA_string_get(op->ptr, "directory", seq_load->path); /* full path, file is set by the caller */
161                 is_file = 0;
162         }
163
164         if ((is_file != -1) && relative)
165                 BLI_path_rel(seq_load->path, G.main->name);
166
167         
168         if (RNA_struct_find_property(op->ptr, "frame_end")) {
169                 seq_load->end_frame = RNA_int_get(op->ptr, "frame_end");
170         }
171
172         if (RNA_struct_find_property(op->ptr, "replace_sel") && RNA_boolean_get(op->ptr, "replace_sel"))
173                 seq_load->flag |= SEQ_LOAD_REPLACE_SEL;
174
175         if (RNA_struct_find_property(op->ptr, "cache") && RNA_boolean_get(op->ptr, "cache"))
176                 seq_load->flag |= SEQ_LOAD_SOUND_CACHE;
177
178         if (RNA_struct_find_property(op->ptr, "sound") && RNA_boolean_get(op->ptr, "sound"))
179                 seq_load->flag |= SEQ_LOAD_MOVIE_SOUND;
180
181         /* always use this for ops */
182         seq_load->flag |= SEQ_LOAD_FRAME_ADVANCE;
183
184
185         if (is_file == 1) {
186                 BLI_strncpy(seq_load->name, BLI_path_basename(seq_load->path), sizeof(seq_load->name));
187         }
188         else if (RNA_struct_find_property(op->ptr, "files")) {
189                 /* used for image strip */
190                 /* best guess, first images name */
191                 RNA_BEGIN (op->ptr, itemptr, "files")
192                 {
193                         char *name = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
194                         BLI_strncpy(seq_load->name, name, sizeof(seq_load->name));
195                         MEM_freeN(name);
196                         break;
197                 }
198                 RNA_END;
199         }
200 }
201
202 /* add scene operator */
203 static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
204 {
205         Scene *scene = CTX_data_scene(C);
206         Editing *ed = seq_give_editing(scene, TRUE);
207         
208         Scene *sce_seq;
209
210         Sequence *seq;  /* generic strip vars */
211         Strip *strip;
212         
213         int start_frame, channel; /* operator props */
214         
215         start_frame = RNA_int_get(op->ptr, "frame_start");
216         channel = RNA_int_get(op->ptr, "channel");
217         
218         sce_seq = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
219         
220         if (sce_seq == NULL) {
221                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
222                 return OPERATOR_CANCELLED;
223         }
224         
225         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
226         seq->type = SEQ_SCENE;
227         seq->blend_mode = SEQ_CROSS; /* so alpha adjustment fade to the strip below */
228
229         seq->scene = sce_seq;
230         
231         /* basic defaults */
232         seq->strip = strip = MEM_callocN(sizeof(Strip), "strip");
233         seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
234         strip->us = 1;
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 + seq->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_struct_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 /* add movieclip operator */
302 static int sequencer_add_movieclip_strip_exec(bContext *C, wmOperator *op)
303 {
304         Scene *scene = CTX_data_scene(C);
305         Editing *ed = seq_give_editing(scene, TRUE);
306         
307         MovieClip *clip;
308
309         Sequence *seq;  /* generic strip vars */
310         Strip *strip;
311         
312         int start_frame, channel; /* operator props */
313         
314         start_frame = RNA_int_get(op->ptr, "frame_start");
315         channel = RNA_int_get(op->ptr, "channel");
316         
317         clip = BLI_findlink(&CTX_data_main(C)->movieclip, RNA_enum_get(op->ptr, "clip"));
318         
319         if (clip == NULL) {
320                 BKE_report(op->reports, RPT_ERROR, "MovieClip not found");
321                 return OPERATOR_CANCELLED;
322         }
323         
324         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
325         seq->type = SEQ_MOVIECLIP;
326         seq->blend_mode = SEQ_CROSS;
327         seq->clip = clip;
328
329         if (seq->clip->id.us == 0)
330                 seq->clip->id.us = 1;
331
332         /* basic defaults */
333         seq->strip = strip = MEM_callocN(sizeof(Strip), "strip");
334         seq->len =  BKE_movieclip_get_duration(clip);
335         strip->us = 1;
336         
337         BLI_strncpy(seq->name + 2, clip->id.name + 2, sizeof(seq->name) - 2);
338         seqbase_unique_name_recursive(&ed->seqbase, seq);
339
340         calc_sequence_disp(scene, seq);
341         sort_seq(scene);
342         
343         if (RNA_boolean_get(op->ptr, "replace_sel")) {
344                 deselect_all_seq(scene);
345                 seq_active_set(scene, seq);
346                 seq->flag |= SELECT;
347         }
348
349         if (RNA_boolean_get(op->ptr, "overlap") == FALSE) {
350                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
351         }
352
353         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
354         
355         return OPERATOR_FINISHED;
356 }
357
358
359 static int sequencer_add_movieclip_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
360 {
361         if (!ED_operator_sequencer_active(C)) {
362                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
363                 return OPERATOR_CANCELLED;
364         }
365
366         if (!RNA_struct_property_is_set(op->ptr, "clip"))
367                 return WM_enum_search_invoke(C, op, event);
368
369         sequencer_generic_invoke_xy__internal(C, op, event, 0);
370         return sequencer_add_movieclip_strip_exec(C, op);
371         // needs a menu
372         // return WM_menu_invoke(C, op, event);
373 }
374
375
376 void SEQUENCER_OT_movieclip_strip_add(struct wmOperatorType *ot)
377 {
378         PropertyRNA *prop;
379         
380         /* identifiers */
381         ot->name = "Add MovieClip Strip";
382         ot->idname = "SEQUENCER_OT_movieclip_strip_add";
383         ot->description = "Add a movieclip strip to the sequencer";
384
385         /* api callbacks */
386         ot->invoke = sequencer_add_movieclip_strip_invoke;
387         ot->exec = sequencer_add_movieclip_strip_exec;
388
389         ot->poll = ED_operator_scene_editable;
390         
391         /* flags */
392         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
393         
394         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
395         prop = RNA_def_enum(ot->srna, "clip", DummyRNA_NULL_items, 0, "Clip", "");
396         RNA_def_enum_funcs(prop, RNA_movieclip_itemf);
397         ot->prop = prop;
398 }
399
400
401 static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoadFunc seq_load_func)
402 {
403         Scene *scene = CTX_data_scene(C); /* only for sound */
404         Editing *ed = seq_give_editing(scene, TRUE);
405         SeqLoadInfo seq_load;
406         Sequence *seq;
407         int tot_files;
408         const short overlap = RNA_boolean_get(op->ptr, "overlap");
409
410         seq_load_operator_info(&seq_load, op);
411
412         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
413                 deselect_all_seq(scene);
414
415         tot_files = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
416
417         if (tot_files) {
418                 /* multiple files */
419                 char dir_only[FILE_MAX];
420                 char file_only[FILE_MAX];
421
422                 BLI_split_dir_part(seq_load.path, dir_only, sizeof(dir_only));
423
424                 RNA_BEGIN (op->ptr, itemptr, "files")
425                 {
426                         RNA_string_get(&itemptr, "name", file_only);
427                         BLI_join_dirfile(seq_load.path, sizeof(seq_load.path), dir_only, file_only);
428
429                         seq = seq_load_func(C, ed->seqbasep, &seq_load);
430                         if (seq) {
431                                 if (overlap == FALSE) {
432                                         if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
433                                 }
434                         }
435                 }
436                 RNA_END;
437         }
438         else {
439                 /* single file */
440                 seq = seq_load_func(C, ed->seqbasep, &seq_load);
441                 if (seq) {
442                         if (overlap == FALSE) {
443                                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
444                         }
445                 }
446         }
447
448         if (seq_load.tot_success == 0) {
449                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
450                 return OPERATOR_CANCELLED;
451         }
452
453         sort_seq(scene);
454         seq_update_muting(ed);
455
456         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
457
458         return OPERATOR_FINISHED;
459 }
460
461 /* add movie operator */
462 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
463 {
464         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
465 }
466
467
468 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
469 {
470
471         if (!ED_operator_sequencer_active(C)) {
472                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
473                 return OPERATOR_CANCELLED;
474         }
475
476         /* This is for drag and drop */
477         if ((RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) ||
478             RNA_struct_property_is_set(op->ptr, "filepath"))
479         {
480                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
481                 return sequencer_add_movie_strip_exec(C, op);
482         }
483         
484         sequencer_generic_invoke_xy__internal(C, op, event, 0);
485         
486         WM_event_add_fileselect(C, op);
487         return OPERATOR_RUNNING_MODAL;
488
489         //return sequencer_add_movie_strip_exec(C, op);
490 }
491
492
493 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
494 {
495         
496         /* identifiers */
497         ot->name = "Add Movie Strip";
498         ot->idname = "SEQUENCER_OT_movie_strip_add";
499         ot->description = "Add a movie strip to the sequencer";
500
501         /* api callbacks */
502         ot->invoke = sequencer_add_movie_strip_invoke;
503         ot->exec = sequencer_add_movie_strip_exec;
504
505         ot->poll = ED_operator_scene_editable;
506         
507         /* flags */
508         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
509         
510         WM_operator_properties_filesel(ot, FOLDERFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
511         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
512         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
513 }
514
515 /* add sound operator */
516
517 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
518 {
519         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
520 }
521
522 static int sequencer_add_sound_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         /* This is for drag and drop */
531         if ((RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) ||
532             RNA_struct_property_is_set(op->ptr, "filepath"))
533         {
534                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
535                 return sequencer_add_sound_strip_exec(C, op);
536         }
537         
538         sequencer_generic_invoke_xy__internal(C, op, event, 0);
539
540         WM_event_add_fileselect(C, op);
541         return OPERATOR_RUNNING_MODAL;
542
543         //return sequencer_add_sound_strip_exec(C, op);
544 }
545
546
547 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
548 {
549         
550         /* identifiers */
551         ot->name = "Add Sound Strip";
552         ot->idname = "SEQUENCER_OT_sound_strip_add";
553         ot->description = "Add a sound strip to the sequencer";
554
555         /* api callbacks */
556         ot->invoke = sequencer_add_sound_strip_invoke;
557         ot->exec = sequencer_add_sound_strip_exec;
558
559         ot->poll = ED_operator_scene_editable;
560         
561         /* flags */
562         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
563         
564         WM_operator_properties_filesel(ot, FOLDERFILE | SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
565         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
566         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
567 }
568
569 /* add image operator */
570 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
571 {
572         /* cant use the generic function for this */
573
574         Scene *scene = CTX_data_scene(C); /* only for sound */
575         Editing *ed = seq_give_editing(scene, TRUE);
576         SeqLoadInfo seq_load;
577         Sequence *seq;
578
579         Strip *strip;
580         StripElem *se;
581
582         seq_load_operator_info(&seq_load, op);
583
584         /* images are unique in how they handle this - 1 per strip elem */
585         seq_load.len = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
586
587         if (seq_load.len == 0)
588                 return OPERATOR_CANCELLED;
589
590         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
591                 deselect_all_seq(scene);
592
593
594         /* main adding function */
595         seq = sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
596         strip = seq->strip;
597         se = strip->stripdata;
598
599         RNA_BEGIN (op->ptr, itemptr, "files")
600         {
601                 char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
602                 BLI_strncpy(se->name, filename, sizeof(se->name));
603                 MEM_freeN(filename);
604                 se++;
605         }
606         RNA_END;
607
608         if (seq_load.len == 1) {
609                 if (seq_load.start_frame < seq_load.end_frame) {
610                         seq->endstill = seq_load.end_frame - seq_load.start_frame;
611                 }
612         }
613         
614         calc_sequence_disp(scene, seq);
615
616         sort_seq(scene);
617
618         /* last active name */
619         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR - 1);
620
621         if (RNA_boolean_get(op->ptr, "overlap") == FALSE) {
622                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
623         }
624
625         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
626
627         return OPERATOR_FINISHED;
628 }
629
630 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
631 {
632
633         if (!ED_operator_sequencer_active(C)) {
634                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
635                 return OPERATOR_CANCELLED;
636         }
637
638
639         /* drag drop has set the names */
640         if (RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) {
641                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME | SEQPROP_NOPATHS);
642                 return sequencer_add_image_strip_exec(C, op);
643         }
644         
645         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
646
647         WM_event_add_fileselect(C, op);
648         return OPERATOR_RUNNING_MODAL;
649 }
650
651
652 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
653 {
654         
655         /* identifiers */
656         ot->name = "Add Image Strip";
657         ot->idname = "SEQUENCER_OT_image_strip_add";
658         ot->description = "Add an image or image sequence to the sequencer";
659
660         /* api callbacks */
661         ot->invoke = sequencer_add_image_strip_invoke;
662         ot->exec = sequencer_add_image_strip_exec;
663
664         ot->poll = ED_operator_scene_editable;
665         
666         /* flags */
667         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
668         
669         WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
670         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME | SEQPROP_ENDFRAME);
671 }
672
673
674 /* add_effect_strip operator */
675 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
676 {
677         Scene *scene = CTX_data_scene(C);
678         Editing *ed = seq_give_editing(scene, TRUE);
679
680         Sequence *seq;  /* generic strip vars */
681         Strip *strip;
682         struct SeqEffectHandle sh;
683
684         int start_frame, end_frame, channel, type; /* operator props */
685         
686         Sequence *seq1, *seq2, *seq3;
687         const char *error_msg;
688
689         start_frame = RNA_int_get(op->ptr, "frame_start");
690         end_frame = RNA_int_get(op->ptr, "frame_end");
691         channel = RNA_int_get(op->ptr, "channel");
692
693         type = RNA_enum_get(op->ptr, "type");
694         
695         // XXX move to invoke
696         if (!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
697                 BKE_report(op->reports, RPT_ERROR, error_msg);
698                 return OPERATOR_CANCELLED;
699         }
700
701         /* If seq1 is NULL and no error was raised it means the seq is standalone
702          * (like color strips) and we need to check its start and end frames are valid */
703         if (seq1 == NULL && end_frame <= start_frame) {
704                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
705                 return OPERATOR_CANCELLED;
706         }
707
708         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
709         seq->type = type;
710
711         BLI_strncpy(seq->name + 2, give_seqname(seq), sizeof(seq->name) - 2);
712         seqbase_unique_name_recursive(&ed->seqbase, seq);
713
714         sh = get_sequence_effect(seq);
715
716         seq->seq1 = seq1;
717         seq->seq2 = seq2;
718         seq->seq3 = seq3;
719
720         sh.init(seq);
721
722         if (!seq1) { /* effect has no deps */
723                 seq->len = 1;
724                 seq_tx_set_final_right(seq, end_frame);
725         }
726
727         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
728
729         calc_sequence(scene, seq);
730         
731         /* basic defaults */
732         seq->strip = strip = MEM_callocN(sizeof(Strip), "strip");
733         strip->us = 1;
734
735         if (seq->type == SEQ_PLUGIN) {
736                 char path[FILE_MAX];
737                 RNA_string_get(op->ptr, "filepath", path);
738
739                 sh.init_plugin(seq, path);
740
741                 if (seq->plugin == NULL) {
742                         BLI_remlink(ed->seqbasep, seq);
743                         seq_free_sequence(scene, seq);
744                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load", path);
745                         return OPERATOR_CANCELLED;
746                 }
747         }
748         else if (seq->type == SEQ_COLOR) {
749                 SolidColorVars *colvars = (SolidColorVars *)seq->effectdata;
750                 RNA_float_get_array(op->ptr, "color", colvars->col);
751                 seq->blend_mode = SEQ_CROSS; /* so alpha adjustment fade to the strip below */
752
753         }
754         else if (seq->type == SEQ_ADJUSTMENT) {
755                 seq->blend_mode = SEQ_CROSS;
756         }
757
758         /* an unset channel is a special case where we automatically go above
759          * the other strips. */
760         if (!RNA_struct_property_is_set(op->ptr, "channel")) {
761                 if (seq->seq1) {
762                         int chan = MAX3(seq->seq1 ? seq->seq1->machine : 0,
763                                         seq->seq2 ? seq->seq2->machine : 0,
764                                         seq->seq3 ? seq->seq3->machine : 0);
765                         if (chan < MAXSEQ)
766                                 seq->machine = chan;
767                 }
768         }
769
770         if (RNA_boolean_get(op->ptr, "overlap") == FALSE) {
771                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
772         }
773
774         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
775
776
777         /* not sure if this is needed with update_changed_seq_and_deps.
778          * it was NOT called in blender 2.4x, but wont hurt */
779         sort_seq(scene); 
780
781         if (RNA_boolean_get(op->ptr, "replace_sel")) {
782                 deselect_all_seq(scene);
783                 seq_active_set(scene, seq);
784                 seq->flag |= SELECT;
785         }
786
787         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
788
789         return OPERATOR_FINISHED;
790 }
791
792
793 /* add color */
794 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
795 {
796         short is_type_set = RNA_struct_property_is_set(op->ptr, "type");
797         int type = -1;
798         int prop_flag = SEQPROP_ENDFRAME;
799
800         if (!ED_operator_sequencer_active(C)) {
801                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
802                 return OPERATOR_CANCELLED;
803         }
804
805         if (is_type_set) {
806                 type = RNA_enum_get(op->ptr, "type");
807
808                 /* when invoking an effect strip which uses inputs,
809                  * skip initializing the channel from the mouse.
810                  * Instead leave the property unset so exec() initializes it to be
811                  * above the strips its applied to. */
812                 if (get_sequence_effect_num_inputs(type) != 0) {
813                         prop_flag |= SEQPROP_NOCHAN;
814                 }
815         }
816
817         sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
818
819         if (is_type_set && type == SEQ_PLUGIN) {
820                 /* only plugins need the file selector */
821                 WM_event_add_fileselect(C, op);
822                 return OPERATOR_RUNNING_MODAL;
823         }
824         else {
825                 return sequencer_add_effect_strip_exec(C, op);
826         }
827 }
828
829 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
830 {
831         /* identifiers */
832         ot->name = "Add Effect Strip";
833         ot->idname = "SEQUENCER_OT_effect_strip_add";
834         ot->description = "Add an effect to the sequencer, most are applied on top of existing strips";
835
836         /* api callbacks */
837         ot->invoke = sequencer_add_effect_strip_invoke;
838         ot->exec = sequencer_add_effect_strip_exec;
839
840         ot->poll = ED_operator_scene_editable;
841         
842         /* flags */
843         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
844         
845         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
846         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME | SEQPROP_ENDFRAME);
847         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
848         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);
849 }