merge with trunk at r31523
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
index 0b1bdf0007864fed6f45b3230e457518f199c74f..776048c8f74d39e6715f67d57de71eac30d4c429 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id$
+ * $Id:
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
 #include <math.h>
 #include <float.h>
 
+#include "DNA_action_types.h"
 #include "DNA_armature_types.h"
 #include "DNA_curve_types.h"
+#include "DNA_camera_types.h"
+#include "DNA_lamp_types.h"
 #include "DNA_meta_types.h"
+#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
+#include "DNA_space_types.h"
 #include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_userdef_types.h"
+#include "DNA_view3d_types.h"
+#include "DNA_world_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -48,7 +57,9 @@
 
 #include "BKE_context.h"
 #include "BKE_paint.h"
+#include "BKE_tessmesh.h"
 
+#include "RE_pipeline.h"       // make_stars
 
 #include "BIF_gl.h"
 
 #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 "UI_resources.h"
+#include "UI_view2d.h"
 
+#include "PIL_time.h" /* smoothview */
 
 #include "view3d_intern.h"     // own include
 
@@ -98,7 +114,7 @@ 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);
-               sub_v3_v3(fp, dvec);
+               sub_v3_v3v3(fp, fp, dvec);
        }
 }
 
@@ -133,56 +149,62 @@ void view3d_get_transformation(ARegion *ar, RegionView3D *rv3d, Object *ob, bglM
 
 /* local prototypes */
 
-void EM_backbuf_checkAndSelectVerts(EditMesh *em, int select)
+void EDBM_backbuf_checkAndSelectVerts(BMEditMesh *em, int select)
 {
-       EditVert *eve;
-       int index= em_wireoffs;
+       BMVert *eve;
+       BMIter iter;
+       int index= bm_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);
+       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;
+       BMEdge *eed;
+       BMIter iter;
+       int index= bm_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);
+       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);
                        }
                }
@@ -211,23 +233,6 @@ void arrows_move_cursor(unsigned short event)
 
 /* *********************** GESTURE AND LASSO ******************* */
 
-static int view3d_selectable_data(bContext *C)
-{
-       Object *ob = CTX_data_active_object(C);
-       
-       if (!ED_operator_view3d_active(C))
-               return 0;
-       
-       if (!CTX_data_edit_object(C))
-               if (ob && ob->mode & OB_MODE_SCULPT)
-                       return 0;
-               if (ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT) && !paint_facesel_test(ob))
-                       return 0;
-       
-       return 1;
-}
-
-
 /* helper also for borderselect */
 static int edge_fully_inside_rect(rcti *rect, short x1, short y1, short x2, short y2)
 {
@@ -404,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;
@@ -460,24 +464,24 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
        data.done = 0;
        data.pass = 0;
 
-       /* workaround: init mats first, EM_mask_init_backbuf_border can change
+       /* 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= EM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       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 */
 
@@ -490,17 +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
@@ -536,7 +540,7 @@ static void do_lasso_select_mesh_uv(short mcords[][2], short moves, short select
        } else { /* Vert Sel*/
                for (efa= em->faces.first; efa; efa= efa->next) {
                        tf = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                       if (uvedit_face_visible(scene, ima, efa, tf)) {         
+                       if (simaFaceDraw_Check(efa, tf)) {              
                                nverts= efa->v4? 4: 3;
                                for(i=0; i<nverts; i++) {
                                        if ((select) != (simaUVSel_Check(efa, tf, i))) {
@@ -562,15 +566,14 @@ 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 { ViewContext *vc; short (*mcords)[2]; short moves; short select; } *data = userData;
-       Object *obedit= data->vc->obedit;
-       Curve *cu= (Curve*)obedit->data;
-
+       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);
-                       if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
                } else {
+                       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);
@@ -583,18 +586,16 @@ static void do_lasso_select_curve__doSelect(void *userData, Nurb *nu, BPoint *bp
                                        bezt->f3 = data->select?(bezt->f3|SELECT):(bezt->f3&~SELECT);
                                }
                        }
-
-                       if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
                }
        }
 }
 
 static void do_lasso_select_curve(ViewContext *vc, short mcords[][2], short moves, short select)
 {
-       struct { ViewContext *vc; 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.vc = *vc;
        data.mcords = mcords;
        data.moves = moves;
        data.select = select;
@@ -670,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);
 }
@@ -729,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 */
@@ -767,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;
@@ -784,13 +791,13 @@ void VIEW3D_OT_select_lasso(wmOperatorType *ot)
        ot->invoke= WM_gesture_lasso_invoke;
        ot->modal= WM_gesture_lasso_modal;
        ot->exec= view3d_lasso_select_exec;
-       ot->poll= view3d_selectable_data;
+       ot->poll= WM_operator_winactive;
        
        /* flags */
        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", "");
 }
 
 
