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