Animation Editors: Select Linked Operator
[blender.git] / source / blender / editors / space_action / action_select.c
index d8ed3fd10681cdeffe8b9c1d4d342b7d1f52cfd6..8f1d2d4be0e63e158371204eb1f8e83d54e110c9 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id: editaction.c 17746 2008-12-08 11:19:44Z aligorith $
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2008 Blender Foundation
  *
 #include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
+#include "BLI_dlrbTree.h"
 
 #include "DNA_anim_types.h"
-#include "DNA_action_types.h"
-#include "DNA_armature_types.h"
-#include "DNA_camera_types.h"
-#include "DNA_curve_types.h"
 #include "DNA_object_types.h"
-#include "DNA_screen_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_space_types.h"
-#include "DNA_constraint_types.h"
-#include "DNA_key_types.h"
-#include "DNA_lamp_types.h"
-#include "DNA_material_types.h"
-#include "DNA_userdef_types.h"
-#include "DNA_gpencil_types.h"
-#include "DNA_windowmanager_types.h"
-#include "DNA_world_types.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -64,6 +51,7 @@
 #include "BKE_fcurve.h"
 #include "BKE_key.h"
 #include "BKE_material.h"
+#include "BKE_nla.h"
 #include "BKE_object.h"
 #include "BKE_context.h"
 #include "BKE_utildefines.h"
 #include "UI_view2d.h"
 
 #include "ED_anim_api.h"
-#include "ED_keyframing.h"
 #include "ED_keyframes_draw.h"
 #include "ED_keyframes_edit.h"
 #include "ED_markers.h"
 #include "ED_screen.h"
-#include "ED_space_api.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -108,8 +94,8 @@ static void deselect_action_keys (bAnimContext *ac, short test, short sel)
        bAnimListElem *ale;
        int filter;
        
-       BeztEditData bed;
-       BeztEditFunc test_cb, sel_cb;
+       KeyframeEditData ked;
+       KeyframeEditFunc test_cb, sel_cb;
        
        /* determine type-based settings */
        if (ac->datatype == ANIMCONT_GPENCIL)
@@ -121,7 +107,7 @@ static void deselect_action_keys (bAnimContext *ac, short test, short sel)
        ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
        
        /* init BezTriple looping data */
-       memset(&bed, 0, sizeof(BeztEditData));
+       memset(&ked, 0, sizeof(KeyframeEditData));
        test_cb= ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
        
        /* See if we should be selecting or deselecting */
@@ -134,7 +120,7 @@ static void deselect_action_keys (bAnimContext *ac, short test, short sel)
                                //}
                        }
                        else {
-                               if (ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, test_cb, NULL)) {
+                               if (ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, test_cb, NULL)) {
                                        sel= SELECT_SUBTRACT;
                                        break;
                                }
@@ -150,7 +136,7 @@ static void deselect_action_keys (bAnimContext *ac, short test, short sel)
                //if (ale->type == ACTTYPE_GPLAYER)
                //      set_gplayer_frame_selection(ale->data, sel);
                //else
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, sel_cb, NULL);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, sel_cb, NULL);
        }
        
        /* Cleanup */
@@ -173,27 +159,28 @@ static int actkeys_deselectall_exec(bContext *C, wmOperator *op)
        else
                deselect_action_keys(&ac, 1, SELECT_ADD);
        
-       /* set notifier that things have changed */
-       ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_BOTH);
+       /* set notifier that keyframe selection have changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
        
        return OPERATOR_FINISHED;
 }
  
-void ACT_OT_keyframes_select_all_toggle (wmOperatorType *ot)
+void ACTION_OT_select_all_toggle (wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Select All";
-       ot->idname= "ACT_OT_keyframes_select_all_toggle";
+       ot->idname= "ACTION_OT_select_all_toggle";
+       ot->description= "Toggle selection of all keyframes";
        
        /* api callbacks */
        ot->exec= actkeys_deselectall_exec;
-       ot->poll= ED_operator_areaactive;
+       ot->poll= ED_operator_action_active;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* props */
-       RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
+       ot->prop= RNA_def_boolean(ot->srna, "invert", 0, "Invert", "");
 }
 
 /* ******************** Border Select Operator **************************** */
@@ -216,13 +203,13 @@ static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short
 {
        ListBase anim_data = {NULL, NULL};
        bAnimListElem *ale;
-       int filter;
+       int filter, filterflag;
        
-       BeztEditData bed;
-       BeztEditFunc ok_cb, select_cb;
+       KeyframeEditData ked;
+       KeyframeEditFunc ok_cb, select_cb;
        View2D *v2d= &ac->ar->v2d;
        rctf rectf;
-       float ymin=0, ymax=(float)(-ACHANNEL_HEIGHT);
+       float ymin=0, ymax=(float)(-ACHANNEL_HEIGHT_HALF);
        
        /* convert mouse coordinates to frame ranges and channel coordinates corrected for view pan/zoom */
        UI_view2d_region_to_view(v2d, rect.xmin, rect.ymin+2, &rectf.xmin, &rectf.ymin);
@@ -232,6 +219,14 @@ static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short
        filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CHANNELS);
        ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
        
