Object mode select grouped operator (Shift+G and Select manu)
[blender.git] / source / blender / editors / space_view3d / space_view3d.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_action_types.h"
33 #include "DNA_armature_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_screen_types.h"
38 #include "DNA_view3d_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_arithb.h"
44 #include "BLI_rand.h"
45
46 #include "BKE_action.h"
47 #include "BKE_context.h"
48 #include "BKE_global.h"
49 #include "BKE_screen.h"
50 #include "BKE_utildefines.h"
51
52 #include "ED_armature.h"
53 #include "ED_space_api.h"
54 #include "ED_screen.h"
55
56 #include "BIF_gl.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
64
65 #include "RNA_access.h"
66
67 #include "view3d_intern.h"      // own include
68
69 /* ******************** manage regions ********************* */
70
71 ARegion *view3d_has_buttons_region(ScrArea *sa)
72 {
73         ARegion *ar, *arnew;
74         
75         for(ar= sa->regionbase.first; ar; ar= ar->next)
76                 if(ar->regiontype==RGN_TYPE_UI)
77                         return ar;
78         
79         /* add subdiv level; after header */
80         for(ar= sa->regionbase.first; ar; ar= ar->next)
81                 if(ar->regiontype==RGN_TYPE_HEADER)
82                         break;
83         
84         /* is error! */
85         if(ar==NULL) return NULL;
86         
87         arnew= MEM_callocN(sizeof(ARegion), "buttons for view3d");
88         
89         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
90         arnew->regiontype= RGN_TYPE_UI;
91         arnew->alignment= RGN_ALIGN_RIGHT;
92         
93         arnew->flag = RGN_FLAG_HIDDEN;
94         
95         return arnew;
96 }
97
98 ARegion *view3d_has_tools_region(ScrArea *sa)
99 {
100         ARegion *ar, *artool=NULL, *arprops=NULL, *arhead;
101         
102         for(ar= sa->regionbase.first; ar; ar= ar->next) {
103                 if(ar->regiontype==RGN_TYPE_TOOLS)
104                         artool= ar;
105                 if(ar->regiontype==RGN_TYPE_TOOL_PROPS)
106                         arprops= ar;
107         }
108         
109         /* tool region hide/unhide also hides props */
110         if(arprops && artool) return artool;
111         
112         if(artool==NULL) {
113                 /* add subdiv level; after header */
114                 for(arhead= sa->regionbase.first; arhead; arhead= arhead->next)
115                         if(arhead->regiontype==RGN_TYPE_HEADER)
116                                 break;
117                 
118                 /* is error! */
119                 if(arhead==NULL) return NULL;
120                 
121                 artool= MEM_callocN(sizeof(ARegion), "tools for view3d");
122                 
123                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
124                 artool->regiontype= RGN_TYPE_TOOLS;
125                 artool->alignment= RGN_OVERLAP_LEFT;
126                 artool->flag = RGN_FLAG_HIDDEN;
127         }
128
129         if(arprops==NULL) {
130                 /* add extra subdivided region for tool properties */
131                 arprops= MEM_callocN(sizeof(ARegion), "tool props for view3d");
132                 
133                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
134                 arprops->regiontype= RGN_TYPE_TOOL_PROPS;
135                 arprops->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
136         }
137         
138         return artool;
139 }
140
141 /* ****************************************************** */
142
143 /* function to always find a regionview3d context inside 3D window */
144 RegionView3D *ED_view3d_context_rv3d(bContext *C)
145 {
146         RegionView3D *rv3d= CTX_wm_region_view3d(C);
147         
148         if(rv3d==NULL) {
149                 ScrArea *sa =CTX_wm_area(C);
150                 if(sa->spacetype==SPACE_VIEW3D) {
151                         ARegion *ar;
152                         for(ar= sa->regionbase.first; ar; ar= ar->next)
153                                 if(ar->regiontype==RGN_TYPE_WINDOW)
154                                         return ar->regiondata;
155                 }
156         }
157         return rv3d;
158 }
159
160
161 /* ******************** default callbacks for view3d space ***************** */
162
163 static SpaceLink *view3d_new(const bContext *C)
164 {
165         Scene *scene= CTX_data_scene(C);
166         ARegion *ar;
167         View3D *v3d;
168         RegionView3D *rv3d;
169         
170         v3d= MEM_callocN(sizeof(View3D), "initview3d");
171         v3d->spacetype= SPACE_VIEW3D;
172         v3d->blockscale= 0.7f;
173         v3d->lay= v3d->layact= 1;
174         if(scene) {
175                 v3d->lay= v3d->layact= scene->lay;
176                 v3d->camera= scene->camera;
177         }
178         v3d->scenelock= 1;
179         v3d->grid= 1.0f;
180         v3d->gridlines= 16;
181         v3d->gridsubdiv = 10;
182         v3d->drawtype= OB_WIRE;
183         
184         v3d->gridflag |= V3D_SHOW_X;
185         v3d->gridflag |= V3D_SHOW_Y;
186         v3d->gridflag |= V3D_SHOW_FLOOR;
187         v3d->gridflag &= ~V3D_SHOW_Z;
188         
189         v3d->lens= 35.0f;
190         v3d->near= 0.01f;
191         v3d->far= 500.0f;
192         
193         /* header */
194         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
195         
196         BLI_addtail(&v3d->regionbase, ar);
197         ar->regiontype= RGN_TYPE_HEADER;
198         ar->alignment= RGN_ALIGN_BOTTOM;
199         
200         /* buttons/list view */
201         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
202         
203         BLI_addtail(&v3d->regionbase, ar);
204         ar->regiontype= RGN_TYPE_UI;
205         ar->alignment= RGN_ALIGN_LEFT;
206         ar->flag = RGN_FLAG_HIDDEN;
207         
208         /* main area */
209         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
210         
211         BLI_addtail(&v3d->regionbase, ar);
212         ar->regiontype= RGN_TYPE_WINDOW;
213         
214         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
215         rv3d= ar->regiondata;
216         rv3d->viewquat[0]= 1.0f;
217         rv3d->persp= 1;
218         rv3d->view= 7;
219         rv3d->dist= 10.0;
220         
221         return (SpaceLink *)v3d;
222 }
223
224 /* not spacelink itself */
225 static void view3d_free(SpaceLink *sl)
226 {
227         View3D *vd= (View3D *) sl;
228         
229         if(vd->bgpic) {
230                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
231                 MEM_freeN(vd->bgpic);
232         }
233         
234         if(vd->localvd) MEM_freeN(vd->localvd);
235         
236         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
237         
238 }
239
240
241 /* spacetype; init callback */
242 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
243 {
244
245 }
246
247 static SpaceLink *view3d_duplicate(SpaceLink *sl)
248 {
249         View3D *v3do= (View3D *)sl;
250         View3D *v3dn= MEM_dupallocN(sl);
251         
252         /* clear or remove stuff from old */
253         
254 // XXX  BIF_view3d_previewrender_free(v3do);
255         
256         if(v3do->localvd) {
257                 v3do->localvd= NULL;
258                 v3do->properties_storage= NULL;
259                 v3do->localview= 0;
260                 v3do->lay= v3dn->localvd->lay;
261                 v3do->lay &= 0xFFFFFF;
262         }
263         
264         /* copy or clear inside new stuff */
265
266         if(v3dn->bgpic) {
267                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
268                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
269         }
270         v3dn->properties_storage= NULL;
271         
272         return (SpaceLink *)v3dn;
273 }
274
275 static void view3d_modal_keymaps(wmWindowManager *wm, ARegion *ar, int stype)
276 {
277         RegionView3D *rv3d= ar->regiondata;
278         ListBase *keymap;
279         
280         /* copy last mode, then we can re-init the region maps */
281         rv3d->lastmode= stype;
282         
283         keymap= WM_keymap_listbase(wm, "Object Mode", 0, 0);
284         if(ELEM(stype, 0, NS_MODE_OBJECT))
285                 WM_event_add_keymap_handler(&ar->handlers, keymap);
286         else
287                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
288         
289         keymap= WM_keymap_listbase(wm, "EditMesh", 0, 0);
290         if(stype==NS_EDITMODE_MESH)
291                 WM_event_add_keymap_handler(&ar->handlers, keymap);
292         else
293                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
294         
295         keymap= WM_keymap_listbase(wm, "Curve", 0, 0);
296         if(stype==NS_EDITMODE_CURVE)
297                 WM_event_add_keymap_handler(&ar->handlers, keymap);
298         else
299                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
300         
301         keymap= WM_keymap_listbase(wm, "Armature", 0, 0);
302         if(stype==NS_EDITMODE_ARMATURE)
303                 WM_event_add_keymap_handler(&ar->handlers, keymap);
304         else
305                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
306
307         keymap= WM_keymap_listbase(wm, "Lattice", 0, 0);
308         if(stype==NS_EDITMODE_LATTICE)
309                 WM_event_add_keymap_handler(&ar->handlers, keymap);
310         else
311                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
312
313         /* armature sketching needs to take over mouse */
314         keymap= WM_keymap_listbase(wm, "Armature_Sketch", 0, 0);
315         if(stype==NS_EDITMODE_TEXT)
316                 WM_event_add_keymap_handler_priority(&ar->handlers, keymap, 10);
317         else
318                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
319
320         keymap= WM_keymap_listbase(wm, "Particle", 0, 0);
321         if(stype==NS_MODE_PARTICLE)
322                 WM_event_add_keymap_handler(&ar->handlers, keymap);
323         else
324                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
325
326         /* editfont keymap swallows all... */
327         keymap= WM_keymap_listbase(wm, "Font", 0, 0);
328         if(stype==NS_EDITMODE_TEXT)
329                 WM_event_add_keymap_handler_priority(&ar->handlers, keymap, 10);
330         else
331                 WM_event_remove_keymap_handler(&ar->handlers, keymap);
332 }
333
334 /* add handlers, stuff you only do once or on area/region changes */
335 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
336 {
337         RegionView3D *rv3d= ar->regiondata;
338         ListBase *keymap;
339         
340         /* own keymap */
341         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
342         WM_event_add_keymap_handler(&ar->handlers, keymap);
343         keymap= WM_keymap_listbase(wm, "View3D", SPACE_VIEW3D, 0);
344         WM_event_add_keymap_handler(&ar->handlers, keymap);
345         
346         /* object ops. */
347         keymap= WM_keymap_listbase(wm, "Object Non-modal", 0, 0);
348         WM_event_add_keymap_handler(&ar->handlers, keymap);
349         
350         /* pose is not modal, operator poll checks for this */
351         keymap= WM_keymap_listbase(wm, "Pose", 0, 0);
352         WM_event_add_keymap_handler(&ar->handlers, keymap);
353         
354         /* modal ops keymaps */
355         view3d_modal_keymaps(wm, ar, rv3d->lastmode);
356         /* operator poll checks for modes */
357         keymap= WM_keymap_listbase(wm, "ImagePaint", 0, 0);
358         WM_event_add_keymap_handler(&ar->handlers, keymap);
359 }
360
361 /* type callback, not region itself */
362 static void view3d_main_area_free(ARegion *ar)
363 {
364         RegionView3D *rv3d= ar->regiondata;
365         
366         if(rv3d) {
367                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
368                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
369
370                 // XXX  retopo_free_view_data(rv3d);
371                 if(rv3d->ri) { 
372                         // XXX          BIF_view3d_previewrender_free(rv3d);
373                 }
374                 
375                 if(rv3d->depths) {
376                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
377                         MEM_freeN(rv3d->depths);
378                 }
379                 MEM_freeN(rv3d);
380                 ar->regiondata= NULL;
381         }
382 }
383
384 /* copy regiondata */
385 static void *view3d_main_area_duplicate(void *poin)
386 {
387         if(poin) {
388                 RegionView3D *rv3d= poin, *new;
389         
390                 new= MEM_dupallocN(rv3d);
391                 if(rv3d->localvd) 
392                         new->localvd= MEM_dupallocN(rv3d->localvd);
393                 if(rv3d->clipbb) 
394                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
395                 
396                 new->depths= NULL;
397                 new->retopo_view_data= NULL;
398                 new->ri= NULL;
399                 new->gpd= NULL;
400                 new->sms= NULL;
401                 new->smooth_timer= NULL;
402                 
403                 return new;
404         }
405         return NULL;
406 }
407
408 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
409 {
410         /* context changes */
411         switch(wmn->category) {
412                 case NC_ANIMATION:
413                         switch(wmn->data) {
414                                 case ND_KEYFRAME_EDIT:
415                                 case ND_KEYFRAME_PROP:
416                                 case ND_NLA_ACTCHANGE:
417                                 case ND_ANIMCHAN_SELECT:
418                                         ED_region_tag_redraw(ar);
419                                         break;
420                         }
421                 case NC_SCENE:
422                         switch(wmn->data) {
423                                 case ND_TRANSFORM:
424                                 case ND_FRAME:
425                                 case ND_OB_ACTIVE:
426                                 case ND_OB_SELECT:
427                                         ED_region_tag_redraw(ar);
428                                         break;
429                                 case ND_MODE:
430                                         view3d_modal_keymaps(wmn->wm, ar, wmn->subtype);
431                                         ED_region_tag_redraw(ar);
432                                         break;
433                         }
434                         break;
435                 case NC_OBJECT:
436                         switch(wmn->data) {
437                                 case ND_BONE_ACTIVE:
438                                 case ND_BONE_SELECT:
439                                 case ND_TRANSFORM:
440                                 case ND_GEOM_SELECT:
441                                 case ND_GEOM_DATA:
442                                 case ND_DRAW:
443                                 case ND_MODIFIER:
444                                 case ND_CONSTRAINT:
445                                 case ND_KEYS:
446                                 case ND_PARTICLE:
447                                         ED_region_tag_redraw(ar);
448                                         break;
449                         }
450                 case NC_GROUP:
451                         /* all group ops for now */
452                         ED_region_tag_redraw(ar);
453                         break;
454                 case NC_MATERIAL:
455                         switch(wmn->data) {
456                                 case ND_SHADING_DRAW:
457                                         ED_region_tag_redraw(ar);
458                                         break;
459                         }
460                 case NC_LAMP:
461                         switch(wmn->data) {
462                                 case ND_LIGHTING_DRAW:
463                                         ED_region_tag_redraw(ar);
464                                         break;
465                         }
466                 case NC_IMAGE:  
467                         /* this could be more fine grained checks if we had
468                          * more context than just the region */
469                         ED_region_tag_redraw(ar);
470                         break;
471         }
472 }
473
474 /* concept is to retrieve cursor type context-less */
475 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
476 {
477         Scene *scene= win->screen->scene;
478
479         if(scene->obedit) {
480                 WM_cursor_set(win, CURSOR_EDIT);
481         }
482         else {
483                 WM_cursor_set(win, CURSOR_STD);
484         }
485 }
486
487 /* add handlers, stuff you only do once or on area/region changes */
488 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
489 {
490         ListBase *keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
491         
492         WM_event_add_keymap_handler(&ar->handlers, keymap);
493         
494         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
495 }
496
497 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
498 {
499         float col[3];
500         
501         /* clear */
502         if(ED_screen_area_active(C))
503                 UI_GetThemeColor3fv(TH_HEADER, col);
504         else
505                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
506         
507         glClearColor(col[0], col[1], col[2], 0.0);
508         glClear(GL_COLOR_BUFFER_BIT);
509         
510         /* set view2d view matrix for scrolling (without scrollers) */
511         UI_view2d_view_ortho(C, &ar->v2d);
512         
513         view3d_header_buttons(C, ar);
514         
515         /* restore view matrix? */
516         UI_view2d_view_restore(C);
517 }
518
519 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
520 {
521         /* context changes */
522         switch(wmn->category) {
523                 case NC_SCENE:
524                         switch(wmn->data) {
525                                 case ND_FRAME:
526                                 case ND_OB_ACTIVE:
527                                 case ND_OB_SELECT:
528                                 case ND_MODE:
529                                         ED_region_tag_redraw(ar);
530                                         break;
531                         }
532                         break;
533         }
534 }
535
536 /* add handlers, stuff you only do once or on area/region changes */
537 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
538 {
539         ListBase *keymap;
540
541         ED_region_panels_init(wm, ar);
542         
543         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
544         WM_event_add_keymap_handler(&ar->handlers, keymap);
545 }
546
547 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
548 {
549         ED_region_panels(C, ar, 1, NULL);
550 }
551
552 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
553 {
554         /* context changes */
555         switch(wmn->category) {
556                 case NC_SCENE:
557                         switch(wmn->data) {
558                                 case ND_FRAME:
559                                 case ND_OB_ACTIVE:
560                                 case ND_OB_SELECT:
561                                 case ND_MODE:
562                                         ED_region_tag_redraw(ar);
563                                         break;
564                         }
565                         break;
566                 case NC_OBJECT:
567                         switch(wmn->data) {
568                                 case ND_BONE_ACTIVE:
569                                 case ND_BONE_SELECT:
570                                 case ND_TRANSFORM:
571                                 case ND_GEOM_SELECT:
572                                 case ND_GEOM_DATA:
573                                 case ND_DRAW:
574                                 case ND_KEYS:
575                                         ED_region_tag_redraw(ar);
576                                         break;
577                         }
578         }
579 }
580
581 /* add handlers, stuff you only do once or on area/region changes */
582 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
583 {
584         ListBase *keymap;
585         
586         ED_region_panels_init(wm, ar);
587
588         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
589         WM_event_add_keymap_handler(&ar->handlers, keymap);
590 }
591
592
593
594 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
595 {
596         ED_region_panels(C, ar, 1, view3d_context_string(C));
597 }
598
599 /*
600  * Returns true if the Object is a from an external blend file (libdata)
601  */
602 static int object_is_libdata(Object *ob)
603 {
604         if (!ob) return 0;
605         if (ob->proxy) return 0;
606         if (ob->id.lib) return 1;
607         return 0;
608 }
609
610 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
611 {
612         View3D *v3d= CTX_wm_view3d(C);
613         Scene *scene= CTX_data_scene(C);
614         Base *base;
615
616         if(v3d==NULL) return 0;
617
618         if(CTX_data_dir(member)) {
619                 static const char *dir[] = {
620                         "selected_objects", "selected_bases" "selected_editable_objects",
621                         "selected_editable_bases" "visible_objects", "visible_bases",
622                         "active_base", "active_object", "visible_bones", "editable_bones",
623                         "selected_bones", "selected_editable_bones" "visible_pchans",
624                         "selected_pchans", "active_bone", "active_pchan", NULL};
625
626                 CTX_data_dir_set(result, dir);
627         }
628         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
629                 int selected_objects= CTX_data_equals(member, "selected_objects");
630
631                 for(base=scene->base.first; base; base=base->next) {
632                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
633                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
634                                         if(selected_objects)
635                                                 CTX_data_id_list_add(result, &base->object->id);
636                                         else
637                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
638                                 }
639                         }
640                 }
641
642                 return 1;
643         }
644         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
645                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
646
647                 for(base=scene->base.first; base; base=base->next) {
648                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
649                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
650                                         if(0==object_is_libdata(base->object)) {
651                                                 if(selected_editable_objects)
652                                                         CTX_data_id_list_add(result, &base->object->id);
653                                                 else
654                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
655                                         }
656                                 }
657                         }
658                 }
659                 
660                 return 1;
661         }
662         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
663                 int visible_objects= CTX_data_equals(member, "visible_objects");
664
665                 for(base=scene->base.first; base; base=base->next) {
666                         if(base->lay & v3d->lay) {
667                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
668                                         if(visible_objects)
669                                                 CTX_data_id_list_add(result, &base->object->id);
670                                         else
671                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
672                                 }
673                         }
674                 }
675                 
676                 return 1;
677         }
678         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
679                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
680
681                 for(base=scene->base.first; base; base=base->next) {
682                         if(base->lay & v3d->lay) {
683                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
684                                         if(selectable_objects)
685                                                 CTX_data_id_list_add(result, &base->object->id);
686                                         else
687                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
688                                 }
689                         }
690                 }
691                 
692                 return 1;
693         }
694         else if(CTX_data_equals(member, "active_base")) {
695                 if(scene->basact && (scene->basact->lay & v3d->lay))
696                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
697                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
698                 
699                 return 1;
700         }
701         else if(CTX_data_equals(member, "active_object")) {
702                 if(scene->basact && (scene->basact->lay & v3d->lay))
703                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
704                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
705                 
706                 return 1;
707         }
708         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
709                 Object *obedit= scene->obedit; // XXX get from context?
710                 bArmature *arm= (obedit) ? obedit->data : NULL;
711                 EditBone *ebone, *flipbone=NULL;
712                 int editable_bones= CTX_data_equals(member, "editable_bones");
713                 
714                 if (arm && arm->edbo) {
715                         /* Attention: X-Axis Mirroring is also handled here... */
716                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
717                                 /* first and foremost, bone must be visible and selected */
718                                 if (EBONE_VISIBLE(arm, ebone)) {
719                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
720                                          * so that most users of this data don't need to explicitly check for it themselves.
721                                          * 
722                                          * We need to make sure that these mirrored copies are not selected, otherwise some
723                                          * bones will be operated on twice.
724                                          */
725                                         if (arm->flag & ARM_MIRROR_EDIT)
726                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
727                                         
728                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
729                                         if (editable_bones) {
730                                                 /* only selected + editable */
731                                                 if (EBONE_EDITABLE(ebone)) {
732                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
733                                                 
734                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
735                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
736                                                 }
737                                         }
738                                         else {
739                                                 /* only include bones if visible */
740                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
741                                                 
742                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
743                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
744                                         }
745                                 }
746                         }       
747                         
748                         return 1;
749                 }
750         }
751         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
752                 Object *obedit= scene->obedit; // XXX get from context?
753                 bArmature *arm= (obedit) ? obedit->data : NULL;
754                 EditBone *ebone, *flipbone=NULL;
755                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
756                 
757                 if (arm && arm->edbo) {
758                         /* Attention: X-Axis Mirroring is also handled here... */
759                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
760                                 /* first and foremost, bone must be visible and selected */
761                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
762                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
763                                          * so that most users of this data don't need to explicitly check for it themselves.
764                                          * 
765                                          * We need to make sure that these mirrored copies are not selected, otherwise some
766                                          * bones will be operated on twice.
767                                          */
768                                         if (arm->flag & ARM_MIRROR_EDIT)
769                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
770                                         
771                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
772                                         if (selected_editable_bones) {
773                                                 /* only selected + editable */
774                                                 if (EBONE_EDITABLE(ebone)) {
775                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
776                                                 
777                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
778                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
779                                                 }
780                                         }
781                                         else {
782                                                 /* only include bones if selected */
783                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
784                                                 
785                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
786                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
787                                         }
788                                 }
789                         }       
790                         
791                         return 1;
792                 }
793         }
794         else if(CTX_data_equals(member, "visible_pchans")) {
795                 Object *obact= OBACT;
796                 bArmature *arm= (obact) ? obact->data : NULL;
797                 bPoseChannel *pchan;
798                 
799                 if (obact && arm) {
800                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
801                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
802                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
803                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
804                                 }
805                         }
806                         
807                         return 1;
808                 }
809         }
810         else if(CTX_data_equals(member, "selected_pchans")) {
811                 Object *obact= OBACT;
812                 bArmature *arm= (obact) ? obact->data : NULL;
813                 bPoseChannel *pchan;
814                 
815                 if (obact && arm) {
816                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
817                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
818                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
819                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
820                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
821                                 }
822                         }
823                         
824                         return 1;
825                 }
826         }
827         else if(CTX_data_equals(member, "active_bone")) {
828                 Object *obedit= scene->obedit; // XXX get from context?
829                 bArmature *arm= (obedit) ? obedit->data : NULL;
830                 EditBone *ebone;
831                 
832                 if (arm && arm->edbo) {
833                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
834                                 if (EBONE_VISIBLE(arm, ebone)) {
835                                         if (ebone->flag & BONE_ACTIVE) {
836                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
837                                                 
838                                                 return 1;
839                                         }
840                                 }
841                         }
842                 }
843                 
844         }
845         else if(CTX_data_equals(member, "active_pchan")) {
846                 Object *obact= OBACT;
847                 bPoseChannel *pchan;
848                 
849                 pchan= get_active_posechannel(obact);
850                 if (pchan) {
851                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
852                         return 1;
853                 }
854         }
855
856         return 0;
857 }
858
859 /* only called once, from space/spacetypes.c */
860 void ED_spacetype_view3d(void)
861 {
862         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
863         ARegionType *art;
864         
865         st->spaceid= SPACE_VIEW3D;
866         
867         st->new= view3d_new;
868         st->free= view3d_free;
869         st->init= view3d_init;
870         st->duplicate= view3d_duplicate;
871         st->operatortypes= view3d_operatortypes;
872         st->keymap= view3d_keymap;
873         st->context= view3d_context;
874         
875         /* regions: main window */
876         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
877         art->regionid = RGN_TYPE_WINDOW;
878         art->keymapflag= ED_KEYMAP_FRAMES;
879         art->draw= view3d_main_area_draw;
880         art->init= view3d_main_area_init;
881         art->free= view3d_main_area_free;
882         art->duplicate= view3d_main_area_duplicate;
883         art->listener= view3d_main_area_listener;
884         art->cursor= view3d_main_area_cursor;
885         BLI_addhead(&st->regiontypes, art);
886         
887         /* regions: listview/buttons */
888         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
889         art->regionid = RGN_TYPE_UI;
890         art->minsizex= 220; // XXX
891         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
892         art->listener= view3d_buttons_area_listener;
893         art->init= view3d_buttons_area_init;
894         art->draw= view3d_buttons_area_draw;
895         BLI_addhead(&st->regiontypes, art);
896
897         view3d_buttons_register(art);
898
899         /* regions: tool(bar) */
900         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
901         art->regionid = RGN_TYPE_TOOLS;
902         art->minsizex= 120; // XXX
903         art->minsizey= 50; // XXX
904         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
905         art->listener= view3d_buttons_area_listener;
906         art->init= view3d_tools_area_init;
907         art->draw= view3d_tools_area_draw;
908         BLI_addhead(&st->regiontypes, art);
909         
910         view3d_toolbar_register(art);
911
912         /* regions: tool properties */
913         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
914         art->regionid = RGN_TYPE_TOOL_PROPS;
915         art->minsizex= 0;
916         art->minsizey= 120;
917         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
918         art->listener= view3d_buttons_area_listener;
919         art->init= view3d_tools_area_init;
920         art->draw= view3d_tools_area_draw;
921         BLI_addhead(&st->regiontypes, art);
922         
923         view3d_tool_props_register(art);
924         
925         
926         /* regions: header */
927         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
928         art->regionid = RGN_TYPE_HEADER;
929         art->minsizey= HEADERY;
930         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
931         art->listener= view3d_header_area_listener;
932         art->init= view3d_header_area_init;
933         art->draw= view3d_header_area_draw;
934         BLI_addhead(&st->regiontypes, art);
935         
936         BKE_spacetype_register(st);
937 }
938