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