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 35b9df6..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
 
 
-static void view3d_set_viewcontext(bContext *C, ViewContext *vc)
+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= (View3D *)CTX_wm_space_data(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 *************** */
@@ -309,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;
@@ -328,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);
                        }
                }
@@ -396,6 +440,7 @@ 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;
        
@@ -414,14 +459,14 @@ static void do_lasso_select_mesh(ViewContext *vc, short mcords[][2], short moves
 
        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;
@@ -433,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 {
@@ -571,10 +616,10 @@ static void do_lasso_select_armature(ViewContext *vc, short mcords[][2], short m
 
                VECCOPY(vec, ebone->head);
                Mat4MulVecfl(vc->obedit->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco1);
+               project_short(vc->ar, vec, sco1);
                VECCOPY(vec, ebone->tail);
                Mat4MulVecfl(vc->obedit->obmat, vec);
-               project_short(vc->ar, vc->v3d, vec, sco2);
+               project_short(vc->ar, vec, sco2);
                
                didpoint= 0;
                if(lasso_inside(mcords, moves, sco1[0], sco1[1])) {
@@ -593,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)
@@ -648,15 +692,17 @@ 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)
 {
+       Object *ob = CTX_data_active_object(C);
+
        if(vc->obedit==NULL) {
-               if(FACESEL_PAINT_TEST)
+               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);
        }
@@ -673,12 +719,6 @@ void view3d_lasso_select(ViewContext *vc, short mcords[][2], short moves, short
        
 }
 
-static EnumPropertyItem lasso_select_types[] = {
-       {0, "SELECT", "Select", ""},
-       {1, "DESELECT", "Deselect", ""},
-       {0, NULL, NULL, NULL}
-};
-
 
 /* lasso operator gives properties, but since old code works
    with short array we convert */
@@ -699,34 +739,35 @@ static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
        }
        RNA_END;
        
-       /* setup view context for argument to callbacks */
-       view3d_set_viewcontext(C, &vc);
-       
-       select= RNA_enum_is_equal(op->ptr, "type", "SELECT");
-       view3d_lasso_select(&vc, mcords, i, select);
-       
-       return OPERATOR_FINISHED;
+       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_lasso_select(wmOperatorType *ot)
+void VIEW3D_OT_select_lasso(wmOperatorType *ot)
 {
-       PropertyRNA *prop;
-       
        ot->name= "Lasso Select";
-       ot->idname= "VIEW3D_OT_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;
        
-       prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
-       RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
        
-       prop = RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
-       RNA_def_property_enum_items(prop, lasso_select_types);
-
+       RNA_def_collection_runtime(ot->srna, "path", &RNA_OperatorMousePath, "Path", "");
+       RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items.");
 }
 
 
@@ -784,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;
@@ -827,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;
@@ -919,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];
@@ -945,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;
@@ -1062,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 */
@@ -1098,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);
@@ -1120,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 ************************************** */
@@ -1240,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;
@@ -1250,14 +1287,14 @@ static void do_mesh_box_select(ViewContext *vc, rcti *rect, int select)
 
        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;
@@ -1269,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 {
@@ -1289,6 +1326,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        ScrArea *sa= CTX_wm_area(C);
        View3D *v3d= sa->spacedata.first;
        Object *obedit= CTX_data_edit_object(C);
+       Object *obact= CTX_data_active_object(C);
        rcti rect;
        Base *base;
        MetaElem *ml;
@@ -1307,13 +1345,12 @@ 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(obedit==NULL && (FACESEL_PAINT_TEST)) {
+       if(obedit==NULL && (paint_facesel_test(OBACT))) {
 // XXX         face_borderselect();
                return OPERATOR_FINISHED;
        }
-       else if(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(obedit) {
@@ -1327,12 +1364,12 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                }
                else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
                        do_nurbs_box_select(&vc, &rect, val==LEFTMOUSE);
-//                     allqueue(REDRAWVIEW3D, 0);
                }
                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++) {
@@ -1405,6 +1442,7 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
                                }
                        }
                        
+                       ED_armature_sync_selection(arm->edbo);
                }
                else if(obedit->type==OB_LATTICE) {
                        do_lattice_box_select(&vc, &rect, val==LEFTMOUSE);
@@ -1412,13 +1450,14 @@ static int view3d_borderselect_exec(bContext *C, wmOperator *op)
        }
        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;
