File: PixmapLstCvt.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (223 lines) | stat: -rw-r--r-- 6,638 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* 
 * Motif Tools Library, Version 3.1
 * $Id$
 * 
 * Written by David Flanagan.
 * Copyright (c) 1992-2001 by David Flanagan.
 * All Rights Reserved.  See the file COPYRIGHT for details.
 * This is open source software.  See the file LICENSE for details.
 * There is no warranty for this software.  See NO_WARRANTY for details.
 *
 * $Log$
 * Revision 1.1.1.1  2001/07/18 11:06:03  root
 * Initial checkin.
 *
 * Revision 1.2  2001/06/12 16:25:28  andre
 * *** empty log message ***
 *
 *
 */

#include <ctype.h>
#include <Xmt/Xmt.h>
#include <Xmt/ConvertersP.h>
#include <Xmt/Pixmap.h>
#include <Xmt/AppResP.h>
#include <Xmt/Color.h>

#define skipblanks(s) while (isspace(*s)) s++

/*
 * A PixmapList is a StringList optionally followed by a colon and
 * a color table.  So we invoke the StringList and ColorTable converters,
 * and then call XmtGetPixmap.
 */

/* ARGSUSED */
#if NeedFunctionPrototypes
Boolean XmtConvertStringToPixmapList(Display *dpy,
				     XrmValue *args, Cardinal *num_args,
				     XrmValue *from, XrmValue *to,
				     XtPointer *converter_data)
#else
Boolean XmtConvertStringToPixmapList(dpy, args, num_args,
				     from, to, converter_data)
