f0c3a2c200c491fb403b5da102951fdc997ef7b1
[blender-staging.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         ED_region_header_init(ar);
495 }
496
497 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
498 {
499         ED_region_header(C, ar);
500 }
501
502 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
503 {
504         /* context changes */
505         switch(wmn->category) {
506                 case NC_SCENE:
507                         switch(wmn->data) {
508                                 case ND_FRAME:
509                                 case ND_OB_ACTIVE:
510                                 case ND_OB_SELECT:
511                                 case ND_MODE:
512                                         ED_region_tag_redraw(ar);
513                                         break;
514                         }
515                         break;
516         }
517 }
518
519 /* add handlers, stuff you only do once or on area/region changes */
520 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
521 {
522         ListBase *keymap;
523
524         ED_region_panels_init(wm, ar);
525         
526         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
527         WM_event_add_keymap_handler(&ar->handlers, keymap);
528 }
529
530 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
531 {
532         ED_region_panels(C, ar, 1, NULL);
533 }
534
535 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
536 {
537         /* context changes */
538         switch(wmn->category) {
539                 case NC_SCENE:
540                         switch(wmn->data) {
541                                 case ND_FRAME:
542                                 case ND_OB_ACTIVE:
543                                 case ND_OB_SELECT:
544                                 case ND_MODE:
545                                         ED_region_tag_redraw(ar);
546                                         break;
547                         }
548                         break;
549                 case NC_OBJECT:
550                         switch(wmn->data) {
551                                 case ND_BONE_ACTIVE:
552                                 case ND_BONE_SELECT:
553                                 case ND_TRANSFORM:
554                                 case ND_GEOM_SELECT:
555                                 case ND_GEOM_DATA:
556                                 case ND_DRAW:
557                                 case ND_KEYS:
558                                         ED_region_tag_redraw(ar);
559                                         break;
560                         }
561         }
562 }
563
564 /* add handlers, stuff you only do once or on area/region changes */
565 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
566 {
567         ListBase *keymap;
568         
569         ED_region_panels_init(wm, ar);
570
571         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
572         WM_event_add_keymap_handler(&ar->handlers, keymap);
573 }
574
575
576
577 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
578 {
579         ED_region_panels(C, ar, 1, view3d_context_string(C));
580 }
581
582 /*
583  * Returns true if the Object is a from an external blend file (libdata)
584  */
585 static int object_is_libdata(Object *ob)
586 {
587         if (!ob) return 0;
588         if (ob->proxy) return 0;
589         if (ob->id.lib) return 1;
590         return 0;
591 }
592
593 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
594 {
595         View3D *v3d= CTX_wm_view3d(C);
596         Scene *scene= CTX_data_scene(C);
597         Base *base;
598
599         if(v3d==NULL) return 0;
600
601         if(CTX_data_dir(member)) {
602                 static const char *dir[] = {
603                         "selected_objects", "selected_bases" "selected_editable_objects",
604                         "selected_editable_bases" "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
605                         "active_base", "active_object", "visible_bones", "editable_bones",
606                         "selected_bones", "selected_editable_bones" "visible_pchans",
607                         "selected_pchans", "active_bone", "active_pchan", NULL};
608
609                 CTX_data_dir_set(result, dir);
610         }
611         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
612                 int selected_objects= CTX_data_equals(member, "selected_objects");
613
614                 for(base=scene->base.first; base; base=base->next) {
615                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
616                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
617                                         if(selected_objects)
618                                                 CTX_data_id_list_add(result, &base->object->id);
619                                         else
620                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
621                                 }
622                         }
623                 }
624
625                 return 1;
626         }
627         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
628                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
629
630                 for(base=scene->base.first; base; base=base->next) {
631                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
632                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
633                                         if(0==object_is_libdata(base->object)) {
634                                                 if(selected_editable_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                 
643                 return 1;
644         }
645         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
646                 int visible_objects= CTX_data_equals(member, "visible_objects");
647
648                 for(base=scene->base.first; base; base=base->next) {
649                         if(base->lay & v3d->lay) {
650                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
651                                         if(visible_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                 return 1;
660         }
661         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
662                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
663
664                 for(base=scene->base.first; base; base=base->next) {
665                         if(base->lay & v3d->lay) {
666                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
667                                         if(selectable_objects)
668                                                 CTX_data_id_list_add(result, &base->object->id);
669                                         else
670                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
671                                 }
672                         }
673                 }
674                 
675                 return 1;
676         }
677         else if(CTX_data_equals(member, "active_base")) {
678                 if(scene->basact && (scene->basact->lay & v3d->lay))
679                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
680                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
681                 
682                 return 1;
683         }
684         else if(CTX_data_equals(member, "active_object")) {
685                 if(scene->basact && (scene->basact->lay & v3d->lay))
686                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
687                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
688                 
689                 return 1;
690         }
691         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
692                 Object *obedit= scene->obedit; // XXX get from context?
693                 bArmature *arm= (obedit) ? obedit->data : NULL;
694                 EditBone *ebone, *flipbone=NULL;
695                 int editable_bones= CTX_data_equals(member, "editable_bones");
696                 
697                 if (arm && arm->edbo) {
698                         /* Attention: X-Axis Mirroring is also handled here... */
699                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
700                                 /* first and foremost, bone must be visible and selected */
701                                 if (EBONE_VISIBLE(arm, ebone)) {
702                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
703                                          * so that most users of this data don't need to explicitly check for it themselves.
704                                          * 
705                                          * We need to make sure that these mirrored copies are not selected, otherwise some
706                                          * bones will be operated on twice.
707                                          */
708                                         if (arm->flag & ARM_MIRROR_EDIT)
709                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
710                                         
711                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
712                                         if (editable_bones) {
713                                                 /* only selected + editable */
714                                                 if (EBONE_EDITABLE(ebone)) {
715                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
716                                                 
717                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
718                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
719                                                 }
720                                         }
721                                         else {
722                                                 /* only include bones if visible */
723                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
724                                                 
725                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
726                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
727                                         }
728                                 }
729                         }       
730                         
731                         return 1;
732                 }
733         }
734         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
735                 Object *obedit= scene->obedit; // XXX get from context?
736                 bArmature *arm= (obedit) ? obedit->data : NULL;
737                 EditBone *ebone, *flipbone=NULL;
738                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
739                 
740                 if (arm && arm->edbo) {
741                         /* Attention: X-Axis Mirroring is also handled here... */
742                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
743                                 /* first and foremost, bone must be visible and selected */
744                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
745                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
746                                          * so that most users of this data don't need to explicitly check for it themselves.
747                                          * 
748                                          * We need to make sure that these mirrored copies are not selected, otherwise some
749                                          * bones will be operated on twice.
750                                          */
751                                         if (arm->flag & ARM_MIRROR_EDIT)
752                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
753                                         
754                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
755                                         if (selected_editable_bones) {
756                                                 /* only selected + editable */
757                                                 if (EBONE_EDITABLE(ebone)) {
758                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
759                                                 
760                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
761                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
762                                                 }
763                                         }
764                                         else {
765                                                 /* only include bones if selected */
766                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
767                                                 
768                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
769                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
770                                         }
771                                 }
772                         }       
773                         
774                         return 1;
775                 }
776         }
777         else if(CTX_data_equals(member, "visible_pchans")) {
778                 Object *obact= OBACT;
779                 bArmature *arm= (obact) ? obact->data : NULL;
780                 bPoseChannel *pchan;
781                 
782                 if (obact && arm) {
783                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
784                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
785                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
786                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
787                                 }
788                         }
789                         
790                         return 1;
791                 }
792         }
793         else if(CTX_data_equals(member, "selected_pchans")) {
794                 Object *obact= OBACT;
795                 bArmature *arm= (obact) ? obact->data : NULL;
796                 bPoseChannel *pchan;
797                 
798                 if (obact && arm) {
799                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
800                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
801                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
802                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
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, "active_bone")) {
811                 Object *obedit= scene->obedit; // XXX get from context?
812                 bArmature *arm= (obedit) ? obedit->data : NULL;
813                 EditBone *ebone;
814                 
815                 if (arm && arm->edbo) {
816                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
817                                 if (EBONE_VISIBLE(arm, ebone)) {
818                                         if (ebone->flag & BONE_ACTIVE) {
819                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
820                                                 
821                                                 return 1;
822                                         }
823                                 }
824                         }
825                 }
826                 
827         }
828         else if(CTX_data_equals(member, "active_pchan")) {
829                 Object *obact= OBACT;
830                 bPoseChannel *pchan;
831                 
832                 pchan= get_active_posechannel(obact);
833                 if (pchan) {
834                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
835                         return 1;
836                 }
837         }
838
839         return 0;
840 }
841
842 /* only called once, from space/spacetypes.c */
843 void ED_spacetype_view3d(void)
844 {
845         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
846         ARegionType *art;
847         
848         st->spaceid= SPACE_VIEW3D;
849         
850         st->new= view3d_new;
851         st->free= view3d_free;
852         st->init= view3d_init;
853         st->duplicate= view3d_duplicate;
854         st->operatortypes= view3d_operatortypes;
855         st->keymap= view3d_keymap;
856         st->context= view3d_context;
857         
858         /* regions: main window */
859         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
860         art->regionid = RGN_TYPE_WINDOW;
861         art->keymapflag= ED_KEYMAP_FRAMES;
862         art->draw= view3d_main_area_draw;
863         art->init= view3d_main_area_init;
864         art->free= view3d_main_area_free;
865         art->duplicate= view3d_main_area_duplicate;
866         art->listener= view3d_main_area_listener;
867         art->cursor= view3d_main_area_cursor;
868         BLI_addhead(&st->regiontypes, art);
869         
870         /* regions: listview/buttons */
871         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
872         art->regionid = RGN_TYPE_UI;
873         art->minsizex= 220; // XXX
874         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
875         art->listener= view3d_buttons_area_listener;
876         art->init= view3d_buttons_area_init;
877         art->draw= view3d_buttons_area_draw;
878         BLI_addhead(&st->regiontypes, art);
879
880         view3d_buttons_register(art);
881
882         /* regions: tool(bar) */
883         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
884         art->regionid = RGN_TYPE_TOOLS;
885         art->minsizex= 120; // XXX
886         art->minsizey= 50; // XXX
887         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
888         art->listener= view3d_buttons_area_listener;
889         art->init= view3d_tools_area_init;
890         art->draw= view3d_tools_area_draw;
891         BLI_addhead(&st->regiontypes, art);
892         
893         view3d_toolbar_register(art);
894
895         /* regions: tool properties */
896         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
897         art->regionid = RGN_TYPE_TOOL_PROPS;
898         art->minsizex= 0;
899         art->minsizey= 120;
900         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
901         art->listener= view3d_buttons_area_listener;
902         art->init= view3d_tools_area_init;
903         art->draw= view3d_tools_area_draw;
904         BLI_addhead(&st->regiontypes, art);
905         
906         view3d_tool_props_register(art);
907         
908         
909         /* regions: header */
910         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
911         art->regionid = RGN_TYPE_HEADER;
912         art->minsizey= HEADERY;
913         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
914         art->listener= view3d_header_area_listener;
915         art->init= view3d_header_area_init;
916         art->draw= view3d_header_area_draw;
917         BLI_addhead(&st->regiontypes, art);
918         
919         BKE_spacetype_register(st);
920 }
921