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