- dont create scene.sequence_editor on read, means data-browser or autocomplete will...
[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 = BKE_sequencer_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 = BKE_sequencer_editing_get(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         BKE_sequencer_sort(scene);
243         
244         if (RNA_boolean_get(op->ptr, "replace_sel")) {
245                 deselect_all_seq(scene);
246                 BKE_sequencer_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 = BKE_sequencer_editing_get(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         BKE_sequencer_sort(scene);
342         
343         if (RNA_boolean_get(op->ptr, "replace_sel")) {
344                 deselect_all_seq(scene);
345                 BKE_sequencer_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 = BKE_sequencer_editing_get(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         if (RNA_struct_property_is_set(op->ptr, "files"))
416                 tot_files = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
417         else
418                 tot_files = 0;
419
420         if (tot_files) {
421                 /* multiple files */
422                 char dir_only[FILE_MAX];
423                 char file_only[FILE_MAX];
424
425                 BLI_split_dir_part(seq_load.path, dir_only, sizeof(dir_only));
426
427                 RNA_BEGIN (op->ptr, itemptr, "files")
428                 {
429                         RNA_string_get(&itemptr, "name", file_only);
430                         BLI_join_dirfile(seq_load.path, sizeof(seq_load.path), dir_only, file_only);
431
432                         seq = seq_load_func(C, ed->seqbasep, &seq_load);
433                         if (seq) {
434                                 if (overlap == FALSE) {
435                                         if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
436                                 }
437                         }
438                 }
439                 RNA_END;
440         }
441         else {
442                 /* single file */
443                 seq = seq_load_func(C, ed->seqbasep, &seq_load);
444                 if (seq) {
445                         if (overlap == FALSE) {
446                                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
447                         }
448                 }
449         }
450
451         if (seq_load.tot_success == 0) {
452                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
453                 return OPERATOR_CANCELLED;
454         }
455
456         BKE_sequencer_sort(scene);
457         seq_update_muting(ed);
458
459         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
460
461         return OPERATOR_FINISHED;
462 }
463
464 /* add movie operator */
465 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
466 {
467         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
468 }
469
470
471 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
472 {
473
474         if (!ED_operator_sequencer_active(C)) {
475                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
476                 return OPERATOR_CANCELLED;
477         }
478
479         /* This is for drag and drop */
480         if ((RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) ||
481             RNA_struct_property_is_set(op->ptr, "filepath"))
482         {
483                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
484                 return sequencer_add_movie_strip_exec(C, op);
485         }
486         
487         sequencer_generic_invoke_xy__internal(C, op, event, 0);
488         
489         WM_event_add_fileselect(C, op);
490         return OPERATOR_RUNNING_MODAL;
491
492         //return sequencer_add_movie_strip_exec(C, op);
493 }
494
495
496 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
497 {
498         
499         /* identifiers */
500         ot->name = "Add Movie Strip";
501         ot->idname = "SEQUENCER_OT_movie_strip_add";
502         ot->description = "Add a movie strip to the sequencer";
503
504         /* api callbacks */
505         ot->invoke = sequencer_add_movie_strip_invoke;
506         ot->exec = sequencer_add_movie_strip_exec;
507
508         ot->poll = ED_operator_scene_editable;
509         
510         /* flags */
511         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
512         
513         WM_operator_properties_filesel(ot, FOLDERFILE | MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
514         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
515         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
516 }
517
518 /* add sound operator */
519
520 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
521 {
522         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
523 }
524
525 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
526 {
527
528         if (!ED_operator_sequencer_active(C)) {
529                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
530                 return OPERATOR_CANCELLED;
531         }
532         
533         /* This is for drag and drop */
534         if ((RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) ||
535             RNA_struct_property_is_set(op->ptr, "filepath"))
536         {
537                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
538                 return sequencer_add_sound_strip_exec(C, op);
539         }
540         
541         sequencer_generic_invoke_xy__internal(C, op, event, 0);
542
543         WM_event_add_fileselect(C, op);
544         return OPERATOR_RUNNING_MODAL;
545
546         //return sequencer_add_sound_strip_exec(C, op);
547 }
548
549
550 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
551 {
552         
553         /* identifiers */
554         ot->name = "Add Sound Strip";
555         ot->idname = "SEQUENCER_OT_sound_strip_add";
556         ot->description = "Add a sound strip to the sequencer";
557
558         /* api callbacks */
559         ot->invoke = sequencer_add_sound_strip_invoke;
560         ot->exec = sequencer_add_sound_strip_exec;
561
562         ot->poll = ED_operator_scene_editable;
563         
564         /* flags */
565         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
566         
567         WM_operator_properties_filesel(ot, FOLDERFILE | SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
568         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
569         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
570 }
571
572 /* add image operator */
573 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
574 {
575         /* cant use the generic function for this */
576
577         Scene *scene = CTX_data_scene(C); /* only for sound */
578         Editing *ed = BKE_sequencer_editing_get(scene, TRUE);
579         SeqLoadInfo seq_load;
580         Sequence *seq;
581
582         Strip *strip;
583         StripElem *se;
584
585         seq_load_operator_info(&seq_load, op);
586
587         /* images are unique in how they handle this - 1 per strip elem */
588         seq_load.len = RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
589
590         if (seq_load.len == 0)
591                 return OPERATOR_CANCELLED;
592
593         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
594                 deselect_all_seq(scene);
595
596
597         /* main adding function */
598         seq = sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
599         strip = seq->strip;
600         se = strip->stripdata;
601
602         RNA_BEGIN (op->ptr, itemptr, "files")
603         {
604                 char *filename = RNA_string_get_alloc(&itemptr, "name", NULL, 0);
605                 BLI_strncpy(se->name, filename, sizeof(se->name));
606                 MEM_freeN(filename);
607                 se++;
608         }
609         RNA_END;
610
611         if (seq_load.len == 1) {
612                 if (seq_load.start_frame < seq_load.end_frame) {
613                         seq->endstill = seq_load.end_frame - seq_load.start_frame;
614                 }
615         }
616         
617         calc_sequence_disp(scene, seq);
618
619         BKE_sequencer_sort(scene);
620
621         /* last active name */
622         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR - 1);
623
624         if (RNA_boolean_get(op->ptr, "overlap") == FALSE) {
625                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
626         }
627
628         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
629
630         return OPERATOR_FINISHED;
631 }
632
633 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
634 {
635
636         if (!ED_operator_sequencer_active(C)) {
637                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
638                 return OPERATOR_CANCELLED;
639         }
640
641
642         /* drag drop has set the names */
643         if (RNA_struct_property_is_set(op->ptr, "files") && RNA_collection_length(op->ptr, "files")) {
644                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME | SEQPROP_NOPATHS);
645                 return sequencer_add_image_strip_exec(C, op);
646         }
647         
648         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
649
650         WM_event_add_fileselect(C, op);
651         return OPERATOR_RUNNING_MODAL;
652 }
653
654
655 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
656 {
657         
658         /* identifiers */
659         ot->name = "Add Image Strip";
660         ot->idname = "SEQUENCER_OT_image_strip_add";
661         ot->description = "Add an image or image sequence to the sequencer";
662
663         /* api callbacks */
664         ot->invoke = sequencer_add_image_strip_invoke;
665         ot->exec = sequencer_add_image_strip_exec;
666
667         ot->poll = ED_operator_scene_editable;
668         
669         /* flags */
670         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
671         
672         WM_operator_properties_filesel(ot, FOLDERFILE | IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY | WM_FILESEL_RELPATH | WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
673         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME | SEQPROP_ENDFRAME);
674 }
675
676
677 /* add_effect_strip operator */
678 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
679 {
680         Scene *scene = CTX_data_scene(C);
681         Editing *ed = BKE_sequencer_editing_get(scene, TRUE);
682
683         Sequence *seq;  /* generic strip vars */
684         Strip *strip;
685         struct SeqEffectHandle sh;
686
687         int start_frame, end_frame, channel, type; /* operator props */
688         
689         Sequence *seq1, *seq2, *seq3;
690         const char *error_msg;
691
692         start_frame = RNA_int_get(op->ptr, "frame_start");
693         end_frame = RNA_int_get(op->ptr, "frame_end");
694         channel = RNA_int_get(op->ptr, "channel");
695
696         type = RNA_enum_get(op->ptr, "type");
697         
698         // XXX move to invoke
699         if (!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
700                 BKE_report(op->reports, RPT_ERROR, error_msg);
701                 return OPERATOR_CANCELLED;
702         }
703
704         /* If seq1 is NULL and no error was raised it means the seq is standalone
705          * (like color strips) and we need to check its start and end frames are valid */
706         if (seq1 == NULL && end_frame <= start_frame) {
707                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
708                 return OPERATOR_CANCELLED;
709         }
710
711         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
712         seq->type = type;
713
714         BLI_strncpy(seq->name + 2, give_seqname(seq), sizeof(seq->name) - 2);
715         seqbase_unique_name_recursive(&ed->seqbase, seq);
716
717         sh = get_sequence_effect(seq);
718
719         seq->seq1 = seq1;
720         seq->seq2 = seq2;
721         seq->seq3 = seq3;
722
723         sh.init(seq);
724
725         if (!seq1) { /* effect has no deps */
726                 seq->len = 1;
727                 seq_tx_set_final_right(seq, end_frame);
728         }
729
730         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
731
732         calc_sequence(scene, seq);
733         
734         /* basic defaults */
735         seq->strip = strip = MEM_callocN(sizeof(Strip), "strip");
736         strip->us = 1;
737
738         if (seq->type == SEQ_PLUGIN) {
739                 char path[FILE_MAX];
740                 RNA_string_get(op->ptr, "filepath", path);
741
742                 sh.init_plugin(seq, path);
743
744                 if (seq->plugin == NULL) {
745                         BLI_remlink(ed->seqbasep, seq);
746                         seq_free_sequence(scene, seq);
747                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load", path);
748                         return OPERATOR_CANCELLED;
749                 }
750         }
751         else if (seq->type == SEQ_COLOR) {
752                 SolidColorVars *colvars = (SolidColorVars *)seq->effectdata;
753                 RNA_float_get_array(op->ptr, "color", colvars->col);
754                 seq->blend_mode = SEQ_CROSS; /* so alpha adjustment fade to the strip below */
755
756         }
757         else if (seq->type == SEQ_ADJUSTMENT) {
758                 seq->blend_mode = SEQ_CROSS;
759         }
760
761         /* an unset channel is a special case where we automatically go above
762          * the other strips. */
763         if (!RNA_struct_property_is_set(op->ptr, "channel")) {
764                 if (seq->seq1) {
765                         int chan = MAX3(seq->seq1 ? seq->seq1->machine : 0,
766                                         seq->seq2 ? seq->seq2->machine : 0,
767                                         seq->seq3 ? seq->seq3->machine : 0);
768                         if (chan < MAXSEQ)
769                                 seq->machine = chan;
770                 }
771         }
772
773         if (RNA_boolean_get(op->ptr, "overlap") == FALSE) {
774                 if (seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
775         }
776
777         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
778
779
780         /* not sure if this is needed with update_changed_seq_and_deps.
781          * it was NOT called in blender 2.4x, but wont hurt */
782         BKE_sequencer_sort(scene); 
783
784         if (RNA_boolean_get(op->ptr, "replace_sel")) {
785                 deselect_all_seq(scene);
786                 BKE_sequencer_active_set(scene, seq);
787                 seq->flag |= SELECT;
788         }
789
790         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene);
791
792         return OPERATOR_FINISHED;
793 }
794
795
796 /* add color */
797 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
798 {
799         short is_type_set = RNA_struct_property_is_set(op->ptr, "type");
800         int type = -1;
801         int prop_flag = SEQPROP_ENDFRAME;
802
803         if (!ED_operator_sequencer_active(C)) {
804                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
805                 return OPERATOR_CANCELLED;
806         }
807
808         if (is_type_set) {
809                 type = RNA_enum_get(op->ptr, "type");
810
811                 /* when invoking an effect strip which uses inputs,
812                  * skip initializing the channel from the mouse.
813                  * Instead leave the property unset so exec() initializes it to be
814                  * above the strips its applied to. */
815                 if (get_sequence_effect_num_inputs(type) != 0) {
816                         prop_flag |= SEQPROP_NOCHAN;
817                 }
818         }
819
820         sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
821
822         if (is_type_set && type == SEQ_PLUGIN) {
823                 /* only plugins need the file selector */
824                 WM_event_add_fileselect(C, op);
825                 return OPERATOR_RUNNING_MODAL;
826         }
827         else {
828                 return sequencer_add_effect_strip_exec(C, op);
829         }
830 }
831
832 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
833 {
834         /* identifiers */
835         ot->name = "Add Effect Strip";
836         ot->idname = "SEQUENCER_OT_effect_strip_add";
837         ot->description = "Add an effect to the sequencer, most are applied on top of existing strips";
838
839         /* api callbacks */
840         ot->invoke = sequencer_add_effect_strip_invoke;
841         ot->exec = sequencer_add_effect_strip_exec;
842
843         ot->poll = ED_operator_scene_editable;
844         
845         /* flags */
846         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
847         
848         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
849         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME | SEQPROP_ENDFRAME);
850         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
851         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);
852 }