merge with trunk at r31523
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
index 508bf56cc718fb5407703b8f2163b56e29042e63..776048c8f74d39e6715f67d57de71eac30d4c429 100644 (file)
@@ -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.
  * All rights reserved.
 #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"
+#include "BKE_tessmesh.h"
 
 #include "RE_pipeline.h"       // make_stars
 
 #include "ED_particle.h"
 #include "ED_mesh.h"
 #include "ED_object.h"
-#include "ED_retopo.h"
 #include "ED_screen.h"
 #include "ED_types.h"
 #include "ED_util.h"
+#include "ED_retopo.h"
 #include "ED_mball.h"
 
 #include "UI_interface.h"
 
 #include "view3d_intern.h"     // own include
 
-
+// TODO: should return whether there is valid context to continue
 void view3d_set_viewcontext(bContext *C, ViewContext *vc)
 {
        memset(vc, 0, sizeof(ViewContext));
        vc->ar= CTX_wm_region(C);
        vc->scene= CTX_data_scene(C);
        vc->v3d= CTX_wm_view3d(C);
-       vc->rv3d= vc->ar->regiondata;
+       vc->rv3d= CTX_wm_region_view3d(C);
        vc->obact= CTX_data_active_object(C);
        vc->obedit= CTX_data_edit_object(C); 
 }
@@ -124,80 +118,93 @@ void view3d_get_view_aligned_coordinate(ViewContext *vc, float *fp, short mval[2
        }
 }
 