Display *dpy;
XrmValue *args;
Cardinal *num_args;
XrmValue *from;
XrmValue *to;
XtPointer *converter_data;
#endif
{
    char *s = (char *)from->addr;
    Widget widget = *(Widget *)args[0].addr;
    XmtAppResources *app = XmtGetApplicationResources(widget);
    char *strlist;
    char *tablestr;
    char *mark;
    Boolean instring;
    Boolean free_string = False;
    Boolean free_table = False;
    Boolean free_string_list = False;
    XrmValue fromval, toval;
    XrmValue color_table_args[2];
    XmtColorTable table, parent_table;
    String *pixmap_names;
    XtCacheRef color_table_cache_ref, string_list_cache_ref;
    XtCacheRef refs[2];
    Boolean status;
    Pixmap *pixmaps;
    int i, num_items;

    table = parent_table = app->colortable;

    /* scan for an unquoted colon */
    for(tablestr = s, instring=False; *tablestr; tablestr++) {
	if (*tablestr == '"') instring = !instring;
	if (!instring && (*tablestr == ':')) break;
    }

    /* if there is one, make a NULL-terminated copy of the leading string list
     */
    if (*tablestr) {
	free_string = True;
	for(mark=tablestr-1; isspace(*mark); mark--);
	strlist = XtMalloc(mark-s+2);
	strncpy(strlist, s, mark-s+1);
	strlist[mark-s+1] = '\0';
    }
    else
	strlist = s;

    /*
     * convert the string list by calling the string list converter
     * directly.  This means we will always link that converter.  No
     * big deal.
     */
    fromval.addr = strlist;
    fromval.size = strlen(strlist) + 1;
    toval.addr = (XPointer) &pixmap_names;
    toval.size = sizeof(String *);
    status = XtCallConverter(dpy, XmtConvertStringToStringList, NULL, 0,
			     &fromval, &toval, &string_list_cache_ref);
    if (!status) goto error;
    else free_string_list = True;
    
    /*
     * convert the color table, if defined, and if a converter is registered.
     */
    if (*tablestr) {
	tablestr++;  /* skip colon */
	for(; isspace(*tablestr); tablestr++);
	if (_XmtColorTableConverter == NULL) {
	    XmtWarningMsg("XmtConvertStringToPixmapList", "noColor",
			  "can't convert string '%s' to XmtColorTable.\n\tNo XmtColorTable converter registered.\n\tSee XmtRegisterColorTableConverter();",
			  tablestr);
	}
	else {
	    Screen *screen = XtScreenOfObject(widget);
	    fromval.addr = tablestr;
	    fromval.size = strlen(tablestr) + 1;
	    toval.addr = (XPointer) &table;
	    toval.size = sizeof(XmtColorTable);
	    color_table_args[0].addr = (XPointer) &parent_table;
	    color_table_args[0].size = sizeof(XmtColorTable);
	    color_table_args[1].addr = (XPointer) &screen;
	    color_table_args[1].size = sizeof(Screen *);
	    status = XtCallConverter(dpy, _XmtColorTableConverter,
				     color_table_args, 2,
				     &fromval, &toval, &color_table_cache_ref);
	    if (!status)
		XmtWarningMsg("XmtConvertStringToPixmapList", "badColor",
			      "continuing with default color table.");
	    else
		free_table = True;
	}
    }

    /*
     * now convert the list of strings to a list of pixmaps
     * First, count the string, and allocate a pixmap array.
     * If a pixmap conversion fails and we get None, we convert it
     * to XmUNSPECIFIED_PIXMAP, because None would be interpreted as
     * terminating the array.  Motif widgets like this value better than
     * None, but if this converter is used with other widget types, the
     * returned array will have to be tested for these values.
     */
    for(i=0; pixmap_names[i]; i++);
    num_items = i;
    pixmaps = (Pixmap *)XtMalloc((num_items+1) * sizeof(Pixmap));
    for(i = 0; i < num_items; i++) {
	pixmaps[i] = XmtGetPixmap(widget, table, pixmap_names[i]);
	if (pixmaps[i] == None) pixmaps[i] = XmUNSPECIFIED_PIXMAP;
    }
    /* NULL-terminate the array */
    pixmaps[num_items] = (Pixmap) NULL;
	
    if (free_string) XtFree(strlist);
    if (free_table) {
	refs[0] = color_table_cache_ref;
	refs[1] = NULL;
	XtAppReleaseCacheRefs(XtWidgetToApplicationContext(widget), refs);
    }
    if (free_string_list) {
	refs[0] = string_list_cache_ref;
	refs[1] = NULL;
	XtAppReleaseCacheRefs(XtWidgetToApplicationContext(widget), refs);
    }

    done(Pixmap *, pixmaps);

 error:
    /*
     * some other converter has called XtDisplayConversionWarning
     * to print the string, so we just have to say that we failed
     */
    XmtWarningMsg("XmtCvtStringToPixmapList", "failed",
		  "Pixmap list conversion failed.");
    if (free_string) XtFree(strlist);
    return False;
}

/* ARGSUSED */
#if NeedFunctionPrototypes
static void XmtDestroyPixmapList(XtAppContext app, XrmValue *to,
				 XtPointer converter_data,
				 XrmValue *args, Cardinal *num_args)
#else
static void XmtDestroyPixmapList(app, to, converter_data, args, num_args)
XtAppContext app;
XrmValue *to;
XtPointer converter_data;
XrmValue *args;
Cardinal *num_args;
#endif
{
    Widget widget = *(Widget *)args[0].addr;
    Pixmap *pixmaps = *(Pixmap **) to->addr;
    int i;

    for(i=0; pixmaps[i] != (Pixmap)NULL; i++)
	XmtReleasePixmap(widget, pixmaps[i]);
    XtFree((char *)pixmaps);
}

#if NeedFunctionPrototypes
void XmtRegisterPixmapListConverter(void)
#else
void XmtRegisterPixmapListConverter()
#endif
{
    static Boolean registered = False;

    if (!registered) {
	registered = True;
	XmtRegisterStringListConverter();
	XtSetTypeConverter(XtRString, XmtRPixmapList,
			   XmtConvertStringToPixmapList,
			   _XmtWidgetConvertArg, 1,
			   XtCacheNone | XtCacheRefCount,
			   XmtDestroyPixmapList);
    }
}