code cleanup: make clipping enable/disable into functions (was being done inline...
[blender.git] / source / blender / editors / space_view3d / space_view3d.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/space_view3d.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_rand.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_object.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51 #include "ED_object.h"
52
53 #include "BIF_gl.h"
54
55
56 #include "WM_api.h"
57 #include "WM_types.h"
58
59 #include "RE_engine.h"
60
61 #include "RNA_access.h"
62
63 #include "UI_resources.h"
64
65 #include "view3d_intern.h"      // own include
66
67 /* ******************** manage regions ********************* */
68
69 ARegion *view3d_has_buttons_region(ScrArea *sa)
70 {
71         ARegion *ar, *arnew;
72
73         ar= BKE_area_find_region_type(sa, RGN_TYPE_UI);
74         if (ar) return ar;
75         
76         /* add subdiv level; after header */
77         ar= BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
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= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
147                         if (ar) {
148                                 rv3d = ar->regiondata;
149                         }
150                 }
151         }
152         return rv3d;
153 }
154
155 /* ideally would return an rv3d but in some cases the region is needed too
156  * so return that, the caller can then access the ar->regiondata */
157 int ED_view3d_context_user_region(bContext *C, View3D **v3d_r, ARegion **ar_r)
158 {
159         ScrArea *sa= CTX_wm_area(C);
160
161         *v3d_r = NULL;
162         *ar_r = NULL;
163
164         if (sa && sa->spacetype==SPACE_VIEW3D) {
165                 ARegion *ar= CTX_wm_region(C);
166                 View3D *v3d = (View3D *)sa->spacedata.first;
167
168                 if (ar) {
169                         RegionView3D *rv3d= ar->regiondata;
170                         if (rv3d && rv3d->viewlock == 0) {
171                                 *v3d_r = v3d;
172                                 *ar_r = ar;
173                                 return 1;
174                         }
175                         else {
176                                 ARegion *ar_unlock_user= NULL;
177                                 ARegion *ar_unlock= NULL;
178                                 for (ar= sa->regionbase.first; ar; ar= ar->next) {
179                                         /* find the first unlocked rv3d */
180                                         if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
181                                                 rv3d= ar->regiondata;
182                                                 if (rv3d->viewlock == 0) {
183                                                         ar_unlock= ar;
184                                                         if (rv3d->persp==RV3D_PERSP || rv3d->persp==RV3D_CAMOB) {
185                                                                 ar_unlock_user= ar;
186                                                                 break;
187                                                         }
188                                                 } 
189                                         }
190                                 }
191
192                                 /* camera/perspective view get priority when the active region is locked */
193                                 if (ar_unlock_user) {
194                                         *v3d_r = v3d;
195                                         *ar_r = ar_unlock_user;
196                                         return 1;
197                                 }
198
199                                 if (ar_unlock) {
200                                         *v3d_r = v3d;
201                                         *ar_r = ar_unlock;
202                                         return 1;
203                                 }
204                         }
205                 }
206         }
207
208         return 0;
209 }
210
211 /* Most of the time this isn't needed since you could assume the view matrix was
212  * set while drawing, however when functions like mesh_foreachScreenVert are
213  * called by selection tools, we can't be sure this object was the last.
214  *
215  * for example, transparent objects are drawn after editmode and will cause
216  * the rv3d mat's to change and break selection.
217  *
218  * 'ED_view3d_init_mats_rv3d' should be called before
219  * view3d_project_short_clip and view3d_project_short_noclip in cases where
220  * these functions are not used during draw_object
221  */
222 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
223 {
224         /* local viewmat and persmat, to calculate projections */
225         mult_m4_m4m4(rv3d->viewmatob, rv3d->viewmat, ob->obmat);
226         mult_m4_m4m4(rv3d->persmatob, rv3d->persmat, ob->obmat);
227
228         /* initializes object space clipping, speeds up clip tests */
229         ED_view3d_clipping_local(rv3d, ob->obmat);
230 }
231
232 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d)
233 {
234         ED_view3d_init_mats_rv3d(ob, rv3d);
235
236         /* we have to multiply instead of loading viewmatob to make
237            it work with duplis using displists, otherwise it will
238            override the dupli-matrix */
239         glMultMatrixf(ob->obmat);
240 }
241
242 /* ******************** default callbacks for view3d space ***************** */
243
244 static SpaceLink *view3d_new(const bContext *C)
245 {
246         Scene *scene= CTX_data_scene(C);
247         ARegion *ar;
248         View3D *v3d;
249         RegionView3D *rv3d;
250         
251         v3d= MEM_callocN(sizeof(View3D), "initview3d");
252         v3d->spacetype= SPACE_VIEW3D;
253         v3d->blockscale= 0.7f;
254         v3d->lay= v3d->layact= 1;
255         if (scene) {
256                 v3d->lay= v3d->layact= scene->lay;
257                 v3d->camera= scene->camera;
258         }
259         v3d->scenelock= TRUE;
260         v3d->grid= 1.0f;
261         v3d->gridlines= 16;
262         v3d->gridsubdiv = 10;
263         v3d->drawtype= OB_SOLID;
264         
265         v3d->gridflag |= V3D_SHOW_X;
266         v3d->gridflag |= V3D_SHOW_Y;
267         v3d->gridflag |= V3D_SHOW_FLOOR;
268         v3d->gridflag &= ~V3D_SHOW_Z;
269         
270         v3d->flag |= V3D_SELECT_OUTLINE;
271         v3d->flag2 |= V3D_SHOW_RECONSTRUCTION;
272         
273         v3d->lens= 35.0f;
274         v3d->near= 0.01f;
275         v3d->far= 1000.0f;
276
277         v3d->twflag |= U.tw_flag & V3D_USE_MANIPULATOR;
278         v3d->twtype= V3D_MANIP_TRANSLATE;
279         v3d->around= V3D_CENTROID;
280         
281         v3d->bundle_size= 0.2f;
282         v3d->bundle_drawtype= OB_PLAINAXES;
283         
284         /* header */
285         ar= MEM_callocN(sizeof(ARegion), "header for view3d");
286         
287         BLI_addtail(&v3d->regionbase, ar);
288         ar->regiontype= RGN_TYPE_HEADER;
289         ar->alignment= RGN_ALIGN_BOTTOM;
290         
291         /* tool shelf */
292         ar= MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
293         
294         BLI_addtail(&v3d->regionbase, ar);
295         ar->regiontype= RGN_TYPE_TOOLS;
296         ar->alignment= RGN_ALIGN_LEFT;
297         ar->flag = RGN_FLAG_HIDDEN;
298         
299         /* tool properties */
300         ar= MEM_callocN(sizeof(ARegion), "tool properties for view3d");
301         
302         BLI_addtail(&v3d->regionbase, ar);
303         ar->regiontype= RGN_TYPE_TOOL_PROPS;
304         ar->alignment= RGN_ALIGN_BOTTOM|RGN_SPLIT_PREV;
305         ar->flag = RGN_FLAG_HIDDEN;
306         
307         /* buttons/list view */
308         ar= MEM_callocN(sizeof(ARegion), "buttons for view3d");
309         
310         BLI_addtail(&v3d->regionbase, ar);
311         ar->regiontype= RGN_TYPE_UI;
312         ar->alignment= RGN_ALIGN_RIGHT;
313         ar->flag = RGN_FLAG_HIDDEN;
314         
315         /* main area */
316         ar= MEM_callocN(sizeof(ARegion), "main area for view3d");
317         
318         BLI_addtail(&v3d->regionbase, ar);
319         ar->regiontype= RGN_TYPE_WINDOW;
320         
321         ar->regiondata= MEM_callocN(sizeof(RegionView3D), "region view3d");
322         rv3d= ar->regiondata;
323         rv3d->viewquat[0]= 1.0f;
324         rv3d->persp= RV3D_PERSP;
325         rv3d->view= RV3D_VIEW_PERSPORTHO;
326         rv3d->dist= 10.0;
327         
328         return (SpaceLink *)v3d;
329 }
330
331 /* not spacelink itself */
332 static void view3d_free(SpaceLink *sl)
333 {
334         View3D *vd= (View3D *) sl;
335
336         BGpic *bgpic;
337         for (bgpic= vd->bgpicbase.first; bgpic; bgpic= bgpic->next) {
338                 if (bgpic->ima) bgpic->ima->id.us--;
339         }
340         BLI_freelistN(&vd->bgpicbase);
341
342         if (vd->localvd) MEM_freeN(vd->localvd);
343         
344         if (vd->properties_storage) MEM_freeN(vd->properties_storage);
345 }
346
347
348 /* spacetype; init callback */
349 static void view3d_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
350 {
351
352 }
353
354 static SpaceLink *view3d_duplicate(SpaceLink *sl)
355 {
356         View3D *v3do= (View3D *)sl;
357         View3D *v3dn= MEM_dupallocN(sl);
358         BGpic *bgpic;
359         
360         /* clear or remove stuff from old */
361         
362 // XXX  BIF_view3d_previewrender_free(v3do);
363         
364         if (v3do->localvd) {
365                 v3do->localvd= NULL;
366                 v3do->properties_storage= NULL;
367                 v3do->lay= v3dn->localvd->lay;
368                 v3do->lay &= 0xFFFFFF;
369         }
370
371         if (v3dn->drawtype == OB_RENDER)
372                 v3dn->drawtype = OB_SOLID;
373         
374         /* copy or clear inside new stuff */
375
376         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
377         for (bgpic= v3dn->bgpicbase.first; bgpic; bgpic= bgpic->next)
378                 if (bgpic->ima)
379                         bgpic->ima->id.us++;
380
381         v3dn->properties_storage= NULL;
382         
383         return (SpaceLink *)v3dn;
384 }
385
386 /* add handlers, stuff you only do once or on area/region changes */
387 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
388 {
389         ListBase *lb;
390         wmKeyMap *keymap;
391
392         /* object ops. */
393         
394         /* important to be before Pose keymap since they can both be enabled at once */
395         keymap= WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
396         WM_event_add_keymap_handler(&ar->handlers, keymap);
397         
398         
399         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
400         WM_event_add_keymap_handler(&ar->handlers, keymap);
401
402         /* pose is not modal, operator poll checks for this */
403         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
404         WM_event_add_keymap_handler(&ar->handlers, keymap);
405         
406         keymap= WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
407         WM_event_add_keymap_handler(&ar->handlers, keymap);
408
409         keymap= WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
410         WM_event_add_keymap_handler(&ar->handlers, keymap);
411
412         keymap= WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
413         WM_event_add_keymap_handler(&ar->handlers, keymap);
414
415         keymap= WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
416         WM_event_add_keymap_handler(&ar->handlers, keymap);
417
418         keymap= WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
419         WM_event_add_keymap_handler(&ar->handlers, keymap);
420         
421         keymap= WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
422         WM_event_add_keymap_handler(&ar->handlers, keymap);
423         
424         keymap= WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
425         WM_event_add_keymap_handler(&ar->handlers, keymap);
426         
427         keymap= WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
428         WM_event_add_keymap_handler(&ar->handlers, keymap);
429
430         keymap= WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
431         WM_event_add_keymap_handler(&ar->handlers, keymap);
432
433         keymap= WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
434         WM_event_add_keymap_handler(&ar->handlers, keymap);
435         
436         keymap= WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
437         WM_event_add_keymap_handler(&ar->handlers, keymap);
438
439         /* armature sketching needs to take over mouse */
440         keymap= WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
441         WM_event_add_keymap_handler(&ar->handlers, keymap);
442
443         keymap= WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
444         WM_event_add_keymap_handler(&ar->handlers, keymap);
445
446         /* editfont keymap swallows all... */
447         keymap= WM_keymap_find(wm->defaultconf, "Font", 0, 0);
448         WM_event_add_keymap_handler(&ar->handlers, keymap);
449
450         keymap= WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
451         WM_event_add_keymap_handler(&ar->handlers, keymap);
452
453         keymap= WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
454         WM_event_add_keymap_handler(&ar->handlers, keymap);
455
456         /* own keymap, last so modes can override it */
457         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
458         WM_event_add_keymap_handler(&ar->handlers, keymap);
459
460         keymap= WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
461         WM_event_add_keymap_handler(&ar->handlers, keymap);
462         
463         /* add drop boxes */
464         lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
465         
466         WM_event_add_dropbox_handler(&ar->handlers, lb);
467         
468 }
469
470 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
471 {
472         if (drag->type==WM_DRAG_ID) {
473                 ID *id= (ID *)drag->poin;
474                 if ( GS(id->name)==ID_OB )
475                         return 1;
476         }
477         return 0;
478 }
479
480 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
481 {
482         if (drag->type==WM_DRAG_ID) {
483                 ID *id= (ID *)drag->poin;
484                 if ( GS(id->name)==ID_MA )
485                         return 1;
486         }
487         return 0;
488 }
489
490 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
491 {
492         if (drag->type==WM_DRAG_ID) {
493                 ID *id= (ID *)drag->poin;
494                 if ( GS(id->name)==ID_IM )
495                         return 1;
496         }
497         else if (drag->type==WM_DRAG_PATH) {
498                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))       /* rule might not work? */
499                         return 1;
500         }
501         return 0;
502 }
503
504
505 static int view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
506 {
507         if ( ED_view3d_give_base_under_cursor(C, event->mval) ) {
508                 return 0;
509         }
510         return view3d_ima_drop_poll(C, drag, event);
511 }
512
513 static int view3d_ima_ob_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
514 {
515         if ( ED_view3d_give_base_under_cursor(C, event->mval) ) {
516                 return view3d_ima_drop_poll(C, drag, event);
517         }
518         return 0;
519 }
520
521 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
522 {
523         ID *id= (ID *)drag->poin;
524         PointerRNA ptr;
525
526         /* need to put name in sub-operator in macro */
527         ptr= RNA_pointer_get(drop->ptr, "OBJECT_OT_add_named");
528         if (ptr.data)
529                 RNA_string_set(&ptr, "name", id->name+2);
530         else
531                 RNA_string_set(drop->ptr, "name", id->name+2);
532 }
533
534 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
535 {
536         ID *id= (ID *)drag->poin;
537         
538         RNA_string_set(drop->ptr, "name", id->name+2);
539 }
540
541 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
542 {
543         ID *id= (ID *)drag->poin;
544         
545         if (id)
546                 RNA_string_set(drop->ptr, "name", id->name+2);
547         if (drag->path[0])
548                 RNA_string_set(drop->ptr, "filepath", drag->path);
549 }
550
551
552 /* region dropbox definition */
553 static void view3d_dropboxes(void)
554 {
555         ListBase *lb= WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
556         
557         WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_ob_drop_copy);
558         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
559         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_ob_drop_poll, view3d_id_path_drop_copy);
560         WM_dropbox_add(lb, "VIEW3D_OT_background_image_add", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
561 }
562
563
564
565 /* type callback, not region itself */
566 static void view3d_main_area_free(ARegion *ar)
567 {
568         RegionView3D *rv3d= ar->regiondata;
569         
570         if (rv3d) {
571                 if (rv3d->localvd) MEM_freeN(rv3d->localvd);
572                 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
573
574                 if (rv3d->ri) {
575                         // XXX          BIF_view3d_previewrender_free(rv3d);
576                 }
577
578                 if (rv3d->render_engine)
579                         RE_engine_free(rv3d->render_engine);
580                 
581                 if (rv3d->depths) {
582                         if (rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
583                         MEM_freeN(rv3d->depths);
584                 }
585                 MEM_freeN(rv3d);
586                 ar->regiondata= NULL;
587         }
588 }
589
590 /* copy regiondata */
591 static void *view3d_main_area_duplicate(void *poin)
592 {
593         if (poin) {
594                 RegionView3D *rv3d= poin, *new;
595         
596                 new= MEM_dupallocN(rv3d);
597                 if (rv3d->localvd)
598                         new->localvd= MEM_dupallocN(rv3d->localvd);
599                 if (rv3d->clipbb)
600                         new->clipbb= MEM_dupallocN(rv3d->clipbb);
601                 
602                 new->depths= NULL;
603                 new->ri= NULL;
604                 new->render_engine= NULL;
605                 new->gpd= NULL;
606                 new->sms= NULL;
607                 new->smooth_timer= NULL;
608                 
609                 return new;
610         }
611         return NULL;
612 }
613
614 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
615 {
616         wmWindow *win= wmn->wm->winactive;
617         ScrArea *sa;
618         unsigned int lay_used= 0;
619         Base *base;
620
621         if (!win) return;
622
623         base= scene->base.first;
624         while (base) {
625                 lay_used |= base->lay & ((1<<20)-1); /* ignore localview */
626
627                 if (lay_used == (1<<20)-1)
628                         break;
629
630                 base= base->next;
631         }
632
633         for (sa= win->screen->areabase.first; sa; sa= sa->next) {
634                 if (sa->spacetype == SPACE_VIEW3D) {
635                         if (BLI_findindex(&sa->regionbase, ar) != -1) {
636                                 View3D *v3d= sa->spacedata.first;
637                                 v3d->lay_used= lay_used;
638                                 break;
639                         }
640                 }
641         }
642 }
643
644 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
645 {
646         bScreen *sc;
647
648         /* context changes */
649         switch(wmn->category) {
650                 case NC_ANIMATION:
651                         switch(wmn->data) {
652                                 case ND_KEYFRAME_PROP:
653                                 case ND_NLA_ACTCHANGE:
654                                         ED_region_tag_redraw(ar);
655                                         break;
656                                 case ND_NLA:
657                                 case ND_KEYFRAME:
658                                         if (wmn->action == NA_EDITED)
659                                                 ED_region_tag_redraw(ar);
660                                         break;
661                                 case ND_ANIMCHAN:
662                                         if (wmn->action == NA_SELECTED)
663                                                 ED_region_tag_redraw(ar);
664                                         break;
665                         }
666                         break;
667                 case NC_SCENE:
668                         switch(wmn->data) {
669                                 case ND_LAYER_CONTENT:
670                                         view3d_recalc_used_layers(ar, wmn, wmn->reference);
671                                         ED_region_tag_redraw(ar);
672                                         break;
673                                 case ND_FRAME:
674                                 case ND_TRANSFORM:
675                                 case ND_OB_ACTIVE:
676                                 case ND_OB_SELECT:
677                                 case ND_OB_VISIBLE:
678                                 case ND_LAYER:
679                                 case ND_RENDER_OPTIONS:
680                                 case ND_MODE:
681                                         ED_region_tag_redraw(ar);
682                                         break;
683                                 case ND_WORLD:
684                                         /* handled by space_view3d_listener() for v3d access */
685                                         break;
686                         }
687                         if (wmn->action == NA_EDITED)
688                                 ED_region_tag_redraw(ar);
689                         break;
690                 case NC_OBJECT:
691                         switch(wmn->data) {
692                                 case ND_BONE_ACTIVE:
693                                 case ND_BONE_SELECT:
694                                 case ND_TRANSFORM:
695                                 case ND_POSE:
696                                 case ND_DRAW:
697                                 case ND_MODIFIER:
698                                 case ND_CONSTRAINT:
699                                 case ND_KEYS:
700                                 case ND_PARTICLE:
701                                         ED_region_tag_redraw(ar);
702                                         break;
703                         }
704                         switch(wmn->action) {
705                                 case NA_ADDED:
706                                         ED_region_tag_redraw(ar);
707                                         break;
708                         }
709                         break;
710                 case NC_GEOM:
711                         switch(wmn->data) {
712                                 case ND_DATA:
713                                 case ND_SELECT:
714                                         ED_region_tag_redraw(ar);
715                                         break;
716                         }
717                         switch(wmn->action) {
718                                 case NA_EDITED:
719                                         ED_region_tag_redraw(ar);
720                                         break;
721                         }
722                         break;
723                 case NC_GROUP:
724                         /* all group ops for now */
725                         ED_region_tag_redraw(ar);
726                         break;
727                 case NC_BRUSH:
728                         if (wmn->action == NA_EDITED)
729                                 ED_region_tag_redraw_overlay(ar);
730                         break;                  
731                 case NC_MATERIAL:
732                         switch(wmn->data) {
733                                 case ND_SHADING_DRAW:
734                                         ED_region_tag_redraw(ar);
735                                         break;
736                         }
737                         break;
738                 case NC_WORLD:
739                         switch(wmn->data) {
740                                 case ND_WORLD_DRAW:
741                                         /* handled by space_view3d_listener() for v3d access */
742                                         break;
743                                 case ND_WORLD_STARS:
744                                 {
745                                         RegionView3D *rv3d= ar->regiondata;
746                                         if (rv3d->persp == RV3D_CAMOB) {
747                                                 ED_region_tag_redraw(ar);
748                                         }
749                                 }
750                         }
751                         break;
752                 case NC_LAMP:
753                         switch(wmn->data) {
754                                 case ND_LIGHTING_DRAW:
755                                         ED_region_tag_redraw(ar);
756                                         break;
757                         }
758                         break;
759                 case NC_IMAGE:  
760                         /* this could be more fine grained checks if we had
761                          * more context than just the region */
762                         ED_region_tag_redraw(ar);
763                         break;
764                 case NC_TEXTURE:        
765                         /* same as above */
766                         ED_region_tag_redraw(ar);
767                         break;
768                 case NC_MOVIECLIP:
769                         if (wmn->data==ND_DISPLAY)
770                                 ED_region_tag_redraw(ar);
771                         break;
772                 case NC_SPACE:
773                         if (wmn->data == ND_SPACE_VIEW3D) {
774                                 if (wmn->subtype == NS_VIEW3D_GPU) {
775                                         RegionView3D *rv3d= ar->regiondata;
776                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
777                                 }
778                                 ED_region_tag_redraw(ar);
779                         }
780                         break;
781                 case NC_ID:
782                         if (wmn->action == NA_RENAME)
783                                 ED_region_tag_redraw(ar);
784                         break;
785                 case NC_SCREEN:
786                         switch(wmn->data) {
787                                 case ND_GPENCIL:
788                                 case ND_ANIMPLAY:
789                                 case ND_SKETCH:
790                                         ED_region_tag_redraw(ar);
791                                         break;
792                                 case ND_SCREENBROWSE:
793                                 case ND_SCREENDELETE:
794                                 case ND_SCREENSET:
795                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
796                                         /* updates used layers only for View3D in active screen */
797                                         sc= wmn->reference;
798                                         view3d_recalc_used_layers(ar, wmn, sc->scene);
799                                         ED_region_tag_redraw(ar);
800                                         break;
801                         }
802
803                         break;
804         }
805 }
806
807 /* concept is to retrieve cursor type context-less */
808 static void view3d_main_area_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
809 {
810         Scene *scene= win->screen->scene;
811
812         if (scene->obedit) {
813                 WM_cursor_set(win, CURSOR_EDIT);
814         }
815         else {
816                 WM_cursor_set(win, CURSOR_STD);
817         }
818 }
819
820 /* add handlers, stuff you only do once or on area/region changes */
821 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
822 {
823         wmKeyMap *keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
824         
825         WM_event_add_keymap_handler(&ar->handlers, keymap);
826
827         ED_region_header_init(ar);
828 }
829
830 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
831 {
832         ED_region_header(C, ar);
833 }
834
835 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
836 {
837         /* context changes */
838         switch(wmn->category) {
839                 case NC_SCENE:
840                         switch(wmn->data) {
841                                 case ND_FRAME:
842                                 case ND_OB_ACTIVE:
843                                 case ND_OB_SELECT:
844                                 case ND_OB_VISIBLE:
845                                 case ND_MODE:
846                                 case ND_LAYER:
847                                 case ND_TOOLSETTINGS:
848                                 case ND_LAYER_CONTENT:
849                                         ED_region_tag_redraw(ar);
850                                         break;
851                         }
852                         break;
853                 case NC_SPACE:
854                         if (wmn->data == ND_SPACE_VIEW3D)
855                                 ED_region_tag_redraw(ar);
856                         break;
857         }
858 }
859
860 /* add handlers, stuff you only do once or on area/region changes */
861 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
862 {
863         wmKeyMap *keymap;
864
865         ED_region_panels_init(wm, ar);
866         
867         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
868         WM_event_add_keymap_handler(&ar->handlers, keymap);
869 }
870
871 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
872 {
873         ED_region_panels(C, ar, 1, NULL, -1);
874 }
875
876 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
877 {
878         /* context changes */
879         switch(wmn->category) {
880                 case NC_ANIMATION:
881                         switch(wmn->data) {
882                                 case ND_KEYFRAME_PROP:
883                                 case ND_NLA_ACTCHANGE:
884                                         ED_region_tag_redraw(ar);
885                                         break;
886                                 case ND_NLA:
887                                 case ND_KEYFRAME:
888                                         if (wmn->action == NA_EDITED)
889                                                 ED_region_tag_redraw(ar);
890                                         break;  
891                         }
892                         break;
893                 case NC_SCENE:
894                         switch(wmn->data) {
895                                 case ND_FRAME:
896                                 case ND_OB_ACTIVE:
897                                 case ND_OB_SELECT:
898                                 case ND_OB_VISIBLE:
899                                 case ND_MODE:
900                                 case ND_LAYER:
901                                 case ND_LAYER_CONTENT:
902                                 case ND_TOOLSETTINGS:
903                                         ED_region_tag_redraw(ar);
904                                         break;
905                         }
906                         switch(wmn->action) {
907                                 case NA_EDITED:
908                                         ED_region_tag_redraw(ar);
909                                         break;
910                         }
911                         break;
912                 case NC_OBJECT:
913                         switch(wmn->data) {
914                                 case ND_BONE_ACTIVE:
915                                 case ND_BONE_SELECT:
916                                 case ND_TRANSFORM:
917                                 case ND_POSE:
918                                 case ND_DRAW:
919                                 case ND_KEYS:
920                                 case ND_MODIFIER:
921                                         ED_region_tag_redraw(ar);
922                                         break;
923                         }
924                         break;
925                 case NC_GEOM:
926                         switch(wmn->data) {
927                                 case ND_DATA:
928                                 case ND_SELECT:
929                                         ED_region_tag_redraw(ar);
930                                         break;
931                         }
932                         if (wmn->action == NA_EDITED)
933                                 ED_region_tag_redraw(ar);
934                         break;
935                 case NC_TEXTURE:
936                 case NC_MATERIAL:
937                         /* for brush textures */
938                         ED_region_tag_redraw(ar);
939                         break;
940                 case NC_BRUSH:
941                         if (wmn->action==NA_EDITED)
942                                 ED_region_tag_redraw(ar);
943                         break;
944                 case NC_SPACE:
945                         if (wmn->data == ND_SPACE_VIEW3D)
946                                 ED_region_tag_redraw(ar);
947                         break;
948                 case NC_ID:
949                         if (wmn->action == NA_RENAME)
950                                 ED_region_tag_redraw(ar);
951                         break;
952                 case NC_SCREEN: 
953                         if (wmn->data == ND_GPENCIL)
954                                 ED_region_tag_redraw(ar);
955                         break;
956         }
957 }
958
959 /* add handlers, stuff you only do once or on area/region changes */
960 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
961 {
962         wmKeyMap *keymap;
963         
964         ED_region_panels_init(wm, ar);
965
966         keymap= WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
967         WM_event_add_keymap_handler(&ar->handlers, keymap);
968 }
969
970 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
971 {
972         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
973 }
974
975 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
976 {
977         /* context changes */
978         switch(wmn->category) {
979                 case NC_WM:
980                         if (wmn->data == ND_HISTORY)
981                                 ED_region_tag_redraw(ar);
982                         break;
983                 case NC_SCENE:
984                         if (wmn->data == ND_MODE)
985                                 ED_region_tag_redraw(ar);
986                         break;
987                 case NC_SPACE:
988                         if (wmn->data == ND_SPACE_VIEW3D)
989                                 ED_region_tag_redraw(ar);
990                         break;
991         }
992 }
993
994 /*area (not region) level listener*/
995 static void space_view3d_listener(struct ScrArea *sa, struct wmNotifier *wmn)
996 {
997         View3D *v3d = sa->spacedata.first;
998
999         /* context changes */
1000         switch(wmn->category) {
1001                 case NC_SCENE:
1002                         switch(wmn->data) {
1003                                 case ND_WORLD:
1004                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1005                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1006                                         break;
1007                         }
1008                         break;
1009                 case NC_WORLD:
1010                         switch(wmn->data) {
1011                                 case ND_WORLD_DRAW:
1012                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1013                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1014                                         break;
1015                         }
1016                         break;
1017                 case NC_MATERIAL:
1018                         switch(wmn->data) {
1019                                 case ND_NODES:
1020                                         if (v3d->drawtype == OB_TEXTURE)
1021                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1022                                         break;
1023                         }
1024                         break;
1025         }
1026
1027         // removed since BKE_image_user_calc_frame is now called in draw_bgpic because screen_ops doesnt call the notifier.
1028 #if 0
1029         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
1030                 View3D *v3d = area->spacedata.first;
1031                 BGpic *bgpic = v3d->bgpicbase.first;
1032
1033                 for (; bgpic; bgpic = bgpic->next) {
1034                         if (bgpic->ima) {
1035                                 Scene *scene = wmn->reference;
1036                                 BKE_image_user_calc_frame(&bgpic->iuser, scene->r.cfra, 0);
1037                         }
1038                 }
1039         }
1040 #endif
1041 }
1042
1043 const char *view3d_context_dir[] = {
1044         "selected_objects", "selected_bases", "selected_editable_objects",
1045         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
1046         "active_base", "active_object", NULL};
1047
1048 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1049 {
1050         View3D *v3d= CTX_wm_view3d(C);
1051         Scene *scene= CTX_data_scene(C);
1052         Base *base;
1053         /* fallback to the scene layer, allows duplicate and other object operators to run outside the 3d view */
1054         unsigned int lay = v3d ? v3d->lay:scene->lay;
1055
1056         if (CTX_data_dir(member)) {
1057                 CTX_data_dir_set(result, view3d_context_dir);
1058         }
1059         else if (CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
1060                 int selected_objects= CTX_data_equals(member, "selected_objects");
1061
1062                 for (base=scene->base.first; base; base=base->next) {
1063                         if ((base->flag & SELECT) && (base->lay & lay)) {
1064                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1065                                         if (selected_objects)
1066                                                 CTX_data_id_list_add(result, &base->object->id);
1067                                         else
1068                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1069                                 }
1070                         }
1071                 }
1072                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1073                 return 1;
1074         }
1075         else if (CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
1076                 int selected_editable_objects= CTX_data_equals(member, "selected_editable_objects");
1077
1078                 for (base=scene->base.first; base; base=base->next) {
1079                         if ((base->flag & SELECT) && (base->lay & lay)) {
1080                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1081                                         if (0==object_is_libdata(base->object)) {
1082                                                 if (selected_editable_objects)
1083                                                         CTX_data_id_list_add(result, &base->object->id);
1084                                                 else
1085                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1086                                         }
1087                                 }
1088                         }
1089                 }
1090                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1091                 return 1;
1092         }
1093         else if (CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
1094                 int visible_objects= CTX_data_equals(member, "visible_objects");
1095
1096                 for (base=scene->base.first; base; base=base->next) {
1097                         if (base->lay & lay) {
1098                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW)==0) {
1099                                         if (visible_objects)
1100                                                 CTX_data_id_list_add(result, &base->object->id);
1101                                         else
1102                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1103                                 }
1104                         }
1105                 }
1106                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1107                 return 1;
1108         }
1109         else if (CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
1110                 int selectable_objects= CTX_data_equals(member, "selectable_objects");
1111
1112                 for (base=scene->base.first; base; base=base->next) {
1113                         if (base->lay & lay) {
1114                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW)==0 && (base->object->restrictflag & OB_RESTRICT_SELECT)==0) {
1115                                         if (selectable_objects)
1116                                                 CTX_data_id_list_add(result, &base->object->id);
1117                                         else
1118                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1119                                 }
1120                         }
1121                 }
1122                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1123                 return 1;
1124         }
1125         else if (CTX_data_equals(member, "active_base")) {
1126                 if (scene->basact && (scene->basact->lay & lay))
1127                         if ((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1128                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
1129                 
1130                 return 1;
1131         }
1132         else if (CTX_data_equals(member, "active_object")) {
1133                 if (scene->basact && (scene->basact->lay & lay))
1134                         if ((scene->basact->object->restrictflag & OB_RESTRICT_VIEW)==0)
1135                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
1136                 
1137                 return 1;
1138         }
1139         else {
1140                 return 0; /* not found */
1141         }
1142
1143         return -1; /* found but not available */
1144 }
1145
1146
1147 /* only called once, from space/spacetypes.c */
1148 void ED_spacetype_view3d(void)
1149 {
1150         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1151         ARegionType *art;
1152         
1153         st->spaceid= SPACE_VIEW3D;
1154         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1155         
1156         st->new= view3d_new;
1157         st->free= view3d_free;
1158         st->init= view3d_init;
1159         st->listener = space_view3d_listener;
1160         st->duplicate= view3d_duplicate;
1161         st->operatortypes= view3d_operatortypes;
1162         st->keymap= view3d_keymap;
1163         st->dropboxes= view3d_dropboxes;
1164         st->context= view3d_context;
1165         
1166         /* regions: main window */
1167         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1168         art->regionid = RGN_TYPE_WINDOW;
1169         art->keymapflag= ED_KEYMAP_GPENCIL;
1170         art->draw= view3d_main_area_draw;
1171         art->init= view3d_main_area_init;
1172         art->free= view3d_main_area_free;
1173         art->duplicate= view3d_main_area_duplicate;
1174         art->listener= view3d_main_area_listener;
1175         art->cursor= view3d_main_area_cursor;
1176         art->lock= 1;   /* can become flag, see BKE_spacedata_draw_locks */
1177         BLI_addhead(&st->regiontypes, art);
1178         
1179         /* regions: listview/buttons */
1180         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1181         art->regionid = RGN_TYPE_UI;
1182         art->prefsizex= 180; // XXX
1183         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1184         art->listener= view3d_buttons_area_listener;
1185         art->init= view3d_buttons_area_init;
1186         art->draw= view3d_buttons_area_draw;
1187         BLI_addhead(&st->regiontypes, art);
1188
1189         view3d_buttons_register(art);
1190
1191         /* regions: tool(bar) */
1192         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1193         art->regionid = RGN_TYPE_TOOLS;
1194         art->prefsizex= 160; // XXX
1195         art->prefsizey= 50; // XXX
1196         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1197         art->listener= view3d_buttons_area_listener;
1198         art->init= view3d_tools_area_init;
1199         art->draw= view3d_tools_area_draw;
1200         BLI_addhead(&st->regiontypes, art);
1201         
1202 #if 0
1203         /* unfinished still */
1204         view3d_toolshelf_register(art);
1205 #endif
1206
1207         /* regions: tool properties */
1208         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d tool properties region");
1209         art->regionid = RGN_TYPE_TOOL_PROPS;
1210         art->prefsizex= 0;
1211         art->prefsizey= 120;
1212         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
1213         art->listener= view3d_props_area_listener;
1214         art->init= view3d_tools_area_init;
1215         art->draw= view3d_tools_area_draw;
1216         BLI_addhead(&st->regiontypes, art);
1217         
1218         view3d_tool_props_register(art);
1219         
1220         
1221         /* regions: header */
1222         art= MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1223         art->regionid = RGN_TYPE_HEADER;
1224         art->prefsizey= HEADERY;
1225         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
1226         art->listener= view3d_header_area_listener;
1227         art->init= view3d_header_area_init;
1228         art->draw= view3d_header_area_draw;
1229         BLI_addhead(&st->regiontypes, art);
1230         
1231         BKE_spacetype_register(st);
1232 }
1233