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