Keymaps now have a poll() function, rather than adding/removing
[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->localview= 0;
263                 v3do->lay= v3dn->localvd->lay;
264                 v3do->lay &= 0xFFFFFF;
265         }
266         
267         /* copy or clear inside new stuff */
268
269         if(v3dn->bgpic) {
270                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
271                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
272         }
273         v3dn->properties_storage= NULL;
274         
275         return (SpaceLink *)v3dn;
276 }
277
278 /* add handlers, stuff you only do once or on area/region changes */
279 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
280 {
281         wmKeyMap *keymap;
282         
283         /* own keymap */
284         keymap= WM_keymap_find(wm, "View3D Generic", SPACE_VIEW3D, 0);
285         WM_event_add_keymap_handler(&ar->handlers, keymap);
286
287         keymap= WM_keymap_find(wm, "View3D", SPACE_VIEW3D, 0);
288         WM_event_add_keymap_handler(&ar->handlers, keymap);
289         
290         /* object ops. */
291         keymap= WM_keymap_find(wm, "Object Non-modal", 0, 0);
292         WM_event_add_keymap_handler(&ar->handlers, keymap);
293         
294         /* pose is not modal, operator poll checks for this */
295         keymap= WM_keymap_find(wm, "Pose", 0, 0);
296         WM_event_add_keymap_handler(&ar->handlers, keymap);
297         
298         /* operator poll checks for modes */
299         keymap= WM_keymap_find(wm, "ImagePaint", 0, 0);
300         WM_event_add_keymap_handler(&ar->handlers, keymap);
301
302         keymap= WM_keymap_find(wm, "Object Mode", 0, 0);
303         WM_event_add_keymap_handler(&ar->handlers, keymap);
304         
305         keymap= WM_keymap_find(wm, "EditMesh", 0, 0);
306         WM_event_add_keymap_handler(&ar->handlers, keymap);
307         
308         keymap= WM_keymap_find(wm, "Curve", 0, 0);
309         WM_event_add_keymap_handler(&ar->handlers, keymap);
310         
311         keymap= WM_keymap_find(wm, "Armature", 0, 0);
312         WM_event_add_keymap_handler(&ar->handlers, keymap);
313
314         keymap= WM_keymap_find(wm, "Pose", 0, 0);
315         WM_event_add_keymap_handler(&ar->handlers, keymap);
316
317         keymap= WM_keymap_find(wm, "Metaball", 0, 0);
318         WM_event_add_keymap_handler(&ar->handlers, keymap);
319         
320         keymap= WM_keymap_find(wm, "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, "Armature_Sketch", 0, 0);
325         WM_event_add_keymap_handler(&ar->handlers, keymap);
326
327         keymap= WM_keymap_find(wm, "Particle", 0, 0);
328         WM_event_add_keymap_handler(&ar->handlers, keymap);
329
330         /* editfont keymap swallows all... */
331         keymap= WM_keymap_find(wm, "Font", 0, 0);
332         WM_event_add_keymap_handler(&ar->handlers, keymap);
333 }
334
335 /* type callback, not region itself */
336 static void view3d_main_area_free(ARegion *ar)
337 {
338         RegionView3D *rv3d= ar->regiondata;
339         
340         if(rv3d) {
341                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
342                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
343
344                 // XXX  retopo_free_view_data(rv3d);
345                 if(rv3d->ri) { 
346                         // XXX          BIF_view3d_previewrender_free(rv3d);
347                 }
348                 
349                 if(rv3d->depths) {
350                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
351                         MEM_freeN(rv3d->depths);
352                 }
353                 MEM_freeN(rv3d);
354                 ar->regiondata= NULL;
355         }
356 }
357
358 /* copy regiondata */
359 static void *view3d_main_area_duplicate(void *poin)
360 {
361         if(poin) {
362                 RegionView3D *rv3d= poin, *new;
363         
364                 new= MEM_dupallocN(rv3d);
365                 if(rv3d->localvd) 
366                         new->localvd= MEM_dupallocN(rv3d->localvd);
367                 if(rv3d->clipbb) 
368                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
369                 
370                 new->depths= NULL;
371                 new->retopo_view_data= NULL;
372                 new->ri= NULL;
373                 new->gpd= NULL;
374                 new->sms= NULL;
375                 new->smooth_timer= NULL;
376                 
377                 return new;
378         }
379         return NULL;
380 }
381
382 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
383 {
384         /* context changes */
385         switch(wmn->category) {
386                 case NC_ANIMATION:
387                         switch(wmn->data) {
388                                 case ND_KEYFRAME_EDIT:
389                                 case ND_KEYFRAME_PROP:
390                                 case ND_NLA_EDIT:
391                                 case ND_NLA_ACTCHANGE:
392                                 case ND_ANIMCHAN_SELECT:
393                                         ED_region_tag_redraw(ar);
394                                         break;
395                         }
396                         break;
397                 case NC_SCENE:
398                         switch(wmn->data) {
399                                 case ND_TRANSFORM:
400                                 case ND_FRAME:
401                                 case ND_OB_ACTIVE:
402                                 case ND_OB_SELECT:
403                                         ED_region_tag_redraw(ar);
404                                         break;
405                                 case ND_MODE:
406                                         ED_region_tag_redraw(ar);
407                                         break;
408                         }
409                         break;
410                 case NC_OBJECT:
411                         switch(wmn->data) {
412                                 case ND_BONE_ACTIVE:
413                                 case ND_BONE_SELECT:
414                                 case ND_TRANSFORM:
415                                 case ND_DRAW:
416                                 case ND_MODIFIER:
417                                 case ND_CONSTRAINT:
418                                 case ND_KEYS:
419                                 case ND_PARTICLE_SELECT:
420                                 case ND_PARTICLE_DATA:
421                                         ED_region_tag_redraw(ar);
422                                         break;
423                         }
424                         break;
425                 case NC_GEOM:
426                         switch(wmn->data) {
427                                 case ND_DATA:
428                                 case ND_SELECT:
429                                         ED_region_tag_redraw(ar);
430                                         break;
431                         }
432                         break;
433                 case NC_GROUP:
434                         /* all group ops for now */
435                         ED_region_tag_redraw(ar);
436                         break;
437                 case NC_MATERIAL:
438                         switch(wmn->data) {
439                                 case ND_SHADING_DRAW:
440                                         ED_region_tag_redraw(ar);
441                                         break;
442                         }
443                 case NC_LAMP:
444                         switch(wmn->data) {
445                                 case ND_LIGHTING_DRAW:
446                                         ED_region_tag_redraw(ar);
447                                         break;
448                         }
449                 case NC_IMAGE:  
450                         /* this could be more fine grained checks if we had
451                          * more context than just the region */
452                         ED_region_tag_redraw(ar);
453                         break;
454                 case NC_SPACE:
455                         if(wmn->data == ND_SPACE_VIEW3D)
456                                 ED_region_tag_redraw(ar);
457                         break;
458         }
459 }
460
461 /* concept is to retrieve cursor type context-less */
462 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
463 {
464         Scene *scene= win->screen->scene;
465
466         if(scene->obedit) {
467                 WM_cursor_set(win, CURSOR_EDIT);
468         }
469         else {
470                 WM_cursor_set(win, CURSOR_STD);
471         }
472 }
473
474 /* add handlers, stuff you only do once or on area/region changes */
475 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
476 {
477         wmKeyMap *keymap= WM_keymap_find(wm, "View3D Generic", SPACE_VIEW3D, 0);
478         
479         WM_event_add_keymap_handler(&ar->handlers, keymap);
480
481         ED_region_header_init(ar);
482 }
483
484 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
485 {
486         ED_region_header(C, ar);
487 }
488
489 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
490 {
491         /* context changes */
492         switch(wmn->category) {
493                 case NC_SCENE:
494                         switch(wmn->data) {
495                                 case ND_FRAME:
496                                 case ND_OB_ACTIVE:
497                                 case ND_OB_SELECT:
498                                 case ND_MODE:
499                                         ED_region_tag_redraw(ar);
500                                         break;
501                         }
502                         break;
503                 case NC_SPACE:
504                         if(wmn->data == ND_SPACE_VIEW3D)
505                                 ED_region_tag_redraw(ar);
506                         break;
507         }
508 }
509
510 /* add handlers, stuff you only do once or on area/region changes */
511 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
512 {
513         wmKeyMap *keymap;
514
515         ED_region_panels_init(wm, ar);
516         
517         keymap= WM_keymap_find(wm, "View3D Generic", SPACE_VIEW3D, 0);
518         WM_event_add_keymap_handler(&ar->handlers, keymap);
519 }
520
521 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
522 {
523         ED_region_panels(C, ar, 1, NULL, -1);
524 }
525
526 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
527 {
528         /* context changes */
529         switch(wmn->category) {
530                 case NC_ANIMATION:
531                         switch(wmn->data) {
532                                 case ND_KEYFRAME_EDIT:
533                                 case ND_KEYFRAME_PROP:
534                                 case ND_NLA_EDIT:
535                                 case ND_NLA_ACTCHANGE:
536                                         ED_region_tag_redraw(ar);
537                                         break;
538                         }
539                         break;
540                 case NC_SCENE:
541                         switch(wmn->data) {
542                                 case ND_FRAME:
543                                 case ND_OB_ACTIVE:
544                                 case ND_OB_SELECT:
545                                 case ND_MODE:
546                                         ED_region_tag_redraw(ar);
547                                         break;
548                         }
549                         break;
550                 case NC_OBJECT:
551                         switch(wmn->data) {
552                                 case ND_BONE_ACTIVE:
553                                 case ND_BONE_SELECT:
554                                 case ND_TRANSFORM:
555                                 case ND_DRAW:
556                                 case ND_KEYS:
557                                         ED_region_tag_redraw(ar);
558                                         break;
559                         }
560                         break;
561                 case NC_GEOM:
562                         switch(wmn->data) {
563                                 case ND_DATA:
564                                 case ND_SELECT:
565                                         ED_region_tag_redraw(ar);
566                                         break;
567                         }
568                         break;
569                 case NC_SPACE:
570                         if(wmn->data == ND_SPACE_VIEW3D)
571                                 ED_region_tag_redraw(ar);
572                         break;
573         }
574 }
575
576 /* add handlers, stuff you only do once or on area/region changes */
577 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
578 {
579         wmKeyMap *keymap;
580         
581         ED_region_panels_init(wm, ar);
582
583         keymap= WM_keymap_find(wm, "View3D Generic", SPACE_VIEW3D, 0);
584         WM_event_add_keymap_handler(&ar->handlers, keymap);
585 }
586
587 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
588 {
589         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
590 }
591
592 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
593 {
594         View3D *v3d= CTX_wm_view3d(C);
595         Scene *scene= CTX_data_scene(C);
596         Base *base;
597         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
598
599         if(CTX_data_dir(member)) {
600                 static const char *dir[] = {
601                         "selected_objects", "selected_bases" "selected_editable_objects",
602                         "selected_editable_bases" "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
603                         "active_base", "active_object", "visible_bones", "editable_bones",
604                         "selected_bones", "selected_editable_bones" "visible_pchans",
605                         "selected_pchans", "active_bone", "active_pchan", NULL};
606
607                 CTX_data_dir_set(result, dir);
608         }
609         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
610                 int selected_objects= CTX_data_equals(member, "selected_objects");
611
612                 for(base=scene->base.first; base; base=base->next) {
613                         if((base->flag & SELECT) && (base->lay & lay)) {
614                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
615                                         if(selected_objects)
616                                                 CTX_data_id_list_add(result, &base->object->id);
617                                         else
618                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
619                                 }
620                         }
621                 }
622
623                 return 1;
624         }
625         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
626                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
627
628                 for(base=scene->base.first; base; base=base->next) {
629                         if((base->flag & SELECT) && (base->lay & lay)) {
630                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
631                                         if(0==object_is_libdata(base->object)) {
632                                                 if(selected_editable_objects)
633                                                         CTX_data_id_list_add(result, &base->object->id);
634                                                 else
635                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
636                                         }
637                                 }
638                         }
639                 }
640                 
641                 return 1;
642         }
643         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
644                 int visible_objects= CTX_data_equals(member, "visible_objects");
645
646                 for(base=scene->base.first; base; base=base->next) {
647                         if(base->lay & lay) {
648                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
649                                         if(visible_objects)
650                                                 CTX_data_id_list_add(result, &base->object->id);
651                                         else
652                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
653                                 }
654                         }
655                 }
656                 
657                 return 1;
658         }
659         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
660                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
661
662                 for(base=scene->base.first; base; base=base->next) {
663                         if(base->lay & lay) {
664                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
665                                         if(selectable_objects)
666                                                 CTX_data_id_list_add(result, &base->object->id);
667                                         else
668                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
669                                 }
670                         }
671                 }
672                 
673                 return 1;
674         }
675         else if(CTX_data_equals(member, "active_base")) {
676                 if(scene->basact && (scene->basact->lay & lay))
677                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
678                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
679                 
680                 return 1;
681         }
682         else if(CTX_data_equals(member, "active_object")) {
683                 if(scene->basact && (scene->basact->lay & lay))
684                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
685                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
686                 
687                 return 1;
688         }
689         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
690                 Object *obedit= scene->obedit; // XXX get from context?
691                 bArmature *arm= (obedit) ? obedit->data : NULL;
692                 EditBone *ebone, *flipbone=NULL;
693                 int editable_bones= CTX_data_equals(member, "editable_bones");
694                 
695                 if (arm && arm->edbo) {
696                         /* Attention: X-Axis Mirroring is also handled here... */
697                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
698                                 /* first and foremost, bone must be visible and selected */
699                                 if (EBONE_VISIBLE(arm, ebone)) {
700                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
701                                          * so that most users of this data don't need to explicitly check for it themselves.
702                                          * 
703                                          * We need to make sure that these mirrored copies are not selected, otherwise some
704                                          * bones will be operated on twice.
705                                          */
706                                         if (arm->flag & ARM_MIRROR_EDIT)
707                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
708                                         
709                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
710                                         if (editable_bones) {
711                                                 /* only selected + editable */
712                                                 if (EBONE_EDITABLE(ebone)) {
713                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
714                                                 
715                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
716                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
717                                                 }
718                                         }
719                                         else {
720                                                 /* only include bones if visible */
721                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
722                                                 
723                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
724                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
725                                         }
726                                 }
727                         }       
728                         
729                         return 1;
730                 }
731         }
732         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
733                 Object *obedit= scene->obedit; // XXX get from context?
734                 bArmature *arm= (obedit) ? obedit->data : NULL;
735                 EditBone *ebone, *flipbone=NULL;
736                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
737                 
738                 if (arm && arm->edbo) {
739                         /* Attention: X-Axis Mirroring is also handled here... */
740                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
741                                 /* first and foremost, bone must be visible and selected */
742                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
743                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
744                                          * so that most users of this data don't need to explicitly check for it themselves.
745                                          * 
746                                          * We need to make sure that these mirrored copies are not selected, otherwise some
747                                          * bones will be operated on twice.
748                                          */
749                                         if (arm->flag & ARM_MIRROR_EDIT)
750                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
751                                         
752                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
753                                         if (selected_editable_bones) {
754                                                 /* only selected + editable */
755                                                 if (EBONE_EDITABLE(ebone)) {
756                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
757                                                 
758                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
759                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
760                                                 }
761                                         }
762                                         else {
763                                                 /* only include bones if selected */
764                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
765                                                 
766                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
767                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
768                                         }
769                                 }
770                         }       
771                         
772                         return 1;
773                 }
774         }
775         else if(CTX_data_equals(member, "visible_pchans")) {
776                 Object *obact= OBACT;
777                 bArmature *arm= (obact) ? obact->data : NULL;
778                 bPoseChannel *pchan;
779                 
780                 if (obact && arm) {
781                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
782                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
783                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
784                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
785                                 }
786                         }
787                         
788                         return 1;
789                 }
790         }
791         else if(CTX_data_equals(member, "selected_pchans")) {
792                 Object *obact= OBACT;
793                 bArmature *arm= (obact) ? obact->data : NULL;
794                 bPoseChannel *pchan;
795                 
796                 if (obact && arm) {
797                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
798                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
799                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
800                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
801                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
802                                 }
803                         }
804                         
805                         return 1;
806                 }
807         }
808         else if(CTX_data_equals(member, "active_bone")) {
809                 Object *obedit= scene->obedit; // XXX get from context?
810                 bArmature *arm= (obedit) ? obedit->data : NULL;
811                 EditBone *ebone;
812                 
813                 if (arm && arm->edbo) {
814                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
815                                 if (EBONE_VISIBLE(arm, ebone)) {
816                                         if (ebone->flag & BONE_ACTIVE) {
817                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
818                                                 
819                                                 return 1;
820                                         }
821                                 }
822                         }
823                 }
824                 
825         }
826         else if(CTX_data_equals(member, "active_pchan")) {
827                 Object *obact= OBACT;
828                 bPoseChannel *pchan;
829                 
830                 pchan= get_active_posechannel(obact);
831                 if (pchan) {
832                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
833                         return 1;
834                 }
835         }
836
837         return 0;
838 }
839
840 /* only called once, from space/spacetypes.c */
841 void ED_spacetype_view3d(void)
842 {
843         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
844         ARegionType *art;
845         
846         st->spaceid= SPACE_VIEW3D;
847         
848         st->new= view3d_new;
849         st->free= view3d_free;
850         st->init= view3d_init;
851         st->duplicate= view3d_duplicate;
852         st->operatortypes= view3d_operatortypes;
853         st->keymap= view3d_keymap;
854         st->context= view3d_context;
855         
856         /* regions: main window */
857         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
858         art->regionid = RGN_TYPE_WINDOW;
859         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
860         art->draw= view3d_main_area_draw;
861         art->init= view3d_main_area_init;
862         art->free= view3d_main_area_free;
863         art->duplicate= view3d_main_area_duplicate;
864         art->listener= view3d_main_area_listener;
865         art->cursor= view3d_main_area_cursor;
866         BLI_addhead(&st->regiontypes, art);
867         
868         /* regions: listview/buttons */
869         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
870         art->regionid = RGN_TYPE_UI;
871         art->minsizex= 180; // XXX
872         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
873         art->listener= view3d_buttons_area_listener;
874         art->init= view3d_buttons_area_init;
875         art->draw= view3d_buttons_area_draw;
876         BLI_addhead(&st->regiontypes, art);
877
878         view3d_buttons_register(art);
879
880         /* regions: tool(bar) */
881         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
882         art->regionid = RGN_TYPE_TOOLS;
883         art->minsizex= 160; // XXX
884         art->minsizey= 50; // XXX
885         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
886         art->listener= view3d_buttons_area_listener;
887         art->init= view3d_tools_area_init;
888         art->draw= view3d_tools_area_draw;
889         BLI_addhead(&st->regiontypes, art);
890         
891         view3d_toolbar_register(art);
892
893         /* regions: tool properties */
894         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
895         art->regionid = RGN_TYPE_TOOL_PROPS;
896         art->minsizex= 0;
897         art->minsizey= 120;
898         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
899         art->listener= view3d_buttons_area_listener;
900         art->init= view3d_tools_area_init;
901         art->draw= view3d_tools_area_draw;
902         BLI_addhead(&st->regiontypes, art);
903         
904         view3d_tool_props_register(art);
905         
906         
907         /* regions: header */
908         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
909         art->regionid = RGN_TYPE_HEADER;
910         art->minsizey= HEADERY;
911         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
912         art->listener= view3d_header_area_listener;
913         art->init= view3d_header_area_init;
914         art->draw= view3d_header_area_draw;
915         BLI_addhead(&st->regiontypes, art);
916         
917         BKE_spacetype_register(st);
918 }
919