+       /* get filtering flag for dopesheet data (if applicable) */
+       if (ac->datatype == ANIMCONT_DOPESHEET) {
+               bDopeSheet *ads= (bDopeSheet *)ac->data;
+               filterflag= ads->filterflag;
+       }
+       else
+               filterflag= 0;
+       
        /* get beztriple editing/validation funcs  */
        select_cb= ANIM_editkeyframes_select(selectmode);
        
@@ -241,11 +236,11 @@ static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short
                ok_cb= NULL;
                
        /* init editing data */
-       memset(&bed, 0, sizeof(BeztEditData));
+       memset(&ked, 0, sizeof(KeyframeEditData));
        
        /* loop over data, doing border select */
        for (ale= anim_data.first; ale; ale= ale->next) {
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
                
                /* get new vertical minimum extent of channel */
                ymin= ymax - ACHANNEL_STEP;
@@ -253,13 +248,13 @@ static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short
                /* set horizontal range (if applicable) */
                if (ELEM(mode, ACTKEYS_BORDERSEL_FRAMERANGE, ACTKEYS_BORDERSEL_ALLKEYS)) {
                        /* if channel is mapped in NLA, apply correction */
-                       if (nob) {
-                               bed.f1= get_action_frame(nob, rectf.xmin);
-                               bed.f2= get_action_frame(nob, rectf.xmax);
+                       if (adt) {
+                               ked.f1= BKE_nla_tweakedit_remap(adt, rectf.xmin, NLATIME_CONVERT_UNMAP);
+                               ked.f2= BKE_nla_tweakedit_remap(adt, rectf.xmax, NLATIME_CONVERT_UNMAP);
                        }
                        else {
-                               bed.f1= rectf.xmin;
-                               bed.f2= rectf.xmax;
+                               ked.f1= rectf.xmin;
+                               ked.f2= rectf.xmax;
                        }
                }
                
@@ -268,20 +263,10 @@ static void borderselect_action (bAnimContext *ac, rcti rect, short mode, short
                        !((ymax < rectf.ymin) || (ymin > rectf.ymax)) )
                {
                        /* loop over data selecting */
-                       if (ale->key_data) {
-                               if (ale->datatype == ALE_FCURVE)
-                                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
-                       }
-                       else if (ale->type == ANIMTYPE_GROUP) {
-                               bActionGroup *agrp= ale->data;
-                               FCurve *fcu;
-                               
-                               for (fcu= agrp->channels.first; fcu && fcu->grp==agrp; fcu= fcu->next) 
-                                       ANIM_fcurve_keys_bezier_loop(&bed, fcu, ok_cb, select_cb, NULL);
-                       }
-                       //else if (ale->type == ANIMTYPE_GPLAYER) {
+                       //if (ale->type == ANIMTYPE_GPLAYER)
                        //      borderselect_gplayer_frames(ale->data, rectf.xmin, rectf.xmax, selectmode);
-                       //}
+                       //else
+                               ANIM_animchannel_keyframes_loop(&ked, ale, ok_cb, select_cb, NULL, filterflag);
                }
                
                /* set minimum extent to be the maximum of the next channel */
@@ -299,7 +284,7 @@ static int actkeys_borderselect_exec(bContext *C, wmOperator *op)
        bAnimContext ac;
        rcti rect;
        short mode=0, selectmode=0;
-       int event;
+       int gesture_mode;
        
        /* get editor data */
        if (ANIM_animdata_get_context(C, &ac) == 0)
@@ -311,8 +296,8 @@ static int actkeys_borderselect_exec(bContext *C, wmOperator *op)
        rect.xmax= RNA_int_get(op->ptr, "xmax");
        rect.ymax= RNA_int_get(op->ptr, "ymax");
                
-       event= RNA_int_get(op->ptr, "event_type");
-       if (event == LEFTMOUSE) // FIXME... hardcoded
+       gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
+       if (gesture_mode == GESTURE_MODAL_SELECT)
                selectmode = SELECT_ADD;
        else
                selectmode = SELECT_SUBTRACT;
@@ -335,33 +320,33 @@ static int actkeys_borderselect_exec(bContext *C, wmOperator *op)
        /* apply borderselect action */
        borderselect_action(&ac, rect, mode, selectmode);
        
+       /* set notifier that keyframe selection have changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
+       
        return OPERATOR_FINISHED;
 } 
 
-void ACT_OT_keyframes_select_border(wmOperatorType *ot)
+void ACTION_OT_select_border(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Border Select";
-       ot->idname= "ACT_OT_keyframes_select_border";
+       ot->idname= "ACTION_OT_select_border";
+       ot->description= "Select all keyframes within the specified region";
        
        /* api callbacks */
        ot->invoke= WM_border_select_invoke;
        ot->exec= actkeys_borderselect_exec;
        ot->modal= WM_border_select_modal;
        
-       ot->poll= ED_operator_areaactive;
+       ot->poll= ED_operator_action_active;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* rna */
-       RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
-       RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
-       RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
-       RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
-       RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
+       WM_operator_properties_gesture_border(ot, FALSE);
        
-       RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
+       ot->prop= RNA_def_boolean(ot->srna, "axis_range", 0, "Axis Range", "");
 }
 
 /* ******************** Column Select Operator **************************** */
@@ -374,11 +359,11 @@ void ACT_OT_keyframes_select_border(wmOperatorType *ot)
 
 /* defines for column-select mode */
 static EnumPropertyItem prop_column_select_types[] = {
-       {ACTKEYS_COLUMNSEL_KEYS, "KEYS", "On Selected Keyframes", ""},
-       {ACTKEYS_COLUMNSEL_CFRA, "CFRA", "On Current Frame", ""},
-       {ACTKEYS_COLUMNSEL_MARKERS_COLUMN, "MARKERS_COLUMN", "On Selected Markers", ""},
-       {ACTKEYS_COLUMNSEL_MARKERS_BETWEEN, "MARKERS_BETWEEN", "Between Min/Max Selected Markers", ""},
-       {0, NULL, NULL, NULL}
+       {ACTKEYS_COLUMNSEL_KEYS, "KEYS", 0, "On Selected Keyframes", ""},
+       {ACTKEYS_COLUMNSEL_CFRA, "CFRA", 0, "On Current Frame", ""},
+       {ACTKEYS_COLUMNSEL_MARKERS_COLUMN, "MARKERS_COLUMN", 0, "On Selected Markers", ""},
+       {ACTKEYS_COLUMNSEL_MARKERS_BETWEEN, "MARKERS_BETWEEN", 0, "Between Min/Max Selected Markers", ""},
+       {0, NULL, 0, NULL, NULL}
 };
 
 /* ------------------- */ 
@@ -390,8 +375,8 @@ static void markers_selectkeys_between (bAnimContext *ac)
        bAnimListElem *ale;
        int filter;
        
-       BeztEditFunc ok_cb, select_cb;
-       BeztEditData bed;
+       KeyframeEditFunc ok_cb, select_cb;
+       KeyframeEditData ked;
        float min, max;
        
        /* get extreme markers */
@@ -403,9 +388,9 @@ static void markers_selectkeys_between (bAnimContext *ac)
        ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
        select_cb= ANIM_editkeyframes_select(SELECT_ADD);
        
-       memset(&bed, 0, sizeof(BeztEditData));
-       bed.f1= min; 
-       bed.f2= max;
+       memset(&ked, 0, sizeof(KeyframeEditData));
+       ked.f1= min; 
+       ked.f2= max;
        
        /* filter data */
        filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
@@ -413,15 +398,15 @@ static void markers_selectkeys_between (bAnimContext *ac)
        
        /* select keys in-between */
        for (ale= anim_data.first; ale; ale= ale->next) {
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
                
-               if (nob) {      
-                       ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
-                       ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
+               if (adt) {
+                       ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
+                       ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
                }
                else {
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
                }
        }
        
@@ -439,11 +424,11 @@ static void columnselect_action_keys (bAnimContext *ac, short mode)
        
        Scene *scene= ac->scene;
        CfraElem *ce;
-       BeztEditFunc select_cb, ok_cb;
-       BeztEditData bed;
+       KeyframeEditFunc select_cb, ok_cb;
+       KeyframeEditData ked;
        
        /* initialise keyframe editing data */
-       memset(&bed, 0, sizeof(BeztEditData));
+       memset(&ked, 0, sizeof(KeyframeEditData));
        
        /* build list of columns */
        switch (mode) {
@@ -460,7 +445,7 @@ static void columnselect_action_keys (bAnimContext *ac, short mode)
                                ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
                                
                                for (ale= anim_data.first; ale; ale= ale->next)
-                                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, NULL, bezt_to_cfraelem, NULL);
+                                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, NULL, bezt_to_cfraelem, NULL);
                        }
                        BLI_freelistN(&anim_data);
                        break;
