~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/3rdparty/qmon/Xmt310/Xmt/GetPixmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Motif Tools Library, Version 3.1
 
3
 * $Id$
 
4
 * 
 
5
 * Written by David Flanagan.
 
6
 * Copyright (c) 1992-2001 by David Flanagan.
 
7
 * All Rights Reserved.  See the file COPYRIGHT for details.
 
8
 * This is open source software.  See the file LICENSE for details.
 
9
 * There is no warranty for this software.  See NO_WARRANTY for details.
 
10
 *
 
11
 * $Log$
 
12
 * Revision 1.1.1.1  2001/07/18 11:06:02  root
 
13
 * Initial checkin.
 
14
 *
 
15
 * Revision 1.2  2001/06/12 16:25:28  andre
 
16
 * *** empty log message ***
 
17
 *
 
18
 *
 
19
 */
 
20
 
 
21
#include <stdio.h>
 
22
#include <ctype.h>
 
23
#include <unistd.h>
 
24
#include <stdlib.h>
 
25
 
 
26
#include <Xmt/Xmt.h>
 
27
#include <Xmt/Pixmap.h>
 
28
#include <Xmt/Xpm.h>
 
29
#include <Xmt/Xbm.h>
 
30
#include <Xmt/Color.h>
 
31
#include <Xmt/AppResP.h>
 
32
#include <Xmt/ConvertersP.h>
 
33
#include <Xmt/LookupP.h>
 
34
#include <X11/IntrinsicP.h>
 
35
 
 
36
#if NeedFunctionPrototypes
 
37
static Pixmap GetPixmap(Widget object, StringConst str, XmtColorTable table,
 
38
                        Boolean get_bitmap)
 
39
#else
 
40
static Pixmap GetPixmap(object, str, table, get_bitmap)
 
41
Widget object;
 
42
StringConst str;
 
43
XmtColorTable table;
 
44
Boolean get_bitmap;
 
45
#endif
 
