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

« back to all changes in this revision

Viewing changes to source/3rdparty/qmon/Xmt310/Xmt/Pixmap.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 <Xmt/Xmt.h>
 
22
#include <Xmt/Pixmap.h>
 
23
#include <Xmt/Xpm.h>
 
24
#include <Xmt/Hash.h>
 
25
#include <Xmt/Color.h>
 
26
#include <Xmt/AppResP.h>
 
27
#include <X11/IntrinsicP.h>
 
28
 
 
29
static XmtHashTable image_cache = NULL;
 
30
static XmtHashTable pixmap_to_image_table = NULL;
 
31
 
 
32
typedef enum {
 
33
    PixmapImage, PixmapMask
 
34
} PixmapType;
 
35
 
 
36
typedef struct _PixmapRec {
 
37
    PixmapType type;
 
38
    Pixmap pixmap;
 
39
    Screen *screen;
 
40
    Visual *visual;
 
41
    Colormap colormap;
 
42
    unsigned int depth;
 
43
    XmtColorTable table;
 
44
    short refcount;
 
45
    Pixel *allocated_pixels;    /* free these pixels when done w/ pixmap */
 
46
    short num_allocated_pixels; 
 
47
    struct _PixmapRec *next;
 
48
    struct _ImageCacheEntry *listhead;
 
49
} PixmapRec, *PixmapList;
 
50
 
 
51
typedef enum {
 
52
    ImageTypeXbm, ImageTypeXpm
 
53
} ImageType;    
 
54
 
 
55
typedef struct {
 
56
    char *imagedata;
 
57
    char *maskdata;
 
58
    unsigned short width, height;
 
59
    XPoint hotspot;
 
60
} XbmImage;
 
61
 
 
62
typedef struct _ImageCacheEntry {
 
63
    ImageType type;
 
64
    union {
 
65
        XbmImage *xbm;
 
66
        XmtImage *xpm;
 
67
    } data;
 
68
    Boolean orphan;
 
69
    PixmapList list;
 
70
} ImageCacheEntry;
 
71
 
 
72
#if NeedFunctionPrototypes
 
73
static void FreeImageCacheEntry(ImageCacheEntry *ie)
 
74
#else
 
75
static void FreeImageCacheEntry(ie)
 
76
ImageCacheEntry *ie;
 
77
#endif
 
78
{
 
79
    if (ie->type == ImageTypeXbm) {
 
80
        XtFree((char *)ie->data.xbm);
 
81
    }
 
82
    XtFree((char *)ie);
 
83
}
 
84
 
 
85
#if NeedFunctionPrototypes
 
86
static void RegisterImage(StringConst name, ImageType type, XtPointer data)
 
87
#else
 
88
static void RegisterImage(name, type, data)
 
89
StringConst name;
 
90
ImageType type;
 
91
XtPointer data;
 
92
#endif
 
93
{
 
94
    XrmQuark q = XrmStringToQuark(name);
 
95
    ImageCacheEntry *ie;
 
96
    
 
97
    if (image_cache == NULL) {
 
98
        image_cache = XmtHashTableCreate(3);
 
99
        pixmap_to_image_table = XmtHashTableCreate(3);
 
100
    }
 
101
 
 
102
    /*
 
103
     * if there is already an image by that name, free the old image
 
104
     * if possible, or make it an orphan so it gets cleaned up when
 
105
     * all of its outstanding pixmaps have been released.
 
106
     */
 
107
    if (XmtHashTableLookup(image_cache, (XtPointer)q, (XtPointer *)&ie)) {
 
108
        if (ie->list == NULL) FreeImageCacheEntry(ie);
 
109
        else ie->orphan = True;
 
110
    }
 
111
 
 
112
    ie = XtNew(ImageCacheEntry);
 
113
    ie->type = type;
 
114
    if (type == ImageTypeXbm) ie->data.xbm = (XbmImage *) data;
 
115
    else ie->data.xpm = (XmtImage *)data;
 
116
    ie->orphan = False;
 
117
    ie->list = NULL;
 
118
    XmtHashTableStore(image_cache, (XtPointer)q, (XtPointer)ie);
 
119
}
 
