2.5 - Editing Animation data (keyframes/nla-strips) using transform tools now refresh...
[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_EDIT:
417                                 case ND_NLA_ACTCHANGE:
418                                 case ND_ANIMCHAN_SELECT:
419                                         ED_region_tag_redraw(ar);
420                                         break;
421                         }
422                         break;
423                 case NC_SCENE:
424                         switch(wmn->data) {
425                                 case ND_TRANSFORM:
426                                 case ND_FRAME:
427                                 case ND_OB_ACTIVE:
428                                 case ND_OB_SELECT:
429                                         ED_region_tag_redraw(ar);
430                                         break;
431                                 case ND_MODE:
432                                         view3d_modal_keymaps(wmn->wm, ar, wmn->subtype);
433                                         ED_region_tag_redraw(ar);
434                                         break;
435                         }
436                         break;
437                 case NC_OBJECT:
438                         switch(wmn->data) {
439                                 case ND_BONE_ACTIVE:
440                                 case ND_BONE_SELECT:
441                                 case ND_TRANSFORM:
442                                 case ND_GEOM_SELECT:
443                                 case ND_GEOM_DATA:
444                                 case ND_DRAW:
445                                 case ND_MODIFIER:
446                                 case ND_CONSTRAINT:
447                                 case ND_KEYS:
448                                 case ND_PARTICLE:
449                                         ED_region_tag_redraw(ar);
450                                         break;
451                         }
452                 case NC_GROUP:
453                         /* all group ops for now */
454                         ED_region_tag_redraw(ar);
455                         break;
456                 case NC_MATERIAL:
457                         switch(wmn->data) {
458                                 case ND_SHADING_DRAW:
459                                         ED_region_tag_redraw(ar);
460                                         break;
461                         }
462                 case NC_LAMP:
463                         switch(wmn->data) {
464                                 case ND_LIGHTING_DRAW:
465                                         ED_region_tag_redraw(ar);
466                                         break;
467                         }
468                 case NC_IMAGE:  
469                         /* this could be more fine grained checks if we had
470                          * more context than just the region */
471                         ED_region_tag_redraw(ar);
472                         break;
473         }
474 }
475
476 /* concept is to retrieve cursor type context-less */
477 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
478 {
479         Scene *scene= win->screen->scene;
480
481         if(scene->obedit) {
482                 WM_cursor_set(win, CURSOR_EDIT);
483         }
484         else {
485                 WM_cursor_set(win, CURSOR_STD);
486         }
487 }
488
489 /* add handlers, stuff you only do once or on area/region changes */
490 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
491 {
492         ListBase *keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
493         
494         WM_event_add_keymap_handler(&ar->handlers, keymap);
495
496         ED_region_header_init(ar);
497 }
498
499 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
500 {
501         ED_region_header(C, ar);
502 }
503
504 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
505 {
506         /* context changes */
507         switch(wmn->category) {
508                 case NC_SCENE:
509                         switch(wmn->data) {
510                                 case ND_FRAME:
511                                 case ND_OB_ACTIVE:
512                                 case ND_OB_SELECT:
513                                 case ND_MODE:
514                                         ED_region_tag_redraw(ar);
515                                         break;
516                         }
517                         break;
518         }
519 }
520
521 /* add handlers, stuff you only do once or on area/region changes */
522 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
523 {
524         ListBase *keymap;
525
526         ED_region_panels_init(wm, ar);
527         
528         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
529         WM_event_add_keymap_handler(&ar->handlers, keymap);
530 }
531
532 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
533 {
534         ED_region_panels(C, ar, 1, NULL);
535 }
536
537 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
538 {
539         /* context changes */
540         switch(wmn->category) {
541                 case NC_ANIMATION:
542                         switch(wmn->data) {
543                                 case ND_KEYFRAME_EDIT:
544                                 case ND_KEYFRAME_PROP:
545                                 case ND_NLA_EDIT:
546                                 case ND_NLA_ACTCHANGE:
547                                         ED_region_tag_redraw(ar);
548                                         break;
549                         }
550                         break;
551                 case NC_SCENE:
552                         switch(wmn->data) {
553                                 case ND_FRAME:
554                                 case ND_OB_ACTIVE:
555                                 case ND_OB_SELECT:
556                                 case ND_MODE:
557                                         ED_region_tag_redraw(ar);
558                                         break;
559                         }
560                         break;
561                 case NC_OBJECT:
562                         switch(wmn->data) {
563                                 case ND_BONE_ACTIVE:
564                                 case ND_BONE_SELECT:
565                                 case ND_TRANSFORM:
566                                 case ND_GEOM_SELECT:
567                                 case ND_GEOM_DATA:
568                                 case ND_DRAW:
569                                 case ND_KEYS:
570                                         ED_region_tag_redraw(ar);
571                                         break;
572                         }
573         }
574 }
575
576 /* add handlers, stuff you only do once or on area/region changes */
577 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
578 {
579         ListBase *keymap;
580         
581         ED_region_panels_init(wm, ar);
582
583         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
584         WM_event_add_keymap_handler(&ar->handlers, keymap);
585 }
586
587
588
589 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
590 {
591         ED_region_panels(C, ar, 1, view3d_context_string(C));
592 }
593
594 /*
595  * Returns true if the Object is a from an external blend file (libdata)
596  */
597 static int object_is_libdata(Object *ob)
598 {
599         if (!ob) return 0;
600         if (ob->proxy) return 0;
601         if (ob->id.lib) return 1;
602         return 0;
603 }
604
605 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
606 {
607         View3D *v3d= CTX_wm_view3d(C);
608         Scene *scene= CTX_data_scene(C);
609         Base *base;
610
611         if(v3d==NULL) return 0;
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 & v3d->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 & v3d->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 & v3d->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 & v3d->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 & v3d->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 & v3d->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;
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= 220; // 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= 120; // 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