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