Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/uxpm.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.2 $
3 *---------------------------------------------------------------------
4 *
5 *
6 * Copyright (c) 1990-1992, Visual Edge Software Ltd.
7 *
8 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
9 * distribute this software and its documentation for any purpose
10 * and without fee is hereby granted, provided that the above
11 * copyright notice appear in all copies and that both that
12 * copyright notice and this permission notice appear in supporting
13 * documentation, and that the name of Visual Edge Software not be
14 * used in advertising or publicity pertaining to distribution of
15 * the software without specific, written prior permission. The year
16 * included in the notice is the year of the creation of the work.
17 *-------------------------------------------------------------------*/
18
19 /*
20 .VERSION
21 090818 last modif
22 */
23
24 /*-------------------------------------------------------------------------
25 * uxpm.c
26 * Create Pixmaps from xpm format files, either by reading the
27 * file, or by including it and compiling it in.
28 *------------------------------------------------------------------------*/
29
30 #include <X11/Intrinsic.h>
31
32 #ifndef RUNTIME
33 #include "global.h"
34 #include "veos.h"
35 #include "swidget.h"
36 #include "Ux.h"
37 #endif /* ! RUNTIME */
38
39 #ifdef RUNTIME
40 #include "UxLib.h"
41 #endif /* RUNTIME */
42
43 #include "uxpm.h"
44 #include "xpm.h"
45
46 #include "uimx_cat.h"
47 #include "uxpm_ds.h"
48 #define CGETS(ms) UXCATGETS(MC_UXPM,MS_UXPM_,ms)
49
50 extern int UxXpmReadFileToPixmap();
51
52
53 /***************************************************************************
54 NAME: UxReadPixmapFile(display, d, colormap, filename, width, height,
55 x_hot, y_hot, depth, pixmap)
56 INPUT: Display *display;
57 Drawable d;
58 Colormap colormap;
59 char *filename;
60 unsigned int depth;
61 OUTPUT: unsigned int *width, *height;
62 int *x_hot, *y_hot;
63 Pixmap *pixmap;
64 RETURN: int NO_ERROR if PixmapSuccess, otherwise ERROR
65 DESCRIPTION: create a pixmap from an XPM file.
66 EXT REFERENCES: ---
67 EXT EFFECTS: ---
68 CREATION: Apr 23/90
69 MODIFIED: Dec 11 92 fix3781 - Pass the widget
70
71 ---------------------------------------------------------------------------*/
72 int UxReadPixmapFile(display, d, w, colormap, filename, width, height,
73 x_hot, y_hot, depth, pixmap)
74 Display *display;
75 Drawable d;
76 Widget w;
77 Colormap colormap;
78 char *filename;
79 unsigned int *width, *height;
80 int *x_hot, *y_hot;
81 unsigned int depth;
82 Pixmap *pixmap;
83 {
84 XpmAttributes xpm_attributes;
85
86 /* Set the Colormap we want used. */
87 xpm_attributes.valuemask = XpmColormap;
88 xpm_attributes.colormap = colormap;
89
90 /* Try reading the pixmap file.*/
91 if (UxXpmReadFileToPixmap(display, d, w, filename, pixmap, NULL,
92 &xpm_attributes) == XpmSuccess) {
93 /* On success, set return values. */
94 *width = xpm_attributes.width;
95 *height = xpm_attributes.height;
96 *x_hot = xpm_attributes.x_hotspot;
97 *y_hot = xpm_attributes.y_hotspot;
98 return (NO_ERROR);
99 }
100 /* Pixmap couldn't be created. */
101 else
102 return (ERROR);
103 }
104
105 /***************************************************************************
106 NAME: UxReadPixmapOrBitmapFile(display, d, colormap, filename, width,
107 height, depth, pixmap, x_hot, y_hot)
108 INPUT: Display *display;
109 Drawable d;
110 Colormap colormap;
111 char *filename;
112 unsigned int depth;
113 OUTPUT: unsigned int *width, *height; - width & height, returned
114 Pixmap *pixmap;
115 int *x_hot, *y_hot; - hot-spot coordinates, returned
116 RETURN: int ERROR or NO_ERROR
117 DESCRIPTION: Create a pixmap from either a bitmap file or XPM file.
118 EXT REFERENCES: ---
119 EXT EFFECTS: ---
120 CREATION: Apr 20/90
121 MODIFIED: Dec 11 92 fix3781 - Pass the widget
122
123 ---------------------------------------------------------------------------*/
124 int UxReadPixmapOrBitmapFile(display, d, w, colormap, filename, width, height,
125 depth, pixmap, x_hot, y_hot, which)
126 Display *display;
127 Drawable d;
128 Widget w;
129 Colormap colormap;
130 char *filename;
131 unsigned int *width, *height;
132 unsigned int depth;
133 Pixmap *pixmap;
134 int *x_hot, *y_hot;
135 int *which;
136 {
137 Pixmap my_pixmap;
138 unsigned int my_width, my_height;
139 int my_x_hot, my_y_hot;
140
141 /* Try treating file as an X11 Bitmap File. */
142 if(XReadBitmapFile(display, d, filename, &my_width, &my_height,
143 &my_pixmap, &my_x_hot, &my_y_hot)==BitmapSuccess) {
144 /* it's a bitmap ! */
145 if (pixmap != NULL)
146 *pixmap = my_pixmap;
147 if (width != NULL)
148 *width = my_width;
149 if (height != NULL)
150 *height = my_height;
151 if (x_hot != NULL)
152 *x_hot = my_x_hot;
153 if (y_hot != NULL)
154 *y_hot = my_y_hot;
155 if (which != NULL)
156 *which = UX_IS_A_BITMAP;
157 return (NO_ERROR);
158 }
159 /* Try treating it as an XPM 3 pixmap file. */
160 else if (UxReadPixmapFile(display, d, w, colormap, filename, &my_width,
161 &my_height, &my_x_hot, &my_y_hot, depth, pixmap)==NO_ERROR) {
162 if (width != NULL)
163 *width = my_width;
164 if (height != NULL)
165 *height = my_height;
166 if (x_hot != NULL)
167 *x_hot = my_x_hot;
168 if (y_hot != NULL)
169 *y_hot = my_y_hot;
170 if (which != NULL)
171 *which = UX_IS_A_PIXMAP;
172 return (NO_ERROR);
173 }
174 /* Neither tries worked, so it must be an unknown file format. */
175 return (ERROR);
176 }
177
178 /***************************************************************************
179 NAME: UxLoadPixmapFromPixmapOrBitmapFile(sw, fname, pixmap_ptr,
180 width_ptr, height_ptr)
181 INPUT: swidget sw; - swidget to get colors from
182 char *fname; - the filename to load
183 OUTPUT: Pixmap *pixmap_ptr; - ptr to a Pixmap variable
184 unsigned *width_ptr, *height_ptr; - ptr to return width & height
185 RETURN: int - ERROR or NO_ERROR
186 DESCRIPTION: The named file is read as a Bitmap file, with XReadBitmapFile().
187 If an error results, it is then read as a Pixmap file.
188 If another error results, ERROR is returned.
189 Otherwise, a pixmap of the same depth as the root window of
190 UxDisplay is returned in *pixmap_ptr, if that pointer is
191 non-NULL. Bitmaps are 'deepened' to the appropriate depth.
192 If width_ptr and height_ptr are non-NULL, then they are used
193 to return the width and height of the resulting Pixmap.
194 In the case of Bitmaps, an attempt is made to select
195 appropriate colours. The filename should be expanded,
196 if desired, before calling this function, since no
197 processing of the filename is done here.
198 EXT REFERENCES: UxPixmapColorWidget, UxDisplay
199 EXT EFFECTS: ---
200 CREATION: Apr 20/90
201
202 ---------------------------------------------------------------------------*/
203 int UxLoadPixmapFromPixmapOrBitmapFile(sw, fname, pixmap_ptr,
204 width_ptr, height_ptr,fore,back)
205 swidget sw;
206 char *fname;
207 Pixmap *pixmap_ptr;
208 unsigned *width_ptr, *height_ptr;
209 Pixel fore, back;
210 {
211 int screen, depth;
212 unsigned int width, height;
213 int which;
214 Window rw;
215 GC pixmapGC;
216 XGCValues gcv;
217 Pixel UxName_to_pixel();
218 Pixmap pmap, qmap;
219 Colormap c;
220
221 if (fname == NULL)
222 return(ERROR);
223
224 screen = DefaultScreen(UxDisplay);
225 rw = RootWindow(UxDisplay, screen);
226 depth = DisplayPlanes(UxDisplay, screen);
227 c = DefaultColormap(UxDisplay, screen);
228
229 if (UxReadPixmapOrBitmapFile(UxDisplay, rw, UxGetWidget(sw), c, fname, &width, &height,
230 depth, &pmap, (int *)NULL, (int *)NULL, &which ) != NO_ERROR)
231 return (ERROR);
232
233 if (width_ptr != NULL)
234 *width_ptr = width;
235 if (height_ptr != NULL)
236 *height_ptr = height;
237
238 if (which == UX_IS_A_PIXMAP) {
239 if (pixmap_ptr != NULL)
240 *pixmap_ptr = pmap;
241 else
242 XFreePixmap(UxDisplay, pmap);
243 return (NO_ERROR);
244 }
245
246 /* found a bitmap--must deepen it */
247 gcv.foreground = fore;
248 gcv.background = back;
249
250 pixmapGC=XCreateGC(UxDisplay, UxRootWindow,
251 GCForeground | GCBackground, &gcv);
252
253 qmap = XCreatePixmap(UxDisplay, rw, width, height, depth);
254
255 XCopyPlane(UxDisplay, pmap, qmap, pixmapGC, 0, 0, width, height, 0, 0, 1L);
256
257 XFreeGC(UxDisplay, pixmapGC);
258
259 XFreePixmap(UxDisplay, pmap);
260
261 if (pixmap_ptr != NULL)
262 *pixmap_ptr = qmap;
263 else
264 XFreePixmap(UxDisplay, qmap);
265
266 return (NO_ERROR);
267 }