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