4a9042aa31c90d1324a8d27ab93e52aab35b2f60
[blender-staging.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_ALIGN_LEFT; //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 /* Most of the time this isn't needed since you could assume the view matrix was
162  * set while drawing, however when functions like mesh_foreachScreenVert are
163  * called by selection tools, we can't be sure this object was the last.
164  *
165  * for example, transparent objects are drawn after editmode and will cause
166  * the rv3d mat's to change and break selection.
167  *
168  * 'ED_view3d_init_mats_rv3d' should be called before
169  * view3d_project_short_clip and view3d_project_short_noclip in cases where
170  * these functions are not used during draw_object
171  */
172 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
173 {
174         wmMultMatrix(ob->obmat);
175         /* local viewmat and persmat, to calculate projections */
176         wmGetMatrix(rv3d->viewmatob);
177         wmGetSingleMatrix(rv3d->persmatob);
178 }
179
180 /* ******************** default callbacks for view3d space ***************** */
181
182 static SpaceLink *view3d_new(const bContext *C)
183 {
184         Scene *scene= CTX_data_scene(C);
185         ARegion *ar;
186         View3D *v3d;
187         RegionView3D *rv3d;
188         
189         v3d= MEM_callocN(sizeof(View3D), "initview3d");
190         v3d->spacetype= SPACE_VIEW3D;
191         v3d->blockscale= 0.7f;
192         v3d->lay= v3d->layact= 1;
193         if(scene) {
194                 v3d->lay= v3d->layact= scene->lay;
195                 v3d->camera= scene->camera;
196         }
197         v3d->scenelock= 1;
198         v3d->grid= 1.0f;
199         v3d->gridlines= 16;
200         v3d->gridsubdiv = 10;
201         v3d->drawtype= OB_WIRE;
202         
203         v3d->gridflag |= V3D_SHOW_X;
204         v3d->gridflag |= V3D_SHOW_Y;
205         v3d->gridflag |= V3D_SHOW_FLOOR;
206         v3d->gridflag &= ~V3D_SHOW_Z;
207         
208         v3d->lens= 35.0f;
209         v3d->near= 0.01f;
210         v3d->far= 500.0f;
211
212         v3d->twtype= V3D_MANIP_TRANSLATE;
213         
214         /* header */
215         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
216         
217         BLI_addtail(&v3d->regionbase, ar);
218         ar->regiontype= RGN_TYPE_HEADER;
219         ar->alignment= RGN_ALIGN_BOTTOM;
220         
221         /* toolbar */
222         ar= MEM_callocN(sizeof(ARegion), "toolbar for view3d");
223         
224         BLI_addtail(&v3d->regionbase, ar);
225         ar->regiontype= RGN_TYPE_UI;
226         ar->alignment= RGN_ALIGN_LEFT;
227         ar->flag = RGN_FLAG_HIDDEN;
228         
229         /* tool properties */
230         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
231         
232         BLI_addtail(&v3d->regionbase, ar);
233         ar->regiontype= RGN_TYPE_UI;
234         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
235         ar->flag = RGN_FLAG_HIDDEN;
236         
237         /* buttons/list view */
238         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
239         
240         BLI_addtail(&v3d->regionbase, ar);
241         ar->regiontype= RGN_TYPE_UI;
242         ar->alignment= RGN_ALIGN_RIGHT;
243         ar->flag = RGN_FLAG_HIDDEN;
244         
245         /* main area */
246         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
247         
248         BLI_addtail(&v3d->regionbase, ar);
249         ar->regiontype= RGN_TYPE_WINDOW;
250         
251         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
252         rv3d= ar->regiondata;
253         rv3d->viewquat[0]= 1.0f;
254         rv3d->persp= 1;
255         rv3d->view= 7;
256         rv3d->dist= 10.0;
257         
258         return (SpaceLink *)v3d;
259 }
260
261 /* not spacelink itself */
262 static void view3d_free(SpaceLink *sl)
263 {
264         View3D *vd= (View3D *) sl;
265         
266         if(vd->bgpic) {
267                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
268                 MEM_freeN(vd->bgpic);
269         }
270         
271         if(vd->localvd) MEM_freeN(vd->localvd);
272         
273         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
274         
275 }
276
277
278 /* spacetype; init callback */
279 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
280 {
281
282 }
283
284 static SpaceLink *view3d_duplicate(SpaceLink *sl)
285 {
286         View3D *v3do= (View3D *)sl;
287         View3D *v3dn= MEM_dupallocN(sl);
288         
289         /* clear or remove stuff from old */
290         
291 // XXX  BIF_view3d_previewrender_free(v3do);
292         
293         if(v3do->localvd) {
294                 v3do->localvd= NULL;
295                 v3do->properties_storage= NULL;
296                 v3do->lay= v3dn->localvd->lay;
297                 v3do->lay &= 0xFFFFFF;
298         }
299         
300         /* copy or clear inside new stuff */
301
302         if(v3dn->bgpic) {
303                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
304                 if(v3dn->bgpic->ima) v3dn->bgpic->ima->id.us++;
305         }
306         v3dn->properties_storage= NULL;
307         
308         return (SpaceLink *)v3dn;
309 }
310
311 /* add handlers, stuff you only do once or on area/region changes */
312 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
313 {
314         wmKeyMap *keymap;
315
316         /* object ops. */
317         
318         /* pose is not modal, operator poll checks for this */
319         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
320         WM_event_add_keymap_handler(&ar->handlers, keymap);
321         
322         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
323         WM_event_add_keymap_handler(&ar->handlers, keymap);
324
325         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
326         WM_event_add_keymap_handler(&ar->handlers, keymap);
327
328         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
329         WM_event_add_keymap_handler(&ar->handlers, keymap);
330
331         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
332         WM_event_add_keymap_handler(&ar->handlers, keymap);
333         
334         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
335         WM_event_add_keymap_handler(&ar->handlers, keymap);
336
337         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
338         WM_event_add_keymap_handler(&ar->handlers, keymap);
339         
340         keymap= WM_keymap_find(wm->defaultconf, "EditMesh", 0, 0);
341         WM_event_add_keymap_handler(&ar->handlers, keymap);
342         
343         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
344         WM_event_add_keymap_handler(&ar->handlers, keymap);
345         
346         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
347         WM_event_add_keymap_handler(&ar->handlers, keymap);
348
349         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
350         WM_event_add_keymap_handler(&ar->handlers, keymap);
351
352         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
353         WM_event_add_keymap_handler(&ar->handlers, keymap);
354         
355         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
356         WM_event_add_keymap_handler(&ar->handlers, keymap);
357
358         /* armature sketching needs to take over mouse */
359         keymap= WM_keymap_find(wm->defaultconf, "Armature_Sketch", 0, 0);
360         WM_event_add_keymap_handler(&ar->handlers, keymap);
361
362         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
363         WM_event_add_keymap_handler(&ar->handlers, keymap);
364
365         /* editfont keymap swallows all... */
366         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
367         WM_event_add_keymap_handler(&ar->handlers, keymap);
368
369         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
370         WM_event_add_keymap_handler(&ar->handlers, keymap);
371
372         /* own keymap, last so modes can override it */
373         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
374         WM_event_add_keymap_handler(&ar->handlers, keymap);
375
376         keymap= WM_keymap_find(wm->defaultconf, "View3D", SPACE_VIEW3D, 0);
377         WM_event_add_keymap_handler(&ar->handlers, keymap);
378 }
379
380 /* type callback, not region itself */
381 static void view3d_main_area_free(ARegion *ar)
382 {
383         RegionView3D *rv3d= ar->regiondata;
384         
385         if(rv3d) {
386                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
387                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
388
389                 // XXX  retopo_free_view_data(rv3d);
390                 if(rv3d->ri) { 
391                         // XXX          BIF_view3d_previewrender_free(rv3d);
392                 }
393                 
394                 if(rv3d->depths) {
395                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
396                         MEM_freeN(rv3d->depths);
397                 }
398                 MEM_freeN(rv3d);
399                 ar->regiondata= NULL;
400         }
401 }
402
403 /* copy regiondata */
404 static void *view3d_main_area_duplicate(void *poin)
405 {
406         if(poin) {
407                 RegionView3D *rv3d= poin, *new;
408         
409                 new= MEM_dupallocN(rv3d);
410                 if(rv3d->localvd) 
411                         new->localvd= MEM_dupallocN(rv3d->localvd);
412                 if(rv3d->clipbb) 
413                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
414                 
415                 new->depths= NULL;
416                 new->retopo_view_data= NULL;
417                 new->ri= NULL;
418                 new->gpd= NULL;
419                 new->sms= NULL;
420                 new->smooth_timer= NULL;
421                 
422                 return new;
423         }
424         return NULL;
425 }
426
427 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
428 {
429         /* context changes */
430         switch(wmn->category) {
431                 case NC_ANIMATION:
432                         switch(wmn->data) {
433                                 case ND_KEYFRAME_EDIT:
434                                 case ND_KEYFRAME_PROP:
435                                 case ND_NLA_EDIT:
436                                 case ND_NLA_ACTCHANGE:
437                                 case ND_ANIMCHAN_SELECT:
438                                         ED_region_tag_redraw(ar);
439                                         break;
440                         }
441                         break;
442                 case NC_SCENE:
443                         switch(wmn->data) {
444                                 case ND_TRANSFORM:
445                                 case ND_FRAME:
446                                 case ND_OB_ACTIVE:
447                                 case ND_OB_SELECT:
448                                 case ND_LAYER:
449                                         ED_region_tag_redraw(ar);
450                                         break;
451                                 case ND_MODE:
452                                         ED_region_tag_redraw(ar);
453                                         break;
454                         }
455                         break;
456                 case NC_OBJECT:
457                         switch(wmn->data) {
458                                 case ND_BONE_ACTIVE:
459                                 case ND_BONE_SELECT:
460                                 case ND_TRANSFORM:
461                                 case ND_POSE:
462                                 case ND_DRAW:
463                                 case ND_MODIFIER:
464                                 case ND_CONSTRAINT:
465                                 case ND_KEYS:
466                                 case ND_PARTICLE_SELECT:
467                                 case ND_PARTICLE_DATA:
468                                         ED_region_tag_redraw(ar);
469                                         break;
470                         }
471                         break;
472                 case NC_GEOM:
473                         switch(wmn->data) {
474                                 case ND_DATA:
475                                 case ND_SELECT:
476                                         ED_region_tag_redraw(ar);
477                                         break;
478                         }
479                         break;
480                 case NC_GROUP:
481                         /* all group ops for now */
482                         ED_region_tag_redraw(ar);
483                         break;
484                 case NC_MATERIAL:
485                         switch(wmn->data) {
486                                 case ND_SHADING_DRAW:
487                                         ED_region_tag_redraw(ar);
488                                         break;
489                         }
490                         break;
491                 case NC_WORLD:
492                         switch(wmn->data) {
493                                 case ND_WORLD_DRAW:
494                                         ED_region_tag_redraw(ar);
495                                         break;
496                         }
497                         break;
498                 case NC_LAMP:
499                         switch(wmn->data) {
500                                 case ND_LIGHTING_DRAW:
501                                         ED_region_tag_redraw(ar);
502                                         break;
503                         }
504                         break;
505                 case NC_IMAGE:  
506                         /* this could be more fine grained checks if we had
507                          * more context than just the region */
508                         ED_region_tag_redraw(ar);
509                         break;
510                 case NC_SPACE:
511                         if(wmn->data == ND_SPACE_VIEW3D)
512                                 ED_region_tag_redraw(ar);
513                         break;
514         }
515 }
516
517 /* concept is to retrieve cursor type context-less */
518 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
519 {
520         Scene *scene= win->screen->scene;
521
522         if(scene->obedit) {
523                 WM_cursor_set(win, CURSOR_EDIT);
524         }
525         else {
526                 WM_cursor_set(win, CURSOR_STD);
527         }
528 }
529
530 /* add handlers, stuff you only do once or on area/region changes */
531 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
532 {
533         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
534         
535         WM_event_add_keymap_handler(&ar->handlers, keymap);
536
537         ED_region_header_init(ar);
538 }
539
540 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
541 {
542         ED_region_header(C, ar);
543 }
544
545 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
546 {
547         /* context changes */
548         switch(wmn->category) {
549                 case NC_SCENE:
550                         switch(wmn->data) {
551                                 case ND_FRAME:
552                                 case ND_OB_ACTIVE:
553                                 case ND_OB_SELECT:
554                                 case ND_MODE:
555                                 case ND_LAYER:
556                                         ED_region_tag_redraw(ar);
557                                         break;
558                         }
559                         break;
560                 case NC_SPACE:
561                         if(wmn->data == ND_SPACE_VIEW3D)
562                                 ED_region_tag_redraw(ar);
563                         break;
564         }
565 }
566
567 /* add handlers, stuff you only do once or on area/region changes */
568 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
569 {
570         wmKeyMap *keymap;
571
572         ED_region_panels_init(wm, ar);
573         
574         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
575         WM_event_add_keymap_handler(&ar->handlers, keymap);
576 }
577
578 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
579 {
580         ED_region_panels(C, ar, 1, NULL, -1);
581 }
582
583 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
584 {
585         /* context changes */
586         switch(wmn->category) {
587                 case NC_ANIMATION:
588                         switch(wmn->data) {
589                                 case ND_KEYFRAME_EDIT:
590                                 case ND_KEYFRAME_PROP:
591                                 case ND_NLA_EDIT:
592                                 case ND_NLA_ACTCHANGE:
593                                         ED_region_tag_redraw(ar);
594                                         break;
595                         }
596                         break;
597                 case NC_SCENE:
598                         switch(wmn->data) {
599                                 case ND_FRAME:
600                                 case ND_OB_ACTIVE:
601                                 case ND_OB_SELECT:
602                                 case ND_MODE:
603                                 case ND_LAYER:
604                                         ED_region_tag_redraw(ar);
605                                         break;
606                         }
607                         break;
608                 case NC_OBJECT:
609                         switch(wmn->data) {
610                                 case ND_BONE_ACTIVE:
611                                 case ND_BONE_SELECT:
612                                 case ND_TRANSFORM:
613                                 case ND_POSE:
614                                 case ND_DRAW:
615                                 case ND_KEYS:
616                                         ED_region_tag_redraw(ar);
617                                         break;
618                         }
619                         break;
620                 case NC_GEOM:
621                         switch(wmn->data) {
622                                 case ND_DATA:
623                                 case ND_SELECT:
624                                         ED_region_tag_redraw(ar);
625                                         break;
626                         }
627                         break;
628                 case NC_BRUSH:
629                         if(wmn->action==NA_EDITED)
630                                 ED_region_tag_redraw(ar);
631                         break;
632                 case NC_SPACE:
633                         if(wmn->data == ND_SPACE_VIEW3D)
634                                 ED_region_tag_redraw(ar);
635                         break;
636         }
637 }
638
639 /* add handlers, stuff you only do once or on area/region changes */
640 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
641 {
642         wmKeyMap *keymap;
643         
644         ED_region_panels_init(wm, ar);
645
646         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
647         WM_event_add_keymap_handler(&ar->handlers, keymap);
648 }
649
650 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
651 {
652         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
653 }
654
655 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
656 {
657         View3D *v3d= CTX_wm_view3d(C);
658         Scene *scene= CTX_data_scene(C);
659         Base *base;
660         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
661
662         if(CTX_data_dir(member)) {
663                 static const char *dir[] = {
664                         "selected_objects", "selected_bases", "selected_editable_objects",
665                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
666                         "active_base", "active_object", NULL};
667
668                 CTX_data_dir_set(result, dir);
669         }
670         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
671                 int selected_objects= CTX_data_equals(member, "selected_objects");
672
673                 for(base=scene->base.first; base; base=base->next) {
674                         if((base->flag & SELECT) && (base->lay & lay)) {
675                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
676                                         if(selected_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                 return 1;
685         }
686         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
687                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
688
689                 for(base=scene->base.first; base; base=base->next) {
690                         if((base->flag & SELECT) && (base->lay & lay)) {
691                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
692                                         if(0==object_is_libdata(base->object)) {
693                                                 if(selected_editable_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                 
702                 return 1;
703         }
704         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
705                 int visible_objects= CTX_data_equals(member, "visible_objects");
706
707                 for(base=scene->base.first; base; base=base->next) {
708                         if(base->lay & lay) {
709                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
710                                         if(visible_objects)
711                                                 CTX_data_id_list_add(result, &base->object->id);
712                                         else
713                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
714                                 }
715                         }
716                 }
717                 
718                 return 1;
719         }
720         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
721                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
722
723                 for(base=scene->base.first; base; base=base->next) {
724                         if(base->lay & lay) {
725                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
726                                         if(selectable_objects)
727                                                 CTX_data_id_list_add(result, &base->object->id);
728                                         else
729                                                 CTX_data_list_add(result, &scene->id, &RNA_UnknownType, base);
730                                 }
731                         }
732                 }
733                 
734                 return 1;
735         }
736         else if(CTX_data_equals(member, "active_base")) {
737                 if(scene->basact && (scene->basact->lay & lay))
738                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
739                                 CTX_data_pointer_set(result, &scene->id, &RNA_UnknownType, scene->basact);
740                 
741                 return 1;
742         }
743         else if(CTX_data_equals(member, "active_object")) {
744                 if(scene->basact && (scene->basact->lay & lay))
745                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
746                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
747                 
748                 return 1;
749         }
750         return 0;
751 }
752
753 /* only called once, from space/spacetypes.c */
754 void ED_spacetype_view3d(void)
755 {
756         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
757         ARegionType *art;
758         
759         st->spaceid= SPACE_VIEW3D;
760         
761         st->new= view3d_new;
762         st->free= view3d_free;
763         st->init= view3d_init;
764         st->duplicate= view3d_duplicate;
765         st->operatortypes= view3d_operatortypes;
766         st->keymap= view3d_keymap;
767         st->context= view3d_context;
768         
769         /* regions: main window */
770         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
771         art->regionid = RGN_TYPE_WINDOW;
772         art->keymapflag= ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
773         art->draw= view3d_main_area_draw;
774         art->init= view3d_main_area_init;
775         art->free= view3d_main_area_free;
776         art->duplicate= view3d_main_area_duplicate;
777         art->listener= view3d_main_area_listener;
778         art->cursor= view3d_main_area_cursor;
779         BLI_addhead(&st->regiontypes, art);
780         
781         /* regions: listview/buttons */
782         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
783         art->regionid = RGN_TYPE_UI;
784         art->minsizex= 180; // XXX
785         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
786         art->listener= view3d_buttons_area_listener;
787         art->init= view3d_buttons_area_init;
788         art->draw= view3d_buttons_area_draw;
789         BLI_addhead(&st->regiontypes, art);
790
791         view3d_buttons_register(art);
792
793         /* regions: tool(bar) */
794         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
795         art->regionid = RGN_TYPE_TOOLS;
796         art->minsizex= 160; // XXX
797         art->minsizey= 50; // XXX
798         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
799         art->listener= view3d_buttons_area_listener;
800         art->init= view3d_tools_area_init;
801         art->draw= view3d_tools_area_draw;
802         BLI_addhead(&st->regiontypes, art);
803         
804         view3d_toolbar_register(art);
805
806         /* regions: tool properties */
807         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
808         art->regionid = RGN_TYPE_TOOL_PROPS;
809         art->minsizex= 0;
810         art->minsizey= 120;
811         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
812         art->listener= view3d_buttons_area_listener;
813         art->init= view3d_tools_area_init;
814         art->draw= view3d_tools_area_draw;
815         BLI_addhead(&st->regiontypes, art);
816         
817         view3d_tool_props_register(art);
818         
819         
820         /* regions: header */
821         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
822         art->regionid = RGN_TYPE_HEADER;
823         art->minsizey= HEADERY;
824         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
825         art->listener= view3d_header_area_listener;
826         art->init= view3d_header_area_init;
827         art->draw= view3d_header_area_draw;
828         BLI_addhead(&st->regiontypes, art);
829         
830         BKE_spacetype_register(st);
831 }
832