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