120
 
 
121
 
 
122
#if NeedFunctionPrototypes
 
123
void XmtRegisterXbmData(StringConst name, char *imagedata, char *maskdata,
 
124
                        int width, int height, int hotspot_x, int hotspot_y)
 
125
#else
 
126
void XmtRegisterXbmData(name, imagedata, maskdata, width, height,
 
127
                        hotspot_x, hotspot_y)
 
128
StringConst name;
 
129
char *imagedata;
 
130
char *maskdata;
 
131
int width;
 
132
int height;
 
133
int hotspot_x;
 
134
int hotspot_y;
 
135
#endif
 
136
{
 
137
    XbmImage *data;
 
138
 
 
139
    data = XtNew(XbmImage);
 
140
    data->imagedata = imagedata;
 
141
    data->maskdata = maskdata;
 
142
    data->width = width;
 
143
    data->height = height;
 
144
    data->hotspot.x = hotspot_x;
 
145
    data->hotspot.y = hotspot_y;
 
146
    
 
147
    RegisterImage(name, ImageTypeXbm, (XtPointer)data);
 
148
}
 
149
    
 
150
#if NeedFunctionPrototypes
 
151
void XmtRegisterImage(StringConst name, XmtImage *data)
 
152
#else
 
153
void XmtRegisterImage(name, data)
 
154
StringConst name;
 
155
XmtImage *data;
 
156
#endif
 
157
{
 
158
    RegisterImage(name, ImageTypeXpm, (XtPointer)data);
 
159
}
 
160
 
 
161
 
 
162
#if NeedFunctionPrototypes
 
163
Pixmap XmtLookupBitmap(Widget widget, StringConst name)
 
164
#else
 
165
Pixmap XmtLookupBitmap(widget, name)
 
166
Widget widget;
 
167
StringConst name;
 
168
#endif
 
169
{
 
170
    Screen *screen = XtScreenOfObject(widget);
 
171
    ImageCacheEntry *ie;
 
172
    PixmapRec *p;
 
173
 
 
174
    if (image_cache == NULL) return None;
 
175
 
 
176
    /* if nothing by that name, return None */
 
177
    if (!XmtHashTableLookup(image_cache, (XtPointer)XrmStringToQuark(name),
 
178
                            (XtPointer *)&ie))
 
179
        return None;
 
180
 
 
181
    /* if it is not bitmap data, return None*/
 
182
    if (ie->type != ImageTypeXbm) return None;
 
183
 
 
184
    /* if there is no image data, return None */
 
185
    if (ie->data.xbm->imagedata == NULL) return None;
 
186
 
 
187
    /* go see if a 1-bit image pixmap has  been created for this screen */
 
188
    for (p=ie->list; p; p = p->next) {
 
189
        if ((p->type == PixmapImage) &&
 
190
            (p->screen == screen) &&
 
191
            (p->depth == 1)) {
 
192
            p->refcount++;
 
193
            return p->pixmap;
 
194
        }
 
195
    }
 
196
 
 
197
    /* otherwise create it and add to the list */
 
198
    p = XtNew(PixmapRec);
 
199
    p->type = PixmapImage;
 
200
    p->pixmap = XCreateBitmapFromData(DisplayOfScreen(screen),
 
201
                                      RootWindowOfScreen(screen),
 
202
                                      ie->data.xbm->imagedata,
 
203
                                      ie->data.xbm->width,
 
204
                                      ie->data.xbm->height);
 
205
    p->screen = screen;
 
206
    p->visual = NULL;
 
207
    p->colormap = None;
 
208
    p->depth = 1;
 
209
    p->table = NULL;
 
210
    p->refcount = 1;
 
211
    p->allocated_pixels = NULL;
 
212
    p->num_allocated_pixels = 0;
 
213
    p->listhead = ie;
 
214
    p->next = ie->list;
 
215
    ie->list = p;
 
216
 
 
217
    /*
 
218
     * map the bitmap back to its list entry, so that we
 
219
     * can decrement its reference count when done with it.
 
220
     */
 
221
    XmtHashTableStore(pixmap_to_image_table,
 
222
                      (XtPointer) p->pixmap, (XtPointer)p);
 
223
 
 
224
    return p->pixmap;
 
225
}
 
