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