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