]> git.saurik.com Git - wxWidgets.git/blob - src/xpm/simx.c
Some little compilation fixes, etc.
[wxWidgets.git] / src / xpm / simx.c
1 /*
2 * Copyright (C) 1989-94 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26 /*****************************************************************************\
27 * simx.c: 0.1a *
28 * *
29 * This emulates some Xlib functionality for MSW. It's not a general solution, *
30 * it is close related to XPM-lib. It is only intended to satisfy what is need *
31 * there. Thus allowing to read XPM files under MS windows. *
32 * *
33 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
34 \*****************************************************************************/
35
36 #include "xpm34.h"
37
38 #ifdef FOR_MSW
39
40 #include "xpm34p.h" /* for XpmMalloc */
41
42 /*
43 * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
44 * 64K. BUT an expression data=malloc(width*height) may result in an
45 * overflow. So this function takes a long as input, and returns NULL if the
46 * request is larger than 64K, is size_t is only 2 bytes.
47 *
48 * This requires casts like XpmMalloc( (long)width*(long(height)), else it
49 * might have no effect at all.
50 */
51
52 void *
53 boundCheckingMalloc(long s)
54 {
55 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
56 return (malloc((size_t) s));
57 } else {
58 if (sizeof(size_t) == 2) {
59 if (s > 0xFFFF)
60 return (NULL); /* to large, size_t with 2 bytes
61 * only allows 16 bits */
62 else
63 return (malloc((size_t) s));
64 } else { /* it's not a long, not 2 bytes,
65 * what is it ??? */
66 return (malloc((size_t) s));
67 }
68 }
69 }
70 void *
71 boundCheckingCalloc(long num, long s)
72 {
73 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
74 return (calloc((size_t) num, (size_t) s));
75 } else {
76 if (sizeof(size_t) == 2) {
77 if (s > 0xFFFF || num * s > 0xFFFF)
78 return (NULL); /* to large, size_t with 2 bytes
79 * only allows 16 bits */
80 else
81 return (calloc((size_t) num, (size_t) s));
82 } else { /* it's not a long, not 2 bytes,
83 * what is it ??? */
84 return (calloc((size_t) num, (size_t) s));
85 }
86 }
87 }
88 void *
89 boundCheckingRealloc(void *p, long s)
90 {
91 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
92 return (realloc(p, (size_t) s));
93 } else {
94 if (sizeof(size_t) == 2) {
95 if (s > 0xFFFF)
96 return (NULL); /* to large, size_t with 2 bytes
97 * only allows 16 bits */
98 else
99 return (realloc(p, (size_t) s));
100 } else { /* it's not a long, not 2 bytes,
101 * what is it ??? */
102 return (realloc(p, (size_t) s));
103 }
104 }
105 }
106
107 /* static Visual theVisual = { 0 }; */
108 Visual *
109 XDefaultVisual(Display *display, Screen *screen)
110 {
111 return (NULL); /* struct could contain info about
112 * MONO, GRAY, COLOR */
113 }
114
115 Screen *
116 XDefaultScreen(Display *d)
117 {
118 return (NULL);
119 }
120
121 /* I get only 1 plane but 8 bits per pixel,
122 so I think BITSPIXEL should be depth
123
124 TRS: I assume that all "displays" have the same number of
125 planes later in the code, which is based on the assumption
126 that the display variable is ignored below. :)
127 */
128 int
129 XDefaultDepth(Display *display, Screen *screen)
130 {
131 int d, b;
132
133 #if !defined(__VISAGECPP__) /* fisme for OS/2 */
134 b = GetDeviceCaps(*display, BITSPIXEL);
135 d = GetDeviceCaps(*display, PLANES);
136 #endif
137 return (b);
138 }
139
140 Colormap *
141 XDefaultColormap(Display *display, Screen *screen)
142 {
143 return (NULL);
144 }
145
146 /* convert hex color names,
147 wrong digits (not a-f,A-F,0-9) are treated as zero */
148 static int
149 hexCharToInt(char c)
150 {
151 int r;
152
153 if (c >= '0' && c <= '9')
154 r = c - '0';
155 else if (c >= 'a' && c <= 'f')
156 r = c - 'a' + 10;
157 else if (c >= 'A' && c <= 'F')
158 r = c - 'A' + 10;
159 else
160 r = 0;
161
162 return (r);
163 }
164
165 static int
166 rgbFromHex(char *hex, int *r, int *g, int *b)
167 {
168 int len;
169
170 if (hex == NULL || hex[0] != '#')
171 return (0);
172
173 len = strlen(hex);
174 if (len == 3 + 1) {
175 *r = hexCharToInt(hex[1]);
176 *g = hexCharToInt(hex[2]);
177 *b = hexCharToInt(hex[3]);
178 } else if (len == 6 + 1) {
179 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
180 *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
181 *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
182 } else if (len == 12 + 1) {
183 /* it's like c #32329999CCCC */
184 /* so for now only take two digits */
185 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
186 *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
187 *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
188 } else
189 return (0);
190
191 return (1);
192 }
193
194 /* Color related functions */
195 int
196 XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
197 {
198 int r, g, b; /* only 8 bit values used */
199 int okay;
200
201 /* TODO: use colormap via PALETTE */
202 /* parse name either in table or #RRGGBB #RGB */
203 if (name == NULL)
204 return (0);
205
206 if (name[0] == '#') { /* a hex string */
207 okay = rgbFromHex(name, &r, &g, &b);
208 } else {
209 okay = xpmGetRGBfromName(name, &r, &g, &b);
210 }
211
212 if (okay) {
213 #if !defined(__VISAGECPP__) /* fixme for OS/2 */
214 color->pixel = RGB(r, g, b);
215 #endif
216 color->red = (BYTE) r;
217 color->green = (BYTE) g;
218 color->blue = (BYTE) b;
219 return (1);
220 } else
221 return (0); /* --> ColorError */
222 }
223
224
225 int
226 XAllocColor(Display *d, Colormap *cmap, XColor *color)
227 {
228 /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
229 index in some colormap as in X */
230 return (1);
231 }
232 void
233 XQueryColors(Display *display, Colormap *colormap,
234 XColor *xcolors, int ncolors)
235 {
236 /* under X this fills the rgb values to given .pixel */
237 /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
238 XColor *xc = xcolors;
239 int i;
240
241 #if !defined(__VISAGECPP__) /* fixme for OS/2 */
242 for (i = 0; i < ncolors; i++, xc++) {
243 xc->red = GetRValue(xc->pixel);
244 xc->green = GetGValue(xc->pixel);
245 xc->blue = GetBValue(xc->pixel);
246 }
247 #endif
248 return;
249 }
250 int
251 XFreeColors(Display *d, Colormap cmap,
252 unsigned long pixels[], int npixels, unsigned long planes)
253 {
254 /* no colormap yet */
255 return (0); /* correct ??? */
256 }
257
258 /* XImage functions */
259 XImage *
260 XCreateImage(Display *d, Visual *v,
261 int depth, int format,
262 int x, int y, int width, int height,
263 int pad, int foo)
264 {
265 XImage *img = (XImage *) XpmMalloc(sizeof(XImage));
266
267 if (img) {
268 /* *img = CreateCompatibleBitmap(*d, width, height); */
269
270 #if !defined(__VISAGECPP__) /* fixme for OS/2 */
271 /* create the bitmap with the same number of planes as the default display
272 * (otherwise it wouldn't work for 16 color mode) */
273 img->bitmap = CreateBitmap(width, height,
274 GetDeviceCaps(*d, PLANES),
275 depth /* bits per pixel */ , NULL);
276 #endif
277 img->width = width;
278 img->height = height;
279 img->depth = depth;
280 }
281 return (img);
282
283 }
284
285 void
286 XImageFree(XImage *img)
287 {
288 if (img) {
289 XpmFree(img);
290 }
291 }
292 void
293 XDestroyImage(XImage *img)
294 {
295 if (img) {
296 #if !defined(__VISAGECPP__) /* fixme for OS/2 */
297 DeleteObject(img->bitmap); /* check return ??? */
298 #endif
299 XImageFree(img);
300 }
301 }
302
303 #endif