Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
index 7a5442b..7743ede 100644 (file)
@@ -59,6 +59,7 @@
 #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"
@@ -66,8 +67,6 @@
 #include "RE_pipeline.h"       // make_stars
 
 #include "BIF_gl.h"
-#include "BIF_retopo.h"
-#include "BIF_editarmature.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
 #include "RNA_access.h"
 #include "RNA_define.h"
 
+#include "ED_armature.h"
+#include "ED_curve.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_mball.h"
 
 #include "UI_interface.h"
 #include "UI_resources.h"
 
 #include "view3d_intern.h"     // own include
 
+
+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->obact= CTX_data_active_object(C);
+       vc->obedit= CTX_data_edit_object(C); 
+}
+
+void view3d_get_view_aligned_coordinate(ViewContext *vc, float *fp, short mval[2])
+{
+       float dvec[3];
+       short mx, my;
+       
+       mx= mval[0];
+       my= mval[1];
+       
+       project_short_noclip(vc->ar, fp, mval);
+       
+       initgrabz(vc->rv3d, fp[0], fp[1], fp[2]);
+       
+       if(mval[0]!=IS_CLIPPED) {
+               window_to_3d_delta(vc->ar, dvec, mval[0]-mx, mval[1]-my);
+               VecSubf(fp, fp, dvec);
+       }
+}
+
+void view3d_get_transformation(ViewContext *vc, Object *ob, bglMats *mats)
+{
+       float cpy[4][4];
+       int i, j;
+
+       Mat4MulMat4(cpy, ob->obmat, vc->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->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;       
+}
+
 /* ********************** view3d_select: selection manipulations ********************* */
 
 /* XXX to solve *************** */
@@ -298,10 +353,10 @@ static void do_lasso_select_pose(ViewContext *vc, short mcords[][2], short moves
        for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
                VECCOPY(vec, pchan->pose_head);
                Mat4MulVecfl(ob->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco1);
+               project_short(vc->ar, vec, sco1);
                VECCOPY(vec, pchan->pose_tail);
                Mat4MulVecfl(ob->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco2);
+               project_short(vc->ar, vec, sco2);
                
                if(lasso_inside_edge(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1])) {
                        if(select) pchan->bone->flag |= BONE_SELECTED;
@@ -317,14 +372,14 @@ static void do_lasso_select_objects(ViewContext *vc, short mcords[][2], short mo
        
        for(base= vc->scene->base.first; base; base= base->next) {
                if(base->lay & vc->v3d->lay) {
-                       project_short(vc->ar, vc->v3d, base->object->obmat[3], &base->sx);
+                       project_short(vc->ar, base->object->obmat[3], &base->sx);
                        if(lasso_inside(mcords, moves, base->sx, base->sy)) {
                                
                                if(select) ED_base_object_select(base, BA_SELECT);
                                else ED_base_object_select(base, BA_DESELECT);
                                base->object->flag= base->flag;
                        }
-                       if(base->object->flag & OB_POSEMODE) {
+                       if(base->object->mode & OB_MODE_POSE) {
                                do_lasso_select_pose(vc, mcords, moves, select);
                        }
                }
@@ -385,11 +440,15 @@ static void do_lasso_select_mesh__doSelectFace(void *userData, EditFace *efa, in
 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;
+
        data.vc= *vc;
        data.rect = &rect;
        data.mcords = mcords;
@@ -398,16 +457,16 @@ 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->v3d, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       bbsel= EM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
        
-       if(vc->scene->selectmode & SCE_SELECT_VERTEX) {
+       if(ts->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
                        EM_backbuf_checkAndSelectVerts(vc->em, select);
                } else {
                        mesh_foreachScreenVert(vc, do_lasso_select_mesh__doSelectVert, &data, 1);
                }
        }
-       if(vc->scene->selectmode & SCE_SELECT_EDGE) {
+       if(ts->selectmode & SCE_SELECT_EDGE) {
                        /* Does both bbsel and non-bbsel versions (need screen cos for both) */
 
                data.pass = 0;
@@ -419,7 +478,7 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
                }
        }
        
-       if(vc->scene->selectmode & SCE_SELECT_FACE) {
+       if(ts->selectmode & SCE_SELECT_FACE) {
                if (bbsel) {
                        EM_backbuf_checkAndSelectFaces(vc->em, select);
                } else {
@@ -516,6 +575,7 @@ static void do_lasso_select_curve(ViewContext *vc, short mcords[][2], short move
 {
        struct { short (*mcords)[2]; short moves; short select; } data;
 
+       /* set vc->editnurb */
        data.mcords = mcords;
        data.moves = moves;
        data.select = select;
@@ -535,6 +595,7 @@ static void do_lasso_select_lattice(ViewContext *vc, short mcords[][2], short mo
 {
        struct { short (*mcords)[2]; short moves; short select; } data;
 
+       /* set editdata in vc */
        data.mcords = mcords;
        data.moves = moves;
        data.select = select;
@@ -544,18 +605,21 @@ static void do_lasso_select_lattice(ViewContext *vc, short mcords[][2], short mo
 
 static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short moves, short select)
 {
+       bArmature *arm= vc->obedit->data;
        EditBone *ebone;
        float vec[3];
        short sco1[2], sco2[2], didpoint;
        
-       for (ebone=G.edbo.first; ebone; ebone=ebone->next) {
+       /* set editdata in vc */
+       
+       for (ebone= arm->edbo->first; ebone; ebone=ebone->next) {
 
                VECCOPY(vec, ebone->head);
-               Mat4MulVecfl(G.obedit->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco1);
+               Mat4MulVecfl(vc->obedit->obmat, vec);
+               project_short(vc->ar, vec, sco1);
                VECCOPY(vec, ebone->tail);
-               Mat4MulVecfl(G.obedit->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco2);
+               Mat4MulVecfl(vc->obedit->obmat, vec);
+               project_short(vc->ar, vec, sco2);
                
                didpoint= 0;
                if(lasso_inside(mcords, moves, sco1[0], sco1[1])) {
@@ -574,7 +638,6 @@ static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short m
                        else ebone->flag &= ~(BONE_ACTIVE|BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL);
                }
        }
-       // XXX countall();      /* abused for flushing selection!!!! */
 }
 
 static void do_lasso_select_facemode(ViewContext *vc, short mcords[][2], short moves, short select)
@@ -589,7 +652,7 @@ static void do_lasso_select_facemode(ViewContext *vc, short mcords[][2], short m
        em_vertoffs= me->totface+1;     /* max index array */
        
        lasso_select_boundbox(&rect, mcords, moves);
-       EM_mask_init_backbuf_border(vc->v3d, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
+       EM_mask_init_backbuf_border(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
        
        EM_backbuf_checkAndSelectTFaces(me, select);
        
@@ -629,31 +692,87 @@ static void do_lasso_select_node(short mcords[][2], short moves, short select)
 }
 #endif
 
-void view3d_lasso_select(ViewContext *vc, short mcords[][2], short moves, short select)
+void view3d_lasso_select(bContext *C, ViewContext *vc, short mcords[][2], short moves, short select)
 {
-       if(G.obedit==NULL) {
-               if(FACESEL_PAINT_TEST)
+       Object *ob = CTX_data_active_object(C);
+
+       if(vc->obedit==NULL) {
+               if(paint_facesel_test(ob))
                        do_lasso_select_facemode(vc, mcords, moves, select);
-               else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))
+               else if(ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT))
                        ;
-// XX          else if(G.f & G_PARTICLEEDIT)
-//                     PE_do_lasso_select(mcords, moves, select);
+               else if(ob && ob->mode & OB_MODE_PARTICLE_EDIT)
+                       PE_lasso_select(C, mcords, moves, select);
                else  
                        do_lasso_select_objects(vc, mcords, moves, select);
        }
-       else if(G.obedit->type==OB_MESH) {
+       else if(vc->obedit->type==OB_MESH) {
                do_lasso_select_mesh(vc, mcords, moves, select);
-       } else if(G.obedit->type==OB_CURVE || G.obedit->type==OB_SURF) 
+       } else if(vc->obedit->type==OB_CURVE || vc->obedit->type==OB_SURF) 
                do_lasso_select_curve(vc, mcords, moves, select);
-       else if(G.obedit->type==OB_LATTICE) 
+       else if(vc->obedit->type==OB_LATTICE) 
                do_lasso_select_lattice(vc, mcords, moves, select);
-       else if(G.obedit->type==OB_ARMATURE)
+       else if(vc->obedit->type==OB_ARMATURE)
                do_lasso_select_armature(vc, mcords, moves, select);
 
        BIF_undo_push("Lasso select");
        
 }
 
+
+/* lasso operator gives properties, but since old code works
+   with short array we convert */
+static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
+{
+       ViewContext vc;
+       int select, i= 0;
+       short mcords[1024][2];
+
+       RNA_BEGIN(op->ptr, itemptr, "path") {
+               float loc[2];
+               
+               RNA_float_get_array(&itemptr, "loc", loc);
+               mcords[i][0]= (short)loc[0];
+               mcords[i][1]= (short)loc[1];
+               i++;
+               if(i>=1024) break;
+       }
+       RNA_END;
+       
+       if(i>1) {
+               view3d_operator_needs_opengl(C);
+               
+               /* setup view context for argument to callbacks */
+               view3d_set_viewcontext(C, &vc);
+               
+               select= !RNA_boolean_get(op->ptr, "deselect");
+               view3d_lasso_select(C, &vc, mcords, i, select);
+               
+               return OPERATOR_FINISHED;
+       }
+       return OPERATOR_PASS_THROUGH;
+}
+
+void VIEW3D_OT_select_lasso(wmOperatorType *ot)
+{
+       ot->name= "Lasso Select";
+       ot->idname= "VIEW3D_OT_select_lasso";
+       
+       ot->invoke= WM_gesture_lasso_invoke;
+       ot->modal= WM_gesture_lasso_modal;
+       ot->exec= view3d_lasso_select_exec;
+       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.");
+}
+
+
+/* ************************************************* */
+
 #if 0
 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
 static unsigned int samplerect(unsigned int *buf, int size, unsigned int dontdo)
@@ -706,11 +825,9 @@ static unsigned int samplerect(unsigned int *buf, int size, unsigned int dontdo)
 /* ************************** mouse select ************************* */
 
 
-#define MAXPICKBUF      10000
 /* The max number of menu items in an object select menu */
 #define SEL_MENU_SIZE  22
 
-
 static void deselectall_except(Scene *scene, Base *b)   /* deselect all except b */
 {
        Base *base;
@@ -749,7 +866,7 @@ static Base *mouse_select_menu(ViewContext *vc, unsigned int *buffer, int hits,
                        else {
                                int temp, dist=15;
                                
-                               project_short(vc->ar, vc->v3d, base->object->obmat[3], &base->sx);
+                               project_short(vc->ar, base->object->obmat[3], &base->sx);
                                
                                temp= abs(base->sx -mval[0]) + abs(base->sy -mval[1]);
                                if(temp<dist ) baseList[baseCount] = base;
@@ -841,7 +958,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
 {
        ViewContext vc;
        ARegion *ar= CTX_wm_region(C);
-       View3D *v3d= (View3D *)CTX_wm_space_data(C);
+       View3D *v3d= CTX_wm_view3d(C);
        Scene *scene= CTX_data_scene(C);
        Base *base, *startbase=NULL, *basact=NULL, *oldbasact=NULL;
        unsigned int buffer[4*MAXPICKBUF];
@@ -849,11 +966,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
        short hits;
        
        /* setup view context for argument to callbacks */
-       memset(&vc, 0, sizeof(ViewContext));
-       vc.ar= ar;
-       vc.scene= scene;
-       vc.v3d= v3d;
-       vc.obact= OBACT;
+       view3d_set_viewcontext(C, &vc);
        
        /* always start list from basact in wire mode */
        startbase=  FIRSTBASE;
@@ -861,7 +974,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
        
        /* This block uses the control key to make the object selected by its center point rather then its contents */
        /* XXX later on, in editmode do not activate */
-       if(G.obedit==NULL && obcenter) {
+       if(vc.obedit==NULL && obcenter) {
                
                /* note; shift+alt goes to group-flush-selecting */
                /* XXX solve */
@@ -871,7 +984,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                        base= startbase;
                        while(base) {
                                if (BASE_SELECTABLE(v3d, base)) {
-                                       project_short(ar, v3d, base->object->obmat[3], &base->sx);
+                                       project_short(ar, base->object->obmat[3], &base->sx);
                                        
                                        temp= abs(base->sx -mval[0]) + abs(base->sy -mval[1]);
                                        if(base==BASACT) temp+=10;
@@ -988,21 +1101,21 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                        }
                        
                        if(has_bones && basact) {
-                               if(0) {// XXX do_pose_selectbuffer(basact, buffer, hits) ) {    /* then bone is found */
+                               if(ED_do_pose_selectbuffer(scene, basact, buffer, hits, extend) ) {     /* then bone is found */
                                
                                        /* we make the armature selected: 
                                           not-selected active object in posemode won't work well for tools */
                                        basact->flag|= SELECT;
                                        basact->object->flag= basact->flag;
                                        
+                                       WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, basact->object);
+                                       WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, basact->object);
+                                       
                                        /* in weightpaint, we use selected bone to select vertexgroup, so no switch to new active object */
-                                       if(G.f & G_WEIGHTPAINT) {
+                                       if(basact->object->mode & OB_MODE_WEIGHT_PAINT) {
                                                /* prevent activating */
                                                basact= NULL;
                                        }
-                                       
-                                       WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, basact->object);
-                                       WM_event_add_notifier(C, NC_OBJECT|ND_BONE_ACTIVE, basact->object);
 
                                }
                                /* prevent bone selecting to pass on to object selecting */
@@ -1015,7 +1128,7 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
        /* so, do we have something selected? */
        if(basact) {
                
-               if(G.obedit) {
+               if(vc.obedit) {
                        /* only do select */
                        deselectall_except(scene, basact);
                        ED_base_object_select(basact, BA_SELECT);
@@ -1024,7 +1137,6 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                else if (BASE_SELECTABLE(v3d, basact)) {
 
                        oldbasact= BASACT;
-                       BASACT= basact;
                        
                        if(!extend) {
                                deselectall_except(scene, basact);
@@ -1046,10 +1158,8 @@ static void mouse_select(bContext *C, short *mval, short extend, short obcenter)
                        }
 
                        WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
-                       
                }
        }
-
 }
 
 /* ********************  border and circle ************************************** */
@@ -1166,6 +1276,7 @@ static void do_mesh_box_select__doSelectFace(void *userData, EditFace *efa, int
 static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
 {
        struct { ViewContext vc; rcti *rect; short select, pass, done; } data;
+       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
        
        data.vc= *vc;
@@ -1174,16 +1285,16 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
        data.pass = 0;
        data.done = 0;
 
-       bbsel= EM_init_backbuf_border(vc->v3d, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
+       bbsel= EM_init_backbuf_border(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
 
-       if(vc->scene->selectmode & SCE_SELECT_VERTEX) {
+       if(ts->selectmode & SCE_SELECT_VERTEX) {
                if (bbsel) {
                        EM_backbuf_checkAndSelectVerts(vc->em, select);
                } else {
                        mesh_foreachScreenVert(vc, do_mesh_box_select__doSelectVert, &data, 1);
                }
        }
-       if(vc->scene->selectmode & SCE_SELECT_EDGE) {
+       if(ts->selectmode & SCE_SELECT_EDGE) {
                        /* Does both bbsel and non-bbsel versions (need screen cos for both) */
 
                data.pass = 0;
@@ -1195,7 +1306,7 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
                }
        }
        
-       if(vc->scene->selectmode & SCE_SELECT_FACE) {
+       if(ts->selectmode & SCE_SELECT_FACE) {
                if(bbsel) {
                        EM_backbuf_checkAndSelectFaces(vc->em, select);
                } else {
@@ -1213,8 +1324,9 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        ViewContext vc;
        Scene *scene= CTX_data_scene(C);
        ScrArea *sa= CTX_wm_area(C);
-       ARegion *ar= CTX_wm_region(C);
        View3D *v3d= sa->spacedata.first;
+       Object *obedit= CTX_data_edit_object(C);
+       Object *obact= CTX_data_active_object(C);
        rcti rect;
        Base *base;
        MetaElem *ml;
@@ -1222,13 +1334,10 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        int a, index;
        short hits, val;
 
+       view3d_operator_needs_opengl(C);
+       
        /* setup view context for argument to callbacks */
-       memset(&vc, 0, sizeof(ViewContext));
-       vc.ar= ar;
-       vc.scene= scene;
-       vc.v3d= v3d;
-       vc.obact= OBACT;
-       vc.obedit= G.obedit;
+       view3d_set_viewcontext(C, &vc);
        
        val= RNA_int_get(op->ptr, "event_type");
        rect.xmin= RNA_int_get(op->ptr, "xmin");
@@ -1236,32 +1345,31 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        rect.xmax= RNA_int_get(op->ptr, "xmax");
        rect.ymax= RNA_int_get(op->ptr, "ymax");
        
-       if(G.obedit==NULL && (FACESEL_PAINT_TEST)) {
+       if(obedit==NULL && (paint_facesel_test(OBACT))) {
 // XXX         face_borderselect();
                return OPERATOR_FINISHED;
        }
-       else if(G.obedit==NULL && (G.f & G_PARTICLEEDIT)) {
-// XXX         PE_borderselect();
-               return OPERATOR_FINISHED;
+       else if(obedit==NULL && (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
+               return PE_border_select(C, &rect, (val==LEFTMOUSE));
        }
        
-       if(G.obedit) {
-               if(G.obedit->type==OB_MESH) {
-                       Mesh *me= G.obedit->data;
+       if(obedit) {
+               if(obedit->type==OB_MESH) {
+                       Mesh *me= obedit->data;
                        vc.em= me->edit_mesh;
                        do_mesh_box_select(&vc, &rect, (val==LEFTMOUSE));
 //                     if (EM_texFaceCheck())
-                       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, G.obedit);
+                       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obedit);
                        
                }
-               else if(ELEM(G.obedit->type, OB_CURVE, OB_SURF)) {
+               else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
                        do_nurbs_box_select(&vc, &rect, val==LEFTMOUSE);
-//                     allqueue(REDRAWVIEW3D, 0);
                }
-               else if(G.obedit->type==OB_MBALL) {
+               else if(obedit->type==OB_MBALL) {
+                       MetaBall *mb = (MetaBall*)obedit->data;
                        hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
                        
-                       ml= NULL; // XXX editelems.first;
+                       ml= mb->editelems->first;
                        
                        while(ml) {
                                for(a=0; a<hits; a++) {
@@ -1281,11 +1389,12 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                ml= ml->next;
                        }
                }
-               else if(G.obedit->type==OB_ARMATURE) {
+               else if(obedit->type==OB_ARMATURE) {
+                       bArmature *arm= obedit->data;
                        EditBone *ebone;
                        
                        /* clear flag we use to detect point was affected */
-                       for(ebone= G.edbo.first; ebone; ebone= ebone->next)
+                       for(ebone= arm->edbo->first; ebone; ebone= ebone->next)
                                ebone->flag &= ~BONE_DONE;
                        
                        hits= view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect);
@@ -1294,7 +1403,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        for (a=0; a<hits; a++){
                                index = buffer[(4*a)+3];
                                if (index!=-1) {
-                                       ebone = BLI_findlink(&G.edbo, index & ~(BONESEL_ANY));
+                                       ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
                                        if (index & BONESEL_TIP) {
                                                ebone->flag |= BONE_DONE;
                                                if (val==LEFTMOUSE) ebone->flag |= BONE_TIPSEL;
@@ -1310,7 +1419,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        }
                        
                        /* now we have to flush tag from parents... */
-                       for(ebone= G.edbo.first; ebone; ebone= ebone->next) {
+                       for(ebone= arm->edbo->first; ebone; ebone= ebone->next) {
                                if(ebone->parent && (ebone->flag & BONE_CONNECTED)) {
                                        if(ebone->parent->flag & BONE_DONE)
                                                ebone->flag |= BONE_DONE;
@@ -1321,7 +1430,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        for (a=0; a<hits; a++){
                                index = buffer[(4*a)+3];
                                if (index!=-1) {
-                                       ebone = BLI_findlink(&G.edbo, index & ~(BONESEL_ANY));
+                                       ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
                                        if (index & BONESEL_BONE) {
                                                if(!(ebone->flag & BONE_DONE)) {
                                                        if (val==LEFTMOUSE)
@@ -1333,20 +1442,22 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                }
                        }
                        
+                       ED_armature_sync_selection(arm->edbo);
                }
-               else if(G.obedit->type==OB_LATTICE) {
+               else if(obedit->type==OB_LATTICE) {
                        do_lattice_box_select(&vc, &rect, val==LEFTMOUSE);
                }
        }
        else {  /* no editmode, unified for bones and objects */
                Bone *bone;
+               Object *ob= OBACT;
                unsigned int *vbuffer=NULL; /* selection buffer */
                unsigned int *col;                      /* color in buffer      */
                short selecting = 0;
-               Object *ob= OBACT;
                int bone_only;
+               int totobj= MAXPICKBUF; // XXX solve later
                
-               if((ob) && (ob->flag & OB_POSEMODE))
+               if((ob) && (ob->mode & OB_MODE_POSE))
                        bone_only= 1;
                else
                        bone_only= 0;
@@ -1355,8 +1466,8 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                        selecting = 1;
                
                /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
-               vbuffer = MEM_mallocN(4 * (G.totobj+MAXPICKBUF) * sizeof(unsigned int), "selection buffer");
-               hits= view3d_opengl_select(&vc, vbuffer, 4*(G.totobj+MAXPICKBUF), &rect);
+               vbuffer = MEM_mallocN(4 * (totobj+MAXPICKBUF) * sizeof(unsigned int), "selection buffer");
+               hits= view3d_opengl_select(&vc, vbuffer, 4*(totobj+MAXPICKBUF), &rect);
                /*
                LOGIC NOTES (theeth):
                The buffer and ListBase have the same relative order, which makes the selection
@@ -1379,7 +1490,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                        while (base->selcol == (*col & 0xFFFF)) {       /* we got an object */
                                                
                                                if(*col & 0xFFFF0000) {                                 /* we got a bone */
-                                                       bone = NULL; // XXX get_indexed_bone(base->object, *col & ~(BONESEL_ANY));
+                                                       bone = get_indexed_bone(base->object, *col & ~(BONESEL_ANY));
                                                        if(bone) {
                                                                if(selecting) {
                                                                        bone->flag |= BONE_SELECTED;
@@ -1412,26 +1523,18 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                }
                MEM_freeN(vbuffer);
        }
-       
        return OPERATOR_FINISHED;
 } 
 
 
 /* *****************Selection Operators******************* */
-static EnumPropertyItem prop_select_types[] = {
-       {0, "EXCLUSIVE", "Exclusive", ""},
-       {1, "EXTEND", "Extend", ""},
-       {0, NULL, NULL, NULL}
-};
 
 /* ****** Border Select ****** */
-void VIEW3D_OT_borderselect(wmOperatorType *ot)
+void VIEW3D_OT_select_border(wmOperatorType *ot)
 {
-       PropertyRNA *prop;
-       
        /* identifiers */
        ot->name= "Border Select";
-       ot->idname= "VIEW3D_OT_borderselect";
+       ot->idname= "VIEW3D_OT_select_border";
        
        /* api callbacks */
        ot->invoke= WM_border_select_invoke;
@@ -1440,15 +1543,17 @@ void VIEW3D_OT_borderselect(wmOperatorType *ot)
        
        ot->poll= ED_operator_view3d_active;
        
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
+       
        /* rna */
-       RNA_def_property(ot->srna, "event_type", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "xmin", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "xmax", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "ymin", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "ymax", PROP_INT, PROP_NONE);
-
-       prop = RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, prop_select_types);
+       RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
+
+       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everyting first.");
 }
 
 /* ****** Mouse Select ****** */
@@ -1456,31 +1561,36 @@ void VIEW3D_OT_borderselect(wmOperatorType *ot)
 
 static int view3d_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
-       ARegion *ar= CTX_wm_region(C);
        Object *obedit= CTX_data_edit_object(C);
-       short extend= RNA_enum_is_equal(op->ptr, "type", "EXTEND");
-       short mval[2];  
-       
-       mval[0]= event->x - ar->winrct.xmin;
-       mval[1]= event->y - ar->winrct.ymin;
+       Object *obact= CTX_data_active_object(C);
+       short extend= RNA_boolean_get(op->ptr, "extend");
 
        view3d_operator_needs_opengl(C);
        
        if(obedit) {
                if(obedit->type==OB_MESH)
-                       mouse_mesh(C, mval, extend);
+                       mouse_mesh(C, event->mval, extend);
+               else if(obedit->type==OB_ARMATURE)
+                       mouse_armature(C, event->mval, extend);
+               else if(obedit->type==OB_LATTICE)
+                       mouse_lattice(C, event->mval, extend);
+               else if(ELEM(obedit->type, OB_CURVE, OB_SURF))
+                       mouse_nurb(C, event->mval, extend);
+               else if(obedit->type==OB_MBALL)
+                       mouse_mball(C, event->mval, extend);
+                       
        }
+       else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
+               PE_mouse_particles(C, event->mval, extend);
        else 
-               mouse_select(C, mval, extend, 0);
-       
+               mouse_select(C, event->mval, extend, 0);
+
        /* allowing tweaks */
-       return OPERATOR_PASS_THROUGH;
+       return OPERATOR_PASS_THROUGH|OPERATOR_FINISHED;
 }
 
 void VIEW3D_OT_select(wmOperatorType *ot)
 {
-       PropertyRNA *prop;
-
        /* identifiers */
        ot->name= "Activate/Select";
        ot->idname= "VIEW3D_OT_select";
@@ -1488,26 +1598,20 @@ void VIEW3D_OT_select(wmOperatorType *ot)
        /* api callbacks */
        ot->invoke= view3d_select_invoke;
        ot->poll= ED_operator_view3d_active;
-
-       prop = RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, prop_select_types);
+       
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
+       
+       /* properties */
+       RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everyting first.");
 }
 
 
-/* ------------------------------------------------------------------------- */
-
-/** The following functions are quick & dirty callback functions called
-  * on the Circle select function (press B twice in Editmode)
-  * They were torn out of the circle_select to make the latter more reusable
-  * The callback version of circle_select (called circle_selectCB) was moved
-  * to edit.c because of it's (wanted) generality.
+/* -------------------- circle select --------------------------------------------- */
 
-       XXX These callback functions are still dirty, because they call globals... 
-  */
-
-static void mesh_selectionCB__doSelectVert(void *userData, EditVert *eve, int x, int y, int index)
+static void mesh_circle_doSelectVert(void *userData, EditVert *eve, int x, int y, int index)
 {
-       struct { short select, mval[2]; float radius; } *data = userData;
+       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);
 
@@ -1515,87 +1619,88 @@ static void mesh_selectionCB__doSelectVert(void *userData, EditVert *eve, int x,
                eve->f = data->select?(eve->f|1):(eve->f&~1);
        }
 }
-static void mesh_selectionCB__doSelectEdge(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index)
+static void mesh_circle_doSelectEdge(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index)
 {
-       struct { short select, mval[2]; float radius; } *data = userData;
+       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);
        }
 }
-static void mesh_selectionCB__doSelectFace(void *userData, EditFace *efa, int x, int y, int index)
+static void mesh_circle_doSelectFace(void *userData, EditFace *efa, int x, int y, int index)
 {
-       struct { short select, mval[2]; float radius; } *data = userData;
+       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);
-       EditMesh *em= NULL; // XXX
        
        if (r<=data->radius) {
-               EM_select_face_fgon(em, efa, data->select);
+               EM_select_face_fgon(data->vc->em, efa, data->select);
        }
 }
 
-static void mesh_selectionCB(ViewContext *vc, int selecting, Object *editobj, short *mval, float rad)
+static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
 {
-       struct { short select, mval[2]; float radius; } data;
+       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
-
-       if(!G.obedit && (FACESEL_PAINT_TEST)) {
-               Object *ob= vc->obact;
+       Object *ob= vc->obact;
+       
+       if(vc->obedit==NULL && paint_facesel_test(ob)) {
                Mesh *me = ob?ob->data:NULL;
 
                if (me) {
                        em_vertoffs= me->totface+1;     /* max index array */
 
-                       bbsel= EM_init_backbuf_circle(vc->v3d, mval[0], mval[1], (short)(rad+1.0));
+                       bbsel= EM_init_backbuf_circle(vc, mval[0], mval[1], (short)(rad+1.0));
                        EM_backbuf_checkAndSelectTFaces(me, selecting==LEFTMOUSE);
                        EM_free_backbuf();
 
 // XXX                 object_tface_flags_changed(OBACT, 0);
                }
-
-               return;
        }
+       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));
+               vc->em= ((Mesh *)vc->obedit->data)->edit_mesh;
 
-       bbsel= EM_init_backbuf_circle(vc->v3d, mval[0], mval[1], (short)(rad+1.0));
-       
-       data.select = (selecting==LEFTMOUSE);
-       data.mval[0] = mval[0];
-       data.mval[1] = mval[1];
-       data.radius = rad;
+               data.select = selecting;
+               data.mval[0] = mval[0];
+               data.mval[1] = mval[1];
+               data.radius = rad;
 
-       if(vc->scene->selectmode & SCE_SELECT_VERTEX) {
-               if(bbsel) {
-                       EM_backbuf_checkAndSelectVerts(vc->em, selecting==LEFTMOUSE);
-               } else {
-                       mesh_foreachScreenVert(vc, mesh_selectionCB__doSelectVert, &data, 1);
+               if(ts->selectmode & SCE_SELECT_VERTEX) {
+                       if(bbsel) {
+                               EM_backbuf_checkAndSelectVerts(vc->em, selecting==LEFTMOUSE);
+                       } else {
+                               mesh_foreachScreenVert(vc, mesh_circle_doSelectVert, &data, 1);
+                       }
                }
-       }
 
-       if(vc->scene->selectmode & SCE_SELECT_EDGE) {
-               if (bbsel) {
-                       EM_backbuf_checkAndSelectEdges(vc->em, selecting==LEFTMOUSE);
-               } else {
-                       mesh_foreachScreenEdge(vc, mesh_selectionCB__doSelectEdge, &data, 0);
+               if(ts->selectmode & SCE_SELECT_EDGE) {
+                       if (bbsel) {
+                               EM_backbuf_checkAndSelectEdges(vc->em, selecting==LEFTMOUSE);
+                       } else {
+                               mesh_foreachScreenEdge(vc, mesh_circle_doSelectEdge, &data, 0);
+                       }
                }
-       }
-       
-       if(vc->scene->selectmode & SCE_SELECT_FACE) {
-               if(bbsel) {
-                       EM_backbuf_checkAndSelectFaces(vc->em, selecting==LEFTMOUSE);
-               } else {
-                       mesh_foreachScreenFace(vc, mesh_selectionCB__doSelectFace, &data);
+               
+               if(ts->selectmode & SCE_SELECT_FACE) {
+                       if(bbsel) {
+                               EM_backbuf_checkAndSelectFaces(vc->em, selecting==LEFTMOUSE);
+                       } else {
+                               mesh_foreachScreenFace(vc, mesh_circle_doSelectFace, &data);
+                       }
                }
-       }
 
-       EM_free_backbuf();
-       EM_selectmode_flush(vc->em);
+               EM_free_backbuf();
+               EM_selectmode_flush(vc->em);
+       }
 }
 
 
-static void nurbscurve_selectionCB__doSelect(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
+static void nurbscurve_circle_doSelect(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
 {
-       struct { short select, mval[2]; float radius; } *data = userData;
+       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);
 
@@ -1613,22 +1718,24 @@ static void nurbscurve_selectionCB__doSelect(void *userData, Nurb *nu, BPoint *b
                }
        }
 }
-static void nurbscurve_selectionCB(ViewContext *vc, int selecting, Object *editobj, short *mval, float rad)
+static void nurbscurve_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
 {
-       struct { short select, mval[2]; float radius; } data;
+       struct {ViewContext *vc; short select, mval[2]; float radius; } data;
 
-       data.select = (selecting==LEFTMOUSE);
+       /* set vc-> edit data */
+       
+       data.select = selecting;
        data.mval[0] = mval[0];
        data.mval[1] = mval[1];
        data.radius = rad;
 
-       nurbs_foreachScreenVert(vc, nurbscurve_selectionCB__doSelect, &data);
+       nurbs_foreachScreenVert(vc, nurbscurve_circle_doSelect, &data);
 }
 
 
-static void latticecurve_selectionCB__doSelect(void *userData, BPoint *bp, int x, int y)
+static void latticecurve_circle_doSelect(void *userData, BPoint *bp, int x, int y)
 {
-       struct { short select, mval[2]; float radius; } *data = userData;
+       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);
 
@@ -1636,81 +1743,106 @@ static void latticecurve_selectionCB__doSelect(void *userData, BPoint *bp, int x
                bp->f1 = data->select?(bp->f1|SELECT):(bp->f1&~SELECT);
        }
 }
-static void lattice_selectionCB(ViewContext *vc, int selecting, Object *editobj, short *mval, float rad)
+static void lattice_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
 {
-       struct { short select, mval[2]; float radius; } data;
+       struct {ViewContext *vc; short select, mval[2]; float radius; } data;
 
-       data.select = (selecting==LEFTMOUSE);
+       /* set vc-> edit data */
+       
+       data.select = selecting;
        data.mval[0] = mval[0];
        data.mval[1] = mval[1];
        data.radius = rad;
 
-       lattice_foreachScreenVert(vc, latticecurve_selectionCB__doSelect, &data);
+       lattice_foreachScreenVert(vc, latticecurve_circle_doSelect, &data);
 }
 
-/** Callbacks for selection in Editmode */
+/** Callbacks for circle selection in Editmode */
 
-void obedit_selectionCB(ViewContext *vc, short selecting, Object *editobj, short *mval, float rad) 
+static void obedit_circle_select(ViewContext *vc, short selecting, short *mval, float rad) 
 {
-       switch(editobj->type) {         
+       switch(vc->obedit->type) {              
        case OB_MESH:
-               mesh_selectionCB(vc, selecting, editobj, mval, rad);
+               mesh_circle_select(vc, selecting, mval, rad);
                break;
        case OB_CURVE:
        case OB_SURF:
-               nurbscurve_selectionCB(vc, selecting, editobj, mval, rad);
+               nurbscurve_circle_select(vc, selecting, mval, rad);
                break;
        case OB_LATTICE:
-               lattice_selectionCB(vc, selecting, editobj, mval, rad);
+               lattice_circle_select(vc, selecting, mval, rad);
                break;
        default:
                return;
        }
-
-//     draw_sel_circle(0, 0, 0, 0, 0); /* signal */
-//     force_draw(0);
 }
 
 /* not a real operator, only for circle test */
-static int view3d_circle_select(bContext *C, wmOperator *op)
+static int view3d_circle_select_exec(bContext *C, wmOperator *op)
 {
        ScrArea *sa= CTX_wm_area(C);
        ARegion *ar= CTX_wm_region(C);
        Scene *scene= CTX_data_scene(C);
+       Object *obact= CTX_data_active_object(C);
        View3D *v3d= sa->spacedata.first;
-       Base *base;
-
        int x= RNA_int_get(op->ptr, "x");
        int y= RNA_int_get(op->ptr, "y");
        int radius= RNA_int_get(op->ptr, "radius");
        
-       for(base= FIRSTBASE; base; base= base->next) {
-               if(base->lay & v3d->lay) {
-                       project_short(ar, v3d, base->object->obmat[3], &base->sx);
-                       if(base->sx!=IS_CLIPPED) {
-                               int dx= base->sx-x;
-                               int dy= base->sy-y;
-                               if( dx*dx + dy*dy < radius*radius)
-                                       ED_base_object_select(base, BA_SELECT);
+       if(CTX_data_edit_object(C) || (obact && obact->mode & OB_MODE_PARTICLE_EDIT)) {
+               ViewContext vc;
+               short mval[2], selecting;
+               
+               view3d_operator_needs_opengl(C);
+               
+               view3d_set_viewcontext(C, &vc);
+               mval[0]= x;
+               mval[1]= y;
+               selecting= LEFTMOUSE==RNA_int_get(op->ptr, "event_type"); // XXX solve
+
+               if(CTX_data_edit_object(C)) {
+                       obedit_circle_select(&vc, selecting, mval, (float)radius);
+                       WM_event_add_notifier(C, NC_OBJECT|ND_GEOM_SELECT, obact);
+               }
+               else
+                       return PE_circle_select(C, selecting, mval, (float)radius);
+       }
+       else {
+               Base *base;
+               
+               for(base= FIRSTBASE; base; base= base->next) {
+                       if(base->lay & v3d->lay) {
+                               project_short(ar, base->object->obmat[3], &base->sx);
+                               if(base->sx!=IS_CLIPPED) {
+                                       int dx= base->sx-x;
+                                       int dy= base->sy-y;
+                                       if( dx*dx + dy*dy < radius*radius)
+                                               ED_base_object_select(base, BA_SELECT);
+                               }
                        }
                }
+               
+               WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
        }
-       WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
-       return 0;
+       
+       return OPERATOR_FINISHED;
 }
 
-void VIEW3D_OT_circle_select(wmOperatorType *ot)
+void VIEW3D_OT_select_circle(wmOperatorType *ot)
 {
        ot->name= "Circle Select";
-       ot->idname= "VIEW3D_OT_circle_select";
+       ot->idname= "VIEW3D_OT_select_circle";
        
        ot->invoke= WM_gesture_circle_invoke;
        ot->modal= WM_gesture_circle_modal;
-       ot->exec= view3d_circle_select;
+       ot->exec= view3d_circle_select_exec;
        ot->poll= ED_operator_view3d_active;
        
-       RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
-       RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
        
+       RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
+       RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
 }