@@ -935,7 +942,7 @@ static Base *mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffe
                                WM_operator_properties_create(&ptr, "OBJECT_OT_select_name");
                                RNA_string_set(&ptr, "name", name);
                                RNA_boolean_set(&ptr, "extend", extend);
-                               uiItemFullO(column, "OBJECT_OT_select_name", name, uiIconFromID((ID *)ob), ptr.data, WM_OP_EXEC_DEFAULT, 0);
+                               uiItemFullO(column, name, uiIconFromID((ID *)ob), "OBJECT_OT_select_name", ptr.data, WM_OP_EXEC_DEFAULT, 0);
                        }
 
                        node= node->next;
@@ -1283,15 +1290,14 @@ int edge_inside_circle(short centx, short centy, short rad, short x1, short y1,
 
 static void do_nurbs_box_select__doSelect(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
 {
-       struct { ViewContext *vc; rcti *rect; int select; } *data = userData;
-       Object *obedit= data->vc->obedit;
-       Curve *cu= (Curve*)obedit->data;
+       struct { ViewContext vc; rcti *rect; int select; } *data = userData;
 
        if (BLI_in_rcti(data->rect, x, y)) {
                if (bp) {
                        bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
-                       if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
                } else {
+                       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);
@@ -1304,16 +1310,14 @@ static void do_nurbs_box_select__doSelect(void *userData, Nurb *nu, BPoint *bp,
                                        bezt->f3 = data->select?(bezt->f3|SELECT):(bezt->f3&~SELECT);
                                }
                        }
-
-                       if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
                }
        }
 }
 static void do_nurbs_box_select(ViewContext *vc, rcti *rect, int select, int extend)
 {
-       struct { ViewContext *vc; rcti *rect; int select; } data;
+       struct { ViewContext vc; rcti *rect; int select; } data;
        
-       data.vc = vc;
+       data.vc = *vc;
        data.rect = rect;
        data.select = select;
 
@@ -1349,43 +1353,47 @@ static void do_lattice_box_select(ViewContext *vc, rcti *rect, int select, int e
        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, int extend)
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } data;
-       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
        
        data.vc= *vc;
@@ -1396,7 +1404,7 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int exte
 
        if (extend == 0 && select)
        {
-               EM_deselect_all(vc->em);
+               EDBM_clear_flag_all(vc->em, BM_SELECT);
        }
 
        /* workaround: init mats first, EM_mask_init_backbuf_border can change
@@ -1407,17 +1415,17 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int exte
        /* ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d) */
 
        glLoadMatrixf(vc->rv3d->viewmat);
-       bbsel= EM_init_backbuf_border(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
+       bbsel= EDBM_init_backbuf_border(vc, 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);
                        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;
@@ -1429,18 +1437,18 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int exte
                }
        }
        
-       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)
@@ -1484,9 +1492,9 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        if(obedit) {
                if(obedit->type==OB_MESH) {
                        Mesh *me= obedit->data;
-                       vc.em= me->edit_mesh;
+                       vc.em= me->edit_btmesh;
                        do_mesh_box_select(&vc, &rect, selecting, extend);
-//                     if (EM_texFaceCheck())
+//                     if (EDBM_texFaceCheck())
                        WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
                        
                }
@@ -1590,7 +1598,6 @@ 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, extend);
@@ -1602,7 +1609,6 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                unsigned int *vbuffer=NULL; /* selection buffer */
                unsigned int *col;                      /* color in buffer      */
                int bone_only;
