4c9d8f88dd190da1dcd4d6ace6a9216bedd1ebd6
[blender.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 *wm, ScrArea *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 *C, wmDrag *drag, wmEvent *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 *C, wmDrag *drag, wmEvent *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 *C, wmDrag *drag, wmEvent *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         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;
544
545                 if (lay_used & (1<<20-1)) break;
546
547                 base= base->next;
548         }
549
550         sa= win->screen->areabase.first;
551         while(sa) {
552                 if(sa->spacetype == SPACE_VIEW3D)
553                         if(BLI_findindex(&sa->regionbase, ar) >= 0) {
554                                 View3D *v3d= sa->spacedata.first;
555                                 v3d->lay_used= lay_used;
556                                 break;
557                         }
558
559                 sa= sa->next;
560         }
561 }
562
563 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
564 {
565         bScreen *sc;
566
567         /* context changes */
568         switch(wmn->category) {
569                 case NC_ANIMATION:
570                         switch(wmn->data) {
571                                 case ND_KEYFRAME_PROP:
572                                 case ND_NLA_ACTCHANGE:
573                                         ED_region_tag_redraw(ar);
574                                         break;
575                                 case ND_NLA:
576                                 case ND_KEYFRAME:
577                                         if (wmn->action == NA_EDITED)
578                                                 ED_region_tag_redraw(ar);
579                                         break;
580                                 case ND_ANIMCHAN:
581                                         if (wmn->action == NA_SELECTED)
582                                                 ED_region_tag_redraw(ar);
583                                         break;
584                         }
585                         break;
586                 case NC_SCENE:
587                         switch(wmn->data) {
588                                 case ND_LAYER_CONTENT:
589                                         view3d_recalc_used_layers(ar, wmn, wmn->reference);
590                                         ED_region_tag_redraw(ar);
591                                         break;
592                                 case ND_FRAME:
593                                 case ND_TRANSFORM:
594                                 case ND_OB_ACTIVE:
595                                 case ND_OB_SELECT:
596                                 case ND_OB_VISIBLE:
597                                 case ND_LAYER:
598                                 case ND_RENDER_OPTIONS:
599                                 case ND_MODE:
600                                         ED_region_tag_redraw(ar);
601                                         break;
602                         }
603                         if (wmn->action == NA_EDITED)
604                                 ED_region_tag_redraw(ar);
605                         break;
606                 case NC_OBJECT:
607                         switch(wmn->data) {
608                                 case ND_BONE_ACTIVE:
609                                 case ND_BONE_SELECT:
610                                 case ND_TRANSFORM:
611                                 case ND_POSE:
612                                 case ND_DRAW:
613                                 case ND_MODIFIER:
614                                 case ND_CONSTRAINT:
615                                 case ND_KEYS:
616                                 case ND_PARTICLE:
617                                         ED_region_tag_redraw(ar);
618                                         break;
619                         }
620                         break;
621                 case NC_GEOM:
622                         switch(wmn->data) {
623                                 case ND_DATA:
624                                 case ND_SELECT:
625                                         ED_region_tag_redraw(ar);
626                                         break;
627                         }
628                         switch(wmn->action) {
629                                 case NA_EDITED:
630                                         ED_region_tag_redraw(ar);
631                                         break;
632                         }
633                         break;
634                 case NC_GROUP:
635                         /* all group ops for now */
636                         ED_region_tag_redraw(ar);
637                         break;
638                 case NC_BRUSH:
639                         if(wmn->action == NA_EDITED)
640                                 ED_region_tag_redraw_overlay(ar);
641                         break;                  
642                 case NC_MATERIAL:
643                         switch(wmn->data) {
644                                 case ND_SHADING_DRAW:
645                                         ED_region_tag_redraw(ar);
646                                         break;
647                         }
648                         break;
649                 case NC_WORLD:
650                         switch(wmn->data) {
651                                 case ND_WORLD_DRAW:
652                                         ED_region_tag_redraw(ar);
653                                         break;
654                         }
655                         break;
656                 case NC_LAMP:
657                         switch(wmn->data) {
658                                 case ND_LIGHTING_DRAW:
659                                         ED_region_tag_redraw(ar);
660                                         break;
661                         }
662                         break;
663                 case NC_IMAGE:  
664                         /* this could be more fine grained checks if we had
665                          * more context than just the region */
666                         ED_region_tag_redraw(ar);
667                         break;
668                 case NC_TEXTURE:        
669                         /* same as above */
670                         ED_region_tag_redraw(ar);
671                         break;
672                 case NC_SPACE:
673                         if(wmn->data == ND_SPACE_VIEW3D) {
674                                 if (wmn->subtype == NS_VIEW3D_GPU) {
675                                         RegionView3D *rv3d= ar->regiondata;
676                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
677                                 }
678                                 ED_region_tag_redraw(ar);
679                         }
680                         break;
681                 case NC_ID:
682                         if(wmn->action == NA_RENAME)
683                                 ED_region_tag_redraw(ar);
684                         break;
685                 case NC_SCREEN:
686                         switch(wmn->data) {
687                                 case ND_GPENCIL:
688                                 case ND_ANIMPLAY:
689                                         ED_region_tag_redraw(ar);
690                                         break;
691                                 case ND_SCREENBROWSE:
692                                 case ND_SCREENDELETE:
693                                 case ND_SCREENSET:
694                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
695                                         /* updates used layers only for View3D in active screen */
696                                         sc= wmn->reference;
697                                         view3d_recalc_used_layers(ar, wmn, sc->scene);
698                                         ED_region_tag_redraw(ar);
699                                         break;
700                         }
701
702                         break;
703         }
704 }
705
706 /* concept is to retrieve cursor type context-less */
707 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
708 {
709         Scene *scene= win->screen->scene;
710
711         if(scene->obedit) {
712                 WM_cursor_set(win, CURSOR_EDIT);
713         }
714         else {
715                 WM_cursor_set(win, CURSOR_STD);
716         }
717 }
718
719 /* add handlers, stuff you only do once or on area/region changes */
720 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
721 {
722         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
723         
724         WM_event_add_keymap_handler(&ar->handlers, keymap);
725
726         ED_region_header_init(ar);
727 }
728
729 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
730 {
731         ED_region_header(C, ar);
732 }
733
734 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
735 {
736         /* context changes */
737         switch(wmn->category) {
738                 case NC_SCENE:
739                         switch(wmn->data) {
740                                 case ND_FRAME:
741                                 case ND_OB_ACTIVE:
742                                 case ND_OB_SELECT:
743                                 case ND_OB_VISIBLE:
744                                 case ND_MODE:
745                                 case ND_LAYER:
746                                 case ND_TOOLSETTINGS:
747                                 case ND_LAYER_CONTENT:
748                                         ED_region_tag_redraw(ar);
749                                         break;
750                         }
751                         break;
752                 case NC_SPACE:
753                         if(wmn->data == ND_SPACE_VIEW3D)
754                                 ED_region_tag_redraw(ar);
755                         break;
756         }
757 }
758
759 /* add handlers, stuff you only do once or on area/region changes */
760 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
761 {
762         wmKeyMap *keymap;
763
764         ED_region_panels_init(wm, ar);
765         
766         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
767         WM_event_add_keymap_handler(&ar->handlers, keymap);
768 }
769
770 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
771 {
772         ED_region_panels(C, ar, 1, NULL, -1);
773 }
774
775 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
776 {
777         /* context changes */
778         switch(wmn->category) {
779                 case NC_ANIMATION:
780                         switch(wmn->data) {
781                                 case ND_KEYFRAME_PROP:
782                                 case ND_NLA_ACTCHANGE:
783                                         ED_region_tag_redraw(ar);
784                                         break;
785                                 case ND_NLA:
786                                 case ND_KEYFRAME:
787                                         if (wmn->action == NA_EDITED)
788                                                 ED_region_tag_redraw(ar);
789                                         break;  
790                         }
791                         break;
792                 case NC_SCENE:
793                         switch(wmn->data) {
794                                 case ND_FRAME:
795                                 case ND_OB_ACTIVE:
796                                 case ND_OB_SELECT:
797                                 case ND_OB_VISIBLE:
798                                 case ND_MODE:
799                                 case ND_LAYER:
800                                 case ND_LAYER_CONTENT:
801                                         ED_region_tag_redraw(ar);
802                                         break;
803                         }
804                         switch(wmn->action) {
805                                 case NA_EDITED:
806                                         ED_region_tag_redraw(ar);
807                                         break;
808                         }
809                         break;
810                 case NC_OBJECT:
811                         switch(wmn->data) {
812                                 case ND_BONE_ACTIVE:
813                                 case ND_BONE_SELECT:
814                                 case ND_TRANSFORM:
815                                 case ND_POSE:
816                                 case ND_DRAW:
817                                 case ND_KEYS:
818                                 case ND_MODIFIER:
819                                         ED_region_tag_redraw(ar);
820                                         break;
821                         }
822                         break;
823                 case NC_GEOM:
824                         switch(wmn->data) {
825                                 case ND_DATA:
826                                 case ND_SELECT:
827                                         ED_region_tag_redraw(ar);
828                                         break;
829                         }
830                         if (wmn->action == NA_EDITED)
831                                 ED_region_tag_redraw(ar);
832                         break;
833                 case NC_TEXTURE:
834                         /* for brush textures */
835                         ED_region_tag_redraw(ar);
836                         break;
837                 case NC_BRUSH:
838                         if(wmn->action==NA_EDITED)
839                                 ED_region_tag_redraw(ar);
840                         break;
841                 case NC_SPACE:
842                         if(wmn->data == ND_SPACE_VIEW3D)
843                                 ED_region_tag_redraw(ar);
844                         break;
845                 case NC_ID:
846                         if(wmn->action == NA_RENAME)
847                                 ED_region_tag_redraw(ar);
848                         break;
849                 case NC_SCREEN: 
850                         if(wmn->data == ND_GPENCIL)
851                                 ED_region_tag_redraw(ar);
852                         break;
853         }
854 }
855
856 /* add handlers, stuff you only do once or on area/region changes */
857 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
858 {
859         wmKeyMap *keymap;
860         
861         ED_region_panels_init(wm, ar);
862
863         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
864         WM_event_add_keymap_handler(&ar->handlers, keymap);
865 }
866
867 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
868 {
869         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
870 }
871
872 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
873 {
874         /* context changes */
875         switch(wmn->category) {
876                 case NC_WM:
877                         if(wmn->data == ND_HISTORY)
878                                 ED_region_tag_redraw(ar);
879                         break;
880                 case NC_SCENE:
881                         if(wmn->data == ND_MODE)
882                                 ED_region_tag_redraw(ar);
883                         break;
884                 case NC_SPACE:
885                         if(wmn->data == ND_SPACE_VIEW3D)
886                                 ED_region_tag_redraw(ar);
887                         break;
888         }
889 }
890
891 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
892 {
893         View3D *v3d= CTX_wm_view3d(C);
894         Scene *scene= CTX_data_scene(C);
895         Base *base;
896         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
897
898         if(CTX_data_dir(member)) {
899                 static const char *dir[] = {
900                         "selected_objects", "selected_bases", "selected_editable_objects",
901                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
902                         "active_base", "active_object", NULL};
903
904                 CTX_data_dir_set(result, dir);
905         }
906         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
907                 int selected_objects= CTX_data_equals(member, "selected_objects");
908
909                 for(base=scene->base.first; base; base=base->next) {
910                         if((base->flag & SELECT) && (base->lay & lay)) {
911                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
912                                         if(selected_objects)
913                                                 CTX_data_id_list_add(result, &base->object->id);
914                                         else
915                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
916                                 }
917                         }
918                 }
919                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
920                 return 1;
921         }
922         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
923                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
924
925                 for(base=scene->base.first; base; base=base->next) {
926                         if((base->flag & SELECT) && (base->lay & lay)) {
927                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
928                                         if(0==object_is_libdata(base->object)) {
929                                                 if(selected_editable_objects)
930                                                         CTX_data_id_list_add(result, &base->object->id);
931                                                 else
932                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
933                                         }
934                                 }
935                         }
936                 }
937                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
938                 return 1;
939         }
940         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
941                 int visible_objects= CTX_data_equals(member, "visible_objects");
942
943                 for(base=scene->base.first; base; base=base->next) {
944                         if(base->lay & lay) {
945                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
946                                         if(visible_objects)
947                                                 CTX_data_id_list_add(result, &base->object->id);
948                                         else
949                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
950                                 }
951                         }
952                 }
953                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
954                 return 1;
955         }
956         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
957                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
958
959                 for(base=scene->base.first; base; base=base->next) {
960                         if(base->lay & lay) {
961                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
962                                         if(selectable_objects)
963                                                 CTX_data_id_list_add(result, &base->object->id);
964                                         else
965                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
966                                 }
967                         }
968                 }
969                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
970                 return 1;
971         }
972         else if(CTX_data_equals(member, "active_base")) {
973                 if(scene->basact && (scene->basact->lay & lay))
974                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
975                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
976                 
977                 return 1;
978         }
979         else if(CTX_data_equals(member, "active_object")) {
980                 if(scene->basact && (scene->basact->lay & lay))
981                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
982                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
983                 
984                 return 1;
985         }
986         else {
987                 return 0; /* not found */
988         }
989
990         return -1; /* found but not available */
991 }
992
993 /*area (not region) level listener*/
994 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
995 void space_view3d_listener(struct ScrArea *area, struct wmNotifier *wmn)
996 {
997         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
998                 View3D *v3d = area->spacedata.first;
999                 BGpic *bgpic = v3d->bgpicbase.first;
1000
1001                 for (; bgpic; bgpic = bgpic->next) {
1002                         if (bgpic->ima) {
1003                                 Scene *scene = wmn->reference;
1004                                 BKE_image_user_calc_frame(&bgpic->iuser, scene->r.cfra, 0);
1005                         }
1006                 }
1007         }
1008 }
1009 #endif
1010
1011 /* only called once, from space/spacetypes.c */
1012 void ED_spacetype_view3d(void)
1013 {
1014         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1015         ARegionType *art;
1016         
1017         st->spaceid= SPACE_VIEW3D;
1018         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1019         
1020         st->new= view3d_new;
1021         st->free= view3d_free;
1022         st->init= view3d_init;
1023 //      st->listener = space_view3d_listener;
1024         st->duplicate= view3d_duplicate;
1025         st->operatortypes= view3d_operatortypes;
1026         st->keymap= view3d_keymap;
1027         st->dropboxes= view3d_dropboxes;
1028         st->context= view3d_context;
1029         
1030         /* regions: main window */
1031         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1032         art->regionid = RGN_TYPE_WINDOW;
1033         art->keymapflag= ED_KEYMAP_GPENCIL;
1034         art->draw= view3d_main_area_draw;
1035         art->init= view3d_main_area_init;
1036         art->free= view3d_main_area_free;
1037         art->duplicate= view3d_main_area_duplicate;
1038         art->listener= view3d_main_area_listener;
1039         art->cursor= view3d_main_area_cursor;
1040         BLI_addhead(&st->regiontypes, art);
1041         
1042         /* regions: listview/buttons */
1043         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1044         art->regionid = RGN_TYPE_UI;
1045         art->prefsizex= 180; // XXX
1046         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1047         art->listener= view3d_buttons_area_listener;
1048         art->init= view3d_buttons_area_init;
1049         art->draw= view3d_buttons_area_draw;
1050         BLI_addhead(&st->regiontypes, art);
1051
1052         view3d_buttons_register(art);
1053
1054         /* regions: tool(bar) */
1055         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1056         art->regionid = RGN_TYPE_TOOLS;
1057         art->prefsizex= 160; // XXX
1058         art->prefsizey= 50; // XXX
1059         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1060         art->listener= view3d_buttons_area_listener;
1061         art->init= view3d_tools_area_init;
1062         art->draw= view3d_tools_area_draw;
1063         BLI_addhead(&st->regiontypes, art);
1064         
1065 #if 0
1066         /* unfinished still */
1067         view3d_toolshelf_register(art);
1068 #endif
1069
1070         /* regions: tool properties */
1071         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1072         art->regionid = RGN_TYPE_TOOL_PROPS;
1073         art->prefsizex= 0;
1074         art->prefsizey= 120;
1075         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1076         art->listener= view3d_props_area_listener;
1077         art->init= view3d_tools_area_init;
1078         art->draw= view3d_tools_area_draw;
1079         BLI_addhead(&st->regiontypes, art);
1080         
1081         view3d_tool_props_register(art);
1082         
1083         
1084         /* regions: header */
1085         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
1086         art->regionid = RGN_TYPE_HEADER;
1087         art->prefsizey= HEADERY;
1088         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1089         art->listener= view3d_header_area_listener;
1090         art->init= view3d_header_area_init;
1091         art->draw= view3d_header_area_draw;
1092         BLI_addhead(&st->regiontypes, art);
1093         
1094         BKE_spacetype_register(st);
1095 }
1096