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