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