-               int bone_selected=0;
                int totobj= MAXPICKBUF; // XXX solve later
                
                if((ob) && (ob->mode & OB_MODE_POSE))
@@ -1658,7 +1664,6 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                                        if(bone) {
                                                                if(selecting) {
                                                                        bone->flag |= BONE_SELECTED;
-                                                                       bone_selected=1;
 // XXX                                                                 select_actionchannel_by_name(base->object->action, bone->name, 1);
                                                                }
                                                                else {
@@ -1684,8 +1689,6 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                        }
                                }
                                
-                               if (bone_selected)      WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, base->object);
-                               
                                base= next;
                        }
 
@@ -1699,6 +1702,11 @@ 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)
@@ -1712,7 +1720,8 @@ void VIEW3D_OT_select_border(wmOperatorType *ot)
        ot->invoke= WM_border_select_invoke;
        ot->exec= view3d_borderselect_exec;
        ot->modal= WM_border_select_modal;
-       ot->poll= view3d_selectable_data;
+       
+       ot->poll= ED_operator_view3d_active;
        
        /* flags */
        ot->flag= OPTYPE_UNDO;
@@ -1727,8 +1736,8 @@ 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;
@@ -1789,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;
        
@@ -1828,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);
                }
@@ -1840,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);
        }
 }
 
@@ -1886,29 +1894,18 @@ static void nurbscurve_circle_doSelect(void *userData, Nurb *nu, BPoint *bp, Bez
        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);
-       Object *obedit= data->vc->obedit;
-       Curve *cu= (Curve*)obedit->data;
 
        if (r<=data->radius) {
                if (bp) {
                        bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
-
-                       if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
                } else {
-                       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);
+                       if (beztindex==0) {
+                               bezt->f1 = data->select?(bezt->f1|SELECT):(bezt->f1&~SELECT);
+                       } else if (beztindex==1) {
+                               bezt->f2 = data->select?(bezt->f2|SELECT):(bezt->f2&~SELECT);
                        } else {
-                               if (beztindex==0) {
-                                       bezt->f1 = data->select?(bezt->f1|SELECT):(bezt->f1&~SELECT);
-                               } else if (beztindex==1) {
-                                       bezt->f2 = data->select?(bezt->f2|SELECT):(bezt->f2&~SELECT);
-                               } else {
-                                       bezt->f3 = data->select?(bezt->f3|SELECT):(bezt->f3&~SELECT);
-                               }
+                               bezt->f3 = data->select?(bezt->f3|SELECT):(bezt->f3&~SELECT);
                        }
-
-                       if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
                }
        }
 }
@@ -1922,7 +1919,6 @@ static void nurbscurve_circle_select(ViewContext *vc, int selecting, short *mval
        data.mval[0] = mval[0];
        data.mval[1] = mval[1];
        data.radius = rad;
-       data.vc = vc;
 
        ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
        nurbs_foreachScreenVert(vc, nurbscurve_circle_doSelect, &data);
@@ -2061,10 +2057,10 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
        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;
+    int gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
+    int selecting;
        
-       selecting= (gesture_mode==GESTURE_MODAL_SELECT);
+    selecting= (gesture_mode==GESTURE_MODAL_SELECT);
     
        if(CTX_data_edit_object(C) || (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
                ViewContext vc;
@@ -2116,7 +2112,7 @@ void VIEW3D_OT_select_circle(wmOperatorType *ot)
        ot->invoke= WM_gesture_circle_invoke;
        ot->modal= WM_gesture_circle_modal;
        ot->exec= view3d_circle_select_exec;
-       ot->poll= view3d_selectable_data;
+       ot->poll= ED_operator_view3d_active;
        
        /* flags */
        ot->flag= OPTYPE_UNDO;