Sculpt Branch:
[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                                 case ND_MODE:
456                                         ED_region_tag_redraw(ar);
457                                         break;
458                         }
459                         if (wmn->action == NA_EDITED)
460                                 ED_region_tag_redraw(ar);
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                 case NC_ID:
521                         if(wmn->data == ND_ID_RENAME)
522                                 ED_region_tag_redraw(ar);
523                         break;
524         }
525 }
526
527 /* concept is to retrieve cursor type context-less */
528 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
529 {
530         Scene *scene= win->screen->scene;
531
532         if(scene->obedit) {
533                 WM_cursor_set(win, CURSOR_EDIT);
534         }
535         else {
536                 WM_cursor_set(win, CURSOR_STD);
537         }
538 }
539
540 /* add handlers, stuff you only do once or on area/region changes */
541 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
542 {
543         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
544         
545         WM_event_add_keymap_handler(&ar->handlers, keymap);
546
547         ED_region_header_init(ar);
548 }
549
550 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
551 {
552         ED_region_header(C, ar);
553 }
554
555 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
556 {
557         /* context changes */
558         switch(wmn->category) {
559                 case NC_SCENE:
560                         switch(wmn->data) {
561                                 case ND_FRAME:
562                                 case ND_OB_ACTIVE:
563                                 case ND_OB_SELECT:
564                                 case ND_MODE:
565                                 case ND_LAYER:
566                                         ED_region_tag_redraw(ar);
567                                         break;
568                         }
569                         break;
570                 case NC_SPACE:
571                         if(wmn->data == ND_SPACE_VIEW3D)
572                                 ED_region_tag_redraw(ar);
573                         break;
574         }
575 }
576
577 /* add handlers, stuff you only do once or on area/region changes */
578 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
579 {
580         wmKeyMap *keymap;
581
582         ED_region_panels_init(wm, ar);
583         
584         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
585         WM_event_add_keymap_handler(&ar->handlers, keymap);
586 }
587
588 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
589 {
590         ED_region_panels(C, ar, 1, NULL, -1);
591 }
592
593 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
594 {
595         /* context changes */
596         switch(wmn->category) {
597                 case NC_ANIMATION:
598                         switch(wmn->data) {
599                                 case ND_KEYFRAME_EDIT:
600                                 case ND_KEYFRAME_PROP:
601                                 case ND_NLA_EDIT:
602                                 case ND_NLA_ACTCHANGE:
603                                         ED_region_tag_redraw(ar);
604                                         break;
605                         }
606                         break;
607                 case NC_SCENE:
608                         switch(wmn->data) {
609                                 case ND_FRAME:
610                                 case ND_OB_ACTIVE:
611                                 case ND_OB_SELECT:
612                                 case ND_MODE:
613                                 case ND_LAYER:
614                                         ED_region_tag_redraw(ar);
615                                         break;
616                         }
617                         break;
618                 case NC_OBJECT:
619                         switch(wmn->data) {
620                                 case ND_BONE_ACTIVE:
621                                 case ND_BONE_SELECT:
622                                 case ND_TRANSFORM:
623                                 case ND_POSE:
624                                 case ND_DRAW:
625                                 case ND_KEYS:
626                                         ED_region_tag_redraw(ar);
627                                         break;
628                         }
629                         break;
630                 case NC_GEOM:
631                         switch(wmn->data) {
632                                 case ND_DATA:
633                                 case ND_SELECT:
634                                         ED_region_tag_redraw(ar);
635                                         break;
636                         }
637                         break;
638                 case NC_BRUSH:
639                         if(wmn->action==NA_EDITED)
640                                 ED_region_tag_redraw(ar);
641                         break;
642                 case NC_SPACE:
643                         if(wmn->data == ND_SPACE_VIEW3D)
644                                 ED_region_tag_redraw(ar);
645                         break;
646                 case NC_ID:
647                         if(wmn->data == ND_ID_RENAME)
648                                 ED_region_tag_redraw(ar);
649                         break;
650         }
651 }
652
653 /* add handlers, stuff you only do once or on area/region changes */
654 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
655 {
656         wmKeyMap *keymap;
657         
658         ED_region_panels_init(wm, ar);
659
660         keymap= WM_keymap_find(wm->defaultconf, "View3D Generic", SPACE_VIEW3D, 0);
661         WM_event_add_keymap_handler(&ar->handlers, keymap);
662 }
663
664 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
665 {
666         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
667 }
668
669 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
670 {
671         View3D *v3d= CTX_wm_view3d(C);
672         Scene *scene= CTX_data_scene(C);
673         Base *base;
674         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
675
676         if(CTX_data_dir(member)) {
677                 static const char *dir[] = {
678                         "selected_objects", "selected_bases", "selected_editable_objects",
679                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
680                         "active_base", "active_object", NULL};
681
682                 CTX_data_dir_set(result, dir);
683         }
684         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
685                 int selected_objects= CTX_data_equals(member, "selected_objects");
686
687                 for(base=scene->base.first; base; base=base->next) {
688                         if((base->flag & SELECT) && (base->lay & lay)) {
689                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
690                                         if(selected_objects)
691                                                 CTX_data_id_list_add(result, &base->object->id);
692                                         else
693                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
694                                 }
695                         }
696                 }
697
698                 return 1;
699         }
700         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
701                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
702
703                 for(base=scene->base.first; base; base=base->next) {
704                         if((base->flag & SELECT) && (base->lay & lay)) {
705                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
706                                         if(0==object_is_libdata(base->object)) {
707                                                 if(selected_editable_objects)
708                                                         CTX_data_id_list_add(result, &base->object->id);
709                                                 else
710                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
711                                         }
712                                 }
713                         }
714                 }
715                 
716                 return 1;
717         }
718         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
719                 int visible_objects= CTX_data_equals(member, "visible_objects");
720
721                 for(base=scene->base.first; base; base=base->next) {
722                         if(base->lay & lay) {
723                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
724                                         if(visible_objects)
725                                                 CTX_data_id_list_add(result, &base->object->id);
726                                         else
727                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
728                                 }
729                         }
730                 }
731                 
732                 return 1;
733         }
734         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
735                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
736
737                 for(base=scene->base.first; base; base=base->next) {
738                         if(base->lay & lay) {
739                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
740                                         if(selectable_objects)
741                                                 CTX_data_id_list_add(result, &base->object->id);
742                                         else
743                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
744                                 }
745                         }
746                 }
747                 
748                 return 1;
749         }
750         else if(CTX_data_equals(member, "active_base")) {
751                 if(scene->basact && (scene->basact->lay & lay))
752                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
753                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
754                 
755                 return 1;
756         }
757         else if(CTX_data_equals(member, "active_object")) {
758                 if(scene->basact && (scene->basact->lay & lay))
759                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
760                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
761                 
762                 return 1;
763         }
764         else {
765                 return 0; /* not found */
766         }
767
768         return -1; /* found but not available */
769 }
770
771 /* only called once, from space/spacetypes.c */
772 void ED_spacetype_view3d(void)
773 {
774         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
775         ARegionType *art;
776         
777         st->spaceid= SPACE_VIEW3D;
778         
779         st->new= view3d_new;
780         st->free= view3d_free;
781         st->init= view3d_init;
782         st->duplicate= view3d_duplicate;
783         st->operatortypes= view3d_operatortypes;
784         st->keymap= view3d_keymap;
785         st->context= view3d_context;
786         
787         /* regions: main window */
788         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
789         art->regionid = RGN_TYPE_WINDOW;
790         art->keymapflag= ED_KEYMAP_GPENCIL;
791         art->draw= view3d_main_area_draw;
792         art->init= view3d_main_area_init;
793         art->free= view3d_main_area_free;
794         art->duplicate= view3d_main_area_duplicate;
795         art->listener= view3d_main_area_listener;
796         art->cursor= view3d_main_area_cursor;
797         BLI_addhead(&st->regiontypes, art);
798         
799         /* regions: listview/buttons */
800         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
801         art->regionid = RGN_TYPE_UI;
802         art->minsizex= 180; // XXX
803         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
804         art->listener= view3d_buttons_area_listener;
805         art->init= view3d_buttons_area_init;
806         art->draw= view3d_buttons_area_draw;
807         BLI_addhead(&st->regiontypes, art);
808
809         view3d_buttons_register(art);
810
811         /* regions: tool(bar) */
812         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
813         art->regionid = RGN_TYPE_TOOLS;
814         art->minsizex= 160; // XXX
815         art->minsizey= 50; // XXX
816         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
817         art->listener= view3d_buttons_area_listener;
818         art->init= view3d_tools_area_init;
819         art->draw= view3d_tools_area_draw;
820         BLI_addhead(&st->regiontypes, art);
821         
822         view3d_toolbar_register(art);
823
824         /* regions: tool properties */
825         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
826         art->regionid = RGN_TYPE_TOOL_PROPS;
827         art->minsizex= 0;
828         art->minsizey= 120;
829         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
830         art->listener= view3d_buttons_area_listener;
831         art->init= view3d_tools_area_init;
832         art->draw= view3d_tools_area_draw;
833         BLI_addhead(&st->regiontypes, art);
834         
835         view3d_tool_props_register(art);
836         
837         
838         /* regions: header */
839         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
840         art->regionid = RGN_TYPE_HEADER;
841         art->minsizey= HEADERY;
842         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
843         art->listener= view3d_header_area_listener;
844         art->init= view3d_header_area_init;
845         art->draw= view3d_header_area_draw;
846         BLI_addhead(&st->regiontypes, art);
847         
848         BKE_spacetype_register(st);
849 }
850