Drag and drop 2.5 integration! Finally, slashdot regulars can use
[blender.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         RNA_string_set(drop->ptr, "name", id->name+2);
432 }
433
434 /* region dropbox definition */
435 static void view3d_dropboxes(void)
436 {
437         ListBase *lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
438         
439         WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_id_drop_copy);
440         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
441         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_drop_poll, view3d_id_drop_copy);
442 }
443
444
445
446 /* type callback, not region itself */
447 static void view3d_main_area_free(ARegion *ar)
448 {
449         RegionView3D *rv3d= ar->regiondata;
450         
451         if(rv3d) {
452                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
453                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
454
455                 // XXX  retopo_free_view_data(rv3d);
456                 if(rv3d->ri) { 
457                         // XXX          BIF_view3d_previewrender_free(rv3d);
458                 }
459                 
460                 if(rv3d->depths) {
461                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
462                         MEM_freeN(rv3d->depths);
463                 }
464                 MEM_freeN(rv3d);
465                 ar->regiondata= NULL;
466         }
467 }
468
469 /* copy regiondata */
470 static void *view3d_main_area_duplicate(void *poin)
471 {
472         if(poin) {
473                 RegionView3D *rv3d= poin, *new;
474         
475                 new= MEM_dupallocN(rv3d);
476                 if(rv3d->localvd) 
477                         new->localvd= MEM_dupallocN(rv3d->localvd);
478                 if(rv3d->clipbb) 
479                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
480                 
481                 new->depths= NULL;
482                 new->retopo_view_data= NULL;
483                 new->ri= NULL;
484                 new->gpd= NULL;
485                 new->sms= NULL;
486                 new->smooth_timer= NULL;
487                 
488                 return new;
489         }
490         return NULL;
491 }
492
493 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
494 {
495         /* context changes */
496         switch(wmn->category) {
497                 case NC_ANIMATION:
498                         switch(wmn->data) {
499                                 case ND_KEYFRAME_EDIT:
500                                 case ND_KEYFRAME_PROP:
501                                 case ND_NLA_EDIT:
502                                 case ND_NLA_ACTCHANGE:
503                                 case ND_ANIMCHAN_SELECT:
504                                         ED_region_tag_redraw(ar);
505                                         break;
506                         }
507                         break;
508                 case NC_SCENE:
509                         switch(wmn->data) {
510                                 case ND_FRAME:
511                                 case ND_TRANSFORM:
512                                 case ND_OB_ACTIVE:
513                                 case ND_OB_SELECT:
514                                 case ND_LAYER:
515                                 case ND_RENDER_OPTIONS:
516                                 case ND_MODE:
517                                         ED_region_tag_redraw(ar);
518                                         break;
519                         }
520                         if (wmn->action == NA_EDITED)
521                                 ED_region_tag_redraw(ar);
522                         break;
523                 case NC_OBJECT:
524                         switch(wmn->data) {
525                                 case ND_BONE_ACTIVE:
526                                 case ND_BONE_SELECT:
527                                 case ND_TRANSFORM:
528                                 case ND_POSE:
529                                 case ND_DRAW:
530                                 case ND_MODIFIER:
531                                 case ND_CONSTRAINT:
532                                 case ND_KEYS:
533                                 case ND_PARTICLE_SELECT:
534                                 case ND_PARTICLE_DATA:
535                                         ED_region_tag_redraw(ar);
536                                         break;
537                         }
538                         break;
539                 case NC_GEOM:
540                         switch(wmn->data) {
541                                 case ND_DATA:
542                                 case ND_SELECT:
543                                         ED_region_tag_redraw(ar);
544                                         break;
545                         }
546                         switch(wmn->action) {
547                                 case NA_EDITED:
548                                         ED_region_tag_redraw(ar);
549                                         break;
550                         }
551                         break;
552                 case NC_GROUP:
553                         /* all group ops for now */
554                         ED_region_tag_redraw(ar);
555                         break;
556                 case NC_BRUSH:
557                         if(wmn->action == NA_EDITED)
558                                 ED_region_tag_redraw(ar);
559                         break;                  
560                 case NC_MATERIAL:
561                         switch(wmn->data) {
562                                 case ND_SHADING_DRAW:
563                                         ED_region_tag_redraw(ar);
564                                         break;
565                         }
566                         break;
567                 case NC_WORLD:
568                         switch(wmn->data) {
569                                 case ND_WORLD_DRAW:
570                                         ED_region_tag_redraw(ar);
571                                         break;
572                         }
573                         break;
574                 case NC_LAMP:
575                         switch(wmn->data) {
576                                 case ND_LIGHTING_DRAW:
577                                         ED_region_tag_redraw(ar);
578                                         break;
579                         }
580                         break;
581                 case NC_IMAGE:  
582                         /* this could be more fine grained checks if we had
583                          * more context than just the region */
584                         ED_region_tag_redraw(ar);
585                         break;
586                 case NC_SPACE:
587                         if(wmn->data == ND_SPACE_VIEW3D)
588                                 ED_region_tag_redraw(ar);
589                         break;
590                 case NC_ID:
591                         if(wmn->action == NA_RENAME)
592                                 ED_region_tag_redraw(ar);
593                         break;
594         }
595 }
596
597 /* concept is to retrieve cursor type context-less */
598 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
599 {
600         Scene *scene= win->screen->scene;
601
602         if(scene->obedit) {
603                 WM_cursor_set(win, CURSOR_EDIT);
604         }
605         else {
606                 WM_cursor_set(win, CURSOR_STD);
607         }
608 }
609
610 /* add handlers, stuff you only do once or on area/region changes */
611 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
612 {
613         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
614         
615         WM_event_add_keymap_handler(&ar->handlers, keymap);
616
617         ED_region_header_init(ar);
618 }
619
620 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
621 {
622         ED_region_header(C, ar);
623 }
624
625 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
626 {
627         /* context changes */
628         switch(wmn->category) {
629                 case NC_SCENE:
630                         switch(wmn->data) {
631                                 case ND_FRAME:
632                                 case ND_OB_ACTIVE:
633                                 case ND_OB_SELECT:
634                                 case ND_MODE:
635                                 case ND_LAYER:
636                                 case ND_TOOLSETTINGS:
637                                         ED_region_tag_redraw(ar);
638                                         break;
639                         }
640                         break;
641                 case NC_SPACE:
642                         if(wmn->data == ND_SPACE_VIEW3D)
643                                 ED_region_tag_redraw(ar);
644                         break;
645         }
646 }
647
648 /* add handlers, stuff you only do once or on area/region changes */
649 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
650 {
651         wmKeyMap *keymap;
652
653         ED_region_panels_init(wm, ar);
654         
655         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
656         WM_event_add_keymap_handler(&ar->handlers, keymap);
657 }
658
659 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
660 {
661         ED_region_panels(C, ar, 1, NULL, -1);
662 }
663
664 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
665 {
666         /* context changes */
667         switch(wmn->category) {
668                 case NC_ANIMATION:
669                         switch(wmn->data) {
670                                 case ND_KEYFRAME_EDIT:
671                                 case ND_KEYFRAME_PROP:
672                                 case ND_NLA_EDIT:
673                                 case ND_NLA_ACTCHANGE:
674                                         ED_region_tag_redraw(ar);
675                                         break;
676                         }
677                         break;
678                 case NC_SCENE:
679                         switch(wmn->data) {
680                                 case ND_FRAME:
681                                 case ND_OB_ACTIVE:
682                                 case ND_OB_SELECT:
683                                 case ND_MODE:
684                                 case ND_LAYER:
685                                         ED_region_tag_redraw(ar);
686                                         break;
687                         }
688                         break;
689                 case NC_OBJECT:
690                         switch(wmn->data) {
691                                 case ND_BONE_ACTIVE:
692                                 case ND_BONE_SELECT:
693                                 case ND_TRANSFORM:
694                                 case ND_POSE:
695                                 case ND_DRAW:
696                                 case ND_KEYS:
697                                         ED_region_tag_redraw(ar);
698                                         break;
699                         }
700                         break;
701                 case NC_GEOM:
702                         switch(wmn->data) {
703                                 case ND_DATA:
704                                 case ND_SELECT:
705                                         ED_region_tag_redraw(ar);
706                                         break;
707                         }
708                         break;
709                 case NC_TEXTURE:
710                         /* for brush textures */
711                         ED_region_tag_redraw(ar);
712                         break;
713                 case NC_BRUSH:
714                         if(wmn->action==NA_EDITED)
715                                 ED_region_tag_redraw(ar);
716                         break;
717                 case NC_SPACE:
718                         if(wmn->data == ND_SPACE_VIEW3D)
719                                 ED_region_tag_redraw(ar);
720                         break;
721                 case NC_ID:
722                         if(wmn->action == NA_RENAME)
723                                 ED_region_tag_redraw(ar);
724                         break;
725         }
726 }
727
728 /* add handlers, stuff you only do once or on area/region changes */
729 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
730 {
731         wmKeyMap *keymap;
732         
733         ED_region_panels_init(wm, ar);
734
735         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
736         WM_event_add_keymap_handler(&ar->handlers, keymap);
737 }
738
739 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
740 {
741         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
742 }
743
744 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
745 {
746         View3D *v3d= CTX_wm_view3d(C);
747         Scene *scene= CTX_data_scene(C);
748         Base *base;
749         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
750
751         if(CTX_data_dir(member)) {
752                 static const char *dir[] = {
753                         "selected_objects", "selected_bases", "selected_editable_objects",
754                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
755                         "active_base", "active_object", NULL};
756
757                 CTX_data_dir_set(result, dir);
758         }
759         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
760                 int selected_objects= CTX_data_equals(member, "selected_objects");
761
762                 for(base=scene->base.first; base; base=base->next) {
763                         if((base->flag & SELECT) && (base->lay & lay)) {
764                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
765                                         if(selected_objects)
766                                                 CTX_data_id_list_add(result, &base->object->id);
767                                         else
768                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
769                                 }
770                         }
771                 }
772
773                 return 1;
774         }
775         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
776                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_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(0==object_is_libdata(base->object)) {
782                                                 if(selected_editable_objects)
783                                                         CTX_data_id_list_add(result, &base->object->id);
784                                                 else
785                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
786                                         }
787                                 }
788                         }
789                 }
790                 
791                 return 1;
792         }
793         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
794                 int visible_objects= CTX_data_equals(member, "visible_objects");
795
796                 for(base=scene->base.first; base; base=base->next) {
797                         if(base->lay & lay) {
798                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
799                                         if(visible_objects)
800                                                 CTX_data_id_list_add(result, &base->object->id);
801                                         else
802                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
803                                 }
804                         }
805                 }
806                 
807                 return 1;
808         }
809         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
810                 int selectable_objects= CTX_data_equals(member, "selectable_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 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
815                                         if(selectable_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, "active_base")) {
826                 if(scene->basact && (scene->basact->lay & lay))
827                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
828                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
829                 
830                 return 1;
831         }
832         else if(CTX_data_equals(member, "active_object")) {
833                 if(scene->basact && (scene->basact->lay & lay))
834                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
835                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
836                 
837                 return 1;
838         }
839         else {
840                 return 0; /* not found */
841         }
842
843         return -1; /* found but not available */
844 }
845
846 /*area (not region) level listener*/
847 #if 0 // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
848 void space_view3d_listener(struct ScrArea *area, struct wmNotifier *wmn)
849 {
850         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
851                 View3D *v3d = area->spacedata.first;
852                 BGpic *bgpic = v3d->bgpicbase.first;
853
854                 for (; bgpic; bgpic = bgpic->next) {
855                         if (bgpic->ima) {
856                                 Scene *scene = wmn->reference;
857                                 BKE_image_user_calc_imanr(&bgpic->iuser, scene->r.cfra, 0);
858                         }
859                 }
860         }
861 }
862 #endif
863
864 /* only called once, from space/spacetypes.c */
865 void ED_spacetype_view3d(void)
866 {
867         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
868         ARegionType *art;
869         
870         st->spaceid= SPACE_VIEW3D;
871         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
872         
873         st->new= view3d_new;
874         st->free= view3d_free;
875         st->init= view3d_init;
876 //      st->listener = space_view3d_listener;
877         st->duplicate= view3d_duplicate;
878         st->operatortypes= view3d_operatortypes;
879         st->keymap= view3d_keymap;
880         st->dropboxes= view3d_dropboxes;
881         st->context= view3d_context;
882         
883         /* regions: main window */
884         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
885         art->regionid = RGN_TYPE_WINDOW;
886         art->keymapflag= ED_KEYMAP_GPENCIL;
887         art->draw= view3d_main_area_draw;
888         art->init= view3d_main_area_init;
889         art->free= view3d_main_area_free;
890         art->duplicate= view3d_main_area_duplicate;
891         art->listener= view3d_main_area_listener;
892         art->cursor= view3d_main_area_cursor;
893         BLI_addhead(&st->regiontypes, art);
894         
895         /* regions: listview/buttons */
896         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
897         art->regionid = RGN_TYPE_UI;
898         art->prefsizex= 180; // XXX
899         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
900         art->listener= view3d_buttons_area_listener;
901         art->init= view3d_buttons_area_init;
902         art->draw= view3d_buttons_area_draw;
903         BLI_addhead(&st->regiontypes, art);
904
905         view3d_buttons_register(art);
906
907         /* regions: tool(bar) */
908         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
909         art->regionid = RGN_TYPE_TOOLS;
910         art->prefsizex= 160; // XXX
911         art->prefsizey= 50; // XXX
912         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
913         art->listener= view3d_buttons_area_listener;
914         art->init= view3d_tools_area_init;
915         art->draw= view3d_tools_area_draw;
916         BLI_addhead(&st->regiontypes, art);
917         
918         view3d_toolshelf_register(art);
919
920         /* regions: tool properties */
921         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
922         art->regionid = RGN_TYPE_TOOL_PROPS;
923         art->prefsizex= 0;
924         art->prefsizey= 120;
925         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
926         art->listener= view3d_buttons_area_listener;
927         art->init= view3d_tools_area_init;
928         art->draw= view3d_tools_area_draw;
929         BLI_addhead(&st->regiontypes, art);
930         
931         view3d_tool_props_register(art);
932         
933         
934         /* regions: header */
935         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
936         art->regionid = RGN_TYPE_HEADER;
937         art->prefsizey= HEADERY;
938         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
939         art->listener= view3d_header_area_listener;
940         art->init= view3d_header_area_init;
941         art->draw= view3d_header_area_draw;
942         BLI_addhead(&st->regiontypes, art);
943         
944         BKE_spacetype_register(st);
945 }
946