Sculpt:
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
index c1fbb67..f0ebb21 100644 (file)
 
 #include "MEM_guardedalloc.h"
 
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_editVert.h"
 #include "BLI_rand.h"
+#include "BLI_linklist.h"
 
 #include "BKE_action.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_object.h"
 #include "BKE_global.h"
+#include "BKE_paint.h"
 #include "BKE_scene.h"
 #include "BKE_screen.h"
 #include "BKE_utildefines.h"
@@ -118,36 +120,32 @@ void view3d_get_view_aligned_coordinate(ViewContext *vc, float *fp, short mval[2
        
        if(mval[0]!=IS_CLIPPED) {
                window_to_3d_delta(vc->ar, dvec, mval[0]-mx, mval[1]-my);
-               VecSubf(fp, fp, dvec);
+               sub_v3_v3v3(fp, fp, dvec);
        }
 }
 
-void view3d_get_transformation(ViewContext *vc, Object *ob, bglMats *mats)
+void view3d_get_transformation(ARegion *ar, RegionView3D *rv3d, Object *ob, bglMats *mats)
 {
        float cpy[4][4];
        int i, j;
 
-       Mat4MulMat4(cpy, ob->obmat, vc->rv3d->viewmat);
+       mul_m4_m4m4(cpy, ob->obmat, rv3d->viewmat);
 
        for(i = 0; i < 4; ++i) {
                for(j = 0; j < 4; ++j) {
-                       mats->projection[i*4+j] = vc->rv3d->winmat[i][j];
+                       mats->projection[i*4+j] = rv3d->winmat[i][j];
                        mats->modelview[i*4+j] = cpy[i][j];
                }
        }
 
-       mats->viewport[0] = vc->ar->winrct.xmin;
-       mats->viewport[1] = vc->ar->winrct.ymin;
-       mats->viewport[2] = vc->ar->winx;
-       mats->viewport[3] = vc->ar->winy;       
+       mats->viewport[0] = ar->winrct.xmin;
+       mats->viewport[1] = ar->winrct.ymin;
+       mats->viewport[2] = ar->winx;
+       mats->viewport[3] = ar->winy;   
 }
 
 /* ********************** view3d_select: selection manipulations ********************* */
 
-/* XXX to solve *************** */
-static void BIF_undo_push() {}
-/* XXX end ********************* */
-
 /* local prototypes */
 
 void EM_backbuf_checkAndSelectVerts(EditMesh *em, int select)
@@ -328,9 +326,9 @@ int lasso_inside_edge(short mcords[][2], short moves, int x0, int y0, int x1, in
        
        /* no points in lasso, so we have to intersect with lasso edge */
        
-       if( IsectLL2Ds(mcords[0], mcords[moves-1], v1, v2) > 0) return 1;
+       if( isect_line_line_v2_short(mcords[0], mcords[moves-1], v1, v2) > 0) return 1;
        for(a=0; a<moves-1; a++) {
-               if( IsectLL2Ds(mcords[a], mcords[a+1], v1, v2) > 0) return 1;
+               if( isect_line_line_v2_short(mcords[a], mcords[a+1], v1, v2) > 0) return 1;
        }
        
        return 0;
@@ -351,15 +349,22 @@ static void do_lasso_select_pose(ViewContext *vc, short mcords[][2], short moves
        
        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                VECCOPY(vec, pchan->pose_head);
-               Mat4MulVecfl(ob->obmat, vec);
+               mul_m4_v3(ob->obmat, vec);
                project_short(vc->ar, vec, sco1);
                VECCOPY(vec, pchan->pose_tail);
-               Mat4MulVecfl(ob->obmat, vec);
+               mul_m4_v3(ob->obmat, vec);
                project_short(vc->ar, vec, sco2);
                
                if(lasso_inside_edge(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1])) {
                        if(select) pchan->bone->flag |= BONE_SELECTED;
-                       else pchan->bone->flag &= ~(BONE_ACTIVE|BONE_SELECTED);
+                       else pchan->bone->flag &= ~BONE_SELECTED;
+               }
+       }
+       
+       {
+               bArmature *arm= ob->data;
+               if(arm->act_bone && (arm->act_bone->flag & BONE_SELECTED)==0) {
+                       arm->act_bone= NULL;
                }
        }
 }
@@ -378,7 +383,7 @@ static void do_lasso_select_objects(ViewContext *vc, short mcords[][2], short mo
                                else ED_base_object_select(base, BA_DESELECT);
                                base->object->flag= base->flag;
                        }
-                       if(base->object->flag & OB_POSEMODE) {
+                       if(base->object->mode & OB_MODE_POSE) {
                                do_lasso_select_pose(vc, mcords, moves, select);
                        }
                }
