WINGs buttons and labels now have text
[wmaker-crm.git] / src / properties.c
blobebe2b6414f91b78981d9ec1c6761dcba6ff09266
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "GNUstep.h"
34 /* atoms */
35 extern Atom _XA_WM_STATE;
36 extern Atom _XA_WM_CLIENT_LEADER;
37 extern Atom _XA_WM_TAKE_FOCUS;
38 extern Atom _XA_WM_DELETE_WINDOW;
39 extern Atom _XA_WM_SAVE_YOURSELF;
40 extern Atom _XA_GNUSTEP_WM_ATTR;
41 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
42 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
43 extern Atom _XA_WINDOWMAKER_MENU;
44 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
45 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
46 extern Atom _XA_WINDOWMAKER_ICON_TILE;
48 int PropGetNormalHints(Window window, XSizeHints * size_hints, int *pre_iccm)
50 long supplied_hints;
52 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
53 return False;
55 if (supplied_hints == (USPosition | USSize | PPosition | PSize | PMinSize | PMaxSize
56 | PResizeInc | PAspect)) {
57 *pre_iccm = 1;
58 } else {
59 *pre_iccm = 0;
61 return True;
64 int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
66 XClassHint *class_hint;
68 class_hint = XAllocClassHint();
69 if (XGetClassHint(dpy, window, class_hint) == 0) {
70 *wm_class = strdup("default");
71 *wm_instance = strdup("default");
72 XFree(class_hint);
73 return False;
75 *wm_instance = class_hint->res_name;
76 *wm_class = class_hint->res_class;
78 XFree(class_hint);
79 return True;
82 void PropGetProtocols(Window window, WProtocols * prots)
84 Atom *protocols;
85 int count, i;
87 memset(prots, 0, sizeof(WProtocols));
88 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
89 return;
91 for (i = 0; i < count; i++) {
92 if (protocols[i] == _XA_WM_TAKE_FOCUS)
93 prots->TAKE_FOCUS = 1;
94 else if (protocols[i] == _XA_WM_DELETE_WINDOW)
95 prots->DELETE_WINDOW = 1;
96 else if (protocols[i] == _XA_WM_SAVE_YOURSELF)
97 prots->SAVE_YOURSELF = 1;
98 else if (protocols[i] == _XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
99 prots->MINIATURIZE_WINDOW = 1;
101 XFree(protocols);
104 unsigned char *PropGetCheckProperty(Window window, Atom hint, Atom type, int format, int count, int *retCount)
106 Atom type_ret;
107 int fmt_ret;
108 unsigned long nitems_ret;
109 unsigned long bytes_after_ret;
110 unsigned char *data;
111 int tmp;
113 if (count <= 0)
114 tmp = 0xffffff;
115 else
116 tmp = count;
118 if (XGetWindowProperty(dpy, window, hint, 0, tmp, False, type,
119 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
120 (unsigned char **)&data) != Success || !data)
121 return NULL;
123 if ((type != AnyPropertyType && type != type_ret)
124 || (count > 0 && nitems_ret != count)
125 || (format != 0 && format != fmt_ret)) {
126 XFree(data);
127 return NULL;
130 if (retCount)
131 *retCount = nitems_ret;
133 return data;
136 int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
138 unsigned long *data;
140 data = (unsigned long *)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
141 _XA_GNUSTEP_WM_ATTR, 32, 9, NULL);
143 if (!data)
144 return False;
146 *attr = malloc(sizeof(GNUstepWMAttributes));
147 if (!*attr) {
148 XFree(data);
149 return False;
151 (*attr)->flags = data[0];
152 (*attr)->window_style = data[1];
153 (*attr)->window_level = data[2];
154 (*attr)->reserved = data[3];
155 (*attr)->miniaturize_pixmap = data[4];
156 (*attr)->close_pixmap = data[5];
157 (*attr)->miniaturize_mask = data[6];
158 (*attr)->close_mask = data[7];
159 (*attr)->extra_flags = data[8];
161 XFree(data);
163 return True;
166 void PropSetWMakerProtocols(Window root)
168 Atom protocols[3];
169 int count = 0;
171 protocols[count++] = _XA_WINDOWMAKER_MENU;
172 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
173 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
175 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
176 32, PropModeReplace, (unsigned char *)protocols, count);
179 void PropSetIconTileHint(WScreen * scr, RImage * image)
181 static Atom imageAtom = 0;
182 unsigned char *tmp;
183 int x, y;
185 if (scr->info_window == None)
186 return;
188 if (!imageAtom) {
190 * WIDTH, HEIGHT (16 bits, MSB First)
191 * array of R,G,B,A bytes
193 imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
196 tmp = malloc(image->width * image->height * 4 + 4);
197 if (!tmp) {
198 wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
199 return;
202 tmp[0] = image->width >> 8;
203 tmp[1] = image->width & 0xff;
204 tmp[2] = image->height >> 8;
205 tmp[3] = image->height & 0xff;
207 if (image->format == RRGBAFormat) {
208 memcpy(&tmp[4], image->data, image->width * image->height * 4);
209 } else {
210 char *ptr = (char *)(tmp + 4);
211 char *src = (char *)image->data;
213 for (y = 0; y < image->height; y++) {
214 for (x = 0; x < image->width; x++) {
215 *ptr++ = *src++;
216 *ptr++ = *src++;
217 *ptr++ = *src++;
218 *ptr++ = 255;
223 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_ICON_TILE,
224 imageAtom, 8, PropModeReplace, tmp, image->width * image->height * 4 + 4);
225 wfree(tmp);
229 Window PropGetClientLeader(Window window)
231 Window *win;
232 Window leader;
234 win = (Window *) PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER, XA_WINDOW, 32, 1, NULL);
236 if (!win)
237 return None;
239 leader = (Window) * win;
240 XFree(win);
242 return leader;
245 void PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes * attr)
247 unsigned long data[9];
249 data[0] = attr->flags;
250 data[1] = attr->window_style;
251 data[2] = attr->window_level;
252 data[3] = 0; /* reserved */
253 /* The X protocol says XIDs are 32bit */
254 data[4] = attr->miniaturize_pixmap;
255 data[5] = attr->close_pixmap;
256 data[6] = attr->miniaturize_mask;
257 data[7] = attr->close_mask;
258 data[8] = attr->extra_flags;
259 XChangeProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, _XA_GNUSTEP_WM_ATTR,
260 32, PropModeReplace, (unsigned char *)data, 9);
263 int PropGetWindowState(Window window)
265 long *data;
266 long state;
268 data = (long *)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE, 32, 1, NULL);
270 if (!data)
271 return -1;
273 state = *data;
274 XFree(data);
276 return state;
279 void PropCleanUp(Window root)
281 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
282 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
283 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);