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