1 /* $Header: /cvsroot/lesstif/lesstif/test/Xm-2.0/rendition/test3.c,v 1.3 2002/05/01 15:23:26 amai Exp $ */
2 
3 #include <stdlib.h>
4 #include <stdio.h>
5 
6 #include <Xm/XmAll.h>
7 
8 #include "../../common/Test.h"
9 
10 
NoFontCB(Widget w,XtPointer client,XtPointer call)11 static void NoFontCB(Widget w, XtPointer client, XtPointer call)
12 {
13 	XmDisplayCallbackStruct	*cbp = (XmDisplayCallbackStruct *)call;
14 
15 	fprintf(stderr, "No-font-callback\n");
16 }
17 
NoRenditionCB(Widget w,XtPointer client,XtPointer call)18 static void NoRenditionCB(Widget w, XtPointer client, XtPointer call)
19 {
20 	XmDisplayCallbackStruct	*cbp = (XmDisplayCallbackStruct *)call;
21 
22 
23 	fprintf(stderr, "No-rendition-callback\n");
24 }
25 
26 /* ConvertStringToPixel()
27 ** A utility function to convert a color name to a Pixel
28 */
ConvertStringToPixel(Widget widget,char * name)29 Pixel ConvertStringToPixel (Widget widget, char *name)
30 {
31         XrmValue from_value, to_value; /* For resource conversion */
32 
33         from_value.addr = name;
34         from_value.size = strlen( name ) + 1;
35         to_value.addr   = NULL;
36         XtConvertAndStore (widget, XmRString, &from_value, XmRPixel, &to_value);
37 
38         if (to_value.addr) {
39 		return (*((Pixel*) to_value.addr)) ;
40         }
41 
42 	return XmUNSPECIFIED_PIXEL ;
43 }
44 
45 /*
46 ** A convenient structure to hold the data
47 ** for creating various renditions
48 */
49 typedef struct RenditionData_s
50 {
51 	char *tag;
52 	char *color;
53 	char *font;
54 } RenditionData_t;
55 
56 #define MAX_COLUMNS    4
57 
58 RenditionData_t rendition_data[MAX_COLUMNS] =
59 {
60 	{ "one",   "red",    "fixed"                                                      },
61 	{ "two",   "green",  "-adobe-helvetica-bold-r-normal--10-100-75-75-*-*-iso8859-1" },
62 	{ "three", "blue",   "bembo-bold"                                                 },
63 	{ "four",  "orange", "-adobe-*-medium-i-normal--24-240-75-75-*-*-iso8859-1"       }
64 };
65 
66 /*
67 ** Arbitrary data to display in the List
68 */
69 static char *poem[] =
70 {
71 	"Mary", "had a",           "little",    "lamb",
72 	"Its",  "fleece",          "was white", "as snow",
73 	"And",  "everywhere that", "Mary",      "went",
74 	"The",  "lamb was",        "sure",      "to follow",
75 	(char *) 0
76 };
77 
78 /*
79 ** CreateListData(): routine to convert the
80 ** poem into an array of compound strings
81 */
CreateListData(int * count)82 XmStringTable CreateListData (int *count)
83 {
84 	XmStringTable table  = (XmStringTable) 0 ;
85 	int           line   = 0 ;
86 	int           column = 0 ;
87 	int           index  = 0 ;
88 	XmString      entry ;
89 	XmString      row =NULL;
90 	XmString      tmp =NULL;
91 	XmString      tab;
92 
93 	tab = XmStringComponentCreate (XmSTRING_COMPONENT_TAB, 0, NULL);
94 
95 	while (poem[index] != (char *) 0) {
96 		/* create a compound string, using the rendition tag */
97 		entry = XmStringGenerate ((XtPointer) poem[index],
98 					  NULL,
99 					  XmCHARSET_TEXT,
100 					  rendition_data[column].tag) ;
101 
102 		if (row != (XmString)NULL) {
103 			tmp = XmStringConcat (row, tab) ;
104 			XmStringFree (row) ;
105 			row = XmStringConcatAndFree (tmp, entry) ;
106 		}
107 		else {
108 			row = entry ;
109 		}
110 
111 		++column ;
112 
113 		if (column == MAX_COLUMNS) {
114 			if (table == (XmStringTable) 0) {
115 				table = (XmStringTable) XtMalloc((unsigned) 1 * sizeof (XmString)) ;
116 			}
117 			else {
118 				table = (XmStringTable) XtRealloc((char *) table, (unsigned) (line + 1) * sizeof (XmString)) ;
119 			}
120 
121 			table[line++] = row ;
122 			row           = (XmString) 0 ;
123 			column        = 0 ;
124 		}
125 
126 		index++ ;
127 	}
128 
129 	XmStringFree (tab) ;
130 
131 	table[line] = (XmString) 0 ;
132 
133 	*count = line ;
134 
135 	return table ;
136 }
137 
138 
139 int
main(int argc,char ** argv)140 main(int argc, char **argv)
141 {
142 	Widget		toplevel, one, d;
143 	XtAppContext	app;
144 	XmFontList	fontlist;
145 	XmString	xms;
146 	Arg           args[10];
147 	XmTab         tabs[MAX_COLUMNS];
148 	XmTabList     tablist;
149 	XmRendition   renditions[MAX_COLUMNS];
150 	XmRenderTable rendertable;
151 	XmStringTable xmstring_table;
152 	int           xmstring_count;
153 	Pixel         pixels[MAX_COLUMNS];
154 	int           n, i;
155 
156 	XtSetLanguageProc(NULL, NULL, NULL);
157 	toplevel = XtVaAppInitialize(&app, "Label", NULL, 0, &argc, argv, NULL, NULL);
158 
159 	d = XmGetXmDisplay(XtDisplay(toplevel));
160 	XtAddCallback(d, XmNnoFontCallback, NoFontCB, NULL);
161 	XtAddCallback(d, XmNnoRenditionCallback, NoRenditionCB, NULL);
162 
163 	/* Create some colors */
164 	for (i = 0 ; i < MAX_COLUMNS ; i++) {
165 		pixels[i] = ConvertStringToPixel (toplevel, rendition_data[i].color) ;
166 	}
167 
168 	/* Create tab stops for columnar output */
169 	for (i = 0 ; i < MAX_COLUMNS ; i++) {
170 		tabs[i] = XmTabCreate ((float) 1.5,
171 				       XmINCHES,
172 				       ((i == 0) ? XmABSOLUTE : XmRELATIVE),
173 				       XmALIGNMENT_BEGINNING,
174 				       ".") ;
175 	}
176 
177 	/* Create a tablist table which contains the tabs */
178 	tablist = XmTabListInsertTabs (NULL, tabs, XtNumber (tabs), 0) ;
179 
180 	/* Create some multi-font/color renditions, and use the tablist */
181 	/* This will be inherited if we use it on the first rendition   */
182 	for (i = 0 ; i < MAX_COLUMNS ; i++) {
183 		n = 0 ;
184 
185 		if (i == 0) {
186 			XtSetArg (args[n], XmNtabList, tablist); n++;
187 		}
188 
189 		XtSetArg (args[n], XmNrenditionForeground, pixels[i]); n++;
190 		XtSetArg (args[n], XmNfontName, rendition_data[i].font); n++;
191 		XtSetArg (args[n], XmNfontType, XmFONT_IS_FONT); n++;
192 		renditions[i] = XmRenditionCreate (toplevel, rendition_data[i].tag, args, n);
193 	}
194 
195 	/* Create the Render Table */
196 	rendertable = XmRenderTableAddRenditions (NULL, renditions, XtNumber (renditions), XmMERGE_NEW) ;
197 
198 	/* Create the multi-column data for the list */
199 
200 	xmstring_table = CreateListData(&xmstring_count) ;
201 
202 	xms = xmstring_table[0];
203 	for (i=1; i<xmstring_count; i++) {
204 		XmString	s, x;
205 		s = XmStringSeparatorCreate();
206 		x = XmStringConcat(xms, s);
207 		xms = XmStringConcat(x, xmstring_table[i]);
208 	}
209 
210 	one = XtVaCreateManagedWidget("One", xmLabelWidgetClass, toplevel,
211 		XmNalignment,	XmALIGNMENT_BEGINNING,
212 		XmNrenderTable,	rendertable,
213 		XmNlabelString, xms,
214 		NULL);
215 
216 	XtRealizeWidget(toplevel);
217 
218 #if 0
219 /* Note: the following values are the result of
220  * querying the current geometry.
221  */
222 {
223 static XtWidgetGeometry Expected[] = {
224    {CWWidth | CWHeight            ,    0,    0,  112,   58, 0,0,0}, /* One */
225 };
226 /* toplevel should be replaced with to correct applicationShell */
227 PrintDetails(toplevel, Expected);
228 }
229 #endif
230 
231 	LessTifTestMainLoop(toplevel);
232 	exit(0);
233 }
234