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)
273 ARegionType *art = BKE_regiontype_from_id(st, ar->regiontype);
275 if (art && art->free)
279 printf("regiondata free error\n");
281 else if (ar->type && ar->type->free)
284 if (ar->v2d.tab_offset) {
285 MEM_freeN(ar->v2d.tab_offset);
286 ar->v2d.tab_offset = NULL;
289 BLI_freelistN(&ar->panels);
291 for (uilst = ar->ui_lists.first; uilst; uilst = uilst->next) {
292 if (uilst->dyn_data) {
293 MEM_freeN(uilst->dyn_data);
296 BLI_freelistN(&ar->ui_lists);
299 /* not area itself */
300 void BKE_screen_area_free(ScrArea *sa)
302 SpaceType *st = BKE_spacetype_from_id(sa->spacetype);
305 for (ar = sa->regionbase.first; ar; ar = ar->next)
306 BKE_area_region_free(st, ar);
308 BLI_freelistN(&sa->regionbase);
310 BKE_spacedata_freelist(&sa->spacedata);
312 BLI_freelistN(&sa->actionzones);
315 /* don't free screen itself */
316 void BKE_screen_free(bScreen *sc)
321 for (ar = sc->regionbase.first; ar; ar = ar->next)
322 BKE_area_region_free(NULL, ar);
324 BLI_freelistN(&sc->regionbase);
326 for (sa = sc->areabase.first; sa; sa = san) {
328 BKE_screen_area_free(sa);
331 BLI_freelistN(&sc->vertbase);
332 BLI_freelistN(&sc->edgebase);
333 BLI_freelistN(&sc->areabase);
337 unsigned int BKE_screen_visible_layers(bScreen *screen, Scene *scene)
340 unsigned int layer = 0;
343 /* get all used view3d layers */
344 for (sa = screen->areabase.first; sa; sa = sa->next)
345 if (sa->spacetype == SPACE_VIEW3D)
346 layer |= ((View3D *)sa->spacedata.first)->lay;
355 /* ***************** Utilities ********************** */
357 /* Find a region of the specified type from the given area */
358 ARegion *BKE_area_find_region_type(ScrArea *sa, int type)
363 for (ar = sa->regionbase.first; ar; ar = ar->next) {
364 if (ar->regiontype == type)
371 ARegion *BKE_area_find_region_active_win(ScrArea *sa)
374 ARegion *ar = BLI_findlink(&sa->regionbase, sa->region_active_win);
375 if (ar && (ar->regiontype == RGN_TYPE_WINDOW)) {
379 /* fallback to any */
380 return BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
385 /* note, using this function is generally a last resort, you really want to be
386 * using the context when you can - campbell
388 ScrArea *BKE_screen_find_big_area(bScreen *sc, const int spacetype, const short min)
390 ScrArea *sa, *big = NULL;
391 int size, maxsize = 0;
393 for (sa = sc->areabase.first; sa; sa = sa->next) {
394 if ((spacetype == -1) || sa->spacetype == spacetype) {
395 if (min <= sa->winx && min <= sa->winy) {
396 size = sa->winx * sa->winy;
397 if (size > maxsize) {
408 void BKE_screen_view3d_sync(struct View3D *v3d, struct Scene *scene)
412 if (v3d->scenelock && v3d->localvd == NULL) {
413 v3d->lay = scene->lay;
414 v3d->camera = scene->camera;
416 if (v3d->camera == NULL) {
419 for (ar = v3d->regionbase.first; ar; ar = ar->next) {
420 if (ar->regiontype == RGN_TYPE_WINDOW) {
421 RegionView3D *rv3d = ar->regiondata;
422 if (rv3d->persp == RV3D_CAMOB)
423 rv3d->persp = RV3D_PERSP;
428 if ((v3d->lay & v3d->layact) == 0) {
429 for (bit = 0; bit < 32; bit++) {
430 if (v3d->lay & (1 << bit)) {
431 v3d->layact = 1 << bit;
439 void BKE_screen_view3d_scene_sync(bScreen *sc)
441 /* are there cameras in the views that are not in the scene? */
443 for (sa = sc->areabase.first; sa; sa = sa->next) {
445 for (sl = sa->spacedata.first; sl; sl = sl->next) {
446 if (sl->spacetype == SPACE_VIEW3D) {
447 View3D *v3d = (View3D *) sl;
448 BKE_screen_view3d_sync(v3d, sc->scene);
454 void BKE_screen_view3d_main_sync(ListBase *screen_lb, Scene *scene)
460 /* from scene copy to the other views */
461 for (sc = screen_lb->first; sc; sc = sc->id.next) {
462 if (sc->scene != scene)
465 for (sa = sc->areabase.first; sa; sa = sa->next)
466 for (sl = sa->spacedata.first; sl; sl = sl->next)
467 if (sl->spacetype == SPACE_VIEW3D)
468 BKE_screen_view3d_sync((View3D *)sl, scene);
472 /* magic zoom calculation, no idea what
473 * it signifies, if you find out, tell me! -zr
476 /* simple, its magic dude!
477 * well, to be honest, this gives a natural feeling zooming
478 * with multiple keypad presses (ton)
480 float BKE_screen_view3d_zoom_to_fac(float camzoom)
482 return powf(((float)M_SQRT2 + camzoom / 50.0f), 2.0f) / 4.0f;
485 float BKE_screen_view3d_zoom_from_fac(float zoomfac)
487 return ((sqrtf(4.0f * zoomfac) - (float)M_SQRT2) * 50.0f);