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