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