226
 
 
227
 
 
228
#if NeedFunctionPrototypes
 
229
Pixmap XmtLookupPixmap(Widget widget, Visual *visual, Colormap colormap,
 
230
                       unsigned int depth, XmtColorTable table,
 
231
                       StringConst name)
 
232
#else
 
233
Pixmap XmtLookupPixmap(widget, visual, colormap, depth, table, name)
 
234
Widget widget;
 
235
Visual *visual;
 
236
Colormap colormap;
 
237
unsigned int depth;
 
238
XmtColorTable table;
 
239
StringConst name;
 
240
#endif
 
241
{
 
242
    Screen *screen = XtScreenOfObject(widget);
 
243
    ImageCacheEntry *ie;
 
244
    PixmapRec *p;
 
245
    Boolean status;
 
246
    Pixmap pixmap;
 
247
    Pixel fg, bg;
 
248
    Pixel *pixels;
 
249
    int num_pixels;
 
250
 
 
251
    if (image_cache == NULL) return None;
 
252
 
 
253
    /* if nothing by that name, return None */
 
254
    if (!XmtHashTableLookup(image_cache, (XtPointer)XrmStringToQuark(name),
 
255
                            (XtPointer *)&ie))
 
256
        return None;
 
257
 
 
258
    /* if xbm format with no image data, return None */
 
259
    if (ie->type == ImageTypeXbm && ie->data.xbm->imagedata == NULL)
 
260
        return None;
 
261
 
 
262
    /* go see if an appropriate Pixmap is already created */
 
263
    for (p=ie->list; p; p = p->next) {
 
264
        if ((p->type == PixmapImage) &&
 
265
            (p->screen == screen) &&
 
266
            (p->visual == visual) &&
 
267
            (p->colormap == colormap) &&
 
268
            (p->depth == depth) &&
 
269
            (p->table == table)) {
 
270
            p->refcount++;
 
271
            return p->pixmap;
 
272
        }
 
273
    }
 
274
 
 
275
    /* otherwise go create it */
 
276
    if (ie->type == ImageTypeXpm) {
 
277
        status = XmtCreatePixmapFromXmtImage(widget,
 
278
                                             RootWindowOfScreen(screen),
 
279
                                             visual, colormap, depth, table,
 
280
                                             ie->data.xpm,
 
281
                                             &pixmap, NULL,
 
282
                                             &pixels,
 
283
                                             &num_pixels);
 
284
        if (!status) 
 
285
            return None;
 
286
    }
 
287
    else {
 
288
        if (XmtAllocColor(widget, colormap, visual, table,
 
289
                          "+$foreground", &fg) ||
 
290
            XmtAllocColor(widget, colormap, visual, table,
 
291
                          "-$background", &bg)) {
 
292
            XmtWarningMsg("XmtLookupPixmap", "color",
 
293
                          "bitmap '%s'\n\tforeground and/or background colors are not defined in colortable,\n\tor are invalid colors, or the colormap is full.",
 
294
                          name);
 
295
            fg = (Pixel)1;
 
296
            bg = (Pixel)0;
 
297
        }
 
298
        pixmap = XCreatePixmapFromBitmapData(DisplayOfScreen(screen),
 
299
                                             RootWindowOfScreen(screen),
 
300
                                             ie->data.xbm->imagedata,
 
301
                                             ie->data.xbm->width,
 
302
                                             ie->data.xbm->height,
 
303
                                             fg, bg, depth);
 
304
        pixels = NULL;
 
305
        num_pixels = 0;
 
306
    }
 
307
    
 
308
    p = XtNew(PixmapRec);
 
309
    p->type = PixmapImage;
 
310
    p->pixmap = pixmap;
 
311
    p->screen = screen;
 
312
    p->visual = visual;
 
313
    p->colormap = colormap;
 
314
    p->depth = depth;
 
315
    p->table = table;
 
316
    p->refcount = 1;
 
317
    p->allocated_pixels = pixels;
 
318
    p->num_allocated_pixels = num_pixels;
 
319
    p->listhead = ie;
 
320
    p->next = ie->list;
 
321
    ie->list = p;
 
322
 
 
323
    /*
 
324
     * map the pixmap back to its list entry, so that we
 
325
     * can decrement its reference count when done with it.
 
326
     */
 
327
    XmtHashTableStore(pixmap_to_image_table,
 
328
                      (XtPointer) p->pixmap, (XtPointer)p);
 
329
 
 
330
    return p->pixmap;
 
331
}
 