@@ -468,13 +453,13 @@ static void columnselect_action_keys (bAnimContext *ac, short mode)
                case ACTKEYS_COLUMNSEL_CFRA: /* current frame */
                        /* make a single CfraElem for storing this */
                        ce= MEM_callocN(sizeof(CfraElem), "cfraElem");
-                       BLI_addtail(&bed.list, ce);
+                       BLI_addtail(&ked.list, ce);
                        
                        ce->cfra= (float)CFRA;
                        break;
                        
                case ACTKEYS_COLUMNSEL_MARKERS_COLUMN: /* list of selected markers */
-                       ED_markers_make_cfra_list(ac->markers, &bed.list, 1);
+                       ED_markers_make_cfra_list(ac->markers, &ked.list, 1);
                        break;
                        
                default: /* invalid option */
@@ -495,20 +480,20 @@ static void columnselect_action_keys (bAnimContext *ac, short mode)
        ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
        
        for (ale= anim_data.first; ale; ale= ale->next) {
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
                
-               /* loop over cfraelems (stored in the BeztEditData->list)
+               /* loop over cfraelems (stored in the KeyframeEditData->list)
                 *      - we need to do this here, as we can apply fewer NLA-mapping conversions
                 */
-               for (ce= bed.list.first; ce; ce= ce->next) {
+               for (ce= ked.list.first; ce; ce= ce->next) {
                        /* set frame for validation callback to refer to */
-                       if (nob)
-                               bed.f1= get_action_frame(nob, ce->cfra);
+                       if (adt)
+                               ked.f1= BKE_nla_tweakedit_remap(adt, ce->cfra, NLATIME_CONVERT_UNMAP);
                        else
-                               bed.f1= ce->cfra;
+                               ked.f1= ce->cfra;
                        
                        /* select elements with frame number matching cfraelem */
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
                        
 #if 0 // XXX reenable when Grease Pencil stuff is back
                        if (ale->type == ANIMTYPE_GPLAYER) {
@@ -526,7 +511,7 @@ static void columnselect_action_keys (bAnimContext *ac, short mode)
        }
        
        /* free elements */
-       BLI_freelistN(&bed.list);
+       BLI_freelistN(&ked.list);
        BLI_freelistN(&anim_data);
 }
 
@@ -549,27 +534,195 @@ static int actkeys_columnselect_exec(bContext *C, wmOperator *op)
        else
                columnselect_action_keys(&ac, mode);
        
-       /* set notifier that things have changed */
-       ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_KEYFRAMES_SELECT);
+       /* set notifier that keyframe selection have changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
        
        return OPERATOR_FINISHED;
 }
  
-void ACT_OT_keyframes_select_column (wmOperatorType *ot)
+void ACTION_OT_select_column (wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Select All";
-       ot->idname= "ACT_OT_keyframes_select_column";
+       ot->idname= "ACTION_OT_select_column";
+       ot->description= "Select all keyframes on the specified frame(s)";
        
        /* api callbacks */
        ot->exec= actkeys_columnselect_exec;
-       ot->poll= ED_operator_areaactive;
+       ot->poll= ED_operator_action_active;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
        
        /* props */
-       RNA_def_enum(ot->srna, "mode", prop_column_select_types, 0, "Mode", "");
+       ot->prop= RNA_def_enum(ot->srna, "mode", prop_column_select_types, 0, "Mode", "");
+}
+
+/* ******************** Select Linked Operator *********************** */
+
+static int actkeys_select_linked_exec (bContext *C, wmOperator *op)
+{
+       bAnimContext ac;
+       
+       ListBase anim_data= {NULL, NULL};
+       bAnimListElem *ale;
+       int filter;
+       
+       KeyframeEditFunc ok_cb = ANIM_editkeyframes_ok(BEZT_OK_SELECTED);
+       KeyframeEditFunc sel_cb = ANIM_editkeyframes_select(SELECT_ADD);
+       
+       /* get editor data */
+       if (ANIM_animdata_get_context(C, &ac) == 0)
+               return OPERATOR_CANCELLED;
+       
+       /* loop through all of the keys and select additional keyframes based on these */
+       filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVEVISIBLE | ANIMFILTER_CURVESONLY);
+       ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
+       
+       for (ale= anim_data.first; ale; ale= ale->next) {
+               FCurve *fcu= (FCurve *)ale->key_data;
+               
+               /* check if anything selected? */
+               if (ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, ok_cb, NULL)) {
+                       /* select every keyframe in this curve then */
+                       ANIM_fcurve_keyframes_loop(NULL, fcu, NULL, sel_cb, NULL);
+               }
+       }
+       
+       /* Cleanup */
+       BLI_freelistN(&anim_data);
+       
+       /* set notifier that keyframe selection has changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
+       
+       return OPERATOR_FINISHED;
+}
+
+void ACTION_OT_select_linked (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Select Linked";
+       ot->idname= "ACTION_OT_select_linked";
+       ot->description = "Select keyframes occurring the same F-Curves as selected ones";
+       
+       /* api callbacks */
+       ot->exec= actkeys_select_linked_exec;
+       ot->poll= ED_operator_action_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
+}
+
+/* ******************** Select More/Less Operators *********************** */
+
+/* Common code to perform selection */
+static void select_moreless_action_keys (bAnimContext *ac, short mode)
+{
+       ListBase anim_data= {NULL, NULL};
+       bAnimListElem *ale;
+       int filter;
+       
+       KeyframeEditData ked;
+       KeyframeEditFunc build_cb;
+       
+       
+       /* init selmap building data */
+       build_cb= ANIM_editkeyframes_buildselmap(mode);
+       memset(&ked, 0, sizeof(KeyframeEditData)); 
+       
+       /* loop through all of the keys and select additional keyframes based on these */
+       filter= (ANIMFILTER_VISIBLE | ANIMFILTER_CURVESONLY);
+       ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
+       
+       for (ale= anim_data.first; ale; ale= ale->next) {
+               FCurve *fcu= (FCurve *)ale->key_data;
+               
+               /* only continue if F-Curve has keyframes */
+               if (fcu->bezt == NULL)
+                       continue;
+               
+               /* build up map of whether F-Curve's keyframes should be selected or not */
+               ked.data= MEM_callocN(fcu->totvert, "selmap actEdit more");
+               ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, build_cb, NULL);
+               
+               /* based on this map, adjust the selection status of the keyframes */
+               ANIM_fcurve_keyframes_loop(&ked, fcu, NULL, bezt_selmap_flush, NULL);
+               
+               /* free the selmap used here */
+               MEM_freeN(ked.data);
+               ked.data= NULL;
+       }
+       
+       /* Cleanup */
+       BLI_freelistN(&anim_data);
+}
+
+/* ----------------- */
+
+static int actkeys_select_more_exec (bContext *C, wmOperator *op)
+{
+       bAnimContext ac;
+       
+       /* get editor data */
+       if (ANIM_animdata_get_context(C, &ac) == 0)
+               return OPERATOR_CANCELLED;
+       
+       /* perform select changes */
+       select_moreless_action_keys(&ac, SELMAP_MORE);
+       
+       /* set notifier that keyframe selection has changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
+       
+       return OPERATOR_FINISHED;
+}
+
+void ACTION_OT_select_more (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Select More";
+       ot->idname= "ACTION_OT_select_more";
+       ot->description = "Select keyframes beside already selected ones";
+       
+       /* api callbacks */
+       ot->exec= actkeys_select_more_exec;
+       ot->poll= ED_operator_action_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
+}
+
+/* ----------------- */
+
+static int actkeys_select_less_exec (bContext *C, wmOperator *op)
+{
+       bAnimContext ac;
+       
+       /* get editor data */
+       if (ANIM_animdata_get_context(C, &ac) == 0)
+               return OPERATOR_CANCELLED;
+       
+       /* perform select changes */
+       select_moreless_action_keys(&ac, SELMAP_LESS);
+       
+       /* set notifier that keyframe selection has changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT, NULL);
+       
+       return OPERATOR_FINISHED;
+}
+
+void ACTION_OT_select_less (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Select Less";
+       ot->idname= "ACTION_OT_select_less";
+       ot->description = "Deselect keyframes on ends of selection islands";
+       
+       /* api callbacks */
+       ot->exec= actkeys_select_less_exec;
+       ot->poll= ED_operator_action_active;
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER/*|OPTYPE_UNDO*/;
 }
 
 /* ******************** Mouse-Click Select Operator *********************** */
