ebd1ab6dbfff7421f8b8d2679fb9bbf55686f3f7
[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_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
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         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
373         WM_event_add_keymap_handler(&ar->handlers, keymap);
374
375         /* own keymap, last so modes can override it */
376         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
377         WM_event_add_keymap_handler(&ar->handlers, keymap);
378
379         keymap= WM_keymap_find(wm->defaultconf, "View3D", SPACE_VIEW3D, 0);
380         WM_event_add_keymap_handler(&ar->handlers, keymap);
381 }
382
383 /* type callback, not region itself */
384 static void view3d_main_area_free(ARegion *ar)
385 {
386         RegionView3D *rv3d= ar->regiondata;
387         
388         if(rv3d) {
389                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
390                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
391
392                 // XXX  retopo_free_view_data(rv3d);
393                 if(rv3d->ri) { 
394                         // XXX          BIF_view3d_previewrender_free(rv3d);
395                 }
396                 
397                 if(rv3d->depths) {
398                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
399                         MEM_freeN(rv3d->depths);
400                 }
401                 MEM_freeN(rv3d);
402                 ar->regiondata= NULL;
403         }
404 }
405
406 /* copy regiondata */
407 static void *view3d_main_area_duplicate(void *poin)
408 {
409         if(poin) {
410                 RegionView3D *rv3d= poin, *new;
411         
412                 new= MEM_dupallocN(rv3d);
413                 if(rv3d->localvd) 
414                         new->localvd= MEM_dupallocN(rv3d->localvd);
415                 if(rv3d->clipbb) 
416                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
417                 
418                 new->depths= NULL;
419                 new->retopo_view_data= NULL;
420                 new->ri= NULL;
421                 new->gpd= NULL;
422                 new->sms= NULL;
423                 new->smooth_timer= NULL;
424                 
425                 return new;
426         }
427         return NULL;
428 }
429
430 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
431 {
432         /* context changes */
433         switch(wmn->category) {
434                 case NC_ANIMATION:
435                         switch(wmn->data) {
436                                 case ND_KEYFRAME_EDIT:
437                                 case ND_KEYFRAME_PROP:
438                                 case ND_NLA_EDIT:
439                                 case ND_NLA_ACTCHANGE:
440                                 case ND_ANIMCHAN_SELECT:
441                                         ED_region_tag_redraw(ar);
442                                         break;
443                         }
444                         break;
445                 case NC_SCENE:
446                         switch(wmn->data) {
447                                 case ND_TRANSFORM:
448                                 case ND_FRAME:
449                                 case ND_OB_ACTIVE:
450                                 case ND_OB_SELECT:
451                                 case ND_LAYER:
452                                         ED_region_tag_redraw(ar);
453                                         break;
454                                 case ND_MODE:
455                                         ED_region_tag_redraw(ar);
456                                         break;
457                         }
458                         break;
459                 case NC_OBJECT:
460                         switch(wmn->data) {
461                                 case ND_BONE_ACTIVE:
462                                 case ND_BONE_SELECT:
463                                 case ND_TRANSFORM:
464                                 case ND_POSE:
465                                 case ND_DRAW:
466                                 case ND_MODIFIER:
467                                 case ND_CONSTRAINT:
468                                 case ND_KEYS:
469                                 case ND_PARTICLE_SELECT:
470                                 case ND_PARTICLE_DATA:
471                                         ED_region_tag_redraw(ar);
472                                         break;
473                         }
474                         break;
475                 case NC_GEOM:
476                         switch(wmn->data) {
477                                 case ND_DATA:
478                                 case ND_SELECT:
479                                         ED_region_tag_redraw(ar);
480                                         break;
481                         }
482                         break;
483                 case NC_GROUP:
484                         /* all group ops for now */
485                         ED_region_tag_redraw(ar);
486                         break;
487                 case NC_MATERIAL:
488                         switch(wmn->data) {
489                                 case ND_SHADING_DRAW:
490                                         ED_region_tag_redraw(ar);
491                                         break;
492                         }
493                         break;
494                 case NC_WORLD:
495                         switch(wmn->data) {
496                                 case ND_WORLD_DRAW:
497                                         ED_region_tag_redraw(ar);
498                                         break;
499                         }
500                         break;
501                 case NC_LAMP:
502                         switch(wmn->data) {
503                                 case ND_LIGHTING_DRAW:
504                                         ED_region_tag_redraw(ar);
505                                         break;
506                         }
507                         break;
508                 case NC_IMAGE:  
509                         /* this could be more fine grained checks if we had
510                          * more context than just the region */
511                         ED_region_tag_redraw(ar);
512                         break;
513                 case NC_SPACE:
514                         if(wmn->data == ND_SPACE_VIEW3D)
515                                 ED_region_tag_redraw(ar);
516                         break;
517         }
518 }
519
520 /* concept is to retrieve cursor type context-less */
521 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
522 {
523         Scene *scene= win->screen->scene;
524
525         if(scene->obedit) {
526                 WM_cursor_set(win, CURSOR_EDIT);
527         }
528         else {
529                 WM_cursor_set(win, CURSOR_STD);
530         }
531 }
532
533 /* add handlers, stuff you only do once or on area/region changes */
534 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
535 {
536         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
537         
538         WM_event_add_keymap_handler(&ar->handlers, keymap);
539
540         ED_region_header_init(ar);
541 }
542
543 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
544 {
545         ED_region_header(C, ar);
546 }
547
548 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
549 {
550         /* context changes */
551         switch(wmn->category) {
552                 case NC_SCENE:
553                         switch(wmn->data) {
554                                 case ND_FRAME:
555                                 case ND_OB_ACTIVE:
556                                 case ND_OB_SELECT:
557                                 case ND_MODE:
558                                 case ND_LAYER:
559                                         ED_region_tag_redraw(ar);
560                                         break;
561                         }
562                         break;
563                 case NC_SPACE:
564                         if(wmn->data == ND_SPACE_VIEW3D)
565                                 ED_region_tag_redraw(ar);
566                         break;
567         }
568 }
569
570 /* add handlers, stuff you only do once or on area/region changes */
571 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
572 {
573         wmKeyMap *keymap;
574
575         ED_region_panels_init(wm, ar);
576         
577         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
578         WM_event_add_keymap_handler(&ar->handlers, keymap);
579 }
580
581 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
582 {
583         ED_region_panels(C, ar, 1, NULL, -1);
584 }
585
586 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
587 {
588         /* context changes */
589         switch(wmn->category) {
590                 case NC_ANIMATION:
591                         switch(wmn->data) {
592                                 case ND_KEYFRAME_EDIT:
593                                 case ND_KEYFRAME_PROP:
594                                 case ND_NLA_EDIT:
595                                 case ND_NLA_ACTCHANGE:
596                                         ED_region_tag_redraw(ar);
597                                         break;
598                         }
599                         break;
600                 case NC_SCENE:
601                         switch(wmn->data) {
602                                 case ND_FRAME:
603                                 case ND_OB_ACTIVE:
604                                 case ND_OB_SELECT:
605                                 case ND_MODE:
606                                 case ND_LAYER:
607                                         ED_region_tag_redraw(ar);
608                                         break;
609                         }
610                         break;
611                 case NC_OBJECT:
612                         switch(wmn->data) {
613                                 case ND_BONE_ACTIVE:
614                                 case ND_BONE_SELECT:
615                                 case ND_TRANSFORM:
616                                 case ND_POSE:
617                                 case ND_DRAW:
618                                 case ND_KEYS:
619                                         ED_region_tag_redraw(ar);
620                                         break;
621                         }
622                         break;
623                 case NC_GEOM:
624                         switch(wmn->data) {
625                                 case ND_DATA:
626                                 case ND_SELECT:
627                                         ED_region_tag_redraw(ar);
628                                         break;
629                         }
630                         break;
631                 case NC_BRUSH:
632                         if(wmn->action==NA_EDITED)
633                                 ED_region_tag_redraw(ar);
634                         break;
635                 case NC_SPACE:
636                         if(wmn->data == ND_SPACE_VIEW3D)
637                                 ED_region_tag_redraw(ar);
638                         break;
639         }
640 }
641
642 /* add handlers, stuff you only do once or on area/region changes */
643 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
644 {
645         wmKeyMap *keymap;
646         
647         ED_region_panels_init(wm, ar);
648
649         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
650         WM_event_add_keymap_handler(&ar->handlers, keymap);
651 }
652
653 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
654 {
655         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
656 }
657
658 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
659 {
660         View3D *v3d= CTX_wm_view3d(C);
661         Scene *scene= CTX_data_scene(C);
662         Base *base;
663         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
664
665         if(CTX_data_dir(member)) {
666                 static const char *dir[] = {
667                         "selected_objects", "selected_bases", "selected_editable_objects",
668                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
669                         "active_base", "active_object", NULL};
670
671                 CTX_data_dir_set(result, dir);
672         }
673         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
674                 int selected_objects= CTX_data_equals(member, "selected_objects");
675
676                 for(base=scene->base.first; base; base=base->next) {
677                         if((base->flag & SELECT) && (base->lay & lay)) {
678                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
679                                         if(selected_objects)
680                                                 CTX_data_id_list_add(result, &base->object->id);
681                                         else
682                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
683                                 }
684                         }
685                 }
686
687                 return 1;
688         }
689         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
690                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
691
692                 for(base=scene->base.first; base; base=base->next) {
693                         if((base->flag & SELECT) && (base->lay & lay)) {
694                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
695                                         if(0==object_is_libdata(base->object)) {
696                                                 if(selected_editable_objects)
697                                                         CTX_data_id_list_add(result, &base->object->id);
698                                                 else
699                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
700                                         }
701                                 }
702                         }
703                 }
704                 
705                 return 1;
706         }
707         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
708                 int visible_objects= CTX_data_equals(member, "visible_objects");
709
710                 for(base=scene->base.first; base; base=base->next) {
711                         if(base->lay & lay) {
712                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
713                                         if(visible_objects)
714                                                 CTX_data_id_list_add(result, &base->object->id);
715                                         else
716                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
717                                 }
718                         }
719                 }
720                 
721                 return 1;
722         }
723         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
724                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
725
726                 for(base=scene->base.first; base; base=base->next) {
727                         if(base->lay & lay) {
728                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
729                                         if(selectable_objects)
730                                                 CTX_data_id_list_add(result, &base->object->id);
731                                         else
732                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
733                                 }
734                         }
735                 }
736                 
737                 return 1;
738         }
739         else if(CTX_data_equals(member, "active_base")) {
740                 if(scene->basact && (scene->basact->lay & lay))
741                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
742                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
743                 
744                 return 1;
745         }
746         else if(CTX_data_equals(member, "active_object")) {
747                 if(scene->basact && (scene->basact->lay & lay))
748                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
749                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
750                 
751                 return 1;
752         }
753         else {
754                 return 0; /* not found */
755         }
756
757         return -1; /* found but not available */
758 }
759
760 /* only called once, from space/spacetypes.c */
761 void ED_spacetype_view3d(void)
762 {
763         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
764         ARegionType *art;
765         
766         st->spaceid= SPACE_VIEW3D;
767         
768         st->new= view3d_new;
769         st->free= view3d_free;
770         st->init= view3d_init;
771         st->duplicate= view3d_duplicate;
772         st->operatortypes= view3d_operatortypes;
773         st->keymap= view3d_keymap;
774         st->context= view3d_context;
775         
776         /* regions: main window */
777         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
778         art->regionid = RGN_TYPE_WINDOW;
779         art->keymapflag= ED_KEYMAP_GPENCIL;
780         art->draw= view3d_main_area_draw;
781         art->init= view3d_main_area_init;
782         art->free= view3d_main_area_free;
783         art->duplicate= view3d_main_area_duplicate;
784         art->listener= view3d_main_area_listener;
785         art->cursor= view3d_main_area_cursor;
786         BLI_addhead(&st->regiontypes, art);
787         
788         /* regions: listview/buttons */
789         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
790         art->regionid = RGN_TYPE_UI;
791         art->minsizex= 180; // XXX
792         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
793         art->listener= view3d_buttons_area_listener;
794         art->init= view3d_buttons_area_init;
795         art->draw= view3d_buttons_area_draw;
796         BLI_addhead(&st->regiontypes, art);
797
798         view3d_buttons_register(art);
799
800         /* regions: tool(bar) */
801         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
802         art->regionid = RGN_TYPE_TOOLS;
803         art->minsizex= 160; // XXX
804         art->minsizey= 50; // XXX
805         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
806         art->listener= view3d_buttons_area_listener;
807         art->init= view3d_tools_area_init;
808         art->draw= view3d_tools_area_draw;
809         BLI_addhead(&st->regiontypes, art);
810         
811         view3d_toolbar_register(art);
812
813         /* regions: tool properties */
814         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
815         art->regionid = RGN_TYPE_TOOL_PROPS;
816         art->minsizex= 0;
817         art->minsizey= 120;
818         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
819         art->listener= view3d_buttons_area_listener;
820         art->init= view3d_tools_area_init;
821         art->draw= view3d_tools_area_draw;
822         BLI_addhead(&st->regiontypes, art);
823         
824         view3d_tool_props_register(art);
825         
826         
827         /* regions: header */
828         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
829         art->regionid = RGN_TYPE_HEADER;
830         art->minsizey= HEADERY;
831         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
832         art->listener= view3d_header_area_listener;
833         art->init= view3d_header_area_init;
834         art->draw= view3d_header_area_draw;
835         BLI_addhead(&st->regiontypes, art);
836         
837         BKE_spacetype_register(st);
838 }
839