23003790dc3f40ffd8f66f841c096e7e7b447a03
[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 #include "BKE_image.h"
52
53 #include "ED_armature.h"
54 #include "ED_space_api.h"
55 #include "ED_screen.h"
56 #include "ED_object.h"
57
58 #include "BIF_gl.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65 #include "UI_view2d.h"
66
67 #include "RNA_access.h"
68
69 #include "view3d_intern.h"      // own include
70
71 /* ******************** manage regions ********************* */
72
73 ARegion *view3d_has_buttons_region(ScrArea *sa)
74 {
75         ARegion *ar, *arnew;
76         
77         for(ar= sa->regionbase.first; ar; ar= ar->next)
78                 if(ar->regiontype==RGN_TYPE_UI)
79                         return ar;
80         
81         /* add subdiv level; after header */
82         for(ar= sa->regionbase.first; ar; ar= ar->next)
83                 if(ar->regiontype==RGN_TYPE_HEADER)
84                         break;
85         
86         /* is error! */
87         if(ar==NULL) return NULL;
88         
89         arnew= MEM_callocN(sizeof(ARegion), "buttons for view3d");
90         
91         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
92         arnew->regiontype= RGN_TYPE_UI;
93         arnew->alignment= RGN_ALIGN_RIGHT;
94         
95         arnew->flag = RGN_FLAG_HIDDEN;
96         
97         return arnew;
98 }
99
100 ARegion *view3d_has_tools_region(ScrArea *sa)
101 {
102         ARegion *ar, *artool=NULL, *arprops=NULL, *arhead;
103         
104         for(ar= sa->regionbase.first; ar; ar= ar->next) {
105                 if(ar->regiontype==RGN_TYPE_TOOLS)
106                         artool= ar;
107                 if(ar->regiontype==RGN_TYPE_TOOL_PROPS)
108                         arprops= ar;
109         }
110         
111         /* tool region hide/unhide also hides props */
112         if(arprops && artool) return artool;
113         
114         if(artool==NULL) {
115                 /* add subdiv level; after header */
116                 for(arhead= sa->regionbase.first; arhead; arhead= arhead->next)
117                         if(arhead->regiontype==RGN_TYPE_HEADER)
118                                 break;
119                 
120                 /* is error! */
121                 if(arhead==NULL) return NULL;
122                 
123                 artool= MEM_callocN(sizeof(ARegion), "tools for view3d");
124                 
125                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
126                 artool->regiontype= RGN_TYPE_TOOLS;
127                 artool->alignment= RGN_ALIGN_LEFT; //RGN_OVERLAP_LEFT;
128                 artool->flag = RGN_FLAG_HIDDEN;
129         }
130
131         if(arprops==NULL) {
132                 /* add extra subdivided region for tool properties */
133                 arprops= MEM_callocN(sizeof(ARegion), "tool props for view3d");
134                 
135                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
136                 arprops->regiontype= RGN_TYPE_TOOL_PROPS;
137                 arprops->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
138         }
139         
140         return artool;
141 }
142
143 /* ****************************************************** */
144
145 /* function to always find a regionview3d context inside 3D window */
146 RegionView3D *ED_view3d_context_rv3d(bContext *C)
147 {
148         RegionView3D *rv3d= CTX_wm_region_view3d(C);
149         
150         if(rv3d==NULL) {
151                 ScrArea *sa =CTX_wm_area(C);
152                 if(sa && sa->spacetype==SPACE_VIEW3D) {
153                         ARegion *ar;
154                         for(ar= sa->regionbase.first; ar; ar= ar->next)
155                                 if(ar->regiontype==RGN_TYPE_WINDOW)
156                                         return ar->regiondata;
157                 }
158         }
159         return rv3d;
160 }
161
162 /* Most of the time this isn't needed since you could assume the view matrix was
163  * set while drawing, however when functions like mesh_foreachScreenVert are
164  * called by selection tools, we can't be sure this object was the last.
165  *
166  * for example, transparent objects are drawn after editmode and will cause
167  * the rv3d mat's to change and break selection.
168  *
169  * 'ED_view3d_init_mats_rv3d' should be called before
170  * view3d_project_short_clip and view3d_project_short_noclip in cases where
171  * these functions are not used during draw_object
172  */
173 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
174 {
175         wmMultMatrix(ob->obmat);
176         /* local viewmat and persmat, to calculate projections */
177         wmGetMatrix(rv3d->viewmatob);
178         wmGetSingleMatrix(rv3d->persmatob);
179
180         /* initializes object space clipping, speeds up clip tests */
181         ED_view3d_local_clipping(rv3d, ob->obmat);
182 }
183
184 /* ******************** default callbacks for view3d space ***************** */
185
186 static SpaceLink *view3d_new(const bContext *C)
187 {
188         Scene *scene= CTX_data_scene(C);
189         ARegion *ar;
190         View3D *v3d;
191         RegionView3D *rv3d;
192         
193         v3d= MEM_callocN(sizeof(View3D), "initview3d");
194         v3d->spacetype= SPACE_VIEW3D;
195         v3d->blockscale= 0.7f;
196         v3d->lay= v3d->layact= 1;
197         if(scene) {
198                 v3d->lay= v3d->layact= scene->lay;
199                 v3d->camera= scene->camera;
200         }
201         v3d->scenelock= 1;
202         v3d->grid= 1.0f;
203         v3d->gridlines= 16;
204         v3d->gridsubdiv = 10;
205         v3d->drawtype= OB_WIRE;
206         
207         v3d->gridflag |= V3D_SHOW_X;
208         v3d->gridflag |= V3D_SHOW_Y;
209         v3d->gridflag |= V3D_SHOW_FLOOR;
210         v3d->gridflag &= ~V3D_SHOW_Z;
211         
212         v3d->lens= 35.0f;
213         v3d->near= 0.01f;
214         v3d->far= 500.0f;
215
216         v3d->twtype= V3D_MANIP_TRANSLATE;
217         
218         /* header */
219         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
220         
221         BLI_addtail(&v3d->regionbase, ar);
222         ar->regiontype= RGN_TYPE_HEADER;
223         ar->alignment= RGN_ALIGN_BOTTOM;
224         
225         /* tool shelf */
226         ar= MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
227         
228         BLI_addtail(&v3d->regionbase, ar);
229         ar->regiontype= RGN_TYPE_UI;
230         ar->alignment= RGN_ALIGN_LEFT;
231         ar->flag = RGN_FLAG_HIDDEN;
232         
233         /* tool properties */
234         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
235         
236         BLI_addtail(&v3d->regionbase, ar);
237         ar->regiontype= RGN_TYPE_UI;
238         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
239         ar->flag = RGN_FLAG_HIDDEN;
240         
241         /* buttons/list view */
242         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
243         
244         BLI_addtail(&v3d->regionbase, ar);
245         ar->regiontype= RGN_TYPE_UI;
246         ar->alignment= RGN_ALIGN_RIGHT;
247         ar->flag = RGN_FLAG_HIDDEN;
248         
249         /* main area */
250         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
251         
252         BLI_addtail(&v3d->regionbase, ar);
253         ar->regiontype= RGN_TYPE_WINDOW;
254         
255         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
256         rv3d= ar->regiondata;
257         rv3d->viewquat[0]= 1.0f;
258         rv3d->persp= 1;
259         rv3d->view= 7;
260         rv3d->dist= 10.0;
261         
262         return (SpaceLink *)v3d;
263 }
264
265 /* not spacelink itself */
266 static void view3d_free(SpaceLink *sl)
267 {
268         View3D *vd= (View3D *) sl;
269
270         BGpic *bgpic;
271         for(bgpic= vd->bgpicbase.first; bgpic; bgpic= bgpic->next) {
272                 if(bgpic->ima) bgpic->ima->id.us--;
273         }
274         BLI_freelistN(&vd->bgpicbase);
275
276         if(vd->localvd) MEM_freeN(vd->localvd);
277         
278         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
279         
280 }
281
282
283 /* spacetype; init callback */
284 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
285 {
286
287 }
288
289 static SpaceLink *view3d_duplicate(SpaceLink *sl)
290 {
291         View3D *v3do= (View3D *)sl;
292         View3D *v3dn= MEM_dupallocN(sl);
293         
294         /* clear or remove stuff from old */
295         
296 // XXX  BIF_view3d_previewrender_free(v3do);
297         
298         if(v3do->localvd) {
299                 v3do->localvd= NULL;
300                 v3do->properties_storage= NULL;
301                 v3do->lay= v3dn->localvd->lay;
302                 v3do->lay &= 0xFFFFFF;
303         }
304         
305         /* copy or clear inside new stuff */
306
307         if(v3dn->bgpicbase.first) {
308                 BGpic *bgpic;
309                         for ( bgpic= v3dn->bgpicbase.first; bgpic; bgpic= bgpic->next ) {
310                         bgpic= MEM_dupallocN(bgpic);
311                         if(bgpic->ima) bgpic->ima->id.us++;
312                         }
313         }
314         v3dn->properties_storage= NULL;
315         
316         return (SpaceLink *)v3dn;
317 }
318
319 /* add handlers, stuff you only do once or on area/region changes */
320 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
321 {
322         wmKeyMap *keymap;
323
324         /* object ops. */
325         
326         /* pose is not modal, operator poll checks for this */
327         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
328         WM_event_add_keymap_handler(&ar->handlers, keymap);
329         
330         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
331         WM_event_add_keymap_handler(&ar->handlers, keymap);
332
333         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
334         WM_event_add_keymap_handler(&ar->handlers, keymap);
335
336         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
337         WM_event_add_keymap_handler(&ar->handlers, keymap);
338
339         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
340         WM_event_add_keymap_handler(&ar->handlers, keymap);
341         
342         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
343         WM_event_add_keymap_handler(&ar->handlers, keymap);
344
345         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
346         WM_event_add_keymap_handler(&ar->handlers, keymap);
347         
348         keymap= WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
349         WM_event_add_keymap_handler(&ar->handlers, keymap);
350         
351         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
352         WM_event_add_keymap_handler(&ar->handlers, keymap);
353         
354         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
355         WM_event_add_keymap_handler(&ar->handlers, keymap);
356
357         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
358         WM_event_add_keymap_handler(&ar->handlers, keymap);
359
360         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
361         WM_event_add_keymap_handler(&ar->handlers, keymap);
362         
363         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
364         WM_event_add_keymap_handler(&ar->handlers, keymap);
365
366         /* armature sketching needs to take over mouse */
367         keymap= WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
368         WM_event_add_keymap_handler(&ar->handlers, keymap);
369
370         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
371         WM_event_add_keymap_handler(&ar->handlers, keymap);
372
373         /* editfont keymap swallows all... */
374         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
375         WM_event_add_keymap_handler(&ar->handlers, keymap);
376
377         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
378         WM_event_add_keymap_handler(&ar->handlers, keymap);
379
380         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
381         WM_event_add_keymap_handler(&ar->handlers, keymap);
382
383         /* own keymap, last so modes can override it */
384         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
385         WM_event_add_keymap_handler(&ar->handlers, keymap);
386
387         keymap= WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
388         WM_event_add_keymap_handler(&ar->handlers, keymap);
389 }
390
391 /* type callback, not region itself */
392 static void view3d_main_area_free(ARegion *ar)
393 {
394         RegionView3D *rv3d= ar->regiondata;
395         
396         if(rv3d) {
397                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
398                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
399
400                 // XXX  retopo_free_view_data(rv3d);
401                 if(rv3d->ri) { 
402                         // XXX          BIF_view3d_previewrender_free(rv3d);
403                 }
404                 
405                 if(rv3d->depths) {
406                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
407                         MEM_freeN(rv3d->depths);
408                 }
409                 MEM_freeN(rv3d);
410                 ar->regiondata= NULL;
411         }
412 }
413
414 /* copy regiondata */
415 static void *view3d_main_area_duplicate(void *poin)
416 {
417         if(poin) {
418                 RegionView3D *rv3d= poin, *new;
419         
420                 new= MEM_dupallocN(rv3d);
421                 if(rv3d->localvd) 
422                         new->localvd= MEM_dupallocN(rv3d->localvd);
423                 if(rv3d->clipbb) 
424                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
425                 
426                 new->depths= NULL;
427                 new->retopo_view_data= NULL;
428                 new->ri= NULL;
429                 new->gpd= NULL;
430                 new->sms= NULL;
431                 new->smooth_timer= NULL;
432                 
433                 return new;
434         }
435         return NULL;
436 }
437
438 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
439 {
440         /* context changes */
441         switch(wmn->category) {
442                 case NC_ANIMATION:
443                         switch(wmn->data) {
444                                 case ND_KEYFRAME_EDIT:
445                                 case ND_KEYFRAME_PROP:
446                                 case ND_NLA_EDIT:
447                                 case ND_NLA_ACTCHANGE:
448                                 case ND_ANIMCHAN_SELECT:
449                                         ED_region_tag_redraw(ar);
450                                         break;
451                         }
452                         break;
453                 case NC_SCENE:
454                         switch(wmn->data) {
455                                 case ND_FRAME:
456                                 case ND_TRANSFORM:
457                                 case ND_VIEW3D_TRANSFORM:
458                                 case ND_OB_ACTIVE:
459                                 case ND_OB_SELECT:
460                                 case ND_LAYER:
461                                 case ND_RENDER_OPTIONS:
462                                 case ND_MODE:
463                                         ED_region_tag_redraw(ar);
464                                         break;
465                         }
466                         if (wmn->action == NA_EDITED)
467                                 ED_region_tag_redraw(ar);
468                         break;
469                 case NC_OBJECT:
470                         switch(wmn->data) {
471                                 case ND_BONE_ACTIVE:
472                                 case ND_BONE_SELECT:
473                                 case ND_TRANSFORM:
474                                 case ND_VIEW3D_TRANSFORM:
475                                 case ND_POSE:
476                                 case ND_DRAW:
477                                 case ND_MODIFIER:
478                                 case ND_CONSTRAINT:
479                                 case ND_KEYS:
480                                 case ND_PARTICLE_SELECT:
481                                 case ND_PARTICLE_DATA:
482                                         ED_region_tag_redraw(ar);
483                                         break;
484                         }
485                         break;
486                 case NC_GEOM:
487                         switch(wmn->data) {
488                                 case ND_DATA:
489                                 case ND_SELECT:
490                                         ED_region_tag_redraw(ar);
491                                         break;
492                         }
493                         switch(wmn->action) {
494                                 case NA_EDITED:
495                                         ED_region_tag_redraw(ar);
496                                         break;
497                         }
498                         break;
499                 case NC_GROUP:
500                         /* all group ops for now */
501                         ED_region_tag_redraw(ar);
502                         break;
503                 case NC_BRUSH:
504                         if(wmn->action == NA_EDITED)
505                                 ED_region_tag_redraw(ar);
506                         break;                  
507                 case NC_MATERIAL:
508                         switch(wmn->data) {
509                                 case ND_SHADING_DRAW:
510                                         ED_region_tag_redraw(ar);
511                                         break;
512                         }
513                         break;
514                 case NC_WORLD:
515                         switch(wmn->data) {
516                                 case ND_WORLD_DRAW:
517                                         ED_region_tag_redraw(ar);
518                                         break;
519                         }
520                         break;
521                 case NC_LAMP:
522                         switch(wmn->data) {
523                                 case ND_LIGHTING_DRAW:
524                                         ED_region_tag_redraw(ar);
525                                         break;
526                         }
527                         break;
528                 case NC_IMAGE:  
529                         /* this could be more fine grained checks if we had
530                          * more context than just the region */
531                         ED_region_tag_redraw(ar);
532                         break;
533                 case NC_SPACE:
534                         if(wmn->data == ND_SPACE_VIEW3D)
535                                 ED_region_tag_redraw(ar);
536                         break;
537                 case NC_ID:
538                         if(wmn->action == NA_RENAME)
539                                 ED_region_tag_redraw(ar);
540                         break;
541         }
542 }
543
544 /* concept is to retrieve cursor type context-less */
545 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
546 {
547         Scene *scene= win->screen->scene;
548
549         if(scene->obedit) {
550                 WM_cursor_set(win, CURSOR_EDIT);
551         }
552         else {
553                 WM_cursor_set(win, CURSOR_STD);
554         }
555 }
556
557 /* add handlers, stuff you only do once or on area/region changes */
558 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
559 {
560         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
561         
562         WM_event_add_keymap_handler(&ar->handlers, keymap);
563
564         ED_region_header_init(ar);
565 }
566
567 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
568 {
569         ED_region_header(C, ar);
570 }
571
572 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
573 {
574         /* context changes */
575         switch(wmn->category) {
576                 case NC_SCENE:
577                         switch(wmn->data) {
578                                 case ND_FRAME:
579                                 case ND_OB_ACTIVE:
580                                 case ND_OB_SELECT:
581                                 case ND_MODE:
582                                 case ND_LAYER:
583                                 case ND_TOOLSETTINGS:
584                                         ED_region_tag_redraw(ar);
585                                         break;
586                         }
587                         break;
588                 case NC_SPACE:
589                         if(wmn->data == ND_SPACE_VIEW3D)
590                                 ED_region_tag_redraw(ar);
591                         break;
592         }
593 }
594
595 /* add handlers, stuff you only do once or on area/region changes */
596 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
597 {
598         wmKeyMap *keymap;
599
600         ED_region_panels_init(wm, ar);
601         
602         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
603         WM_event_add_keymap_handler(&ar->handlers, keymap);
604 }
605
606 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
607 {
608         ED_region_panels(C, ar, 1, NULL, -1);
609 }
610
611 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
612 {
613         /* context changes */
614         switch(wmn->category) {
615                 case NC_ANIMATION:
616                         switch(wmn->data) {
617                                 case ND_KEYFRAME_EDIT:
618                                 case ND_KEYFRAME_PROP:
619                                 case ND_NLA_EDIT:
620                                 case ND_NLA_ACTCHANGE:
621                                         ED_region_tag_redraw(ar);
622                                         break;
623                         }
624                         break;
625                 case NC_SCENE:
626                         switch(wmn->data) {
627                                 case ND_FRAME:
628                                 case ND_OB_ACTIVE:
629                                 case ND_OB_SELECT:
630                                 case ND_MODE:
631                                 case ND_LAYER:
632                                         ED_region_tag_redraw(ar);
633                                         break;
634                         }
635                         break;
636                 case NC_OBJECT:
637                         switch(wmn->data) {
638                                 case ND_BONE_ACTIVE:
639                                 case ND_BONE_SELECT:
640                                 case ND_TRANSFORM:
641                                 case ND_VIEW3D_TRANSFORM:
642                                 case ND_POSE:
643                                 case ND_DRAW:
644                                 case ND_KEYS:
645                                         ED_region_tag_redraw(ar);
646                                         break;
647                         }
648                         break;
649                 case NC_GEOM:
650                         switch(wmn->data) {
651                                 case ND_DATA:
652                                 case ND_SELECT:
653                                         ED_region_tag_redraw(ar);
654                                         break;
655                         }
656                         break;
657                 case NC_TEXTURE:
658                         /* for brush textures */
659                         ED_region_tag_redraw(ar);
660                         break;
661                 case NC_BRUSH:
662                         if(wmn->action==NA_EDITED)
663                                 ED_region_tag_redraw(ar);
664                         break;
665                 case NC_SPACE:
666                         if(wmn->data == ND_SPACE_VIEW3D)
667                                 ED_region_tag_redraw(ar);
668                         break;
669                 case NC_ID:
670                         if(wmn->action == NA_RENAME)
671                                 ED_region_tag_redraw(ar);
672                         break;
673         }
674 }
675
676 /* add handlers, stuff you only do once or on area/region changes */
677 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
678 {
679         wmKeyMap *keymap;
680         
681         ED_region_panels_init(wm, ar);
682
683         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
684         WM_event_add_keymap_handler(&ar->handlers, keymap);
685 }
686
687 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
688 {
689         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
690 }
691
692 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
693 {
694         View3D *v3d= CTX_wm_view3d(C);
695         Scene *scene= CTX_data_scene(C);
696         Base *base;
697         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
698
699         if(CTX_data_dir(member)) {
700                 static const char *dir[] = {
701                         "selected_objects", "selected_bases", "selected_editable_objects",
702                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
703                         "active_base", "active_object", NULL};
704
705                 CTX_data_dir_set(result, dir);
706         }
707         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
708                 int selected_objects= CTX_data_equals(member, "selected_objects");
709
710                 for(base=scene->base.first; base; base=base->next) {
711                         if((base->flag & SELECT) && (base->lay & lay)) {
712                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
713                                         if(selected_objects)
714                                                 CTX_data_id_list_add(result, &base->object->id);
715                                         else
716                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
717                                 }
718                         }
719                 }
720
721                 return 1;
722         }
723         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
724                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
725
726                 for(base=scene->base.first; base; base=base->next) {
727                         if((base->flag & SELECT) && (base->lay & lay)) {
728                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
729                                         if(0==object_is_libdata(base->object)) {
730                                                 if(selected_editable_objects)
731                                                         CTX_data_id_list_add(result, &base->object->id);
732                                                 else
733                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
734                                         }
735                                 }
736                         }
737                 }
738                 
739                 return 1;
740         }
741         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
742                 int visible_objects= CTX_data_equals(member, "visible_objects");
743
744                 for(base=scene->base.first; base; base=base->next) {
745                         if(base->lay & lay) {
746                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
747                                         if(visible_objects)
748                                                 CTX_data_id_list_add(result, &base->object->id);
749                                         else
750                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
751                                 }
752                         }
753                 }
754                 
755                 return 1;
756         }
757         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
758                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
759
760                 for(base=scene->base.first; base; base=base->next) {
761                         if(base->lay & lay) {
762                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
763                                         if(selectable_objects)
764                                                 CTX_data_id_list_add(result, &base->object->id);
765                                         else
766                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
767                                 }
768                         }
769                 }
770                 
771                 return 1;
772         }
773         else if(CTX_data_equals(member, "active_base")) {
774                 if(scene->basact && (scene->basact->lay & lay))
775                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
776                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
777                 
778                 return 1;
779         }
780         else if(CTX_data_equals(member, "active_object")) {
781                 if(scene->basact && (scene->basact->lay & lay))
782                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
783                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
784                 
785                 return 1;
786         }
787         else {
788                 return 0; /* not found */
789         }
790
791         return -1; /* found but not available */
792 }
793
794 /*area (not region) level listener*/
795 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
796 void space_view3d_listener(struct ScrArea *area, struct wmNotifier *wmn)
797 {
798         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
799                 View3D *v3d = area->spacedata.first;
800                 BGpic *bgpic = v3d->bgpicbase.first;
801
802                 for (; bgpic; bgpic = bgpic->next) {
803                         if (bgpic->ima) {
804                                 Scene *scene = wmn->reference;
805                                 BKE_image_user_calc_imanr(&bgpic->iuser, scene->r.cfra, 0);
806                         }
807                 }
808         }
809 }
810 #endif
811
812 /* only called once, from space/spacetypes.c */
813 void ED_spacetype_view3d(void)
814 {
815         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
816         ARegionType *art;
817         
818         st->spaceid= SPACE_VIEW3D;
819         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
820         
821         st->new= view3d_new;
822         st->free= view3d_free;
823         st->init= view3d_init;
824 //      st->listener = space_view3d_listener;
825         st->duplicate= view3d_duplicate;
826         st->operatortypes= view3d_operatortypes;
827         st->keymap= view3d_keymap;
828         st->context= view3d_context;
829         
830         /* regions: main window */
831         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
832         art->regionid = RGN_TYPE_WINDOW;
833         art->keymapflag= ED_KEYMAP_GPENCIL;
834         art->draw= view3d_main_area_draw;
835         art->init= view3d_main_area_init;
836         art->free= view3d_main_area_free;
837         art->duplicate= view3d_main_area_duplicate;
838         art->listener= view3d_main_area_listener;
839         art->cursor= view3d_main_area_cursor;
840         BLI_addhead(&st->regiontypes, art);
841         
842         /* regions: listview/buttons */
843         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
844         art->regionid = RGN_TYPE_UI;
845         art->minsizex= 180; // XXX
846         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
847         art->listener= view3d_buttons_area_listener;
848         art->init= view3d_buttons_area_init;
849         art->draw= view3d_buttons_area_draw;
850         BLI_addhead(&st->regiontypes, art);
851
852         view3d_buttons_register(art);
853
854         /* regions: tool(bar) */
855         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
856         art->regionid = RGN_TYPE_TOOLS;
857         art->minsizex= 160; // XXX
858         art->minsizey= 50; // XXX
859         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
860         art->listener= view3d_buttons_area_listener;
861         art->init= view3d_tools_area_init;
862         art->draw= view3d_tools_area_draw;
863         BLI_addhead(&st->regiontypes, art);
864         
865         view3d_toolshelf_register(art);
866
867         /* regions: tool properties */
868         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
869         art->regionid = RGN_TYPE_TOOL_PROPS;
870         art->minsizex= 0;
871         art->minsizey= 120;
872         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
873         art->listener= view3d_buttons_area_listener;
874         art->init= view3d_tools_area_init;
875         art->draw= view3d_tools_area_draw;
876         BLI_addhead(&st->regiontypes, art);
877         
878         view3d_tool_props_register(art);
879         
880         
881         /* regions: header */
882         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
883         art->regionid = RGN_TYPE_HEADER;
884         art->minsizey= HEADERY;
885         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
886         art->listener= view3d_header_area_listener;
887         art->init= view3d_header_area_init;
888         art->draw= view3d_header_area_draw;
889         BLI_addhead(&st->regiontypes, art);
890         
891         BKE_spacetype_register(st);
892 }
893