Merging trunk up to r39447.
[blender-staging.git] / source / blender / editors / space_sequencer / sequencer_add.c
index c426cd9fdc2b92a5759dad13435e0f66f725f6c1..2fe16b3a85dbcb8bd6d5c188e34a25e87147035d 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/space_sequencer/sequencer_add.c
+ *  \ingroup spseq
+ */
+
+
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
@@ -40,7 +45,7 @@
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 #include "BLI_storage_types.h"
-
+#include "BLI_utildefines.h"
 
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
@@ -51,8 +56,6 @@
 #include "BKE_sequencer.h"
 #include "BKE_report.h"
 
-#include "BIF_gl.h"
-#include "BIF_glutil.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 #include "UI_view2d.h"
 
 #include "BKE_sound.h"
-#include "AUD_C-API.h"
+
+#ifdef WITH_AUDASPACE
+#  include "AUD_C-API.h"
+#endif
 
 /* own include */
 #include "sequencer_intern.h"
 /* Generic functions, reused by add strip operators */
 
 /* avoid passing multiple args and be more verbose */
-#define SEQPROP_STARTFRAME     1<<0
-#define SEQPROP_ENDFRAME       1<<1
-#define SEQPROP_FILES          1<<2
+#define SEQPROP_STARTFRAME     (1<<0)
+#define SEQPROP_ENDFRAME       (1<<1)
+#define SEQPROP_NOPATHS                (1<<2)
+#define SEQPROP_NOCHAN         (1<<3)
+
+#define SELECT 1
 
 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
 {
@@ -91,8 +100,7 @@ static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
        
        RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "replace the current selection");
 
-       if(flag & SEQPROP_FILES)
-               RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
+       RNA_def_boolean(ot->srna, "overlap", 0, "Allow Overlap", "Don't correct overlap on new sequence strips");
 }
 
 static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op, const char *identifier)
@@ -103,7 +111,7 @@ static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op,
                if(last_seq && last_seq->strip && SEQ_HAS_PATH(last_seq)) {
                        char path[sizeof(last_seq->strip->dir)];
                        BLI_strncpy(path, last_seq->strip->dir, sizeof(path));
-                       BLI_path_abs(path, G.sce);
+                       BLI_path_abs(path, G.main->name);
                        RNA_string_set(op->ptr, identifier, path);
                }
        }
@@ -111,30 +119,31 @@ static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op,
 
 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
 {
-       ARegion *ar= CTX_wm_region(C);
        View2D *v2d= UI_view2d_fromcontext(C);
        
-       short mval[2];  
        float mval_v2d[2];
        
+       UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &mval_v2d[0], &mval_v2d[1]);
+
+       /* effect strips dont need a channel initialized from the mouse */
+       if(!(flag & SEQPROP_NOCHAN)) {
+               RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
+       }
 
-       mval[0]= event->x - ar->winrct.xmin;
-       mval[1]= event->y - ar->winrct.ymin;
-       
-       UI_view2d_region_to_view(v2d, mval[0], mval[1], &mval_v2d[0], &mval_v2d[1]);
-       
-       RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
        RNA_int_set(op->ptr, "frame_start", (int)mval_v2d[0]);
        
        if ((flag & SEQPROP_ENDFRAME) && RNA_property_is_set(op->ptr, "frame_end")==0)
                RNA_int_set(op->ptr, "frame_end", (int)mval_v2d[0] + 25); // XXX arbitary but ok for now.
 
-       sequencer_generic_invoke_path__internal(C, op, "filepath");
-       sequencer_generic_invoke_path__internal(C, op, "directory");
+       if (!(flag & SEQPROP_NOPATHS)) {
+               sequencer_generic_invoke_path__internal(C, op, "filepath");
+               sequencer_generic_invoke_path__internal(C, op, "directory");
+       }
 }
 
 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
 {
+       int relative= RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path");
        int is_file= -1;
        memset(seq_load, 0, sizeof(SeqLoadInfo));
 
@@ -152,6 +161,10 @@ static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
                is_file= 0;
        }
 
+       if((is_file != -1) && relative)
+               BLI_path_rel(seq_load->path, G.main->name);
+
+       
        if (RNA_struct_find_property(op->ptr, "frame_end")) {
                seq_load->end_frame = RNA_int_get(op->ptr, "frame_end");
        }
@@ -195,7 +208,6 @@ static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
 
        Sequence *seq;  /* generic strip vars */
        Strip *strip;
