bugfix [#24333] Horizon Color Picker Crashes Blender if Two 3D View Windows are Open
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_object_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_context.h"
42 #include "BKE_screen.h"
43
44 #include "ED_screen.h"
45 #include "ED_object.h"
46
47 #include "BIF_gl.h"
48
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53
54 #include "RNA_access.h"
55
56 #include "UI_resources.h"
57
58 #include "view3d_intern.h"      // own include
59
60 /* ******************** manage regions ********************* */
61
62 ARegion *view3d_has_buttons_region(ScrArea *sa)
63 {
64         ARegion *ar, *arnew;
65         
66         for(ar= sa->regionbase.first; ar; ar= ar->next)
67                 if(ar->regiontype==RGN_TYPE_UI)
68                         return ar;
69         
70         /* add subdiv level; after header */
71         for(ar= sa->regionbase.first; ar; ar= ar->next)
72                 if(ar->regiontype==RGN_TYPE_HEADER)
73                         break;
74         
75         /* is error! */
76         if(ar==NULL) return NULL;
77         
78         arnew= MEM_callocN(sizeof(ARegion), "buttons for view3d");
79         
80         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
81         arnew->regiontype= RGN_TYPE_UI;
82         arnew->alignment= RGN_ALIGN_RIGHT;
83         
84         arnew->flag = RGN_FLAG_HIDDEN;
85         
86         return arnew;
87 }
88
89 ARegion *view3d_has_tools_region(ScrArea *sa)
90 {
91         ARegion *ar, *artool=NULL, *arprops=NULL, *arhead;
92         
93         for(ar= sa->regionbase.first; ar; ar= ar->next) {
94                 if(ar->regiontype==RGN_TYPE_TOOLS)
95                         artool= ar;
96                 if(ar->regiontype==RGN_TYPE_TOOL_PROPS)
97                         arprops= ar;
98         }
99         
100         /* tool region hide/unhide also hides props */
101         if(arprops && artool) return artool;
102         
103         if(artool==NULL) {
104                 /* add subdiv level; after header */
105                 for(arhead= sa->regionbase.first; arhead; arhead= arhead->next)
106                         if(arhead->regiontype==RGN_TYPE_HEADER)
107                                 break;
108                 
109                 /* is error! */
110                 if(arhead==NULL) return NULL;
111                 
112                 artool= MEM_callocN(sizeof(ARegion), "tools for view3d");
113                 
114                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
115                 artool->regiontype= RGN_TYPE_TOOLS;
116                 artool->alignment= RGN_ALIGN_LEFT; //RGN_OVERLAP_LEFT;
117                 artool->flag = RGN_FLAG_HIDDEN;
118         }
119
120         if(arprops==NULL) {
121                 /* add extra subdivided region for tool properties */
122                 arprops= MEM_callocN(sizeof(ARegion), "tool props for view3d");
123                 
124                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
125                 arprops->regiontype= RGN_TYPE_TOOL_PROPS;
126                 arprops->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
127         }
128         
129         return artool;
130 }
131
132 /* ****************************************************** */
133
134 /* function to always find a regionview3d context inside 3D window */
135 RegionView3D *ED_view3d_context_rv3d(bContext *C)
136 {
137         RegionView3D *rv3d= CTX_wm_region_view3d(C);
138         
139         if(rv3d==NULL) {
140                 ScrArea *sa =CTX_wm_area(C);
141                 if(sa && sa->spacetype==SPACE_VIEW3D) {
142                         ARegion *ar;
143                         for(ar= sa->regionbase.first; ar; ar= ar->next)
144                                 if(ar->regiontype==RGN_TYPE_WINDOW)
145                                         return ar->regiondata;
146                 }
147         }
148         return rv3d;
149 }
150
151 /* Most of the time this isn't needed since you could assume the view matrix was
152  * set while drawing, however when functions like mesh_foreachScreenVert are
153  * called by selection tools, we can't be sure this object was the last.
154  *
155  * for example, transparent objects are drawn after editmode and will cause
156  * the rv3d mat's to change and break selection.
157  *
158  * 'ED_view3d_init_mats_rv3d' should be called before
159  * view3d_project_short_clip and view3d_project_short_noclip in cases where
160  * these functions are not used during draw_object
161  */
162 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
163 {
164         /* local viewmat and persmat, to calculate projections */
165         mul_m4_m4m4(rv3d->viewmatob, ob->obmat, rv3d->viewmat);
166         mul_m4_m4m4(rv3d->persmatob, ob->obmat, rv3d->persmat);
167
168         /* we have to multiply instead of loading viewmatob to make
169            it work with duplis using displists, otherwise it will
170            override the dupli-matrix */
171         glMultMatrixf(ob->obmat);
172
173         /* initializes object space clipping, speeds up clip tests */
174         ED_view3d_local_clipping(rv3d, ob->obmat);
175 }
176
177 /* ******************** default callbacks for view3d space ***************** */
178
179 static SpaceLink *view3d_new(const bContext *C)
180 {
181         Scene *scene= CTX_data_scene(C);
182         ARegion *ar;
183         View3D *v3d;
184         RegionView3D *rv3d;
185         
186         v3d= MEM_callocN(sizeof(View3D), "initview3d");
187         v3d->spacetype= SPACE_VIEW3D;
188         v3d->blockscale= 0.7f;
189         v3d->lay= v3d->layact= 1;
190         if(scene) {
191                 v3d->lay= v3d->layact= scene->lay;
192                 v3d->camera= scene->camera;
193         }
194         v3d->scenelock= 1;
195         v3d->grid= 1.0f;
196         v3d->gridlines= 16;
197         v3d->gridsubdiv = 10;
198         v3d->drawtype= OB_WIRE;
199         
200         v3d->gridflag |= V3D_SHOW_X;
201         v3d->gridflag |= V3D_SHOW_Y;
202         v3d->gridflag |= V3D_SHOW_FLOOR;
203         v3d->gridflag &= ~V3D_SHOW_Z;
204         
205         v3d->lens= 35.0f;
206         v3d->near= 0.01f;
207         v3d->far= 500.0f;
208
209         v3d->twtype= V3D_MANIP_TRANSLATE;
210         v3d->around= V3D_CENTROID;
211         
212         /* header */
213         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
214         
215         BLI_addtail(&v3d->regionbase, ar);
216         ar->regiontype= RGN_TYPE_HEADER;
217         ar->alignment= RGN_ALIGN_BOTTOM;
218         
219         /* tool shelf */
220         ar= MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
221         
222         BLI_addtail(&v3d->regionbase, ar);
223         ar->regiontype= RGN_TYPE_TOOLS;
224         ar->alignment= RGN_ALIGN_LEFT;
225         ar->flag = RGN_FLAG_HIDDEN;
226         
227         /* tool properties */
228         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
229         
230         BLI_addtail(&v3d->regionbase, ar);
231         ar->regiontype= RGN_TYPE_TOOL_PROPS;
232         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
233         ar->flag = RGN_FLAG_HIDDEN;
234         
235         /* buttons/list view */
236         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
237         
238         BLI_addtail(&v3d->regionbase, ar);
239         ar->regiontype= RGN_TYPE_UI;
240         ar->alignment= RGN_ALIGN_RIGHT;
241         ar->flag = RGN_FLAG_HIDDEN;
242         
243         /* main area */
244         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
245         
246         BLI_addtail(&v3d->regionbase, ar);
247         ar->regiontype= RGN_TYPE_WINDOW;
248         
249         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
250         rv3d= ar->regiondata;
251         rv3d->viewquat[0]= 1.0f;
252         rv3d->persp= 1;
253         rv3d->view= 7;
254         rv3d->dist= 10.0;
255         
256         return (SpaceLink *)v3d;
257 }
258
259 /* not spacelink itself */
260 static void view3d_free(SpaceLink *sl)
261 {
262         View3D *vd= (View3D *) sl;
263
264         BGpic *bgpic;
265         for(bgpic= vd->bgpicbase.first; bgpic; bgpic= bgpic->next) {
266                 if(bgpic->ima) bgpic->ima->id.us--;
267         }
268         BLI_freelistN(&vd->bgpicbase);
269
270         if(vd->localvd) MEM_freeN(vd->localvd);
271         
272         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
273 }
274
275
276 /* spacetype; init callback */
277 static void view3d_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
278 {
279
280 }
281
282 static SpaceLink *view3d_duplicate(SpaceLink *sl)
283 {
284         View3D *v3do= (View3D *)sl;
285         View3D *v3dn= MEM_dupallocN(sl);
286         BGpic *bgpic;
287         
288         /* clear or remove stuff from old */
289         
290 // XXX  BIF_view3d_previewrender_free(v3do);
291         
292         if(v3do->localvd) {
293                 v3do->localvd= NULL;
294                 v3do->properties_storage= NULL;
295                 v3do->lay= v3dn->localvd->lay;
296                 v3do->lay &= 0xFFFFFF;
297         }
298         
299         /* copy or clear inside new stuff */
300
301         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
302         for(bgpic= v3dn->bgpicbase.first; bgpic; bgpic= bgpic->next)
303                 if(bgpic->ima)
304                         bgpic->ima->id.us++;
305
306         v3dn->properties_storage= NULL;
307         
308         return (SpaceLink *)v3dn;
309 }
310
311 /* add handlers, stuff you only do once or on area/region changes */
312 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
313 {
314         ListBase *lb;
315         wmKeyMap *keymap;
316
317         /* object ops. */
318         
319         /* pose is not modal, operator poll checks for this */
320         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
321         WM_event_add_keymap_handler(&ar->handlers, keymap);
322         
323         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
324         WM_event_add_keymap_handler(&ar->handlers, keymap);
325
326         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
327         WM_event_add_keymap_handler(&ar->handlers, keymap);
328
329         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
330         WM_event_add_keymap_handler(&ar->handlers, keymap);
331
332         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
333         WM_event_add_keymap_handler(&ar->handlers, keymap);
334         
335         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
336         WM_event_add_keymap_handler(&ar->handlers, keymap);
337
338         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
339         WM_event_add_keymap_handler(&ar->handlers, keymap);
340         
341         keymap= WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
342         WM_event_add_keymap_handler(&ar->handlers, keymap);
343         
344         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
345         WM_event_add_keymap_handler(&ar->handlers, keymap);
346         
347         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
348         WM_event_add_keymap_handler(&ar->handlers, keymap);
349
350         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
351         WM_event_add_keymap_handler(&ar->handlers, keymap);
352
353         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
354         WM_event_add_keymap_handler(&ar->handlers, keymap);
355         
356         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
357         WM_event_add_keymap_handler(&ar->handlers, keymap);
358
359         /* armature sketching needs to take over mouse */
360         keymap= WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
361         WM_event_add_keymap_handler(&ar->handlers, keymap);
362
363         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
364         WM_event_add_keymap_handler(&ar->handlers, keymap);
365
366         /* editfont keymap swallows all... */
367         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
368         WM_event_add_keymap_handler(&ar->handlers, keymap);
369
370         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
371         WM_event_add_keymap_handler(&ar->handlers, keymap);
372
373         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
374         WM_event_add_keymap_handler(&ar->handlers, keymap);
375
376         /* own keymap, last so modes can override it */
377         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
378         WM_event_add_keymap_handler(&ar->handlers, keymap);
379
380         keymap= WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
381         WM_event_add_keymap_handler(&ar->handlers, keymap);
382         
383         /* add drop boxes */
384         lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
385         
386         WM_event_add_dropbox_handler(&ar->handlers, lb);
387         
388 }
389
390 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
391 {
392         if(drag->type==WM_DRAG_ID) {
393                 ID *id= (ID *)drag->poin;
394                 if( GS(id->name)==ID_OB )
395                         return 1;
396         }
397         return 0;
398 }
399
400 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
401 {
402         if(drag->type==WM_DRAG_ID) {
403                 ID *id= (ID *)drag->poin;
404                 if( GS(id->name)==ID_MA )
405                         return 1;
406         }
407         return 0;
408 }
409
410 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
411 {
412         if(drag->type==WM_DRAG_ID) {
413                 ID *id= (ID *)drag->poin;
414                 if( GS(id->name)==ID_IM )
415                         return 1;
416         }
417         else if(drag->type==WM_DRAG_PATH){
418                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
419                         return 1;
420         }
421         return 0;
422 }
423
424
425 static int view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
426 {
427         if( ED_view3d_give_base_under_cursor(C, event->mval) ) {
428                 return 0;
429         }
430         return view3d_ima_drop_poll(C, drag, event);
431 }
432
433 static int view3d_ima_ob_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
434 {
435         if( ED_view3d_give_base_under_cursor(C, event->mval) ) {
436                 return view3d_ima_drop_poll(C, drag, event);
437         }
438         return 0;
439 }
440
441 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
442 {
443         ID *id= (ID *)drag->poin;
444         PointerRNA ptr;
445
446         /* need to put name in sub-operator in macro */
447         ptr= RNA_pointer_get(drop->ptr, "OBJECT_OT_add_named");
448         if(ptr.data)
449                 RNA_string_set(&ptr, "name", id->name+2);
450         else
451                 RNA_string_set(drop->ptr, "name", id->name+2);
452 }
453
454 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
455 {
456         ID *id= (ID *)drag->poin;
457         
458         RNA_string_set(drop->ptr, "name", id->name+2);
459 }
460
461 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
462 {
463         ID *id= (ID *)drag->poin;
464         
465         if(id)
466                 RNA_string_set(drop->ptr, "name", id->name+2);
467         if(drag->path[0]) 
468                 RNA_string_set(drop->ptr, "filepath", drag->path);
469 }
470
471
472 /* region dropbox definition */
473 static void view3d_dropboxes(void)
474 {
475         ListBase *lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
476         
477         WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_ob_drop_copy);
478         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
479         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_ob_drop_poll, view3d_id_path_drop_copy);
480         WM_dropbox_add(lb, "VIEW3D_OT_add_background_image", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
481 }
482
483
484
485 /* type callback, not region itself */
486 static void view3d_main_area_free(ARegion *ar)
487 {
488         RegionView3D *rv3d= ar->regiondata;
489         
490         if(rv3d) {
491                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
492                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
493
494                 // XXX  retopo_free_view_data(rv3d);
495                 if(rv3d->ri) { 
496                         // XXX          BIF_view3d_previewrender_free(rv3d);
497                 }
498                 
499                 if(rv3d->depths) {
500                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
501                         MEM_freeN(rv3d->depths);
502                 }
503                 MEM_freeN(rv3d);
504                 ar->regiondata= NULL;
505         }
506 }
507
508 /* copy regiondata */
509 static void *view3d_main_area_duplicate(void *poin)
510 {
511         if(poin) {
512                 RegionView3D *rv3d= poin, *new;
513         
514                 new= MEM_dupallocN(rv3d);
515                 if(rv3d->localvd) 
516                         new->localvd= MEM_dupallocN(rv3d->localvd);
517                 if(rv3d->clipbb) 
518                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
519                 
520                 new->depths= NULL;
521                 new->retopo_view_data= NULL;
522                 new->ri= NULL;
523                 new->gpd= NULL;
524                 new->sms= NULL;
525                 new->smooth_timer= NULL;
526                 
527                 return new;
528         }
529         return NULL;
530 }
531
532 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
533 {
534         wmWindow *win= wmn->wm->winactive;
535         ScrArea *sa;
536         unsigned int lay_used= 0;
537         Base *base;
538
539         if (!win) return;
540
541         base= scene->base.first;
542         while(base) {
543                 lay_used |= base->lay & ((1<<20)-1); /* ignore localview */
544
545                 if (lay_used == (1<<20)-1)
546                         break;
547
548                 base= base->next;
549         }
550
551         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
552                 if(sa->spacetype == SPACE_VIEW3D) {
553                         if(BLI_findindex(&sa->regionbase, ar) != -1) {
554                                 View3D *v3d= sa->spacedata.first;
555                                 v3d->lay_used= lay_used;
556                                 break;
557                         }
558                 }
559         }
560 }
561
562 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
563 {
564         bScreen *sc;
565
566         /* context changes */
567         switch(wmn->category) {
568                 case NC_ANIMATION:
569                         switch(wmn->data) {
570                                 case ND_KEYFRAME_PROP:
571                                 case ND_NLA_ACTCHANGE:
572                                         ED_region_tag_redraw(ar);
573                                         break;
574                                 case ND_NLA:
575                                 case ND_KEYFRAME:
576                                         if (wmn->action == NA_EDITED)
577                                                 ED_region_tag_redraw(ar);
578                                         break;
579                                 case ND_ANIMCHAN:
580                                         if (wmn->action == NA_SELECTED)
581                                                 ED_region_tag_redraw(ar);
582                                         break;
583                         }
584                         break;
585                 case NC_SCENE:
586                         switch(wmn->data) {
587                                 case ND_LAYER_CONTENT:
588                                         view3d_recalc_used_layers(ar, wmn, wmn->reference);
589                                         ED_region_tag_redraw(ar);
590                                         break;
591                                 case ND_FRAME:
592                                 case ND_TRANSFORM:
593                                 case ND_OB_ACTIVE:
594                                 case ND_OB_SELECT:
595                                 case ND_OB_VISIBLE:
596                                 case ND_LAYER:
597                                 case ND_RENDER_OPTIONS:
598                                 case ND_MODE:
599                                         ED_region_tag_redraw(ar);
600                                         break;
601                                 case ND_WORLD:
602                                         /* handled by space_view3d_listener() for v3d access */
603                                         break;
604                         }
605                         if (wmn->action == NA_EDITED)
606                                 ED_region_tag_redraw(ar);
607                         break;
608                 case NC_OBJECT:
609                         switch(wmn->data) {
610                                 case ND_BONE_ACTIVE:
611                                 case ND_BONE_SELECT:
612                                 case ND_TRANSFORM:
613                                 case ND_POSE:
614                                 case ND_DRAW:
615                                 case ND_MODIFIER:
616                                 case ND_CONSTRAINT:
617                                 case ND_KEYS:
618                                 case ND_PARTICLE:
619                                         ED_region_tag_redraw(ar);
620                                         break;
621                         }
622                         break;
623                 case NC_GEOM:
624                         switch(wmn->data) {
625                                 case ND_DATA:
626                                 case ND_SELECT:
627                                         ED_region_tag_redraw(ar);
628                                         break;
629                         }
630                         switch(wmn->action) {
631                                 case NA_EDITED:
632                                         ED_region_tag_redraw(ar);
633                                         break;
634                         }
635                         break;
636                 case NC_GROUP:
637                         /* all group ops for now */
638                         ED_region_tag_redraw(ar);
639                         break;
640                 case NC_BRUSH:
641                         if(wmn->action == NA_EDITED)
642                                 ED_region_tag_redraw_overlay(ar);
643                         break;                  
644                 case NC_MATERIAL:
645                         switch(wmn->data) {
646                                 case ND_SHADING_DRAW:
647                                         ED_region_tag_redraw(ar);
648                                         break;
649                         }
650                         break;
651                 case NC_WORLD:
652                         switch(wmn->data) {
653                                 case ND_WORLD_DRAW:
654                                         /* handled by space_view3d_listener() for v3d access */
655                                         break;
656                         }
657                         break;
658                 case NC_LAMP:
659                         switch(wmn->data) {
660                                 case ND_LIGHTING_DRAW:
661                                         ED_region_tag_redraw(ar);
662                                         break;
663                         }
664                         break;
665                 case NC_IMAGE:  
666                         /* this could be more fine grained checks if we had
667                          * more context than just the region */
668                         ED_region_tag_redraw(ar);
669                         break;
670                 case NC_TEXTURE:        
671                         /* same as above */
672                         ED_region_tag_redraw(ar);
673                         break;
674                 case NC_SPACE:
675                         if(wmn->data == ND_SPACE_VIEW3D) {
676                                 if (wmn->subtype == NS_VIEW3D_GPU) {
677                                         RegionView3D *rv3d= ar->regiondata;
678                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
679                                 }
680                                 ED_region_tag_redraw(ar);
681                         }
682                         break;
683                 case NC_ID:
684                         if(wmn->action == NA_RENAME)
685                                 ED_region_tag_redraw(ar);
686                         break;
687                 case NC_SCREEN:
688                         switch(wmn->data) {
689                                 case ND_GPENCIL:
690                                 case ND_ANIMPLAY:
691                                         ED_region_tag_redraw(ar);
692                                         break;
693                                 case ND_SCREENBROWSE:
694                                 case ND_SCREENDELETE:
695                                 case ND_SCREENSET:
696                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
697                                         /* updates used layers only for View3D in active screen */
698                                         sc= wmn->reference;
699                                         view3d_recalc_used_layers(ar, wmn, sc->scene);
700                                         ED_region_tag_redraw(ar);
701                                         break;
702                         }
703
704                         break;
705         }
706 }
707
708 /* concept is to retrieve cursor type context-less */
709 static void view3d_main_area_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
710 {
711         Scene *scene= win->screen->scene;
712
713         if(scene->obedit) {
714                 WM_cursor_set(win, CURSOR_EDIT);
715         }
716         else {
717                 WM_cursor_set(win, CURSOR_STD);
718         }
719 }
720
721 /* add handlers, stuff you only do once or on area/region changes */
722 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
723 {
724         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
725         
726         WM_event_add_keymap_handler(&ar->handlers, keymap);
727
728         ED_region_header_init(ar);
729 }
730
731 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
732 {
733         ED_region_header(C, ar);
734 }
735
736 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
737 {
738         /* context changes */
739         switch(wmn->category) {
740                 case NC_SCENE:
741                         switch(wmn->data) {
742                                 case ND_FRAME:
743                                 case ND_OB_ACTIVE:
744                                 case ND_OB_SELECT:
745                                 case ND_OB_VISIBLE:
746                                 case ND_MODE:
747                                 case ND_LAYER:
748                                 case ND_TOOLSETTINGS:
749                                 case ND_LAYER_CONTENT:
750                                         ED_region_tag_redraw(ar);
751                                         break;
752                         }
753                         break;
754                 case NC_SPACE:
755                         if(wmn->data == ND_SPACE_VIEW3D)
756                                 ED_region_tag_redraw(ar);
757                         break;
758         }
759 }
760
761 /* add handlers, stuff you only do once or on area/region changes */
762 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
763 {
764         wmKeyMap *keymap;
765
766         ED_region_panels_init(wm, ar);
767         
768         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
769         WM_event_add_keymap_handler(&ar->handlers, keymap);
770 }
771
772 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
773 {
774         ED_region_panels(C, ar, 1, NULL, -1);
775 }
776
777 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
778 {
779         /* context changes */
780         switch(wmn->category) {
781                 case NC_ANIMATION:
782                         switch(wmn->data) {
783                                 case ND_KEYFRAME_PROP:
784                                 case ND_NLA_ACTCHANGE:
785                                         ED_region_tag_redraw(ar);
786                                         break;
787                                 case ND_NLA:
788                                 case ND_KEYFRAME:
789                                         if (wmn->action == NA_EDITED)
790                                                 ED_region_tag_redraw(ar);
791                                         break;  
792                         }
793                         break;
794                 case NC_SCENE:
795                         switch(wmn->data) {
796                                 case ND_FRAME:
797                                 case ND_OB_ACTIVE:
798                                 case ND_OB_SELECT:
799                                 case ND_OB_VISIBLE:
800                                 case ND_MODE:
801                                 case ND_LAYER:
802                                 case ND_LAYER_CONTENT:
803                                         ED_region_tag_redraw(ar);
804                                         break;
805                         }
806                         switch(wmn->action) {
807                                 case NA_EDITED:
808                                         ED_region_tag_redraw(ar);
809                                         break;
810                         }
811                         break;
812                 case NC_OBJECT:
813                         switch(wmn->data) {
814                                 case ND_BONE_ACTIVE:
815                                 case ND_BONE_SELECT:
816                                 case ND_TRANSFORM:
817                                 case ND_POSE:
818                                 case ND_DRAW:
819                                 case ND_KEYS:
820                                 case ND_MODIFIER:
821                                         ED_region_tag_redraw(ar);
822                                         break;
823                         }
824                         break;
825                 case NC_GEOM:
826                         switch(wmn->data) {
827                                 case ND_DATA:
828                                 case ND_SELECT:
829                                         ED_region_tag_redraw(ar);
830                                         break;
831                         }
832                         if (wmn->action == NA_EDITED)
833                                 ED_region_tag_redraw(ar);
834                         break;
835                 case NC_TEXTURE:
836                         /* for brush textures */
837                         ED_region_tag_redraw(ar);
838                         break;
839                 case NC_BRUSH:
840                         if(wmn->action==NA_EDITED)
841                                 ED_region_tag_redraw(ar);
842                         break;
843                 case NC_SPACE:
844                         if(wmn->data == ND_SPACE_VIEW3D)
845                                 ED_region_tag_redraw(ar);
846                         break;
847                 case NC_ID:
848                         if(wmn->action == NA_RENAME)
849                                 ED_region_tag_redraw(ar);
850                         break;
851                 case NC_SCREEN: 
852                         if(wmn->data == ND_GPENCIL)
853                                 ED_region_tag_redraw(ar);
854                         break;
855         }
856 }
857
858 /* add handlers, stuff you only do once or on area/region changes */
859 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
860 {
861         wmKeyMap *keymap;
862         
863         ED_region_panels_init(wm, ar);
864
865         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
866         WM_event_add_keymap_handler(&ar->handlers, keymap);
867 }
868
869 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
870 {
871         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
872 }
873
874 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
875 {
876         /* context changes */
877         switch(wmn->category) {
878                 case NC_WM:
879                         if(wmn->data == ND_HISTORY)
880                                 ED_region_tag_redraw(ar);
881                         break;
882                 case NC_SCENE:
883                         if(wmn->data == ND_MODE)
884                                 ED_region_tag_redraw(ar);
885                         break;
886                 case NC_SPACE:
887                         if(wmn->data == ND_SPACE_VIEW3D)
888                                 ED_region_tag_redraw(ar);
889                         break;
890         }
891 }
892
893 /*area (not region) level listener*/
894 void space_view3d_listener(struct ScrArea *sa, struct wmNotifier *wmn)
895 {
896         View3D *v3d = sa->spacedata.first;
897
898         /* context changes */
899         switch(wmn->category) {
900                 case NC_SCENE:
901                         switch(wmn->data) {
902                                 case ND_WORLD:
903                                         if(v3d->flag2 & V3D_RENDER_OVERRIDE)
904                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
905                                         break;
906                         }
907                         break;
908                 case NC_WORLD:
909                         switch(wmn->data) {
910                                 case ND_WORLD_DRAW:
911                                         if(v3d->flag2 & V3D_RENDER_OVERRIDE)
912                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
913                                         break;
914                         }
915                         break;
916
917         }
918
919 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
920         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
921                 View3D *v3d = area->spacedata.first;
922                 BGpic *bgpic = v3d->bgpicbase.first;
923
924                 for (; bgpic; bgpic = bgpic->next) {
925                         if (bgpic->ima) {
926                                 Scene *scene = wmn->reference;
927                                 BKE_image_user_calc_frame(&bgpic->iuser, scene->r.cfra, 0);
928                         }
929                 }
930         }
931 #endif
932 }
933
934 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
935 {
936         View3D *v3d= CTX_wm_view3d(C);
937         Scene *scene= CTX_data_scene(C);
938         Base *base;
939         unsigned int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
940
941         if(CTX_data_dir(member)) {
942                 static const char *dir[] = {
943                         "selected_objects", "selected_bases", "selected_editable_objects",
944                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
945                         "active_base", "active_object", NULL};
946
947                 CTX_data_dir_set(result, dir);
948         }
949         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
950                 int selected_objects= CTX_data_equals(member, "selected_objects");
951
952                 for(base=scene->base.first; base; base=base->next) {
953                         if((base->flag & SELECT) && (base->lay & lay)) {
954                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
955                                         if(selected_objects)
956                                                 CTX_data_id_list_add(result, &base->object->id);
957                                         else
958                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
959                                 }
960                         }
961                 }
962                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
963                 return 1;
964         }
965         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
966                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
967
968                 for(base=scene->base.first; base; base=base->next) {
969                         if((base->flag & SELECT) && (base->lay & lay)) {
970                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
971                                         if(0==object_is_libdata(base->object)) {
972                                                 if(selected_editable_objects)
973                                                         CTX_data_id_list_add(result, &base->object->id);
974                                                 else
975                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
976                                         }
977                                 }
978                         }
979                 }
980                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
981                 return 1;
982         }
983         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
984                 int visible_objects= CTX_data_equals(member, "visible_objects");
985
986                 for(base=scene->base.first; base; base=base->next) {
987                         if(base->lay & lay) {
988                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
989                                         if(visible_objects)
990                                                 CTX_data_id_list_add(result, &base->object->id);
991                                         else
992                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
993                                 }
994                         }
995                 }
996                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
997                 return 1;
998         }
999         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
1000                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
1001
1002                 for(base=scene->base.first; base; base=base->next) {
1003                         if(base->lay & lay) {
1004                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
1005                                         if(selectable_objects)
1006                                                 CTX_data_id_list_add(result, &base->object->id);
1007                                         else
1008                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1009                                 }
1010                         }
1011                 }
1012                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1013                 return 1;
1014         }
1015         else if(CTX_data_equals(member, "active_base")) {
1016                 if(scene->basact && (scene->basact->lay & lay))
1017                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1018                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
1019                 
1020                 return 1;
1021         }
1022         else if(CTX_data_equals(member, "active_object")) {
1023                 if(scene->basact && (scene->basact->lay & lay))
1024                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1025                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
1026                 
1027                 return 1;
1028         }
1029         else {
1030                 return 0; /* not found */
1031         }
1032
1033         return -1; /* found but not available */
1034 }
1035
1036
1037 /* only called once, from space/spacetypes.c */
1038 void ED_spacetype_view3d(void)
1039 {
1040         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1041         ARegionType *art;
1042         
1043         st->spaceid= SPACE_VIEW3D;
1044         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1045         
1046         st->new= view3d_new;
1047         st->free= view3d_free;
1048         st->init= view3d_init;
1049         st->listener = space_view3d_listener;
1050         st->duplicate= view3d_duplicate;
1051         st->operatortypes= view3d_operatortypes;
1052         st->keymap= view3d_keymap;
1053         st->dropboxes= view3d_dropboxes;
1054         st->context= view3d_context;
1055         
1056         /* regions: main window */
1057         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1058         art->regionid = RGN_TYPE_WINDOW;
1059         art->keymapflag= ED_KEYMAP_GPENCIL;
1060         art->draw= view3d_main_area_draw;
1061         art->init= view3d_main_area_init;
1062         art->free= view3d_main_area_free;
1063         art->duplicate= view3d_main_area_duplicate;
1064         art->listener= view3d_main_area_listener;
1065         art->cursor= view3d_main_area_cursor;
1066         BLI_addhead(&st->regiontypes, art);
1067         
1068         /* regions: listview/buttons */
1069         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1070         art->regionid = RGN_TYPE_UI;
1071         art->prefsizex= 180; // XXX
1072         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1073         art->listener= view3d_buttons_area_listener;
1074         art->init= view3d_buttons_area_init;
1075         art->draw= view3d_buttons_area_draw;
1076         BLI_addhead(&st->regiontypes, art);
1077
1078         view3d_buttons_register(art);
1079
1080         /* regions: tool(bar) */
1081         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1082         art->regionid = RGN_TYPE_TOOLS;
1083         art->prefsizex= 160; // XXX
1084         art->prefsizey= 50; // XXX
1085         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1086         art->listener= view3d_buttons_area_listener;
1087         art->init= view3d_tools_area_init;
1088         art->draw= view3d_tools_area_draw;
1089         BLI_addhead(&st->regiontypes, art);
1090         
1091 #if 0
1092         /* unfinished still */
1093         view3d_toolshelf_register(art);
1094 #endif
1095
1096         /* regions: tool properties */
1097         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1098         art->regionid = RGN_TYPE_TOOL_PROPS;
1099         art->prefsizex= 0;
1100         art->prefsizey= 120;
1101         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1102         art->listener= view3d_props_area_listener;
1103         art->init= view3d_tools_area_init;
1104         art->draw= view3d_tools_area_draw;
1105         BLI_addhead(&st->regiontypes, art);
1106         
1107         view3d_tool_props_register(art);
1108         
1109         
1110         /* regions: header */
1111         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1112         art->regionid = RGN_TYPE_HEADER;
1113         art->prefsizey= HEADERY;
1114         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1115         art->listener= view3d_header_area_listener;
1116         art->init= view3d_header_area_init;
1117         art->draw= view3d_header_area_draw;
1118         BLI_addhead(&st->regiontypes, art);
1119         
1120         BKE_spacetype_register(st);
1121 }
1122