Bugfix #20574: New 3D View regions were all had their 'type' set to 'RGN_TYPE_UI...
[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_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_TOOLS;
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_TOOL_PROPS;
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                         else if(wmn->data==ND_ANIMPLAY)
616                                 ED_region_tag_redraw(ar);
617                         break;
618         }
619 }
620
621 /* concept is to retrieve cursor type context-less */
622 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
623 {
624         Scene *scene= win->screen->scene;
625
626         if(scene->obedit) {
627                 WM_cursor_set(win, CURSOR_EDIT);
628         }
629         else {
630                 WM_cursor_set(win, CURSOR_STD);
631         }
632 }
633
634 /* add handlers, stuff you only do once or on area/region changes */
635 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
636 {
637         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
638         
639         WM_event_add_keymap_handler(&ar->handlers, keymap);
640
641         ED_region_header_init(ar);
642 }
643
644 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
645 {
646         ED_region_header(C, ar);
647 }
648
649 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
650 {
651         /* context changes */
652         switch(wmn->category) {
653                 case NC_SCENE:
654                         switch(wmn->data) {
655                                 case ND_FRAME:
656                                 case ND_OB_ACTIVE:
657                                 case ND_OB_SELECT:
658                                 case ND_MODE:
659                                 case ND_LAYER:
660                                 case ND_TOOLSETTINGS:
661                                         ED_region_tag_redraw(ar);
662                                         break;
663                         }
664                         break;
665                 case NC_SPACE:
666                         if(wmn->data == ND_SPACE_VIEW3D)
667                                 ED_region_tag_redraw(ar);
668                         break;
669         }
670 }
671
672 /* add handlers, stuff you only do once or on area/region changes */
673 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
674 {
675         wmKeyMap *keymap;
676
677         ED_region_panels_init(wm, ar);
678         
679         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
680         WM_event_add_keymap_handler(&ar->handlers, keymap);
681 }
682
683 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
684 {
685         ED_region_panels(C, ar, 1, NULL, -1);
686 }
687
688 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
689 {
690         /* context changes */
691         switch(wmn->category) {
692                 case NC_ANIMATION:
693                         switch(wmn->data) {
694                                 case ND_KEYFRAME_EDIT:
695                                 case ND_KEYFRAME_PROP:
696                                 case ND_NLA_EDIT:
697                                 case ND_NLA_ACTCHANGE:
698                                         ED_region_tag_redraw(ar);
699                                         break;
700                         }
701                         break;
702                 case NC_SCENE:
703                         switch(wmn->data) {
704                                 case ND_FRAME:
705                                 case ND_OB_ACTIVE:
706                                 case ND_OB_SELECT:
707                                 case ND_MODE:
708                                 case ND_LAYER:
709                                         ED_region_tag_redraw(ar);
710                                         break;
711                         }
712                         switch(wmn->action) {
713                                 case NA_EDITED:
714                                         ED_region_tag_redraw(ar);
715                                         break;
716                         }
717                         break;
718                 case NC_OBJECT:
719                         switch(wmn->data) {
720                                 case ND_BONE_ACTIVE:
721                                 case ND_BONE_SELECT:
722                                 case ND_TRANSFORM:
723                                 case ND_POSE:
724                                 case ND_DRAW:
725                                 case ND_KEYS:
726                                         ED_region_tag_redraw(ar);
727                                         break;
728                         }
729                         break;
730                 case NC_GEOM:
731                         switch(wmn->data) {
732                                 case ND_DATA:
733                                 case ND_SELECT:
734                                         ED_region_tag_redraw(ar);
735                                         break;
736                         }
737                         break;
738                 case NC_TEXTURE:
739                         /* for brush textures */
740                         ED_region_tag_redraw(ar);
741                         break;
742                 case NC_BRUSH:
743                         if(wmn->action==NA_EDITED)
744                                 ED_region_tag_redraw(ar);
745                         break;
746                 case NC_SPACE:
747                         if(wmn->data == ND_SPACE_VIEW3D)
748                                 ED_region_tag_redraw(ar);
749                         break;
750                 case NC_ID:
751                         if(wmn->action == NA_RENAME)
752                                 ED_region_tag_redraw(ar);
753                         break;
754                 case NC_SCREEN: 
755                         if(wmn->data == ND_GPENCIL)
756                                 ED_region_tag_redraw(ar);
757                         break;
758         }
759 }
760
761 /* add handlers, stuff you only do once or on area/region changes */
762 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
763 {
764         wmKeyMap *keymap;
765         
766         ED_region_panels_init(wm, ar);
767
768         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
769         WM_event_add_keymap_handler(&ar->handlers, keymap);
770 }
771
772 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
773 {
774         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
775 }
776
777 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
778 {
779         View3D *v3d= CTX_wm_view3d(C);
780         Scene *scene= CTX_data_scene(C);
781         Base *base;
782         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
783
784         if(CTX_data_dir(member)) {
785                 static const char *dir[] = {
786                         "selected_objects", "selected_bases", "selected_editable_objects",
787                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
788                         "active_base", "active_object", NULL};
789
790                 CTX_data_dir_set(result, dir);
791         }
792         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
793                 int selected_objects= CTX_data_equals(member, "selected_objects");
794
795                 for(base=scene->base.first; base; base=base->next) {
796                         if((base->flag & SELECT) && (base->lay & lay)) {
797                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
798                                         if(selected_objects)
799                                                 CTX_data_id_list_add(result, &base->object->id);
800                                         else
801                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
802                                 }
803                         }
804                 }
805
806                 return 1;
807         }
808         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
809                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
810
811                 for(base=scene->base.first; base; base=base->next) {
812                         if((base->flag & SELECT) && (base->lay & lay)) {
813                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
814                                         if(0==object_is_libdata(base->object)) {
815                                                 if(selected_editable_objects)
816                                                         CTX_data_id_list_add(result, &base->object->id);
817                                                 else
818                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
819                                         }
820                                 }
821                         }
822                 }
823                 
824                 return 1;
825         }
826         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
827                 int visible_objects= CTX_data_equals(member, "visible_objects");
828
829                 for(base=scene->base.first; base; base=base->next) {
830                         if(base->lay & lay) {
831                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
832                                         if(visible_objects)
833                                                 CTX_data_id_list_add(result, &base->object->id);
834                                         else
835                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
836                                 }
837                         }
838                 }
839                 
840                 return 1;
841         }
842         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
843                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
844
845                 for(base=scene->base.first; base; base=base->next) {
846                         if(base->lay & lay) {
847                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
848                                         if(selectable_objects)
849                                                 CTX_data_id_list_add(result, &base->object->id);
850                                         else
851                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
852                                 }
853                         }
854                 }
855                 
856                 return 1;
857         }
858         else if(CTX_data_equals(member, "active_base")) {
859                 if(scene->basact && (scene->basact->lay & lay))
860                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
861                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
862                 
863                 return 1;
864         }
865         else if(CTX_data_equals(member, "active_object")) {
866                 if(scene->basact && (scene->basact->lay & lay))
867                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
868                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
869                 
870                 return 1;
871         }
872         else {
873                 return 0; /* not found */
874         }
875
876         return -1; /* found but not available */
877 }
878
879 /*area (not region) level listener*/
880 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
881 void space_view3d_listener(struct ScrArea *area, struct wmNotifier *wmn)
882 {
883         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
884                 View3D *v3d = area->spacedata.first;
885                 BGpic *bgpic = v3d->bgpicbase.first;
886
887                 for (; bgpic; bgpic = bgpic->next) {
888                         if (bgpic->ima) {
889                                 Scene *scene = wmn->reference;
890                                 BKE_image_user_calc_frame(&bgpic->iuser, scene->r.cfra, 0);
891                         }
892                 }
893         }
894 }
895 #endif
896
897 /* only called once, from space/spacetypes.c */
898 void ED_spacetype_view3d(void)
899 {
900         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
901         ARegionType *art;
902         
903         st->spaceid= SPACE_VIEW3D;
904         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
905         
906         st->new= view3d_new;
907         st->free= view3d_free;
908         st->init= view3d_init;
909 //      st->listener = space_view3d_listener;
910         st->duplicate= view3d_duplicate;
911         st->operatortypes= view3d_operatortypes;
912         st->keymap= view3d_keymap;
913         st->dropboxes= view3d_dropboxes;
914         st->context= view3d_context;
915         
916         /* regions: main window */
917         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
918         art->regionid = RGN_TYPE_WINDOW;
919         art->keymapflag= ED_KEYMAP_GPENCIL;
920         art->draw= view3d_main_area_draw;
921         art->init= view3d_main_area_init;
922         art->free= view3d_main_area_free;
923         art->duplicate= view3d_main_area_duplicate;
924         art->listener= view3d_main_area_listener;
925         art->cursor= view3d_main_area_cursor;
926         BLI_addhead(&st->regiontypes, art);
927         
928         /* regions: listview/buttons */
929         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
930         art->regionid = RGN_TYPE_UI;
931         art->prefsizex= 180; // XXX
932         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
933         art->listener= view3d_buttons_area_listener;
934         art->init= view3d_buttons_area_init;
935         art->draw= view3d_buttons_area_draw;
936         BLI_addhead(&st->regiontypes, art);
937
938         view3d_buttons_register(art);
939
940         /* regions: tool(bar) */
941         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
942         art->regionid = RGN_TYPE_TOOLS;
943         art->prefsizex= 160; // XXX
944         art->prefsizey= 50; // XXX
945         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
946         art->listener= view3d_buttons_area_listener;
947         art->init= view3d_tools_area_init;
948         art->draw= view3d_tools_area_draw;
949         BLI_addhead(&st->regiontypes, art);
950         
951         view3d_toolshelf_register(art);
952
953         /* regions: tool properties */
954         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
955         art->regionid = RGN_TYPE_TOOL_PROPS;
956         art->prefsizex= 0;
957         art->prefsizey= 120;
958         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
959         art->listener= view3d_buttons_area_listener;
960         art->init= view3d_tools_area_init;
961         art->draw= view3d_tools_area_draw;
962         BLI_addhead(&st->regiontypes, art);
963         
964         view3d_tool_props_register(art);
965         
966         
967         /* regions: header */
968         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
969         art->regionid = RGN_TYPE_HEADER;
970         art->prefsizey= HEADERY;
971         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
972         art->listener= view3d_header_area_listener;
973         art->init= view3d_header_area_init;
974         art->draw= view3d_header_area_draw;
975         BLI_addhead(&st->regiontypes, art);
976         
977         BKE_spacetype_register(st);
978 }
979