SetHints.c 8.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

/***********************************************************

Copyright 1987, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

31 32
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
33
provided that the above copyright notice appear in all copies and that
34
both that copyright notice and this permission notice appear in
35 36
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
37
software without specific, written prior permission.
38 39 40 41 42 43 44 45 46 47 48 49 50 51

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
52 53
#include <nx-X11/Xlibint.h>
#include <nx-X11/Xutil.h>
54
#include "Xatomtype.h"
55 56
#include <nx-X11/Xatom.h>
#include <nx-X11/Xos.h>
57 58 59 60

#define safestrlen(s) ((s) ? strlen(s) : 0)

int
61 62 63 64 65
XSetSizeHints(		/* old routine */
	Display *dpy,
	Window w,
	XSizeHints *hints,
        Atom property)
66
{
67 68
	xPropSizeHints prop;
	memset(&prop, 0, sizeof(prop));
69
	prop.flags = (hints->flags & (USPosition|USSize|PAllHints));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	if (hints->flags & (USPosition|PPosition)) {
	    prop.x = hints->x;
	    prop.y = hints->y;
	}
	if (hints->flags & (USSize|PSize)) {
	    prop.width = hints->width;
	    prop.height = hints->height;
	}
	if (hints->flags & PMinSize) {
	    prop.minWidth = hints->min_width;
	    prop.minHeight = hints->min_height;
	}
	if (hints->flags & PMaxSize) {
	    prop.maxWidth  = hints->max_width;
	    prop.maxHeight = hints->max_height;
	}
	if (hints->flags & PResizeInc) {
	    prop.widthInc = hints->width_inc;
	    prop.heightInc = hints->height_inc;
	}
	if (hints->flags & PAspect) {
	    prop.minAspectX = hints->min_aspect.x;
	    prop.minAspectY = hints->min_aspect.y;
	    prop.maxAspectX = hints->max_aspect.x;
	    prop.maxAspectY = hints->max_aspect.y;
	}
96
	return XChangeProperty (dpy, w, property, XA_WM_SIZE_HINTS, 32,
97
				PropModeReplace, (unsigned char *) &prop,
98 99 100
				OldNumPropSizeElements);
}

101 102
/*
 * XSetWMHints sets the property
103 104 105 106
 *	WM_HINTS 	type: WM_HINTS	format:32
 */

int
107 108 109 110
XSetWMHints (
	Display *dpy,
	Window w,
	XWMHints *wmhints)
111 112
{
	xPropWMHints prop;
113
	memset(&prop, 0, sizeof(prop));
114
	prop.flags = wmhints->flags;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	if (wmhints->flags & InputHint)
	    prop.input = (wmhints->input == True ? 1 : 0);
	if (wmhints->flags & StateHint)
	    prop.initialState = wmhints->initial_state;
	if (wmhints->flags & IconPixmapHint)
	    prop.iconPixmap = wmhints->icon_pixmap;
	if (wmhints->flags & IconWindowHint)
	    prop.iconWindow = wmhints->icon_window;
	if (wmhints->flags & IconPositionHint) {
	    prop.iconX = wmhints->icon_x;
	    prop.iconY = wmhints->icon_y;
	}
	if (wmhints->flags & IconMaskHint)
	    prop.iconMask = wmhints->icon_mask;
	if (wmhints->flags & WindowGroupHint)
	    prop.windowGroup = wmhints->window_group;
131
	return XChangeProperty (dpy, w, XA_WM_HINTS, XA_WM_HINTS, 32,
132
				PropModeReplace, (unsigned char *) &prop,
133 134 135 136 137
				NumPropWMHintsElements);
}



138 139
/*
 * XSetZoomHints sets the property
140 141 142 143
 *	WM_ZOOM_HINTS 	type: WM_SIZE_HINTS format: 32
 */

int
144 145 146 147
XSetZoomHints (
	Display *dpy,
	Window w,
	XSizeHints *zhints)
148 149 150 151 152
{
	return XSetSizeHints (dpy, w, zhints, XA_WM_ZOOM_HINTS);
}


153 154
/*
 * XSetNormalHints sets the property
155 156 157 158
 *	WM_NORMAL_HINTS 	type: WM_SIZE_HINTS format: 32
 */

int
159 160 161 162
XSetNormalHints (			/* old routine */
	Display *dpy,
	Window w,
	XSizeHints *hints)
163 164 165 166 167 168 169 170 171 172 173 174 175
{
	return XSetSizeHints (dpy, w, hints, XA_WM_NORMAL_HINTS);
}



/*
 * Note, the following is one of the few cases were we really do want sizeof
 * when examining a protocol structure.  This is because the XChangeProperty
 * routine will take care of converting to host to network data structures.
 */

int
176 177 178 179 180
XSetIconSizes (
	Display *dpy,
	Window w,	/* typically, root */
	XIconSize *list,
	int count) 	/* number of items on the list */
