]> git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/Attrib.c
ANSI 'C' fix (decs in "for" clauses no longer have any scope outside clause itself)
[wxWidgets.git] / src / mac / xpm / Attrib.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 * Attrib.c: *
28 * *
29 * XPM library *
30 * Functions related to the XpmAttributes structure *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 #include "XpmI.h"
36
37 /* 3.2 backward compatibility code */
38 LFUNC(CreateOldColorTable, int, (XpmColor *ct, int ncolors,
39 XpmColor ***oldct));
40
41 LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, int ncolors));
42
43 /*
44 * Create a colortable compatible with the old style colortable
45 */
46 static int
47 CreateOldColorTable(ct, ncolors, oldct)
48 XpmColor *ct;
49 int ncolors;
50 XpmColor ***oldct;
51 {
52 XpmColor **colorTable, **color;
53 int a;
54
55 colorTable = (XpmColor **) XpmMalloc(ncolors * sizeof(XpmColor *));
56 if (!colorTable) {
57 *oldct = NULL;
58 return (XpmNoMemory);
59 }
60 for (a = 0, color = colorTable; a < ncolors; a++, color++, ct++)
61 *color = ct;
62 *oldct = colorTable;
63 return (XpmSuccess);
64 }
65
66 static void
67 FreeOldColorTable(colorTable, ncolors)
68 XpmColor **colorTable;
69 int ncolors;
70 {
71 int a, b;
72 XpmColor **color;
73 char **sptr;
74
75 if (colorTable) {
76 for (a = 0, color = colorTable; a < ncolors; a++, color++) {
77 for (b = 0, sptr = (char **) *color; b <= NKEYS; b++, sptr++)
78 if (*sptr)
79 XpmFree(*sptr);
80 }
81 XpmFree(*colorTable);
82 XpmFree(colorTable);
83 }
84 }
85
86 /* end 3.2 bc */
87
88 /*
89 * Free the computed color table
90 */
91 void
92 xpmFreeColorTable(colorTable, ncolors)
93 XpmColor *colorTable;
94 int ncolors;
95 {
96 int a, b;
97 XpmColor *color;
98 char **sptr;
99
100 if (colorTable) {
101 for (a = 0, color = colorTable; a < ncolors; a++, color++) {
102 for (b = 0, sptr = (char **) color; b <= NKEYS; b++, sptr++)
103 if (*sptr)
104 XpmFree(*sptr);
105 }
106 XpmFree(colorTable);
107 }
108 }
109
110 /*
111 * Free array of extensions
112 */
113 void
114 XpmFreeExtensions(extensions, nextensions)
115 XpmExtension *extensions;
116 int nextensions;
117 {
118 unsigned int i, j, nlines;
119 XpmExtension *ext;
120 char **sptr;
121
122 if (extensions) {
123 for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
124 if (ext->name)
125 XpmFree(ext->name);
126 nlines = ext->nlines;
127 for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
128 if (*sptr)
129 XpmFree(*sptr);
130 if (ext->lines)
131 XpmFree(ext->lines);
132 }
133 XpmFree(extensions);
134 }
135 }
136
137 /*
138 * Return the XpmAttributes structure size
139 */
140
141 int
142 XpmAttributesSize()
143 {
144 return sizeof(XpmAttributes);
145 }
146
147 /*
148 * Init returned data to free safely later on
149 */
150 void
151 xpmInitAttributes(attributes)
152 XpmAttributes *attributes;
153 {
154 if (attributes) {
155 attributes->pixels = NULL;
156 attributes->npixels = 0;
157 attributes->colorTable = NULL;
158 attributes->ncolors = 0;
159 /* 3.2 backward compatibility code */
160 attributes->hints_cmt = NULL;
161 attributes->colors_cmt = NULL;
162 attributes->pixels_cmt = NULL;
163 /* end 3.2 bc */
164 if (attributes->valuemask & XpmReturnExtensions) {
165 attributes->extensions = NULL;
166 attributes->nextensions = 0;
167 }
168 if (attributes->valuemask & XpmReturnAllocPixels) {
169 attributes->alloc_pixels = NULL;
170 attributes->nalloc_pixels = 0;
171 }
172 }
173 }
174
175 /*
176 * Fill in the XpmAttributes with the XpmImage and the XpmInfo
177 */
178 void
179 xpmSetAttributes(attributes, image, info)
180 XpmAttributes *attributes;
181 XpmImage *image;
182 XpmInfo *info;
183 {
184 if (attributes->valuemask & XpmReturnColorTable) {
185 attributes->colorTable = image->colorTable;
186 attributes->ncolors = image->ncolors;
187
188 /* avoid deletion of copied data */
189 image->ncolors = 0;
190 image->colorTable = NULL;
191 }
192 /* 3.2 backward compatibility code */
193 else if (attributes->valuemask & XpmReturnInfos) {
194 int ErrorStatus;
195
196 ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
197 (XpmColor ***)
198 &attributes->colorTable);
199
200 /* if error just say we can't return requested data */
201 if (ErrorStatus != XpmSuccess) {
202 attributes->valuemask &= ~XpmReturnInfos;
203 if (!(attributes->valuemask & XpmReturnPixels)) {
204 XpmFree(attributes->pixels);
205 attributes->pixels = NULL;
206 attributes->npixels = 0;
207 }
208 attributes->ncolors = 0;
209 } else {
210 attributes->ncolors = image->ncolors;
211 attributes->hints_cmt = info->hints_cmt;
212 attributes->colors_cmt = info->colors_cmt;
213 attributes->pixels_cmt = info->pixels_cmt;
214
215 /* avoid deletion of copied data */
216 image->ncolors = 0;
217 image->colorTable = NULL;
218 info->hints_cmt = NULL;
219 info->colors_cmt = NULL;
220 info->pixels_cmt = NULL;
221 }
222 }
223 /* end 3.2 bc */
224 if (attributes->valuemask & XpmReturnExtensions) {
225 attributes->extensions = info->extensions;
226 attributes->nextensions = info->nextensions;
227
228 /* avoid deletion of copied data */
229 info->extensions = NULL;
230 info->nextensions = 0;
231 }
232 if (info->valuemask & XpmHotspot) {
233 attributes->valuemask |= XpmHotspot;
234 attributes->x_hotspot = info->x_hotspot;
235 attributes->y_hotspot = info->y_hotspot;
236 }
237 attributes->valuemask |= XpmCharsPerPixel;
238 attributes->cpp = image->cpp;
239 attributes->valuemask |= XpmSize;
240 attributes->width = image->width;
241 attributes->height = image->height;
242 }
243
244 /*
245 * Free the XpmAttributes structure members
246 * but the structure itself
247 */
248 void
249 XpmFreeAttributes(attributes)
250 XpmAttributes *attributes;
251 {
252 if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
253 XpmFree(attributes->pixels);
254 attributes->pixels = NULL;
255 attributes->npixels = 0;
256 }
257 if (attributes->valuemask & XpmReturnColorTable) {
258 xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
259 attributes->colorTable = NULL;
260 attributes->ncolors = 0;
261 }
262 /* 3.2 backward compatibility code */
263 else if (attributes->valuemask & XpmInfos) {
264 if (attributes->colorTable) {
265 FreeOldColorTable((XpmColor **) attributes->colorTable,
266 attributes->ncolors);
267 attributes->colorTable = NULL;
268 attributes->ncolors = 0;
269 }
270 if (attributes->hints_cmt) {
271 XpmFree(attributes->hints_cmt);
272 attributes->hints_cmt = NULL;
273 }
274 if (attributes->colors_cmt) {
275 XpmFree(attributes->colors_cmt);
276 attributes->colors_cmt = NULL;
277 }
278 if (attributes->pixels_cmt) {
279 XpmFree(attributes->pixels_cmt);
280 attributes->pixels_cmt = NULL;
281 }
282 if (attributes->pixels) {
283 XpmFree(attributes->pixels);
284 attributes->pixels = NULL;
285 attributes->npixels = 0;
286 }
287 }
288 /* end 3.2 bc */
289 if (attributes->valuemask & XpmReturnExtensions
290 && attributes->nextensions) {
291 XpmFreeExtensions(attributes->extensions, attributes->nextensions);
292 attributes->extensions = NULL;
293 attributes->nextensions = 0;
294 }
295 if (attributes->valuemask & XpmReturnAllocPixels
296 && attributes->nalloc_pixels) {
297 XpmFree(attributes->alloc_pixels);
298 attributes->alloc_pixels = NULL;
299 attributes->nalloc_pixels = 0;
300 }
301 attributes->valuemask = 0;
302 }