-void view3d_get_transformation(ViewContext *vc, Object *ob, bglMats *mats)
+/*
+ * ob == NULL if you want global matrices
+ * */
+void view3d_get_transformation(ARegion *ar, RegionView3D *rv3d, Object *ob, bglMats *mats)
 {
        float cpy[4][4];
        int i, j;
 
-       mul_m4_m4m4(cpy, ob->obmat, vc->rv3d->viewmat);
+       if (ob) {
+               mul_m4_m4m4(cpy, ob->obmat, rv3d->viewmat);
+       } else {
+               copy_m4_m4(cpy, 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 ********************* */
 
 /* local prototypes */
 
-void EM_backbuf_checkAndSelectVerts(EditMesh *em, int select)
+void EDBM_backbuf_checkAndSelectVerts(BMEditMesh *em, int select)
 {
-       EditVert *eve;
-       int index= em_wireoffs;
-
-       for(eve= em->verts.first; eve; eve= eve->next, index++) {
-               if(eve->h==0) {
-                       if(EM_check_backbuf(index)) {
-                               eve->f = select?(eve->f|1):(eve->f&~1);
+       BMVert *eve;
+       BMIter iter;
+       int index= bm_wireoffs;
+
+       eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
+       for ( ; eve; eve=BMIter_Step(&iter), index++) {
+               if(!BM_TestHFlag(eve, BM_HIDDEN)) {
+                       if(EDBM_check_backbuf(index)) {
+                               BM_Select_Vert(em->bm, eve, select);
                        }
                }
        }
 }
 
-void EM_backbuf_checkAndSelectEdges(EditMesh *em, int select)
+void EDBM_backbuf_checkAndSelectEdges(BMEditMesh *em, int select)
 {
-       EditEdge *eed;
-       int index= em_solidoffs;
-
-       for(eed= em->edges.first; eed; eed= eed->next, index++) {
-               if(eed->h==0) {
-                       if(EM_check_backbuf(index)) {
-                               EM_select_edge(eed, select);
+       BMEdge *eed;
+       BMIter iter;
+       int index= bm_solidoffs;
+
+       eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
+       for ( ; eed; eed=BMIter_Step(&iter), index++) {
+               if(!BM_TestHFlag(eed, BM_HIDDEN)) {
+                       if(EDBM_check_backbuf(index)) {
+                               BM_Select_Edge(em->bm, eed, select);
                        }
                }
        }
 }
 
-void EM_backbuf_checkAndSelectFaces(EditMesh *em, int select)
+void EDBM_backbuf_checkAndSelectFaces(BMEditMesh *em, int select)
 {
-       EditFace *efa;
+       BMFace *efa;
+       BMIter iter;
        int index= 1;
 
-       for(efa= em->faces.first; efa; efa= efa->next, index++) {
-               if(efa->h==0) {
-                       if(EM_check_backbuf(index)) {
-                               EM_select_face_fgon(em, efa, select);
+       efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
+       for ( ; efa; efa=BMIter_Step(&iter), index++) {
+               if(!BM_TestHFlag(efa, BM_HIDDEN)) {
+                       if(EDBM_check_backbuf(index)) {
+                               BM_Select_Face(em->bm, efa, select);
                        }
                }
        }
 }
 
-void EM_backbuf_checkAndSelectTFaces(Mesh *me, int select)
+void EDBM_backbuf_checkAndSelectTFaces(Mesh *me, int select)
 {
        MFace *mface = me->mface;
        int a;
 
        if (mface) {
                for(a=1; a<=me->totface; a++, mface++) {
-                       if(EM_check_backbuf(a)) {
+                       if(EDBM_check_backbuf(a)) {
                                mface->flag = select?(mface->flag|ME_FACE_SEL):(mface->flag&~ME_FACE_SEL);
                        }
                }
@@ -338,9 +345,8 @@ int lasso_inside_edge(short mcords[][2], short moves, int x0, int y0, int x1, in
 /* warning; lasso select with backbuffer-check draws in backbuf with persp(PERSP_WIN) 
    and returns with persp(PERSP_VIEW). After lasso select backbuf is not OK
 */
-static void do_lasso_select_pose(ViewContext *vc, short mcords[][2], short moves, short select)
+static void do_lasso_select_pose(ViewContext *vc, Object *ob, short mcords[][2], short moves, short select)
 {
-       Object *ob= vc->obact;
        bPoseChannel *pchan;
        float vec[3];
        short sco1[2], sco2[2];
@@ -348,11 +354,9 @@ static void do_lasso_select_pose(ViewContext *vc, short mcords[][2], short moves
        if(ob->type!=OB_ARMATURE || ob->pose==NULL) return;
        
        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
-               VECCOPY(vec, pchan->pose_head);
-               mul_m4_v3(ob->obmat, vec);
+               mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
                project_short(vc->ar, vec, sco1);
-               VECCOPY(vec, pchan->pose_tail);
-               mul_m4_v3(ob->obmat, vec);
+               mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
                project_short(vc->ar, vec, sco2);
                
                if(lasso_inside_edge(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1])) {
@@ -375,7 +379,7 @@ static void do_lasso_select_objects(ViewContext *vc, short mcords[][2], short mo
        Base *base;
        
        for(base= vc->scene->base.first; base; base= base->next) {
-               if(base->lay & vc->v3d->lay) {
+               if(BASE_SELECTABLE(vc->v3d, base)) { /* use this to avoid un-needed lasso lookups */
                        project_short(vc->ar, base->object->obmat[3], &base->sx);
                        if(lasso_inside(mcords, moves, base->sx, base->sy)) {
                                
@@ -384,7 +388,7 @@ static void do_lasso_select_objects(ViewContext *vc, short mcords[][2], short mo
                                base->object->flag= base->flag;
                        }
                        if(base->object->mode & OB_MODE_POSE) {
-                               do_lasso_select_pose(vc, mcords, moves, select);
+                               do_lasso_select_pose(vc, base->object, mcords, moves, select);
                        }
                }
        }
@@ -405,53 +409,52 @@ void lasso_select_boundbox(rcti *rect, short mcords[][2], short moves)
        }
 }
 
-static void do_lasso_select_mesh__doSelectVert(void *userData, EditVert *eve, int x, int y, int index)
+static void do_lasso_select_mesh__doSelectVert(void *userData, BMVert *eve, int x, int y, int index)
 {
        struct { ViewContext vc; rcti *rect; short (*mcords)[2], moves, select, pass, done; } *data = userData;
 
        if (BLI_in_rcti(data->rect, x, y) && lasso_inside(data->mcords, data->moves, x, y)) {
-               eve->f = data->select?(eve->f|1):(eve->f&~1);
+               BM_Select_Vert(data->vc.em->bm, eve, data->select);
        }
 }
-static void do_lasso_select_mesh__doSelectEdge(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index)
+static void do_lasso_select_mesh__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
 {
        struct { ViewContext vc; rcti *rect; short (*mcords)[2], moves, select, pass, done; } *data = userData;
 
-       if (EM_check_backbuf(em_solidoffs+index)) {
+       if (EDBM_check_backbuf(bm_solidoffs+index)) {
                if (data->pass==0) {
                        if (    edge_fully_inside_rect(data->rect, x0, y0, x1, y1)  &&
                                        lasso_inside(data->mcords, data->moves, x0, y0) &&
                                        lasso_inside(data->mcords, data->moves, x1, y1)) {
-                               EM_select_edge(eed, data->select);
+                               BM_Select_Edge(data->vc.em->bm, eed, data->select);
                                data->done = 1;
                        }
                } else {
                        if (lasso_inside_edge(data->mcords, data->moves, x0, y0, x1, y1)) {
-                               EM_select_edge(eed, data->select);
+                               BM_Select_Edge(data->vc.em->bm, eed, data->select);
                        }
                }
        }
 }
-static void do_lasso_select_mesh__doSelectFace(void *userData, EditFace *efa, int x, int y, int index)
+static void do_lasso_select_mesh__doSelectFace(void *userData, BMFace *efa, int x, int y, int index)
 {
        struct { ViewContext vc; rcti *rect; short (*mcords)[2], moves, select, pass, done; } *data = userData;
 
        if (BLI_in_rcti(data->rect, x, y) && lasso_inside(data->mcords, data->moves, x, y)) {
-               EM_select_face_fgon(data->vc.em, efa, data->select);
+               BM_Select_Face(data->vc.em->bm, efa, data->select);
        }
 }
 
 static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves, short select)
 {
        struct { ViewContext vc; rcti *rect; short (*mcords)[2], moves, select, pass, done; } data;
-       ToolSettings *ts= vc->scene->toolsettings;
        rcti rect;
        int bbsel;
        
        lasso_select_boundbox(&rect, mcords, moves);
        
        /* set editmesh */
-       vc->em= ((Mesh *)vc->obedit->data)->edit_mesh;
+       vc->em= ((Mesh *)vc->obedit->data)->edit_btmesh;
 
        data.vc= *vc;
        data.rect = &rect;
@@ -461,18 +464,26 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
        data.done = 0;
        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 */
+       /* workaround: init mats first, EDBM_mask_init_backbuf_border can change
+          view matrix to pixel space, breaking edge select with backbuf. fixes bug #20936 */
+
+       /* [#21018] breaks zbuf select. run below. only if bbsel fails */
+       /* ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d) */
+
+       glLoadMatrixf(vc->rv3d->viewmat);
+       bbsel= EDBM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
        
-       if(ts->selectmode & SCE_SELECT_VERTEX) {
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
-                       EM_backbuf_checkAndSelectVerts(vc->em, select);
+                       EDBM_backbuf_checkAndSelectVerts(vc->em, select);
                } else {
+                       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
                        mesh_foreachScreenVert(vc, do_lasso_select_mesh__doSelectVert, &data, 1);
                }
        }
-       if(ts->selectmode & SCE_SELECT_EDGE) {
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_EDGE) {
                        /* Does both bbsel and non-bbsel versions (need screen cos for both) */
+               ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
 
                data.pass = 0;
                mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge, &data, 0);
@@ -483,16 +494,17 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
                }
        }
        
-       if(ts->selectmode & SCE_SELECT_FACE) {
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_FACE) {
                if (bbsel) {
-                       EM_backbuf_checkAndSelectFaces(vc->em, select);
+                       EDBM_backbuf_checkAndSelectFaces(vc->em, select);
                } else {
+                       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
                        mesh_foreachScreenFace(vc, do_lasso_select_mesh__doSelectFace, &data);
                }
        }
        
-       EM_free_backbuf();
-       EM_selectmode_flush(vc->em);    
+       EDBM_free_backbuf();
+       EDBM_selectmode_flush(vc->em);  
 }
 
 #if 0
@@ -624,11 +636,9 @@ 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);
-               mul_m4_v3(vc->obedit->obmat, vec);
+               mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
                project_short(vc->ar, vec, sco1);
-               VECCOPY(vec, ebone->tail);
-               mul_m4_v3(vc->obedit->obmat, vec);
+               mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
                project_short(vc->ar, vec, sco2);
                
                didpoint= 0;
@@ -648,7 +658,7 @@ static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short m
                        else ebone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
                }
        }
-
+       ED_armature_sync_selection(arm->edbo);
        ED_armature_validate_active(arm);
 }
 
@@ -661,14 +671,14 @@ static void do_lasso_select_facemode(ViewContext *vc, short mcords[][2], short m
        if(me==NULL || me->mtface==NULL) return;
        if(me->totface==0) return;
        
-       em_vertoffs= me->totface+1;     /* max index array */
+       bm_vertoffs= me->totface+1;     /* max index array */
        
        lasso_select_boundbox(&rect, mcords, moves);
-       EM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       EDBM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
        
-       EM_backbuf_checkAndSelectTFaces(me, select);
+       EDBM_backbuf_checkAndSelectTFaces(me, select);
        
-       EM_free_backbuf();
+       EDBM_free_backbuf();
        
 // XXX object_tface_flags_changed(ob, 0);
 }
@@ -720,18 +730,24 @@ void view3d_lasso_select(bContext *C, ViewContext *vc, short mcords[][2], short
        }
        else { /* Edit Mode */
                if(vc->obedit->type==OB_MESH)
-                       do_lasso_select_mesh(vc, mcords, moves, select);
+               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);
+               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);
        }
 }
 
+static EnumPropertyItem lasso_select_types[] = {
+       {0, "SELECT", 0, "Select", ""},
+       {1, "DESELECT", 0, "Deselect", ""},
+       {0, NULL, 0, NULL, NULL}
+};
+
 
 /* lasso operator gives properties, but since old code works
    with short array we convert */
@@ -758,7 +774,7 @@ static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
                /* setup view context for argument to callbacks */
                view3d_set_viewcontext(C, &vc);
                
-               select= !RNA_boolean_get(op->ptr, "deselect");
+               select= RNA_enum_is_equal(C, op->ptr, "type", "SELECT");
                view3d_lasso_select(C, &vc, mcords, i, select);
                
                return OPERATOR_FINISHED;
@@ -769,7 +785,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->description= "Select items using lasso selection";
        ot->idname= "VIEW3D_OT_select_lasso";
        
        ot->invoke= WM_gesture_lasso_invoke;
@@ -781,7 +797,7 @@ void VIEW3D_OT_select_lasso(wmOperatorType *ot)
        ot->flag= OPTYPE_UNDO;
        
        RNA_def_collection_runtime(ot->srna, "path", &RNA_OperatorMousePath, "Path", "");
-       RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items.");
+       RNA_def_enum(ot->srna, "type", lasso_select_types, 0, "Type", "");
 }
 
 
@@ -908,7 +924,7 @@ static Base *mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffe
                /* UI */
                uiPopupMenu *pup= uiPupMenuBegin(C, "Select Object", 0);
                uiLayout *layout= uiPupMenuLayout(pup);
-               uiLayout *split= uiLayoutSplit(layout, 0);
+               uiLayout *split= uiLayoutSplit(layout, 0, 0);
                uiLayout *column= uiLayoutColumn(split, 0);
                LinkNode *node;
 
@@ -997,9 +1013,126 @@ static short mixed_bones_object_selectbuffer(ViewContext *vc, unsigned int *buff
        return 0;
 }
 
+/* returns basact */
+static Base *mouse_select_eval_buffer(ViewContext *vc, unsigned int *buffer, int hits, short *mval, Base *startbase, int has_bones)
+{
+       Scene *scene= vc->scene;
+       View3D *v3d= vc->v3d;
+       Base *base, *basact= NULL;
+       static short lastmval[2]={-100, -100};
+       int a, donearest= 0;
+       
+       /* define if we use solid nearest select or not */
+       if(v3d->drawtype>OB_WIRE) {
+               donearest= 1;
+               if( ABS(mval[0]-lastmval[0])<3 && ABS(mval[1]-lastmval[1])<3) {
+                       if(!has_bones)  /* hrms, if theres bones we always do nearest */
+                               donearest= 0;
+               }
+       }
+       lastmval[0]= mval[0]; lastmval[1]= mval[1];
+       
+       if(donearest) {
+               unsigned int min= 0xFFFFFFFF;
+               int selcol= 0, notcol=0;
+               
+               
+               if(has_bones) {
+                       /* we skip non-bone hits */
+                       for(a=0; a<hits; a++) {
+                               if( min > buffer[4*a+1] && (buffer[4*a+3] & 0xFFFF0000) ) {
+                                       min= buffer[4*a+1];
+                                       selcol= buffer[4*a+3] & 0xFFFF;
+                               }
+                       }
+               }
+               else {
+                       /* only exclude active object when it is selected... */
+                       if(BASACT && (BASACT->flag & SELECT) && hits>1) notcol= BASACT->selcol; 
+                       
+                       for(a=0; a<hits; a++) {
+                               if( min > buffer[4*a+1] && notcol!=(buffer[4*a+3] & 0xFFFF)) {
+                                       min= buffer[4*a+1];
+                                       selcol= buffer[4*a+3] & 0xFFFF;
+                               }
+                       }
+               }
+               
+               base= FIRSTBASE;
+               while(base) {
+                       if(base->lay & v3d->lay) {
+                               if(base->selcol==selcol) break;
+                       }
+                       base= base->next;
+               }
+               if(base) basact= base;
+       }
+       else {
+               
+               base= startbase;
+               while(base) {
+                       /* skip objects with select restriction, to prevent prematurely ending this loop
+                       * with an un-selectable choice */
+                       if (base->object->restrictflag & OB_RESTRICT_SELECT) {
+                               base=base->next;
+                               if(base==NULL) base= FIRSTBASE;
+                               if(base==startbase) break;
+                       }
+                       
+                       if(base->lay & v3d->lay) {
+                               for(a=0; a<hits; a++) {
+                                       if(has_bones) {
+                                               /* skip non-bone objects */
+                                               if((buffer[4*a+3] & 0xFFFF0000)) {
+                                                       if(base->selcol== (buffer[(4*a)+3] & 0xFFFF))
+                                                               basact= base;
+                                               }
+                                       }
+                                       else {
+                                               if(base->selcol== (buffer[(4*a)+3] & 0xFFFF))
+                                                       basact= base;
+                                       }
+                               }
+                       }
+                       
+                       if(basact) break;
+                       
+                       base= base->next;
+                       if(base==NULL) base= FIRSTBASE;
+                       if(base==startbase) break;
+               }
+       }
+       
+       return basact;
+}
+
+/* mval comes from event->mval, only use within region handlers */
+Base *ED_view3d_give_base_under_cursor(bContext *C, short *mval)
+{
+       ViewContext vc;
+       Base *basact= NULL;
+       unsigned int buffer[4*MAXPICKBUF];
+       int hits;
+       
+       /* setup view context for argument to callbacks */
+       view3d_operator_needs_opengl(C);
+       view3d_set_viewcontext(C, &vc);
+       
+       hits= mixed_bones_object_selectbuffer(&vc, buffer, mval);
+       
+       if(hits>0) {
+               int a, has_bones= 0;
+               
+               for(a=0; a<hits; a++) if(buffer[4*a+3] & 0xFFFF0000) has_bones= 1;
+               
+               basact= mouse_select_eval_buffer(&vc, buffer, hits, mval, vc.scene->base.first, has_bones);
+       }
+       
+       return basact;
+}
 
 /* mval is region coords */
-static void mouse_select(bContext *C, short *mval, short extend, short obcenter, short enumerate)
+static int mouse_select(bContext *C, short *mval, short extend, short obcenter, short enumerate)
 {
        ViewContext vc;
        ARegion *ar= CTX_wm_region(C);
@@ -1007,6 +1140,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter,
        Scene *scene= CTX_data_scene(C);
        Base *base, *startbase=NULL, *basact=NULL, *oldbasact=NULL;
        int temp, a, dist=100;
+       int retval = 0;
        short hits;
        
        /* setup view context for argument to callbacks */
@@ -1060,89 +1194,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter,
                        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;
-                               
-                               /* define if we use solid nearest select or not */
-                               if(v3d->drawtype>OB_WIRE) {
-                                       donearest= 1;
-                                       if( ABS(mval[0]-lastmval[0])<3 && ABS(mval[1]-lastmval[1])<3) {
-                                               if(!has_bones)  /* hrms, if theres bones we always do nearest */
-                                                       donearest= 0;
-                                       }
-                               }
-                               lastmval[0]= mval[0]; lastmval[1]= mval[1];
-                               
-                               if(donearest) {
-                                       unsigned int min= 0xFFFFFFFF;
-                                       int selcol= 0, notcol=0;
-                                       
-
-                                       if(has_bones) {
-                                               /* we skip non-bone hits */
-                                               for(a=0; a<hits; a++) {
-                                                       if( min > buffer[4*a+1] && (buffer[4*a+3] & 0xFFFF0000) ) {
-                                                               min= buffer[4*a+1];
-                                                               selcol= buffer[4*a+3] & 0xFFFF;
-                                                       }
-                                               }
-                                       }
-                                       else {
-                                               /* only exclude active object when it is selected... */
-                                               if(BASACT && (BASACT->flag & SELECT) && hits>1) notcol= BASACT->selcol; 
-                                       
-                                               for(a=0; a<hits; a++) {
-                                                       if( min > buffer[4*a+1] && notcol!=(buffer[4*a+3] & 0xFFFF)) {
-                                                               min= buffer[4*a+1];
-                                                               selcol= buffer[4*a+3] & 0xFFFF;
-                                                       }
-                                               }
-                                       }
-
-                                       base= FIRSTBASE;
-                                       while(base) {
-                                               if(base->lay & v3d->lay) {
-                                                       if(base->selcol==selcol) break;
-                                               }
-                                               base= base->next;
-                                       }
-                                       if(base) basact= base;
-                               }
-                               else {
-                                       
-                                       base= startbase;
-                                       while(base) {
-                                               /* skip objects with select restriction, to prevent prematurely ending this loop
-                                                * with an un-selectable choice */
-                                               if (base->object->restrictflag & OB_RESTRICT_SELECT) {
-                                                       base=base->next;
-                                                       if(base==NULL) base= FIRSTBASE;
-                                                       if(base==startbase) break;
-                                               }
-                                       
-                                               if(base->lay & v3d->lay) {
-                                                       for(a=0; a<hits; a++) {
-                                                               if(has_bones) {
-                                                                       /* skip non-bone objects */
-                                                                       if((buffer[4*a+3] & 0xFFFF0000)) {
-                                                                               if(base->selcol== (buffer[(4*a)+3] & 0xFFFF))
-                                                                                       basact= base;
-                                                                       }
-                                                               }
-                                                               else {
-                                                                       if(base->selcol== (buffer[(4*a)+3] & 0xFFFF))
-                                                                               basact= base;
-                                                               }
-                                                       }
-                                               }
-                                               
-                                               if(basact) break;
-                                               
-                                               base= base->next;
-                                               if(base==NULL) base= FIRSTBASE;
-                                               if(base==startbase) break;
-                                       }
-                               }
+                               basact= mouse_select_eval_buffer(&vc, buffer, hits, mval, startbase, has_bones);
                        }
                        
                        if(has_bones && basact) {
@@ -1153,6 +1205,7 @@ 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);
                                        
@@ -1172,6 +1225,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 */
@@ -1205,6 +1259,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 ************************************** */
@@ -1257,7 +1313,7 @@ static void do_nurbs_box_select__doSelect(void *userData, Nurb *nu, BPoint *bp,
                }
        }
 }
-static void do_nurbs_box_select(ViewContext *vc, rcti *rect, int select)
+static void do_nurbs_box_select(ViewContext *vc, rcti *rect, int select, int extend)
 {
        struct { ViewContext vc; rcti *rect; int select; } data;
        
@@ -1265,6 +1321,10 @@ static void do_nurbs_box_select(ViewContext *vc, rcti *rect, int select)
        data.rect = rect;
        data.select = select;
 
+       if (extend == 0 && select) {
+               CU_deselect_all(vc->obedit);
+       }
+
        ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        nurbs_foreachScreenVert(vc, do_nurbs_box_select__doSelect, &data);
 }
@@ -1277,7 +1337,7 @@ static void do_lattice_box_select__doSelect(void *userData, BPoint *bp, int x, i
                bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
        }
 }
-static void do_lattice_box_select(ViewContext *vc, rcti *rect, int select)
+static void do_lattice_box_select(ViewContext *vc, rcti *rect, int select, int extend)
 {
        struct { ViewContext vc; rcti *rect; int select, pass, done; } data;
 
@@ -1285,47 +1345,55 @@ static void do_lattice_box_select(ViewContext *vc, rcti *rect, int select)
        data.rect = rect;
        data.select = select;
 
+       if (extend == 0 && select) {
+               ED_setflagsLatt(vc->obedit, 0);
+       }
+
        ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        lattice_foreachScreenVert(vc, do_lattice_box_select__doSelect, &data);
 }
 
-static void do_mesh_box_select__doSelectVert(void *userData, EditVert *eve, int x, int y, int index)
+static void do_mesh_box_select__doSelectVert(void *userData, BMVert *eve, int x, int y, int index)
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } *data = userData;
 
        if (BLI_in_rcti(data->rect, x, y)) {
-               eve->f = data->select?(eve->f|1):(eve->f&~1);
+               BM_Select_Vert(data->vc.em->bm, eve, data->select);
        }
 }
-static void do_mesh_box_select__doSelectEdge(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index)
+static void do_mesh_box_select__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } *data = userData;
 
-       if(EM_check_backbuf(em_solidoffs+index)) {
+       if(EDBM_check_backbuf(bm_solidoffs+index)) {
                if (data->pass==0) {
                        if (edge_fully_inside_rect(data->rect, x0, y0, x1, y1)) {
-                               EM_select_edge(eed, data->select);
+                               BM_Select_Edge(data->vc.em->bm, eed, data->select);
                                data->done = 1;
                        }
                } else {
                        if (edge_inside_rect(data->rect, x0, y0, x1, y1)) {
-                               EM_select_edge(eed, data->select);
+                               BM_Select_Edge(data->vc.em->bm, eed, data->select);
                        }
                }
        }
 }
-static void do_mesh_box_select__doSelectFace(void *userData, EditFace *efa, int x, int y, int index)
+
+static void 
+do_mesh_box_select__doSelectFace(void *userData, BMFace
+                               *efa, int x, int y,
+                               int index)
+
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } *data = userData;
 
        if (BLI_in_rcti(data->rect, x, y)) {
-               EM_select_face_fgon(data->vc.em, efa, data->select);
+               BM_Select_Face(data->vc.em->bm, efa, data->select);
        }
 }
