Holiday coding log :)
[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;
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         BLI_freelistN(&vd->bgpicbase);
337
338         if (vd->localvd) MEM_freeN(vd->localvd);
339         
340         if (vd->properties_storage) MEM_freeN(vd->properties_storage);
341 }
342
343
344 /* spacetype; init callback */
345 static void view3d_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
346 {
347
348 }
349
350 static SpaceLink *view3d_duplicate(SpaceLink *sl)
351 {
352         View3D *v3do = (View3D *)sl;
353         View3D *v3dn = MEM_dupallocN(sl);
354         
355         /* clear or remove stuff from old */
356         
357 // XXX  BIF_view3d_previewrender_free(v3do);
358         
359         if (v3do->localvd) {
360                 v3do->localvd = NULL;
361                 v3do->properties_storage = NULL;
362                 v3do->lay = v3dn->localvd->lay;
363                 v3do->lay &= 0xFFFFFF;
364         }
365
366         if (v3dn->drawtype == OB_RENDER)
367                 v3dn->drawtype = OB_SOLID;
368         
369         /* copy or clear inside new stuff */
370
371         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
372
373         v3dn->properties_storage = NULL;
374         
375         return (SpaceLink *)v3dn;
376 }
377
378 /* add handlers, stuff you only do once or on area/region changes */
379 static void view3d_main_area_init(wmWindowManager *wm, ARegion *ar)
380 {
381         ListBase *lb;
382         wmKeyMap *keymap;
383
384         /* object ops. */
385         
386         /* important to be before Pose keymap since they can both be enabled at once */
387         keymap = WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
388         WM_event_add_keymap_handler(&ar->handlers, keymap);
389         
390         
391         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
392         WM_event_add_keymap_handler(&ar->handlers, keymap);
393
394         /* pose is not modal, operator poll checks for this */
395         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
396         WM_event_add_keymap_handler(&ar->handlers, keymap);
397         
398         keymap = WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
399         WM_event_add_keymap_handler(&ar->handlers, keymap);
400
401         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
402         WM_event_add_keymap_handler(&ar->handlers, keymap);
403
404         keymap = WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
405         WM_event_add_keymap_handler(&ar->handlers, keymap);
406
407         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
408         WM_event_add_keymap_handler(&ar->handlers, keymap);
409
410         keymap = WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
411         WM_event_add_keymap_handler(&ar->handlers, keymap);
412         
413         keymap = WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
414         WM_event_add_keymap_handler(&ar->handlers, keymap);
415         
416         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
417         WM_event_add_keymap_handler(&ar->handlers, keymap);
418         
419         keymap = WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
420         WM_event_add_keymap_handler(&ar->handlers, keymap);
421
422         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
423         WM_event_add_keymap_handler(&ar->handlers, keymap);
424
425         keymap = WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
426         WM_event_add_keymap_handler(&ar->handlers, keymap);
427         
428         keymap = WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
429         WM_event_add_keymap_handler(&ar->handlers, keymap);
430
431         /* armature sketching needs to take over mouse */
432         keymap = WM_keymap_find(wm->defaultconf, "Armature Sketch", 0, 0);
433         WM_event_add_keymap_handler(&ar->handlers, keymap);
434
435         keymap = WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
436         WM_event_add_keymap_handler(&ar->handlers, keymap);
437
438         /* editfont keymap swallows all... */
439         keymap = WM_keymap_find(wm->defaultconf, "Font", 0, 0);
440         WM_event_add_keymap_handler(&ar->handlers, keymap);
441
442         keymap = WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
443         WM_event_add_keymap_handler(&ar->handlers, keymap);
444
445         keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
446         WM_event_add_keymap_handler(&ar->handlers, keymap);
447
448         /* own keymap, last so modes can override it */
449         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
450         WM_event_add_keymap_handler(&ar->handlers, keymap);
451
452         keymap = WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
453         WM_event_add_keymap_handler(&ar->handlers, keymap);
454         
455         /* add drop boxes */
456         lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
457         
458         WM_event_add_dropbox_handler(&ar->handlers, lb);
459         
460 }
461
462 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
463 {
464         if (drag->type == WM_DRAG_ID) {
465                 ID *id = (ID *)drag->poin;
466                 if (GS(id->name) == ID_OB)
467                         return 1;
468         }
469         return 0;
470 }
471
472 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
473 {
474         if (drag->type == WM_DRAG_ID) {
475                 ID *id = (ID *)drag->poin;
476                 if (GS(id->name) == ID_MA)
477                         return 1;
478         }
479         return 0;
480 }
481
482 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
483 {
484         if (drag->type == WM_DRAG_ID) {
485                 ID *id = (ID *)drag->poin;
486                 if (GS(id->name) == ID_IM)
487                         return 1;
488         }
489         else if (drag->type == WM_DRAG_PATH) {
490                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))   /* rule might not work? */
491                         return 1;
492         }
493         return 0;
494 }
495
496
497 static int view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
498 {
499         if (ED_view3d_give_base_under_cursor(C, event->mval) ) {
500                 return 0;
501         }
502         return view3d_ima_drop_poll(C, drag, event);
503 }
504
505 static int view3d_ima_ob_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
506 {
507         if (ED_view3d_give_base_under_cursor(C, event->mval) ) {
508                 return view3d_ima_drop_poll(C, drag, event);
509         }
510         return 0;
511 }
512
513 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
514 {
515         ID *id = (ID *)drag->poin;
516         PointerRNA ptr;
517
518         /* need to put name in sub-operator in macro */
519         ptr = RNA_pointer_get(drop->ptr, "OBJECT_OT_add_named");
520         if (ptr.data)
521                 RNA_string_set(&ptr, "name", id->name + 2);
522         else
523                 RNA_string_set(drop->ptr, "name", id->name + 2);
524 }
525
526 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
527 {
528         ID *id = (ID *)drag->poin;
529         
530         RNA_string_set(drop->ptr, "name", id->name + 2);
531 }
532
533 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
534 {
535         ID *id = (ID *)drag->poin;
536         
537         if (id)
538                 RNA_string_set(drop->ptr, "name", id->name + 2);
539         if (drag->path[0])
540                 RNA_string_set(drop->ptr, "filepath", drag->path);
541 }
542
543
544 /* region dropbox definition */
545 static void view3d_dropboxes(void)
546 {
547         ListBase *lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
548         
549         WM_dropbox_add(lb, "OBJECT_OT_add_named_cursor", view3d_ob_drop_poll, view3d_ob_drop_copy);
550         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
551         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_ob_drop_poll, view3d_id_path_drop_copy);
552         WM_dropbox_add(lb, "VIEW3D_OT_background_image_add", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
553 }
554
555
556
557 /* type callback, not region itself */
558 static void view3d_main_area_free(ARegion *ar)
559 {
560         RegionView3D *rv3d = ar->regiondata;
561         
562         if (rv3d) {
563                 if (rv3d->localvd) MEM_freeN(rv3d->localvd);
564                 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
565
566                 if (rv3d->ri) {
567                         // XXX          BIF_view3d_previewrender_free(rv3d);
568                 }
569
570                 if (rv3d->render_engine)
571                         RE_engine_free(rv3d->render_engine);
572                 
573                 if (rv3d->depths) {
574                         if (rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
575                         MEM_freeN(rv3d->depths);
576                 }
577                 MEM_freeN(rv3d);
578                 ar->regiondata = NULL;
579         }
580 }
581
582 /* copy regiondata */
583 static void *view3d_main_area_duplicate(void *poin)
584 {
585         if (poin) {
586                 RegionView3D *rv3d = poin, *new;
587         
588                 new = MEM_dupallocN(rv3d);
589                 if (rv3d->localvd)
590                         new->localvd = MEM_dupallocN(rv3d->localvd);
591                 if (rv3d->clipbb)
592                         new->clipbb = MEM_dupallocN(rv3d->clipbb);
593                 
594                 new->depths = NULL;
595                 new->ri = NULL;
596                 new->render_engine = NULL;
597                 new->gpd = NULL;
598                 new->sms = NULL;
599                 new->smooth_timer = NULL;
600                 
601                 return new;
602         }
603         return NULL;
604 }
605
606 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
607 {
608         wmWindow *win = wmn->wm->winactive;
609         ScrArea *sa;
610         unsigned int lay_used = 0;
611         Base *base;
612
613         if (!win) return;
614
615         base = scene->base.first;
616         while (base) {
617                 lay_used |= base->lay & ((1 << 20) - 1); /* ignore localview */
618
619                 if (lay_used == (1 << 20) - 1)
620                         break;
621
622                 base = base->next;
623         }
624
625         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
626                 if (sa->spacetype == SPACE_VIEW3D) {
627                         if (BLI_findindex(&sa->regionbase, ar) != -1) {
628                                 View3D *v3d = sa->spacedata.first;
629                                 v3d->lay_used = lay_used;
630                                 break;
631                         }
632                 }
633         }
634 }
635
636 static void view3d_main_area_listener(ARegion *ar, wmNotifier *wmn)
637 {
638         bScreen *sc;
639
640         /* context changes */
641         switch (wmn->category) {
642                 case NC_ANIMATION:
643                         switch (wmn->data) {
644                                 case ND_KEYFRAME_PROP:
645                                 case ND_NLA_ACTCHANGE:
646                                         ED_region_tag_redraw(ar);
647                                         break;
648                                 case ND_NLA:
649                                 case ND_KEYFRAME:
650                                         if (wmn->action == NA_EDITED)
651                                                 ED_region_tag_redraw(ar);
652                                         break;
653                                 case ND_ANIMCHAN:
654                                         if (wmn->action == NA_SELECTED)
655                                                 ED_region_tag_redraw(ar);
656                                         break;
657                         }
658                         break;
659                 case NC_SCENE:
660                         switch (wmn->data) {
661                                 case ND_LAYER_CONTENT:
662                                         view3d_recalc_used_layers(ar, wmn, wmn->reference);
663                                         ED_region_tag_redraw(ar);
664                                         break;
665                                 case ND_FRAME:
666                                 case ND_TRANSFORM:
667                                 case ND_OB_ACTIVE:
668                                 case ND_OB_SELECT:
669                                 case ND_OB_VISIBLE:
670                                 case ND_LAYER:
671                                 case ND_RENDER_OPTIONS:
672                                 case ND_MODE:
673                                         ED_region_tag_redraw(ar);
674                                         break;
675                                 case ND_WORLD:
676                                         /* handled by space_view3d_listener() for v3d access */
677                                         break;
678                         }
679                         if (wmn->action == NA_EDITED)
680                                 ED_region_tag_redraw(ar);
681                         break;
682                 case NC_OBJECT:
683                         switch (wmn->data) {
684                                 case ND_BONE_ACTIVE:
685                                 case ND_BONE_SELECT:
686                                 case ND_TRANSFORM:
687                                 case ND_POSE:
688                                 case ND_DRAW:
689                                 case ND_MODIFIER:
690                                 case ND_CONSTRAINT:
691                                 case ND_KEYS:
692                                 case ND_PARTICLE:
693                                         ED_region_tag_redraw(ar);
694                                         break;
695                         }
696                         switch (wmn->action) {
697                                 case NA_ADDED:
698                                         ED_region_tag_redraw(ar);
699                                         break;
700                         }
701                         break;
702                 case NC_GEOM:
703                         switch (wmn->data) {
704                                 case ND_DATA:
705                                 case ND_VERTEX_GROUP:
706                                 case ND_SELECT:
707                                         ED_region_tag_redraw(ar);
708                                         break;
709                         }
710                         switch (wmn->action) {
711                                 case NA_EDITED:
712                                         ED_region_tag_redraw(ar);
713                                         break;
714                         }
715                         break;
716                 case NC_GROUP:
717                         /* all group ops for now */
718                         ED_region_tag_redraw(ar);
719                         break;
720                 case NC_BRUSH:
721                         if (wmn->action == NA_EDITED)
722                                 ED_region_tag_redraw_overlay(ar);
723                         break;
724                 case NC_MATERIAL:
725                         switch (wmn->data) {
726                                 case ND_SHADING_DRAW:
727                                 case ND_SHADING_LINKS:
728                                         ED_region_tag_redraw(ar);
729                                         break;
730                         }
731                         break;
732                 case NC_WORLD:
733                         switch (wmn->data) {
734                                 case ND_WORLD_DRAW:
735                                         /* handled by space_view3d_listener() for v3d access */
736                                         break;
737                                 case ND_WORLD_STARS:
738                                 {
739                                         RegionView3D *rv3d = ar->regiondata;
740                                         if (rv3d->persp == RV3D_CAMOB) {
741                                                 ED_region_tag_redraw(ar);
742                                         }
743                                 }
744                         }
745                         break;
746                 case NC_LAMP:
747                         switch (wmn->data) {
748                                 case ND_LIGHTING_DRAW:
749                                         ED_region_tag_redraw(ar);
750                                         break;
751                         }
752                         break;
753                 case NC_IMAGE:
754                         /* this could be more fine grained checks if we had
755                          * more context than just the region */
756                         ED_region_tag_redraw(ar);
757                         break;
758                 case NC_TEXTURE:
759                         /* same as above */
760                         ED_region_tag_redraw(ar);
761                         break;
762                 case NC_MOVIECLIP:
763                         if (wmn->data == ND_DISPLAY)
764                                 ED_region_tag_redraw(ar);
765                         break;
766                 case NC_SPACE:
767                         if (wmn->data == ND_SPACE_VIEW3D) {
768                                 if (wmn->subtype == NS_VIEW3D_GPU) {
769                                         RegionView3D *rv3d = ar->regiondata;
770                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
771                                 }
772                                 ED_region_tag_redraw(ar);
773                         }
774                         break;
775                 case NC_ID:
776                         if (wmn->action == NA_RENAME)
777                                 ED_region_tag_redraw(ar);
778                         break;
779                 case NC_SCREEN:
780                         switch (wmn->data) {
781                                 case ND_ANIMPLAY:
782                                 case ND_SKETCH:
783                                         ED_region_tag_redraw(ar);
784                                         break;
785                                 case ND_SCREENBROWSE:
786                                 case ND_SCREENDELETE:
787                                 case ND_SCREENSET:
788                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
789                                         /* updates used layers only for View3D in active screen */
790                                         sc = wmn->reference;
791                                         view3d_recalc_used_layers(ar, wmn, sc->scene);
792                                         ED_region_tag_redraw(ar);
793                                         break;
794                         }
795
796                         break;
797                 case NC_GPENCIL:
798                         if (wmn->action == NA_EDITED)
799                                 ED_region_tag_redraw(ar);
800                         break;
801         }
802 }
803
804 /* concept is to retrieve cursor type context-less */
805 static void view3d_main_area_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
806 {
807         Scene *scene = win->screen->scene;
808
809         if (scene->obedit) {
810                 WM_cursor_set(win, CURSOR_EDIT);
811         }
812         else {
813                 WM_cursor_set(win, CURSOR_STD);
814         }
815 }
816
817 /* add handlers, stuff you only do once or on area/region changes */
818 static void view3d_header_area_init(wmWindowManager *wm, ARegion *ar)
819 {
820         wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
821         
822         WM_event_add_keymap_handler(&ar->handlers, keymap);
823
824         ED_region_header_init(ar);
825 }
826
827 static void view3d_header_area_draw(const bContext *C, ARegion *ar)
828 {
829         ED_region_header(C, ar);
830 }
831
832 static void view3d_header_area_listener(ARegion *ar, wmNotifier *wmn)
833 {
834         /* context changes */
835         switch (wmn->category) {
836                 case NC_SCENE:
837                         switch (wmn->data) {
838                                 case ND_FRAME:
839                                 case ND_OB_ACTIVE:
840                                 case ND_OB_SELECT:
841                                 case ND_OB_VISIBLE:
842                                 case ND_MODE:
843                                 case ND_LAYER:
844                                 case ND_TOOLSETTINGS:
845                                 case ND_LAYER_CONTENT:
846                                         ED_region_tag_redraw(ar);
847                                         break;
848                         }
849                         break;
850                 case NC_SPACE:
851                         if (wmn->data == ND_SPACE_VIEW3D)
852                                 ED_region_tag_redraw(ar);
853                         break;
854         }
855 }
856
857 /* add handlers, stuff you only do once or on area/region changes */
858 static void view3d_buttons_area_init(wmWindowManager *wm, ARegion *ar)
859 {
860         wmKeyMap *keymap;
861
862         ED_region_panels_init(wm, ar);
863         
864         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
865         WM_event_add_keymap_handler(&ar->handlers, keymap);
866 }
867
868 static void view3d_buttons_area_draw(const bContext *C, ARegion *ar)
869 {
870         ED_region_panels(C, ar, 1, NULL, -1);
871 }
872
873 static void view3d_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
874 {
875         /* context changes */
876         switch (wmn->category) {
877                 case NC_ANIMATION:
878                         switch (wmn->data) {
879                                 case ND_KEYFRAME_PROP:
880                                 case ND_NLA_ACTCHANGE:
881                                         ED_region_tag_redraw(ar);
882                                         break;
883                                 case ND_NLA:
884                                 case ND_KEYFRAME:
885                                         if (wmn->action == NA_EDITED)
886                                                 ED_region_tag_redraw(ar);
887                                         break;
888                         }
889                         break;
890                 case NC_SCENE:
891                         switch (wmn->data) {
892                                 case ND_FRAME:
893                                 case ND_OB_ACTIVE:
894                                 case ND_OB_SELECT:
895                                 case ND_OB_VISIBLE:
896                                 case ND_MODE:
897                                 case ND_LAYER:
898                                 case ND_LAYER_CONTENT:
899                                 case ND_TOOLSETTINGS:
900                                         ED_region_tag_redraw(ar);
901                                         break;
902                         }
903                         switch (wmn->action) {
904                                 case NA_EDITED:
905                                         ED_region_tag_redraw(ar);
906                                         break;
907                         }
908                         break;
909                 case NC_OBJECT:
910                         switch (wmn->data) {
911                                 case ND_BONE_ACTIVE:
912                                 case ND_BONE_SELECT:
913                                 case ND_TRANSFORM:
914                                 case ND_POSE:
915                                 case ND_DRAW:
916                                 case ND_KEYS:
917                                 case ND_MODIFIER:
918                                         ED_region_tag_redraw(ar);
919                                         break;
920                         }
921                         break;
922                 case NC_GEOM:
923                         switch (wmn->data) {
924                                 case ND_DATA:
925                                 case ND_VERTEX_GROUP:
926                                 case ND_SELECT:
927                                         ED_region_tag_redraw(ar);
928                                         break;
929                         }
930                         if (wmn->action == NA_EDITED)
931                                 ED_region_tag_redraw(ar);
932                         break;
933                 case NC_TEXTURE:
934                 case NC_MATERIAL:
935                         /* for brush textures */
936                         ED_region_tag_redraw(ar);
937                         break;
938                 case NC_BRUSH:
939                         if (wmn->action == NA_EDITED)
940                                 ED_region_tag_redraw(ar);
941                         break;
942                 case NC_SPACE:
943                         if (wmn->data == ND_SPACE_VIEW3D)
944                                 ED_region_tag_redraw(ar);
945                         break;
946                 case NC_ID:
947                         if (wmn->action == NA_RENAME)
948                                 ED_region_tag_redraw(ar);
949                         break;
950                 case NC_GPENCIL:
951                         if (wmn->data == ND_DATA || wmn->action == NA_EDITED)
952                                 ED_region_tag_redraw(ar);
953                         break;
954         }
955 }
956
957 /* add handlers, stuff you only do once or on area/region changes */
958 static void view3d_tools_area_init(wmWindowManager *wm, ARegion *ar)
959 {
960         wmKeyMap *keymap;
961         
962         ED_region_panels_init(wm, ar);
963
964         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
965         WM_event_add_keymap_handler(&ar->handlers, keymap);
966 }
967
968 static void view3d_tools_area_draw(const bContext *C, ARegion *ar)
969 {
970         ED_region_panels(C, ar, 1, CTX_data_mode_string(C), -1);
971 }
972
973 static void view3d_props_area_listener(ARegion *ar, wmNotifier *wmn)
974 {
975         /* context changes */
976         switch (wmn->category) {
977                 case NC_WM:
978                         if (wmn->data == ND_HISTORY)
979                                 ED_region_tag_redraw(ar);
980                         break;
981                 case NC_SCENE:
982                         if (wmn->data == ND_MODE)
983                                 ED_region_tag_redraw(ar);
984                         break;
985                 case NC_SPACE:
986                         if (wmn->data == ND_SPACE_VIEW3D)
987                                 ED_region_tag_redraw(ar);
988                         break;
989         }
990 }
991
992 /*area (not region) level listener*/
993 static void space_view3d_listener(ScrArea *sa, struct wmNotifier *wmn)
994 {
995         View3D *v3d = sa->spacedata.first;
996
997         /* context changes */
998         switch (wmn->category) {
999                 case NC_SCENE:
1000                         switch (wmn->data) {
1001                                 case ND_WORLD:
1002                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1003                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1004                                         break;
1005                         }
1006                         break;
1007                 case NC_WORLD:
1008                         switch (wmn->data) {
1009                                 case ND_WORLD_DRAW:
1010                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1011                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1012                                         break;
1013                         }
1014                         break;
1015                 case NC_MATERIAL:
1016                         switch (wmn->data) {
1017                                 case ND_NODES:
1018                                         if (v3d->drawtype == OB_TEXTURE)
1019                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1020                                         break;
1021                         }
1022                         break;
1023         }
1024
1025         /* removed since BKE_image_user_frame_calc is now called in view3d_draw_bgpic because screen_ops doesnt call the notifier. */
1026 #if 0
1027         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
1028                 View3D *v3d = area->spacedata.first;
1029                 BGpic *bgpic = v3d->bgpicbase.first;
1030
1031                 for (; bgpic; bgpic = bgpic->next) {
1032                         if (bgpic->ima) {
1033                                 Scene *scene = wmn->reference;
1034                                 BKE_image_user_frame_calc(&bgpic->iuser, scene->r.cfra, 0);
1035                         }
1036                 }
1037         }
1038 #endif
1039 }
1040
1041 const char *view3d_context_dir[] = {
1042         "selected_objects", "selected_bases", "selected_editable_objects",
1043         "selected_editable_bases", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
1044         "active_base", "active_object", NULL
1045 };
1046
1047 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1048 {
1049         View3D *v3d = CTX_wm_view3d(C);
1050         Scene *scene = CTX_data_scene(C);
1051         Base *base;
1052         /* fallback to the scene layer, allows duplicate and other object operators to run outside the 3d view */
1053         unsigned int lay = v3d ? v3d->lay : scene->lay;
1054
1055         if (CTX_data_dir(member)) {
1056                 CTX_data_dir_set(result, view3d_context_dir);
1057         }
1058         else if (CTX_data_equals(member, "selected_objects") || CTX_data_equals(member, "selected_bases")) {
1059                 int selected_objects = CTX_data_equals(member, "selected_objects");
1060
1061                 for (base = scene->base.first; base; base = base->next) {
1062                         if ((base->flag & SELECT) && (base->lay & lay)) {
1063                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1064                                         if (selected_objects)
1065                                                 CTX_data_id_list_add(result, &base->object->id);
1066                                         else
1067                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1068                                 }
1069                         }
1070                 }
1071                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1072                 return 1;
1073         }
1074         else if (CTX_data_equals(member, "selected_editable_objects") || CTX_data_equals(member, "selected_editable_bases")) {
1075                 int selected_editable_objects = CTX_data_equals(member, "selected_editable_objects");
1076
1077                 for (base = scene->base.first; base; base = base->next) {
1078                         if ((base->flag & SELECT) && (base->lay & lay)) {
1079                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1080                                         if (0 == BKE_object_is_libdata(base->object)) {
1081                                                 if (selected_editable_objects)
1082                                                         CTX_data_id_list_add(result, &base->object->id);
1083                                                 else
1084                                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1085                                         }
1086                                 }
1087                         }
1088                 }
1089                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1090                 return 1;
1091         }
1092         else if (CTX_data_equals(member, "visible_objects") || CTX_data_equals(member, "visible_bases")) {
1093                 int visible_objects = CTX_data_equals(member, "visible_objects");
1094
1095                 for (base = scene->base.first; base; base = base->next) {
1096                         if (base->lay & lay) {
1097                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0) {
1098                                         if (visible_objects)
1099                                                 CTX_data_id_list_add(result, &base->object->id);
1100                                         else
1101                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1102                                 }
1103                         }
1104                 }
1105                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1106                 return 1;
1107         }
1108         else if (CTX_data_equals(member, "selectable_objects") || CTX_data_equals(member, "selectable_bases")) {
1109                 int selectable_objects = CTX_data_equals(member, "selectable_objects");
1110
1111                 for (base = scene->base.first; base; base = base->next) {
1112                         if (base->lay & lay) {
1113                                 if ((base->object->restrictflag & OB_RESTRICT_VIEW) == 0 && (base->object->restrictflag & OB_RESTRICT_SELECT) == 0) {
1114                                         if (selectable_objects)
1115                                                 CTX_data_id_list_add(result, &base->object->id);
1116                                         else
1117                                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
1118                                 }
1119                         }
1120                 }
1121                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
1122                 return 1;
1123         }
1124         else if (CTX_data_equals(member, "active_base")) {
1125                 if (scene->basact && (scene->basact->lay & lay)) {
1126                         Object *ob = scene->basact->object;
1127                         /* if hidden but in edit mode, we still display, can happen with animation */
1128                         if ((ob->restrictflag & OB_RESTRICT_VIEW) == 0 || (ob->mode & OB_MODE_EDIT))
1129                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, scene->basact);
1130                 }
1131                 
1132                 return 1;
1133         }
1134         else if (CTX_data_equals(member, "active_object")) {
1135                 if (scene->basact && (scene->basact->lay & lay)) {
1136                         Object *ob = scene->basact->object;
1137                         if ((ob->restrictflag & OB_RESTRICT_VIEW) == 0 || (ob->mode & OB_MODE_EDIT))
1138                                 CTX_data_id_pointer_set(result, &scene->basact->object->id);
1139                 }
1140                 
1141                 return 1;
1142         }
1143         else {
1144                 return 0; /* not found */
1145         }
1146
1147         return -1; /* found but not available */
1148 }
1149
1150
1151 /* only called once, from space/spacetypes.c */
1152 void ED_spacetype_view3d(void)
1153 {
1154         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1155         ARegionType *art;
1156         
1157         st->spaceid = SPACE_VIEW3D;
1158         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1159         
1160         st->new = view3d_new;
1161         st->free = view3d_free;
1162         st->init = view3d_init;
1163         st->listener = space_view3d_listener;
1164         st->duplicate = view3d_duplicate;
1165         st->operatortypes = view3d_operatortypes;
1166         st->keymap = view3d_keymap;
1167         st->dropboxes = view3d_dropboxes;
1168         st->context = view3d_context;
1169         
1170         /* regions: main window */
1171         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1172         art->regionid = RGN_TYPE_WINDOW;
1173         art->keymapflag = ED_KEYMAP_GPENCIL;
1174         art->draw = view3d_main_area_draw;
1175         art->init = view3d_main_area_init;
1176         art->free = view3d_main_area_free;
1177         art->duplicate = view3d_main_area_duplicate;
1178         art->listener = view3d_main_area_listener;
1179         art->cursor = view3d_main_area_cursor;
1180         art->lock = 1;   /* can become flag, see BKE_spacedata_draw_locks */
1181         BLI_addhead(&st->regiontypes, art);
1182         
1183         /* regions: listview/buttons */
1184         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1185         art->regionid = RGN_TYPE_UI;
1186         art->prefsizex = 180; /* XXX */
1187         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1188         art->listener = view3d_buttons_area_listener;
1189         art->init = view3d_buttons_area_init;
1190         art->draw = view3d_buttons_area_draw;
1191         BLI_addhead(&st->regiontypes, art);
1192
1193         view3d_buttons_register(art);
1194
1195         /* regions: tool(bar) */
1196         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1197         art->regionid = RGN_TYPE_TOOLS;
1198         art->prefsizex = 160; /* XXX */
1199         art->prefsizey = 50; /* XXX */
1200         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1201         art->listener = view3d_buttons_area_listener;
1202         art->init = view3d_tools_area_init;
1203         art->draw = view3d_tools_area_draw;
1204         BLI_addhead(&st->regiontypes, art);
1205         
1206 #if 0
1207         /* unfinished still */
1208         view3d_toolshelf_register(art);
1209 #endif
1210
1211         /* regions: tool properties */
1212         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tool properties region");
1213         art->regionid = RGN_TYPE_TOOL_PROPS;
1214         art->prefsizex = 0;
1215         art->prefsizey = 120;
1216         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1217         art->listener = view3d_props_area_listener;
1218         art->init = view3d_tools_area_init;
1219         art->draw = view3d_tools_area_draw;
1220         BLI_addhead(&st->regiontypes, art);
1221         
1222         view3d_tool_props_register(art);
1223         
1224         
1225         /* regions: header */
1226         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1227         art->regionid = RGN_TYPE_HEADER;
1228         art->prefsizey = HEADERY;
1229         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1230         art->listener = view3d_header_area_listener;
1231         art->init = view3d_header_area_init;
1232         art->draw = view3d_header_area_draw;
1233         BLI_addhead(&st->regiontypes, art);
1234         
1235         BKE_spacetype_register(st);
1236 }
1237