-       StripElem *se;
        
        int start_frame, channel; /* operator props */
        
@@ -221,7 +233,7 @@ static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
        strip->len = seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
        strip->us= 1;
        
-       strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
+       strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
        
        strcpy(seq->name+2, sce_seq->id.name+2);
        seqbase_unique_name_recursive(&ed->seqbase, seq);
@@ -236,7 +248,11 @@ static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
                seq_active_set(scene, seq);
                seq->flag |= SELECT;
        }
-       
+
+       if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
+               if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+       }
+
        WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
        
        return OPERATOR_FINISHED;
@@ -250,6 +266,9 @@ static int sequencer_add_scene_strip_invoke(bContext *C, wmOperator *op, wmEvent
                return OPERATOR_CANCELLED;
        }
 
+       if(!RNA_property_is_set(op->ptr, "scene"))
+               return WM_enum_search_invoke(C, op, event);
+
        sequencer_generic_invoke_xy__internal(C, op, event, 0);
        return sequencer_add_scene_strip_exec(C, op);
        // needs a menu
@@ -288,6 +307,7 @@ static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoad
        SeqLoadInfo seq_load;
        Sequence *seq;
        int tot_files;
+       const short overlap= RNA_boolean_get(op->ptr, "overlap");
 
        seq_load_operator_info(&seq_load, op);
 
@@ -305,15 +325,23 @@ static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoad
 
                RNA_BEGIN(op->ptr, itemptr, "files") {
                        RNA_string_get(&itemptr, "name", file_only);
-                       BLI_join_dirfile(seq_load.path, dir_only, file_only);
+                       BLI_join_dirfile(seq_load.path, sizeof(seq_load.path), dir_only, file_only);
 
                        seq= seq_load_func(C, ed->seqbasep, &seq_load);
+
+                       if(overlap == FALSE) {
+                               if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+                       }
                }
                RNA_END;
        }
        else {
                /* single file */
                seq= seq_load_func(C, ed->seqbasep, &seq_load);
+
+               if(overlap == FALSE) {
+                       if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+               }
        }
 
        if (seq_load.tot_success==0) {
@@ -322,7 +350,7 @@ static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoad
        }
 
        sort_seq(scene);
-       seq_update_muting(scene, ed);
+       seq_update_muting(ed);
 
        WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
 
@@ -338,23 +366,23 @@ static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
 
 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath"))
-               return sequencer_add_movie_strip_exec(C, op);
 
        if(!ED_operator_sequencer_active(C)) {
                BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
                return OPERATOR_CANCELLED;
        }
 
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
-               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
-
        /* This is for drag and drop */
-       if(RNA_property_is_set(op->ptr, "filepath"))
+       if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath")) {
+               sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
                return sequencer_add_movie_strip_exec(C, op);
-
+       }
+       
        sequencer_generic_invoke_xy__internal(C, op, event, 0);
-
+       
+       if(!RNA_property_is_set(op->ptr, "relative_path"))
+               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
+       
        WM_event_add_fileselect(C, op);
        return OPERATOR_RUNNING_MODAL;
 
@@ -379,8 +407,8 @@ void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
-       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
+       WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
+       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
        RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
 }
 
@@ -393,23 +421,23 @@ static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
 
 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath"))
-               return sequencer_add_sound_strip_exec(C, op);
 
        if(!ED_operator_sequencer_active(C)) {
                BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
                return OPERATOR_CANCELLED;
        }
-
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
-               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
-
+       
        /* This is for drag and drop */
-       if(RNA_property_is_set(op->ptr, "filepath"))
+       if(RNA_collection_length(op->ptr, "files") || RNA_property_is_set(op->ptr, "filepath")) {
+               sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
                return sequencer_add_sound_strip_exec(C, op);
-
+       }
+       
        sequencer_generic_invoke_xy__internal(C, op, event, 0);
-
+       
+       if(!RNA_property_is_set(op->ptr, "relative_path"))
+               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
+       
        WM_event_add_fileselect(C, op);
        return OPERATOR_RUNNING_MODAL;
 
@@ -434,8 +462,8 @@ void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
-       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
+       WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
+       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
        RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
 }
 
@@ -489,7 +517,11 @@ static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
 
        /* last active name */
        strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
-       
+
+       if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
+               if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+       }
+
        WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
 
        return OPERATOR_FINISHED;
@@ -497,23 +529,26 @@ static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
 
 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       if(RNA_collection_length(op->ptr, "files"))
