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