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