181 182 183 184 185 186
{
	register int i;
	xPropIconSize *pp, *prop;
#define size_of_the_real_thing sizeof	/* avoid grepping screwups */
	unsigned nbytes = count * size_of_the_real_thing(xPropIconSize);
#undef size_of_the_real_thing
187
	if ((prop = pp = Xmalloc (nbytes))) {
188 189 190 191 192 193 194 195 196 197
	    for (i = 0; i < count; i++) {
		pp->minWidth  = list->min_width;
		pp->minHeight = list->min_height;
		pp->maxWidth  = list->max_width;
		pp->maxHeight = list->max_height;
		pp->widthInc  = list->width_inc;
		pp->heightInc = list->height_inc;
		pp += 1;
		list += 1;
	    }
198 199
	    XChangeProperty (dpy, w, XA_WM_ICON_SIZE, XA_WM_ICON_SIZE, 32,
			     PropModeReplace, (unsigned char *) prop,
200
			     count * NumPropIconSizeElements);
201
	    Xfree (prop);
202 203 204 205 206
	}
	return 1;
}

int
207 208 209 210 211
XSetCommand (
	Display *dpy,
	Window w,
	char **argv,
	int argc)
212 213
{
	register int i;
214
	size_t nbytes;
215 216 217 218
	register char *buf, *bp;
	for (i = 0, nbytes = 0; i < argc; i++) {
		nbytes += safestrlen(argv[i]) + 1;
	}
219
	if ((bp = buf = Xmalloc(nbytes))) {
220 221
	    /* copy arguments into single buffer */
	    for (i = 0; i < argc; i++) {
222
		if (argv[i]) {
223 224 225 226 227 228 229 230
		    (void) strcpy(bp, argv[i]);
		    bp += strlen(argv[i]) + 1;
		}
		else
		    *bp++ = '\0';
	    }
	    XChangeProperty (dpy, w, XA_WM_COMMAND, XA_STRING, 8,
			     PropModeReplace, (unsigned char *)buf, nbytes);
231
	    Xfree(buf);
232 233 234
	}
	return 1;
}
235
/*
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
 * XSetStandardProperties sets the following properties:
 *	WM_NAME		  type: STRING		format: 8
 *	WM_ICON_NAME	  type: STRING		format: 8
 *	WM_HINTS	  type: WM_HINTS	format: 32
 *	WM_COMMAND	  type: STRING
 *	WM_NORMAL_HINTS	  type: WM_SIZE_HINTS 	format: 32
 */

int
XSetStandardProperties (
    	Display *dpy,
    	Window w,		/* window to decorate */
    	_Xconst char *name,	/* name of application */
    	_Xconst char *icon_string,/* name string for icon */
	Pixmap icon_pixmap,	/* pixmap to use as icon, or None */
    	char **argv,		/* command to be used to restart application */
    	int argc,		/* count of arguments */
    	XSizeHints *hints)	/* size hints for window in its normal state */
{
	XWMHints phints;
	phints.flags = 0;

	if (name != NULL) XStoreName (dpy, w, name);

	if (icon_string != NULL) {
	    XChangeProperty (dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
262 263 264
                             PropModeReplace,
                             (_Xconst unsigned char *)icon_string,
                             safestrlen(icon_string));
265 266 267 268 269 270 271
		}

	if (icon_pixmap != None) {
		phints.icon_pixmap = icon_pixmap;
		phints.flags |= IconPixmapHint;
		}
	if (argv != NULL) XSetCommand(dpy, w, argv, argc);
272

273 274 275 276 277 278 279 280
	if (hints != NULL) XSetNormalHints(dpy, w, hints);

	if (phints.flags != 0) XSetWMHints(dpy, w, &phints);

	return 1;
}

int
281 282 283 284
XSetTransientForHint(
	Display *dpy,
	Window w,
	Window propWindow)
285 286 287 288 289 290
{
	return XChangeProperty(dpy, w, XA_WM_TRANSIENT_FOR, XA_WINDOW, 32,
			       PropModeReplace, (unsigned char *) &propWindow, 1);
}

int
291 292 293 294
XSetClassHint(
	Display *dpy,
	Window w,
	XClassHint *classhint)
295 296 297
{
	char *class_string;
	char *s;
298
	size_t len_nm, len_cl;
299 300 301

	len_nm = safestrlen(classhint->res_name);
	len_cl = safestrlen(classhint->res_class);
302
	if ((class_string = s = Xmalloc(len_nm + len_cl + 2))) {
303 304 305 306 307 308 309 310 311 312 313
	    if (len_nm) {
		strcpy(s, classhint->res_name);
		s += len_nm + 1;
	    }
	    else
		*s++ = '\0';
	    if (len_cl)
		strcpy(s, classhint->res_class);
	    else
		*s = '\0';
	    XChangeProperty(dpy, w, XA_WM_CLASS, XA_STRING, 8,
314
			    PropModeReplace, (unsigned char *) class_string,
315 316 317 318 319
			    len_nm+len_cl+2);
	    Xfree(class_string);
	}
	return 1;
}