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