Renamed the FILE_OPEN option for the file browser to FILE_OPENFILE to cleanup compile...
[blender.git] / source / blender / editors / space_sequencer / sequencer_add.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation, 2003-2009, Campbell Barton
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif
36 #include <sys/types.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_storage_types.h"
43
44 #include "IMB_imbuf_types.h"
45 #include "IMB_imbuf.h"
46
47 #include "DNA_ipo_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_sequence_types.h"
53 #include "DNA_view2d_types.h"
54 #include "DNA_userdef_types.h"
55 #include "DNA_sound_types.h"
56
57 #include "BKE_context.h"
58 #include "BKE_global.h"
59 #include "BKE_image.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_plugin_types.h"
63 #include "BKE_sequencer.h"
64 #include "BKE_scene.h"
65 #include "BKE_utildefines.h"
66 #include "BKE_report.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76 #include "RNA_enum_types.h"
77
78 /* for menu/popup icons etc etc*/
79 #include "UI_interface.h"
80 #include "UI_resources.h"
81
82 #include "ED_anim_api.h"
83 #include "ED_space_api.h"
84 #include "ED_types.h"
85 #include "ED_screen.h"
86 #include "ED_util.h"
87 #include "ED_fileselect.h"
88
89 #include "UI_interface.h"
90 #include "UI_resources.h"
91 #include "UI_view2d.h"
92
93 #include "BKE_sound.h"
94 #include "AUD_C-API.h"
95
96 /* own include */
97 #include "sequencer_intern.h"
98
99 /* Generic functions, reused by add strip operators */
100
101 /* avoid passing multiple args and be more verbose */
102 #define SEQPROP_STARTFRAME      1<<0
103 #define SEQPROP_ENDFRAME        1<<1
104 #define SEQPROP_FILES           1<<2
105
106 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
107 {
108         RNA_def_string(ot->srna, "name", "", MAX_ID_NAME-2, "Name", "Name of the new sequence strip");
109
110         if(flag & SEQPROP_STARTFRAME)
111                 RNA_def_int(ot->srna, "start_frame", 0, INT_MIN, INT_MAX, "Start Frame", "Start frame of the sequence strip", INT_MIN, INT_MAX);
112         
113         if(flag & SEQPROP_ENDFRAME)
114                 RNA_def_int(ot->srna, "end_frame", 0, INT_MIN, INT_MAX, "End Frame", "End frame for the color strip", INT_MIN, INT_MAX); /* not useual since most strips have a fixed length */
115         
116         RNA_def_int(ot->srna, "channel", 1, 1, MAXSEQ, "Channel", "Channel to place this strip into", 1, MAXSEQ);
117         
118         RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "replace the current selection");
119
120         if(flag & SEQPROP_FILES)
121                 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
122 }
123
124 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
125 {
126         ARegion *ar= CTX_wm_region(C);
127         View2D *v2d= UI_view2d_fromcontext(C);
128         
129         short mval[2];  
130         float mval_v2d[2];
131         
132
133         mval[0]= event->x - ar->winrct.xmin;
134         mval[1]= event->y - ar->winrct.ymin;
135         
136         UI_view2d_region_to_view(v2d, mval[0], mval[1], &mval_v2d[0], &mval_v2d[1]);
137         
138         RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
139         RNA_int_set(op->ptr, "start_frame", (int)mval_v2d[0]);
140         
141         if ((flag & SEQPROP_ENDFRAME) && RNA_property_is_set(op->ptr, "end_frame")==0)
142                 RNA_int_set(op->ptr, "end_frame", (int)mval_v2d[0] + 25); // XXX arbitary but ok for now.
143         
144 }
145
146 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
147 {
148         memset(seq_load, 0, sizeof(SeqLoadInfo));
149
150         seq_load->start_frame=  RNA_int_get(op->ptr, "start_frame");
151         seq_load->end_frame=    seq_load->start_frame; /* un-set */
152
153         seq_load->channel=              RNA_int_get(op->ptr, "channel");
154         seq_load->len=                  1; // images only, if endframe isnt set!
155
156         RNA_string_get(op->ptr, "name", seq_load->name+2);
157
158         RNA_string_get(op->ptr, "path", seq_load->path); /* full path, file is set by the caller */
159
160         if (RNA_struct_find_property(op->ptr, "end_frame")) {
161                 seq_load->end_frame = RNA_int_get(op->ptr, "end_frame");
162         }
163
164         if (RNA_struct_find_property(op->ptr, "replace_sel") && RNA_boolean_get(op->ptr, "replace_sel"))
165                 seq_load->flag |= SEQ_LOAD_REPLACE_SEL;
166
167         if (RNA_struct_find_property(op->ptr, "cache") && RNA_boolean_get(op->ptr, "cache"))
168                 seq_load->flag |= SEQ_LOAD_SOUND_CACHE;
169
170         if (RNA_struct_find_property(op->ptr, "sound") && RNA_boolean_get(op->ptr, "sound"))
171                 seq_load->flag |= SEQ_LOAD_MOVIE_SOUND;
172
173         /* always use this for ops */
174         seq_load->flag |= SEQ_LOAD_FRAME_ADVANCE;
175 }
176
177 /* add scene operator */
178 static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
179 {
180         Scene *scene= CTX_data_scene(C);
181         Editing *ed= seq_give_editing(scene, TRUE);
182         
183         Scene *sce_seq;
184
185         Sequence *seq;  /* generic strip vars */
186         Strip *strip;
187         StripElem *se;
188         
189         int start_frame, channel; /* operator props */
190         
191         start_frame= RNA_int_get(op->ptr, "start_frame");
192         channel= RNA_int_get(op->ptr, "channel");
193         
194         sce_seq= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "type"));
195         
196         if (sce_seq==NULL) {
197                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
198                 return OPERATOR_CANCELLED;
199         }
200         
201         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
202         
203         seq->type= SEQ_SCENE;
204         seq->scene= sce_seq;
205         
206         /* basic defaults */
207         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
208         strip->len = seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
209         strip->us= 1;
210         
211         strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
212         
213         if(RNA_property_is_set(op->ptr, "name"))
214                 RNA_string_get(op->ptr, "name", seq->name+2);
215         else
216                 strcpy(seq->name+2, sce_seq->id.name+2);
217         
218         calc_sequence_disp(seq);
219         sort_seq(scene);
220         
221         if (RNA_boolean_get(op->ptr, "replace_sel")) {
222                 deselect_all_seq(scene);
223                 active_seq_set(scene, seq);
224                 seq->flag |= SELECT;
225         }
226         
227         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
228         
229         return OPERATOR_FINISHED;
230 }
231
232
233 static int sequencer_add_scene_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
234 {
235         sequencer_generic_invoke_xy__internal(C, op, event, 0);
236         return sequencer_add_scene_strip_exec(C, op);
237         // needs a menu
238         // return WM_menu_invoke(C, op, event);
239 }
240
241
242 void SEQUENCER_OT_scene_strip_add(struct wmOperatorType *ot)
243 {
244         PropertyRNA *prop;
245         
246         /* identifiers */
247         ot->name= "Add Scene Strip";
248         ot->idname= "SEQUENCER_OT_scene_strip_add";
249         ot->description= "Add a strip to the sequencer using a blender scene as a source";
250
251         /* api callbacks */
252         ot->invoke= sequencer_add_scene_strip_invoke;
253         ot->exec= sequencer_add_scene_strip_exec;
254
255         ot->poll= ED_operator_sequencer_active;
256         
257         /* flags */
258         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
259         
260         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
261         prop= RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, 0, "Type", "");
262         RNA_def_enum_funcs(prop, RNA_scene_itemf);
263 }
264
265 static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoadFunc seq_load_func)
266 {
267         Scene *scene= CTX_data_scene(C); /* only for sound */
268         Editing *ed= seq_give_editing(scene, TRUE);
269         SeqLoadInfo seq_load;
270         Sequence *seq;
271         int tot_files;
272
273         seq_load_operator_info(&seq_load, op);
274
275         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
276                 deselect_all_seq(scene);
277
278         tot_files= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
279
280         if(tot_files) {
281                 /* multiple files */
282                 char dir_only[FILE_MAX];
283                 char file_only[FILE_MAX];
284
285                 BLI_split_dirfile_basic(seq_load.path, dir_only, NULL);
286
287                 RNA_BEGIN(op->ptr, itemptr, "files") {
288                         RNA_string_get(&itemptr, "name", file_only);
289                         BLI_join_dirfile(seq_load.path, dir_only, file_only);
290
291                         seq= seq_load_func(C, ed->seqbasep, &seq_load);
292                 }
293                 RNA_END;
294         }
295         else {
296                 /* single file */
297                 seq= seq_load_func(C, ed->seqbasep, &seq_load);
298         }
299
300         if (seq_load.tot_success==0) {
301                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
302                 return OPERATOR_CANCELLED;
303         }
304
305         sort_seq(scene);
306         seq_update_muting(ed);
307
308         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
309
310         return OPERATOR_FINISHED;
311 }
312
313 /* add movie operator */
314 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
315 {
316         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
317 }
318
319
320 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
321 {       
322         sequencer_generic_invoke_xy__internal(C, op, event, 0);
323         return WM_operator_filesel(C, op, event);
324         //return sequencer_add_movie_strip_exec(C, op);
325 }
326
327
328 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
329 {
330         
331         /* identifiers */
332         ot->name= "Add Movie Strip";
333         ot->idname= "SEQUENCER_OT_movie_strip_add";
334         ot->description= "Add a movie strip to the sequencer";
335
336         /* api callbacks */
337         ot->invoke= sequencer_add_movie_strip_invoke;
338         ot->exec= sequencer_add_movie_strip_exec;
339
340         ot->poll= ED_operator_sequencer_active;
341         
342         /* flags */
343         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
344         
345         WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE);
346         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
347         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
348 }
349
350 /* add sound operator */
351
352 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
353 {
354         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
355 }
356
357 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
358 {       
359         sequencer_generic_invoke_xy__internal(C, op, event, 0);
360         return WM_operator_filesel(C, op, event);
361         //return sequencer_add_sound_strip_exec(C, op);
362 }
363
364
365 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
366 {
367         
368         /* identifiers */
369         ot->name= "Add Sound Strip";
370         ot->idname= "SEQUENCER_OT_sound_strip_add";
371         ot->description= "Add a sound strip to the sequencer";
372
373         /* api callbacks */
374         ot->invoke= sequencer_add_sound_strip_invoke;
375         ot->exec= sequencer_add_sound_strip_exec;
376
377         ot->poll= ED_operator_sequencer_active;
378         
379         /* flags */
380         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
381         
382         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE);
383         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
384         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
385 }
386
387 /* add image operator */
388 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
389 {
390         /* cant use the generic function for this */
391
392         Scene *scene= CTX_data_scene(C); /* only for sound */
393         Editing *ed= seq_give_editing(scene, TRUE);
394         SeqLoadInfo seq_load;
395         Sequence *seq;
396
397         Strip *strip;
398         StripElem *se;
399
400         seq_load_operator_info(&seq_load, op);
401
402         /* images are unique in how they handle this - 1 per strip elem */
403         seq_load.len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
404
405         if(seq_load.len==0)
406                 seq_load.len= 1;
407
408         if(seq_load.flag & SEQ_LOAD_REPLACE_SEL)
409                 deselect_all_seq(scene);
410
411         
412         /* main adding function */
413         seq= sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
414         strip= seq->strip;
415         se= strip->stripdata;
416
417         if(seq_load.len > 1) {
418                 RNA_BEGIN(op->ptr, itemptr, "files") {
419                         RNA_string_get(&itemptr, "name", se->name);
420                         se++;
421                 }
422                 RNA_END;
423         }
424         else {
425                 BLI_split_dirfile_basic(seq_load.path, NULL, se->name);
426                 if(seq_load.start_frame < seq_load.end_frame) {
427                         seq->endstill= seq_load.end_frame - seq_load.start_frame;
428                 }
429         }
430         
431         calc_sequence_disp(seq);
432
433         sort_seq(scene);
434
435         /* last active name */
436         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
437         
438         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
439
440         return OPERATOR_FINISHED;
441 }
442
443 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
444 {
445         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
446         return WM_operator_filesel(C, op, event);       
447         //return sequencer_add_image_strip_exec(C, op);
448 }
449
450
451 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
452 {
453         
454         /* identifiers */
455         ot->name= "Add Image Strip";
456         ot->idname= "SEQUENCER_OT_image_strip_add";
457         ot->description= "Add an image or image sequence to the sequencer";
458
459         /* api callbacks */
460         ot->invoke= sequencer_add_image_strip_invoke;
461         ot->exec= sequencer_add_image_strip_exec;
462
463         ot->poll= ED_operator_sequencer_active;
464         
465         /* flags */
466         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
467         
468         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE);
469         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME|SEQPROP_FILES);
470 }
471
472
473 /* add_effect_strip operator */
474 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
475 {
476         Scene *scene= CTX_data_scene(C);
477         Editing *ed= seq_give_editing(scene, TRUE);
478
479         Sequence *seq;  /* generic strip vars */
480         Strip *strip;
481         StripElem *se;
482         struct SeqEffectHandle sh;
483
484         int start_frame, end_frame, channel, type; /* operator props */
485         
486         Sequence *seq1, *seq2, *seq3;
487         char *error_msg;
488
489         start_frame= RNA_int_get(op->ptr, "start_frame");
490         end_frame= RNA_int_get(op->ptr, "end_frame");
491         channel= RNA_int_get(op->ptr, "channel");
492
493         type= RNA_enum_get(op->ptr, "type");
494         
495         // XXX We need unique names and move to invoke
496         if(!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
497                 BKE_report(op->reports, RPT_ERROR, error_msg);
498                 return OPERATOR_CANCELLED;
499         }
500
501         /* If seq1 is NULL and no error was rasied it means the seq is standalone
502          * (like color strips) and we need to check its start and end frames are valid */
503         if (seq1==NULL && end_frame <= start_frame) {
504                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
505                 return OPERATOR_CANCELLED;
506         }
507
508         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
509         seq->type= type;
510
511         if(RNA_property_is_set(op->ptr, "name"))
512                 RNA_string_get(op->ptr, "name", seq->name+2);
513         else
514                 strcpy(seq->name+2, give_seqname(seq));
515
516         seqUniqueName(ed->seqbasep, seq);
517
518         sh = get_sequence_effect(seq);
519
520         seq->seq1= seq1;
521         seq->seq2= seq2;
522         seq->seq3= seq3;
523
524         sh.init(seq);
525
526         if (!seq1) { /* effect has no deps */
527                 seq->len= 1;
528                 seq_tx_set_final_right(seq, end_frame);
529         }
530
531         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
532
533         calc_sequence(seq);
534         
535         /* basic defaults */
536         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
537         strip->len = seq->len;
538         strip->us= 1;
539         if(seq->len>0)
540                 strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
541
542         if (seq->type==SEQ_PLUGIN) {
543                 char path[FILE_MAX];
544                 RNA_string_get(op->ptr, "path", path);
545
546                 sh.init_plugin(seq, path);
547
548                 if(seq->plugin==NULL) {
549                         BLI_remlink(ed->seqbasep, seq);
550                         seq_free_sequence(scene, seq);
551                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load.", path);
552                         return OPERATOR_CANCELLED;
553                 }
554         }
555         else if (seq->type==SEQ_COLOR) {
556                 SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
557                 RNA_float_get_array(op->ptr, "color", colvars->col);
558         }
559
560         if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
561
562         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
563
564
565         /* not sure if this is needed with update_changed_seq_and_deps.
566          * it was NOT called in blender 2.4x, but wont hurt */
567         sort_seq(scene); 
568
569         if (RNA_boolean_get(op->ptr, "replace_sel")) {
570                 deselect_all_seq(scene);
571                 active_seq_set(scene, seq);
572                 seq->flag |= SELECT;
573         }
574
575         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
576
577         return OPERATOR_FINISHED;
578 }
579
580
581 /* add color */
582 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
583 {
584         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
585
586         if (RNA_property_is_set(op->ptr, "type") && RNA_enum_get(op->ptr, "type")==SEQ_PLUGIN) {
587                 /* only plugins need the file selector */
588                 return WM_operator_filesel(C, op, event);
589         }
590         else {
591                 return sequencer_add_effect_strip_exec(C, op);
592         }
593 }
594
595 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
596 {
597         /* identifiers */
598         ot->name= "Add Effect Strip";
599         ot->idname= "SEQUENCER_OT_effect_strip_add";
600         ot->description= "Add an effect to the sequencer, most are applied on top of existing strips";
601
602         /* api callbacks */
603         ot->invoke= sequencer_add_effect_strip_invoke;
604         ot->exec= sequencer_add_effect_strip_exec;
605
606         ot->poll= ED_operator_sequencer_active;
607         
608         /* flags */
609         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
610         
611         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE);
612         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
613         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
614         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);
615 }