2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/screen.c
33 # include "BLI_winstuff.h"
40 #include "MEM_guardedalloc.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_space_types.h"
45 #include "DNA_view3d_types.h"
47 #include "BLI_listbase.h"
48 #include "BLI_utildefines.h"
50 #include "BKE_screen.h"
52 /* ************ Spacetype/regiontype handling ************** */
54 /* keep global; this has to be accessible outside of windowmanager */
55 static ListBase spacetypes = {NULL, NULL};
57 /* not SpaceType itself */
58 static void spacetype_free(SpaceType *st)
64 for (art = st->regiontypes.first; art; art = art->next) {
65 BLI_freelistN(&art->drawcalls);
67 for (pt = art->paneltypes.first; pt; pt = pt->next)
69 pt->ext.free(pt->ext.data);
71 for (ht = art->headertypes.first; ht; ht = ht->next)
73 ht->ext.free(ht->ext.data);
75 BLI_freelistN(&art->paneltypes);
76 BLI_freelistN(&art->headertypes);
79 BLI_freelistN(&st->regiontypes);
80 BLI_freelistN(&st->toolshelf);
84 void BKE_spacetypes_free(void)
88 for (st = spacetypes.first; st; st = st->next) {
92 BLI_freelistN(&spacetypes);
95 SpaceType *BKE_spacetype_from_id(int spaceid)
99 for (st = spacetypes.first; st; st = st->next) {
100 if (st->spaceid == spaceid)
106 ARegionType *BKE_regiontype_from_id(SpaceType *st, int regionid)
110 for (art = st->regiontypes.first; art; art = art->next)
111 if (art->regionid == regionid)
114 printf("Error, region type missing in - name:\"%s\", id:%d\n", st->name, st->spaceid);
115 return st->regiontypes.first;
119 const ListBase *BKE_spacetypes_list(void)
124 void BKE_spacetype_register(SpaceType *st)
129 stype = BKE_spacetype_from_id(st->spaceid);
131 printf("error: redefinition of spacetype %s\n", stype->name);
132 spacetype_free(stype);
136 BLI_addtail(&spacetypes, st);
139 int BKE_spacetype_exists(int spaceid)
141 return BKE_spacetype_from_id(spaceid) != NULL;
144 /* ***************** Space handling ********************** */
146 void BKE_spacedata_freelist(ListBase *lb)
151 for (sl = lb->first; sl; sl = sl->next) {
152 SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
154 /* free regions for pushed spaces */
155 for (ar = sl->regionbase.first; ar; ar = ar->next)
156 BKE_area_region_free(st, ar);
158 BLI_freelistN(&sl->regionbase);
167 ARegion *BKE_area_region_copy(SpaceType *st, ARegion *ar)
169 ARegion *newar = MEM_dupallocN(ar);
170 Panel *pa, *newpa, *patab;
172 newar->prev = newar->next = NULL;
173 newar->handlers.first = newar->handlers.last = NULL;
174 newar->uiblocks.first = newar->uiblocks.last = NULL;
175 newar->ui_lists.first = newar->ui_lists.last = NULL;
178 /* use optional regiondata callback */
179 if (ar->regiondata) {
180 ARegionType *art = BKE_regiontype_from_id(st, ar->regiontype);
182 if (art && art->duplicate)
183 newar->regiondata = art->duplicate(ar->regiondata);
185 newar->regiondata = MEM_dupallocN(ar->regiondata);
188 if (ar->v2d.tab_offset)
189 newar->v2d.tab_offset = MEM_dupallocN(ar->v2d.tab_offset);
191 newar->panels.first = newar->panels.last = NULL;
192 BLI_duplicatelist(&newar->panels, &ar->panels);
194 /* copy panel pointers */
195 for (newpa = newar->panels.first; newpa; newpa = newpa->next) {
196 patab = newar->panels.first;
197 pa = ar->panels.first;
199 if (newpa->paneltab == pa) {
200 newpa->paneltab = patab;
212 /* from lb2 to lb1, lb1 is supposed to be freed */
213 static void region_copylist(SpaceType *st, ListBase *lb1, ListBase *lb2)
218 lb1->first = lb1->last = NULL;
220 for (ar = lb2->first; ar; ar = ar->next) {
221 ARegion *arnew = BKE_area_region_copy(st, ar);
222 BLI_addtail(lb1, arnew);
227 /* lb1 should be empty */
228 void BKE_spacedata_copylist(ListBase *lb1, ListBase *lb2)
232 lb1->first = lb1->last = NULL; /* to be sure */
234 for (sl = lb2->first; sl; sl = sl->next) {
235 SpaceType *st = BKE_spacetype_from_id(sl->spacetype);
237 if (st && st->duplicate) {
238 SpaceLink *slnew = st->duplicate(sl);
240 BLI_addtail(lb1, slnew);
242 region_copylist(st, &slnew->regionbase, &sl->regionbase);
247 /* facility to set locks for drawing to survive (render) threads accessing drawing data */
248 /* lock can become bitflag too */
249 /* should be replaced in future by better local data handling for threads */
250 void BKE_spacedata_draw_locks(int set)
254 for (st = spacetypes.first; st; st = st->next) {
257 for (art = st->regiontypes.first; art; art = art->next) {
259 art->do_lock = art->lock;
261 art->do_lock = FALSE;
267 /* not region itself */
268 void BKE_area_region_free(SpaceType *st, ARegion *ar)
271 ARegionType *art = BKE_regiontype_from_id(st, ar->regiontype);
273 if (art && art->free)
277 printf("regiondata free error\n");
279 else if (ar->type && ar->type->free)
282 if (ar->v2d.tab_offset) {
283 MEM_freeN(ar->v2d.tab_offset);
284 ar->v2d.tab_offset = NULL;
287 BLI_freelistN(&ar->panels);
288 BLI_freelistN(&ar->ui_lists);
291 /* not area itself */
292 void BKE_screen_area_free(ScrArea *sa)
294 SpaceType *st = BKE_spacetype_from_id(sa->spacetype);
297 for (ar = sa->regionbase.first; ar; ar = ar->next)
298 BKE_area_region_free(st, ar);
300 BLI_freelistN(&sa->regionbase);
302 BKE_spacedata_freelist(&sa->spacedata);
304 BLI_freelistN(&sa->actionzones);
307 /* don't free screen itself */
308 void BKE_screen_free(bScreen *sc)
313 for (ar = sc->regionbase.first; ar; ar = ar->next)
314 BKE_area_region_free(NULL, ar);
316 BLI_freelistN(&sc->regionbase);
318 for (sa = sc->areabase.first; sa; sa = san) {
320 BKE_screen_area_free(sa);
323 BLI_freelistN(&sc->vertbase);
324 BLI_freelistN(&sc->edgebase);
325 BLI_freelistN(&sc->areabase);
329 unsigned int BKE_screen_visible_layers(bScreen *screen, Scene *scene)
332 unsigned int layer = 0;
335 /* get all used view3d layers */
336 for (sa = screen->areabase.first; sa; sa = sa->next)
337 if (sa->spacetype == SPACE_VIEW3D)
338 layer |= ((View3D *)sa->spacedata.first)->lay;
347 /* ***************** Utilities ********************** */
349 /* Find a region of the specified type from the given area */
350 ARegion *BKE_area_find_region_type(ScrArea *sa, int type)
355 for (ar = sa->regionbase.first; ar; ar = ar->next) {
356 if (ar->regiontype == type)
363 ARegion *BKE_area_find_region_active_win(ScrArea *sa)
366 ARegion *ar = BLI_findlink(&sa->regionbase, sa->region_active_win);
367 if (ar && (ar->regiontype == RGN_TYPE_WINDOW)) {
371 /* fallback to any */
372 return BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
377 /* note, using this function is generally a last resort, you really want to be
378 * using the context when you can - campbell
380 ScrArea *BKE_screen_find_big_area(bScreen *sc, const int spacetype, const short min)
382 ScrArea *sa, *big = NULL;
383 int size, maxsize = 0;
385 for (sa = sc->areabase.first; sa; sa = sa->next) {
386 if ((spacetype == -1) || sa->spacetype == spacetype) {
387 if (min <= sa->winx && min <= sa->winy) {
388 size = sa->winx * sa->winy;
389 if (size > maxsize) {
400 void BKE_screen_view3d_sync(struct View3D *v3d, struct Scene *scene)
404 if (v3d->scenelock && v3d->localvd == NULL) {
405 v3d->lay = scene->lay;
406 v3d->camera = scene->camera;
408 if (v3d->camera == NULL) {
411 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
412 if (ar->regiontype == RGN_TYPE_WINDOW) {
413 RegionView3D *rv3d = ar->regiondata;
414 if (rv3d->persp == RV3D_CAMOB)
415 rv3d->persp = RV3D_PERSP;
420 if ((v3d->lay & v3d->layact) == 0) {
421 for (bit = 0; bit < 32; bit++) {
422 if (v3d->lay & (1 << bit)) {
423 v3d->layact = 1 << bit;
431 void BKE_screen_view3d_scene_sync(bScreen *sc)
433 /* are there cameras in the views that are not in the scene? */
435 for (sa = sc->areabase.first; sa; sa = sa->next) {
437 for (sl = sa->spacedata.first; sl; sl = sl->next) {
438 if (sl->spacetype == SPACE_VIEW3D) {
439 View3D *v3d = (View3D *) sl;
440 BKE_screen_view3d_sync(v3d, sc->scene);
446 void BKE_screen_view3d_main_sync(ListBase *screen_lb, Scene *scene)
452 /* from scene copy to the other views */
453 for (sc = screen_lb->first; sc; sc = sc->id.next) {
454 if (sc->scene != scene)
457 for (sa = sc->areabase.first; sa; sa = sa->next)
458 for (sl = sa->spacedata.first; sl; sl = sl->next)
459 if (sl->spacetype == SPACE_VIEW3D)
460 BKE_screen_view3d_sync((View3D *)sl, scene);
464 /* magic zoom calculation, no idea what
465 * it signifies, if you find out, tell me! -zr
468 /* simple, its magic dude!
469 * well, to be honest, this gives a natural feeling zooming
470 * with multiple keypad presses (ton)
472 float BKE_screen_view3d_zoom_to_fac(float camzoom)
474 return powf(((float)M_SQRT2 + camzoom / 50.0f), 2.0f) / 4.0f;
477 float BKE_screen_view3d_zoom_from_fac(float zoomfac)
479 return ((sqrtf(4.0f * zoomfac) - (float)M_SQRT2) * 50.0f);