2.5 merged 20773:21020
[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_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
568                 int selected_objects= CTX_data_equals(member, "selected_objects");
569
570                 for(base=scene->base.first; base; base=base->next) {
571                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
572                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
573                                         if(selected_objects)
574                                                 CTX_data_id_list_add(result, &base->object->id);
575                                         else
576                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
577                                 }
578                         }
579                 }
580
581                 return 1;
582         }
583         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
584                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
585
586                 for(base=scene->base.first; base; base=base->next) {
587                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
588                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
589                                         if(0==object_is_libdata(base->object)) {
590                                                 if(selected_editable_objects)
591                                                         CTX_data_id_list_add(result, &base->object->id);
592                                                 else
593                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
594                                         }
595                                 }
596                         }
597                 }
598                 
599                 return 1;
600         }
601         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
602                 int visible_objects= CTX_data_equals(member, "visible_objects");
603
604                 for(base=scene->base.first; base; base=base->next) {
605                         if(base->lay & v3d->lay) {
606                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
607                                         if(visible_objects)
608                                                 CTX_data_id_list_add(result, &base->object->id);
609                                         else
610                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
611                                 }
612                         }
613                 }
614                 
615                 return 1;
616         }
617         else if(CTX_data_equals(member, "active_base")) {
618                 if(scene->basact && (scene->basact->lay & v3d->lay))
619                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
620                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
621                 
622                 return 1;
623         }
624         else if(CTX_data_equals(member, "active_object")) {
625                 if(scene->basact && (scene->basact->lay & v3d->lay))
626                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
627                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
628                 
629                 return 1;
630         }
631         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
632                 Object *obedit= scene->obedit; // XXX get from context?
633                 bArmature *arm= (obedit) ? obedit->data : NULL;
634                 EditBone *ebone, *flipbone=NULL;
635                 int editable_bones= CTX_data_equals(member, "editable_bones");
636                 
637                 if (arm && arm->edbo) {
638                         /* Attention: X-Axis Mirroring is also handled here... */
639                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
640                                 /* first and foremost, bone must be visible and selected */
641                                 if (EBONE_VISIBLE(arm, ebone)) {
642                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
643                                          * so that most users of this data don't need to explicitly check for it themselves.
644                                          * 
645                                          * We need to make sure that these mirrored copies are not selected, otherwise some
646                                          * bones will be operated on twice.
647                                          */
648                                         if (arm->flag & ARM_MIRROR_EDIT)
649                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
650                                         
651                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
652                                         if (editable_bones) {
653                                                 /* only selected + editable */
654                                                 if (EBONE_EDITABLE(ebone)) {
655                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
656                                                 
657                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
658                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
659                                                 }
660                                         }
661                                         else {
662                                                 /* only include bones if visible */
663                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
664                                                 
665                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
666                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
667                                         }
668                                 }
669                         }       
670                         
671                         return 1;
672                 }
673         }
674         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
675                 Object *obedit= scene->obedit; // XXX get from context?
676                 bArmature *arm= (obedit) ? obedit->data : NULL;
677                 EditBone *ebone, *flipbone=NULL;
678                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
679                 
680                 if (arm && arm->edbo) {
681                         /* Attention: X-Axis Mirroring is also handled here... */
682                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
683                                 /* first and foremost, bone must be visible and selected */
684                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
685                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
686                                          * so that most users of this data don't need to explicitly check for it themselves.
687                                          * 
688                                          * We need to make sure that these mirrored copies are not selected, otherwise some
689                                          * bones will be operated on twice.
690                                          */
691                                         if (arm->flag & ARM_MIRROR_EDIT)
692                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
693                                         
694                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
695                                         if (selected_editable_bones) {
696                                                 /* only selected + editable */
697                                                 if (EBONE_EDITABLE(ebone)) {
698                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
699                                                 
700                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
701                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
702                                                 }
703                                         }
704                                         else {
705                                                 /* only include bones if selected */
706                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
707                                                 
708                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
709                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
710                                         }
711                                 }
712                         }       
713                         
714                         return 1;
715                 }
716         }
717         else if(CTX_data_equals(member, "visible_pchans")) {
718                 Object *obact= OBACT;
719                 bArmature *arm= (obact) ? obact->data : NULL;
720                 bPoseChannel *pchan;
721                 
722                 if (obact && arm) {
723                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
724                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
725                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
726                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
727                                 }
728                         }
729                         
730                         return 1;
731                 }
732         }
733         else if(CTX_data_equals(member, "selected_pchans")) {
734                 Object *obact= OBACT;
735                 bArmature *arm= (obact) ? obact->data : NULL;
736                 bPoseChannel *pchan;
737                 
738                 if (obact && arm) {
739                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
740                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
741                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
742                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
743                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
744                                 }
745                         }
746                         
747                         return 1;
748                 }
749         }
750         else if(CTX_data_equals(member, "active_bone")) {
751                 Object *obedit= scene->obedit; // XXX get from context?
752                 bArmature *arm= (obedit) ? obedit->data : NULL;
753                 EditBone *ebone;
754                 
755                 if (arm && arm->edbo) {
756                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
757                                 if (EBONE_VISIBLE(arm, ebone)) {
758                                         if (ebone->flag & BONE_ACTIVE) {
759                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
760                                                 
761                                                 return 1;
762                                         }
763                                 }
764                         }
765                 }
766                 
767         }
768         else if(CTX_data_equals(member, "active_pchan")) {
769                 Object *obact= OBACT;
770                 bPoseChannel *pchan;
771                 
772                 pchan= get_active_posechannel(obact);
773                 if (pchan) {
774                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
775                         return 1;
776                 }
777         }
778
779         return 0;
780 }
781
782 /* only called once, from space/spacetypes.c */
783 void ED_spacetype_view3d(void)
784 {
785         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
786         ARegionType *art;
787         
788         st->spaceid= SPACE_VIEW3D;
789         
790         st->new= view3d_new;
791         st->free= view3d_free;
792         st->init= view3d_init;
793         st->duplicate= view3d_duplicate;
794         st->operatortypes= view3d_operatortypes;
795         st->keymap= view3d_keymap;
796         st->context= view3d_context;
797         
798         /* regions: main window */
799         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
800         art->regionid = RGN_TYPE_WINDOW;
801         art->keymapflag= ED_KEYMAP_FRAMES;
802         art->draw= view3d_main_area_draw;
803         art->init= view3d_main_area_init;
804         art->free= view3d_main_area_free;
805         art->duplicate= view3d_main_area_duplicate;
806         art->listener= view3d_main_area_listener;
807         art->cursor= view3d_main_area_cursor;
808         BLI_addhead(&st->regiontypes, art);
809         
810         /* regions: listview/buttons */
811         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
812         art->regionid = RGN_TYPE_UI;
813         art->minsizex= 220; // XXX
814         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
815         art->listener= view3d_buttons_area_listener;
816         art->init= view3d_buttons_area_init;
817         art->draw= view3d_buttons_area_draw;
818         BLI_addhead(&st->regiontypes, art);
819
820         view3d_buttons_register(art);
821
822         /* regions: tool(bar) */
823         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
824         art->regionid = RGN_TYPE_TOOLS;
825         art->minsizex= 120; // XXX
826         art->minsizey= 50; // XXX
827         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
828         art->listener= view3d_buttons_area_listener;
829         art->init= view3d_tools_area_init;
830         art->draw= view3d_tools_area_draw;
831         BLI_addhead(&st->regiontypes, art);
832         
833         view3d_toolbar_register(art);
834         
835         /* regions: header */
836         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
837         art->regionid = RGN_TYPE_HEADER;
838         art->minsizey= HEADERY;
839         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
840         art->listener= view3d_header_area_listener;
841         art->init= view3d_header_area_init;
842         art->draw= view3d_header_area_draw;
843         BLI_addhead(&st->regiontypes, art);
844         
845         BKE_spacetype_register(st);
846 }
847