Base refactor 3/4
[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_material_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_context.h"
46 #include "BKE_depsgraph.h"
47 #include "BKE_icons.h"
48 #include "BKE_library.h"
49 #include "BKE_main.h"
50 #include "BKE_object.h"
51 #include "BKE_scene.h"
52 #include "BKE_screen.h"
53
54 #include "ED_space_api.h"
55 #include "ED_screen.h"
56
57 #include "GPU_compositing.h"
58 #include "GPU_framebuffer.h"
59 #include "GPU_material.h"
60 #include "GPU_viewport.h"
61
62 #include "BIF_gl.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "RE_engine.h"
68 #include "RE_pipeline.h"
69
70 #include "RNA_access.h"
71
72 #include "UI_resources.h"
73
74 #ifdef WITH_PYTHON
75 #  include "BPY_extern.h"
76 #endif
77
78 #include "DEG_depsgraph.h"
79
80 #include "view3d_intern.h"  /* own include */
81
82 /* ******************** manage regions ********************* */
83
84 ARegion *view3d_has_buttons_region(ScrArea *sa)
85 {
86         ARegion *ar, *arnew;
87
88         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
89         if (ar) return ar;
90         
91         /* add subdiv level; after header */
92         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
93
94         /* is error! */
95         if (ar == NULL) return NULL;
96         
97         arnew = MEM_callocN(sizeof(ARegion), "buttons for view3d");
98         
99         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
100         arnew->regiontype = RGN_TYPE_UI;
101         arnew->alignment = RGN_ALIGN_RIGHT;
102         
103         arnew->flag = RGN_FLAG_HIDDEN;
104         
105         return arnew;
106 }
107
108 ARegion *view3d_has_tools_region(ScrArea *sa)
109 {
110         ARegion *ar, *artool = NULL, *arprops = NULL, *arhead;
111         
112         for (ar = sa->regionbase.first; ar; ar = ar->next) {
113                 if (ar->regiontype == RGN_TYPE_TOOLS)
114                         artool = ar;
115                 if (ar->regiontype == RGN_TYPE_TOOL_PROPS)
116                         arprops = ar;
117         }
118         
119         /* tool region hide/unhide also hides props */
120         if (arprops && artool) return artool;
121         
122         if (artool == NULL) {
123                 /* add subdiv level; after header */
124                 for (arhead = sa->regionbase.first; arhead; arhead = arhead->next)
125                         if (arhead->regiontype == RGN_TYPE_HEADER)
126                                 break;
127                 
128                 /* is error! */
129                 if (arhead == NULL) return NULL;
130                 
131                 artool = MEM_callocN(sizeof(ARegion), "tools for view3d");
132                 
133                 BLI_insertlinkafter(&sa->regionbase, arhead, artool);
134                 artool->regiontype = RGN_TYPE_TOOLS;
135                 artool->alignment = RGN_ALIGN_LEFT;
136                 artool->flag = RGN_FLAG_HIDDEN;
137         }
138
139         if (arprops == NULL) {
140                 /* add extra subdivided region for tool properties */
141                 arprops = MEM_callocN(sizeof(ARegion), "tool props for view3d");
142                 
143                 BLI_insertlinkafter(&sa->regionbase, artool, arprops);
144                 arprops->regiontype = RGN_TYPE_TOOL_PROPS;
145                 arprops->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
146         }
147         
148         return artool;
149 }
150
151 /* ****************************************************** */
152
153 /* function to always find a regionview3d context inside 3D window */
154 RegionView3D *ED_view3d_context_rv3d(bContext *C)
155 {
156         RegionView3D *rv3d = CTX_wm_region_view3d(C);
157         
158         if (rv3d == NULL) {
159                 ScrArea *sa = CTX_wm_area(C);
160                 if (sa && sa->spacetype == SPACE_VIEW3D) {
161                         ARegion *ar = BKE_area_find_region_active_win(sa);
162                         if (ar) {
163                                 rv3d = ar->regiondata;
164                         }
165                 }
166         }
167         return rv3d;
168 }
169
170 /* ideally would return an rv3d but in some cases the region is needed too
171  * so return that, the caller can then access the ar->regiondata */
172 bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_ar)
173 {
174         ScrArea *sa = CTX_wm_area(C);
175
176         *r_v3d = NULL;
177         *r_ar = NULL;
178
179         if (sa && sa->spacetype == SPACE_VIEW3D) {
180                 ARegion *ar = CTX_wm_region(C);
181                 View3D *v3d = (View3D *)sa->spacedata.first;
182
183                 if (ar) {
184                         RegionView3D *rv3d = ar->regiondata;
185                         if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) {
186                                 *r_v3d = v3d;
187                                 *r_ar = ar;
188                                 return true;
189                         }
190                         else {
191                                 ARegion *ar_unlock_user = NULL;
192                                 ARegion *ar_unlock = NULL;
193                                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
194                                         /* find the first unlocked rv3d */
195                                         if (ar->regiondata && ar->regiontype == RGN_TYPE_WINDOW) {
196                                                 rv3d = ar->regiondata;
197                                                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
198                                                         ar_unlock = ar;
199                                                         if (rv3d->persp == RV3D_PERSP || rv3d->persp == RV3D_CAMOB) {
200                                                                 ar_unlock_user = ar;
201                                                                 break;
202                                                         }
203                                                 }
204                                         }
205                                 }
206
207                                 /* camera/perspective view get priority when the active region is locked */
208                                 if (ar_unlock_user) {
209                                         *r_v3d = v3d;
210                                         *r_ar = ar_unlock_user;
211                                         return true;
212                                 }
213
214                                 if (ar_unlock) {
215                                         *r_v3d = v3d;
216                                         *r_ar = ar_unlock;
217                                         return true;
218                                 }
219                         }
220                 }
221         }
222
223         return false;
224 }
225
226 /* Most of the time this isn't needed since you could assume the view matrix was
227  * set while drawing, however when functions like mesh_foreachScreenVert are
228  * called by selection tools, we can't be sure this object was the last.
229  *
230  * for example, transparent objects are drawn after editmode and will cause
231  * the rv3d mat's to change and break selection.
232  *
233  * 'ED_view3d_init_mats_rv3d' should be called before
234  * view3d_project_short_clip and view3d_project_short_noclip in cases where
235  * these functions are not used during draw_object
236  */
237 void ED_view3d_init_mats_rv3d(struct Object *ob, struct RegionView3D *rv3d)
238 {
239         /* local viewmat and persmat, to calculate projections */
240         mul_m4_m4m4(rv3d->viewmatob, rv3d->viewmat, ob->obmat);
241         mul_m4_m4m4(rv3d->persmatob, rv3d->persmat, ob->obmat);
242
243         /* initializes object space clipping, speeds up clip tests */
244         ED_view3d_clipping_local(rv3d, ob->obmat);
245 }
246
247 void ED_view3d_init_mats_rv3d_gl(struct Object *ob, struct RegionView3D *rv3d)
248 {
249         ED_view3d_init_mats_rv3d(ob, rv3d);
250
251         /* we have to multiply instead of loading viewmatob to make
252          * it work with duplis using displists, otherwise it will
253          * override the dupli-matrix */
254         glMultMatrixf(ob->obmat);
255 }
256
257 #ifdef DEBUG
258 /* ensure we correctly initialize */
259 void ED_view3d_clear_mats_rv3d(struct RegionView3D *rv3d)
260 {
261         zero_m4(rv3d->viewmatob);
262         zero_m4(rv3d->persmatob);
263 }
264
265 void ED_view3d_check_mats_rv3d(struct RegionView3D *rv3d)
266 {
267         BLI_ASSERT_ZERO_M4(rv3d->viewmatob);
268         BLI_ASSERT_ZERO_M4(rv3d->persmatob);
269 }
270 #endif
271
272 void ED_view3d_stop_render_preview(wmWindowManager *wm, ARegion *ar)
273 {
274         RegionView3D *rv3d = ar->regiondata;
275
276         if (rv3d->render_engine) {
277 #ifdef WITH_PYTHON
278                 BPy_BEGIN_ALLOW_THREADS;
279 #endif
280
281                 WM_jobs_kill_type(wm, ar, WM_JOB_TYPE_RENDER_PREVIEW);
282
283 #ifdef WITH_PYTHON
284                 BPy_END_ALLOW_THREADS;
285 #endif
286
287                 if (rv3d->render_engine->re)
288                         RE_Database_Free(rv3d->render_engine->re);
289                 RE_engine_free(rv3d->render_engine);
290                 rv3d->render_engine = NULL;
291         }
292 }
293
294 void ED_view3d_shade_update(Main *bmain, Scene *scene, View3D *v3d, ScrArea *sa)
295 {
296         wmWindowManager *wm = bmain->wm.first;
297
298         if (v3d->drawtype != OB_RENDER) {
299                 ARegion *ar;
300
301                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
302                         if (ar->regiondata)
303                                 ED_view3d_stop_render_preview(wm, ar);
304                 }
305         }
306         else if (scene->obedit != NULL && scene->obedit->type == OB_MESH) {
307                 /* Tag mesh to load edit data. */
308                 DAG_id_tag_update(scene->obedit->data, 0);
309         }
310 }
311
312 /* ******************** default callbacks for view3d space ***************** */
313
314 static SpaceLink *view3d_new(const bContext *C)
315 {
316         Scene *scene = CTX_data_scene(C);
317         ARegion *ar;
318         View3D *v3d;
319         RegionView3D *rv3d;
320         
321         v3d = MEM_callocN(sizeof(View3D), "initview3d");
322         v3d->spacetype = SPACE_VIEW3D;
323         v3d->blockscale = 0.7f;
324         v3d->lay = v3d->layact = 1;
325         if (scene) {
326                 v3d->lay = v3d->layact = scene->lay;
327                 v3d->camera = scene->camera;
328         }
329         v3d->scenelock = true;
330         v3d->grid = 1.0f;
331         v3d->gridlines = 16;
332         v3d->gridsubdiv = 10;
333         v3d->drawtype = OB_SOLID;
334
335         v3d->gridflag = V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_FLOOR;
336         
337         v3d->flag = V3D_SELECT_OUTLINE;
338         v3d->flag2 = V3D_SHOW_RECONSTRUCTION | V3D_SHOW_GPENCIL;
339         
340         v3d->lens = 35.0f;
341         v3d->near = 0.01f;
342         v3d->far = 1000.0f;
343
344         v3d->twflag |= U.tw_flag & V3D_USE_MANIPULATOR;
345         v3d->twtype = V3D_MANIP_TRANSLATE;
346         v3d->around = V3D_AROUND_CENTER_MEAN;
347         
348         v3d->bundle_size = 0.2f;
349         v3d->bundle_drawtype = OB_PLAINAXES;
350
351         /* stereo */
352         v3d->stereo3d_camera = STEREO_3D_ID;
353         v3d->stereo3d_flag |= V3D_S3D_DISPPLANE;
354         v3d->stereo3d_convergence_alpha = 0.15f;
355         v3d->stereo3d_volume_alpha = 0.05f;
356
357         /* header */
358         ar = MEM_callocN(sizeof(ARegion), "header for view3d");
359         
360         BLI_addtail(&v3d->regionbase, ar);
361         ar->regiontype = RGN_TYPE_HEADER;
362         ar->alignment = RGN_ALIGN_BOTTOM;
363         
364         /* tool shelf */
365         ar = MEM_callocN(sizeof(ARegion), "toolshelf for view3d");
366         
367         BLI_addtail(&v3d->regionbase, ar);
368         ar->regiontype = RGN_TYPE_TOOLS;
369         ar->alignment = RGN_ALIGN_LEFT;
370         ar->flag = RGN_FLAG_HIDDEN;
371         
372         /* tool properties */
373         ar = MEM_callocN(sizeof(ARegion), "tool properties for view3d");
374         
375         BLI_addtail(&v3d->regionbase, ar);
376         ar->regiontype = RGN_TYPE_TOOL_PROPS;
377         ar->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
378         ar->flag = RGN_FLAG_HIDDEN;
379         
380         /* buttons/list view */
381         ar = MEM_callocN(sizeof(ARegion), "buttons for view3d");
382         
383         BLI_addtail(&v3d->regionbase, ar);
384         ar->regiontype = RGN_TYPE_UI;
385         ar->alignment = RGN_ALIGN_RIGHT;
386         ar->flag = RGN_FLAG_HIDDEN;
387         
388         /* main region */
389         ar = MEM_callocN(sizeof(ARegion), "main region for view3d");
390         
391         BLI_addtail(&v3d->regionbase, ar);
392         ar->regiontype = RGN_TYPE_WINDOW;
393         
394         ar->regiondata = MEM_callocN(sizeof(RegionView3D), "region view3d");
395         rv3d = ar->regiondata;
396         rv3d->viewquat[0] = 1.0f;
397         rv3d->persp = RV3D_PERSP;
398         rv3d->view = RV3D_VIEW_USER;
399         rv3d->dist = 10.0;
400         
401         return (SpaceLink *)v3d;
402 }
403
404 /* not spacelink itself */
405 static void view3d_free(SpaceLink *sl)
406 {
407         View3D *vd = (View3D *) sl;
408         BGpic *bgpic;
409
410         for (bgpic = vd->bgpicbase.first; bgpic; bgpic = bgpic->next) {
411                 if (bgpic->source == V3D_BGPIC_IMAGE) {
412                         id_us_min((ID *)bgpic->ima);
413                 }
414                 else if (bgpic->source == V3D_BGPIC_MOVIE) {
415                         id_us_min((ID *)bgpic->clip);
416                 }
417         }
418         BLI_freelistN(&vd->bgpicbase);
419
420         if (vd->localvd) MEM_freeN(vd->localvd);
421         
422         if (vd->properties_storage) MEM_freeN(vd->properties_storage);
423         
424         /* matcap material, its preview rect gets freed via icons */
425         if (vd->defmaterial) {
426                 if (vd->defmaterial->gpumaterial.first)
427                         GPU_material_free(&vd->defmaterial->gpumaterial);
428                 BKE_previewimg_free(&vd->defmaterial->preview);
429                 MEM_freeN(vd->defmaterial);
430         }
431
432         if (vd->fx_settings.ssao)
433                 MEM_freeN(vd->fx_settings.ssao);
434         if (vd->fx_settings.dof)
435                 MEM_freeN(vd->fx_settings.dof);
436 }
437
438
439 /* spacetype; init callback */
440 static void view3d_init(wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
441 {
442
443 }
444
445 static SpaceLink *view3d_duplicate(SpaceLink *sl)
446 {
447         View3D *v3do = (View3D *)sl;
448         View3D *v3dn = MEM_dupallocN(sl);
449         BGpic *bgpic;
450         
451         /* clear or remove stuff from old */
452
453         if (v3dn->localvd) {
454                 v3dn->localvd = NULL;
455                 v3dn->properties_storage = NULL;
456                 v3dn->lay = v3do->localvd->lay & 0xFFFFFF;
457         }
458
459         if (v3dn->drawtype == OB_RENDER)
460                 v3dn->drawtype = OB_SOLID;
461         
462         /* copy or clear inside new stuff */
463
464         v3dn->defmaterial = NULL;
465
466         BLI_duplicatelist(&v3dn->bgpicbase, &v3do->bgpicbase);
467         for (bgpic = v3dn->bgpicbase.first; bgpic; bgpic = bgpic->next) {
468                 if (bgpic->source == V3D_BGPIC_IMAGE) {
469                         id_us_plus((ID *)bgpic->ima);
470                 }
471                 else if (bgpic->source == V3D_BGPIC_MOVIE) {
472                         id_us_plus((ID *)bgpic->clip);
473                 }
474         }
475
476         v3dn->properties_storage = NULL;
477         if (v3dn->fx_settings.dof)
478                 v3dn->fx_settings.dof = MEM_dupallocN(v3do->fx_settings.dof);
479         if (v3dn->fx_settings.ssao)
480                 v3dn->fx_settings.ssao = MEM_dupallocN(v3do->fx_settings.ssao);
481
482         return (SpaceLink *)v3dn;
483 }
484
485 /* add handlers, stuff you only do once or on area/region changes */
486 static void view3d_main_region_init(wmWindowManager *wm, ARegion *ar)
487 {
488         ListBase *lb;
489         wmKeyMap *keymap;
490
491         /* object ops. */
492         
493         /* important to be before Pose keymap since they can both be enabled at once */
494         keymap = WM_keymap_find(wm->defaultconf, "Face Mask", 0, 0);
495         WM_event_add_keymap_handler(&ar->handlers, keymap);
496         
497         
498         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint Vertex Selection", 0, 0);
499         WM_event_add_keymap_handler(&ar->handlers, keymap);
500
501         /* pose is not modal, operator poll checks for this */
502         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
503         WM_event_add_keymap_handler(&ar->handlers, keymap);
504         
505         keymap = WM_keymap_find(wm->defaultconf, "Object Mode", 0, 0);
506         WM_event_add_keymap_handler(&ar->handlers, keymap);
507
508         keymap = WM_keymap_find(wm->defaultconf, "Paint Curve", 0, 0);
509         WM_event_add_keymap_handler(&ar->handlers, keymap);
510
511         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
512         WM_event_add_keymap_handler(&ar->handlers, keymap);
513
514         keymap = WM_keymap_find(wm->defaultconf, "Image Paint", 0, 0);
515         WM_event_add_keymap_handler(&ar->handlers, keymap);
516
517         keymap = WM_keymap_find(wm->defaultconf, "Vertex Paint", 0, 0);
518         WM_event_add_keymap_handler(&ar->handlers, keymap);
519
520         keymap = WM_keymap_find(wm->defaultconf, "Weight Paint", 0, 0);
521         WM_event_add_keymap_handler(&ar->handlers, keymap);
522
523         keymap = WM_keymap_find(wm->defaultconf, "Sculpt", 0, 0);
524         WM_event_add_keymap_handler(&ar->handlers, keymap);
525         
526         keymap = WM_keymap_find(wm->defaultconf, "Mesh", 0, 0);
527         WM_event_add_keymap_handler(&ar->handlers, keymap);
528         
529         keymap = WM_keymap_find(wm->defaultconf, "Curve", 0, 0);
530         WM_event_add_keymap_handler(&ar->handlers, keymap);
531         
532         keymap = WM_keymap_find(wm->defaultconf, "Armature", 0, 0);
533         WM_event_add_keymap_handler(&ar->handlers, keymap);
534
535         keymap = WM_keymap_find(wm->defaultconf, "Pose", 0, 0);
536         WM_event_add_keymap_handler(&ar->handlers, keymap);
537
538         keymap = WM_keymap_find(wm->defaultconf, "Metaball", 0, 0);
539         WM_event_add_keymap_handler(&ar->handlers, keymap);
540         
541         keymap = WM_keymap_find(wm->defaultconf, "Lattice", 0, 0);
542         WM_event_add_keymap_handler(&ar->handlers, keymap);
543
544         keymap = WM_keymap_find(wm->defaultconf, "Particle", 0, 0);
545         WM_event_add_keymap_handler(&ar->handlers, keymap);
546
547         /* editfont keymap swallows all... */
548         keymap = WM_keymap_find(wm->defaultconf, "Font", 0, 0);
549         WM_event_add_keymap_handler(&ar->handlers, keymap);
550
551         keymap = WM_keymap_find(wm->defaultconf, "Object Non-modal", 0, 0);
552         WM_event_add_keymap_handler(&ar->handlers, keymap);
553
554         keymap = WM_keymap_find(wm->defaultconf, "Frames", 0, 0);
555         WM_event_add_keymap_handler(&ar->handlers, keymap);
556
557         /* own keymap, last so modes can override it */
558         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
559         WM_event_add_keymap_handler(&ar->handlers, keymap);
560
561         keymap = WM_keymap_find(wm->defaultconf, "3D View", SPACE_VIEW3D, 0);
562         WM_event_add_keymap_handler(&ar->handlers, keymap);
563         
564         /* add drop boxes */
565         lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
566         
567         WM_event_add_dropbox_handler(&ar->handlers, lb);
568         
569 }
570
571 static void view3d_main_region_exit(wmWindowManager *wm, ARegion *ar)
572 {
573         RegionView3D *rv3d = ar->regiondata;
574
575         ED_view3d_stop_render_preview(wm, ar);
576
577         if (rv3d->gpuoffscreen) {
578                 GPU_offscreen_free(rv3d->gpuoffscreen);
579                 rv3d->gpuoffscreen = NULL;
580         }
581         
582         if (rv3d->compositor) {
583                 GPU_fx_compositor_destroy(rv3d->compositor);
584                 rv3d->compositor = NULL;
585         }
586
587         if (rv3d->viewport) {
588                 GPU_viewport_free(rv3d->viewport);
589                 MEM_freeN(rv3d->viewport);
590                 rv3d->viewport = NULL;
591         }
592 }
593
594 static int view3d_ob_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
595 {
596         if (drag->type == WM_DRAG_ID) {
597                 ID *id = drag->poin;
598                 if (GS(id->name) == ID_OB)
599                         return 1;
600         }
601         return 0;
602 }
603
604 static int view3d_group_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
605 {
606         if (drag->type == WM_DRAG_ID) {
607                 ID *id = drag->poin;
608                 if (GS(id->name) == ID_GR)
609                         return 1;
610         }
611         return 0;
612 }
613
614 static int view3d_mat_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
615 {
616         if (drag->type == WM_DRAG_ID) {
617                 ID *id = drag->poin;
618                 if (GS(id->name) == ID_MA)
619                         return 1;
620         }
621         return 0;
622 }
623
624 static int view3d_ima_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
625 {
626         if (drag->type == WM_DRAG_ID) {
627                 ID *id = drag->poin;
628                 if (GS(id->name) == ID_IM)
629                         return 1;
630         }
631         else if (drag->type == WM_DRAG_PATH) {
632                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE))   /* rule might not work? */
633                         return 1;
634         }
635         return 0;
636 }
637
638 static int view3d_ima_bg_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
639 {
640         if (event->ctrl)
641                 return false;
642
643         if (!ED_view3d_give_base_under_cursor(C, event->mval)) {
644                 return view3d_ima_drop_poll(C, drag, event);
645         }
646         return 0;
647 }
648
649 static int view3d_ima_empty_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
650 {
651         BaseLegacy *base = ED_view3d_give_base_under_cursor(C, event->mval);
652
653         /* either holding and ctrl and no object, or dropping to empty */
654         if (((base == NULL) && event->ctrl) ||
655             ((base != NULL) && base->object->type == OB_EMPTY))
656         {
657                 return view3d_ima_drop_poll(C, drag, event);
658         }
659
660         return 0;
661 }
662
663 static int view3d_ima_mesh_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
664 {
665         BaseLegacy *base = ED_view3d_give_base_under_cursor(C, event->mval);
666
667         if (base && base->object->type == OB_MESH)
668                 return view3d_ima_drop_poll(C, drag, event);
669         return 0;
670 }
671
672 static void view3d_ob_drop_copy(wmDrag *drag, wmDropBox *drop)
673 {
674         ID *id = drag->poin;
675
676         RNA_string_set(drop->ptr, "name", id->name + 2);
677 }
678
679 static void view3d_group_drop_copy(wmDrag *drag, wmDropBox *drop)
680 {
681         ID *id = drag->poin;
682         
683         drop->opcontext = WM_OP_EXEC_DEFAULT;
684         RNA_string_set(drop->ptr, "name", id->name + 2);
685 }
686
687 static void view3d_id_drop_copy(wmDrag *drag, wmDropBox *drop)
688 {
689         ID *id = drag->poin;
690         
691         RNA_string_set(drop->ptr, "name", id->name + 2);
692 }
693
694 static void view3d_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
695 {
696         ID *id = drag->poin;
697         
698         if (id) {
699                 RNA_string_set(drop->ptr, "name", id->name + 2);
700                 RNA_struct_property_unset(drop->ptr, "filepath");
701         }
702         else if (drag->path[0]) {
703                 RNA_string_set(drop->ptr, "filepath", drag->path);
704                 RNA_struct_property_unset(drop->ptr, "image");
705         }
706 }
707
708
709 /* region dropbox definition */
710 static void view3d_dropboxes(void)
711 {
712         ListBase *lb = WM_dropboxmap_find("View3D", SPACE_VIEW3D, RGN_TYPE_WINDOW);
713         
714         WM_dropbox_add(lb, "OBJECT_OT_add_named", view3d_ob_drop_poll, view3d_ob_drop_copy);
715         WM_dropbox_add(lb, "OBJECT_OT_drop_named_material", view3d_mat_drop_poll, view3d_id_drop_copy);
716         WM_dropbox_add(lb, "MESH_OT_drop_named_image", view3d_ima_mesh_drop_poll, view3d_id_path_drop_copy);
717         WM_dropbox_add(lb, "OBJECT_OT_drop_named_image", view3d_ima_empty_drop_poll, view3d_id_path_drop_copy);
718         WM_dropbox_add(lb, "VIEW3D_OT_background_image_add", view3d_ima_bg_drop_poll, view3d_id_path_drop_copy);
719         WM_dropbox_add(lb, "OBJECT_OT_group_instance_add", view3d_group_drop_poll, view3d_group_drop_copy);     
720 }
721
722
723
724 /* type callback, not region itself */
725 static void view3d_main_region_free(ARegion *ar)
726 {
727         RegionView3D *rv3d = ar->regiondata;
728         
729         if (rv3d) {
730                 if (rv3d->localvd) MEM_freeN(rv3d->localvd);
731                 if (rv3d->clipbb) MEM_freeN(rv3d->clipbb);
732
733                 if (rv3d->render_engine)
734                         RE_engine_free(rv3d->render_engine);
735                 
736                 if (rv3d->depths) {
737                         if (rv3d->depths->depths) MEM_freeN(rv3d->depths->depths);
738                         MEM_freeN(rv3d->depths);
739                 }
740                 if (rv3d->sms) {
741                         MEM_freeN(rv3d->sms);
742                 }
743                 if (rv3d->gpuoffscreen) {
744                         GPU_offscreen_free(rv3d->gpuoffscreen);
745                 }
746                 if (rv3d->compositor) {
747                         GPU_fx_compositor_destroy(rv3d->compositor);
748                 }
749                 if (rv3d->viewport) {
750                         GPU_viewport_free(rv3d->viewport);
751                         MEM_freeN(rv3d->viewport);
752                 }
753
754                 MEM_freeN(rv3d);
755                 ar->regiondata = NULL;
756         }
757 }
758
759 /* copy regiondata */
760 static void *view3d_main_region_duplicate(void *poin)
761 {
762         if (poin) {
763                 RegionView3D *rv3d = poin, *new;
764         
765                 new = MEM_dupallocN(rv3d);
766                 if (rv3d->localvd)
767                         new->localvd = MEM_dupallocN(rv3d->localvd);
768                 if (rv3d->clipbb)
769                         new->clipbb = MEM_dupallocN(rv3d->clipbb);
770                 
771                 new->depths = NULL;
772                 new->gpuoffscreen = NULL;
773                 new->render_engine = NULL;
774                 new->sms = NULL;
775                 new->smooth_timer = NULL;
776                 new->compositor = NULL;
777                 new->viewport = NULL;
778                 
779                 return new;
780         }
781         return NULL;
782 }
783
784 static void view3d_recalc_used_layers(ARegion *ar, wmNotifier *wmn, Scene *scene)
785 {
786         wmWindow *win = wmn->wm->winactive;
787         ScrArea *sa;
788         unsigned int lay_used = 0;
789         BaseLegacy *base;
790
791         if (!win) return;
792
793         base = scene->base.first;
794         while (base) {
795                 lay_used |= base->lay & ((1 << 20) - 1); /* ignore localview */
796
797                 if (lay_used == (1 << 20) - 1)
798                         break;
799
800                 base = base->next;
801         }
802
803         for (sa = win->screen->areabase.first; sa; sa = sa->next) {
804                 if (sa->spacetype == SPACE_VIEW3D) {
805                         if (BLI_findindex(&sa->regionbase, ar) != -1) {
806                                 View3D *v3d = sa->spacedata.first;
807                                 v3d->lay_used = lay_used;
808                                 break;
809                         }
810                 }
811         }
812 }
813
814 static void view3d_main_region_listener(bScreen *sc, ScrArea *sa, ARegion *ar, wmNotifier *wmn)
815 {
816         Scene *scene = sc->scene;
817         View3D *v3d = sa->spacedata.first;
818         
819         /* context changes */
820         switch (wmn->category) {
821                 case NC_ANIMATION:
822                         switch (wmn->data) {
823                                 case ND_KEYFRAME_PROP:
824                                 case ND_NLA_ACTCHANGE:
825                                         ED_region_tag_redraw(ar);
826                                         break;
827                                 case ND_NLA:
828                                 case ND_KEYFRAME:
829                                         if (ELEM(wmn->action, NA_EDITED, NA_ADDED, NA_REMOVED))
830                                                 ED_region_tag_redraw(ar);
831                                         break;
832                                 case ND_ANIMCHAN:
833                                         if (wmn->action == NA_SELECTED)
834                                                 ED_region_tag_redraw(ar);
835                                         break;
836                         }
837                         break;
838                 case NC_SCENE:
839                         switch (wmn->data) {
840                                 case ND_LAYER_CONTENT:
841                                         if (wmn->reference)
842                                                 view3d_recalc_used_layers(ar, wmn, wmn->reference);
843                                         ED_region_tag_redraw(ar);
844                                         break;
845                                 case ND_FRAME:
846                                 case ND_TRANSFORM:
847                                 case ND_OB_ACTIVE:
848                                 case ND_OB_SELECT:
849                                 case ND_OB_VISIBLE:
850                                 case ND_LAYER:
851                                 case ND_RENDER_OPTIONS:
852                                 case ND_MARKERS:
853                                 case ND_MODE:
854                                         ED_region_tag_redraw(ar);
855                                         break;
856                                 case ND_WORLD:
857                                         /* handled by space_view3d_listener() for v3d access */
858                                         break;
859                                 case ND_DRAW_RENDER_VIEWPORT:
860                                 {
861                                         if (v3d->camera && (scene == wmn->reference)) {
862                                                 RegionView3D *rv3d = ar->regiondata;
863                                                 if (rv3d->persp == RV3D_CAMOB) {
864                                                         ED_region_tag_redraw(ar);
865                                                 }
866                                         }
867                                         break;
868                                 }
869                         }
870                         if (wmn->action == NA_EDITED)
871                                 ED_region_tag_redraw(ar);
872                         break;
873                 case NC_OBJECT:
874                         switch (wmn->data) {
875                                 case ND_BONE_ACTIVE:
876                                 case ND_BONE_SELECT:
877                                 case ND_TRANSFORM:
878                                 case ND_POSE:
879                                 case ND_DRAW:
880                                 case ND_MODIFIER:
881                                 case ND_CONSTRAINT:
882                                 case ND_KEYS:
883                                 case ND_PARTICLE:
884                                 case ND_LOD:
885                                         ED_region_tag_redraw(ar);
886                                         break;
887                         }
888                         switch (wmn->action) {
889                                 case NA_ADDED:
890                                         ED_region_tag_redraw(ar);
891                                         break;
892                         }
893                         break;
894                 case NC_GEOM:
895                         switch (wmn->data) {
896                                 case ND_DATA:
897                                 case ND_VERTEX_GROUP:
898                                 case ND_SELECT:
899                                         ED_region_tag_redraw(ar);
900                                         break;
901                         }
902                         switch (wmn->action) {
903                                 case NA_EDITED:
904                                         ED_region_tag_redraw(ar);
905                                         break;
906                         }
907                         break;
908                 case NC_CAMERA:
909                         switch (wmn->data) {
910                                 case ND_DRAW_RENDER_VIEWPORT:
911                                 {
912                                         if (v3d->camera && (v3d->camera->data == wmn->reference)) {
913                                                 RegionView3D *rv3d = ar->regiondata;
914                                                 if (rv3d->persp == RV3D_CAMOB) {
915                                                         ED_region_tag_redraw(ar);
916                                                 }
917                                         }
918                                         break;
919                                 }
920                         }
921                         break;
922                 case NC_GROUP:
923                         /* all group ops for now */
924                         ED_region_tag_redraw(ar);
925                         break;
926                 case NC_BRUSH:
927                         switch (wmn->action) {
928                                 case NA_EDITED:
929                                         ED_region_tag_redraw_overlay(ar);
930                                         break;
931                                 case NA_SELECTED:
932                                         /* used on brush changes - needed because 3d cursor
933                                          * has to be drawn if clone brush is selected */
934                                         ED_region_tag_redraw(ar);
935                                         break;
936                         }
937                         break;
938                 case NC_MATERIAL:
939                         switch (wmn->data) {
940                                 case ND_SHADING:
941                                 case ND_NODES:
942                                         /* TODO(sergey) This is a bit too much updates, but needed to
943                                          * have proper material drivers update in the viewport.
944                                          *
945                                          * How to solve?
946                                          */
947                                         ED_region_tag_redraw(ar);
948                                         break;
949                                 case ND_SHADING_DRAW:
950                                 case ND_SHADING_LINKS:
951                                         ED_region_tag_redraw(ar);
952                                         break;
953                         }
954                         break;
955                 case NC_WORLD:
956                         switch (wmn->data) {
957                                 case ND_WORLD_DRAW:
958                                         /* handled by space_view3d_listener() for v3d access */
959                                         break;
960                         }
961                         break;
962                 case NC_LAMP:
963                         switch (wmn->data) {
964                                 case ND_LIGHTING:
965                                         /* TODO(sergey): This is a bit too much, but needed to
966                                          * handle updates from new depsgraph.
967                                          */
968                                         ED_region_tag_redraw(ar);
969                                         break;
970                                 case ND_LIGHTING_DRAW:
971                                         ED_region_tag_redraw(ar);
972                                         break;
973                         }
974                         break;
975                 case NC_IMAGE:
976                         /* this could be more fine grained checks if we had
977                          * more context than just the region */
978                         ED_region_tag_redraw(ar);
979                         break;
980                 case NC_TEXTURE:
981                         /* same as above */
982                         ED_region_tag_redraw(ar);
983                         break;
984                 case NC_MOVIECLIP:
985                         if (wmn->data == ND_DISPLAY || wmn->action == NA_EDITED)
986                                 ED_region_tag_redraw(ar);
987                         break;
988                 case NC_SPACE:
989                         if (wmn->data == ND_SPACE_VIEW3D) {
990                                 if (wmn->subtype == NS_VIEW3D_GPU) {
991                                         RegionView3D *rv3d = ar->regiondata;
992                                         rv3d->rflag |= RV3D_GPULIGHT_UPDATE;
993                                 }
994                                 ED_region_tag_redraw(ar);
995                         }
996                         break;
997                 case NC_ID:
998                         if (wmn->action == NA_RENAME)
999                                 ED_region_tag_redraw(ar);
1000                         break;
1001                 case NC_SCREEN:
1002                         switch (wmn->data) {
1003                                 case ND_ANIMPLAY:
1004                                 case ND_SKETCH:
1005                                         ED_region_tag_redraw(ar);
1006                                         break;
1007                                 case ND_SCREENBROWSE:
1008                                 case ND_SCREENDELETE:
1009                                 case ND_SCREENSET:
1010                                         /* screen was changed, need to update used layers due to NC_SCENE|ND_LAYER_CONTENT */
1011                                         /* updates used layers only for View3D in active screen */
1012                                         if (wmn->reference) {
1013                                                 bScreen *sc_ref = wmn->reference;
1014                                                 view3d_recalc_used_layers(ar, wmn, sc_ref->scene);
1015                                         }
1016                                         ED_region_tag_redraw(ar);
1017                                         break;
1018                         }
1019
1020                         break;
1021                 case NC_GPENCIL:
1022                         if (wmn->data == ND_DATA || ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
1023                                 ED_region_tag_redraw(ar);
1024                         }
1025                         break;
1026         }
1027 }
1028
1029 /* concept is to retrieve cursor type context-less */
1030 static void view3d_main_region_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
1031 {
1032         Scene *scene = win->screen->scene;
1033
1034         if (scene->obedit) {
1035                 WM_cursor_set(win, CURSOR_EDIT);
1036         }
1037         else {
1038                 WM_cursor_set(win, CURSOR_STD);
1039         }
1040 }
1041
1042 /* add handlers, stuff you only do once or on area/region changes */
1043 static void view3d_header_region_init(wmWindowManager *wm, ARegion *ar)
1044 {
1045         wmKeyMap *keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1046         
1047         WM_event_add_keymap_handler(&ar->handlers, keymap);
1048
1049         ED_region_header_init(ar);
1050 }
1051
1052 static void view3d_header_region_draw(const bContext *C, ARegion *ar)
1053 {
1054         ED_region_header(C, ar);
1055 }
1056
1057 static void view3d_header_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
1058 {
1059         /* context changes */
1060         switch (wmn->category) {
1061                 case NC_SCENE:
1062                         switch (wmn->data) {
1063                                 case ND_FRAME:
1064                                 case ND_OB_ACTIVE:
1065                                 case ND_OB_SELECT:
1066                                 case ND_OB_VISIBLE:
1067                                 case ND_MODE:
1068                                 case ND_LAYER:
1069                                 case ND_TOOLSETTINGS:
1070                                 case ND_LAYER_CONTENT:
1071                                 case ND_RENDER_OPTIONS:
1072                                         ED_region_tag_redraw(ar);
1073                                         break;
1074                         }
1075                         break;
1076                 case NC_SPACE:
1077                         if (wmn->data == ND_SPACE_VIEW3D)
1078                                 ED_region_tag_redraw(ar);
1079                         break;
1080                 case NC_GPENCIL:
1081                         if (wmn->data & ND_GPENCIL_EDITMODE)
1082                                 ED_region_tag_redraw(ar);
1083                         break;
1084         }
1085 }
1086
1087 /* add handlers, stuff you only do once or on area/region changes */
1088 static void view3d_buttons_region_init(wmWindowManager *wm, ARegion *ar)
1089 {
1090         wmKeyMap *keymap;
1091
1092         ED_region_panels_init(wm, ar);
1093         
1094         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1095         WM_event_add_keymap_handler(&ar->handlers, keymap);
1096 }
1097
1098 static void view3d_buttons_region_draw(const bContext *C, ARegion *ar)
1099 {
1100         ED_region_panels(C, ar, NULL, -1, true);
1101 }
1102
1103 static void view3d_buttons_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
1104 {
1105         /* context changes */
1106         switch (wmn->category) {
1107                 case NC_ANIMATION:
1108                         switch (wmn->data) {
1109                                 case ND_KEYFRAME_PROP:
1110                                 case ND_NLA_ACTCHANGE:
1111                                         ED_region_tag_redraw(ar);
1112                                         break;
1113                                 case ND_NLA:
1114                                 case ND_KEYFRAME:
1115                                         if (ELEM(wmn->action, NA_EDITED, NA_ADDED, NA_REMOVED))
1116                                                 ED_region_tag_redraw(ar);
1117                                         break;
1118                         }
1119                         break;
1120                 case NC_SCENE:
1121                         switch (wmn->data) {
1122                                 case ND_FRAME:
1123                                 case ND_OB_ACTIVE:
1124                                 case ND_OB_SELECT:
1125                                 case ND_OB_VISIBLE:
1126                                 case ND_MODE:
1127                                 case ND_LAYER:
1128                                 case ND_LAYER_CONTENT:
1129                                 case ND_TOOLSETTINGS:
1130                                         ED_region_tag_redraw(ar);
1131                                         break;
1132                         }
1133                         switch (wmn->action) {
1134                                 case NA_EDITED:
1135                                         ED_region_tag_redraw(ar);
1136                                         break;
1137                         }
1138                         break;
1139                 case NC_OBJECT:
1140                         switch (wmn->data) {
1141                                 case ND_BONE_ACTIVE:
1142                                 case ND_BONE_SELECT:
1143                                 case ND_TRANSFORM:
1144                                 case ND_POSE:
1145                                 case ND_DRAW:
1146                                 case ND_KEYS:
1147                                 case ND_MODIFIER:
1148                                         ED_region_tag_redraw(ar);
1149                                         break;
1150                         }
1151                         break;
1152                 case NC_GEOM:
1153                         switch (wmn->data) {
1154                                 case ND_DATA:
1155                                 case ND_VERTEX_GROUP:
1156                                 case ND_SELECT:
1157                                         ED_region_tag_redraw(ar);
1158                                         break;
1159                         }
1160                         if (wmn->action == NA_EDITED)
1161                                 ED_region_tag_redraw(ar);
1162                         break;
1163                 case NC_TEXTURE:
1164                 case NC_MATERIAL:
1165                         /* for brush textures */
1166                         ED_region_tag_redraw(ar);
1167                         break;
1168                 case NC_BRUSH:
1169                         /* NA_SELECTED is used on brush changes */
1170                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED))
1171                                 ED_region_tag_redraw(ar);
1172                         break;
1173                 case NC_SPACE:
1174                         if (wmn->data == ND_SPACE_VIEW3D)
1175                                 ED_region_tag_redraw(ar);
1176                         break;
1177                 case NC_ID:
1178                         if (wmn->action == NA_RENAME)
1179                                 ED_region_tag_redraw(ar);
1180                         break;
1181                 case NC_GPENCIL:
1182                         if ((wmn->data & (ND_DATA | ND_GPENCIL_EDITMODE)) || (wmn->action == NA_EDITED))
1183                                 ED_region_tag_redraw(ar);
1184                         break;
1185                 case NC_IMAGE:
1186                         /* Update for the image layers in texture paint. */
1187                         if (wmn->action == NA_EDITED)
1188                                 ED_region_tag_redraw(ar);
1189                         break;
1190         }
1191 }
1192
1193 /* add handlers, stuff you only do once or on area/region changes */
1194 static void view3d_tools_region_init(wmWindowManager *wm, ARegion *ar)
1195 {
1196         wmKeyMap *keymap;
1197         
1198         ED_region_panels_init(wm, ar);
1199
1200         keymap = WM_keymap_find(wm->defaultconf, "3D View Generic", SPACE_VIEW3D, 0);
1201         WM_event_add_keymap_handler(&ar->handlers, keymap);
1202 }
1203
1204 static void view3d_tools_region_draw(const bContext *C, ARegion *ar)
1205 {
1206         ED_region_panels(C, ar, CTX_data_mode_string(C), -1, true);
1207 }
1208
1209 static void view3d_props_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
1210 {
1211         /* context changes */
1212         switch (wmn->category) {
1213                 case NC_WM:
1214                         if (wmn->data == ND_HISTORY)
1215                                 ED_region_tag_redraw(ar);
1216                         break;
1217                 case NC_SCENE:
1218                         if (wmn->data == ND_MODE)
1219                                 ED_region_tag_redraw(ar);
1220                         break;
1221                 case NC_SPACE:
1222                         if (wmn->data == ND_SPACE_VIEW3D)
1223                                 ED_region_tag_redraw(ar);
1224                         break;
1225         }
1226 }
1227
1228 /* area (not region) level listener */
1229 static void space_view3d_listener(bScreen *UNUSED(sc), ScrArea *sa, struct wmNotifier *wmn)
1230 {
1231         View3D *v3d = sa->spacedata.first;
1232
1233         /* context changes */
1234         switch (wmn->category) {
1235                 case NC_SCENE:
1236                         switch (wmn->data) {
1237                                 case ND_WORLD:
1238                                         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1239                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1240                                         break;
1241                         }
1242                         break;
1243                 case NC_WORLD:
1244                         switch (wmn->data) {
1245                                 case ND_WORLD_DRAW:
1246                                 case ND_WORLD:
1247                                         if (v3d->flag3 & V3D_SHOW_WORLD)
1248                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1249                                         break;
1250                         }
1251                         break;
1252                 case NC_MATERIAL:
1253                         switch (wmn->data) {
1254                                 case ND_NODES:
1255                                         if (v3d->drawtype == OB_TEXTURE)
1256                                                 ED_area_tag_redraw_regiontype(sa, RGN_TYPE_WINDOW);
1257                                         break;
1258                         }
1259                         break;
1260         }
1261
1262         /* removed since BKE_image_user_frame_calc is now called in view3d_draw_bgpic because screen_ops doesnt call the notifier. */
1263 #if 0
1264         if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) {
1265                 View3D *v3d = area->spacedata.first;
1266                 BGpic *bgpic = v3d->bgpicbase.first;
1267
1268                 for (; bgpic; bgpic = bgpic->next) {
1269                         if (bgpic->ima) {
1270                                 Scene *scene = wmn->reference;
1271                                 BKE_image_user_frame_calc(&bgpic->iuser, scene->r.cfra, 0);
1272                         }
1273                 }
1274         }
1275 #endif
1276 }
1277
1278 const char *view3d_context_dir[] = {
1279         "active_base", "active_object", NULL
1280 };
1281
1282 static int view3d_context(const bContext *C, const char *member, bContextDataResult *result)
1283 {
1284         /* fallback to the scene layer, allows duplicate and other object operators to run outside the 3d view */
1285
1286         if (CTX_data_dir(member)) {
1287                 CTX_data_dir_set(result, view3d_context_dir);
1288         }
1289         else if (CTX_data_equals(member, "active_base")) {
1290                 Scene *scene = CTX_data_scene(C);
1291                 SceneLayer *sl = CTX_data_scene_layer(C);
1292                 if (sl->basact) {
1293                         Object *ob = sl->basact->object;
1294                         /* if hidden but in edit mode, we still display, can happen with animation */
1295                         if ((sl->basact->flag & BASE_VISIBLED) != 0 || (ob->mode & OB_MODE_EDIT)) {
1296                                 CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, sl->basact);
1297                         }
1298                 }
1299                 
1300                 return 1;
1301         }
1302         else if (CTX_data_equals(member, "active_object")) {
1303                 SceneLayer *sl = CTX_data_scene_layer(C);
1304                 if (sl->basact) {
1305                         Object *ob = sl->basact->object;
1306                         /* if hidden but in edit mode, we still display, can happen with animation */
1307                         if ((sl->basact->flag & BASE_VISIBLED) != 0 || (ob->mode & OB_MODE_EDIT) != 0) {
1308                                 CTX_data_id_pointer_set(result, &ob->id);
1309                         }
1310                 }
1311                 
1312                 return 1;
1313         }
1314         else {
1315                 return 0; /* not found */
1316         }
1317
1318         return -1; /* found but not available */
1319 }
1320
1321 static void view3d_id_remap(ScrArea *sa, SpaceLink *slink, ID *old_id, ID *new_id)
1322 {
1323         View3D *v3d;
1324         ARegion *ar;
1325         bool is_local = false;
1326
1327         if (!ELEM(GS(old_id->name), ID_OB, ID_MA, ID_IM, ID_MC)) {
1328                 return;
1329         }
1330
1331         for (v3d = (View3D *)slink; v3d; v3d = v3d->localvd, is_local = true) {
1332                 if ((ID *)v3d->camera == old_id) {
1333                         v3d->camera = (Object *)new_id;
1334                         if (!new_id) {
1335                                 /* 3D view might be inactive, in that case needs to use slink->regionbase */
1336                                 ListBase *regionbase = (slink == sa->spacedata.first) ? &sa->regionbase : &slink->regionbase;
1337                                 for (ar = regionbase->first; ar; ar = ar->next) {
1338                                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1339                                                 RegionView3D *rv3d = is_local ? ((RegionView3D *)ar->regiondata)->localvd : ar->regiondata;
1340                                                 if (rv3d && (rv3d->persp == RV3D_CAMOB)) {
1341                                                         rv3d->persp = RV3D_PERSP;
1342                                                 }
1343                                         }
1344                                 }
1345                         }
1346                 }
1347                 if ((ID *)v3d->ob_centre == old_id) {
1348                         v3d->ob_centre = (Object *)new_id;
1349                         if (new_id == NULL) {  /* Otherwise, bonename may remain valid... We could be smart and check this, too? */
1350                                 v3d->ob_centre_bone[0] = '\0';
1351                         }
1352                 }
1353
1354                 if ((ID *)v3d->defmaterial == old_id) {
1355                         v3d->defmaterial = (Material *)new_id;
1356                 }
1357 #if 0  /* XXX Deprecated? */
1358                 if ((ID *)v3d->gpd == old_id) {
1359                         v3d->gpd = (bGPData *)new_id;
1360                 }
1361 #endif
1362
1363                 if (ELEM(GS(old_id->name), ID_IM, ID_MC)) {
1364                         for (BGpic *bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
1365                                 if ((ID *)bgpic->ima == old_id) {
1366                                         bgpic->ima = (Image *)new_id;
1367                                         id_us_min(old_id);
1368                                         id_us_plus(new_id);
1369                                 }
1370                                 if ((ID *)bgpic->clip == old_id) {
1371                                         bgpic->clip = (MovieClip *)new_id;
1372                                         id_us_min(old_id);
1373                                         id_us_plus(new_id);
1374                                 }
1375                         }
1376                 }
1377
1378                 if (is_local) {
1379                         break;
1380                 }
1381         }
1382 }
1383
1384 /* only called once, from space/spacetypes.c */
1385 void ED_spacetype_view3d(void)
1386 {
1387         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype view3d");
1388         ARegionType *art;
1389         
1390         st->spaceid = SPACE_VIEW3D;
1391         strncpy(st->name, "View3D", BKE_ST_MAXNAME);
1392         
1393         st->new = view3d_new;
1394         st->free = view3d_free;
1395         st->init = view3d_init;
1396         st->listener = space_view3d_listener;
1397         st->duplicate = view3d_duplicate;
1398         st->operatortypes = view3d_operatortypes;
1399         st->keymap = view3d_keymap;
1400         st->dropboxes = view3d_dropboxes;
1401         st->context = view3d_context;
1402         st->id_remap = view3d_id_remap;
1403
1404         /* regions: main window */
1405         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d main region");
1406         art->regionid = RGN_TYPE_WINDOW;
1407         art->keymapflag = ED_KEYMAP_GPENCIL;
1408         art->draw = view3d_main_region_draw;
1409         art->init = view3d_main_region_init;
1410         art->exit = view3d_main_region_exit;
1411         art->free = view3d_main_region_free;
1412         art->duplicate = view3d_main_region_duplicate;
1413         art->listener = view3d_main_region_listener;
1414         art->cursor = view3d_main_region_cursor;
1415         art->lock = 1;   /* can become flag, see BKE_spacedata_draw_locks */
1416         BLI_addhead(&st->regiontypes, art);
1417         
1418         /* regions: listview/buttons */
1419         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d buttons region");
1420         art->regionid = RGN_TYPE_UI;
1421         art->prefsizex = 180; /* XXX */
1422         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1423         art->listener = view3d_buttons_region_listener;
1424         art->init = view3d_buttons_region_init;
1425         art->draw = view3d_buttons_region_draw;
1426         BLI_addhead(&st->regiontypes, art);
1427
1428         view3d_buttons_register(art);
1429
1430         /* regions: tool(bar) */
1431         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tools region");
1432         art->regionid = RGN_TYPE_TOOLS;
1433         art->prefsizex = 160; /* XXX */
1434         art->prefsizey = 50; /* XXX */
1435         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1436         art->listener = view3d_buttons_region_listener;
1437         art->init = view3d_tools_region_init;
1438         art->draw = view3d_tools_region_draw;
1439         BLI_addhead(&st->regiontypes, art);
1440         
1441 #if 0
1442         /* unfinished still */
1443         view3d_toolshelf_register(art);
1444 #endif
1445
1446         /* regions: tool properties */
1447         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d tool properties region");
1448         art->regionid = RGN_TYPE_TOOL_PROPS;
1449         art->prefsizex = 0;
1450         art->prefsizey = 120;
1451         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
1452         art->listener = view3d_props_region_listener;
1453         art->init = view3d_tools_region_init;
1454         art->draw = view3d_tools_region_draw;
1455         BLI_addhead(&st->regiontypes, art);
1456         
1457         view3d_tool_props_register(art);
1458         
1459         
1460         /* regions: header */
1461         art = MEM_callocN(sizeof(ARegionType), "spacetype view3d header region");
1462         art->regionid = RGN_TYPE_HEADER;
1463         art->prefsizey = HEADERY;
1464         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
1465         art->listener = view3d_header_region_listener;
1466         art->init = view3d_header_region_init;
1467         art->draw = view3d_header_region_draw;
1468         BLI_addhead(&st->regiontypes, art);
1469         
1470         BKE_spacetype_register(st);
1471 }
1472