@@ -1427,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
@@ -1451,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;
@@ -1484,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;
@@ -1512,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 ****** */
@@ -1528,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";
@@ -1560,9 +1598,12 @@ 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.");
 }
 
 
@@ -1570,7 +1611,7 @@ void VIEW3D_OT_select(wmOperatorType *ot)
 
 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);
 
@@ -1580,7 +1621,7 @@ static void mesh_circle_doSelectVert(void *userData, EditVert *eve, int x, int y
 }
 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);
@@ -1588,22 +1629,22 @@ static void mesh_circle_doSelectEdge(void *userData, EditEdge *eed, int x0, int
 }
 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_circle_select(ViewContext *vc, int selecting, short *mval, float rad)
 {
+       ToolSettings *ts= vc->scene->toolsettings;
        int bbsel;
+       Object *ob= vc->obact;
        
-       if(vc->obedit==NULL && (FACESEL_PAINT_TEST)) {
-               Object *ob= vc->obact;
+       if(vc->obedit==NULL && paint_facesel_test(ob)) {
                Mesh *me = ob?ob->data:NULL;
 
                if (me) {
@@ -1617,7 +1658,7 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                }
        }
        else {
-               struct { short select, mval[2]; float radius; } data;
+               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;
@@ -1627,7 +1668,7 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                data.mval[1] = mval[1];
                data.radius = rad;
 
-               if(vc->scene->selectmode & SCE_SELECT_VERTEX) {
+               if(ts->selectmode & SCE_SELECT_VERTEX) {
                        if(bbsel) {
                                EM_backbuf_checkAndSelectVerts(vc->em, selecting==LEFTMOUSE);
                        } else {
@@ -1635,7 +1676,7 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                        }
                }
 
-               if(vc->scene->selectmode & SCE_SELECT_EDGE) {
+               if(ts->selectmode & SCE_SELECT_EDGE) {
                        if (bbsel) {
                                EM_backbuf_checkAndSelectEdges(vc->em, selecting==LEFTMOUSE);
                        } else {
@@ -1643,7 +1684,7 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
                        }
                }
                
-               if(vc->scene->selectmode & SCE_SELECT_FACE) {
+               if(ts->selectmode & SCE_SELECT_FACE) {
                        if(bbsel) {
                                EM_backbuf_checkAndSelectFaces(vc->em, selecting==LEFTMOUSE);
                        } else {
@@ -1659,7 +1700,7 @@ static void mesh_circle_select(ViewContext *vc, int selecting, short *mval, floa
 
 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);
 
@@ -1679,7 +1720,7 @@ static void nurbscurve_circle_doSelect(void *userData, Nurb *nu, BPoint *bp, Bez
 }
 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;
 
        /* set vc-> edit data */
        
@@ -1694,7 +1735,7 @@ static void nurbscurve_circle_select(ViewContext *vc, int selecting, short *mval
 
 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);
 
@@ -1704,7 +1745,7 @@ static void latticecurve_circle_doSelect(void *userData, BPoint *bp, int x, int
 }
 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;
 
        /* set vc-> edit data */
        
@@ -1742,27 +1783,36 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
        ScrArea *sa= CTX_wm_area(C);
        ARegion *ar= CTX_wm_region(C);
        Scene *scene= CTX_data_scene(C);
+       Object *obact= CTX_data_active_object(C);
        View3D *v3d= sa->spacedata.first;
        int x= RNA_int_get(op->ptr, "x");
        int y= RNA_int_get(op->ptr, "y");
        int radius= RNA_int_get(op->ptr, "radius");
        
-       if(CTX_data_edit_object(C)) {
+       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
-               obedit_circle_select(&vc, selecting, mval, (float)radius);
+
+               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, v3d, base->object->obmat[3], &base->sx);
+                               project_short(ar, base->object->obmat[3], &base->sx);
                                if(base->sx!=IS_CLIPPED) {
                                        int dx= base->sx-x;
                                        int dy= base->sy-y;
@@ -1778,19 +1828,21 @@ static int view3d_circle_select_exec(bContext *C, wmOperator *op)
        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_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);
-       RNA_def_property(ot->srna, "event_type", 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);
 }