@@ -584,11 +737,11 @@ void ACT_OT_keyframes_select_column (wmOperatorType *ot)
 
 /* defines for left-right select tool */
 static EnumPropertyItem prop_actkeys_leftright_select_types[] = {
-       {ACTKEYS_LRSEL_TEST, "CHECK", "Check if Select Left or Right", ""},
-       {ACTKEYS_LRSEL_NONE, "OFF", "Don't select", ""},
-       {ACTKEYS_LRSEL_LEFT, "LEFT", "Before current frame", ""},
-       {ACTKEYS_LRSEL_RIGHT, "RIGHT", "After current frame", ""},
-       {0, NULL, NULL, NULL}
+       {ACTKEYS_LRSEL_TEST, "CHECK", 0, "Check if Select Left or Right", ""},
+       {ACTKEYS_LRSEL_NONE, "OFF", 0, "Don't select", ""},
+       {ACTKEYS_LRSEL_LEFT, "LEFT", 0, "Before current frame", ""},
+       {ACTKEYS_LRSEL_RIGHT, "RIGHT", 0, "After current frame", ""},
+       {0, NULL, 0, NULL, NULL}
 };
 
 /* sensitivity factor for frame-selections */
@@ -602,17 +755,17 @@ static void actkeys_mselect_single (bAnimContext *ac, bAnimListElem *ale, short
        bDopeSheet *ads= (ac->datatype == ANIMCONT_DOPESHEET) ? ac->data : NULL;
        int ds_filter = ((ads) ? (ads->filterflag) : (0));
        
-       BeztEditData bed;
-       BeztEditFunc select_cb, ok_cb;
+       KeyframeEditData ked;
+       KeyframeEditFunc select_cb, ok_cb;
        
        /* get functions for selecting keyframes */
        select_cb= ANIM_editkeyframes_select(select_mode);
        ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAME);