332
 
 
333
 
 
334
#if NeedFunctionPrototypes
 
335
Pixmap XmtLookupBitmask(Widget widget, StringConst name)
 
336
#else
 
337
Pixmap XmtLookupBitmask(widget, name)
 
338
Widget widget;
 
339
StringConst name;
 
340
#endif
 
341
{
 
342
    Screen *screen = XtScreenOfObject(widget);
 
343
    ImageCacheEntry *ie;
 
344
    PixmapRec *p;
 
345
    Boolean status;
 
346
    Pixmap pixmap;
 
347
 
 
348
    if (image_cache == NULL) return None;
 
349
 
 
350
    /* if nothing by that name, return None */
 
351
    if (!XmtHashTableLookup(image_cache, (XtPointer)XrmStringToQuark(name),
 
352
                            (XtPointer *)&ie))
 
353
        return None;
 
354
 
 
355
    /* if xbm format with no mask data, return None */
 
356
    if (ie->type == ImageTypeXbm && ie->data.xbm->maskdata == NULL)
 
357
        return None;
 
358
 
 
359
    /* go see if an appropriate Pixmap is already created */
 
360
    for (p=ie->list; p; p = p->next) {
 
361
        if ((p->type == PixmapMask) &&
 
362
            (p->screen == screen)) {
 
363
            p->refcount++;
 
364
            return p->pixmap;
 
365
        }
 
366
    }
 
367
 
 
368
    /* otherwise go create one */
 
369
    if (ie->type == ImageTypeXbm) {
 
370
        pixmap = XCreateBitmapFromData(DisplayOfScreen(screen),
 
371
                                       RootWindowOfScreen(screen),
 
372
                                       ie->data.xbm->maskdata,
 
373
                                       ie->data.xbm->width,
 
374
                                       ie->data.xbm->height);
 
375
    }
 
376
    else {
 
377
        status = XmtCreatePixmapFromXmtImage(widget,
 
378
                                             RootWindowOfScreen(screen),
 
379
                                             NULL, None, 1, NULL,
 
380
                                             ie->data.xpm, NULL, &pixmap,
 
381
                                             NULL, NULL);
 
382
        if (!status || pixmap == None) return None;
 
383
    }
 
384
 
 
385
    p = XtNew(PixmapRec);
 
386
    p->type = PixmapMask;
 
387
    p->pixmap = pixmap;
 
388
    p->screen = screen;
 
389
    p->visual = NULL;
 
390
    p->colormap = None;
 
391
    p->depth = 1;
 
392
    p->table = NULL;
 
393
    p->refcount = 1;
 
394
    p->allocated_pixels = NULL;
 
395
    p->num_allocated_pixels = 0;
 
396
    p->listhead = ie;
 
397
    p->next = ie->list;
 
398
    ie->list = p;
 
399
 
 
400
    /*
 
401
     * map the pixmap back to its list entry, so that we
 
402
     * can decrement its reference count when done with it.
 
403
     */
 
404
    XmtHashTableStore(pixmap_to_image_table,
 
405
                      (XtPointer) p->pixmap, (XtPointer)p);
 
406
 
 
407
    return p->pixmap;
 
408
}
 
