svn merge -r41638:41648 ^/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         v3d->flag2 |= V3D_SHOW_RECONSTRUCTION;
254         
255         v3d->lens= 35.0f;
256         v3d->near= 0.01f;
257         v3d->far= 1000.0f;
258
259         v3d->twflag |= U.tw_flag & V3D_USE_MANIPULATOR;
260         v3d->twtype= V3D_MANIP_TRANSLATE;
261         v3d->around= V3D_CENTROID;
262         
263         v3d->bundle_size= 0.2f;
264         v3d->bundle_drawtype= OB_PLAINAXES;
265         
266         /* header */
267         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
268         
269         BLI_addtail(&v3d->regionbase, ar);
270         ar->regiontype= RGN_TYPE_HEADER;
271         ar->alignment= RGN_ALIGN_BOTTOM;
272         
273         /* tool shelf */
274         ar= MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
275         
276         BLI_addtail(&v3d->regionbase, ar);
277         ar->regiontype= RGN_TYPE_TOOLS;
278         ar->alignment= RGN_ALIGN_LEFT;
279         ar->flag = RGN_FLAG_HIDDEN;
280         
281         /* tool properties */
282         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
283         
284         BLI_addtail(&v3d->regionbase, ar);
285         ar->regiontype= RGN_TYPE_TOOL_PROPS;
286         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
287         ar->flag = RGN_FLAG_HIDDEN;
288         
289         /* buttons/list view */
290         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
291         
292         BLI_addtail(&v3d->regionbase, ar);
293         ar->regiontype= RGN_TYPE_UI;
294         ar->alignment= RGN_ALIGN_RIGHT;
295         ar->flag = RGN_FLAG_HIDDEN;
296         
297         /* main area */
298         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
299         
300         BLI_addtail(&v3d->regionbase, ar);
301         ar->regiontype= RGN_TYPE_WINDOW;
302         
303         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
304         rv3d= ar->regiondata;
305         rv3d->viewquat[0]= 1.0f;
306         rv3d->persp= RV3D_PERSP;
307         rv3d->view= RV3D_VIEW_PERSPORTHO;
308         rv3d->dist= 10.0;
309         
310         return (SpaceLink *)v3d;
311 }
312
313 /* not spacelink itself */
314 static void view3d_free(SpaceLink *sl)
315 {
316         View3D *vd= (View3D *) sl;
317
318         BGpic *bgpic;
319         for(bgpic= vd->bgpicbase.first; bgpic; bgpic= bgpic->next) {
320                 if(bgpic->ima) bgpic->ima->id.us--;
321         }
322         BLI_freelistN(&vd->bgpicbase);
323
324         if(vd->localvd) MEM_freeN(vd->localvd);
325         
326         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
327 }
328
329
330 /* spacetype; init callback */
331 static void view3d_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
332 {
333
334 }
335
336 static SpaceLink *view3d_duplicate(SpaceLink *sl)
337 {
338         View3D *v3do= (View3D *)sl;
339         View3D *v3dn= MEM_dupallocN(sl);
340         BGpic *bgpic;
341         
342         /* clear or remove stuff from old */
343         
344 // XXX  BIF_view3d_previewrender_free(v3do);
345         
346         if(v3do->localvd) {
347                 v3do->localvd= NULL;
348                 v3do->properties_storage= NULL;
349                 v3do->lay= v3dn->localvd->lay;
350                 v3do->lay &= 0xFFFFFF;
351         }
352
353         if(v3dn->drawtype == OB_RENDER)
354                 v3dn->drawtype = OB_SOLID;
355         
356         /* copy or clear inside new stuff */
357
358         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
359         for(bgpic= v3dn->bgpicbase.first; bgpic; bgpic= bgpic->next)
360                 if(bgpic->ima)
361                         bgpic->ima->id.us++;
362
363         v3dn->properties_storage= NULL;
364         
365         return (SpaceLink *)v3dn;
366 }
367
368 /* add handlers, stuff you only do once or on area/region changes */
369 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
370 {
371         ListBase *lb;
372         wmKeyMap *keymap;
373
374         /* object ops. */
375         
376         /* important to be before Pose keymap since they can both be enabled at once */
377         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
378         WM_event_add_keymap_handler(&ar->handlers, keymap);
379         
380         
381         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
382         WM_event_add_keymap_handler(&ar->handlers, keymap);
383
384         /* pose is not modal, operator poll checks for this */
385         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
386         WM_event_add_keymap_handler(&ar->handlers, keymap);
387         
388         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
389         WM_event_add_keymap_handler(&ar->handlers, keymap);
390
391         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
392         WM_event_add_keymap_handler(&ar->handlers, keymap);
393
394         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
395         WM_event_add_keymap_handler(&ar->handlers, keymap);
396
397         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
398         WM_event_add_keymap_handler(&ar->handlers, keymap);
399
400         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
401         WM_event_add_keymap_handler(&ar->handlers, keymap);
402         
403         keymap= WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
404         WM_event_add_keymap_handler(&ar->handlers, keymap);
405         
406         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
407         WM_event_add_keymap_handler(&ar->handlers, keymap);
408         
409         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
410         WM_event_add_keymap_handler(&ar->handlers, keymap);
411
412         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
413         WM_event_add_keymap_handler(&ar->handlers, keymap);
414
415         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
416         WM_event_add_keymap_handler(&ar->handlers, keymap);
417         
418         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
419         WM_event_add_keymap_handler(&ar->handlers, keymap);
420
421         /* armature sketching needs to take over mouse */
422         keymap= WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
423         WM_event_add_keymap_handler(&ar->handlers, keymap);
424
425         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
426         WM_event_add_keymap_handler(&ar->handlers, keymap);
427
428         /* editfont keymap swallows all... */
429         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
430         WM_event_add_keymap_handler(&ar->handlers, keymap);
431
432         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
433         WM_event_add_keymap_handler(&ar->handlers, keymap);
434
435         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
436         WM_event_add_keymap_handler(&ar->handlers, keymap);
437
438         /* own keymap, last so modes can override it */
439         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
440         WM_event_add_keymap_handler(&ar->handlers, keymap);
441
442         keymap= WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
443         WM_event_add_keymap_handler(&ar->handlers, keymap);
444         
445         /* add drop boxes */
446         lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
447         
448         WM_event_add_dropbox_handler(&ar->handlers, lb);
449         
450 }
451
452 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
453 {
454         if(drag->type==WM_DRAG_ID) {
455                 ID *id= (ID *)drag->poin;
456                 if( GS(id->name)==ID_OB )
457                         return 1;
458         }
459         return 0;
460 }
461
462 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
463 {
464         if(drag->type==WM_DRAG_ID) {
465                 ID *id= (ID *)drag->poin;
466                 if( GS(id->name)==ID_MA )
467                         return 1;
468         }
469         return 0;
470 }
471
472 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
473 {
474         if(drag->type==WM_DRAG_ID) {
475                 ID *id= (ID *)drag->poin;
476                 if( GS(id->name)==ID_IM )
477                         return 1;
478         }
479         else if(drag->type==WM_DRAG_PATH){
480                 if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
481                         return 1;
482         }
483         return 0;
484 }
485
486
487 static int view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
488 {
489         if( ED_view3d_give_base_under_cursor(C, event->mval) ) {
490                 return 0;
491         }
492
493         return view3d_ima_drop_poll(C, drag, event);
494 }
495
496 static int view3d_ima_ob_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
497 {
498         if( ED_view3d_give_base_under_cursor(C, event->mval) ) {
499                 return view3d_ima_drop_poll(C, drag, event);
500         }
501         return 0;
502 }
503
504 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
505 {
506         ID *id= (ID *)drag->poin;
507         PointerRNA ptr;
508
509         /* need to put name in sub-operator in macro */
510         ptr= RNA_pointer_get(drop->ptr, "OBJECT_OT_add_named");
511         if(ptr.data)
512                 RNA_string_set(&ptr, "name", id->name+2);
513         else
514                 RNA_string_set(drop->ptr, "name", id->name+2);
515 }
516
517 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
518 {
519         ID *id= (ID *)drag->poin;
520         
521         RNA_string_set(drop->ptr, "name", id->name+2);
522 }
523
524 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
525 {
526         ID *id= (ID *)drag->poin;
527         
528         if(id)
529                 RNA_string_set(drop->ptr, "name", id->name+2);
530         if(drag->path[0]) 
531                 RNA_string_set(drop->ptr, "filepath", drag->path);
532 }
533
534
535 /* region dropbox definition */
536 static void view3d_dropboxes(void)
537 {
538         ListBase *lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
539         
540         WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_ob_drop_copy);
541         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
542         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_ob_drop_poll, view3d_id_path_drop_copy);
543         WM_dropbox_add(lb, "VIEW3D_OT_background_image_add", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
544 }
545
546
547
548 /* type callback, not region itself */
549 static void view3d_main_area_free(ARegion *ar)
550 {
551         RegionView3D *rv3d= ar->regiondata;
552         
553         if(rv3d) {
554                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
555                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
556
557                 // XXX  retopo_free_view_data(rv3d);
558                 if(rv3d->ri) { 
559                         // XXX          BIF_view3d_previewrender_free(rv3d);
560                 }
561
562                 if(rv3d->render_engine)
563                         RE_engine_free(rv3d->render_engine);
564                 
565                 if(rv3d->depths) {
566                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
567                         MEM_freeN(rv3d->depths);
568                 }
569                 MEM_freeN(rv3d);
570                 ar->regiondata= NULL;
571         }
572 }
573
574 /* copy regiondata */
575 static void *view3d_main_area_duplicate(void *poin)
576 {
577         if(poin) {
578                 RegionView3D *rv3d= poin, *new;
579         
580                 new= MEM_dupallocN(rv3d);
581                 if(rv3d->localvd) 
582                         new->localvd= MEM_dupallocN(rv3d->localvd);
583                 if(rv3d->clipbb) 
584                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
585                 
586                 new->depths= NULL;
587                 new->ri= NULL;
588                 new->render_engine= NULL;
589                 new->gpd= NULL;
590                 new->sms= NULL;
591                 new->smooth_timer= NULL;
592                 
593                 return new;
594         }
595         return NULL;
596 }
597
598 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
599 {
600         wmWindow *win= wmn->wm->winactive;
601         ScrArea *sa;
602         unsigned int lay_used= 0;
603         Base *base;
604
605         if (!win) return;
606
607         base= scene->base.first;
608         while(base) {
609                 lay_used |= base->lay & ((1<<20)-1); /* ignore localview */
610
611                 if (lay_used == (1<<20)-1)
612                         break;
613
614                 base= base->next;
615         }
616
617         for(sa= win->screen->areabase.first; sa; sa= sa->next) {
618                 if(sa->spacetype == SPACE_VIEW3D) {
619                         if(BLI_findindex(&sa->regionbase, ar) != -1) {
620                                 View3D *v3d= sa->spacedata.first;
621                                 v3d->lay_used= lay_used;
622                                 break;
623                         }
624                 }
625         }
626 }
627
628 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
629 {
630         bScreen *sc;
631
632         /* context changes */
633         switch(wmn->category) {
634                 case NC_ANIMATION:
635                         switch(wmn->data) {
636                                 case ND_KEYFRAME_PROP:
637                                 case ND_NLA_ACTCHANGE:
638                                         ED_region_tag_redraw(ar);
639                                         break;
640                                 case ND_NLA:
641                                 case ND_KEYFRAME:
642                                         if (wmn->action == NA_EDITED)
643                                                 ED_region_tag_redraw(ar);
644                                         break;
645                                 case ND_ANIMCHAN:
646                                         if (wmn->action == NA_SELECTED)
647                                                 ED_region_tag_redraw(ar);
648                                         break;
649                         }
650                         break;
651                 case NC_SCENE:
652                         switch(wmn->data) {
653                                 case ND_LAYER_CONTENT:
654                                         view3d_recalc_used_layers(ar, wmn, wmn->reference);
655                                         ED_region_tag_redraw(ar);
656                                         break;
657                                 case ND_FRAME:
658                                 case ND_TRANSFORM:
659                                 case ND_OB_ACTIVE:
660                                 case ND_OB_SELECT:
661                                 case ND_OB_VISIBLE:
662                                 case ND_LAYER:
663                                 case ND_RENDER_OPTIONS:
664                                 case ND_MODE:
665                                         ED_region_tag_redraw(ar);
666                                         break;
667                                 case ND_WORLD:
668                                         /* handled by space_view3d_listener() for v3d access */
669                                         break;
670                         }
671                         if (wmn->action == NA_EDITED)
672                                 ED_region_tag_redraw(ar);
673                         break;
674                 case NC_OBJECT:
675                         switch(wmn->data) {
676                                 case ND_BONE_ACTIVE:
677                                 case ND_BONE_SELECT:
678                                 case ND_TRANSFORM:
679                                 case ND_POSE:
680                                 case ND_DRAW:
681                                 case ND_MODIFIER:
682                                 case ND_CONSTRAINT:
683                                 case ND_KEYS:
684                                 case ND_PARTICLE:
685                                         ED_region_tag_redraw(ar);
686                                         break;
687                         }
688                         switch(wmn->action) {
689                                 case NA_ADDED:
690                                         ED_region_tag_redraw(ar);
691                                         break;
692                         }
693                         break;
694                 case NC_GEOM:
695                         switch(wmn->data) {
696                                 case ND_DATA:
697                                 case ND_SELECT:
698                                         ED_region_tag_redraw(ar);
699                                         break;
700                         }
701                         switch(wmn->action) {
702                                 case NA_EDITED:
703                                         ED_region_tag_redraw(ar);
704                                         break;
705                         }
706                         break;
707                 case NC_GROUP:
708                         /* all group ops for now */
709                         ED_region_tag_redraw(ar);
710                         break;
711                 case NC_BRUSH:
712                         if(wmn->action == NA_EDITED)
713                                 ED_region_tag_redraw_overlay(ar);
714                         break;                  
715                 case NC_MATERIAL:
716                         switch(wmn->data) {
717                                 case ND_SHADING_DRAW:
718                                         ED_region_tag_redraw(ar);
719                                         break;
720                         }
721                         break;
722                 case NC_WORLD:
723                         switch(wmn->data) {
724                                 case ND_WORLD_DRAW:
725                                         /* handled by space_view3d_listener() for v3d access */
726                                         break;
727                                 case ND_WORLD_STARS:
728                                 {
729                                         RegionView3D *rv3d= ar->regiondata;
730                                         if(rv3d->persp == RV3D_CAMOB) {
731                                                 ED_region_tag_redraw(ar);
732                                         }
733                                 }
734                         }
735                         break;
736                 case NC_LAMP:
737                         switch(wmn->data) {
738                                 case ND_LIGHTING_DRAW:
739                                         ED_region_tag_redraw(ar);
740                                         break;
741                         }
742                         break;
743                 case NC_IMAGE:  
744                         /* this could be more fine grained checks if we had
745                          * more context than just the region */
746                         ED_region_tag_redraw(ar);
747                         break;
748                 case NC_TEXTURE:        
749                         /* same as above */
750                         ED_region_tag_redraw(ar);
751                         break;
752                 case NC_MOVIECLIP:
753                         if(wmn->data==ND_DISPLAY)
754                                 ED_region_tag_redraw(ar);
755                         break;
756                 case NC_SPACE:
757                         if(wmn->data == ND_SPACE_VIEW3D) {
758                                 if (wmn->subtype == NS_VIEW3D_GPU) {
759                                         RegionView3D *rv3d= ar->regiondata;
760                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
761                                 }
762                                 ED_region_tag_redraw(ar);
763                         }
764                         break;
765                 case NC_ID:
766                         if(wmn->action == NA_RENAME)
767                                 ED_region_tag_redraw(ar);
768                         break;
769                 case NC_SCREEN:
770                         switch(wmn->data) {
771                                 case ND_GPENCIL:
772                                 case ND_ANIMPLAY:
773                                 case ND_SKETCH:
774                                         ED_region_tag_redraw(ar);
775                                         break;
776                                 case ND_SCREENBROWSE:
777                                 case ND_SCREENDELETE:
778                                 case ND_SCREENSET:
779                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
780                                         /* updates used layers only for View3D in active screen */
781                                         sc= wmn->reference;
782                                         view3d_recalc_used_layers(ar, wmn, sc->scene);
783                                         ED_region_tag_redraw(ar);
784                                         break;
785                         }
786
787                         break;
788         }
789 }
790
791 /* concept is to retrieve cursor type context-less */
792 static void view3d_main_area_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
793 {
794         Scene *scene= win->screen->scene;
795
796         if(scene->obedit) {
797                 WM_cursor_set(win, CURSOR_EDIT);
798         }
799         else {
800                 WM_cursor_set(win, CURSOR_STD);
801         }
802 }
803
804 /* add handlers, stuff you only do once or on area/region changes */
805 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
806 {
807         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
808         
809         WM_event_add_keymap_handler(&ar->handlers, keymap);
810
811         ED_region_header_init(ar);
812 }
813
814 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
815 {
816         ED_region_header(C, ar);
817 }
818
819 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
820 {
821         /* context changes */
822         switch(wmn->category) {
823                 case NC_SCENE:
824                         switch(wmn->data) {
825                                 case ND_FRAME:
826                                 case ND_OB_ACTIVE:
827                                 case ND_OB_SELECT:
828                                 case ND_OB_VISIBLE:
829                                 case ND_MODE:
830                                 case ND_LAYER:
831                                 case ND_TOOLSETTINGS:
832                                 case ND_LAYER_CONTENT:
833                                         ED_region_tag_redraw(ar);
834                                         break;
835                         }
836                         break;
837                 case NC_SPACE:
838                         if(wmn->data == ND_SPACE_VIEW3D)
839                                 ED_region_tag_redraw(ar);
840                         break;
841         }
842 }
843
844 /* add handlers, stuff you only do once or on area/region changes */
845 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
846 {
847         wmKeyMap *keymap;
848
849         ED_region_panels_init(wm, ar);
850         
851         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
852         WM_event_add_keymap_handler(&ar->handlers, keymap);
853 }
854
855 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
856 {
857         ED_region_panels(C, ar, 1, NULL, -1);
858 }
859
860 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
861 {
862         /* context changes */
863         switch(wmn->category) {
864                 case NC_ANIMATION:
865                         switch(wmn->data) {
866                                 case ND_KEYFRAME_PROP:
867                                 case ND_NLA_ACTCHANGE:
868                                         ED_region_tag_redraw(ar);
869                                         break;
870                                 case ND_NLA:
871                                 case ND_KEYFRAME:
872                                         if (wmn->action == NA_EDITED)
873                                                 ED_region_tag_redraw(ar);
874                                         break;  
875                         }
876                         break;
877                 case NC_SCENE:
878                         switch(wmn->data) {
879                                 case ND_FRAME:
880                                 case ND_OB_ACTIVE:
881                                 case ND_OB_SELECT:
882                                 case ND_OB_VISIBLE:
883                                 case ND_MODE:
884                                 case ND_LAYER:
885                                 case ND_LAYER_CONTENT:
886                                 case ND_TOOLSETTINGS:
887                                         ED_region_tag_redraw(ar);
888                                         break;
889                         }
890                         switch(wmn->action) {
891                                 case NA_EDITED:
892                                         ED_region_tag_redraw(ar);
893                                         break;
894                         }
895                         break;
896                 case NC_OBJECT:
897                         switch(wmn->data) {
898                                 case ND_BONE_ACTIVE:
899                                 case ND_BONE_SELECT:
900                                 case ND_TRANSFORM:
901                                 case ND_POSE:
902                                 case ND_DRAW:
903                                 case ND_KEYS:
904                                 case ND_MODIFIER:
905                                         ED_region_tag_redraw(ar);
906                                         break;
907                         }
908                         break;
909                 case NC_GEOM:
910                         switch(wmn->data) {
911                                 case ND_DATA:
912                                 case ND_SELECT:
913                                         ED_region_tag_redraw(ar);
914                                         break;
915                         }
916                         if (wmn->action == NA_EDITED)
917                                 ED_region_tag_redraw(ar);
918                         break;
919                 case NC_TEXTURE:
920                         /* for brush textures */
921                         ED_region_tag_redraw(ar);
922                         break;
923                 case NC_BRUSH:
924                         if(wmn->action==NA_EDITED)
925                                 ED_region_tag_redraw(ar);
926                         break;
927                 case NC_SPACE:
928                         if(wmn->data == ND_SPACE_VIEW3D)
929                                 ED_region_tag_redraw(ar);
930                         break;
931                 case NC_ID:
932                         if(wmn->action == NA_RENAME)
933                                 ED_region_tag_redraw(ar);
934                         break;
935                 case NC_SCREEN: 
936                         if(wmn->data == ND_GPENCIL)
937                                 ED_region_tag_redraw(ar);
938                         break;
939         }
940 }
941
942 /* add handlers, stuff you only do once or on area/region changes */
943 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
944 {
945         wmKeyMap *keymap;
946         
947         ED_region_panels_init(wm, ar);
948
949         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
950         WM_event_add_keymap_handler(&ar->handlers, keymap);
951 }
952
953 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
954 {
955         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
956 }
957
958 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
959 {
960         /* context changes */
961         switch(wmn->category) {
962                 case NC_WM:
963                         if(wmn->data == ND_HISTORY)
964                                 ED_region_tag_redraw(ar);
965                         break;
966                 case NC_SCENE:
967                         if(wmn->data == ND_MODE)
968                                 ED_region_tag_redraw(ar);
969                         break;
970                 case NC_SPACE:
971                         if(wmn->data == ND_SPACE_VIEW3D)
972                                 ED_region_tag_redraw(ar);
973                         break;
974         }
975 }
976
977 /*area (not region) level listener*/
978 static void space_view3d_listener(struct ScrArea *sa, struct wmNotifier *wmn)
979 {
980         View3D *v3d = sa->spacedata.first;
981
982         /* context changes */
983         switch(wmn->category) {
984                 case NC_SCENE:
985                         switch(wmn->data) {
986                                 case ND_WORLD:
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_WORLD:
993                         switch(wmn->data) {
994                                 case ND_WORLD_DRAW:
995                                         if(v3d->flag2 & V3D_RENDER_OVERRIDE)
996                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
997                                         break;
998                         }
999                         break;
1000                 case NC_MATERIAL:
1001                         switch(wmn->data) {
1002                                 case ND_NODES:
1003                                         if(v3d->drawtype == OB_TEXTURE)
1004                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1005                                         break;
1006                         }
1007                         break;
1008         }
1009
1010 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
1011         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
1012                 View3D *v3d = area->spacedata.first;
1013                 BGpic *bgpic = v3d->bgpicbase.first;
1014
1015                 for (; bgpic; bgpic = bgpic->next) {
1016                         if (bgpic->ima) {
1017                                 Scene *scene = wmn->reference;
1018                                 BKE_image_user_calc_frame(&bgpic->iuser, scene->r.cfra, 0);
1019                         }
1020                 }
1021         }
1022 #endif
1023 }
1024
1025 const char *view3d_context_dir[] = {
1026         "selected_objects", "selected_bases", "selected_editable_objects",
1027         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
1028         "active_base", "active_object", NULL};
1029
1030 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1031 {
1032         View3D *v3d= CTX_wm_view3d(C);
1033         Scene *scene= CTX_data_scene(C);
1034         Base *base;
1035         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 */
1036
1037         if(CTX_data_dir(member)) {
1038                 CTX_data_dir_set(result, view3d_context_dir);
1039         }
1040         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
1041                 int selected_objects= CTX_data_equals(member, "selected_objects");
1042
1043                 for(base=scene->base.first; base; base=base->next) {
1044                         if((base->flag & SELECT) && (base->lay & lay)) {
1045                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1046                                         if(selected_objects)
1047                                                 CTX_data_id_list_add(result, &base->object->id);
1048                                         else
1049                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1050                                 }
1051                         }
1052                 }
1053                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1054                 return 1;
1055         }
1056         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
1057                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
1058
1059                 for(base=scene->base.first; base; base=base->next) {
1060                         if((base->flag & SELECT) && (base->lay & lay)) {
1061                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1062                                         if(0==object_is_libdata(base->object)) {
1063                                                 if(selected_editable_objects)
1064                                                         CTX_data_id_list_add(result, &base->object->id);
1065                                                 else
1066                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1067                                         }
1068                                 }
1069                         }
1070                 }
1071                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1072                 return 1;
1073         }
1074         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
1075                 int visible_objects= CTX_data_equals(member, "visible_objects");
1076
1077                 for(base=scene->base.first; base; base=base->next) {
1078                         if(base->lay & lay) {
1079                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1080                                         if(visible_objects)
1081                                                 CTX_data_id_list_add(result, &base->object->id);
1082                                         else
1083                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1084                                 }
1085                         }
1086                 }
1087                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1088                 return 1;
1089         }
1090         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
1091                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
1092
1093                 for(base=scene->base.first; base; base=base->next) {
1094                         if(base->lay & lay) {
1095                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
1096                                         if(selectable_objects)
1097                                                 CTX_data_id_list_add(result, &base->object->id);
1098                                         else
1099                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1100                                 }
1101                         }
1102                 }
1103                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1104                 return 1;
1105         }
1106         else if(CTX_data_equals(member, "active_base")) {
1107                 if(scene->basact && (scene->basact->lay & lay))
1108                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1109                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
1110                 
1111                 return 1;
1112         }
1113         else if(CTX_data_equals(member, "active_object")) {
1114                 if(scene->basact && (scene->basact->lay & lay))
1115                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1116                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
1117                 
1118                 return 1;
1119         }
1120         else {
1121                 return 0; /* not found */
1122         }
1123
1124         return -1; /* found but not available */
1125 }
1126
1127
1128 /* only called once, from space/spacetypes.c */
1129 void ED_spacetype_view3d(void)
1130 {
1131         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1132         ARegionType *art;
1133         
1134         st->spaceid= SPACE_VIEW3D;
1135         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1136         
1137         st->new= view3d_new;
1138         st->free= view3d_free;
1139         st->init= view3d_init;
1140         st->listener = space_view3d_listener;
1141         st->duplicate= view3d_duplicate;
1142         st->operatortypes= view3d_operatortypes;
1143         st->keymap= view3d_keymap;
1144         st->dropboxes= view3d_dropboxes;
1145         st->context= view3d_context;
1146         
1147         /* regions: main window */
1148         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1149         art->regionid = RGN_TYPE_WINDOW;
1150         art->keymapflag= ED_KEYMAP_GPENCIL;
1151         art->draw= view3d_main_area_draw;
1152         art->init= view3d_main_area_init;
1153         art->free= view3d_main_area_free;
1154         art->duplicate= view3d_main_area_duplicate;
1155         art->listener= view3d_main_area_listener;
1156         art->cursor= view3d_main_area_cursor;
1157         art->lock= 1;   /* can become flag, see BKE_spacedata_draw_locks */
1158         BLI_addhead(&st->regiontypes, art);
1159         
1160         /* regions: listview/buttons */
1161         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1162         art->regionid = RGN_TYPE_UI;
1163         art->prefsizex= 180; // XXX
1164         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1165         art->listener= view3d_buttons_area_listener;
1166         art->init= view3d_buttons_area_init;
1167         art->draw= view3d_buttons_area_draw;
1168         BLI_addhead(&st->regiontypes, art);
1169
1170         view3d_buttons_register(art);
1171
1172         /* regions: tool(bar) */
1173         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1174         art->regionid = RGN_TYPE_TOOLS;
1175         art->prefsizex= 160; // XXX
1176         art->prefsizey= 50; // XXX
1177         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1178         art->listener= view3d_buttons_area_listener;
1179         art->init= view3d_tools_area_init;
1180         art->draw= view3d_tools_area_draw;
1181         BLI_addhead(&st->regiontypes, art);
1182         
1183 #if 0
1184         /* unfinished still */
1185         view3d_toolshelf_register(art);
1186 #endif
1187
1188         /* regions: tool properties */
1189         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d tool properties region");
1190         art->regionid = RGN_TYPE_TOOL_PROPS;
1191         art->prefsizex= 0;
1192         art->prefsizey= 120;
1193         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1194         art->listener= view3d_props_area_listener;
1195         art->init= view3d_tools_area_init;
1196         art->draw= view3d_tools_area_draw;
1197         BLI_addhead(&st->regiontypes, art);
1198         
1199         view3d_tool_props_register(art);
1200         
1201         
1202         /* regions: header */
1203         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1204         art->regionid = RGN_TYPE_HEADER;
1205         art->prefsizey= HEADERY;
1206         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1207         art->listener= view3d_header_area_listener;
1208         art->init= view3d_header_area_init;
1209         art->draw= view3d_header_area_draw;
1210         BLI_addhead(&st->regiontypes, art);
1211         
1212         BKE_spacetype_register(st);
1213 }
1214