-static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
+static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int extend)
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } data;
-       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
        
        data.vc= *vc;
@@ -1334,17 +1402,30 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
        data.pass = 0;
        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 (extend == 0 && select)
+       {
+               EDBM_clear_flag_all(vc->em, BM_SELECT);
+       }
+
+       /* workaround: init mats first, EM_mask_init_backbuf_border can change
+          view matrix to pixel space, breaking edge select with backbuf. fixes bug #20936 */
+       /*ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);*/ /* for foreach's screen/vert projection */
+
+       /* [#21018] breaks zbuf select. run below. only if bbsel fails */
+       /* ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d) */
 
-       if(ts->selectmode & SCE_SELECT_VERTEX) {
+       glLoadMatrixf(vc->rv3d->viewmat);
+       bbsel= EDBM_init_backbuf_border(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
+
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
-                       EM_backbuf_checkAndSelectVerts(vc->em, select);
+                       EDBM_backbuf_checkAndSelectVerts(vc->em, select);
                } else {
+                       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
                        mesh_foreachScreenVert(vc, do_mesh_box_select__doSelectVert, &data, 1);
                }
        }
-       if(ts->selectmode & SCE_SELECT_EDGE) {
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_EDGE) {
                        /* Does both bbsel and non-bbsel versions (need screen cos for both) */
 
                data.pass = 0;
@@ -1356,17 +1437,18 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
                }
        }
        