@@ -457,6 +462,7 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
        data.pass = 0;
 
        bbsel= EM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        
        if(ts->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
@@ -548,13 +554,15 @@ static void do_lasso_select_mesh_uv(short mcords[][2], short moves, short select
 
 static void do_lasso_select_curve__doSelect(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
 {
-       struct { short (*mcords)[2]; short moves; short select; } *data = userData;
-
+       struct { ViewContext vc; short (*mcords)[2]; short moves; short select; } *data = userData;
+       
        if (lasso_inside(data->mcords, data->moves, x, y)) {
                if (bp) {
                        bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
                } else {
-                       if (G.f & G_HIDDENHANDLES) {
+                       Curve *cu= data->vc.obedit->data;
+                       
+                       if (cu->drawflag & CU_HIDE_HANDLES) {
                                /* can only be beztindex==0 here since handles are hidden */
                                bezt->f1 = bezt->f2 = bezt->f3 = data->select?(bezt->f2|SELECT):(bezt->f2&~SELECT);
                        } else {
@@ -572,13 +580,15 @@ static void do_lasso_select_curve__doSelect(void *userData, Nurb *nu, BPoint *bp
 
 static void do_lasso_select_curve(ViewContext *vc, short mcords[][2], short moves, short select)
 {
-       struct { short (*mcords)[2]; short moves; short select; } data;
+       struct { ViewContext vc; short (*mcords)[2]; short moves; short select; } data;
 
        /* set vc->editnurb */
+       data.vc = *vc;
        data.mcords = mcords;
        data.moves = moves;
        data.select = select;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        nurbs_foreachScreenVert(vc, do_lasso_select_curve__doSelect, &data);
 }
 
@@ -599,6 +609,7 @@ static void do_lasso_select_lattice(ViewContext *vc, short mcords[][2], short mo
        data.moves = moves;
        data.select = select;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        lattice_foreachScreenVert(vc, do_lasso_select_lattice__doSelect, &data);
 }
 
@@ -614,10 +625,10 @@ static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short m
        for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
 
                VECCOPY(vec, ebone->head);
-               Mat4MulVecfl(vc->obedit->obmat, vec);
+               mul_m4_v3(vc->obedit->obmat, vec);
                project_short(vc->ar, vec, sco1);
                VECCOPY(vec, ebone->tail);
-               Mat4MulVecfl(vc->obedit->obmat, vec);
+               mul_m4_v3(vc->obedit->obmat, vec);
                project_short(vc->ar, vec, sco2);
                
                didpoint= 0;
@@ -634,9 +645,11 @@ static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short m
                /* if one of points selected, we skip the bone itself */
                if(didpoint==0 && lasso_inside_edge(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1])) {
                        if(select) ebone->flag |= BONE_TIPSEL|BONE_ROOTSEL|BONE_SELECTED;
-                       else ebone->flag &= ~(BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
+                       else ebone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
                }
        }
+
+       ED_armature_validate_active(arm);
 }
 
 static void do_lasso_select_facemode(ViewContext *vc, short mcords[][2], short moves, short select)
@@ -693,27 +706,30 @@ static void do_lasso_select_node(short mcords[][2], short moves, short select)
 
 void view3d_lasso_select(bContext *C, ViewContext *vc, short mcords[][2], short moves, short select)
 {
-       if(vc->obedit==NULL) {
-               if(FACESEL_PAINT_TEST)
+       Object *ob = CTX_data_active_object(C);
+
+       if(vc->obedit==NULL) { /* Object Mode */
+               if(paint_facesel_test(ob))
                        do_lasso_select_facemode(vc, mcords, moves, select);
-               else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))
+               else if(ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT))
                        ;
-               else if(G.f & G_PARTICLEEDIT)
+               else if(ob && ob->mode & OB_MODE_PARTICLE_EDIT)
                        PE_lasso_select(C, mcords, moves, select);
                else  
                        do_lasso_select_objects(vc, mcords, moves, select);
        }
-       else if(vc->obedit->type==OB_MESH) {
-               do_lasso_select_mesh(vc, mcords, moves, select);
-       } else if(vc->obedit->type==OB_CURVE || vc->obedit->type==OB_SURF) 
-               do_lasso_select_curve(vc, mcords, moves, select);
-       else if(vc->obedit->type==OB_LATTICE) 
-               do_lasso_select_lattice(vc, mcords, moves, select);
-       else if(vc->obedit->type==OB_ARMATURE)
-               do_lasso_select_armature(vc, mcords, moves, select);
-
-       BIF_undo_push("Lasso select");
-       
+       else { /* Edit Mode */
+               if(vc->obedit->type==OB_MESH)
+                       do_lasso_select_mesh(vc, mcords, moves, select);
+               else if(vc->obedit->type==OB_CURVE || vc->obedit->type==OB_SURF) 
+                       do_lasso_select_curve(vc, mcords, moves, select);
+               else if(vc->obedit->type==OB_LATTICE) 
+                       do_lasso_select_lattice(vc, mcords, moves, select);
+               else if(vc->obedit->type==OB_ARMATURE)
+                       do_lasso_select_armature(vc, mcords, moves, select);
+       
+               WM_event_add_notifier(C, NC_GEOM|ND_SELECT, vc->obedit->data);
+       }
 }
 
 
@@ -753,6 +769,7 @@ static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
 void VIEW3D_OT_select_lasso(wmOperatorType *ot)
 {
        ot->name= "Lasso Select";
+       ot->description= "Select items using lasso selection.";
        ot->idname= "VIEW3D_OT_select_lasso";
        
        ot->invoke= WM_gesture_lasso_invoke;
@@ -838,56 +855,87 @@ static void deselectall_except(Scene *scene, Base *b)   /* deselect all except b
        }
 }
 
-static Base *mouse_select_menu(ViewContext *vc, unsigned int *buffer, int hits, short *mval)
+static Base *mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffer, int hits, short *mval, short extend)
 {
-       Scene *scene= vc->scene;
-       View3D *v3d= vc->v3d;
-       Base *baseList[SEL_MENU_SIZE]={NULL}; /*baseList is used to store all possible bases to bring up a menu */
-       Base *base;
        short baseCount = 0;
-       char menuText[20 + SEL_MENU_SIZE*32] = "Select Object%t";       /* max ob name = 22 */
-       char str[32];
-       
-       for(base=FIRSTBASE; base; base= base->next) {
-               if (BASE_SELECTABLE(v3d, base)) {
-                       baseList[baseCount] = NULL;
-                       
-                       /* two selection methods, the CTRL select uses max dist of 15 */
-                       if(buffer) {
-                               int a;
-                               for(a=0; a<hits; a++) {
-                                       /* index was converted */
-                                       if(base->selcol==buffer[ (4 * a) + 3 ]) baseList[baseCount] = base;
-                               }
-                       }
-                       else {
-                               int temp, dist=15;
-                               
-                               project_short(vc->ar, base->object->obmat[3], &base->sx);
-                               
-                               temp= abs(base->sx -mval[0]) + abs(base->sy -mval[1]);
-                               if(temp<dist ) baseList[baseCount] = base;
+       short ok;
+       LinkNode *linklist= NULL;
+       
+       CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
+               ok= FALSE;
+
+               /* two selection methods, the CTRL select uses max dist of 15 */
+               if(buffer) {
+                       int a;
+                       for(a=0; a<hits; a++) {
+                               /* index was converted */
+                               if(base->selcol==buffer[ (4 * a) + 3 ])
+                                       ok= TRUE;
                        }
+               }
+               else {
+                       int temp, dist=15;
+
+                       project_short(vc->ar, base->object->obmat[3], &base->sx);
                        
-                       if(baseList[baseCount]) {
-                               if (baseCount < SEL_MENU_SIZE) {
-                                       baseList[baseCount] = base;
-                                       sprintf(str, "|%s %%x%d", base->object->id.name+2, baseCount+1);        /* max ob name == 22 */
-                                                       strcat(menuText, str);
-                                                       baseCount++;
-                               }
-                       }
+                       temp= abs(base->sx -mval[0]) + abs(base->sy -mval[1]);
+                       if(temp < dist)
+                               ok= TRUE;
+               }
+
+               if(ok) {
+                       baseCount++;
+                       BLI_linklist_prepend(&linklist, base);
+
+                       if (baseCount==SEL_MENU_SIZE)
+                               break;
                }
        }
+       CTX_DATA_END;
 
-       if(baseCount<=1) return baseList[0];
+       if(baseCount)
+
+
+       if(baseCount==0) {
+               return NULL;
+       }
+       if(baseCount == 1) {
+               Base *base= (Base *)linklist->link;
+               BLI_linklist_free(linklist, NULL);
+               return base;
+       }
        else {
-               baseCount = -1; // XXX = pupmenu(menuText);
-               
-               if (baseCount != -1) { /* If nothing is selected then dont do anything */
-                       return baseList[baseCount-1];
+               /* UI */
+               uiPopupMenu *pup= uiPupMenuBegin(C, "Select Object", 0);
+               uiLayout *layout= uiPupMenuLayout(pup);
+               uiLayout *split= uiLayoutSplit(layout, 0);
+               uiLayout *column= uiLayoutColumn(split, 0);
+               LinkNode *node;
+
+               node= linklist;
+               while(node) {
+                       Base *base=node->link;
+                       Object *ob= base->object;
+                       char *name= ob->id.name+2;
+                       /* annoying!, since we need to set 2 props cant use this. */
+                       /* uiItemStringO(column, name, 0, "OBJECT_OT_select_name", "name", name); */
+
+                       {
+                               PointerRNA ptr;
+
+                               WM_operator_properties_create(&ptr, "OBJECT_OT_select_name");
+                               RNA_string_set(&ptr, "name", name);
+                               RNA_boolean_set(&ptr, "extend", extend);
+                               uiItemFullO(column, name, uiIconFromID((ID *)ob), "OBJECT_OT_select_name", ptr.data, WM_OP_EXEC_DEFAULT, 0);
+                       }
+
+                       node= node->next;
                }
-               else return NULL;
+
+               uiPupMenuEnd(C, pup);
+
+               BLI_linklist_free(linklist, NULL);
+               return NULL;
        }
 }
 
@@ -951,15 +999,15 @@ static short mixed_bones_object_selectbuffer(ViewContext *vc, unsigned int *buff
 
 
 /* mval is region coords */
-static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
+static int mouse_select(bContext *C, short *mval, short extend, short obcenter, short enumerate)
 {
        ViewContext vc;
        ARegion *ar= CTX_wm_region(C);
        View3D *v3d= CTX_wm_view3d(C);
        Scene *scene= CTX_data_scene(C);
        Base *base, *startbase=NULL, *basact=NULL, *oldbasact=NULL;
-       unsigned int buffer[4*MAXPICKBUF];
        int temp, a, dist=100;
+       int retval = 0;
        short hits;
        
        /* setup view context for argument to callbacks */
@@ -974,10 +1022,9 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
        if(vc.obedit==NULL && obcenter) {
                
                /* note; shift+alt goes to group-flush-selecting */
-               /* XXX solve */
-               if(0) 
-                       basact= mouse_select_menu(&vc, NULL, 0, mval);
-               else {
+               if(enumerate) {
+                       basact= mouse_select_menu(C, &vc, NULL, 0, mval, extend);
+               } else {
                        base= startbase;
                        while(base) {
                                if (BASE_SELECTABLE(v3d, base)) {
@@ -999,6 +1046,8 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                }
        }
        else {
+               unsigned int buffer[4*MAXPICKBUF];
+
                /* if objects have posemode set, the bones are in the same selection buffer */
                
                hits= mixed_bones_object_selectbuffer(&vc, buffer, mval);
@@ -1009,9 +1058,9 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                        for(a=0; a<hits; a++) if(buffer[4*a+3] & 0xFFFF0000) has_bones= 1;
 
                        /* note; shift+alt goes to group-flush-selecting */
-                       if(has_bones==0 && 0) 
-                               basact= mouse_select_menu(&vc, buffer, hits, mval);
-                       else {
+                       if(has_bones==0 && enumerate) {
+                               basact= mouse_select_menu(C, &vc, buffer, hits, mval, extend);
+                       else {
                                static short lastmval[2]={-100, -100};
                                int donearest= 0;
                                
@@ -1105,11 +1154,12 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                                        basact->flag|= SELECT;
                                        basact->object->flag= basact->flag;
                                        
+                                       retval = 1;
                                        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, basact->object);
                                        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, basact->object);
                                        
                                        /* in weightpaint, we use selected bone to select vertexgroup, so no switch to new active object */
-                                       if(G.f & G_WEIGHTPAINT) {
+                                       if(BASACT && BASACT->object->mode & OB_MODE_WEIGHT_PAINT) {
                                                /* prevent activating */
                                                basact= NULL;
                                        }
@@ -1124,6 +1174,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
        
        /* so, do we have something selected? */
        if(basact) {
+               retval = 1;
                
                if(vc.obedit) {
                        /* only do select */
@@ -1157,6 +1208,8 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                        WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
                }
        }
+
+       return retval;
 }
 
 /* ********************  border and circle ************************************** */
@@ -1179,7 +1232,7 @@ int edge_inside_circle(short centx, short centy, short rad, short x1, short y1,
        v2[0]= x2;
        v2[1]= y2;
        
-       if( PdistVL2Dfl(v3, v1, v2) < (float)rad ) return 1;
+       if( dist_to_line_segment_v2(v3, v1, v2) < (float)rad ) return 1;
        
        return 0;
 }
@@ -1192,7 +1245,9 @@ static void do_nurbs_box_select__doSelect(void *userData, Nurb *nu, BPoint *bp,
                if (bp) {
                        bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
                } else {
-                       if (G.f & G_HIDDENHANDLES) {
+                       Curve *cu= data->vc.obedit->data;
+                       
+                       if (cu->drawflag & CU_HIDE_HANDLES) {
                                /* can only be beztindex==0 here since handles are hidden */
                                bezt->f1 = bezt->f2 = bezt->f3 = data->select?(bezt->f2|SELECT):(bezt->f2&~SELECT);
                        } else {
@@ -1211,10 +1266,11 @@ static void do_nurbs_box_select(ViewContext *vc, rcti *rect, int select)
 {
        struct { ViewContext vc; rcti *rect; int select; } data;
        
-       data.vc= *vc;
+       data.vc = *vc;
        data.rect = rect;
        data.select = select;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        nurbs_foreachScreenVert(vc, do_nurbs_box_select__doSelect, &data);
 }
 
@@ -1234,6 +1290,7 @@ static void do_lattice_box_select(ViewContext *vc, rcti *rect, int select)
        data.rect = rect;
        data.select = select;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        lattice_foreachScreenVert(vc, do_lattice_box_select__doSelect, &data);
 }
 
@@ -1283,6 +1340,7 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
        data.done = 0;
 
        bbsel= EM_init_backbuf_border(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
 
        if(ts->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
@@ -1323,43 +1381,44 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        ScrArea *sa= CTX_wm_area(C);
        View3D *v3d= sa->spacedata.first;
        Object *obedit= CTX_data_edit_object(C);
+       Object *obact= CTX_data_active_object(C);
        rcti rect;
        Base *base;
        MetaElem *ml;
        unsigned int buffer[4*MAXPICKBUF];
        int a, index;
-       short hits, val;
+       short hits, selecting;
 
        view3d_operator_needs_opengl(C);
        
        /* setup view context for argument to callbacks */
        view3d_set_viewcontext(C, &vc);
        
-       val= RNA_int_get(op->ptr, "event_type");
+       selecting= (RNA_int_get(op->ptr, "gesture_mode")==GESTURE_MODAL_SELECT);
        rect.xmin= RNA_int_get(op->ptr, "xmin");
        rect.ymin= RNA_int_get(op->ptr, "ymin");
        rect.xmax= RNA_int_get(op->ptr, "xmax");
        rect.ymax= RNA_int_get(op->ptr, "ymax");
        
-       if(obedit==NULL && (FACESEL_PAINT_TEST)) {
-// XXX         face_borderselect();
+       if(obedit==NULL && (paint_facesel_test(OBACT))) {
+               face_borderselect(C, obact, &rect, selecting);
                return OPERATOR_FINISHED;
        }
-       else if(obedit==NULL && (G.f & G_PARTICLEEDIT)) {
-               return PE_border_select(C, &rect, (val==LEFTMOUSE));
+       else if(obedit==NULL && (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
+               return PE_border_select(C, &rect, selecting);
        }
        
        if(obedit) {
                if(obedit->type==OB_MESH) {
                        Mesh *me= obedit->data;
                        vc.em= me->edit_mesh;
-                       do_mesh_box_select(&vc, &rect, (val==LEFTMOUSE));
+                       do_mesh_box_select(&vc, &rect, selecting);
 //                     if (EM_texFaceCheck())
-                       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
+                       WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
                        
                }
                else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
-                       do_nurbs_box_select(&vc, &rect, val==LEFTMOUSE);
+                       do_nurbs_box_select(&vc, &rect, selecting);
                }
                else if(obedit->type==OB_MBALL) {
                        MetaBall *mb = (MetaBall*)obedit->data;
@@ -1371,14 +1430,14 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                for(a=0; a<hits; a++) {
                                        if(ml->selcol1==buffer[ (4 * a) + 3 ]) {
                                                ml->flag |= MB_SCALE_RAD;
-                                               if(val==LEFTMOUSE) ml->flag |= SELECT;
-                                               else ml->flag &= ~SELECT;
+                                               if(selecting)   ml->flag |= SELECT;
+                                               else                    ml->flag &= ~SELECT;
                                                break;
                                        }
                                        if(ml->selcol2==buffer[ (4 * a) + 3 ]) {
                                                ml->flag &= ~MB_SCALE_RAD;
-                                               if(val==LEFTMOUSE) ml->flag |= SELECT;
-                                               else ml->flag &= ~SELECT;
+                                               if(selecting)   ml->flag |= SELECT;
+                                               else                    ml->flag &= ~SELECT;
                                                break;
                                        }
                                }
@@ -1402,14 +1461,14 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                        ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
                                        if (index & BONESEL_TIP) {
                                                ebone->flag |= BONE_DONE;
-                                               if (val==LEFTMOUSE) ebone->flag |= BONE_TIPSEL;
-                                               else ebone->flag &= ~BONE_TIPSEL;
+                                               if (selecting)  ebone->flag |= BONE_TIPSEL;
+                                               else                    ebone->flag &= ~BONE_TIPSEL;
                                        }
                                        
                                        if (index & BONESEL_ROOT) {
                                                ebone->flag |= BONE_DONE;
-                                               if (val==LEFTMOUSE) ebone->flag |= BONE_ROOTSEL;
-                                               else ebone->flag &= ~BONE_ROOTSEL;
+                                               if (selecting)  ebone->flag |= BONE_ROOTSEL;
+                                               else                    ebone->flag &= ~BONE_ROOTSEL;
                                        }
                                }
                        }
@@ -1429,7 +1488,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                        ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
                                        if (index & BONESEL_BONE) {
                                                if(!(ebone->flag & BONE_DONE)) {
-                                                       if (val==LEFTMOUSE)
+                                                       if (selecting)
                                                                ebone->flag |= (BONE_ROOTSEL|BONE_TIPSEL|BONE_SELECTED);
                                                        else
                                                                ebone->flag &= ~(BONE_ROOTSEL|BONE_TIPSEL|BONE_SELECTED);
@@ -1441,7 +1500,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        ED_armature_sync_selection(arm->edbo);
                }
                else if(obedit->type==OB_LATTICE) {
-                       do_lattice_box_select(&vc, &rect, val==LEFTMOUSE);
+                       do_lattice_box_select(&vc, &rect, selecting);
                }
        }
        else {  /* no editmode, unified for bones and objects */
@@ -1449,18 +1508,14 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                Object *ob= OBACT;
                unsigned int *vbuffer=NULL; /* selection buffer */
                unsigned int *col;                      /* color in buffer      */
-               short selecting = 0;
                int bone_only;
                int totobj= MAXPICKBUF; // XXX solve later
                
-               if((ob) && (ob->flag & OB_POSEMODE))
+               if((ob) && (ob->mode & OB_MODE_POSE))
                        bone_only= 1;
                else
                        bone_only= 0;
                
-               if (val==LEFTMOUSE)
-                       selecting = 1;
-               
                /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
                vbuffer = MEM_mallocN(4 * (totobj+MAXPICKBUF) * sizeof(unsigned int), "selection buffer");
                hits= view3d_opengl_select(&vc, vbuffer, 4*(totobj+MAXPICKBUF), &rect);
@@ -1493,8 +1548,12 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
 // XXX                                                                 select_actionchannel_by_name(base->object->action, bone->name, 1);
                                                                }
                                                                else {
-                                                                       bone->flag &= ~(BONE_ACTIVE|BONE_SELECTED);
+                                                                       bArmature *arm= base->object->data;
+                                                                       bone->flag &= ~BONE_SELECTED;
 // XXX                                                                 select_actionchannel_by_name(base->object->action, bone->name, 0);
+                                                                       if(arm->act_bone==bone)
+                                                                               arm->act_bone= NULL;
+                                                                       
                                                                }
                                                        }
                                                }
@@ -1530,6 +1589,7 @@ void VIEW3D_OT_select_border(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Border Select";
+       ot->description= "Select items using border selection.";
        ot->idname= "VIEW3D_OT_select_border";
        
        /* api callbacks */
@@ -1543,13 +1603,7 @@ void VIEW3D_OT_select_border(wmOperatorType *ot)
        ot->flag= 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);
-
-       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everyting first.");
+       WM_operator_properties_gesture_border(ot, TRUE);
 }
 
 /* ****** Mouse Select ****** */
@@ -1558,36 +1612,48 @@ void VIEW3D_OT_select_border(wmOperatorType *ot)
 static int view3d_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        Object *obedit= CTX_data_edit_object(C);
+       Object *obact= CTX_data_active_object(C);
        short extend= RNA_boolean_get(op->ptr, "extend");
+       short center= RNA_boolean_get(op->ptr, "center");
+       short enumerate= RNA_boolean_get(op->ptr, "enumerate");
+       int     retval = 0;
 
        view3d_operator_needs_opengl(C);
        
        if(obedit) {
                if(obedit->type==OB_MESH)
-                       mouse_mesh(C, event->mval, extend);
+                       retval = mouse_mesh(C, event->mval, extend);
                else if(obedit->type==OB_ARMATURE)
-                       mouse_armature(C, event->mval, extend);
+                       retval = mouse_armature(C, event->mval, extend);
                else if(obedit->type==OB_LATTICE)
-                       mouse_lattice(C, event->mval, extend);
+                       retval = mouse_lattice(C, event->mval, extend);
                else if(ELEM(obedit->type, OB_CURVE, OB_SURF))
-                       mouse_nurb(C, event->mval, extend);
+                       retval = mouse_nurb(C, event->mval, extend);
                else if(obedit->type==OB_MBALL)
-                       mouse_mball(C, event->mval, extend);
+                       retval = mouse_mball(C, event->mval, extend);
                        
        }
-       else if(G.f & G_PARTICLEEDIT)
-               PE_mouse_particles(C, event->mval, extend);
-       else 
-               mouse_select(C, event->mval, extend, 0);
-
-       /* allowing tweaks */
-       return OPERATOR_PASS_THROUGH|OPERATOR_FINISHED;
+       else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
+               return PE_mouse_particles(C, event->mval, extend);
+       else if(obact && paint_facesel_test(obact))
+               retval = face_select(C, obact, event->mval, extend);
+       else
+               retval = mouse_select(C, event->mval, extend, center, enumerate);
+
+       /* passthrough allows tweaks
+        * FINISHED to signal one operator worked
+        * */
+       if (retval)
+               return OPERATOR_PASS_THROUGH|OPERATOR_FINISHED;
+       else
+               return OPERATOR_PASS_THROUGH; /* nothing selected, just passthrough */
 }
 
 void VIEW3D_OT_select(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Activate/Select";
+       ot->description= "Activate/select item(s).";
        ot->idname= "VIEW3D_OT_select";
        
        /* api callbacks */
@@ -1598,7 +1664,9 @@ void VIEW3D_OT_select(wmOperatorType *ot)
        ot->flag= OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everyting first.");
+       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first.");
+       RNA_def_boolean(ot->srna, "center", 0, "Center", "Use the object center when selecting (object mode only).");
+       RNA_def_boolean(ot->srna, "enumerate", 0, "Enumerate", "List objects under the mouse (object mode only).");
 }
 
 
@@ -1637,9 +1705,9 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
 {
        ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
+       Object *ob= vc->obact;
        
-       if(vc->obedit==NULL && (FACESEL_PAINT_TEST)) {
-               Object *ob= vc->obact;
+       if(vc->obedit==NULL && paint_facesel_test(ob)) {
                Mesh *me = ob?ob->data:NULL;
 
                if (me) {
@@ -1656,8 +1724,11 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                struct {ViewContext *vc; short select, mval[2]; float radius; } data;
                
                bbsel= EM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
+               ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
+
                vc->em= ((Mesh *)vc->obedit->data)->edit_mesh;
 
+               data.vc = vc;
                data.select = selecting;
                data.mval[0] = mval[0];
                data.mval[1] = mval[1];
@@ -1724,6 +1795,7 @@ static void nurbscurve_circle_select(ViewContext *vc, int selecting, short *mval
        data.mval[1] = mval[1];
        data.radius = rad;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        nurbs_foreachScreenVert(vc, nurbscurve_circle_doSelect, &data);
 }
 
@@ -1749,9 +1821,85 @@ static void lattice_circle_select(ViewContext *vc, int selecting, short *mval, f
        data.mval[1] = mval[1];
        data.radius = rad;
 
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        lattice_foreachScreenVert(vc, latticecurve_circle_doSelect, &data);
 }
 
+
+static short armature_circle_doSelectJoint(void *userData, EditBone *ebone, int x, int y, short head)
+{
+       struct {ViewContext *vc; short select, mval[2]; float radius; } *data = userData;
+       int mx = x - data->mval[0], my = y - data->mval[1];
+       float r = sqrt(mx*mx + my*my);
+       
+       if (r <= data->radius) {
+               if (head) {
+                       if (data->select)
+                               ebone->flag |= BONE_ROOTSEL;
+                       else 
+                               ebone->flag &= ~BONE_ROOTSEL;
+               }
+               else {
+                       if (data->select)
+                               ebone->flag |= BONE_TIPSEL;
+                       else 
+                               ebone->flag &= ~BONE_TIPSEL;
+               }
+               return 1;
+       }
+       return 0;
+}
+static void armature_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
+{
+       struct {ViewContext *vc; short select, mval[2]; float radius; } data;
+       bArmature *arm= vc->obedit->data;
+       EditBone *ebone;
+       
+       /* set vc->edit data */
+       data.select = selecting;
+       data.mval[0] = mval[0];
+       data.mval[1] = mval[1];
+       data.radius = rad;
+
+       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
+       
+       /* check each EditBone... */
+       // TODO: could be optimised at some point
+       for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
+               short sco1[2], sco2[2], didpoint=0;
+               float vec[3];
+               
+               /* project head location to screenspace */
+               VECCOPY(vec, ebone->head);
+               mul_m4_v3(vc->obedit->obmat, vec);
+               project_short(vc->ar, vec, sco1);
+               
+               /* project tail location to screenspace */
+               VECCOPY(vec, ebone->tail);
+               mul_m4_v3(vc->obedit->obmat, vec);
+               project_short(vc->ar, vec, sco2);
+               
+               /* check if the head and/or tail is in the circle 
+                *      - the call to check also does the selection already
+                */
+               if (armature_circle_doSelectJoint(&data, ebone, sco1[0], sco1[1], 1))
+                       didpoint= 1;
+               if (armature_circle_doSelectJoint(&data, ebone, sco2[0], sco2[1], 0))
+                       didpoint= 1;
+                       
+               /* only if the endpoints didn't get selected, deal with the middle of the bone too */
+               // XXX should we just do this always?
+               if ( (didpoint==0) && edge_inside_circle(mval[0], mval[1], rad, sco1[0], sco1[1], sco2[0], sco2[1]) ) {
+                       if (selecting) 
+                               ebone->flag |= BONE_TIPSEL|BONE_ROOTSEL|BONE_SELECTED;
+                       else 
+                               ebone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
+               }
+       }
+
+       ED_armature_validate_active(arm);
+}
+
 /** Callbacks for circle selection in Editmode */
 
 static void obedit_circle_select(ViewContext *vc, short selecting, short *mval, float rad) 
@@ -1767,6 +1915,9 @@ static void obedit_circle_select(ViewContext *vc, short selecting, short *mval,
        case OB_LATTICE:
                lattice_circle_select(vc, selecting, mval, rad);
                break;
+       case OB_ARMATURE:
+               armature_circle_select(vc, selecting, mval, rad);
+               break;
        default:
                return;
        }
@@ -1778,30 +1929,36 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
        ScrArea *sa= CTX_wm_area(C);
        ARegion *ar= CTX_wm_region(C);
        Scene *scene= CTX_data_scene(C);
+       Object *obact= CTX_data_active_object(C);
        View3D *v3d= sa->spacedata.first;
        int x= RNA_int_get(op->ptr, "x");
        int y= RNA_int_get(op->ptr, "y");
        int radius= RNA_int_get(op->ptr, "radius");
+    int gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
+    int selecting;
        
-       if(CTX_data_edit_object(C) || (G.f & G_PARTICLEEDIT)) {
+    selecting= (gesture_mode==GESTURE_MODAL_SELECT);
+    
+       if(CTX_data_edit_object(C) || (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
                ViewContext vc;
-               short mval[2], selecting;
+               short mval[2];
                
                view3d_operator_needs_opengl(C);
                
                view3d_set_viewcontext(C, &vc);
                mval[0]= x;
                mval[1]= y;
-               selecting= LEFTMOUSE==RNA_int_get(op->ptr, "event_type"); // XXX solve
 
-               if(CTX_data_edit_object(C))
+               if(CTX_data_edit_object(C)) {
                        obedit_circle_select(&vc, selecting, mval, (float)radius);
+                       WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obact->data);
+               }
                else
                        return PE_circle_select(C, selecting, mval, (float)radius);
        }
        else {
                Base *base;
-               
+               selecting= selecting?BA_SELECT:BA_DESELECT;
                for(base= FIRSTBASE; base; base= base->next) {
                        if(base->lay & v3d->lay) {
                                project_short(ar, base->object->obmat[3], &base->sx);
@@ -1809,7 +1966,7 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
                                        int dx= base->sx-x;
                                        int dy= base->sy-y;
                                        if( dx*dx + dy*dy < radius*radius)
-                                               ED_base_object_select(base, BA_SELECT);
+                                               ED_base_object_select(base, selecting);
                                }
                        }
                }
@@ -1823,6 +1980,7 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
 void VIEW3D_OT_select_circle(wmOperatorType *ot)
 {
        ot->name= "Circle Select";
+       ot->description= "Select items using circle selection.";
        ot->idname= "VIEW3D_OT_select_circle";
        
        ot->invoke= WM_gesture_circle_invoke;
@@ -1836,5 +1994,5 @@ void VIEW3D_OT_select_circle(wmOperatorType *ot)
        RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
        RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
        RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
-       RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
 }