NLA SoC: Action-line is now drawn with green colouring when a strip is being tweaked...
[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                                         ED_region_tag_redraw(ar);
398                                         break;
399                         }
400                 case NC_GROUP:
401                         /* all group ops for now */
402                         ED_region_tag_redraw(ar);
403                         break;
404                 case NC_MATERIAL:
405                         switch(wmn->data) {
406                                 case ND_SHADING_DRAW:
407                                         ED_region_tag_redraw(ar);
408                                         break;
409                         }
410                 case NC_LAMP:
411                         switch(wmn->data) {
412                                 case ND_LIGHTING_DRAW:
413                                         ED_region_tag_redraw(ar);
414                                         break;
415                         }
416                 case NC_IMAGE:  
417                         /* this could be more fine grained checks if we had
418                          * more context than just the region */
419                         ED_region_tag_redraw(ar);
420                         break;
421         }
422 }
423
424 /* concept is to retrieve cursor type context-less */
425 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
426 {
427         Scene *scene= win->screen->scene;
428
429         if(scene->obedit) {
430                 WM_cursor_set(win, CURSOR_EDIT);
431         }
432         else {
433                 WM_cursor_set(win, CURSOR_STD);
434         }
435 }
436
437 /* add handlers, stuff you only do once or on area/region changes */
438 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
439 {
440         ListBase *keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
441         
442         WM_event_add_keymap_handler(&ar->handlers, keymap);
443         
444         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
445 }
446
447 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
448 {
449         float col[3];
450         
451         /* clear */
452         if(ED_screen_area_active(C))
453                 UI_GetThemeColor3fv(TH_HEADER, col);
454         else
455                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
456         
457         glClearColor(col[0], col[1], col[2], 0.0);
458         glClear(GL_COLOR_BUFFER_BIT);
459         
460         /* set view2d view matrix for scrolling (without scrollers) */
461         UI_view2d_view_ortho(C, &ar->v2d);
462         
463         view3d_header_buttons(C, ar);
464         
465         /* restore view matrix? */
466         UI_view2d_view_restore(C);
467 }
468
469 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
470 {
471         /* context changes */
472         switch(wmn->category) {
473                 case NC_SCENE:
474                         switch(wmn->data) {
475                                 case ND_FRAME:
476                                 case ND_OB_ACTIVE:
477                                 case ND_OB_SELECT:
478                                 case ND_MODE:
479                                         ED_region_tag_redraw(ar);
480                                         break;
481                         }
482                         break;
483         }
484 }
485
486 /* add handlers, stuff you only do once or on area/region changes */
487 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
488 {
489         ListBase *keymap;
490
491         ED_region_panels_init(wm, ar);
492         
493         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
494         WM_event_add_keymap_handler(&ar->handlers, keymap);
495 }
496
497 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
498 {
499         ED_region_panels(C, ar, 1, NULL);
500 }
501
502 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
503 {
504         /* context changes */
505         switch(wmn->category) {
506                 case NC_SCENE:
507                         switch(wmn->data) {
508                                 case ND_FRAME:
509                                 case ND_OB_ACTIVE:
510                                 case ND_OB_SELECT:
511                                 case ND_MODE:
512                                         ED_region_tag_redraw(ar);
513                                         break;
514                         }
515                         break;
516                 case NC_OBJECT:
517                         switch(wmn->data) {
518                                 case ND_BONE_ACTIVE:
519                                 case ND_BONE_SELECT:
520                                 case ND_TRANSFORM:
521                                 case ND_GEOM_SELECT:
522                                 case ND_GEOM_DATA:
523                                 case ND_DRAW:
524                                 case ND_KEYS:
525                                         ED_region_tag_redraw(ar);
526                                         break;
527                         }
528         }
529 }
530
531 /* add handlers, stuff you only do once or on area/region changes */
532 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
533 {
534         ListBase *keymap;
535         
536         ED_region_panels_init(wm, ar);
537
538         keymap= WM_keymap_listbase(wm, "View3D Generic", SPACE_VIEW3D, 0);
539         WM_event_add_keymap_handler(&ar->handlers, keymap);
540 }
541
542 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
543 {
544         ED_region_panels(C, ar, 1, NULL);
545 }
546
547 /*
548  * Returns true if the Object is a from an external blend file (libdata)
549  */
550 static int object_is_libdata(Object *ob)
551 {
552         if (!ob) return 0;
553         if (ob->proxy) return 0;
554         if (ob->id.lib) return 1;
555         return 0;
556 }
557
558 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
559 {
560         View3D *v3d= CTX_wm_view3d(C);
561         Scene *scene= CTX_data_scene(C);
562         Base *base;
563
564         if(v3d==NULL) return 0;
565
566         if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
567                 int selected_objects= CTX_data_equals(member, "selected_objects");
568
569                 for(base=scene->base.first; base; base=base->next) {
570                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
571                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
572                                         if(selected_objects)
573                                                 CTX_data_id_list_add(result, &base->object->id);
574                                         else
575                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
576                                 }
577                         }
578                 }
579
580                 return 1;
581         }
582         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
583                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
584
585                 for(base=scene->base.first; base; base=base->next) {
586                         if((base->flag & SELECT) && (base->lay & v3d->lay)) {
587                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
588                                         if(0==object_is_libdata(base->object)) {
589                                                 if(selected_editable_objects)
590                                                         CTX_data_id_list_add(result, &base->object->id);
591                                                 else
592                                                         CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
593                                         }
594                                 }
595                         }
596                 }
597                 
598                 return 1;
599         }
600         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
601                 int visible_objects= CTX_data_equals(member, "visible_objects");
602
603                 for(base=scene->base.first; base; base=base->next) {
604                         if(base->lay & v3d->lay) {
605                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
606                                         if(visible_objects)
607                                                 CTX_data_id_list_add(result, &base->object->id);
608                                         else
609                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
610                                 }
611                         }
612                 }
613                 
614                 return 1;
615         }
616         else if(CTX_data_equals(member, "active_base")) {
617                 if(scene->basact && (scene->basact->lay & v3d->lay))
618                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
619                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
620                 
621                 return 1;
622         }
623         else if(CTX_data_equals(member, "active_object")) {
624                 if(scene->basact && (scene->basact->lay & v3d->lay))
625                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
626                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
627                 
628                 return 1;
629         }
630         else if(CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
631                 Object *obedit= scene->obedit; // XXX get from context?
632                 bArmature *arm= (obedit) ? obedit->data : NULL;
633                 EditBone *ebone, *flipbone=NULL;
634                 int editable_bones= CTX_data_equals(member, "editable_bones");
635                 
636                 if (arm && arm->edbo) {
637                         /* Attention: X-Axis Mirroring is also handled here... */
638                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
639                                 /* first and foremost, bone must be visible and selected */
640                                 if (EBONE_VISIBLE(arm, ebone)) {
641                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
642                                          * so that most users of this data don't need to explicitly check for it themselves.
643                                          * 
644                                          * We need to make sure that these mirrored copies are not selected, otherwise some
645                                          * bones will be operated on twice.
646                                          */
647                                         if (arm->flag & ARM_MIRROR_EDIT)
648                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
649                                         
650                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
651                                         if (editable_bones) {
652                                                 /* only selected + editable */
653                                                 if (EBONE_EDITABLE(ebone)) {
654                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
655                                                 
656                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
657                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
658                                                 }
659                                         }
660                                         else {
661                                                 /* only include bones if visible */
662                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
663                                                 
664                                                 if ((flipbone) && EBONE_VISIBLE(arm, flipbone)==0)
665                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
666                                         }
667                                 }
668                         }       
669                         
670                         return 1;
671                 }
672         }
673         else if(CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
674                 Object *obedit= scene->obedit; // XXX get from context?
675                 bArmature *arm= (obedit) ? obedit->data : NULL;
676                 EditBone *ebone, *flipbone=NULL;
677                 int selected_editable_bones= CTX_data_equals(member, "selected_editable_bones");
678                 
679                 if (arm && arm->edbo) {
680                         /* Attention: X-Axis Mirroring is also handled here... */
681                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
682                                 /* first and foremost, bone must be visible and selected */
683                                 if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
684                                         /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
685                                          * so that most users of this data don't need to explicitly check for it themselves.
686                                          * 
687                                          * We need to make sure that these mirrored copies are not selected, otherwise some
688                                          * bones will be operated on twice.
689                                          */
690                                         if (arm->flag & ARM_MIRROR_EDIT)
691                                                 flipbone = ED_armature_bone_get_mirrored(arm->edbo, ebone);
692                                         
693                                         /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
694                                         if (selected_editable_bones) {
695                                                 /* only selected + editable */
696                                                 if (EBONE_EDITABLE(ebone)) {
697                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
698                                                 
699                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
700                                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
701                                                 }
702                                         }
703                                         else {
704                                                 /* only include bones if selected */
705                                                 CTX_data_list_add(result, &arm->id, &RNA_UnknownType, ebone);
706                                                 
707                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
708                                                         CTX_data_list_add(result, &arm->id, &RNA_UnknownType, flipbone);
709                                         }
710                                 }
711                         }       
712                         
713                         return 1;
714                 }
715         }
716         else if(CTX_data_equals(member, "visible_pchans")) {
717                 Object *obact= OBACT;
718                 bArmature *arm= (obact) ? obact->data : NULL;
719                 bPoseChannel *pchan;
720                 
721                 if (obact && arm) {
722                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
723                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
724                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
725                                         CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
726                                 }
727                         }
728                         
729                         return 1;
730                 }
731         }
732         else if(CTX_data_equals(member, "selected_pchans")) {
733                 Object *obact= OBACT;
734                 bArmature *arm= (obact) ? obact->data : NULL;
735                 bPoseChannel *pchan;
736                 
737                 if (obact && arm) {
738                         for (pchan= obact->pose->chanbase.first; pchan; pchan= pchan->next) {
739                                 /* ensure that PoseChannel is on visible layer and is not hidden in PoseMode */
740                                 if ((pchan->bone) && (arm->layer & pchan->bone->layer) && !(pchan->bone->flag & BONE_HIDDEN_P)) {
741                                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) 
742                                                 CTX_data_list_add(result, &obact->id, &RNA_PoseChannel, pchan);
743                                 }
744                         }
745                         
746                         return 1;
747                 }
748         }
749         else if(CTX_data_equals(member, "active_bone")) {
750                 Object *obedit= scene->obedit; // XXX get from context?
751                 bArmature *arm= (obedit) ? obedit->data : NULL;
752                 EditBone *ebone;
753                 
754                 if (arm && arm->edbo) {
755                         for (ebone= arm->edbo->first; ebone; ebone= ebone->next) {
756                                 if (EBONE_VISIBLE(arm, ebone)) {
757                                         if (ebone->flag & BONE_ACTIVE) {
758                                                 CTX_data_pointer_set(result, &arm->id, &RNA_UnknownType, ebone);
759                                                 
760                                                 return 1;
761                                         }
762                                 }
763                         }
764                 }
765                 
766         }
767         else if(CTX_data_equals(member, "active_pchan")) {
768                 Object *obact= OBACT;
769                 bPoseChannel *pchan;
770                 
771                 pchan= get_active_posechannel(obact);
772                 if (pchan) {
773                         CTX_data_pointer_set(result, &obact->id, &RNA_PoseChannel, pchan);
774                         return 1;
775                 }
776         }
777
778         return 0;
779 }
780
781 /* only called once, from space/spacetypes.c */
782 void ED_spacetype_view3d(void)
783 {
784         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
785         ARegionType *art;
786         
787         st->spaceid= SPACE_VIEW3D;
788         
789         st->new= view3d_new;
790         st->free= view3d_free;
791         st->init= view3d_init;
792         st->duplicate= view3d_duplicate;
793         st->operatortypes= view3d_operatortypes;
794         st->keymap= view3d_keymap;
795         st->context= view3d_context;
796         
797         /* regions: main window */
798         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
799         art->regionid = RGN_TYPE_WINDOW;
800         art->keymapflag= ED_KEYMAP_FRAMES;
801         art->draw= view3d_main_area_draw;
802         art->init= view3d_main_area_init;
803         art->free= view3d_main_area_free;
804         art->duplicate= view3d_main_area_duplicate;
805         art->listener= view3d_main_area_listener;
806         art->cursor= view3d_main_area_cursor;
807         BLI_addhead(&st->regiontypes, art);
808         
809         /* regions: listview/buttons */
810         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
811         art->regionid = RGN_TYPE_UI;
812         art->minsizex= 220; // XXX
813         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
814         art->listener= view3d_buttons_area_listener;
815         art->init= view3d_buttons_area_init;
816         art->draw= view3d_buttons_area_draw;
817         BLI_addhead(&st->regiontypes, art);
818
819         view3d_buttons_register(art);
820
821         /* regions: tool(bar) */
822         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
823         art->regionid = RGN_TYPE_TOOLS;
824         art->minsizex= 120; // XXX
825         art->minsizey= 50; // XXX
826         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
827         art->listener= view3d_buttons_area_listener;
828         art->init= view3d_tools_area_init;
829         art->draw= view3d_tools_area_draw;
830         BLI_addhead(&st->regiontypes, art);
831         
832         view3d_toolbar_register(art);
833         
834         /* regions: header */
835         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
836         art->regionid = RGN_TYPE_HEADER;
837         art->minsizey= HEADERY;
838         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
839         art->listener= view3d_header_area_listener;
840         art->init= view3d_header_area_init;
841         art->draw= view3d_header_area_draw;
842         BLI_addhead(&st->regiontypes, art);
843         
844         BKE_spacetype_register(st);
845 }
846