-       if(ts->selectmode & SCE_SELECT_FACE) {
+       if(vc->scene->toolsettings->selectmode & SCE_SELECT_FACE) {
                if(bbsel) {
-                       EM_backbuf_checkAndSelectFaces(vc->em, select);
+                       EDBM_backbuf_checkAndSelectFaces(vc->em, select);
                } else {
+                       ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
                        mesh_foreachScreenFace(vc, do_mesh_box_select__doSelectFace, &data);
                }
        }
        
-       EM_free_backbuf();
+       EDBM_free_backbuf();
                
-       EM_selectmode_flush(vc->em);
+       EDBM_selectmode_flush(vc->em);
 }
 
 static int view3d_borderselect_exec(bContext *C, wmOperator *op)
@@ -1382,6 +1464,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        MetaElem *ml;
        unsigned int buffer[4*MAXPICKBUF];
        int a, index;
+       int extend;
        short hits, selecting;
 
        view3d_operator_needs_opengl(C);
@@ -1394,31 +1477,43 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        rect.ymin= RNA_int_get(op->ptr, "ymin");
        rect.xmax= RNA_int_get(op->ptr, "xmax");
        rect.ymax= RNA_int_get(op->ptr, "ymax");
-       
+       extend = RNA_boolean_get(op->ptr, "extend");
+
        if(obedit==NULL && (paint_facesel_test(OBACT))) {
-               face_borderselect(C, obact, &rect, selecting);
+               face_borderselect(C, obact, &rect, selecting, extend);
                return OPERATOR_FINISHED;
        }
        else if(obedit==NULL && (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
-               return PE_border_select(C, &rect, selecting);
+               return PE_border_select(C, &rect, selecting, extend);
        }
+       else if(obedit==NULL && (obact && obact->mode & OB_MODE_SCULPT))
+               return OPERATOR_CANCELLED;
        
        if(obedit) {
                if(obedit->type==OB_MESH) {
                        Mesh *me= obedit->data;
-                       vc.em= me->edit_mesh;
-                       do_mesh_box_select(&vc, &rect, selecting);
-//                     if (EM_texFaceCheck())
+                       vc.em= me->edit_btmesh;
+                       do_mesh_box_select(&vc, &rect, selecting, extend);
+//                     if (EDBM_texFaceCheck())
                        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, selecting);
+                       do_nurbs_box_select(&vc, &rect, selecting, extend);
                }
                else if(obedit->type==OB_MBALL) {
                        MetaBall *mb = (MetaBall*)obedit->data;
                        hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
                        
+                       if (extend == 0 && selecting) {
+                               ml= mb->editelems->first;
+
+                               while(ml) {
+                                       ml->flag &= ~SELECT;
+                                       ml= ml->next;
+                               }
+                       }
+
                        ml= mb->editelems->first;
                        
                        while(ml) {
@@ -1447,6 +1542,17 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        for(ebone= arm->edbo->first; ebone; ebone= ebone->next)
                                ebone->flag &= ~BONE_DONE;
                        
+                       if (extend==0 && selecting) {
+                               /*      Set the flags */
+                               CTX_DATA_BEGIN(C, EditBone *, ebone, visible_bones) {
+                                       /* ignore bone if selection can't change */
+                                       if ((ebone->flag & BONE_UNSELECTABLE) == 0) {
+                                               ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
+                                       }
+                               }
+                               CTX_DATA_END;
+                       }
+
                        hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
                        
                        /* first we only check points inside the border */
@@ -1492,10 +1598,9 @@ 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, selecting);
+                       do_lattice_box_select(&vc, &rect, selecting, extend);
                }
        }
        else {  /* no editmode, unified for bones and objects */
@@ -1511,6 +1616,25 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                else
                        bone_only= 0;
                
+               if (extend == 0 && selecting) {
+                       base= FIRSTBASE;
+
+                       if (bone_only) {
+                               CTX_DATA_BEGIN(C, bPoseChannel *, pchan, visible_pose_bones) {
+                                       pchan->bone->flag &= ~(BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
+                               }
+                               CTX_DATA_END;
+                       } else {
+                               while(base) {
+                                       Base *next = base->next;
+                                       if(base->lay & v3d->lay) {
+                                               ED_base_object_select(base, BA_DESELECT);
+                                       }
+                                       base= next;
+                               }
+                       }
+               }
+
                /* 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);
@@ -1578,13 +1702,18 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
 
 
 /* *****************Selection Operators******************* */
+static EnumPropertyItem prop_select_types[] = {
+       {0, "EXCLUSIVE", 0, "Exclusive", ""},
+       {1, "EXTEND", 0, "Extend", ""},
+       {0, NULL, 0, NULL, NULL}
+};
 
 /* ****** Border Select ****** */
 void VIEW3D_OT_select_border(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name= "Border Select";
-       ot->description= "Select items using border selection.";
+       ot->description= "Select items using border selection";
        ot->idname= "VIEW3D_OT_select_border";
        
        /* api callbacks */
@@ -1607,42 +1736,50 @@ 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");
+       Object *obact= CTX_data_active_object(C);
        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(obact && obact->mode & OB_MODE_SCULPT)
+               return OPERATOR_CANCELLED;
        else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
-               PE_mouse_particles(C, event->mval, extend);
+               return PE_mouse_particles(C, event->mval, extend);
        else if(obact && paint_facesel_test(obact))
-               face_select(C, obact, event->mval, extend);
+               retval = face_select(C, obact, event->mval, extend);
        else
-               mouse_select(C, event->mval, extend, center, enumerate);
+               retval = mouse_select(C, event->mval, extend, center, enumerate);
 
-       /* allowing tweaks */
-       return OPERATOR_PASS_THROUGH|OPERATOR_FINISHED;
+       /* 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->description= "Activate/select item(s)";
        ot->idname= "VIEW3D_OT_select";
        
        /* api callbacks */
@@ -1661,38 +1798,37 @@ void VIEW3D_OT_select(wmOperatorType *ot)
 
 /* -------------------- circle select --------------------------------------------- */
 
-static void mesh_circle_doSelectVert(void *userData, EditVert *eve, int x, int y, int index)
+static void mesh_circle_doSelectVert(void *userData, BMVert *eve, int x, int y, int index)
 {
        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) {
-               eve->f = data->select?(eve->f|1):(eve->f&~1);
+               BM_Select_Vert(data->vc->em->bm, eve, data->select);
        }
 }
-static void mesh_circle_doSelectEdge(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index)
+static void mesh_circle_doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
 {
        struct {ViewContext *vc; short select, mval[2]; float radius; } *data = userData;
 
        if (edge_inside_circle(data->mval[0], data->mval[1], (short) data->radius, x0, y0, x1, y1)) {
-               EM_select_edge(eed, data->select);
+               BM_Select_Edge(data->vc->em->bm, eed, data->select);
        }
 }
-static void mesh_circle_doSelectFace(void *userData, EditFace *efa, int x, int y, int index)
+static void mesh_circle_doSelectFace(void *userData, BMFace *efa, int x, int y, int index)
 {
        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) {
-               EM_select_face_fgon(data->vc->em, efa, data->select);
+               BM_Select_Face(data->vc->em->bm, efa, data->select);
        }
 }
 
 static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
 {
-       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
        Object *ob= vc->obact;
        
@@ -1700,11 +1836,11 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                Mesh *me = ob?ob->data:NULL;
 
                if (me) {
-                       em_vertoffs= me->totface+1;     /* max index array */
+                       bm_vertoffs= me->totface+1;     /* max index array */
 
-                       bbsel= EM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
-                       EM_backbuf_checkAndSelectTFaces(me, selecting==LEFTMOUSE);
-                       EM_free_backbuf();
+                       //bbsel= EDBM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
+                       //BMESH_TODO EM_backbuf_checkAndSelectTFaces(me, selecting==LEFTMOUSE);
+                       //EDBM_free_backbuf();
 
 // XXX                 object_tface_flags_changed(OBACT, 0);
                }
@@ -1712,43 +1848,43 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
        else {
                struct {ViewContext *vc; short select, mval[2]; float radius; } data;
                
-               bbsel= EM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
+               bbsel= EDBM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
+               vc->em= ((Mesh *)vc->obedit->data)->edit_btmesh;
                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];
                data.radius = rad;
+               data.vc = vc;
 
-               if(ts->selectmode & SCE_SELECT_VERTEX) {
+               if(vc->scene->toolsettings->selectmode & SCE_SELECT_VERTEX) {
                        if(bbsel) {
-                               EM_backbuf_checkAndSelectVerts(vc->em, selecting==LEFTMOUSE);
+                               EDBM_backbuf_checkAndSelectVerts(vc->em, selecting==LEFTMOUSE);
                        } else {
                                mesh_foreachScreenVert(vc, mesh_circle_doSelectVert, &data, 1);
                        }
                }
 
-               if(ts->selectmode & SCE_SELECT_EDGE) {
+               if(vc->scene->toolsettings->selectmode & SCE_SELECT_EDGE) {
                        if (bbsel) {
-                               EM_backbuf_checkAndSelectEdges(vc->em, selecting==LEFTMOUSE);
+                               EDBM_backbuf_checkAndSelectEdges(vc->em, selecting==LEFTMOUSE);
                        } else {
                                mesh_foreachScreenEdge(vc, mesh_circle_doSelectEdge, &data, 0);
                        }
                }
                
-               if(ts->selectmode & SCE_SELECT_FACE) {
+               if(vc->scene->toolsettings->selectmode & SCE_SELECT_FACE) {
                        if(bbsel) {
-                               EM_backbuf_checkAndSelectFaces(vc->em, selecting==LEFTMOUSE);
+                               EDBM_backbuf_checkAndSelectFaces(vc->em, selecting==LEFTMOUSE);
                        } else {
                                mesh_foreachScreenFace(vc, mesh_circle_doSelectFace, &data);
                        }
                }
 
-               EM_free_backbuf();
-               EM_selectmode_flush(vc->em);
+               EDBM_free_backbuf();
+               EDBM_selectmode_flush(vc->em);
        }
 }
 
@@ -1859,13 +1995,11 @@ static void armature_circle_select(ViewContext *vc, int selecting, short *mval,
                float vec[3];
                
                /* project head location to screenspace */
-               VECCOPY(vec, ebone->head);
-               mul_m4_v3(vc->obedit->obmat, vec);
+               mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
                project_short(vc->ar, vec, sco1);
                
                /* project tail location to screenspace */
-               VECCOPY(vec, ebone->tail);
-               mul_m4_v3(vc->obedit->obmat, vec);
+               mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
                project_short(vc->ar, vec, sco2);
                
                /* check if the head and/or tail is in the circle 
@@ -1945,6 +2079,9 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
                else
                        return PE_circle_select(C, selecting, mval, (float)radius);
        }
+       else if(obact && obact->mode & OB_MODE_SCULPT) {
+               return OPERATOR_CANCELLED;
+       }
        else {
                Base *base;
                selecting= selecting?BA_SELECT:BA_DESELECT;
@@ -1969,7 +2106,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->description= "Select items using circle selection";
        ot->idname= "VIEW3D_OT_select_circle";
        
        ot->invoke= WM_gesture_circle_invoke;