-       memset(&bed, 0, sizeof(BeztEditData)); 
-       bed.f1= selx;
+       memset(&ked, 0, sizeof(KeyframeEditData)); 
+       ked.f1= selx;
        
        /* select the nominated keyframe on the given frame */
-       ANIM_animchannel_keys_bezier_loop(&bed, ale, ok_cb, select_cb, NULL, ds_filter);
+       ANIM_animchannel_keyframes_loop(&ked, ale, ok_cb, select_cb, NULL, ds_filter);
 }
 
 /* Option 2) Selects all the keyframes on either side of the current frame (depends on which side the mouse is on) */
@@ -622,8 +775,8 @@ static void actkeys_mselect_leftright (bAnimContext *ac, short leftright, short
        bAnimListElem *ale;
        int filter;
        
-       BeztEditFunc ok_cb, select_cb;
-       BeztEditData bed;
+       KeyframeEditFunc ok_cb, select_cb;
+       KeyframeEditData ked;
        Scene *scene= ac->scene;
        
        /* if select mode is replace, deselect all keyframes (and channels) first */
@@ -631,7 +784,7 @@ static void actkeys_mselect_leftright (bAnimContext *ac, short leftright, short
                select_mode= SELECT_ADD;
                
                /* deselect all other channels and keyframes */
-               ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
+               ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
                deselect_action_keys(ac, 0, SELECT_SUBTRACT);
        }
        
@@ -639,14 +792,14 @@ static void actkeys_mselect_leftright (bAnimContext *ac, short leftright, short
        ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
        select_cb= ANIM_editkeyframes_select(select_mode);
        
-       memset(&bed, 0, sizeof(BeztEditFunc));
+       memset(&ked, 0, sizeof(KeyframeEditFunc));
        if (leftright == ACTKEYS_LRSEL_LEFT) {
-               bed.f1 = -MAXFRAMEF;
-               bed.f2 = (float)(CFRA + FRAME_CLICK_THRESH);
+               ked.f1 = MINAFRAMEF;
+               ked.f2 = (float)(CFRA + FRAME_CLICK_THRESH);
        } 
        else {
-               bed.f1 = (float)(CFRA - FRAME_CLICK_THRESH);
-               bed.f2 = MAXFRAMEF;
+               ked.f1 = (float)(CFRA - FRAME_CLICK_THRESH);
+               ked.f2 = MAXFRAMEF;
        }
        
        /* filter data */
@@ -658,19 +811,38 @@ static void actkeys_mselect_leftright (bAnimContext *ac, short leftright, short
                
        /* select keys on the side where most data occurs */
        for (ale= anim_data.first; ale; ale= ale->next) {
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
                
-               if (nob) {
-                       ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 0, 1);
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
-                       ANIM_nla_mapping_apply_fcurve(nob, ale->key_data, 1, 1);
+               if (adt) {
+                       ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 0, 1);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
+                       ANIM_nla_mapping_apply_fcurve(adt, ale->key_data, 1, 1);
                }
                //else if (ale->type == ANIMTYPE_GPLAYER)
                //      borderselect_gplayer_frames(ale->data, min, max, SELECT_ADD);
                else
-                       ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
+                       ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
        }
        
+       /* Sync marker support */
+       if((select_mode==SELECT_ADD) && (ac->spacetype==SPACE_ACTION) && ELEM(leftright, ACTKEYS_LRSEL_LEFT, ACTKEYS_LRSEL_RIGHT)) {
+               SpaceAction *saction= ac->sa->spacedata.first;
+               if (saction && saction->flag & SACTION_MARKERS_MOVE) {
+                       TimeMarker *marker;
+
+                       for (marker= scene->markers.first; marker; marker= marker->next) {
+                               if(     ((leftright == ACTKEYS_LRSEL_LEFT) && marker->frame < CFRA) ||
+                                       ((leftright == ACTKEYS_LRSEL_RIGHT) && marker->frame >= CFRA)
+                               ) {
+                                       marker->flag |= SELECT;
+                               }
+                               else {
+                                       marker->flag &= ~SELECT;
+                               }
+                       }
+               }
+       }
+
        /* Cleanup */
        BLI_freelistN(&anim_data);
 }
@@ -682,11 +854,11 @@ static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float se
        bAnimListElem *ale;
        int filter;
        
-       BeztEditFunc select_cb, ok_cb;
-       BeztEditData bed;
+       KeyframeEditFunc select_cb, ok_cb;
+       KeyframeEditData ked;
        
        /* initialise keyframe editing data */
-       memset(&bed, 0, sizeof(BeztEditData));
+       memset(&ked, 0, sizeof(KeyframeEditData));
        
        /* set up BezTriple edit callbacks */
        select_cb= ANIM_editkeyframes_select(select_mode);
@@ -702,16 +874,16 @@ static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float se
        ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
        
        for (ale= anim_data.first; ale; ale= ale->next) {
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
                
                /* set frame for validation callback to refer to */
-               if (nob)
-                       bed.f1= get_action_frame(nob, selx);
+               if (adt)
+                       ked.f1= BKE_nla_tweakedit_remap(adt, selx, NLATIME_CONVERT_UNMAP);
                else
-                       bed.f1= selx;
+                       ked.f1= selx;
                
                /* select elements with frame number matching cfra */
-               ANIM_fcurve_keys_bezier_loop(&bed, ale->key_data, ok_cb, select_cb, NULL);
+               ANIM_fcurve_keyframes_loop(&ked, ale->key_data, ok_cb, select_cb, NULL);
                        
 #if 0 // XXX reenable when Grease Pencil stuff is back
                        if (ale->type == ANIMTYPE_GPLAYER) {
@@ -728,7 +900,7 @@ static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float se
        }
        
        /* free elements */
-       BLI_freelistN(&bed.list);
+       BLI_freelistN(&ked.list);
        BLI_freelistN(&anim_data);
 }
  
@@ -737,17 +909,21 @@ static void actkeys_mselect_column(bAnimContext *ac, short select_mode, float se
 static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode, short column)
 {
        ListBase anim_data = {NULL, NULL};
-       ListBase anim_keys = {NULL, NULL};
+       DLRBT_Tree anim_keys;
        bAnimListElem *ale;
        int filter;
        
        View2D *v2d= &ac->ar->v2d;
+       bDopeSheet *ads = NULL;
        int channel_index;
        short found = 0;
        float selx = 0.0f;
        float x, y;
        rctf rectf;
        
+       /* get dopesheet info */
+       if (ac->datatype == ANIMCONT_DOPESHEET)
+               ads= ac->data;
        
        /* use View2D to determine the index of the channel (i.e a row in the list) where keyframe was */
        UI_view2d_region_to_view(v2d, mval[0], mval[1], &x, &y);
@@ -771,47 +947,48 @@ static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode,
        }
        else {
                /* found match - must return here... */
-               Object *nob= ANIM_nla_mapping_get(ac, ale);
-               ActKeysInc *aki= init_aki_data(ac, ale);
-               ActKeyColumn *ak;
-               float xmin, xmax;
-               
-               /* apply NLA-scaling correction? */
-               if (nob) {
-                       xmin= get_action_frame(nob, rectf.xmin);
-                       xmax= get_action_frame(nob, rectf.xmax);
-               }
-               else {
-                       xmin= rectf.xmin;
-                       xmax= rectf.xmax;
-               }
+               AnimData *adt= ANIM_nla_mapping_get(ac, ale);
+               ActKeyColumn *ak, *akn=NULL;
                
                /* make list of keyframes */
+               // TODO: it would be great if we didn't have to apply this to all the keyframes to do this...
+               BLI_dlrbTree_init(&anim_keys);
+               
                if (ale->key_data) {
                        switch (ale->datatype) {
+                               case ALE_SCE:
+                               {
+                                       Scene *scene= (Scene *)ale->key_data;
+                                       scene_to_keylist(ads, scene, &anim_keys, NULL);
+                               }
+                                       break;
                                case ALE_OB:
                                {
                                        Object *ob= (Object *)ale->key_data;
-                                       ob_to_keylist(ob, &anim_keys, NULL, aki);
+                                       ob_to_keylist(ads, ob, &anim_keys, NULL);
                                }
                                        break;
                                case ALE_ACT:
                                {
                                        bAction *act= (bAction *)ale->key_data;
-                                       action_to_keylist(act, &anim_keys, NULL, aki);
+                                       action_to_keylist(adt, act, &anim_keys, NULL);
                                }
                                        break;
                                case ALE_FCURVE:
                                {
                                        FCurve *fcu= (FCurve *)ale->key_data;
-                                       fcurve_to_keylist(fcu, &anim_keys, NULL, aki);
+                                       fcurve_to_keylist(adt, fcu, &anim_keys, NULL);
                                }
                                        break;
                        }
                }
+               else if (ale->type == ANIMTYPE_SUMMARY) {
+                       /* dopesheet summary covers everything */
+                       summary_to_keylist(ac, &anim_keys, NULL);
+               }
                else if (ale->type == ANIMTYPE_GROUP) {
                        bActionGroup *agrp= (bActionGroup *)ale->data;
-                       agroup_to_keylist(agrp, &anim_keys, NULL, aki);
+                       agroup_to_keylist(adt, agrp, &anim_keys, NULL);
                }
                else if (ale->type == ANIMTYPE_GPDATABLOCK) {
                        /* cleanup */
@@ -821,24 +998,34 @@ static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode,
                }
                else if (ale->type == ANIMTYPE_GPLAYER) {
                        bGPDlayer *gpl= (bGPDlayer *)ale->data;
-                       gpl_to_keylist(gpl, &anim_keys, NULL, aki);
+                       gpl_to_keylist(ads, gpl, &anim_keys, NULL);
                }
                
-               /* loop through keyframes, finding one that was clicked on */
-               for (ak= anim_keys.first; ak; ak= ak->next) {
-                       if (IN_RANGE(ak->cfra, xmin, xmax)) {
-                               selx= ak->cfra;
+               // the call below is not strictly necessary, since we have adjacency info anyway
+               //BLI_dlrbTree_linkedlist_sync(&anim_keys);
+               
+               /* loop through keyframes, finding one that was within the range clicked on */
+               for (ak= anim_keys.root; ak; ak= akn) {
+                       if (IN_RANGE(ak->cfra, rectf.xmin, rectf.xmax)) {
+                               /* set the frame to use, and apply inverse-correction for NLA-mapping 
+                                * so that the frame will get selected by the selection functiosn without
+                                * requiring to map each frame once again...
+                                */
+                               selx= BKE_nla_tweakedit_remap(adt, ak->cfra, NLATIME_CONVERT_UNMAP);
                                found= 1;
                                break;
                        }
+                       else if (ak->cfra < rectf.xmin)
+                               akn= ak->right;
+                       else
+                               akn= ak->left;
                }
                
                /* remove active channel from list of channels for separate treatment (since it's needed later on) */
                BLI_remlink(&anim_data, ale);
                
                /* cleanup temporary lists */
-               BLI_freelistN(&anim_keys);
-               anim_keys.first = anim_keys.last = NULL;
+               BLI_dlrbTree_free(&anim_keys);
                
                /* free list of channels, since it's not used anymore */
                BLI_freelistN(&anim_data);
@@ -855,24 +1042,26 @@ static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode,
                /* highlight channel clicked on */
                if (ELEM(ac->datatype, ANIMCONT_ACTION, ANIMCONT_DOPESHEET)) {
                        /* deselect all other channels first */
-                       ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
+                       ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
                        
                        /* Highlight Action-Group or F-Curve? */
-                       if (ale->type == ANIMTYPE_GROUP) {
-                               bActionGroup *agrp= ale->data;
-                               
-                               agrp->flag |= AGRP_SELECTED;
-                               ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
-                       }       
-                       else if (ale->type == ANIMTYPE_FCURVE) {
-                               FCurve *fcu= ale->data;
-                               
-                               fcu->flag |= FCURVE_SELECTED;
-                               ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
+                       if (ale && ale->data) {
+                               if (ale->type == ANIMTYPE_GROUP) {
+                                       bActionGroup *agrp= ale->data;
+                                       
+                                       agrp->flag |= AGRP_SELECTED;
+                                       ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, agrp, ANIMTYPE_GROUP);
+                               }       
+                               else if (ale->type == ANIMTYPE_FCURVE) {
+                                       FCurve *fcu= ale->data;
+                                       
+                                       fcu->flag |= FCURVE_SELECTED;
+                                       ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, fcu, ANIMTYPE_FCURVE);
+                               }
                        }
                }
                else if (ac->datatype == ANIMCONT_GPENCIL) {
-                       ANIM_deselect_anim_channels(ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
+                       ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
                        
                        /* Highlight gpencil layer */
                        //gpl->flag |= GP_LAYER_SELECT;
@@ -882,18 +1071,20 @@ static void mouse_action_keys (bAnimContext *ac, int mval[2], short select_mode,
        
        /* only select keyframes if we clicked on a valid channel and hit something */
        if (ale) {
-               /* apply selection to keyframes */
-               if (/*gpl*/0) {
-                       /* grease pencil */
-                       //select_gpencil_frame(gpl, (int)selx, selectmode);
-               }
-               else if (column) {
-                       /* select all keyframes in the same frame as the one we hit on the active channel */
-                       actkeys_mselect_column(ac, select_mode, selx);
-               }
-               else {
-                       /* select the nominated keyframe on the given frame */
-                       actkeys_mselect_single(ac, ale, select_mode, selx);
+               if (found) {
+                       /* apply selection to keyframes */
+                       if (/*gpl*/0) {
+                               /* grease pencil */
+                               //select_gpencil_frame(gpl, (int)selx, selectmode);
+                       }
+                       else if (column) {
+                               /* select all keyframes in the same frame as the one we hit on the active channel */
+                               actkeys_mselect_column(ac, select_mode, selx);
+                       }
+                       else {
+                               /* select the nominated keyframe on the given frame */
+                               actkeys_mselect_single(ac, ale, select_mode, selx);
+                       }
                }
                
                /* free this channel */
@@ -951,22 +1142,23 @@ static int actkeys_clickselect_invoke(bContext *C, wmOperator *op, wmEvent *even
                mouse_action_keys(&ac, mval, selectmode, column);
        }
        
-       /* set notifier that things have changed */
-       ANIM_animdata_send_notifiers(C, &ac, ANIM_CHANGED_BOTH);
+       /* set notifier that keyframe selection (and channels too) have changed */
+       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_SELECT|ND_ANIMCHAN_SELECT, NULL);
        
        /* for tweak grab to work */
        return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
 }
  
-void ACT_OT_keyframes_clickselect (wmOperatorType *ot)
+void ACTION_OT_clickselect (wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Mouse Select Keys";
-       ot->idname= "ACT_OT_keyframes_clickselect";
+       ot->idname= "ACTION_OT_clickselect";
+       ot->description= "Select keyframes by clicking on them";
        
        /* api callbacks - absolutely no exec() this yet... */
        ot->invoke= actkeys_clickselect_invoke;
-       ot->poll= ED_operator_areaactive;
+       ot->poll= ED_operator_action_active;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;