-               return sequencer_add_image_strip_exec(C, op);
 
        if(!ED_operator_sequencer_active(C)) {
                BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
                return OPERATOR_CANCELLED;
        }
 
-       if(!RNA_property_is_set(op->ptr, "relative_path"))
-               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
 
+       /* drag drop has set the names */
+       if(RNA_collection_length(op->ptr, "files")) {
+               sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME|SEQPROP_NOPATHS);
+               return sequencer_add_image_strip_exec(C, op);
+       }
+       
        sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
+       
+       if(!RNA_property_is_set(op->ptr, "relative_path"))
+               RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
 
        WM_event_add_fileselect(C, op);
        return OPERATOR_RUNNING_MODAL;
-
-       //return sequencer_add_image_strip_exec(C, op);
 }
 
 
@@ -534,8 +569,8 @@ void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
-       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH);
-       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME|SEQPROP_FILES);
+       WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH|WM_FILESEL_FILES);
+       sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
 }
 
 
@@ -547,13 +582,12 @@ static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
 
        Sequence *seq;  /* generic strip vars */
        Strip *strip;
-       StripElem *se;
        struct SeqEffectHandle sh;
 
        int start_frame, end_frame, channel, type; /* operator props */
        
        Sequence *seq1, *seq2, *seq3;
-       char *error_msg;
+       const char *error_msg;
 
        start_frame= RNA_int_get(op->ptr, "frame_start");
        end_frame= RNA_int_get(op->ptr, "frame_end");
@@ -602,7 +636,7 @@ static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
        strip->len = seq->len;
        strip->us= 1;
        if(seq->len>0)
-               strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
+               strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
 
        if (seq->type==SEQ_PLUGIN) {
                char path[FILE_MAX];
@@ -616,25 +650,30 @@ static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
                        BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load.", path);
                        return OPERATOR_CANCELLED;
                }
-       }
-       else if (seq->type==SEQ_COLOR) {
+       } else if (seq->type == SEQ_COLOR) {
                SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
                RNA_float_get_array(op->ptr, "color", colvars->col);
                seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
 
+       } else if (seq->type == SEQ_ADJUSTMENT) {
+               seq->blend_mode= SEQ_CROSS;
        }
 
-       // XXX, this conflicts with giving a channel with invoke, perhaps we should have an active channel
-       // but for now this is much more usable
-       if(seq->seq1 || seq->seq2 || seq->seq3) {
-               int chan= MAX3( seq->seq1 ? seq->seq1->machine : 0,
-                                               seq->seq2 ? seq->seq2->machine : 0,
-                                               seq->seq3 ? seq->seq3->machine : 0);
-               if(chan < MAXSEQ)
-                       seq->machine= chan;
+       /* an unset channel is a special case where we automatically go above
+        * the other strips. */
+       if(!RNA_property_is_set(op->ptr, "channel")) {
+               if(seq->seq1) {
+                       int chan= MAX3( seq->seq1 ? seq->seq1->machine : 0,
+                                                       seq->seq2 ? seq->seq2->machine : 0,
+                                                       seq->seq3 ? seq->seq3->machine : 0);
+                       if(chan < MAXSEQ)
+                               seq->machine= chan;
+               }
        }
 
-       if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+       if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
+               if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
+       }
 
        update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
 
@@ -658,14 +697,30 @@ static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
 /* add color */
 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
+       short is_type_set= RNA_property_is_set(op->ptr, "type");
+       int type= -1;
+       int prop_flag= SEQPROP_ENDFRAME;
+
        if(!ED_operator_sequencer_active(C)) {
                BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
                return OPERATOR_CANCELLED;
        }
 
-       sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
+       if(is_type_set) {
+               type= RNA_enum_get(op->ptr, "type");
+
+               /* when invoking an effect strip which uses inputs,
+                * skip initialzing the channel from the mouse.
+                * Instead leave the property unset so exec() initializes it to be
+                * above the strips its applied to. */
+               if(get_sequence_effect_num_inputs(type) != 0) {
+                       prop_flag |= SEQPROP_NOCHAN;
+               }
+       }
+
+       sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
 
-       if (RNA_property_is_set(op->ptr, "type") && RNA_enum_get(op->ptr, "type")==SEQ_PLUGIN) {
+       if (is_type_set && type==SEQ_PLUGIN) {
 
                if(!RNA_property_is_set(op->ptr, "relative_path"))
                        RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);