view3d bg image now updates on frame change, and the panel for it properly draws...
[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         if(vd->bgpic) {
271                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
272                 MEM_freeN(vd->bgpic);
273         }
274         
275         if(vd->localvd) MEM_freeN(vd->localvd);
276         
277         if(vd->properties_storage) MEM_freeN(vd->properties_storage);
278         
279 }
280
281
282 /* spacetype; init callback */
283 static void view3d_init(struct wmWindowManager *wm, ScrArea *sa)
284 {
285
286 }
287
288 static SpaceLink *view3d_duplicate(SpaceLink *sl)
289 {
290         View3D *v3do= (View3D *)sl;
291         View3D *v3dn= MEM_dupallocN(sl);
292         
293         /* clear or remove stuff from old */
294         
295 // XXX  BIF_view3d_previewrender_free(v3do);
296         
297         if(v3do->localvd) {
298                 v3do->localvd= NULL;
299                 v3do->properties_storage= NULL;
300                 v3do->lay= v3dn->localvd->lay;
301                 v3do->lay &= 0xFFFFFF;
302         }
303         
304         /* copy or clear inside new stuff */
305
306         if(v3dn->bgpic) {
307                 v3dn->bgpic= MEM_dupallocN(v3dn->bgpic);
308                 if(v3dn->bgpic->ima) v3dn->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         wmKeyMap *keymap;
319
320         /* object ops. */
321         
322         /* pose is not modal, operator poll checks for this */
323         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
324         WM_event_add_keymap_handler(&ar->handlers, keymap);
325         
326         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
327         WM_event_add_keymap_handler(&ar->handlers, keymap);
328
329         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
330         WM_event_add_keymap_handler(&ar->handlers, keymap);
331
332         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
333         WM_event_add_keymap_handler(&ar->handlers, keymap);
334
335         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
336         WM_event_add_keymap_handler(&ar->handlers, keymap);
337         
338         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
339         WM_event_add_keymap_handler(&ar->handlers, keymap);
340
341         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
342         WM_event_add_keymap_handler(&ar->handlers, keymap);
343         
344         keymap= WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
345         WM_event_add_keymap_handler(&ar->handlers, keymap);
346         
347         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
348         WM_event_add_keymap_handler(&ar->handlers, keymap);
349         
350         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
351         WM_event_add_keymap_handler(&ar->handlers, keymap);
352
353         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
354         WM_event_add_keymap_handler(&ar->handlers, keymap);
355
356         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
357         WM_event_add_keymap_handler(&ar->handlers, keymap);
358         
359         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
360         WM_event_add_keymap_handler(&ar->handlers, keymap);
361
362         /* armature sketching needs to take over mouse */
363         keymap= WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
364         WM_event_add_keymap_handler(&ar->handlers, keymap);
365
366         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
367         WM_event_add_keymap_handler(&ar->handlers, keymap);
368
369         /* editfont keymap swallows all... */
370         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
371         WM_event_add_keymap_handler(&ar->handlers, keymap);
372
373         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
374         WM_event_add_keymap_handler(&ar->handlers, keymap);
375
376         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
377         WM_event_add_keymap_handler(&ar->handlers, keymap);
378
379         /* own keymap, last so modes can override it */
380         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
381         WM_event_add_keymap_handler(&ar->handlers, keymap);
382
383         keymap= WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
384         WM_event_add_keymap_handler(&ar->handlers, keymap);
385 }
386
387 /* type callback, not region itself */
388 static void view3d_main_area_free(ARegion *ar)
389 {
390         RegionView3D *rv3d= ar->regiondata;
391         
392         if(rv3d) {
393                 if(rv3d->localvd) MEM_freeN(rv3d->localvd);
394                 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
395
396                 // XXX  retopo_free_view_data(rv3d);
397                 if(rv3d->ri) { 
398                         // XXX          BIF_view3d_previewrender_free(rv3d);
399                 }
400                 
401                 if(rv3d->depths) {
402                         if(rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
403                         MEM_freeN(rv3d->depths);
404                 }
405                 MEM_freeN(rv3d);
406                 ar->regiondata= NULL;
407         }
408 }
409
410 /* copy regiondata */
411 static void *view3d_main_area_duplicate(void *poin)
412 {
413         if(poin) {
414                 RegionView3D *rv3d= poin, *new;
415         
416                 new= MEM_dupallocN(rv3d);
417                 if(rv3d->localvd) 
418                         new->localvd= MEM_dupallocN(rv3d->localvd);
419                 if(rv3d->clipbb) 
420                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
421                 
422                 new->depths= NULL;
423                 new->retopo_view_data= NULL;
424                 new->ri= NULL;
425                 new->gpd= NULL;
426                 new->sms= NULL;
427                 new->smooth_timer= NULL;
428                 
429                 return new;
430         }
431         return NULL;
432 }
433
434 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
435 {
436         /* context changes */
437         switch(wmn->category) {
438                 case NC_ANIMATION:
439                         switch(wmn->data) {
440                                 case ND_KEYFRAME_EDIT:
441                                 case ND_KEYFRAME_PROP:
442                                 case ND_NLA_EDIT:
443                                 case ND_NLA_ACTCHANGE:
444                                 case ND_ANIMCHAN_SELECT:
445                                         ED_region_tag_redraw(ar);
446                                         break;
447                         }
448                         break;
449                 case NC_SCENE:
450                         switch(wmn->data) {
451                                 case ND_FRAME:
452                                 case ND_TRANSFORM:
453                                 case ND_OB_ACTIVE:
454                                 case ND_OB_SELECT:
455                                 case ND_LAYER:
456                                 case ND_RENDER_OPTIONS:
457                                 case ND_MODE:
458                                         ED_region_tag_redraw(ar);
459                                         break;
460                         }
461                         if (wmn->action == NA_EDITED)
462                                 ED_region_tag_redraw(ar);
463                         break;
464                 case NC_OBJECT:
465                         switch(wmn->data) {
466                                 case ND_BONE_ACTIVE:
467                                 case ND_BONE_SELECT:
468                                 case ND_TRANSFORM:
469                                 case ND_POSE:
470                                 case ND_DRAW:
471                                 case ND_MODIFIER:
472                                 case ND_CONSTRAINT:
473                                 case ND_KEYS:
474                                 case ND_PARTICLE_SELECT:
475                                 case ND_PARTICLE_DATA:
476                                         ED_region_tag_redraw(ar);
477                                         break;
478                         }
479                         break;
480                 case NC_GEOM:
481                         switch(wmn->data) {
482                                 case ND_DATA:
483                                 case ND_SELECT:
484                                         ED_region_tag_redraw(ar);
485                                         break;
486                         }
487                         switch(wmn->action) {
488                                 case NA_EDITED:
489                                         ED_region_tag_redraw(ar);
490                                         break;
491                         }
492                         break;
493                 case NC_GROUP:
494                         /* all group ops for now */
495                         ED_region_tag_redraw(ar);
496                         break;
497                 case NC_BRUSH:
498                         if(wmn->action == NA_EDITED)
499                                 ED_region_tag_redraw(ar);
500                         break;                  
501                 case NC_MATERIAL:
502                         switch(wmn->data) {
503                                 case ND_SHADING_DRAW:
504                                         ED_region_tag_redraw(ar);
505                                         break;
506                         }
507                         break;
508                 case NC_WORLD:
509                         switch(wmn->data) {
510                                 case ND_WORLD_DRAW:
511                                         ED_region_tag_redraw(ar);
512                                         break;
513                         }
514                         break;
515                 case NC_LAMP:
516                         switch(wmn->data) {
517                                 case ND_LIGHTING_DRAW:
518                                         ED_region_tag_redraw(ar);
519                                         break;
520                         }
521                         break;
522                 case NC_IMAGE:  
523                         /* this could be more fine grained checks if we had
524                          * more context than just the region */
525                         ED_region_tag_redraw(ar);
526                         break;
527                 case NC_SPACE:
528                         if(wmn->data == ND_SPACE_VIEW3D)
529                                 ED_region_tag_redraw(ar);
530                         break;
531                 case NC_ID:
532                         if(wmn->action == NA_RENAME)
533                                 ED_region_tag_redraw(ar);
534                         break;
535         }
536 }
537
538 /* concept is to retrieve cursor type context-less */
539 static void view3d_main_area_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
540 {
541         Scene *scene= win->screen->scene;
542
543         if(scene->obedit) {
544                 WM_cursor_set(win, CURSOR_EDIT);
545         }
546         else {
547                 WM_cursor_set(win, CURSOR_STD);
548         }
549 }
550
551 /* add handlers, stuff you only do once or on area/region changes */
552 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
553 {
554         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
555         
556         WM_event_add_keymap_handler(&ar->handlers, keymap);
557
558         ED_region_header_init(ar);
559 }
560
561 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
562 {
563         ED_region_header(C, ar);
564 }
565
566 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
567 {
568         /* context changes */
569         switch(wmn->category) {
570                 case NC_SCENE:
571                         switch(wmn->data) {
572                                 case ND_FRAME:
573                                 case ND_OB_ACTIVE:
574                                 case ND_OB_SELECT:
575                                 case ND_MODE:
576                                 case ND_LAYER:
577                                 case ND_TOOLSETTINGS:
578                                         ED_region_tag_redraw(ar);
579                                         break;
580                         }
581                         break;
582                 case NC_SPACE:
583                         if(wmn->data == ND_SPACE_VIEW3D)
584                                 ED_region_tag_redraw(ar);
585                         break;
586         }
587 }
588
589 /* add handlers, stuff you only do once or on area/region changes */
590 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
591 {
592         wmKeyMap *keymap;
593
594         ED_region_panels_init(wm, ar);
595         
596         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
597         WM_event_add_keymap_handler(&ar->handlers, keymap);
598 }
599
600 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
601 {
602         ED_region_panels(C, ar, 1, NULL, -1);
603 }
604
605 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
606 {
607         /* context changes */
608         switch(wmn->category) {
609                 case NC_ANIMATION:
610                         switch(wmn->data) {
611                                 case ND_KEYFRAME_EDIT:
612                                 case ND_KEYFRAME_PROP:
613                                 case ND_NLA_EDIT:
614                                 case ND_NLA_ACTCHANGE:
615                                         ED_region_tag_redraw(ar);
616                                         break;
617                         }
618                         break;
619                 case NC_SCENE:
620                         switch(wmn->data) {
621                                 case ND_FRAME:
622                                 case ND_OB_ACTIVE:
623                                 case ND_OB_SELECT:
624                                 case ND_MODE:
625                                 case ND_LAYER:
626                                         ED_region_tag_redraw(ar);
627                                         break;
628                         }
629                         break;
630                 case NC_OBJECT:
631                         switch(wmn->data) {
632                                 case ND_BONE_ACTIVE:
633                                 case ND_BONE_SELECT:
634                                 case ND_TRANSFORM:
635                                 case ND_POSE:
636                                 case ND_DRAW:
637                                 case ND_KEYS:
638                                         ED_region_tag_redraw(ar);
639                                         break;
640                         }
641                         break;
642                 case NC_GEOM:
643                         switch(wmn->data) {
644                                 case ND_DATA:
645                                 case ND_SELECT:
646                                         ED_region_tag_redraw(ar);
647                                         break;
648                         }
649                         break;
650                 case NC_TEXTURE:
651                         /* for brush textures */
652                         ED_region_tag_redraw(ar);
653                         break;
654                 case NC_BRUSH:
655                         if(wmn->action==NA_EDITED)
656                                 ED_region_tag_redraw(ar);
657                         break;
658                 case NC_SPACE:
659                         if(wmn->data == ND_SPACE_VIEW3D)
660                                 ED_region_tag_redraw(ar);
661                         break;
662                 case NC_ID:
663                         if(wmn->action == NA_RENAME)
664                                 ED_region_tag_redraw(ar);
665                         break;
666         }
667 }
668
669 /* add handlers, stuff you only do once or on area/region changes */
670 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
671 {
672         wmKeyMap *keymap;
673         
674         ED_region_panels_init(wm, ar);
675
676         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
677         WM_event_add_keymap_handler(&ar->handlers, keymap);
678 }
679
680 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
681 {
682         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
683 }
684
685 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
686 {
687         View3D *v3d= CTX_wm_view3d(C);
688         Scene *scene= CTX_data_scene(C);
689         Base *base;
690         int lay = v3d ? v3d->lay:scene->lay; /* fallback to the scene layer, allows duplicate and other oject operators to run outside the 3d view */
691
692         if(CTX_data_dir(member)) {
693                 static const char *dir[] = {
694                         "selected_objects", "selected_bases", "selected_editable_objects",
695                         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
696                         "active_base", "active_object", NULL};
697
698                 CTX_data_dir_set(result, dir);
699         }
700         else if(CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
701                 int selected_objects= CTX_data_equals(member, "selected_objects");
702
703                 for(base=scene->base.first; base; base=base->next) {
704                         if((base->flag & SELECT) && (base->lay & lay)) {
705                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
706                                         if(selected_objects)
707                                                 CTX_data_id_list_add(result, &base->object->id);
708                                         else
709                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
710                                 }
711                         }
712                 }
713
714                 return 1;
715         }
716         else if(CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
717                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
718
719                 for(base=scene->base.first; base; base=base->next) {
720                         if((base->flag & SELECT) && (base->lay & lay)) {
721                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
722                                         if(0==object_is_libdata(base->object)) {
723                                                 if(selected_editable_objects)
724                                                         CTX_data_id_list_add(result, &base->object->id);
725                                                 else
726                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
727                                         }
728                                 }
729                         }
730                 }
731                 
732                 return 1;
733         }
734         else if(CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
735                 int visible_objects= CTX_data_equals(member, "visible_objects");
736
737                 for(base=scene->base.first; base; base=base->next) {
738                         if(base->lay & lay) {
739                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
740                                         if(visible_objects)
741                                                 CTX_data_id_list_add(result, &base->object->id);
742                                         else
743                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
744                                 }
745                         }
746                 }
747                 
748                 return 1;
749         }
750         else if(CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
751                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
752
753                 for(base=scene->base.first; base; base=base->next) {
754                         if(base->lay & lay) {
755                                 if((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
756                                         if(selectable_objects)
757                                                 CTX_data_id_list_add(result, &base->object->id);
758                                         else
759                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
760                                 }
761                         }
762                 }
763                 
764                 return 1;
765         }
766         else if(CTX_data_equals(member, "active_base")) {
767                 if(scene->basact && (scene->basact->lay & lay))
768                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
769                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
770                 
771                 return 1;
772         }
773         else if(CTX_data_equals(member, "active_object")) {
774                 if(scene->basact && (scene->basact->lay & lay))
775                         if((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
776                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
777                 
778                 return 1;
779         }
780         else {
781                 return 0; /* not found */
782         }
783
784         return -1; /* found but not available */
785 }
786
787 /*area (not region) level listener*/
788 void space_view3d_listener(struct ScrArea *area, struct wmNotifier *wmn)
789 {
790         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
791                 View3D *v3d = area->spacedata.first;
792         
793                 if (v3d->bgpic && v3d->bgpic->ima) {
794                         Scene *scene = wmn->reference;
795
796                         BKE_image_user_calc_imanr(&v3d->bgpic->iuser, scene->r.cfra, 0);
797                 }
798         }
799 }
800
801 /* only called once, from space/spacetypes.c */
802 void ED_spacetype_view3d(void)
803 {
804         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
805         ARegionType *art;
806         
807         st->spaceid= SPACE_VIEW3D;
808         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
809         
810         st->new= view3d_new;
811         st->free= view3d_free;
812         st->init= view3d_init;
813         st->listener = space_view3d_listener;
814         st->duplicate= view3d_duplicate;
815         st->operatortypes= view3d_operatortypes;
816         st->keymap= view3d_keymap;
817         st->context= view3d_context;
818         
819         /* regions: main window */
820         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
821         art->regionid = RGN_TYPE_WINDOW;
822         art->keymapflag= ED_KEYMAP_GPENCIL;
823         art->draw= view3d_main_area_draw;
824         art->init= view3d_main_area_init;
825         art->free= view3d_main_area_free;
826         art->duplicate= view3d_main_area_duplicate;
827         art->listener= view3d_main_area_listener;
828         art->cursor= view3d_main_area_cursor;
829         BLI_addhead(&st->regiontypes, art);
830         
831         /* regions: listview/buttons */
832         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
833         art->regionid = RGN_TYPE_UI;
834         art->minsizex= 180; // XXX
835         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
836         art->listener= view3d_buttons_area_listener;
837         art->init= view3d_buttons_area_init;
838         art->draw= view3d_buttons_area_draw;
839         BLI_addhead(&st->regiontypes, art);
840
841         view3d_buttons_register(art);
842
843         /* regions: tool(bar) */
844         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
845         art->regionid = RGN_TYPE_TOOLS;
846         art->minsizex= 160; // XXX
847         art->minsizey= 50; // XXX
848         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
849         art->listener= view3d_buttons_area_listener;
850         art->init= view3d_tools_area_init;
851         art->draw= view3d_tools_area_draw;
852         BLI_addhead(&st->regiontypes, art);
853         
854         view3d_toolshelf_register(art);
855
856         /* regions: tool properties */
857         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
858         art->regionid = RGN_TYPE_TOOL_PROPS;
859         art->minsizex= 0;
860         art->minsizey= 120;
861         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
862         art->listener= view3d_buttons_area_listener;
863         art->init= view3d_tools_area_init;
864         art->draw= view3d_tools_area_draw;
865         BLI_addhead(&st->regiontypes, art);
866         
867         view3d_tool_props_register(art);
868         
869         
870         /* regions: header */
871         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d region");
872         art->regionid = RGN_TYPE_HEADER;
873         art->minsizey= HEADERY;
874         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
875         art->listener= view3d_header_area_listener;
876         art->init= view3d_header_area_init;
877         art->draw= view3d_header_area_draw;
878         BLI_addhead(&st->regiontypes, art);
879         
880         BKE_spacetype_register(st);
881 }
882