Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_view3d / view3d_select.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_select.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36 #include <assert.h>
37
38 #include "DNA_action_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_meta_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_tracking_types.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "BLI_math.h"
51 #include "BLI_lasso.h"
52 #include "BLI_rect.h"
53 #include "BLI_rand.h"
54 #include "BLI_linklist.h"
55 #include "BLI_listbase.h"
56 #include "BLI_string.h"
57 #include "BLI_utildefines.h"
58
59 /* vertex box select */
60 #include "IMB_imbuf_types.h"
61 #include "IMB_imbuf.h"
62 #include "BKE_global.h"
63
64 #include "BKE_context.h"
65 #include "BKE_paint.h"
66 #include "BKE_armature.h"
67 #include "BKE_tessmesh.h"
68 #include "BKE_movieclip.h"
69 #include "BKE_object.h"
70 #include "BKE_tracking.h"
71
72
73 #include "BIF_gl.h"
74 #include "BIF_glutil.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81 #include "RNA_enum_types.h"
82
83 #include "ED_armature.h"
84 #include "ED_curve.h"
85 #include "ED_particle.h"
86 #include "ED_mesh.h"
87 #include "ED_object.h"
88 #include "ED_screen.h"
89 #include "ED_mball.h"
90
91 #include "UI_interface.h"
92 #include "UI_resources.h"
93
94 #include "view3d_intern.h"  // own include
95
96 // TODO: should return whether there is valid context to continue
97 void view3d_set_viewcontext(bContext *C, ViewContext *vc)
98 {
99         memset(vc, 0, sizeof(ViewContext));
100         vc->ar = CTX_wm_region(C);
101         vc->scene = CTX_data_scene(C);
102         vc->v3d = CTX_wm_view3d(C);
103         vc->rv3d = CTX_wm_region_view3d(C);
104         vc->obact = CTX_data_active_object(C);
105         vc->obedit = CTX_data_edit_object(C);
106 }
107
108 int view3d_get_view_aligned_coordinate(ViewContext *vc, float fp[3], const int mval[2], const short do_fallback)
109 {
110         float dvec[3];
111         int mval_cpy[2];
112
113         mval_cpy[0] = mval[0];
114         mval_cpy[1] = mval[1];
115
116         project_int_noclip(vc->ar, fp, mval_cpy);
117
118         initgrabz(vc->rv3d, fp[0], fp[1], fp[2]);
119
120         if (mval_cpy[0] != IS_CLIPPED) {
121                 float mval_f[2];
122                 VECSUB2D(mval_f, mval_cpy, mval);
123                 ED_view3d_win_to_delta(vc->ar, mval_f, dvec);
124                 sub_v3_v3(fp, dvec);
125
126                 return TRUE;
127         }
128         else {
129                 /* fallback to the view center */
130                 if (do_fallback) {
131                         negate_v3_v3(fp, vc->rv3d->ofs);
132                         return view3d_get_view_aligned_coordinate(vc, fp, mval, FALSE);
133                 }
134                 else {
135                         return FALSE;
136                 }
137         }
138 }
139
140 /*
141  * ob == NULL if you want global matrices
142  * */
143 void view3d_get_transformation(const ARegion *ar, RegionView3D *rv3d, Object *ob, bglMats *mats)
144 {
145         float cpy[4][4];
146         int i, j;
147
148         if (ob) {
149                 mult_m4_m4m4(cpy, rv3d->viewmat, ob->obmat);
150         }
151         else {
152                 copy_m4_m4(cpy, rv3d->viewmat);
153         }
154
155         for (i = 0; i < 4; ++i) {
156                 for (j = 0; j < 4; ++j) {
157                         mats->projection[i * 4 + j] = rv3d->winmat[i][j];
158                         mats->modelview[i * 4 + j] = cpy[i][j];
159                 }
160         }
161
162         mats->viewport[0] = ar->winrct.xmin;
163         mats->viewport[1] = ar->winrct.ymin;
164         mats->viewport[2] = ar->winx;
165         mats->viewport[3] = ar->winy;   
166 }
167
168 /* ********************** view3d_select: selection manipulations ********************* */
169
170 /* local prototypes */
171
172 static void edbm_backbuf_check_and_select_verts(BMEditMesh *em, int select)
173 {
174         BMVert *eve;
175         BMIter iter;
176         int index = bm_wireoffs;
177
178         eve = BM_iter_new(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
179         for (; eve; eve = BM_iter_step(&iter), index++) {
180                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
181                         if (EDBM_backbuf_check(index)) {
182                                 BM_vert_select_set(em->bm, eve, select);
183                         }
184                 }
185         }
186 }
187
188 static void edbm_backbuf_check_and_select_edges(BMEditMesh *em, int select)
189 {
190         BMEdge *eed;
191         BMIter iter;
192         int index = bm_solidoffs;
193
194         eed = BM_iter_new(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
195         for (; eed; eed = BM_iter_step(&iter), index++) {
196                 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
197                         if (EDBM_backbuf_check(index)) {
198                                 BM_edge_select_set(em->bm, eed, select);
199                         }
200                 }
201         }
202 }
203
204 static void edbm_backbuf_check_and_select_faces(BMEditMesh *em, int select)
205 {
206         BMFace *efa;
207         BMIter iter;
208         int index = 1;
209
210         efa = BM_iter_new(&iter, em->bm, BM_FACES_OF_MESH, NULL);
211         for (; efa; efa = BM_iter_step(&iter), index++) {
212                 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
213                         if (EDBM_backbuf_check(index)) {
214                                 BM_face_select_set(em->bm, efa, select);
215                         }
216                 }
217         }
218 }
219
220
221 /* object mode, EM_ prefix is confusing here, rename? */
222 static void edbm_backbuf_check_and_select_verts_obmode(Mesh *me, int select)
223 {
224         MVert *mv = me->mvert;
225         int a;
226
227         if (mv) {
228                 for (a = 1; a <= me->totvert; a++, mv++) {
229                         if (EDBM_backbuf_check(a)) {
230                                 if (!(mv->flag & ME_HIDE)) {
231                                         mv->flag = select ? (mv->flag | SELECT) : (mv->flag & ~SELECT);
232                                 }
233                         }
234                 }
235         }
236 }
237 /* object mode, EM_ prefix is confusing here, rename? */
238
239 static void edbm_backbuf_check_and_select_tfaces(Mesh *me, int select)
240 {
241         MPoly *mpoly = me->mpoly;
242         int a;
243
244         if (mpoly) {
245                 for (a = 1; a <= me->totpoly; a++, mpoly++) {
246                         if (EDBM_backbuf_check(a)) {
247                                 mpoly->flag = select ? (mpoly->flag | ME_FACE_SEL) : (mpoly->flag & ~ME_FACE_SEL);
248                         }
249                 }
250         }
251 }
252
253 /* *********************** GESTURE AND LASSO ******************* */
254
255 typedef struct LassoSelectUserData {
256         ViewContext *vc;
257         rcti *rect;
258         int (*mcords)[2], moves, select, pass, done;
259 } LassoSelectUserData;
260
261 static int view3d_selectable_data(bContext *C)
262 {
263         Object *ob = CTX_data_active_object(C);
264
265         if (!ED_operator_region_view3d_active(C))
266                 return 0;
267
268         if (ob) {
269                 if (ob->mode & OB_MODE_EDIT) {
270                         if (ob->type == OB_FONT) {
271                                 return 0;
272                         }
273                 }
274                 else {
275                         if (ob->mode & OB_MODE_SCULPT) {
276                                 return 0;
277                         }
278                         if ((ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) &&
279                             !paint_facesel_test(ob) && !paint_vertsel_test(ob))
280                         {
281                                 return 0;
282                         }
283                 }
284         }
285
286         return 1;
287 }
288
289
290 /* helper also for borderselect */
291 static int edge_fully_inside_rect(rcti *rect, short x1, short y1, short x2, short y2)
292 {
293         return BLI_in_rcti(rect, x1, y1) && BLI_in_rcti(rect, x2, y2);
294 }
295
296 static int edge_inside_rect(rcti *rect, short x1, short y1, short x2, short y2)
297 {
298         int d1, d2, d3, d4;
299         
300         /* check points in rect */
301         if (edge_fully_inside_rect(rect, x1, y1, x2, y2)) return 1;
302         
303         /* check points completely out rect */
304         if (x1 < rect->xmin && x2 < rect->xmin) return 0;
305         if (x1 > rect->xmax && x2 > rect->xmax) return 0;
306         if (y1 < rect->ymin && y2 < rect->ymin) return 0;
307         if (y1 > rect->ymax && y2 > rect->ymax) return 0;
308         
309         /* simple check lines intersecting. */
310         d1 = (y1 - y2) * (x1 - rect->xmin) + (x2 - x1) * (y1 - rect->ymin);
311         d2 = (y1 - y2) * (x1 - rect->xmin) + (x2 - x1) * (y1 - rect->ymax);
312         d3 = (y1 - y2) * (x1 - rect->xmax) + (x2 - x1) * (y1 - rect->ymax);
313         d4 = (y1 - y2) * (x1 - rect->xmax) + (x2 - x1) * (y1 - rect->ymin);
314         
315         if (d1 < 0 && d2 < 0 && d3 < 0 && d4 < 0) return 0;
316         if (d1 > 0 && d2 > 0 && d3 > 0 && d4 > 0) return 0;
317         
318         return 1;
319 }
320
321
322 #define MOVES_GESTURE 50
323 #define MOVES_LASSO 500
324
325
326 /* warning; lasso select with backbuffer-check draws in backbuf with persp(PERSP_WIN) 
327  * and returns with persp(PERSP_VIEW). After lasso select backbuf is not OK
328  */
329 static void do_lasso_select_pose(ViewContext *vc, Object *ob, int mcords[][2], short moves, short select)
330 {
331         bPoseChannel *pchan;
332         float vec[3];
333         int sco1[2], sco2[2];
334         bArmature *arm = ob->data;
335         
336         if (ob->type != OB_ARMATURE || ob->pose == NULL) return;
337
338         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
339                 if (PBONE_VISIBLE(arm, pchan->bone) && (pchan->bone->flag & BONE_UNSELECTABLE) == 0) {
340                         mul_v3_m4v3(vec, ob->obmat, pchan->pose_head);
341                         project_int(vc->ar, vec, sco1);
342                         mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
343                         project_int(vc->ar, vec, sco2);
344                         
345                         if (BLI_lasso_is_edge_inside(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1], IS_CLIPPED)) {
346                                 if (select) pchan->bone->flag |= BONE_SELECTED;
347                                 else pchan->bone->flag &= ~BONE_SELECTED;
348                         }
349                 }
350         }
351 }
352
353 static void object_deselect_all_visible(Scene *scene, View3D *v3d)
354 {
355         Base *base;
356
357         for (base = scene->base.first; base; base = base->next) {
358                 if (BASE_SELECTABLE(v3d, base)) {
359                         ED_base_object_select(base, BA_DESELECT);
360                 }
361         }
362 }
363
364 static void do_lasso_select_objects(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
365 {
366         Base *base;
367         
368         if (extend == 0 && select)
369                 object_deselect_all_visible(vc->scene, vc->v3d);
370
371         for (base = vc->scene->base.first; base; base = base->next) {
372                 if (BASE_SELECTABLE(vc->v3d, base)) { /* use this to avoid un-needed lasso lookups */
373                         project_short(vc->ar, base->object->obmat[3], &base->sx);
374                         if (BLI_lasso_is_point_inside(mcords, moves, base->sx, base->sy, IS_CLIPPED)) {
375                                 
376                                 if (select) ED_base_object_select(base, BA_SELECT);
377                                 else ED_base_object_select(base, BA_DESELECT);
378                                 base->object->flag = base->flag;
379                         }
380                         if (base->object->mode & OB_MODE_POSE) {
381                                 do_lasso_select_pose(vc, base->object, mcords, moves, select);
382                         }
383                 }
384         }
385 }
386
387 static void do_lasso_select_mesh__doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
388 {
389         LassoSelectUserData *data = userData;
390
391         if (BLI_in_rcti(data->rect, x, y) &&
392             BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED))
393         {
394                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
395         }
396 }
397 static void do_lasso_select_mesh__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
398 {
399         LassoSelectUserData *data = userData;
400
401         if (EDBM_backbuf_check(bm_solidoffs + index)) {
402                 if (data->pass == 0) {
403                         if (edge_fully_inside_rect(data->rect, x0, y0, x1, y1)  &&
404                             BLI_lasso_is_point_inside(data->mcords, data->moves, x0, y0, IS_CLIPPED) &&
405                             BLI_lasso_is_point_inside(data->mcords, data->moves, x1, y1, IS_CLIPPED))
406                         {
407                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
408                                 data->done = 1;
409                         }
410                 }
411                 else {
412                         if (BLI_lasso_is_edge_inside(data->mcords, data->moves, x0, y0, x1, y1, IS_CLIPPED)) {
413                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
414                         }
415                 }
416         }
417 }
418 static void do_lasso_select_mesh__doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
419 {
420         LassoSelectUserData *data = userData;
421
422         if (BLI_in_rcti(data->rect, x, y) &&
423             BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED))
424         {
425                 BM_face_select_set(data->vc->em->bm, efa, data->select);
426         }
427 }
428
429 static void do_lasso_select_mesh(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
430 {
431         LassoSelectUserData data;
432         ToolSettings *ts = vc->scene->toolsettings;
433         rcti rect;
434         int bbsel;
435         
436         BLI_lasso_boundbox(&rect, mcords, moves);
437         
438         /* set editmesh */
439         vc->em = BMEdit_FromObject(vc->obedit);
440
441         data.vc = vc;
442         data.rect = &rect;
443         data.mcords = mcords;
444         data.moves = moves;
445         data.select = select;
446         data.done = 0;
447         data.pass = 0;
448
449         if (extend == 0 && select)
450                 EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
451
452         /* for non zbuf projections, don't change the GL state */
453         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
454
455         glLoadMatrixf(vc->rv3d->viewmat);
456         bbsel = EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
457         
458         if (ts->selectmode & SCE_SELECT_VERTEX) {
459                 if (bbsel) {
460                         edbm_backbuf_check_and_select_verts(vc->em, select);
461                 }
462                 else {
463                         mesh_foreachScreenVert(vc, do_lasso_select_mesh__doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
464                 }
465         }
466         if (ts->selectmode & SCE_SELECT_EDGE) {
467                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
468                 data.pass = 0;
469                 mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
470
471                 if (data.done == 0) {
472                         data.pass = 1;
473                         mesh_foreachScreenEdge(vc, do_lasso_select_mesh__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
474                 }
475         }
476         
477         if (ts->selectmode & SCE_SELECT_FACE) {
478                 if (bbsel) {
479                         edbm_backbuf_check_and_select_faces(vc->em, select);
480                 }
481                 else {
482                         mesh_foreachScreenFace(vc, do_lasso_select_mesh__doSelectFace, &data);
483                 }
484         }
485         
486         EDBM_backbuf_free();
487         EDBM_selectmode_flush(vc->em);
488 }
489
490 static void do_lasso_select_curve__doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
491 {
492         LassoSelectUserData *data = userData;
493         Object *obedit = data->vc->obedit;
494         Curve *cu = (Curve *)obedit->data;
495
496         if (BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED)) {
497                 if (bp) {
498                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
499                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
500                 }
501                 else {
502                         if (cu->drawflag & CU_HIDE_HANDLES) {
503                                 /* can only be beztindex==0 here since handles are hidden */
504                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
505                         }
506                         else {
507                                 if (beztindex == 0) {
508                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
509                                 }
510                                 else if (beztindex == 1) {
511                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
512                                 }
513                                 else {
514                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
515                                 }
516                         }
517
518                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
519                 }
520         }
521 }
522
523 static void do_lasso_select_curve(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
524 {
525         LassoSelectUserData data;
526
527         /* set vc->editnurb */
528         data.vc = vc;
529         data.mcords = mcords;
530         data.moves = moves;
531         data.select = select;
532
533         if (extend == 0 && select)
534                 CU_deselect_all(vc->obedit);
535
536         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
537         nurbs_foreachScreenVert(vc, do_lasso_select_curve__doSelect, &data);
538 }
539
540 static void do_lasso_select_lattice__doSelect(void *userData, BPoint *bp, int x, int y)
541 {
542         LassoSelectUserData *data = userData;
543
544         if (BLI_lasso_is_point_inside(data->mcords, data->moves, x, y, IS_CLIPPED)) {
545                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
546         }
547 }
548 static void do_lasso_select_lattice(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
549 {
550         LassoSelectUserData data;
551
552         /* set editdata in vc */
553         data.mcords = mcords;
554         data.moves = moves;
555         data.select = select;
556
557         if (extend == 0 && select)
558                 ED_setflagsLatt(vc->obedit, 0);
559
560         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
561         lattice_foreachScreenVert(vc, do_lasso_select_lattice__doSelect, &data);
562 }
563
564 static void do_lasso_select_armature(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
565 {
566         bArmature *arm = vc->obedit->data;
567         EditBone *ebone;
568         float vec[3];
569         short sco1[2], sco2[2], didpoint;
570         int change = FALSE;
571
572         if (extend == 0 && select)
573                 ED_armature_deselect_all_visible(vc->obedit);
574
575         /* set editdata in vc */
576         
577         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
578                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_UNSELECTABLE) == 0) {
579                         mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
580                         project_short(vc->ar, vec, sco1);
581                         mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
582                         project_short(vc->ar, vec, sco2);
583                         
584                         didpoint = 0;
585                         if (BLI_lasso_is_point_inside(mcords, moves, sco1[0], sco1[1], IS_CLIPPED)) {
586                                 if (select) ebone->flag |= BONE_ROOTSEL;
587                                 else ebone->flag &= ~BONE_ROOTSEL;
588                                 didpoint = 1;
589                                 change = TRUE;
590                         }
591                         if (BLI_lasso_is_point_inside(mcords, moves, sco2[0], sco2[1], IS_CLIPPED)) {
592                                 if (select) ebone->flag |= BONE_TIPSEL;
593                                 else ebone->flag &= ~BONE_TIPSEL;
594                                 didpoint = 1;
595                                 change = TRUE;
596                         }
597                         /* if one of points selected, we skip the bone itself */
598                         if (didpoint == 0 &&
599                             BLI_lasso_is_edge_inside(mcords, moves, sco1[0], sco1[1], sco2[0], sco2[1], IS_CLIPPED))
600                         {
601                                 if (select) ebone->flag |= BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED;
602                                 else ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
603                                 change = TRUE;
604                         }
605                 }
606         }
607         
608         if (change) {
609                 ED_armature_sync_selection(arm->edbo);
610                 ED_armature_validate_active(arm);
611                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obedit);
612         }
613 }
614
615
616
617
618 static void do_lasso_select_meta(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
619 {
620         MetaBall *mb = (MetaBall *)vc->obedit->data;
621         MetaElem *ml;
622         float vec[3];
623         short sco[2];
624
625         if (extend == 0 && select) {
626                 for (ml = mb->editelems->first; ml; ml = ml->next) {
627                         ml->flag &= ~SELECT;
628                 }
629         }
630
631         for (ml = mb->editelems->first; ml; ml = ml->next) {
632                 
633                 mul_v3_m4v3(vec, vc->obedit->obmat, &ml->x);
634                 project_short(vc->ar, vec, sco);
635
636                 if (BLI_lasso_is_point_inside(mcords, moves, sco[0], sco[1], IS_CLIPPED)) {
637                         if (select) ml->flag |= SELECT;
638                         else ml->flag &= ~SELECT;
639                 }
640         }
641 }
642
643 int do_paintvert_box_select(ViewContext *vc, rcti *rect, int select, int extend)
644 {
645         Mesh *me;
646         MVert *mvert;
647         struct ImBuf *ibuf;
648         unsigned int *rt;
649         int a, index;
650         char *selar;
651         int sx = rect->xmax - rect->xmin + 1;
652         int sy = rect->ymax - rect->ymin + 1;
653
654         me = vc->obact->data;
655
656         if (me == NULL || me->totvert == 0 || sx * sy <= 0)
657                 return OPERATOR_CANCELLED;
658
659         selar = MEM_callocN(me->totvert + 1, "selar");
660
661         if (extend == 0 && select)
662                 paintvert_deselect_all_visible(vc->obact, SEL_DESELECT, FALSE);
663
664         view3d_validate_backbuf(vc);
665
666         ibuf = IMB_allocImBuf(sx, sy, 32, IB_rect);
667         rt = ibuf->rect;
668         glReadPixels(rect->xmin + vc->ar->winrct.xmin,  rect->ymin + vc->ar->winrct.ymin, sx, sy, GL_RGBA, GL_UNSIGNED_BYTE,  ibuf->rect);
669         if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
670
671         a = sx * sy;
672         while (a--) {
673                 if (*rt) {
674                         index = WM_framebuffer_to_index(*rt);
675                         if (index <= me->totvert) selar[index] = 1;
676                 }
677                 rt++;
678         }
679
680         mvert = me->mvert;
681         for (a = 1; a <= me->totvert; a++, mvert++) {
682                 if (selar[a]) {
683                         if ((mvert->flag & ME_HIDE) == 0) {
684                                 if (select) mvert->flag |=  SELECT;
685                                 else        mvert->flag &= ~SELECT;
686                         }
687                 }
688         }
689
690         IMB_freeImBuf(ibuf);
691         MEM_freeN(selar);
692
693 #ifdef __APPLE__        
694         glReadBuffer(GL_BACK);
695 #endif
696
697         paintvert_flush_flags(vc->obact);
698
699         return OPERATOR_FINISHED;
700 }
701
702 static void do_lasso_select_paintvert(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
703 {
704         Object *ob = vc->obact;
705         Mesh *me = ob ? ob->data : NULL;
706         rcti rect;
707
708         if (me == NULL || me->totvert == 0)
709                 return;
710
711         if (extend == 0 && select)
712                 paintvert_deselect_all_visible(ob, SEL_DESELECT, FALSE);  /* flush selection at the end */
713         bm_vertoffs = me->totvert + 1; /* max index array */
714
715         BLI_lasso_boundbox(&rect, mcords, moves);
716         EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
717
718         edbm_backbuf_check_and_select_verts_obmode(me, select);
719
720         EDBM_backbuf_free();
721
722         paintvert_flush_flags(ob);
723 }
724 static void do_lasso_select_paintface(ViewContext *vc, int mcords[][2], short moves, short extend, short select)
725 {
726         Object *ob = vc->obact;
727         Mesh *me = ob ? ob->data : NULL;
728         rcti rect;
729
730         if (me == NULL || me->totpoly == 0)
731                 return;
732
733         if (extend == 0 && select)
734                 paintface_deselect_all_visible(ob, SEL_DESELECT, FALSE);  /* flush selection at the end */
735
736         bm_vertoffs = me->totpoly + 1; /* max index array */
737
738         BLI_lasso_boundbox(&rect, mcords, moves);
739         EDBM_backbuf_border_mask_init(vc, mcords, moves, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
740         
741         edbm_backbuf_check_and_select_tfaces(me, select);
742
743         EDBM_backbuf_free();
744
745         paintface_flush_flags(ob);
746 }
747
748 #if 0
749 static void do_lasso_select_node(int mcords[][2], short moves, short select)
750 {
751         SpaceNode *snode = sa->spacedata.first;
752         
753         bNode *node;
754         rcti rect;
755         short node_cent[2];
756         float node_centf[2];
757         
758         BLI_lasso_boundbox(&rect, mcords, moves);
759         
760         /* store selection in temp test flag */
761         for (node = snode->edittree->nodes.first; node; node = node->next) {
762                 
763                 node_centf[0] = (node->totr.xmin + node->totr.xmax) / 2;
764                 node_centf[1] = (node->totr.ymin + node->totr.ymax) / 2;
765                 
766                 ipoco_to_areaco_noclip(G.v2d, node_centf, node_cent);
767                 if (BLI_in_rcti(&rect, node_cent[0], node_cent[1]) && BLI_lasso_is_point_inside(mcords, moves, node_cent[0], node_cent[1])) {
768                         if (select) {
769                                 node->flag |= SELECT;
770                         }
771                         else {
772                                 node->flag &= ~SELECT;
773                         }
774                 }
775         }
776         BIF_undo_push("Lasso select nodes");
777 }
778 #endif
779
780 static void view3d_lasso_select(bContext *C, ViewContext *vc, int mcords[][2], short moves, short extend, short select)
781 {
782         Object *ob = CTX_data_active_object(C);
783
784         if (vc->obedit == NULL) { /* Object Mode */
785                 if (paint_facesel_test(ob))
786                         do_lasso_select_paintface(vc, mcords, moves, extend, select);
787                 else if (paint_vertsel_test(ob))
788                         do_lasso_select_paintvert(vc, mcords, moves, extend, select);
789                 else if (ob && ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
790                         ;
791                 else if (ob && ob->mode & OB_MODE_PARTICLE_EDIT)
792                         PE_lasso_select(C, mcords, moves, extend, select);
793                 else {
794                         do_lasso_select_objects(vc, mcords, moves, extend, select);
795                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
796                 }
797         }
798         else { /* Edit Mode */
799                 switch (vc->obedit->type) {
800                         case OB_MESH:
801                                 do_lasso_select_mesh(vc, mcords, moves, extend, select);
802                                 break;
803                         case OB_CURVE:
804                         case OB_SURF:
805                                 do_lasso_select_curve(vc, mcords, moves, extend, select);
806                                 break;
807                         case OB_LATTICE:
808                                 do_lasso_select_lattice(vc, mcords, moves, extend, select);
809                                 break;
810                         case OB_ARMATURE:
811                                 do_lasso_select_armature(vc, mcords, moves, extend, select);
812                                 break;
813                         case OB_MBALL:
814                                 do_lasso_select_meta(vc, mcords, moves, extend, select);
815                                 break;
816                         default:
817                                 assert(!"lasso select on incorrect object type");
818                 }
819
820                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc->obedit->data);
821         }
822 }
823
824
825 /* lasso operator gives properties, but since old code works
826  * with short array we convert */
827 static int view3d_lasso_select_exec(bContext *C, wmOperator *op)
828 {
829         ViewContext vc;
830         int i = 0;
831         int mcords[1024][2];
832
833         RNA_BEGIN (op->ptr, itemptr, "path")
834         {
835                 float loc[2];
836                 
837                 RNA_float_get_array(&itemptr, "loc", loc);
838                 mcords[i][0] = (int)loc[0];
839                 mcords[i][1] = (int)loc[1];
840                 i++;
841                 if (i >= 1024) break;
842         }
843         RNA_END;
844         
845         if (i > 1) {
846                 short extend, select;
847                 view3d_operator_needs_opengl(C);
848                 
849                 /* setup view context for argument to callbacks */
850                 view3d_set_viewcontext(C, &vc);
851                 
852                 extend = RNA_boolean_get(op->ptr, "extend");
853                 select = !RNA_boolean_get(op->ptr, "deselect");
854                 view3d_lasso_select(C, &vc, mcords, i, extend, select);
855                 
856                 return OPERATOR_FINISHED;
857         }
858         return OPERATOR_PASS_THROUGH;
859 }
860
861 void VIEW3D_OT_select_lasso(wmOperatorType *ot)
862 {
863         ot->name = "Lasso Select";
864         ot->description = "Select items using lasso selection";
865         ot->idname = "VIEW3D_OT_select_lasso";
866         
867         ot->invoke = WM_gesture_lasso_invoke;
868         ot->modal = WM_gesture_lasso_modal;
869         ot->exec = view3d_lasso_select_exec;
870         ot->poll = view3d_selectable_data;
871         ot->cancel = WM_gesture_lasso_cancel;
872         
873         /* flags */
874         ot->flag = OPTYPE_UNDO;
875         
876         RNA_def_collection_runtime(ot->srna, "path", &RNA_OperatorMousePath, "Path", "");
877         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
878         RNA_def_boolean(ot->srna, "extend", 1, "Extend", "Extend selection instead of deselecting everything first");
879 }
880
881
882 /* ************************************************* */
883
884 #if 0
885 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
886 static unsigned int samplerect(unsigned int *buf, int size, unsigned int dontdo)
887 {
888         Base *base;
889         unsigned int *bufmin, *bufmax;
890         int a, b, rc, tel, len, dirvec[4][2], maxob;
891         unsigned int retval = 0;
892         
893         base = LASTBASE;
894         if (base == 0) return 0;
895         maxob = base->selcol;
896
897         len = (size - 1) / 2;
898         rc = 0;
899
900         dirvec[0][0] = 1;
901         dirvec[0][1] = 0;
902         dirvec[1][0] = 0;
903         dirvec[1][1] = -size;
904         dirvec[2][0] = -1;
905         dirvec[2][1] = 0;
906         dirvec[3][0] = 0;
907         dirvec[3][1] = size;
908
909         bufmin = buf;
910         bufmax = buf + size * size;
911         buf += len * size + len;
912
913         for (tel = 1; tel <= size; tel++) {
914
915                 for (a = 0; a < 2; a++) {
916                         for (b = 0; b < tel; b++) {
917
918                                 if (*buf && *buf <= maxob && *buf != dontdo) return *buf;
919                                 if (*buf == dontdo) retval = dontdo;  /* if only color dontdo is available, still return dontdo */
920                                 
921                                 buf += (dirvec[rc][0] + dirvec[rc][1]);
922
923                                 if (buf < bufmin || buf >= bufmax) return retval;
924                         }
925                         rc++;
926                         rc &= 3;
927                 }
928         }
929         return retval;
930 }
931 #endif
932
933 /* ************************** mouse select ************************* */
934
935
936 /* The max number of menu items in an object select menu */
937 typedef struct SelMenuItemF {
938         char idname[MAX_ID_NAME - 2];
939         int icon;
940 } SelMenuItemF;
941
942 #define SEL_MENU_SIZE   22
943 static SelMenuItemF object_mouse_select_menu_data[SEL_MENU_SIZE];
944
945 /* special (crappy) operator only for menu select */
946 static EnumPropertyItem *object_select_menu_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
947 {
948         EnumPropertyItem *item = NULL, item_tmp = {0};
949         int totitem = 0;
950         int i = 0;
951
952         /* don't need context but avoid docgen using this */
953         if (C == NULL || object_mouse_select_menu_data[i].idname[0] == '\0') {
954                 return DummyRNA_NULL_items;
955         }
956
957         for (; i < SEL_MENU_SIZE && object_mouse_select_menu_data[i].idname[0] != '\0'; i++) {
958                 item_tmp.name = object_mouse_select_menu_data[i].idname;
959                 item_tmp.identifier = object_mouse_select_menu_data[i].idname;
960                 item_tmp.value = i;
961                 item_tmp.icon = object_mouse_select_menu_data[i].icon;
962                 RNA_enum_item_add(&item, &totitem, &item_tmp);
963         }
964
965         RNA_enum_item_end(&item, &totitem);
966         *free = 1;
967
968         return item;
969 }
970
971 static int object_select_menu_exec(bContext *C, wmOperator *op)
972 {
973         int name_index = RNA_enum_get(op->ptr, "name");
974         short extend = RNA_boolean_get(op->ptr, "extend");
975         short changed = 0;
976         const char *name = object_mouse_select_menu_data[name_index].idname;
977
978         if (!extend) {
979                 CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
980                 {
981                         if (base->flag & SELECT) {
982                                 ED_base_object_select(base, BA_DESELECT);
983                                 changed = 1;
984                         }
985                 }
986                 CTX_DATA_END;
987         }
988
989         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
990         {
991                 /* this is a bit dodjy, there should only be ONE object with this name, but library objects can mess this up */
992                 if (strcmp(name, base->object->id.name + 2) == 0) {
993                         ED_base_object_activate(C, base);
994                         ED_base_object_select(base, BA_SELECT);
995                         changed = 1;
996                 }
997         }
998         CTX_DATA_END;
999
1000         /* weak but ensures we activate menu again before using the enum */
1001         memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
1002
1003         /* undo? */
1004         if (changed) {
1005                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
1006                 return OPERATOR_FINISHED;
1007         }
1008         else {
1009                 return OPERATOR_CANCELLED;
1010         }
1011 }
1012
1013 void VIEW3D_OT_select_menu(wmOperatorType *ot)
1014 {
1015         PropertyRNA *prop;
1016
1017         /* identifiers */
1018         ot->name = "Select Menu";
1019         ot->description = "Menu object selection";
1020         ot->idname = "VIEW3D_OT_select_menu";
1021
1022         /* api callbacks */
1023         ot->invoke = WM_menu_invoke;
1024         ot->exec = object_select_menu_exec;
1025
1026         /* flags */
1027         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1028
1029         /* keyingset to use (dynamic enum) */
1030         prop = RNA_def_enum(ot->srna, "name", DummyRNA_NULL_items, 0, "Object Name", "");
1031         RNA_def_enum_funcs(prop, object_select_menu_enum_itemf);
1032         RNA_def_property_flag(prop, PROP_HIDDEN);
1033         ot->prop = prop;
1034
1035         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
1036 }
1037
1038 static void deselectall_except(Scene *scene, Base *b)   /* deselect all except b */
1039 {
1040         Base *base;
1041         
1042         for (base = FIRSTBASE; base; base = base->next) {
1043                 if (base->flag & SELECT) {
1044                         if (b != base) {
1045                                 ED_base_object_select(base, BA_DESELECT);
1046                         }
1047                 }
1048         }
1049 }
1050
1051 static Base *object_mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffer, int hits, const int mval[2], short extend)
1052 {
1053         short baseCount = 0;
1054         short ok;
1055         LinkNode *linklist = NULL;
1056         
1057         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
1058         {
1059                 ok = FALSE;
1060
1061                 /* two selection methods, the CTRL select uses max dist of 15 */
1062                 if (buffer) {
1063                         int a;
1064                         for (a = 0; a < hits; a++) {
1065                                 /* index was converted */
1066                                 if (base->selcol == buffer[(4 * a) + 3])
1067                                         ok = TRUE;
1068                         }
1069                 }
1070                 else {
1071                         int temp, dist = 15;
1072
1073                         project_short(vc->ar, base->object->obmat[3], &base->sx);
1074                         
1075                         temp = abs(base->sx - mval[0]) + abs(base->sy - mval[1]);
1076                         if (temp < dist)
1077                                 ok = TRUE;
1078                 }
1079
1080                 if (ok) {
1081                         baseCount++;
1082                         BLI_linklist_prepend(&linklist, base);
1083
1084                         if (baseCount == SEL_MENU_SIZE)
1085                                 break;
1086                 }
1087         }
1088         CTX_DATA_END;
1089
1090         if (baseCount == 0) {
1091                 return NULL;
1092         }
1093         if (baseCount == 1) {
1094                 Base *base = (Base *)linklist->link;
1095                 BLI_linklist_free(linklist, NULL);
1096                 return base;
1097         }
1098         else {
1099                 /* UI, full in static array values that we later use in an enum function */
1100                 LinkNode *node;
1101                 int i;
1102
1103                 memset(object_mouse_select_menu_data, 0, sizeof(object_mouse_select_menu_data));
1104
1105                 for (node = linklist, i = 0; node; node = node->next, i++) {
1106                         Base *base = node->link;
1107                         Object *ob = base->object;
1108                         char *name = ob->id.name + 2;
1109
1110                         BLI_strncpy(object_mouse_select_menu_data[i].idname, name, MAX_ID_NAME - 2);
1111                         object_mouse_select_menu_data[i].icon = uiIconFromID(&ob->id);
1112                 }
1113
1114                 {
1115                         PointerRNA ptr;
1116
1117                         WM_operator_properties_create(&ptr, "VIEW3D_OT_select_menu");
1118                         RNA_boolean_set(&ptr, "extend", extend);
1119                         WM_operator_name_call(C, "VIEW3D_OT_select_menu", WM_OP_INVOKE_DEFAULT, &ptr);
1120                         WM_operator_properties_free(&ptr);
1121                 }
1122
1123                 BLI_linklist_free(linklist, NULL);
1124                 return NULL;
1125         }
1126 }
1127
1128 /* we want a select buffer with bones, if there are... */
1129 /* so check three selection levels and compare */
1130 static short mixed_bones_object_selectbuffer(ViewContext *vc, unsigned int *buffer, const int mval[2])
1131 {
1132         rcti rect;
1133         int offs;
1134         short a, hits15, hits9 = 0, hits5 = 0;
1135         short has_bones15 = 0, has_bones9 = 0, has_bones5 = 0;
1136         
1137         BLI_init_rcti(&rect, mval[0] - 14, mval[0] + 14, mval[1] - 14, mval[1] + 14);
1138         hits15 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect);
1139         if (hits15 > 0) {
1140                 for (a = 0; a < hits15; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones15 = 1;
1141
1142                 offs = 4 * hits15;
1143                 BLI_init_rcti(&rect, mval[0] - 9, mval[0] + 9, mval[1] - 9, mval[1] + 9);
1144                 hits9 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect);
1145                 if (hits9 > 0) {
1146                         for (a = 0; a < hits9; a++) if (buffer[offs + 4 * a + 3] & 0xFFFF0000) has_bones9 = 1;
1147
1148                         offs += 4 * hits9;
1149                         BLI_init_rcti(&rect, mval[0] - 5, mval[0] + 5, mval[1] - 5, mval[1] + 5);
1150                         hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect);
1151                         if (hits5 > 0) {
1152                                 for (a = 0; a < hits5; a++) if (buffer[offs + 4 * a + 3] & 0xFFFF0000) has_bones5 = 1;
1153                         }
1154                 }
1155                 
1156                 if (has_bones5) {
1157                         offs = 4 * hits15 + 4 * hits9;
1158                         memcpy(buffer, buffer + offs, 4 * offs);
1159                         return hits5;
1160                 }
1161                 if (has_bones9) {
1162                         offs = 4 * hits15;
1163                         memcpy(buffer, buffer + offs, 4 * offs);
1164                         return hits9;
1165                 }
1166                 if (has_bones15) {
1167                         return hits15;
1168                 }
1169                 
1170                 if (hits5 > 0) {
1171                         offs = 4 * hits15 + 4 * hits9;
1172                         memcpy(buffer, buffer + offs, 4 * offs);
1173                         return hits5;
1174                 }
1175                 if (hits9 > 0) {
1176                         offs = 4 * hits15;
1177                         memcpy(buffer, buffer + offs, 4 * offs);
1178                         return hits9;
1179                 }
1180                 return hits15;
1181         }
1182         
1183         return 0;
1184 }
1185
1186 /* returns basact */
1187 static Base *mouse_select_eval_buffer(ViewContext *vc, unsigned int *buffer, int hits, const int mval[2], Base *startbase, int has_bones)
1188 {
1189         Scene *scene = vc->scene;
1190         View3D *v3d = vc->v3d;
1191         Base *base, *basact = NULL;
1192         static int lastmval[2] = {-100, -100};
1193         int a, donearest = 0;
1194         
1195         /* define if we use solid nearest select or not */
1196         if (v3d->drawtype > OB_WIRE) {
1197                 donearest = 1;
1198                 if (ABS(mval[0] - lastmval[0]) < 3 && ABS(mval[1] - lastmval[1]) < 3) {
1199                         if (!has_bones) /* hrms, if theres bones we always do nearest */
1200                                 donearest = 0;
1201                 }
1202         }
1203         lastmval[0] = mval[0]; lastmval[1] = mval[1];
1204         
1205         if (donearest) {
1206                 unsigned int min = 0xFFFFFFFF;
1207                 int selcol = 0, notcol = 0;
1208                 
1209                 
1210                 if (has_bones) {
1211                         /* we skip non-bone hits */
1212                         for (a = 0; a < hits; a++) {
1213                                 if (min > buffer[4 * a + 1] && (buffer[4 * a + 3] & 0xFFFF0000) ) {
1214                                         min = buffer[4 * a + 1];
1215                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1216                                 }
1217                         }
1218                 }
1219                 else {
1220                         /* only exclude active object when it is selected... */
1221                         if (BASACT && (BASACT->flag & SELECT) && hits > 1) notcol = BASACT->selcol;
1222                         
1223                         for (a = 0; a < hits; a++) {
1224                                 if (min > buffer[4 * a + 1] && notcol != (buffer[4 * a + 3] & 0xFFFF)) {
1225                                         min = buffer[4 * a + 1];
1226                                         selcol = buffer[4 * a + 3] & 0xFFFF;
1227                                 }
1228                         }
1229                 }
1230                 
1231                 base = FIRSTBASE;
1232                 while (base) {
1233                         if (BASE_SELECTABLE(v3d, base)) {
1234                                 if (base->selcol == selcol) break;
1235                         }
1236                         base = base->next;
1237                 }
1238                 if (base) basact = base;
1239         }
1240         else {
1241                 
1242                 base = startbase;
1243                 while (base) {
1244                         /* skip objects with select restriction, to prevent prematurely ending this loop
1245                          * with an un-selectable choice */
1246                         if (base->object->restrictflag & OB_RESTRICT_SELECT) {
1247                                 base = base->next;
1248                                 if (base == NULL) base = FIRSTBASE;
1249                                 if (base == startbase) break;
1250                         }
1251                         
1252                         if (BASE_SELECTABLE(v3d, base)) {
1253                                 for (a = 0; a < hits; a++) {
1254                                         if (has_bones) {
1255                                                 /* skip non-bone objects */
1256                                                 if ((buffer[4 * a + 3] & 0xFFFF0000)) {
1257                                                         if (base->selcol == (buffer[(4 * a) + 3] & 0xFFFF))
1258                                                                 basact = base;
1259                                                 }
1260                                         }
1261                                         else {
1262                                                 if (base->selcol == (buffer[(4 * a) + 3] & 0xFFFF))
1263                                                         basact = base;
1264                                         }
1265                                 }
1266                         }
1267                         
1268                         if (basact) break;
1269                         
1270                         base = base->next;
1271                         if (base == NULL) base = FIRSTBASE;
1272                         if (base == startbase) break;
1273                 }
1274         }
1275         
1276         return basact;
1277 }
1278
1279 /* mval comes from event->mval, only use within region handlers */
1280 Base *ED_view3d_give_base_under_cursor(bContext *C, const int mval[2])
1281 {
1282         ViewContext vc;
1283         Base *basact = NULL;
1284         unsigned int buffer[4 * MAXPICKBUF];
1285         int hits;
1286         
1287         /* setup view context for argument to callbacks */
1288         view3d_operator_needs_opengl(C);
1289         view3d_set_viewcontext(C, &vc);
1290         
1291         hits = mixed_bones_object_selectbuffer(&vc, buffer, mval);
1292         
1293         if (hits > 0) {
1294                 int a, has_bones = 0;
1295                 
1296                 for (a = 0; a < hits; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones = 1;
1297                 
1298                 basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, vc.scene->base.first, has_bones);
1299         }
1300         
1301         return basact;
1302 }
1303
1304 static void deselect_all_tracks(MovieTracking *tracking)
1305 {
1306         MovieTrackingObject *object;
1307
1308         object = tracking->objects.first;
1309         while (object) {
1310                 ListBase *tracksbase = BKE_tracking_object_tracks(tracking, object);
1311                 MovieTrackingTrack *track = tracksbase->first;
1312
1313                 while (track) {
1314                         BKE_tracking_deselect_track(track, TRACK_AREA_ALL);
1315
1316                         track = track->next;
1317                 }
1318
1319                 object = object->next;
1320         }
1321 }
1322
1323 /* mval is region coords */
1324 static int mouse_select(bContext *C, const int mval[2], short extend, short obcenter, short enumerate)
1325 {
1326         ViewContext vc;
1327         ARegion *ar = CTX_wm_region(C);
1328         View3D *v3d = CTX_wm_view3d(C);
1329         Scene *scene = CTX_data_scene(C);
1330         Base *base, *startbase = NULL, *basact = NULL, *oldbasact = NULL;
1331         int temp, a, dist = 100;
1332         int retval = 0;
1333         short hits;
1334         
1335         /* setup view context for argument to callbacks */
1336         view3d_set_viewcontext(C, &vc);
1337         
1338         /* always start list from basact in wire mode */
1339         startbase =  FIRSTBASE;
1340         if (BASACT && BASACT->next) startbase = BASACT->next;
1341         
1342         /* This block uses the control key to make the object selected by its center point rather than its contents */
1343         /* in editmode do not activate */
1344         if (obcenter) {
1345                 
1346                 /* note; shift+alt goes to group-flush-selecting */
1347                 if (enumerate) {
1348                         basact = object_mouse_select_menu(C, &vc, NULL, 0, mval, extend);
1349                 }
1350                 else {
1351                         base = startbase;
1352                         while (base) {
1353                                 if (BASE_SELECTABLE(v3d, base)) {
1354                                         project_short(ar, base->object->obmat[3], &base->sx);
1355                                         
1356                                         temp = abs(base->sx - mval[0]) + abs(base->sy - mval[1]);
1357                                         if (base == BASACT) temp += 10;
1358                                         if (temp < dist) {
1359                                                 
1360                                                 dist = temp;
1361                                                 basact = base;
1362                                         }
1363                                 }
1364                                 base = base->next;
1365                                 
1366                                 if (base == NULL) base = FIRSTBASE;
1367                                 if (base == startbase) break;
1368                         }
1369                 }
1370         }
1371         else {
1372                 unsigned int buffer[4 * MAXPICKBUF];
1373
1374                 /* if objects have posemode set, the bones are in the same selection buffer */
1375                 
1376                 hits = mixed_bones_object_selectbuffer(&vc, buffer, mval);
1377                 
1378                 if (hits > 0) {
1379                         int has_bones = 0;
1380                         
1381                         /* note: bundles are handling in the same way as bones */
1382                         for (a = 0; a < hits; a++) if (buffer[4 * a + 3] & 0xFFFF0000) has_bones = 1;
1383
1384                         /* note; shift+alt goes to group-flush-selecting */
1385                         if (has_bones == 0 && enumerate) {
1386                                 basact = object_mouse_select_menu(C, &vc, buffer, hits, mval, extend);
1387                         }
1388                         else {
1389                                 basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, startbase, has_bones);
1390                         }
1391                         
1392                         if (has_bones && basact) {
1393                                 if (basact->object->type == OB_CAMERA) {
1394                                         if (BASACT == basact) {
1395                                                 int i, hitresult;
1396                                                 int changed = 0;
1397
1398                                                 for (i = 0; i < hits; i++) {
1399                                                         hitresult = buffer[3 + (i * 4)];
1400
1401                                                         /* if there's bundles in buffer select bundles first,
1402                                                          * so non-camera elements should be ignored in buffer */
1403                                                         if (basact->selcol != (hitresult & 0xFFFF)) {
1404                                                                 continue;
1405                                                         }
1406
1407                                                         /* index of bundle is 1<<16-based. if there's no "bone" index
1408                                                          * in hight word, this buffer value belongs to camera,. not to bundle */
1409                                                         if (buffer[4 * i + 3] & 0xFFFF0000) {
1410                                                                 MovieClip *clip = object_get_movieclip(scene, basact->object, 0);
1411                                                                 MovieTracking *tracking = &clip->tracking;
1412                                                                 ListBase *tracksbase;
1413                                                                 MovieTrackingTrack *track;
1414
1415                                                                 track = BKE_tracking_indexed_track(&clip->tracking, hitresult >> 16, &tracksbase);
1416
1417                                                                 if (TRACK_SELECTED(track) && extend) {
1418                                                                         changed = 0;
1419                                                                         BKE_tracking_deselect_track(track, TRACK_AREA_ALL);
1420                                                                 }
1421                                                                 else {
1422                                                                         int oldsel = TRACK_SELECTED(track) ? 1 : 0;
1423                                                                         if (!extend)
1424                                                                                 deselect_all_tracks(tracking);
1425
1426                                                                         BKE_tracking_select_track(tracksbase, track, TRACK_AREA_ALL, extend);
1427
1428                                                                         if (oldsel != (TRACK_SELECTED(track) ? 1 : 0))
1429                                                                                 changed = 1;
1430                                                                 }
1431
1432                                                                 basact->flag |= SELECT;
1433                                                                 basact->object->flag = basact->flag;
1434
1435                                                                 retval = 1;
1436
1437                                                                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_SELECT, track);
1438                                                                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1439
1440                                                                 break;
1441                                                         }
1442                                                 }
1443
1444                                                 if (!changed) {
1445                                                         /* fallback to regular object selection if no new bundles were selected,
1446                                                          * allows to select object parented to reconstruction object */
1447                                                         basact = mouse_select_eval_buffer(&vc, buffer, hits, mval, startbase, 0);
1448                                                 }
1449                                         }
1450                                 }
1451                                 else if (ED_do_pose_selectbuffer(scene, basact, buffer, hits, extend) ) {   /* then bone is found */
1452                                 
1453                                         /* we make the armature selected: 
1454                                          * not-selected active object in posemode won't work well for tools */
1455                                         basact->flag |= SELECT;
1456                                         basact->object->flag = basact->flag;
1457                                         
1458                                         retval = 1;
1459                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, basact->object);
1460                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_ACTIVE, basact->object);
1461                                         
1462                                         /* in weightpaint, we use selected bone to select vertexgroup, so no switch to new active object */
1463                                         if (BASACT && (BASACT->object->mode & OB_MODE_WEIGHT_PAINT)) {
1464                                                 /* prevent activating */
1465                                                 basact = NULL;
1466                                         }
1467
1468                                 }
1469                                 /* prevent bone selecting to pass on to object selecting */
1470                                 if (basact == BASACT)
1471                                         basact = NULL;
1472                         }
1473                 }
1474         }
1475         
1476         /* so, do we have something selected? */
1477         if (basact) {
1478                 retval = 1;
1479                 
1480                 if (vc.obedit) {
1481                         /* only do select */
1482                         deselectall_except(scene, basact);
1483                         ED_base_object_select(basact, BA_SELECT);
1484                 }
1485                 /* also prevent making it active on mouse selection */
1486                 else if (BASE_SELECTABLE(v3d, basact)) {
1487
1488                         oldbasact = BASACT;
1489                         
1490                         if (!extend) {
1491                                 deselectall_except(scene, basact);
1492                                 ED_base_object_select(basact, BA_SELECT);
1493                         }
1494                         else if (0) {
1495                                 // XXX select_all_from_groups(basact);
1496                         }
1497                         else {
1498                                 if (basact->flag & SELECT) {
1499                                         if (basact == oldbasact)
1500                                                 ED_base_object_select(basact, BA_DESELECT);
1501                                 }
1502                                 else ED_base_object_select(basact, BA_SELECT);
1503                         }
1504
1505                         if (oldbasact != basact) {
1506                                 ED_base_object_activate(C, basact); /* adds notifier */
1507                         }
1508                 }
1509
1510                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1511         }
1512
1513         return retval;
1514 }
1515
1516 /* ********************  border and circle ************************************** */
1517
1518 typedef struct BoxSelectUserData {
1519         ViewContext *vc;
1520         rcti *rect;
1521         int select, pass, done;
1522 } BoxSelectUserData;
1523
1524 int edge_inside_circle(short centx, short centy, short rad, short x1, short y1, short x2, short y2)
1525 {
1526         int radsq = rad * rad;
1527         float v1[2], v2[2], v3[2];
1528         
1529         /* check points in circle itself */
1530         if ( (x1 - centx) * (x1 - centx) + (y1 - centy) * (y1 - centy) <= radsq) return 1;
1531         if ( (x2 - centx) * (x2 - centx) + (y2 - centy) * (y2 - centy) <= radsq) return 1;
1532         
1533         /* pointdistline */
1534         v3[0] = centx;
1535         v3[1] = centy;
1536         v1[0] = x1;
1537         v1[1] = y1;
1538         v2[0] = x2;
1539         v2[1] = y2;
1540         
1541         if (dist_to_line_segment_v2(v3, v1, v2) < (float)rad) return 1;
1542         
1543         return 0;
1544 }
1545
1546 static void do_nurbs_box_select__doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
1547 {
1548         BoxSelectUserData *data = userData;
1549         Object *obedit = data->vc->obedit;
1550         Curve *cu = (Curve *)obedit->data;
1551
1552         if (BLI_in_rcti(data->rect, x, y)) {
1553                 if (bp) {
1554                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
1555                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
1556                 }
1557                 else {
1558                         if (cu->drawflag & CU_HIDE_HANDLES) {
1559                                 /* can only be beztindex==0 here since handles are hidden */
1560                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
1561                         }
1562                         else {
1563                                 if (beztindex == 0) {
1564                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
1565                                 }
1566                                 else if (beztindex == 1) {
1567                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
1568                                 }
1569                                 else {
1570                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
1571                                 }
1572                         }
1573
1574                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
1575                 }
1576         }
1577 }
1578 static int do_nurbs_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1579 {
1580         BoxSelectUserData data;
1581         
1582         data.vc = vc;
1583         data.rect = rect;
1584         data.select = select;
1585
1586         if (extend == 0 && select)
1587                 CU_deselect_all(vc->obedit);
1588
1589         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
1590         nurbs_foreachScreenVert(vc, do_nurbs_box_select__doSelect, &data);
1591
1592         return OPERATOR_FINISHED;
1593 }
1594
1595 static void do_lattice_box_select__doSelect(void *userData, BPoint *bp, int x, int y)
1596 {
1597         BoxSelectUserData *data = userData;
1598
1599         if (BLI_in_rcti(data->rect, x, y)) {
1600                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
1601         }
1602 }
1603 static int do_lattice_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1604 {
1605         BoxSelectUserData data;
1606
1607         data.vc = vc;
1608         data.rect = rect;
1609         data.select = select;
1610
1611         if (extend == 0 && select)
1612                 ED_setflagsLatt(vc->obedit, 0);
1613
1614         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
1615         lattice_foreachScreenVert(vc, do_lattice_box_select__doSelect, &data);
1616         
1617         return OPERATOR_FINISHED;
1618 }
1619
1620 static void do_mesh_box_select__doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
1621 {
1622         BoxSelectUserData *data = userData;
1623
1624         if (BLI_in_rcti(data->rect, x, y)) {
1625                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
1626         }
1627 }
1628 static void do_mesh_box_select__doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index)
1629 {
1630         BoxSelectUserData *data = userData;
1631
1632         if (EDBM_backbuf_check(bm_solidoffs + index)) {
1633                 if (data->pass == 0) {
1634                         if (edge_fully_inside_rect(data->rect, x0, y0, x1, y1)) {
1635                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
1636                                 data->done = 1;
1637                         }
1638                 }
1639                 else {
1640                         if (edge_inside_rect(data->rect, x0, y0, x1, y1)) {
1641                                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
1642                         }
1643                 }
1644         }
1645 }
1646 static void do_mesh_box_select__doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
1647 {
1648         BoxSelectUserData *data = userData;
1649
1650         if (BLI_in_rcti(data->rect, x, y)) {
1651                 BM_face_select_set(data->vc->em->bm, efa, data->select);
1652         }
1653 }
1654 static int do_mesh_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1655 {
1656         BoxSelectUserData data;
1657         ToolSettings *ts = vc->scene->toolsettings;
1658         int bbsel;
1659         
1660         data.vc = vc;
1661         data.rect = rect;
1662         data.select = select;
1663         data.pass = 0;
1664         data.done = 0;
1665
1666         if (extend == 0 && select)
1667                 EDBM_flag_disable_all(vc->em, BM_ELEM_SELECT);
1668
1669         /* for non zbuf projections, don't change the GL state */
1670         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
1671
1672         glLoadMatrixf(vc->rv3d->viewmat);
1673         bbsel = EDBM_backbuf_border_init(vc, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
1674
1675         if (ts->selectmode & SCE_SELECT_VERTEX) {
1676                 if (bbsel) {
1677                         edbm_backbuf_check_and_select_verts(vc->em, select);
1678                 }
1679                 else {
1680                         mesh_foreachScreenVert(vc, do_mesh_box_select__doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
1681                 }
1682         }
1683         if (ts->selectmode & SCE_SELECT_EDGE) {
1684                 /* Does both bbsel and non-bbsel versions (need screen cos for both) */
1685
1686                 data.pass = 0;
1687                 mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
1688
1689                 if (data.done == 0) {
1690                         data.pass = 1;
1691                         mesh_foreachScreenEdge(vc, do_mesh_box_select__doSelectEdge, &data, V3D_CLIP_TEST_OFF);
1692                 }
1693         }
1694         
1695         if (ts->selectmode & SCE_SELECT_FACE) {
1696                 if (bbsel) {
1697                         edbm_backbuf_check_and_select_faces(vc->em, select);
1698                 }
1699                 else {
1700                         mesh_foreachScreenFace(vc, do_mesh_box_select__doSelectFace, &data);
1701                 }
1702         }
1703         
1704         EDBM_backbuf_free();
1705                 
1706         EDBM_selectmode_flush(vc->em);
1707         
1708         return OPERATOR_FINISHED;
1709 }
1710
1711 static int do_meta_box_select(ViewContext *vc, rcti *rect, int select, int extend)
1712 {
1713         MetaBall *mb = (MetaBall *)vc->obedit->data;
1714         MetaElem *ml;
1715         int a;
1716
1717         unsigned int buffer[4 * MAXPICKBUF];
1718         short hits;
1719
1720         hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect);
1721
1722         if (extend == 0 && select) {
1723                 for (ml = mb->editelems->first; ml; ml = ml->next) {
1724                         ml->flag &= ~SELECT;
1725                 }
1726         }
1727         
1728         for (ml = mb->editelems->first; ml; ml = ml->next) {
1729                 for (a = 0; a < hits; a++) {
1730                         if (ml->selcol1 == buffer[(4 * a) + 3]) {
1731                                 ml->flag |= MB_SCALE_RAD;
1732                                 if (select) ml->flag |= SELECT;
1733                                 else ml->flag &= ~SELECT;
1734                                 break;
1735                         }
1736                         if (ml->selcol2 == buffer[(4 * a) + 3]) {
1737                                 ml->flag &= ~MB_SCALE_RAD;
1738                                 if (select) ml->flag |= SELECT;
1739                                 else ml->flag &= ~SELECT;
1740                                 break;
1741                         }
1742                 }
1743         }
1744
1745         return OPERATOR_FINISHED;
1746 }
1747
1748 static int do_armature_box_select(ViewContext *vc, rcti *rect, short select, short extend)
1749 {
1750         bArmature *arm = vc->obedit->data;
1751         EditBone *ebone;
1752         int a;
1753
1754         unsigned int buffer[4 * MAXPICKBUF];
1755         short hits;
1756
1757         hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect);
1758         
1759         /* clear flag we use to detect point was affected */
1760         for (ebone = arm->edbo->first; ebone; ebone = ebone->next)
1761                 ebone->flag &= ~BONE_DONE;
1762         
1763         if (extend == 0 && select)
1764                 ED_armature_deselect_all_visible(vc->obedit);
1765
1766         /* first we only check points inside the border */
1767         for (a = 0; a < hits; a++) {
1768                 int index = buffer[(4 * a) + 3];
1769                 if (index != -1) {
1770                         ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
1771                         if ((ebone->flag & BONE_UNSELECTABLE) == 0) {
1772                                 if (index & BONESEL_TIP) {
1773                                         ebone->flag |= BONE_DONE;
1774                                         if (select) ebone->flag |= BONE_TIPSEL;
1775                                         else ebone->flag &= ~BONE_TIPSEL;
1776                                 }
1777                                 
1778                                 if (index & BONESEL_ROOT) {
1779                                         ebone->flag |= BONE_DONE;
1780                                         if (select) ebone->flag |= BONE_ROOTSEL;
1781                                         else ebone->flag &= ~BONE_ROOTSEL;
1782                                 }
1783                         }
1784                 }
1785         }
1786         
1787         /* now we have to flush tag from parents... */
1788         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
1789                 if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
1790                         if (ebone->parent->flag & BONE_DONE)
1791                                 ebone->flag |= BONE_DONE;
1792                 }
1793         }
1794         
1795         /* only select/deselect entire bones when no points where in the rect */
1796         for (a = 0; a < hits; a++) {
1797                 int index = buffer[(4 * a) + 3];
1798                 if (index != -1) {
1799                         ebone = BLI_findlink(arm->edbo, index & ~(BONESEL_ANY));
1800                         if (index & BONESEL_BONE) {
1801                                 if ((ebone->flag & BONE_UNSELECTABLE) == 0) {
1802                                         if (!(ebone->flag & BONE_DONE)) {
1803                                                 if (select)
1804                                                         ebone->flag |= (BONE_ROOTSEL | BONE_TIPSEL | BONE_SELECTED);
1805                                                 else
1806                                                         ebone->flag &= ~(BONE_ROOTSEL | BONE_TIPSEL | BONE_SELECTED);
1807                                         }
1808                                 }
1809                         }
1810                 }
1811         }
1812         
1813         ED_armature_sync_selection(arm->edbo);
1814         
1815         return OPERATOR_CANCELLED;
1816 }
1817
1818 static int do_object_pose_box_select(bContext *C, ViewContext *vc, rcti *rect, int select, int extend)
1819 {
1820         Bone *bone;
1821         Object *ob = vc->obact;
1822         unsigned int *vbuffer = NULL; /* selection buffer       */
1823         unsigned int *col;          /* color in buffer  */
1824         int bone_only;
1825         int bone_selected = 0;
1826         int totobj = MAXPICKBUF; // XXX solve later
1827         short hits;
1828         
1829         if ((ob) && (ob->mode & OB_MODE_POSE))
1830                 bone_only = 1;
1831         else
1832                 bone_only = 0;
1833         
1834         if (extend == 0 && select) {
1835                 if (bone_only) {
1836                         CTX_DATA_BEGIN (C, bPoseChannel *, pchan, visible_pose_bones)
1837                         {
1838                                 if ((pchan->bone->flag & BONE_UNSELECTABLE) == 0) {
1839                                         pchan->bone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1840                                 }
1841                         }
1842                         CTX_DATA_END;
1843                 }
1844                 else {
1845                         object_deselect_all_visible(vc->scene, vc->v3d);
1846                 }
1847         }
1848
1849         /* selection buffer now has bones potentially too, so we add MAXPICKBUF */
1850         vbuffer = MEM_mallocN(4 * (totobj + MAXPICKBUF) * sizeof(unsigned int), "selection buffer");
1851         hits = view3d_opengl_select(vc, vbuffer, 4 * (totobj + MAXPICKBUF), rect);
1852         /*
1853          * LOGIC NOTES (theeth):
1854          * The buffer and ListBase have the same relative order, which makes the selection
1855          * very simple. Loop through both data sets at the same time, if the color
1856          * is the same as the object, we have a hit and can move to the next color
1857          * and object pair, if not, just move to the next object,
1858          * keeping the same color until we have a hit.
1859          * 
1860          * The buffer order is defined by OGL standard, hopefully no stupid GFX card
1861          * does it incorrectly.
1862          */
1863
1864         if (hits > 0) { /* no need to loop if there's no hit */
1865                 Base *base;
1866                 col = vbuffer + 3;
1867                 
1868                 for (base = vc->scene->base.first; base && hits; base = base->next) {
1869                         if (BASE_SELECTABLE(vc->v3d, base)) {
1870                                 while (base->selcol == (*col & 0xFFFF)) {   /* we got an object */
1871                                         
1872                                         if (*col & 0xFFFF0000) {                    /* we got a bone */
1873                                                 bone = get_indexed_bone(base->object, *col & ~(BONESEL_ANY));
1874                                                 if (bone) {
1875                                                         if (select) {
1876                                                                 if ((bone->flag & BONE_UNSELECTABLE) == 0) {
1877                                                                         bone->flag |= BONE_SELECTED;
1878                                                                         bone_selected = 1;
1879 // XXX                                                                  select_actionchannel_by_name(base->object->action, bone->name, 1);
1880                                                                 }
1881                                                         }
1882                                                         else {
1883                                                                 bArmature *arm = base->object->data;
1884                                                                 bone->flag &= ~BONE_SELECTED;
1885 // XXX                                                                  select_actionchannel_by_name(base->object->action, bone->name, 0);
1886                                                                 if (arm->act_bone == bone)
1887                                                                         arm->act_bone = NULL;
1888                                                                 
1889                                                         }
1890                                                 }
1891                                         }
1892                                         else if (!bone_only) {
1893                                                 if (select)
1894                                                         ED_base_object_select(base, BA_SELECT);
1895                                                 else
1896                                                         ED_base_object_select(base, BA_DESELECT);
1897                                         }
1898
1899                                         col += 4; /* next color */
1900                                         hits--;
1901                                         if (hits == 0) break;
1902                                 }
1903                         }
1904                         
1905                         if (bone_selected) {
1906                                 WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, base->object);
1907                         }
1908                 }
1909
1910                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, vc->scene);
1911
1912         }
1913         MEM_freeN(vbuffer);
1914
1915         return hits > 0 ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1916 }
1917
1918 static int view3d_borderselect_exec(bContext *C, wmOperator *op)
1919 {
1920         ViewContext vc;
1921         rcti rect;
1922         short extend;
1923         short select;
1924
1925         int ret = OPERATOR_CANCELLED;
1926
1927         view3d_operator_needs_opengl(C);
1928
1929         /* setup view context for argument to callbacks */
1930         view3d_set_viewcontext(C, &vc);
1931         
1932         select = (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT);
1933         rect.xmin = RNA_int_get(op->ptr, "xmin");
1934         rect.ymin = RNA_int_get(op->ptr, "ymin");
1935         rect.xmax = RNA_int_get(op->ptr, "xmax");
1936         rect.ymax = RNA_int_get(op->ptr, "ymax");
1937         extend = RNA_boolean_get(op->ptr, "extend");
1938
1939         if (vc.obedit) {
1940                 switch (vc.obedit->type) {
1941                         case OB_MESH:
1942                                 vc.em = BMEdit_FromObject(vc.obedit);
1943                                 ret = do_mesh_box_select(&vc, &rect, select, extend);
1944 //                      if (EM_texFaceCheck())
1945                                 if (ret & OPERATOR_FINISHED) {
1946                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1947                                 }
1948                                 break;
1949                         case OB_CURVE:
1950                         case OB_SURF:
1951                                 ret = do_nurbs_box_select(&vc, &rect, select, extend);
1952                                 if (ret & OPERATOR_FINISHED) {
1953                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1954                                 }
1955                                 break;
1956                         case OB_MBALL:
1957                                 ret = do_meta_box_select(&vc, &rect, select, extend);
1958                                 if (ret & OPERATOR_FINISHED) {
1959                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1960                                 }
1961                                 break;
1962                         case OB_ARMATURE:
1963                                 ret = do_armature_box_select(&vc, &rect, select, extend);
1964                                 if (ret & OPERATOR_FINISHED) {
1965                                         WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, vc.obedit);
1966                                 }
1967                                 break;
1968                         case OB_LATTICE:
1969                                 ret = do_lattice_box_select(&vc, &rect, select, extend);
1970                                 if (ret & OPERATOR_FINISHED) {
1971                                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit->data);
1972                                 }
1973                                 break;
1974                         default:
1975                                 assert(!"border select on incorrect object type");
1976                 }
1977         }
1978         else {  /* no editmode, unified for bones and objects */
1979                 if (vc.obact && vc.obact->mode & OB_MODE_SCULPT) {
1980                         /* pass */
1981                 }
1982                 else if (vc.obact && paint_facesel_test(vc.obact)) {
1983                         ret = do_paintface_box_select(&vc, &rect, select, extend);
1984                 }
1985                 else if (vc.obact && paint_vertsel_test(vc.obact)) {
1986                         ret = do_paintvert_box_select(&vc, &rect, select, extend);
1987                 }
1988                 else if (vc.obact && vc.obact->mode & OB_MODE_PARTICLE_EDIT) {
1989                         ret = PE_border_select(C, &rect, select, extend);
1990                 }
1991                 else { /* object mode with none active */
1992                         ret = do_object_pose_box_select(C, &vc, &rect, select, extend);
1993                 }
1994         }
1995
1996         return ret;
1997
1998
1999
2000 /* *****************Selection Operators******************* */
2001
2002 /* ****** Border Select ****** */
2003 void VIEW3D_OT_select_border(wmOperatorType *ot)
2004 {
2005         /* identifiers */
2006         ot->name = "Border Select";
2007         ot->description = "Select items using border selection";
2008         ot->idname = "VIEW3D_OT_select_border";
2009         
2010         /* api callbacks */
2011         ot->invoke = WM_border_select_invoke;
2012         ot->exec = view3d_borderselect_exec;
2013         ot->modal = WM_border_select_modal;
2014         ot->poll = view3d_selectable_data;
2015         ot->cancel = WM_border_select_cancel;
2016         
2017         /* flags */
2018         ot->flag = OPTYPE_UNDO;
2019         
2020         /* rna */
2021         WM_operator_properties_gesture_border(ot, TRUE);
2022 }
2023
2024 /* much like facesel_face_pick()*/
2025 /* returns 0 if not found, otherwise 1 */
2026 static int vertsel_vert_pick(struct bContext *C, Mesh *me, const int mval[2], unsigned int *index, int size)
2027 {
2028         ViewContext vc;
2029         view3d_set_viewcontext(C, &vc);
2030
2031         if (!me || me->totvert == 0)
2032                 return 0;
2033
2034         if (size > 0) {
2035                 /* sample rect to increase changes of selecting, so that when clicking
2036                  * on an face in the backbuf, we can still select a vert */
2037
2038                 int dist;
2039                 *index = view3d_sample_backbuf_rect(&vc, mval, size, 1, me->totvert + 1, &dist, 0, NULL, NULL);
2040         }
2041         else {
2042                 /* sample only on the exact position */
2043                 *index = view3d_sample_backbuf(&vc, mval[0], mval[1]);
2044         }
2045
2046         if ((*index) <= 0 || (*index) > (unsigned int)me->totvert)
2047                 return 0;
2048
2049         (*index)--;
2050         
2051         return 1;
2052 }
2053
2054 /* mouse selection in weight paint */
2055 /* gets called via generic mouse select operator */
2056 static int mouse_weight_paint_vertex_select(bContext *C, const int mval[2], short extend, Object *obact)
2057 {
2058         Mesh*me = obact->data; /* already checked for NULL */
2059         unsigned int index = 0;
2060         MVert *mv;
2061
2062         if (vertsel_vert_pick(C, me, mval, &index, 50)) {
2063                 mv = me->mvert + index;
2064                 if (extend) {
2065                         mv->flag ^= SELECT;
2066                 }
2067                 else {
2068                         paintvert_deselect_all_visible(obact, SEL_DESELECT, FALSE);
2069                         mv->flag |= SELECT;
2070                 }
2071                 paintvert_flush_flags(obact);
2072                 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2073                 return 1;
2074         }
2075         return 0;
2076 }
2077
2078 /* ****** Mouse Select ****** */
2079
2080
2081 static int view3d_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
2082 {
2083         Object *obedit = CTX_data_edit_object(C);
2084         Object *obact = CTX_data_active_object(C);
2085         short extend = RNA_boolean_get(op->ptr, "extend");
2086         short center = RNA_boolean_get(op->ptr, "center");
2087         short enumerate = RNA_boolean_get(op->ptr, "enumerate");
2088         short object = RNA_boolean_get(op->ptr, "object");
2089         int retval = 0;
2090
2091         view3d_operator_needs_opengl(C);
2092
2093         if (object) {
2094                 obedit = NULL;
2095                 obact = NULL;
2096
2097                 /* ack, this is incorrect but to do this correctly we would need an
2098                  * alternative editmode/objectmode keymap, this copies the functionality
2099                  * from 2.4x where Ctrl+Select in editmode does object select only */
2100                 center = FALSE;
2101         }
2102
2103         if (obedit && object == FALSE) {
2104                 if (obedit->type == OB_MESH)
2105                         retval = mouse_mesh(C, event->mval, extend);
2106                 else if (obedit->type == OB_ARMATURE)
2107                         retval = mouse_armature(C, event->mval, extend);
2108                 else if (obedit->type == OB_LATTICE)
2109                         retval = mouse_lattice(C, event->mval, extend);
2110                 else if (ELEM(obedit->type, OB_CURVE, OB_SURF))
2111                         retval = mouse_nurb(C, event->mval, extend);
2112                 else if (obedit->type == OB_MBALL)
2113                         retval = mouse_mball(C, event->mval, extend);
2114                         
2115         }
2116         else if (obact && obact->mode & OB_MODE_SCULPT)
2117                 return OPERATOR_CANCELLED;
2118         else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT)
2119                 return PE_mouse_particles(C, event->mval, extend);
2120         else if (obact && paint_facesel_test(obact))
2121                 retval = paintface_mouse_select(C, obact, event->mval, extend);
2122         else if (paint_vertsel_test(obact))
2123                 retval = mouse_weight_paint_vertex_select(C, event->mval, extend, obact);
2124         else
2125                 retval = mouse_select(C, event->mval, extend, center, enumerate);
2126
2127         /* passthrough allows tweaks
2128          * FINISHED to signal one operator worked
2129          * */
2130         if (retval)
2131                 return OPERATOR_PASS_THROUGH | OPERATOR_FINISHED;
2132         else
2133                 return OPERATOR_PASS_THROUGH;  /* nothing selected, just passthrough */
2134 }
2135
2136 void VIEW3D_OT_select(wmOperatorType *ot)
2137 {
2138         /* identifiers */
2139         ot->name = "Activate/Select";
2140         ot->description = "Activate/select item(s)";
2141         ot->idname = "VIEW3D_OT_select";
2142         
2143         /* api callbacks */
2144         ot->invoke = view3d_select_invoke;
2145         ot->poll = ED_operator_view3d_active;
2146         
2147         /* flags */
2148         ot->flag = OPTYPE_UNDO;
2149         
2150         /* properties */
2151         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
2152         RNA_def_boolean(ot->srna, "center", 0, "Center", "Use the object center when selecting, in editmode used to extend object selection");
2153         RNA_def_boolean(ot->srna, "enumerate", 0, "Enumerate", "List objects under the mouse (object mode only)");
2154         RNA_def_boolean(ot->srna, "object", 0, "Object", "Use object selection (editmode only)");
2155 }
2156
2157
2158 /* -------------------- circle select --------------------------------------------- */
2159
2160 typedef struct CircleSelectUserData {
2161         ViewContext *vc;
2162         short select;
2163         int mval[2];
2164         float radius;
2165 } CircleSelectUserData;
2166
2167 static void mesh_circle_doSelectVert(void *userData, BMVert *eve, int x, int y, int UNUSED(index))
2168 {
2169         CircleSelectUserData *data = userData;
2170         int mx = x - data->mval[0], my = y - data->mval[1];
2171         float r = sqrt(mx * mx + my * my);
2172
2173         if (r <= data->radius) {
2174                 BM_vert_select_set(data->vc->em->bm, eve, data->select);
2175         }
2176 }
2177 static void mesh_circle_doSelectEdge(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int UNUSED(index))
2178 {
2179         CircleSelectUserData *data = userData;
2180
2181         if (edge_inside_circle(data->mval[0], data->mval[1], (short) data->radius, x0, y0, x1, y1)) {
2182                 BM_edge_select_set(data->vc->em->bm, eed, data->select);
2183         }
2184 }
2185 static void mesh_circle_doSelectFace(void *userData, BMFace *efa, int x, int y, int UNUSED(index))
2186 {
2187         CircleSelectUserData *data = userData;
2188         int mx = x - data->mval[0], my = y - data->mval[1];
2189         float r = sqrt(mx * mx + my * my);
2190         
2191         if (r <= data->radius) {
2192                 BM_face_select_set(data->vc->em->bm, efa, data->select);
2193         }
2194 }
2195
2196 static void mesh_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2197 {
2198         ToolSettings *ts = vc->scene->toolsettings;
2199         int bbsel;
2200         CircleSelectUserData data;
2201         
2202         bbsel = EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0));
2203         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2204
2205         vc->em = BMEdit_FromObject(vc->obedit);
2206
2207         data.vc = vc;
2208         data.select = select;
2209         data.mval[0] = mval[0];
2210         data.mval[1] = mval[1];
2211         data.radius = rad;
2212
2213         if (ts->selectmode & SCE_SELECT_VERTEX) {
2214                 if (bbsel) {
2215                         edbm_backbuf_check_and_select_verts(vc->em, select == LEFTMOUSE);
2216                 }
2217                 else {
2218                         mesh_foreachScreenVert(vc, mesh_circle_doSelectVert, &data, V3D_CLIP_TEST_RV3D_CLIPPING);
2219                 }
2220         }
2221
2222         if (ts->selectmode & SCE_SELECT_EDGE) {
2223                 if (bbsel) {
2224                         edbm_backbuf_check_and_select_edges(vc->em, select == LEFTMOUSE);
2225                 }
2226                 else {
2227                         mesh_foreachScreenEdge(vc, mesh_circle_doSelectEdge, &data, V3D_CLIP_TEST_OFF);
2228                 }
2229         }
2230         
2231         if (ts->selectmode & SCE_SELECT_FACE) {
2232                 if (bbsel) {
2233                         edbm_backbuf_check_and_select_faces(vc->em, select == LEFTMOUSE);
2234                 }
2235                 else {
2236                         mesh_foreachScreenFace(vc, mesh_circle_doSelectFace, &data);
2237                 }
2238         }
2239
2240         EDBM_backbuf_free();
2241         EDBM_selectmode_flush(vc->em);
2242 }
2243
2244 static void paint_facesel_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2245 {
2246         Object *ob = vc->obact;
2247         Mesh *me = ob ? ob->data : NULL;
2248         /* int bbsel; */ /* UNUSED */
2249
2250         if (me) {
2251                 bm_vertoffs = me->totpoly + 1; /* max index array */
2252
2253                 /* bbsel= */ /* UNUSED */ EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0));
2254                 edbm_backbuf_check_and_select_tfaces(me, select == LEFTMOUSE);
2255                 EDBM_backbuf_free();
2256         }
2257 }
2258
2259
2260 static void paint_vertsel_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2261 {
2262         Object *ob = vc->obact;
2263         Mesh *me = ob ? ob->data : NULL;
2264         /* int bbsel; */ /* UNUSED */
2265         /* CircleSelectUserData data = {NULL}; */ /* UNUSED */
2266         if (me) {
2267                 bm_vertoffs = me->totvert + 1; /* max index array */
2268
2269                 /* bbsel= */ /* UNUSED */ EDBM_backbuf_circle_init(vc, mval[0], mval[1], (short)(rad + 1.0f));
2270                 edbm_backbuf_check_and_select_verts_obmode(me, select == LEFTMOUSE);
2271                 EDBM_backbuf_free();
2272
2273                 paintvert_flush_flags(ob);
2274         }
2275 }
2276
2277
2278 static void nurbscurve_circle_doSelect(void *userData, Nurb *UNUSED(nu), BPoint *bp, BezTriple *bezt, int beztindex, int x, int y)
2279 {
2280         CircleSelectUserData *data = userData;
2281         int mx = x - data->mval[0], my = y - data->mval[1];
2282         float r = sqrt(mx * mx + my * my);
2283         Object *obedit = data->vc->obedit;
2284         Curve *cu = (Curve *)obedit->data;
2285
2286         if (r <= data->radius) {
2287                 if (bp) {
2288                         bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
2289
2290                         if (bp == cu->lastsel && !(bp->f1 & 1)) cu->lastsel = NULL;
2291                 }
2292                 else {
2293                         if (cu->drawflag & CU_HIDE_HANDLES) {
2294                                 /* can only be beztindex==0 here since handles are hidden */
2295                                 bezt->f1 = bezt->f2 = bezt->f3 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
2296                         }
2297                         else {
2298                                 if (beztindex == 0) {
2299                                         bezt->f1 = data->select ? (bezt->f1 | SELECT) : (bezt->f1 & ~SELECT);
2300                                 }
2301                                 else if (beztindex == 1) {
2302                                         bezt->f2 = data->select ? (bezt->f2 | SELECT) : (bezt->f2 & ~SELECT);
2303                                 }
2304                                 else {
2305                                         bezt->f3 = data->select ? (bezt->f3 | SELECT) : (bezt->f3 & ~SELECT);
2306                                 }
2307                         }
2308
2309                         if (bezt == cu->lastsel && !(bezt->f2 & 1)) cu->lastsel = NULL;
2310                 }
2311         }
2312 }
2313 static void nurbscurve_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2314 {
2315         CircleSelectUserData data;
2316
2317         /* set vc-> edit data */
2318         
2319         data.select = select;
2320         data.mval[0] = mval[0];
2321         data.mval[1] = mval[1];
2322         data.radius = rad;
2323         data.vc = vc;
2324
2325         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2326         nurbs_foreachScreenVert(vc, nurbscurve_circle_doSelect, &data);
2327 }
2328
2329
2330 static void latticecurve_circle_doSelect(void *userData, BPoint *bp, int x, int y)
2331 {
2332         CircleSelectUserData *data = userData;
2333         int mx = x - data->mval[0], my = y - data->mval[1];
2334         float r = sqrt(mx * mx + my * my);
2335
2336         if (r <= data->radius) {
2337                 bp->f1 = data->select ? (bp->f1 | SELECT) : (bp->f1 & ~SELECT);
2338         }
2339 }
2340 static void lattice_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2341 {
2342         CircleSelectUserData data;
2343
2344         /* set vc-> edit data */
2345         
2346         data.select = select;
2347         data.mval[0] = mval[0];
2348         data.mval[1] = mval[1];
2349         data.radius = rad;
2350
2351         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2352         lattice_foreachScreenVert(vc, latticecurve_circle_doSelect, &data);
2353 }
2354
2355
2356 // NOTE: pose-bone case is copied from editbone case...
2357 static short pchan_circle_doSelectJoint(void *userData, bPoseChannel *pchan, int x, int y)
2358 {
2359         CircleSelectUserData *data = userData;
2360         int mx = x - data->mval[0], my = y - data->mval[1];
2361         float r = sqrt(mx * mx + my * my);
2362         
2363         if (r <= data->radius) {
2364                 if (data->select)
2365                         pchan->bone->flag |= BONE_SELECTED;
2366                 else
2367                         pchan->bone->flag &= ~BONE_SELECTED;
2368                 return 1;
2369         }
2370         return 0;
2371 }
2372 static void pose_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2373 {
2374         CircleSelectUserData data;
2375         bArmature *arm = vc->obact->data;
2376         bPose *pose = vc->obact->pose;
2377         bPoseChannel *pchan;
2378         int change = FALSE;
2379         
2380         /* set vc->edit data */
2381         data.select = select;
2382         data.mval[0] = mval[0];
2383         data.mval[1] = mval[1];
2384         data.radius = rad;
2385
2386         ED_view3d_init_mats_rv3d(vc->obact, vc->rv3d); /* for foreach's screen/vert projection */
2387         
2388         /* check each PoseChannel... */
2389         // TODO: could be optimized at some point
2390         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2391                 short sco1[2], sco2[2], didpoint = 0;
2392                 float vec[3];
2393                 
2394                 /* skip invisible bones */
2395                 if (PBONE_VISIBLE(arm, pchan->bone) == 0)
2396                         continue;
2397                 
2398                 /* project head location to screenspace */
2399                 mul_v3_m4v3(vec, vc->obact->obmat, pchan->pose_head);
2400                 project_short(vc->ar, vec, sco1);
2401                 
2402                 /* project tail location to screenspace */
2403                 mul_v3_m4v3(vec, vc->obact->obmat, pchan->pose_tail);
2404                 project_short(vc->ar, vec, sco2);
2405                 
2406                 /* check if the head and/or tail is in the circle 
2407                  *      - the call to check also does the selection already
2408                  */
2409                 if (pchan_circle_doSelectJoint(&data, pchan, sco1[0], sco1[1]))
2410                         didpoint = 1;
2411                 if (pchan_circle_doSelectJoint(&data, pchan, sco2[0], sco2[1]))
2412                         didpoint = 1;
2413                 
2414                 change |= didpoint;
2415         }
2416
2417         if (change) {
2418                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obact);
2419         }
2420 }
2421
2422 static short armature_circle_doSelectJoint(void *userData, EditBone *ebone, int x, int y, short head)
2423 {
2424         CircleSelectUserData *data = userData;
2425         int mx = x - data->mval[0], my = y - data->mval[1];
2426         float r = sqrt(mx * mx + my * my);
2427         
2428         if (r <= data->radius) {
2429                 if (head) {
2430                         if (data->select)
2431                                 ebone->flag |= BONE_ROOTSEL;
2432                         else 
2433                                 ebone->flag &= ~BONE_ROOTSEL;
2434                 }
2435                 else {
2436                         if (data->select)
2437                                 ebone->flag |= BONE_TIPSEL;
2438                         else 
2439                                 ebone->flag &= ~BONE_TIPSEL;
2440                 }
2441                 return 1;
2442         }
2443         return 0;
2444 }
2445 static void armature_circle_select(ViewContext *vc, int select, const int mval[2], float rad)
2446 {
2447         CircleSelectUserData data;
2448         bArmature *arm = vc->obedit->data;
2449         EditBone *ebone;
2450         int change = FALSE;
2451         
2452         /* set vc->edit data */
2453         data.select = select;
2454         data.mval[0] = mval[0];
2455         data.mval[1] = mval[1];
2456         data.radius = rad;
2457
2458         ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d); /* for foreach's screen/vert projection */
2459         
2460         /* check each EditBone... */
2461         // TODO: could be optimized at some point
2462         for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
2463                 short sco1[2], sco2[2], didpoint = 0;
2464                 float vec[3];
2465                 
2466                 /* project head location to screenspace */
2467                 mul_v3_m4v3(vec, vc->obedit->obmat, ebone->head);
2468                 project_short(vc->ar, vec, sco1);
2469                 
2470                 /* project tail location to screenspace */
2471                 mul_v3_m4v3(vec, vc->obedit->obmat, ebone->tail);
2472                 project_short(vc->ar, vec, sco2);
2473                 
2474                 /* check if the head and/or tail is in the circle 
2475                  *      - the call to check also does the selection already
2476                  */
2477                 if (armature_circle_doSelectJoint(&data, ebone, sco1[0], sco1[1], 1))
2478                         didpoint = 1;
2479                 if (armature_circle_doSelectJoint(&data, ebone, sco2[0], sco2[1], 0))
2480                         didpoint = 1;
2481                         
2482                 /* only if the endpoints didn't get selected, deal with the middle of the bone too */
2483                 // XXX should we just do this always?
2484                 if ( (didpoint == 0) && edge_inside_circle(mval[0], mval[1], rad, sco1[0], sco1[1], sco2[0], sco2[1]) ) {
2485                         if (select) 
2486                                 ebone->flag |= BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED;
2487                         else 
2488                                 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
2489                         change = TRUE;
2490                 }
2491                 
2492                 change |= didpoint;
2493         }
2494
2495         if (change) {
2496                 ED_armature_sync_selection(arm->edbo);
2497                 ED_armature_validate_active(arm);
2498                 WM_main_add_notifier(NC_OBJECT | ND_BONE_SELECT, vc->obedit);
2499         }
2500 }
2501
2502 /** Callbacks for circle selection in Editmode */
2503
2504 static void obedit_circle_select(ViewContext *vc, short select, const int mval[2], float rad)
2505 {
2506         switch (vc->obedit->type) {
2507                 case OB_MESH:
2508                         mesh_circle_select(vc, select, mval, rad);
2509                         break;
2510                 case OB_CURVE:
2511                 case OB_SURF:
2512                         nurbscurve_circle_select(vc, select, mval, rad);
2513                         break;
2514                 case OB_LATTICE:
2515                         lattice_circle_select(vc, select, mval, rad);
2516                         break;
2517                 case OB_ARMATURE:
2518                         armature_circle_select(vc, select, mval, rad);
2519                         break;
2520                 default:
2521                         return;
2522         }
2523 }
2524
2525 /* not a real operator, only for circle test */
2526 static int view3d_circle_select_exec(bContext *C, wmOperator *op)
2527 {
2528         ScrArea *sa = CTX_wm_area(C);
2529         ARegion *ar = CTX_wm_region(C);
2530         Scene *scene = CTX_data_scene(C);
2531         Object *obact = CTX_data_active_object(C);
2532         View3D *v3d = sa->spacedata.first;
2533         int x = RNA_int_get(op->ptr, "x");
2534         int y = RNA_int_get(op->ptr, "y");
2535         int radius = RNA_int_get(op->ptr, "radius");
2536         int gesture_mode = RNA_int_get(op->ptr, "gesture_mode");
2537         int select;
2538         
2539         select = (gesture_mode == GESTURE_MODAL_SELECT);
2540
2541         if (CTX_data_edit_object(C) || paint_facesel_test(obact) || paint_vertsel_test(obact) ||
2542             (obact && (obact->mode & (OB_MODE_PARTICLE_EDIT | OB_MODE_POSE))) )
2543         {
2544                 ViewContext vc;
2545                 int mval[2];
2546                 
2547                 view3d_operator_needs_opengl(C);
2548                 
2549                 view3d_set_viewcontext(C, &vc);
2550                 mval[0] = x;
2551                 mval[1] = y;
2552
2553                 if (CTX_data_edit_object(C)) {
2554                         obedit_circle_select(&vc, select, mval, (float)radius);
2555                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2556                 }
2557                 else if (paint_facesel_test(obact)) {
2558                         paint_facesel_circle_select(&vc, select, mval, (float)radius);
2559                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2560                 }
2561                 else if (paint_vertsel_test(obact)) {
2562                         paint_vertsel_circle_select(&vc, select, mval, (float)radius);
2563                         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obact->data);
2564                 }
2565                 else if (obact->mode & OB_MODE_POSE)
2566                         pose_circle_select(&vc, select, mval, (float)radius);
2567                 else
2568                         return PE_circle_select(C, select, mval, (float)radius);
2569         }
2570         else if (obact && obact->mode & OB_MODE_SCULPT) {
2571                 return OPERATOR_CANCELLED;
2572         }
2573         else {
2574                 Base *base;
2575                 select = select ? BA_SELECT : BA_DESELECT;
2576                 for (base = FIRSTBASE; base; base = base->next) {
2577                         if (BASE_SELECTABLE(v3d, base)) {
2578                                 project_short(ar, base->object->obmat[3], &base->sx);
2579                                 if (base->sx != IS_CLIPPED) {
2580                                         int dx = base->sx - x;
2581                                         int dy = base->sy - y;
2582                                         if (dx * dx + dy * dy < radius * radius)
2583                                                 ED_base_object_select(base, select);
2584                                 }
2585                         }
2586                 }
2587                 
2588                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
2589         }
2590         
2591         return OPERATOR_FINISHED;
2592 }
2593
2594 void VIEW3D_OT_select_circle(wmOperatorType *ot)
2595 {
2596         ot->name = "Circle Select";
2597         ot->description = "Select items using circle selection";
2598         ot->idname = "VIEW3D_OT_select_circle";
2599         
2600         ot->invoke = WM_gesture_circle_invoke;
2601         ot->modal = WM_gesture_circle_modal;
2602         ot->exec = view3d_circle_select_exec;
2603         ot->poll = view3d_selectable_data;
2604         ot->cancel = WM_gesture_circle_cancel;
2605         
2606         /* flags */
2607         ot->flag = OPTYPE_UNDO;
2608         
2609         RNA_def_int(ot->srna, "x", 0, INT_MIN, INT_MAX, "X", "", INT_MIN, INT_MAX);
2610         RNA_def_int(ot->srna, "y", 0, INT_MIN, INT_MAX, "Y", "", INT_MIN, INT_MAX);
2611         RNA_def_int(ot->srna, "radius", 0, INT_MIN, INT_MAX, "Radius", "", INT_MIN, INT_MAX);
2612         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
2613 }