]> git.saurik.com Git - wxWidgets.git/blob - src/xpm/simx.c
wxInputConsumer
[wxWidgets.git] / src / xpm / simx.c
1 /*
2 * Copyright (C) 1989-95 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 /* Moved here so that FOR_MSW gets defined if we are using wxWindows (GRG) */
37 #include "xpm.h"
38
39 #ifdef FOR_MSW
40
41 #include "XpmI.h" /* for XpmMalloc */
42
43 /*
44 * On DOS size_t is only 2 bytes, thus malloc(size_t s) can only malloc
45 * 64K. BUT an expression data=malloc(width*height) may result in an
46 * overflow. So this function takes a long as input, and returns NULL if the
47 * request is larger than 64K, is size_t is only 2 bytes.
48 *
49 * This requires casts like XpmMalloc( (long)width*(long(height)), else it
50 * might have no effect at all.
51 */
52
53 void *
54 boundCheckingMalloc(long s)
55 {
56 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
57 return (malloc((size_t) s));
58 } else {
59 if (sizeof(size_t) == 2) {
60 if (s > 0xFFFF)
61 return (NULL); /* to large, size_t with 2 bytes
62 * only allows 16 bits */
63 else
64 return (malloc((size_t) s));
65 } else { /* it's not a long, not 2 bytes,
66 * what is it ??? */
67 return (malloc((size_t) s));
68 }
69 }
70 }
71 void *
72 boundCheckingCalloc(long num, long s)
73 {
74 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
75 return (calloc((size_t) num, (size_t) s));
76 } else {
77 if (sizeof(size_t) == 2) {
78 if (s > 0xFFFF || num * s > 0xFFFF)
79 return (NULL); /* to large, size_t with 2 bytes
80 * only allows 16 bits */
81 else
82 return (calloc((size_t) num, (size_t) s));
83 } else { /* it's not a long, not 2 bytes,
84 * what is it ??? */
85 return (calloc((size_t) num, (size_t) s));
86 }
87 }
88 }
89 void *
90 boundCheckingRealloc(void *p, long s)
91 {
92 if (sizeof(size_t) == sizeof(long)) { /* same size, just do it */
93 return (realloc(p, (size_t) s));
94 } else {
95 if (sizeof(size_t) == 2) {
96 if (s > 0xFFFF)
97 return (NULL); /* to large, size_t with 2 bytes
98 * only allows 16 bits */
99 else
100 return (realloc(p, (size_t) s));
101 } else { /* it's not a long, not 2 bytes,
102 * what is it ??? */
103 return (realloc(p, (size_t) s));
104 }
105 }
106 }
107
108 /* static Visual theVisual = { 0 }; */
109 Visual *
110 XDefaultVisual(Display *display, Screen *screen)
111 {
112 return (NULL); /* struct could contain info about
113 * MONO, GRAY, COLOR */
114 }
115
116 Screen *
117 XDefaultScreen(Display *d)
118 {
119 return (NULL);
120 }
121
122 /* I get only 1 plane but 8 bits per pixel,
123 so I think BITSPIXEL should be depth */
124 int
125 XDefaultDepth(Display *display, Screen *screen)
126 {
127 int d, b;
128 #ifdef __OS2__
129 HPS hpsScreen;
130 HDC hdcScreen;
131 LONG lPlanes;
132 LONG lBitsPerPixel;
133 LONG nDepth;
134
135 hpsScreen = WinGetScreenPS(HWND_DESKTOP);
136 hdcScreen = GpiQueryDevice(hpsScreen);
137 DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
138 DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
139 b = (int)lBitsPerPixel;
140 WinReleasePS(hpsScreen);
141 #else
142 b = GetDeviceCaps(*display, BITSPIXEL);
143 d = GetDeviceCaps(*display, PLANES);
144 #endif
145 return (b);
146 }
147
148 Colormap *
149 XDefaultColormap(Display *display, Screen *screen)
150 {
151 return (NULL);
152 }
153
154 /* convert hex color names,
155 wrong digits (not a-f,A-F,0-9) are treated as zero */
156 static int
157 hexCharToInt(char c)
158 {
159 int r;
160
161 if (c >= '0' && c <= '9')
162 r = c - '0';
163 else if (c >= 'a' && c <= 'f')
164 r = c - 'a' + 10;
165 else if (c >= 'A' && c <= 'F')
166 r = c - 'A' + 10;
167 else
168 r = 0;
169
170 return (r);
171 }
172
173 static int
174 rgbFromHex(char *hex, int *r, int *g, int *b)
175 {
176 int len;
177
178 if (hex == NULL || hex[0] != '#')
179 return (0);
180
181 len = strlen(hex);
182 if (len == 3 + 1) {
183 *r = hexCharToInt(hex[1]);
184 *g = hexCharToInt(hex[2]);
185 *b = hexCharToInt(hex[3]);
186 } else if (len == 6 + 1) {
187 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
188 *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
189 *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
190 } else if (len == 12 + 1) {
191 /* it's like c #32329999CCCC */
192 /* so for now only take two digits */
193 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
194 *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
195 *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
196 } else
197 return (0);
198
199 return (1);
200 }
201
202 /* Color related functions */
203 int
204 XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
205 {
206 int r, g, b; /* only 8 bit values used */
207 int okay;
208
209 /* TODO: use colormap via PALETTE */
210 /* parse name either in table or #RRGGBB #RGB */
211 if (name == NULL)
212 return (0);
213
214 if (name[0] == '#') { /* a hex string */
215 okay = rgbFromHex(name, &r, &g, &b);
216 } else {
217 okay = xpmGetRGBfromName(name, &r, &g, &b);
218 }
219
220 if (okay) {
221 #ifdef __OS2__
222 color->pixel = OS2RGB(r, g, b);
223 #else
224 color->pixel = RGB(r, g, b);
225 #endif
226 color->red = (BYTE) r;
227 color->green = (BYTE) g;
228 color->blue = (BYTE) b;
229 return (1);
230 } else
231 return (0); /* --> ColorError */
232 }
233
234
235 /* GRG: 2nd arg is Colormap*, not Colormap */
236
237 int
238 XAllocColor(Display *d, Colormap *cmap, XColor *color)
239 {
240 /* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
241 index in some colormap as in X */
242 return (1);
243 }
244 void
245 XQueryColors(Display *display, Colormap *colormap,
246 XColor *xcolors, int ncolors)
247 {
248 /* under X this fills the rgb values to given .pixel */
249 /* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
250 XColor *xc = xcolors;
251 int i;
252
253 for (i = 0; i < ncolors; i++, xc++) {
254 xc->red = GetRValue(xc->pixel);
255 xc->green = GetGValue(xc->pixel);
256 xc->blue = GetBValue(xc->pixel);
257 }
258 return;
259 }
260 int
261 XFreeColors(Display *d, Colormap cmap,
262 unsigned long pixels[], int npixels, unsigned long planes)
263 {
264 /* no colormap yet */
265 return (0); /* correct ??? */
266 }
267
268 /* XImage functions */
269 XImage *
270 XCreateImage(Display *d, Visual *v,
271 int depth, int format,
272 int x, int y, int width, int height,
273 int pad, int foo)
274 {
275 XImage *img = (XImage *) XpmMalloc(sizeof(XImage));
276 #ifdef __OS2__
277 BITMAPINFOHEADER2 bmih;
278
279 memset(&bmih, 0, sizeof(BITMAPINFOHEADER2));
280 bmih.cbFix = sizeof(BITMAPINFOHEADER2);
281 bmih.cx = width;
282 bmih.cy = height;
283 bmih.cPlanes = 1;
284 bmih.cBitCount = depth;
285 #endif
286
287 if (img) {
288 /*JW: This is what it should be, but the picture comes out
289 just black!? It appears to be doing monochrome reduction,
290 but I've got no clue why. Using CreateBitmap() is supposed
291 to be slower, but otherwise ok
292 if ( depth == GetDeviceCaps(*d, BITSPIXEL) ) {
293 img->bitmap = CreateCompatibleBitmap(*d, width, height);
294 } else*/ {
295 #ifdef __OS2__
296 img->bitmap = GpiCreateBitmap(*d, &bmih, 0L, NULL, NULL);
297 WinReleasePS(*d);
298 #else
299 img->bitmap = CreateBitmap(width, height, 1 /* plane */ ,
300 depth /* bits per pixel */ , NULL);
301 #endif
302 }
303 img->width = width;
304 img->height = height;
305 img->depth = depth;
306 }
307 return (img);
308
309 }
310
311 void
312 XImageFree(XImage *img)
313 {
314 if (img) {
315 XpmFree(img);
316 }
317 }
318 void
319 XDestroyImage(XImage *img)
320 {
321 if (img) {
322 #ifdef __OS2__
323 GpiDeleteBitmap(img->bitmap);
324 #else
325 DeleteObject(img->bitmap); /* check return ??? */
326 #endif
327 XImageFree(img);
328 }
329 }
330
331 #endif