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