]> git.saurik.com Git - wxWidgets.git/blame - src/xpm/simx.c
wxDialogs now use GTK_WINDOW_DIALOG,
[wxWidgets.git] / src / xpm / simx.c
CommitLineData
cfbe03c9
JS
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
52void *
53boundCheckingMalloc(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}
70void *
71boundCheckingCalloc(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}
88void *
89boundCheckingRealloc(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 }; */
108Visual *
109XDefaultVisual(Display *display, Screen *screen)
110{
111 return (NULL); /* struct could contain info about
112 * MONO, GRAY, COLOR */
113}
114
115Screen *
116XDefaultScreen(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 */
16a12a3d 123int
cfbe03c9
JS
124XDefaultDepth(Display *display, Screen *screen)
125{
126 int d, b;
127
16a12a3d 128#if !defined(__VISAGECPP__) /* fisme for OS/2 */
cfbe03c9
JS
129 b = GetDeviceCaps(*display, BITSPIXEL);
130 d = GetDeviceCaps(*display, PLANES);
16a12a3d 131#endif
cfbe03c9
JS
132 return (b);
133}
134
135Colormap *
136XDefaultColormap(Display *display, Screen *screen)
137{
138 return (NULL);
139}
140
141/* convert hex color names,
142 wrong digits (not a-f,A-F,0-9) are treated as zero */
16a12a3d 143static int
cfbe03c9
JS
144hexCharToInt(char c)
145{
146 int r;
147
148 if (c >= '0' && c <= '9')
149 r = c - '0';
150 else if (c >= 'a' && c <= 'f')
151 r = c - 'a' + 10;
152 else if (c >= 'A' && c <= 'F')
153 r = c - 'A' + 10;
154 else
155 r = 0;
156
157 return (r);
158}
159
16a12a3d 160static int
cfbe03c9
JS
161rgbFromHex(char *hex, int *r, int *g, int *b)
162{
163 int len;
164
165 if (hex == NULL || hex[0] != '#')
166 return (0);
167
168 len = strlen(hex);
169 if (len == 3 + 1) {
170 *r = hexCharToInt(hex[1]);
171 *g = hexCharToInt(hex[2]);
172 *b = hexCharToInt(hex[3]);
173 } else if (len == 6 + 1) {
174 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
175 *g = hexCharToInt(hex[3]) * 16 + hexCharToInt(hex[4]);
176 *b = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
177 } else if (len == 12 + 1) {
178 /* it's like c #32329999CCCC */
179 /* so for now only take two digits */
180 *r = hexCharToInt(hex[1]) * 16 + hexCharToInt(hex[2]);
181 *g = hexCharToInt(hex[5]) * 16 + hexCharToInt(hex[6]);
182 *b = hexCharToInt(hex[9]) * 16 + hexCharToInt(hex[10]);
183 } else
184 return (0);
185
186 return (1);
187}
188
189/* Color related functions */
16a12a3d 190int
cfbe03c9
JS
191XParseColor(Display *d, Colormap *cmap, char *name, XColor *color)
192{
193 int r, g, b; /* only 8 bit values used */
194 int okay;
195
196/* TODO: use colormap via PALETTE */
197 /* parse name either in table or #RRGGBB #RGB */
198 if (name == NULL)
199 return (0);
200
201 if (name[0] == '#') { /* a hex string */
202 okay = rgbFromHex(name, &r, &g, &b);
203 } else {
204 okay = xpmGetRGBfromName(name, &r, &g, &b);
205 }
206
207 if (okay) {
16a12a3d 208#if !defined(__VISAGECPP__) /* fixme for OS/2 */
cfbe03c9 209 color->pixel = RGB(r, g, b);
16a12a3d 210#endif
cfbe03c9
JS
211 color->red = (BYTE) r;
212 color->green = (BYTE) g;
213 color->blue = (BYTE) b;
214 return (1);
215 } else
216 return (0); /* --> ColorError */
217}
218
219
16a12a3d 220int
cfbe03c9
JS
221XAllocColor(Display *d, Colormap *cmap, XColor *color)
222{
223/* colormap not used yet so color->pixel is the real COLORREF (RBG) and not an
224 index in some colormap as in X */
225 return (1);
226}
16a12a3d 227void
cfbe03c9
JS
228XQueryColors(Display *display, Colormap *colormap,
229 XColor *xcolors, int ncolors)
230{
231/* under X this fills the rgb values to given .pixel */
232/* since there no colormap use FOR_MSW (not yet!!), rgb is plain encoded */
233 XColor *xc = xcolors;
234 int i;
235
16a12a3d 236#if !defined(__VISAGECPP__) /* fixme for OS/2 */
cfbe03c9
JS
237 for (i = 0; i < ncolors; i++, xc++) {
238 xc->red = GetRValue(xc->pixel);
239 xc->green = GetGValue(xc->pixel);
240 xc->blue = GetBValue(xc->pixel);
241 }
16a12a3d 242#endif
cfbe03c9
JS
243 return;
244}
16a12a3d 245int
cfbe03c9
JS
246XFreeColors(Display *d, Colormap cmap,
247 unsigned long pixels[], int npixels, unsigned long planes)
248{
249 /* no colormap yet */
250 return (0); /* correct ??? */
251}
252
253/* XImage functions */
254XImage *
255XCreateImage(Display *d, Visual *v,
256 int depth, int format,
257 int x, int y, int width, int height,
258 int pad, int foo)
259{
260 XImage *img = (XImage *) XpmMalloc(sizeof(XImage));
261
262 if (img) {
263 /* *img = CreateCompatibleBitmap(*d, width, height); */
16a12a3d 264#if !defined(__VISAGECPP__) /* fixme for OS/2 */
cfbe03c9
JS
265 img->bitmap = CreateBitmap(width, height, 1 /* plane */ ,
266 depth /* bits per pixel */ , NULL);
16a12a3d 267#endif
cfbe03c9
JS
268 img->width = width;
269 img->height = height;
270 img->depth = depth;
271 }
272 return (img);
273
274}
275
16a12a3d 276void
cfbe03c9
JS
277XImageFree(XImage *img)
278{
279 if (img) {
280 XpmFree(img);
281 }
282}
16a12a3d 283void
cfbe03c9
JS
284XDestroyImage(XImage *img)
285{
286 if (img) {
16a12a3d 287#if !defined(__VISAGECPP__) /* fixme for OS/2 */
cfbe03c9 288 DeleteObject(img->bitmap); /* check return ??? */
16a12a3d 289#endif
cfbe03c9
JS
290 XImageFree(img);
291 }
292}
293
294#endif