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