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