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