Cull unused functions when compiling against librplay.
[fvwm.git] / modules / FvwmButtons / misc.c
blobba10992b3203201ea9ccbbe90ea0a32b02c9e9e9
1 /* -*-c-*- */
2 /*
3 * FvwmButtons, copyright 1996, Jarl Totland
5 * This module, and the entire GoodStuff program, and the concept for
6 * interfacing this module to the Window Manager, are all original work
7 * by Robert Nation
9 * Copyright 1993, Robert Nation. No guarantees or warantees or anything
10 * are provided or implied in any way whatsoever. Use this program at your
11 * own risk. Permission to use this program for any purpose is given,
12 * as long as the copyright is kept intact.
16 /* This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "config.h"
33 #include "misc.h"
34 #include <stdio.h>
36 /**
37 *** ConstrainSize()
38 *** Adjust a given width and height to account for the constraints imposed by
39 *** size hints.
40 *** The general algorithm, especially the aspect ratio stuff, is borrowed from
41 *** uwm's CheckConsistency routine.
42 **/
43 void ConstrainSize (XSizeHints *hints, int *widthp, int *heightp)
45 #define makemult(a,b) ((b == 1) ? (a) : (((int)((a) / (b))) * (b)))
47 int minWidth, minHeight, maxWidth, maxHeight, xinc, yinc, delta;
48 int baseWidth, baseHeight;
49 int dwidth = *widthp, dheight = *heightp;
51 if (hints->flags & PMinSize)
53 minWidth = hints->min_width;
54 minHeight = hints->min_height;
55 if (hints->flags & PBaseSize)
57 baseWidth = hints->base_width;
58 baseHeight = hints->base_height;
60 else
62 baseWidth = hints->min_width;
63 baseHeight = hints->min_height;
66 else if (hints->flags & PBaseSize)
68 minWidth = hints->base_width;
69 minHeight = hints->base_height;
70 baseWidth = hints->base_width;
71 baseHeight = hints->base_height;
73 else
75 minWidth = 1;
76 minHeight = 1;
77 baseWidth = 1;
78 baseHeight = 1;
81 if (hints->flags & PMaxSize)
83 maxWidth = hints->max_width;
84 maxHeight = hints->max_height;
86 else
88 maxWidth = 32767;
89 maxHeight = 32767;
91 if (hints->flags & PResizeInc)
93 xinc = hints->width_inc;
94 yinc = hints->height_inc;
96 else
98 xinc = 1;
99 yinc = 1;
103 * First, clamp to min and max values
105 if (dwidth < minWidth)
107 dwidth = minWidth;
109 if (dheight < minHeight)
111 dheight = minHeight;
114 if (dwidth > maxWidth)
116 dwidth = maxWidth;
118 if (dheight > maxHeight)
120 dheight = maxHeight;
125 * Second, fit to base + N * inc
127 dwidth = ((dwidth - baseWidth) / xinc * xinc) + baseWidth;
128 dheight = ((dheight - baseHeight) / yinc * yinc) + baseHeight;
132 * Third, adjust for aspect ratio
134 #define maxAspectX hints->max_aspect.x
135 #define maxAspectY hints->max_aspect.y
136 #define minAspectX hints->min_aspect.x
137 #define minAspectY hints->min_aspect.y
139 * The math looks like this:
141 * minAspectX dwidth maxAspectX
142 * ---------- <= ------- <= ----------
143 * minAspectY dheight maxAspectY
145 * If that is multiplied out, then the width and height are
146 * invalid in the following situations:
148 * minAspectX * dheight > minAspectY * dwidth
149 * maxAspectX * dheight < maxAspectY * dwidth
153 if (hints->flags & PAspect)
155 if (minAspectX * dheight > minAspectY * dwidth)
157 delta = makemult(
158 minAspectX * dheight / minAspectY - dwidth,
159 xinc);
160 if (dwidth + delta <= maxWidth)
162 dwidth += delta;
164 else
166 delta = makemult(
167 dheight - minAspectY*dwidth/minAspectX,
168 yinc);
169 if (dheight - delta >= minHeight)
171 dheight -= delta;
176 if (maxAspectX * dheight < maxAspectY * dwidth)
178 delta = makemult(
179 dwidth * maxAspectY / maxAspectX - dheight,
180 yinc);
181 if (dheight + delta <= maxHeight)
183 dheight += delta;
185 else
187 delta = makemult(
188 dwidth - maxAspectX*dheight/maxAspectY,
189 xinc);
190 if (dwidth - delta >= minWidth)
192 dwidth -= delta;
198 *widthp = dwidth;
199 *heightp = dheight;
200 return;
201 #undef makemult
202 #undef maxAspectX
203 #undef maxAspectY
204 #undef minAspectX
205 #undef minAspectY