1 /*
2         mindlib/misc.c
3 
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2, or (at your option)
7         any later version.
8 
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13 
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #include <X11/Intrinsic.h>
20 #include <X11/StringDefs.h>
21 #include <X11/cursorfont.h>
22 /*#include <X11/Xaw/Label.h>*/
23 #include <ctype.h>
24 
25 #ifndef XtNcursor
26 #define XtNcursor	 "cursor"
27 #endif
28 
29 #include "xpaint.h"
30 
GetShell(Widget w)31 Widget GetShell(Widget w)
32 {
33 	while (!XtIsShell(w))
34 		w = XtParent(w);
35 	return w;
36 }
37 
38 /*
39 **
40 */
GetPixmapWHD(Display * dpy,Drawable d,int * wth,int * hth,int * dth)41 void GetPixmapWHD(Display *dpy, Drawable d, int *wth, int *hth, int *dth)
42 {
43 	Window		root;
44 	int		x, y;
45 	unsigned int	width, height, bw, depth;
46 
47 	XGetGeometry(dpy, d, &root, &x, &y, &width, &height, &bw, &depth);
48 
49 	if (wth != NULL) *wth = width;
50 	if (hth != NULL) *hth = height;
51 	if (dth != NULL) *dth = depth;
52 }
53 
54 /*
55 **  Two useful functions to "cache" both a tiled background
56 **    and a Xor gc
57 */
58 typedef struct bgList_s {
59 	int			depth;
60 	Pixmap			pixmap;
61 	struct	bgList_s	*next;
62 } BackgroundList;
63 
64 typedef struct gcList_s {
65 	Widget			widget;
66 	int			depth;
67 	GC			gc;
68 	struct	gcList_s	*next;
69 } GCXList;
70 
71 /*
72 **  Create a XImage
73 */
NewXImage(Display * dpy,Visual * visual,int depth,int width,int height)74 XImage	*NewXImage(Display *dpy, Visual *visual, int depth, int width, int height)
75 {
76 	XImage	*xim;
77 	int	pad;
78 
79 	if (depth > 16)
80 		pad = 32;
81 	else if (depth > 8)
82 		pad = 16;
83 	else
84 		pad = 8;
85 
86 	xim = XCreateImage(dpy, visual, depth, ZPixmap, 0, NULL, width, height, pad, 0);
87 
88 	if (xim == NULL)
89 		return NULL;
90 
91 	xim->data = (char*)XtMalloc(xim->bytes_per_line * height);
92 
93 	if (xim->data == NULL) {
94 		XDestroyImage(xim);
95 		xim = NULL;
96 	}
97 
98 	return xim;
99 }
100