409
 
 
410
 
 
411
#if NeedFunctionPrototypes
 
412
Pixmap XmtLookupSimplePixmap(Widget widget, XmtColorTable table,
 
413
                             StringConst name)
 
414
#else
 
415
Pixmap XmtLookupSimplePixmap(widget, table, name)
 
416
Widget widget;
 
417
XmtColorTable table;
 
418
StringConst name;
 
419
#endif
 
420
{
 
421
    while(!XtIsWidget(widget)) widget = XtParent(widget);
 
422
    return XmtLookupPixmap(widget,
 
423
                           XmtGetVisual(widget),
 
424
                           widget->core.colormap,
 
425
                           widget->core.depth,
 
426
                           table, name);
 
427
}
 
428
 
 
429
 
 
430
#if NeedFunctionPrototypes
 
431
Pixmap XmtLookupWidgetPixmap(Widget w, StringConst name)
 
432
#else
 
433
Pixmap XmtLookupWidgetPixmap(w, name)
 
434
Widget w;
 
435
StringConst name;
 
436
#endif
 
437
{
 
438
    XmtAppResources *app;
 
439
 
 
440
    while(!XtIsWidget(w)) w = XtParent(w);
 
441
    app = XmtGetApplicationResources(w);
 
442
    return XmtLookupPixmap(w,
 
443
                           XmtGetVisual(w),
 
444
                           w->core.colormap,
 
445
                           w->core.depth,
 
446
                           app->colortable, name);
 
447
}
 
448
 
 
449
 
 
450
#if NeedFunctionPrototypes
 
451
void XmtReleasePixmap(Widget widget, Pixmap pixmap)
 
452
#else
 
453
void XmtReleasePixmap(widget, pixmap)
 
454
Widget widget;
 
455
Pixmap pixmap;
 
456
#endif
 
457
{
 
458
    PixmapRec *p, *q;
 
459
    ImageCacheEntry *ie;
 
460
    
 
461
    if (pixmap_to_image_table == NULL) return;
 
462
 
 
463
    if (!XmtHashTableLookup(pixmap_to_image_table, (XtPointer)pixmap,
 
464
                            (XtPointer *)&p))
 
465
        return;
 
466
 
 
467
    /* decrement the refcount */
 
468
    p->refcount--;
 
469
 
 
470
    /*
 
471
     * if the refcount is 0, remove the hash table entry, 
 
472
     * free the pixmap, unlink the record,
 
473
     * free any allocated pixels, and free the record.
 
474
     */
 
475
    if (p->refcount == 0) {
 
476
        XmtHashTableDelete(pixmap_to_image_table, (XtPointer)pixmap);
 
477
        XFreePixmap(DisplayOfScreen(p->screen), p->pixmap);
 
478
        ie = p->listhead;
 
479
        if (p == ie->list) ie->list = p->next;
 
480
        else {
 
481
            for(q = ie->list; q->next != p; q = q->next);
 
482
            q->next = p->next;
 
483
        }
 
484
        if (p->num_allocated_pixels) {
 
485
            int i;
 
486
            for(i=0; i < p->num_allocated_pixels; i++)
 
487
                XmtFreeColor(widget, p->colormap, p->allocated_pixels[i]);
 
488
            XtFree((char *)p->allocated_pixels);
 
489
        }
 
490
        XtFree((char *) p);
 
491
 
 
492
        /*
 
493
         * if that was the last pixmap for this cache entry, and this
 
494
         * cache entry has been orphaned, (ie. not bound to a symbolic
 
495
         * name in the hash table anymore) free the entry.
 
496
         */
 
497
        if (ie->orphan && ie->list == NULL)
 
498
            FreeImageCacheEntry(ie);
 
499
    }
 
500
        
 
501
}
 
502