WMGeom.c revision 61b2299d
1/* $Xorg: WMGeom.c,v 1.4 2001/02/09 02:03:37 xorgcvs Exp $ */
2/*
3
4Copyright 1989, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/WMGeom.c,v 1.3 2003/04/13 19:22:18 dawes Exp $ */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include "Xlibint.h"
33#include "Xutil.h"
34
35static int _GeometryMaskToGravity(
36    int mask);
37
38/*
39 * This routine given a user supplied positional argument and a default
40 * argument (fully qualified) will return the position the window should take
41 * as well as the gravity to be set in the WM_NORMAL_HINTS size hints.
42 * Always sets all return values and returns a mask describing which fields
43 * were set by the user or'ed with whether or not the x and y values should
44 * be considered "negative".
45 */
46
47int
48XWMGeometry (
49    Display *dpy,			/* user's display connection */
50    int screen,				/* screen on which to do computation */
51    _Xconst char *user_geom,		/* user provided geometry spec */
52    _Xconst char *def_geom,		/* default geometry spec for window */
53    unsigned int bwidth,		/* border width */
54    XSizeHints *hints,			/* usually WM_NORMAL_HINTS */
55    int *x_return,			/* location of window */
56    int *y_return,			/* location of window */
57    int *width_return,			/* size of window */
58    int *height_return,			/* size of window */
59    int *gravity_return)		/* gravity of window */
60{
61    int ux, uy;				/* returned values from parse */
62    unsigned int uwidth, uheight;	/* returned values from parse */
63    int umask;				/* parse mask of returned values */
64    int dx, dy;				/* default values from parse */
65    unsigned int dwidth, dheight;	/* default values from parse */
66    int dmask;				/* parse mask of returned values */
67    int base_width, base_height;	/* valid amounts */
68    int min_width, min_height;		/* valid amounts */
69    int width_inc, height_inc;		/* valid amounts */
70    int rx, ry, rwidth, rheight;	/* return values */
71    int rmask;				/* return mask */
72
73    /*
74     * Get the base sizes and increments.  Section 4.1.2.3 of the ICCCM
75     * states that the base and minimum sizes are defaults for each other.
76     * If neither is given, then the base sizes should be 0.  These parameters
77     * control the sets of sizes that window managers should allow for the
78     * window according to the following formulae:
79     *
80     *          width = base_width  + (i * width_inc)
81     *         height = base_height + (j * height_inc)
82     */
83    base_width =  ((hints->flags & PBaseSize) ? hints->base_width :
84		   ((hints->flags & PMinSize) ? hints->min_width : 0));
85    base_height = ((hints->flags & PBaseSize) ? hints->base_height :
86		   ((hints->flags & PMinSize) ? hints->min_height : 0));
87    min_width = ((hints->flags & PMinSize) ? hints->min_width : base_width);
88    min_height = ((hints->flags & PMinSize) ? hints->min_height : base_height);
89    width_inc = (hints->flags & PResizeInc) ? hints->width_inc : 1;
90    height_inc = (hints->flags & PResizeInc) ? hints->height_inc : 1;
91
92
93    /*
94     * parse the two geometry masks
95     */
96    rmask = umask = XParseGeometry (user_geom, &ux, &uy, &uwidth, &uheight);
97    dmask = XParseGeometry (def_geom, &dx, &dy, &dwidth, &dheight);
98
99
100    /*
101     * get the width and height:
102     *     1.  if user-specified, then take that value
103     *     2.  else, if program-specified, then take that value
104     *     3.  else, take 1
105     *     4.  multiply by the size increment
106     *     5.  and add to the base size
107     */
108    rwidth = ((((umask & WidthValue) ? uwidth :
109		((dmask & WidthValue) ? dwidth : 1)) * width_inc) +
110	      base_width);
111    rheight = ((((umask & HeightValue) ? uheight :
112		 ((dmask & HeightValue) ? dheight : 1)) * height_inc) +
113	       base_height);
114
115    /*
116     * Make sure computed size is within limits.  Note that we always do the
117     * lower bounds check since the base size (which defaults to 0) should
118     * be used if a minimum size isn't specified.
119     */
120    if (rwidth < min_width) rwidth = min_width;
121    if (rheight < min_height) rheight = min_height;
122
123    if (hints->flags & PMaxSize) {
124	if (rwidth > hints->max_width) rwidth = hints->max_width;
125	if (rheight > hints->max_height) rheight = hints->max_height;
126    }
127
128
129    /*
130     * Compute the location.  Set the negative flags in the return mask
131     * (and watch out for borders), if necessary.
132     */
133    if (umask & XValue) {
134	rx = ((umask & XNegative) ?
135	      (DisplayWidth (dpy, screen) + ux - rwidth - 2 * bwidth) : ux);
136    } else if (dmask & XValue) {
137	if (dmask & XNegative) {
138	    rx = (DisplayWidth (dpy, screen) + dx - rwidth - 2 * bwidth);
139	    rmask |= XNegative;
140	} else
141	  rx = dx;
142    } else {
143	rx = 0;				/* gotta choose something... */
144    }
145
146    if (umask & YValue) {
147	ry = ((umask & YNegative) ?
148	      (DisplayHeight(dpy, screen) + uy - rheight - 2 * bwidth) : uy);
149    } else if (dmask & YValue) {
150	if (dmask & YNegative) {
151	    ry = (DisplayHeight(dpy, screen) + dy - rheight - 2 * bwidth);
152	    rmask |= YNegative;
153	} else
154	  ry = dy;
155    } else {
156	ry = 0;				/* gotta choose something... */
157    }
158
159
160    /*
161     * All finished, so set the return variables.
162     */
163    *x_return = rx;
164    *y_return = ry;
165    *width_return = rwidth;
166    *height_return = rheight;
167    *gravity_return = _GeometryMaskToGravity (rmask);
168    return rmask;
169}
170
171
172static int _GeometryMaskToGravity(
173    int mask)
174{
175    switch (mask & (XNegative|YNegative)) {
176      case 0:
177        return NorthWestGravity;
178      case XNegative:
179        return NorthEastGravity;
180      case YNegative:
181        return SouthWestGravity;
182      default:
183        return SouthEastGravity;
184    }
185}
186