]> git.decadent.org.uk Git - ion3.git/blob - ioncore/sizepolicy.c
[svn-upgrade] Integrating new upstream version, ion3 (20070506)
[ion3.git] / ioncore / sizepolicy.c
1 /*
2  * ion/ioncore/sizepolicy.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <libtu/minmax.h>
10 #include <string.h>
11
12 #include "common.h"
13 #include "region.h"
14 #include "resize.h"
15 #include "sizehint.h"
16 #include "sizepolicy.h"
17
18
19
20 static int fit_x(int x, int w, const WRectangle *max_geom)
21 {
22     int mw=maxof(max_geom->w, 1);
23     w=minof(mw, w);
24     return minof(maxof(x, max_geom->x), max_geom->x+mw-w);
25 }
26
27
28 static int fit_y(int y, int h, const WRectangle *max_geom)
29 {
30     int mh=maxof(max_geom->h, 1);
31     h=minof(mh, h);
32     return minof(maxof(y, max_geom->y), max_geom->y+mh-h);
33 }
34
35
36 static void do_gravity(const WRectangle *max_geom, int szplcy,
37                        WRectangle *geom)
38 {
39     /* Assumed: width and height already adjusted within limits */
40     if(geom->h<1)
41         geom->h=1;
42     if(geom->w<1)
43         geom->w=1;
44     
45     switch(szplcy&SIZEPOLICY_HORIZ_MASK){
46     case SIZEPOLICY_HORIZ_LEFT:
47         geom->x=max_geom->x;
48         break;
49         
50     case SIZEPOLICY_HORIZ_RIGHT:
51         geom->x=max_geom->x+max_geom->w-geom->w;
52         break;
53
54     case SIZEPOLICY_HORIZ_CENTER:
55         geom->x=max_geom->x+max_geom->w/2-geom->w/2;
56         break;
57         
58     default:
59         geom->x=fit_x(geom->x, geom->w, max_geom);
60     }
61
62     switch(szplcy&SIZEPOLICY_VERT_MASK){
63     case SIZEPOLICY_VERT_TOP:
64         geom->y=max_geom->y;
65         break;
66         
67     case SIZEPOLICY_VERT_BOTTOM:
68         geom->y=max_geom->y+max_geom->h-geom->h;
69         break;
70
71     case SIZEPOLICY_VERT_CENTER:
72         geom->y=max_geom->y+max_geom->h/2-geom->h/2;
73         break;
74         
75     default:
76         geom->y=fit_x(geom->y, geom->h, max_geom);
77     }
78 }
79
80
81 static void gravity_stretch_policy(int szplcy, WRegion *reg,
82                                    const WRectangle *rq_geom, WFitParams *fp, 
83                                    bool ws, bool hs)
84 {
85     WRectangle max_geom=fp->g;
86     int w, h;
87
88     fp->g=*rq_geom;
89     
90     w=(ws ? max_geom.w : minof(rq_geom->w, max_geom.w));
91     h=(hs ? max_geom.h : minof(rq_geom->h, max_geom.h));
92     
93     if(reg!=NULL)
94         region_size_hints_correct(reg,  &w, &h, FALSE);
95     
96     fp->g.w=w;
97     fp->g.h=h;
98     
99     do_gravity(&max_geom, szplcy, &(fp->g));
100 }
101
102
103 static void sizepolicy_free_snap(WSizePolicy *szplcy, WRegion *reg,
104                                  WRectangle *rq_geom, int rq_flags,
105                                  WFitParams *fp)
106 {
107     WRectangle max_geom=fp->g;
108     bool fullw=((rq_flags&REGION_RQGEOM_WEAK_W) &&
109                 (*szplcy&SIZEPOLICY_HORIZ_MASK)==SIZEPOLICY_HORIZ_CENTER);
110     bool fullh=((rq_flags&REGION_RQGEOM_WEAK_H) &&
111                 (*szplcy&SIZEPOLICY_VERT_MASK)==SIZEPOLICY_VERT_CENTER);
112
113     int w=(fullw ? max_geom.w : minof(rq_geom->w, max_geom.w));
114     int h=(fullh ? max_geom.h : minof(rq_geom->h, max_geom.h));
115     int x_=0, y_=0;
116
117     
118     if(!(rq_flags&REGION_RQGEOM_WEAK_X) 
119        && rq_flags&REGION_RQGEOM_WEAK_W){
120         x_=fit_x(rq_geom->x, 1, &max_geom);
121         if(((*szplcy)&SIZEPOLICY_HORIZ_MASK)==SIZEPOLICY_HORIZ_RIGHT)
122             w=max_geom.x+max_geom.w-x_;
123         else
124             w=minof(w, max_geom.x+max_geom.w-x_);
125     }
126     
127     if(!(rq_flags&REGION_RQGEOM_WEAK_Y)
128        && rq_flags&REGION_RQGEOM_WEAK_H){
129         y_=fit_x(rq_geom->y, 1, &max_geom);
130         if(((*szplcy)&SIZEPOLICY_VERT_MASK)==SIZEPOLICY_VERT_BOTTOM)
131             h=max_geom.y+max_geom.h-y_;
132         else
133             h=minof(h, max_geom.y+max_geom.h-y_);
134     }
135        
136     if(reg!=NULL)
137         region_size_hints_correct(reg, &w, &h, FALSE);
138     
139     fp->g.w=w;
140     fp->g.h=h;
141     
142     if(!(rq_flags&REGION_RQGEOM_WEAK_X) 
143        && rq_flags&REGION_RQGEOM_WEAK_W){
144         fp->g.x=x_;
145     }else if(rq_flags&REGION_RQGEOM_WEAK_X){
146         switch((*szplcy)&SIZEPOLICY_HORIZ_MASK){
147         case SIZEPOLICY_HORIZ_CENTER:
148             fp->g.x=max_geom.x+(max_geom.w-w)/2;
149             break;
150  
151         case SIZEPOLICY_HORIZ_LEFT:
152             fp->g.x=max_geom.x;
153             break;
154             
155         case SIZEPOLICY_HORIZ_RIGHT:
156             fp->g.x=max_geom.x+max_geom.w-w;
157             break;
158             
159         default:
160             fp->g.x=fit_x(rq_geom->x, w, &max_geom);
161             break;
162         }
163     }else{
164         fp->g.x=fit_x(rq_geom->x, w, &max_geom);
165     }
166     
167     if(!(rq_flags&REGION_RQGEOM_WEAK_Y)
168        && rq_flags&REGION_RQGEOM_WEAK_H){
169         fp->g.y=y_;
170     }else if(rq_flags&REGION_RQGEOM_WEAK_Y){
171         switch((*szplcy)&SIZEPOLICY_VERT_MASK){
172         case SIZEPOLICY_VERT_CENTER:
173             fp->g.y=max_geom.y+(max_geom.h-h)/2;
174             break;
175             
176         case SIZEPOLICY_VERT_TOP:
177             fp->g.y=max_geom.y;
178             break;
179             
180         case SIZEPOLICY_VERT_BOTTOM:
181             fp->g.y=max_geom.y+max_geom.h-h;
182             break;
183             
184         default:
185             fp->g.y=fit_y(rq_geom->y, h, &max_geom);
186             break;
187         }
188     }else{
189         fp->g.y=fit_y(rq_geom->y, h, &max_geom);
190     }
191     
192     (*szplcy)&=~(SIZEPOLICY_VERT_MASK|SIZEPOLICY_HORIZ_MASK);
193     
194     *szplcy|=( (fullw || fp->g.x<=max_geom.x ? SIZEPOLICY_HORIZ_LEFT : 0)
195               |(fullw || fp->g.x+fp->g.w>=max_geom.x+max_geom.w ? SIZEPOLICY_HORIZ_RIGHT : 0)
196               |(fullh || fp->g.y<=max_geom.y ? SIZEPOLICY_VERT_TOP : 0)
197               |(fullh || fp->g.y+fp->g.h>=max_geom.y+max_geom.h ? SIZEPOLICY_VERT_BOTTOM : 0));
198 }
199
200
201 void sizepolicy(WSizePolicy *szplcy, WRegion *reg,
202                 const WRectangle *rq_geom, int rq_flags,
203                 WFitParams *fp)
204 {
205     uint extra=fp->mode&REGION_FIT_ROTATE;
206     
207     WRectangle tmp;
208     if(rq_geom!=NULL)
209         tmp=*rq_geom;
210     else if(reg!=NULL)
211         tmp=REGION_GEOM(reg);
212     else
213         tmp=fp->g;
214     
215     if((*szplcy)&SIZEPOLICY_SHRUNK){
216         if(reg!=NULL){
217             tmp.w=region_min_w(reg);
218             tmp.h=region_min_h(reg);
219         }else{
220             tmp.w=1;
221             tmp.h=1;
222         }
223         rq_flags&=~(REGION_RQGEOM_WEAK_W|REGION_RQGEOM_WEAK_H);
224     }
225
226     fp->mode=REGION_FIT_EXACT|extra;
227     
228     switch((*szplcy)&SIZEPOLICY_MASK){
229     case SIZEPOLICY_GRAVITY:
230         gravity_stretch_policy(*szplcy, reg, &tmp, fp, FALSE, FALSE);
231         break;
232         
233     case SIZEPOLICY_STRETCH_LEFT:
234         gravity_stretch_policy(SIZEPOLICY_HORIZ_LEFT|SIZEPOLICY_VERT_CENTER, 
235                                reg, &tmp, fp, FALSE, TRUE);
236         break;
237         
238     case SIZEPOLICY_STRETCH_RIGHT:
239         gravity_stretch_policy(SIZEPOLICY_HORIZ_RIGHT|SIZEPOLICY_VERT_CENTER, 
240                                reg, &tmp, fp, FALSE, TRUE);
241         break;
242         
243     case SIZEPOLICY_STRETCH_TOP:
244         gravity_stretch_policy(SIZEPOLICY_VERT_TOP|SIZEPOLICY_HORIZ_CENTER, 
245                                reg, &tmp, fp, TRUE, FALSE);
246         break;
247         
248     case SIZEPOLICY_STRETCH_BOTTOM:
249         gravity_stretch_policy(SIZEPOLICY_VERT_BOTTOM|SIZEPOLICY_HORIZ_CENTER, 
250                                reg, &tmp, fp, TRUE, FALSE);
251         break;
252         
253     case SIZEPOLICY_FULL_EXACT:
254         gravity_stretch_policy(SIZEPOLICY_VERT_CENTER|SIZEPOLICY_HORIZ_CENTER, 
255                                reg, &tmp, fp, TRUE, TRUE);
256         break;
257         
258     case SIZEPOLICY_FREE:
259         rectangle_constrain(&tmp, &(fp->g));
260         if(reg!=NULL)
261             region_size_hints_correct(reg, &tmp.w, &tmp.h, FALSE);
262         fp->g=tmp;
263         break;
264         
265     case SIZEPOLICY_UNCONSTRAINED:
266         if(reg!=NULL)
267             region_size_hints_correct(reg, &tmp.w, &tmp.h, TRUE);
268         fp->g=tmp;
269         break;
270
271     case SIZEPOLICY_FREE_GLUE:
272         sizepolicy_free_snap(szplcy, reg, &tmp, rq_flags, fp);
273         break;
274         
275     case SIZEPOLICY_FULL_BOUNDS:
276     default:
277         fp->mode=REGION_FIT_BOUNDS|extra;
278         break;
279     }
280 }
281
282
283 /* translation table for sizepolicy specifications */
284 static StringIntMap szplcy_specs[] = {
285     {"default",         SIZEPOLICY_DEFAULT},
286     {"full",            SIZEPOLICY_FULL_EXACT},
287     {"full_bounds",     SIZEPOLICY_FULL_BOUNDS},
288     {"free",            SIZEPOLICY_FREE},
289     {"free_glue",       SIZEPOLICY_FREE_GLUE},
290     {"northwest",       SIZEPOLICY_GRAVITY_NORTHWEST},
291     {"north",           SIZEPOLICY_GRAVITY_NORTH},
292     {"northeast",       SIZEPOLICY_GRAVITY_NORTHEAST},
293     {"west",            SIZEPOLICY_GRAVITY_WEST},
294     {"center",          SIZEPOLICY_GRAVITY_CENTER},
295     {"east",            SIZEPOLICY_GRAVITY_EAST},
296     {"southwest",       SIZEPOLICY_GRAVITY_SOUTHWEST},
297     {"south",           SIZEPOLICY_GRAVITY_SOUTH},
298     {"southeast",       SIZEPOLICY_GRAVITY_SOUTHEAST},
299     {"stretch_top",     SIZEPOLICY_STRETCH_TOP},
300     {"stretch_bottom",  SIZEPOLICY_STRETCH_BOTTOM},
301     {"stretch_left",    SIZEPOLICY_STRETCH_LEFT},
302     {"stretch_right",   SIZEPOLICY_STRETCH_RIGHT},
303     {"free_glue_northwest",  SIZEPOLICY_FREE_GLUE__NORTHWEST},
304     {"free_glue_north",      SIZEPOLICY_FREE_GLUE__NORTH},
305     {"free_glue_northeast",  SIZEPOLICY_FREE_GLUE__NORTHEAST},
306     {"free_glue_west",       SIZEPOLICY_FREE_GLUE__WEST},
307     {"free_glue_center",     SIZEPOLICY_FREE_GLUE__CENTER},
308     {"free_glue_east",       SIZEPOLICY_FREE_GLUE__EAST},
309     {"free_glue_southwest",  SIZEPOLICY_FREE_GLUE__SOUTHWEST},
310     {"free_glue_south",      SIZEPOLICY_FREE_GLUE__SOUTH},
311     {"free_glue_southeast",  SIZEPOLICY_FREE_GLUE__SOUTHEAST},
312     { NULL,             SIZEPOLICY_DEFAULT}   /* end marker */
313 };
314
315
316 bool string2sizepolicy(const char *szplcy, WSizePolicy *value)
317 {
318     int tmp;
319     
320     tmp=stringintmap_value(szplcy_specs, szplcy, -1);
321     
322     if(tmp==-1){
323         *value=SIZEPOLICY_DEFAULT;
324         return FALSE;
325     }else{
326         *value=tmp;
327         return TRUE;
328     }
329 }
330
331
332 const char *sizepolicy2string(WSizePolicy szplcy)
333 {
334     return stringintmap_key(szplcy_specs, szplcy, NULL);
335 }