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