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