46
{
 
47
    Widget w, shell;
 
48
    Display *display;
 
49
    Screen *screen;
 
50
    Visual *visual;
 
51
    Colormap colormap;
 
52
    unsigned int depth;
 
53
    XmtAppResources *app;
 
54
    XmtImage *xmtimage = NULL;
 
55
    struct {
 
56
        char *bits;
 
57
        int width, height, hot_x, hot_y;
 
58
    } xbmdata;
 
59
    Pixmap pixmap = None;
 
60
    String name = NULL;
 
61
    String table_string;
 
62
    String filename = NULL;
 
63
    String path = NULL;
 
64
    Boolean pixmap_file = False;
 
65
    Boolean bitmap_file = False;
 
66
    XtCacheRef color_table_cache_ref;
 
67
    XtCacheRef refs[2];
 
68
    Boolean free_color_table = False;
 
69
    static int unique_image_number;
 
70
 
 
71
    for(w=object; !XtIsWidget(w); w = XtParent(w));
 
72
    shell = XmtGetShell(w);
 
73
 
 
74
    app = XmtGetApplicationResources(shell);
 
75
    screen = XtScreen(w);
 
76
    display = DisplayOfScreen(screen);
 
77
    visual = XmtGetVisual(shell);
 
78
    colormap = w->core.colormap;
 
79
    depth = w->core.depth;
 
80
    if (!table) table = app->colortable;
 
81
 
 
82
    /*
 
83
     * see if it is a literal XPM file
 
84
     */
 
85
    if (!get_bitmap && strncmp(str, "/* XPM */", 9) == 0) {
 
86
        xmtimage = XmtParseXpmString(str);
 
87
        if (xmtimage) {
 
88
            name = XtMalloc(8);
 
89
            sprintf(name, "_%d_", unique_image_number++);
 
90
            goto found;
 
91
        }
 
92
        else return None;
 
93
    }
 
94
 
 
95
    /*
 
96
     * see if it is a literal XBM file
 
97
     */
 
98
    xbmdata.bits = NULL;
 
99
    if (strncmp(str, "#define", 7) == 0) {
 
100
        if (XmtParseXbmString(str, &xbmdata.bits,
 
101
                              &xbmdata.width, &xbmdata.height,
 
102
                              &xbmdata.hot_x, &xbmdata.hot_y)) {
 
103
            name = XtMalloc(16);
 
104
            sprintf(name, "_Xmt%d", unique_image_number++);
 
105
            goto found;
 
106
        }
 
107
        else
 
108
            return None;
 
109
    }
 
110
 
 
111
    /*
 
112
     * Otherwise it is a single name, optionally followed by a color table.
 
113
     * If there is a colon, parse the color table, and get a null-terminated
 
114
     * copy of the single name.
 
115
     * In either case, name is set to the image name.
 
116
     * if (name != str) then name must be freed later.
 
117
     */
 
118
    table_string = strchr(str, ':');
 
119
    if (table_string) {
 
120
        register int i;
 
121
 
 
122
        table_string++; /* points to first char after colon */
 
123
        if (!get_bitmap) {
 
124
            if (_XmtColorTableConverter != NULL) {
 
125
                XrmValue from, to;
 
126
                XrmValue color_table_args[2];
 
127
                XmtColorTable parent_table;
 
128
                Boolean status;
 
129
 
 
130
                parent_table = table;
 
131
                from.addr = table_string;
 
132
                from.size = strlen(table_string);
 
133
                to.addr = (XPointer)&table;
 
134
                to.size = sizeof(XmtColorTable);
 
135
                color_table_args[0].addr = (XPointer)&parent_table;
 
136
                color_table_args[0].size = sizeof(XmtColorTable);
 
137
                color_table_args[1].addr = (XPointer)&screen;
 
138
                color_table_args[1].size = sizeof(Screen *);
 
139
                status = XtCallConverter(display, _XmtColorTableConverter,
 
140
                                         color_table_args, 2, 
 
141
                                         &from, &to, &color_table_cache_ref);
 
142
                if (!status)
 
143
                    XmtWarningMsg("XmtGetPixmap", "badColor",
 
144
                                  "continuing with default color table.");
 
145
                else
 
146
                    free_color_table = True;
 
147
            }
 
148
            else {
 
149
                XmtWarningMsg("XmtGetPixmap", "noColor",
 
150
                              "No XmtColorTable converter registered.\n\tSee XmtRegisterColorTableConverter();");
 
151
            }
 
152
        }
 
153
        else {
 
154
            XmtWarningMsg("XmtGetBitmap", "table",
 
155
                    "color table specification unused in bitmap conversion.");
 
156
        }
 
157
            
 
158
        table_string -= 2;  /* points to char before colon */
 
159
        for(; isspace(*table_string); table_string--);  /* skip over blanks*/
 
160
        i = (table_string - str) + 1;
 
161
        name = strncpy(XtMalloc(i+1), str, i);
 
162
        name[i] = '\0';
 
163
    }
 
164
    else
 
165
        name = (String) str;
 
166
 
 
167
    /*
 
168
     * see if name is defined in the image cache
 
169
     */
 
170
    if (get_bitmap)
 
171
        pixmap = XmtLookupBitmap(w, name);
 
172
    else
 
173
        pixmap = XmtLookupPixmap(w, visual, colormap, depth, table, name);
 
174
    
 
175
    if (pixmap) goto end;
 
176
    else {
 
177
        /*
 
178
         * If it is not in the image cache, check if it is the name of a
 
179
         * resource under _Pixmaps_ or _Bitmaps_.  We lookup the image
 
180
         * as:
 
181
         *    _Pixmaps_.visual.depth.resolution.language.territory.codeset.name
 
182
         * For backward compatibility with Xmt 1.2, we also do just:
 
183
         *    _Pixmaps_.name
 
184
         * because in 1.2 '.name' was more common than '*name'
 
185
         *
 
186
         * For bitmaps, we use _Bitmaps_, and omit the visual and depth.
 
187
         */
 
188
        String value;
 
189
 
 
190
        if (!get_bitmap) {
 
191
            value = _XmtLookupResource(screen, "PVDZltc", name);
 
192
            if (value) {
 
193
                if ((xmtimage = XmtParseXpmString(value))) goto found;
 
194
                else goto end;
 
195
            }
 
196
        }
 
197
 
 
198
        value = _XmtLookupResource(screen, "BZltc", name);
 
199
        if (value) {
 
200
            if (XmtParseXbmString(value, &xbmdata.bits,
 
201
                                  &xbmdata.width, &xbmdata.height,
 
202
                                  &xbmdata.hot_x, &xbmdata.hot_y))
 
203
                goto found;
 
204
            else goto end;
 
205
        }
 
206
        
 
207
        /* if still not found, look up the old way, for compatibility */
 
208
        if (!get_bitmap) {
 
209
            value = _XmtLookupResource(screen, "P", name);
 
210
            if (value) {
 
211
                if ((xmtimage = XmtParseXpmString(value))) goto found;
 
212
                else goto end;
 
213
            }
 
214
        }
 
215
        
 
216
        value = _XmtLookupResource(screen, "B", name);
 
217
        if (value) {
 
218
            if (XmtParseXbmString(value, &xbmdata.bits,
 
219
                                  &xbmdata.width, &xbmdata.height,
 
220
                                  &xbmdata.hot_x, &xbmdata.hot_y))
 
221
                goto found;
 
222
            else goto end;
 
223
        }
 
224
    }
 
225
 
 
226
    /*
 
227
     * if we still haven't found a pixmap, name must be a filename
 
228
     */
 
229
    if (pixmap == None) {
 
230
        /*
 
231
         * handle absolute and relative filenames
 
232
         */
 
233
        if ((name[0] == '/') ||
 
234
            ((name[0] == '.') && (name[1] == '/')) ||
 
235
            ((name[0] == '.') && (name[1] == '.') && (name[2] == '/')))
 
236
            filename = name;
 
237
 
 
238
        /*
 
239
         * check relative to an environment variable, if defined
 
240
         * Note that since we don't know the path, we don't know for
 
241
         * sure that it uses the supplied type and suffix, so we don't
 
242
         * know what type of file is being found
 
243
         */
 
244
        if (!get_bitmap && !filename && (path = getenv("XPMLANGPATH"))) {
 
245
            filename = XmtFindFile(shell, "pixmaps", name, ".xpm",
 
246
                                   NULL, path, XmtSearchPathOnly);
 
247
            if (filename) pixmap_file = True;
 
248
        }
 
249
 
 
250
        if (!filename && (path = getenv("XBMLANGPATH"))) {
 
251
            filename = XmtFindFile(shell, "bitmaps", name, ".xbm",
 
252
                                   NULL, path, XmtSearchPathOnly);
 
253
            if (filename) bitmap_file = True;
 
254
        }
 
255
 
 
256
        /* next check the user, app, and system pixmap and bitmap paths */
 
257
        if (!filename) {
 
258
            if (!get_bitmap) {
 
259
                filename = XmtFindFile(shell, "pixmaps", name, ".xpm",
 
260
                                       NULL, app->pixmap_file_path,
 
261
                                       XmtSearchEverywhere);
 
262
                if (filename)
 
263
                    pixmap_file = True;
 
264
            }
 
265
            if (!filename) {
 
266
                filename = XmtFindFile(shell, "bitmaps", name, ".xbm",
 
267
                                       NULL, app->bitmap_file_path,
 
268
                                       XmtSearchEverywhere);
 
269
                if (filename)
 
270
                    bitmap_file = True;
 
271
            }
 
272
        }
 
273
 
 
274
        /*
 
275
         * if we found a file, read the first few bytes to determine the
 
276
         * type.  Read it as appropriate.  Register the resulting
 
277
         * pixmap in the cache using the filename as the key.
 
278
         */
 
279
        if (filename) {
 
280
            FILE *f;
 
281
            char buf[10];
 
282
 
 
283
            if (!pixmap_file && !bitmap_file) {
 
284
                if (get_bitmap) bitmap_file = True;
 
285
                else if ((f = fopen(filename, "r")) != NULL) {
 
286
                    (void)fgets(buf, 10, f);
 
287
                    if (strncmp(buf, "/* XPM */", 9) == 0)
 
288
                        pixmap_file = True;
 
289
                    else if (strncmp(buf, "#define", 7) == 0)
 
290
                        bitmap_file = True;
 
291
                    (void)fclose(f);
 
292
                }
 
293
            }
 
294
 
 
295
            if (pixmap_file || (!pixmap_file && !bitmap_file)) {
 
296
                xmtimage = XmtParseXpmFile(filename);
 
297
                if (xmtimage) goto found;
 
298
            }
 
299
 
 
300
            if (bitmap_file || (!pixmap_file && !bitmap_file)) {
 
301
                if (XmtParseXbmFile(filename, &xbmdata.bits,
 
302
                                    &xbmdata.width, &xbmdata.height,
 
303
                                    &xbmdata.hot_x, &xbmdata.hot_y))
 
304
                    goto found;
 
305
            }
 
306
        }
 
307
    }
 
308
 
 
309
 found:
 
310
    /*
 
311
     * register the xpm or xbm data, and then get a pixmap for it.
 
312
     * we jump here when we successfully parse a xpm or xbm file or string.
 
313
     */
 
314
    if (xmtimage)
 
315
        XmtRegisterImage(name, xmtimage);
 
316
    else if (xbmdata.bits)
 
317
        XmtRegisterXbmData(name, xbmdata.bits, NULL,
 
318
                           xbmdata.width, xbmdata.height,
 
319
                           xbmdata.hot_x, xbmdata.hot_y);
 
320
 
 
321
    if (xmtimage || xbmdata.bits) {
 
322
        if (get_bitmap)
 
323
            pixmap = XmtLookupBitmap(w, name);
 
324
        else
 
325
            pixmap = XmtLookupPixmap(w, visual, colormap,
 
326
                                     depth, table, name);
 
327
    }
 
328
    
 
329
 end:
 
330
    /*
 
331
     * free what we need to and return.
 
332
     * we jump here if the pixmap is already cached, or on error.
 
333
     */
 
334
    if (filename && filename != name) XtFree(filename);
 
335
    if (name && name != str) XtFree(name);
 
336
    if (free_color_table) {
 
337
        refs[0] = color_table_cache_ref;
 
338
        refs[1] = NULL;
 
339
        XtAppReleaseCacheRefs(XtWidgetToApplicationContext(w), refs);
 
340
    }
 
341
    return pixmap;
 
342
}
 
343
 
 
344
#if NeedFunctionPrototypes
 
345
Pixmap XmtGetBitmap(Widget object, StringConst str)
 
346
#else
 
347
Pixmap XmtGetBitmap(object, str)
 
348
Widget object;
 
349
StringConst str;
 
350
#endif
 
351
{
 
352
    return GetPixmap(object, str, NULL, True);
 
353
}
 
354
 
 
355
#if NeedFunctionPrototypes
 
356
Pixmap XmtGetPixmap(Widget object, XmtColorTable table, StringConst str)
 
357
#else
 
358
Pixmap XmtGetPixmap(object, table, str)
 
359
Widget object;
 
360
XmtColorTable table;
 
361
StringConst str;
 
362
#endif
 
363
{
 
364
    return GetPixmap(object, str, table, False);
 
365
}