Debian Codegraph @

demos/unsupported/xmfonts/xmfonts.c

raw symbols SCIP
1 /*
2 * Motif
3 *
4 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
5 *
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
16 * details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
22 */
23 #ifdef REV_INFO
24 #ifndef lint
25 static char rcsid[] = "$XConsortium: xmfonts.c /main/5 1995/07/14 10:08:26 drk $"
26 #endif
27 #endif
28
29 /**---------------------------------------------------------------------
30 ***
31 *** file: xmfonts.c
32 ***
33 ***
34 Xmfonts.ad:
35
36 xmfonts*XmScrolledWindow.height: 432
37 xmfonts*XmScrolledWindow.width: 390
38 xmfonts*row_column.XmNnumColumns: 5
39 xmfonts*row_column.XmNpacking: XmPACK_COLUMN
40
41 ***-------------------------------------------------------------------*/
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <X11/Intrinsic.h>
45 #include <Xm/Xm.h>
46 #include <Xm/MainW.h>
47 #include <Xm/PushB.h>
48 #include <Xm/RowColumn.h>
49 #include <Xm/CascadeB.h>
50 #include <Xm/ScrolledW.h>
51 #include <Xm/Frame.h>
52 #include <Xm/MessageB.h>
53 #include <Xm/PushBG.h>
54
55
56 /*-------------------------------------------------------------
57 ** Forward Declarations
58 */
59
60 Widget CreateApplication (); /* create main window */
61 Widget CreateFontSample (); /* create font display window */
62 Widget CreateHelp (); /* create help window */
63
64 void SelectFontCB (); /* callback for font buttons */
65 void CloseCB (); /* callback for close button */
66 void HelpCB (); /* callback for help button */
67 void QuitCB (); /* callback for quit button */
68
69
70
71 /*-------------------------------------------------------------
72 ** Global Variables
73 */
74
75 #define MAX_ARGS 20
76
77 static XmStringCharSet charset = (XmStringCharSet) XmSTRING_DEFAULT_CHARSET;
78
79
80 typedef struct {
81 char *fontpattern;
82 int maxfont ; /* number max of font to be displayed */
83 int maxlen ; /* max length of font name to be displayed */
84 Boolean usegadget ;
85 } ApplicationData, *ApplicationDataPtr;
86
87 ApplicationData AppData;
88
89 #define XtNfontPattern "fontPattern"
90 #define XtCFontPattern "FontPattern"
91 #define XtNmaxFont "maxFont"
92 #define XtCMaxFont "MaxFont"
93 #define XtNmaxLen "maxLen"
94 #define XtCMaxLen "MaxLen"
95 #define XtNuseGadget "useGadget"
96 #define XtCUseGadget "UseGadget"
97
98 static XtResource resources[] = {
99 { XtNfontPattern, XtCFontPattern, XmRString, sizeof(String),
100 XtOffset(ApplicationDataPtr, fontpattern), XmRString, "*" },
101 { XtNmaxFont, XtCMaxFont, XmRInt, sizeof(int),
102 XtOffset(ApplicationDataPtr, maxfont), XmRImmediate, (caddr_t) 1000 },
103 { XtNmaxLen, XtCMaxLen, XmRInt, sizeof(int),
104 XtOffset(ApplicationDataPtr, maxlen), XmRImmediate, (caddr_t) 10 },
105 { XtNuseGadget, XtCUseGadget, XmRBoolean, sizeof (Boolean),
106 XtOffset(ApplicationDataPtr, usegadget), XmRImmediate, (caddr_t) TRUE }
107 };
108
109 /*-------------------------------------------------------------
110 ** main - main logic for application
111 */
112 int main (argc,argv)
113 int argc;
114 char **argv;
115 {
116 Display *display; /* Display */
117 Widget app_shell; /* ApplicationShell */
118 Widget main_window; /* MainWindow */
119 XtAppContext app_context;
120
121 /* Initialize toolkit and open the display.
122 */
123 app_shell = XtAppInitialize(&app_context, "XMdemos", NULL, 0,
124 &argc, argv, NULL, NULL, 0);
125
126
127 XtGetApplicationResources(app_shell,
128 (XtPointer)&AppData,
129 resources,
130 XtNumber(resources),
131 NULL,
132 0);
133
134 main_window = CreateApplication (app_shell);
135 XtRealizeWidget (app_shell);
136
137 /* Get and dispatch events.
138 */
139 XtAppMainLoop (app_context);
140
141 return 0; /* make compiler happy */
142 }
143
144
145
146 /*-------------------------------------------------------------
147 ** CreateApplication - create main window
148 */
149 Widget CreateApplication (parent)
150 Widget parent; /* parent widget */
151 {
152 Widget main_window; /* MainWindow */
153 Widget menu_bar; /* MenuBar */
154 Widget menu_pane; /* MenuPane */
155 Widget cascade; /* CascadeButton */
156 Widget frame; /* Frame */
157 Widget swindow; /* ScrolledWindow */
158 Widget row_column; /* RowColumn */
159 Widget button; /* PushButtonGadget */
160 Widget hsb, vsb; /* ScrollBars */
161
162 Arg args[MAX_ARGS]; /* arg list */
163 register int n; /* arg count */
164
165 char * name;
166 int count, i ;
167 char ** fontnamelist ;
168
169 XmString label_string;
170
171
172 /* Create MainWindow.
173 */
174 n = 0;
175 main_window = XmCreateMainWindow (parent, "main_window", args, n);
176 XtManageChild (main_window);
177
178 /* Create MenuBar in MainWindow.
179 */
180 n = 0;
181 menu_bar = XmCreateMenuBar (main_window, "menu_bar", args, n);
182 XtManageChild (menu_bar);
183
184 /* Create "Exit" PulldownMenu.
185 */
186 n = 0;
187 menu_pane = XmCreatePulldownMenu (menu_bar, "menu_pane", args, n);
188
189 n = 0;
190 button = XmCreatePushButton (menu_pane, "Quit", args, n);
191 XtManageChild (button);
192 XtAddCallback (button, XmNactivateCallback, QuitCB, NULL);
193
194 n = 0;
195 XtSetArg (args[n], XmNsubMenuId, menu_pane); n++;
196 cascade = XmCreateCascadeButton (menu_bar, "Exit", args, n);
197 XtManageChild (cascade);
198
199
200 /* Create "Help" button.
201 */
202 n = 0;
203 cascade = XmCreateCascadeButton (menu_bar, "Help", args, n);
204 XtManageChild (cascade);
205 XtAddCallback (cascade, XmNactivateCallback, HelpCB, NULL);
206
207 n = 0;
208 XtSetArg (args[n], XmNmenuHelpWidget, cascade); n++;
209 XtSetValues (menu_bar, args, n);
210
211
212 /* Create Frame MainWindow and ScrolledWindow in Frame.
213 */
214 n = 0;
215 frame = XmCreateFrame (main_window, "frame", args, n);
216 XtManageChild (frame);
217
218 n = 0;
219 XtSetArg (args[n], XmNscrollingPolicy, XmAUTOMATIC); n++;
220 swindow = XmCreateScrolledWindow (frame, "swindow", args, n);
221 XtManageChild (swindow);
222
223
224 /* Create RowColumn in ScrolledWindow to manage buttons.
225 */
226 n = 0;
227
228 row_column = XmCreateRowColumn (swindow, "row_column", args, n);
229 XtManageChild (row_column);
230
231
232 /* Set MainWindow areas and add tab groups
233 */
234 XmMainWindowSetAreas (main_window, menu_bar, NULL, NULL, NULL,
235 frame);
236
237 /* Create a PushButton widget for each font.
238 Get the font list from the server and keep
239 only the font name whose length is less than
240 the given maxLen resource.
241 */
242
243 fontnamelist = XListFonts(XtDisplay(main_window),
244 AppData.fontpattern,
245 AppData.maxfont, &count);
246 if (!fontnamelist) {
247 fprintf(stderr,"No font on your server, exit\n");
248 exit(0);
249 }
250
251 /* read one entry each time through the loop */
252 for (i = 0; i < count; i++ )
253 {
254 name = fontnamelist[i] ;
255 if (strlen(name) > AppData.maxlen) continue ;
256
257 /* create PushButton in RowCol */
258 n = 0;
259 label_string = XmStringCreateLtoR(name, charset);
260 XtSetArg (args[n], XmNlabelString, label_string); n++;
261 if (AppData.usegadget)
262 button = XmCreatePushButtonGadget (row_column, name,
263 args, n);
264 else
265 button = XmCreatePushButton(row_column, name,
266 args, n);
267 XtManageChild (button);
268 XtAddCallback (button, XmNarmCallback, SelectFontCB, NULL);
269 XmStringFree (label_string);
270 }
271
272 /* Return MainWindow.
273 */
274
275 XFreeFontNames(fontnamelist);
276 return (main_window);
277 }
278
279
280
281 /*-------------------------------------------------------------
282 ** CreateFontSample - create font display window
283 */
284 Widget CreateFontSample (parent)
285 Widget parent; /* parent widget */
286 {
287 Widget message_box; /* MessageBox Dialog */
288 Widget button;
289 Arg args[MAX_ARGS]; /* arg list */
290 register int n; /* arg count */
291
292 char *name = NULL; /* font name */
293 XFontStruct *font = NULL; /* font pointer */
294 XmFontList fontlist = NULL; /* fontlist pointer */
295 static char message[BUFSIZ]; /* text sample */
296 XmString name_string = NULL;
297 XmString message_string = NULL;
298 XmString button_string = NULL;
299
300
301 /* Get font name.
302 */
303 n = 0;
304 XtSetArg (args[n], XmNlabelString, &name_string); n++;
305 XtGetValues (parent, args, n);
306 XmStringGetLtoR (name_string, charset, &name);
307
308
309 /* Load font and generate message to display. */
310 if (name)
311 font = XLoadQueryFont (XtDisplay (XtParent (parent)), name);
312 if (!font)
313 sprintf (message, "Unable to load font: %s", name);
314 else
315 {
316 fontlist = XmFontListCreate (font, charset);
317 sprintf (message, "\
318 This is font %s.\n\
319 The quick brown fox jumps over the lazy dog.", name);
320 }
321 message_string = XmStringCreateLtoR (message, charset);
322 button_string = XmStringCreateLtoR ("Close", charset);
323
324
325 /* Create MessageBox dialog.
326 */
327 n = 0;
328 XtSetArg (args[n], XmNdialogTitle, name_string); n++;
329 XtSetArg (args[n], XmNokLabelString, button_string); n++;
330 XtSetArg (args[n], XmNmessageString, message_string); n++;
331 message_box = XmCreateMessageDialog (XtParent (XtParent(parent)),
332 "fontbox",
333 args, n);
334
335 button = XmMessageBoxGetChild (message_box, XmDIALOG_MESSAGE_LABEL);
336 if (fontlist)
337 {
338 n = 0;
339 XtSetArg (args[n], XmNfontList, fontlist); n++;
340 XtSetValues(button, args, n);
341 }
342
343 button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
344 XtUnmanageChild (button);
345 button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
346 XtUnmanageChild (button);
347
348 /* Free strings and return MessageBox.
349 */
350 if (fontlist) XmFontListFree (fontlist);
351 if (name_string) XmStringFree (name_string);
352 if (message_string) XmStringFree (message_string);
353 if (button_string) XmStringFree (button_string);
354 return (message_box);
355 }
356
357
358
359 /*-------------------------------------------------------------
360 ** CreateHelp - create help window
361 */
362 Widget CreateHelp (parent)
363 Widget parent; /* parent widget */
364 {
365 Widget button;
366 Widget message_box; /* Message Dialog */
367 Arg args[MAX_ARGS]; /* arg list */
368 register int n; /* arg count */
369
370 static char message[BUFSIZ]; /* help text */
371 XmString title_string = NULL;
372 XmString message_string = NULL;
373 XmString button_string = NULL;
374
375
376
377 /* Generate message to display.
378 */
379 sprintf (message, "\
380 These are buttons for the fonts in your X11 server whose \n\
381 name lengths are below the given maxLen application resource.\n\
382 The button label is the name of the font. When you select \n\
383 a button, a small window will display a sample of the font. \n\n\
384 Press the 'close' button to close a font window. \n\
385 Select 'quit' from the 'exit' menu to exit this application.");
386 message_string = XmStringCreateLtoR (message, charset);
387 button_string = XmStringCreateLtoR ("Close", charset);
388 title_string = XmStringCreateLtoR ("xmfonts help", charset);
389
390
391 /* Create MessageBox dialog.
392 */
393 n = 0;
394 XtSetArg (args[n], XmNdialogTitle, title_string); n++;
395 XtSetArg (args[n], XmNokLabelString, button_string); n++;
396 XtSetArg (args[n], XmNmessageString, message_string); n++;
397 message_box = XmCreateMessageDialog (parent, "helpbox", args, n);
398
399 button = XmMessageBoxGetChild (message_box, XmDIALOG_CANCEL_BUTTON);
400 XtUnmanageChild (button);
401 button = XmMessageBoxGetChild (message_box, XmDIALOG_HELP_BUTTON);
402 XtUnmanageChild (button);
403
404
405 /* Free strings and return MessageBox.
406 */
407 if (title_string) XmStringFree (title_string);
408 if (message_string) XmStringFree (message_string);
409 if (button_string) XmStringFree (button_string);
410 return (message_box);
411 }
412
413
414
415 /*-------------------------------------------------------------
416 ** SelectFontCB - callback for font buttons
417 */
418 void SelectFontCB (w, client_data, call_data)
419 Widget w; /* widget id */
420 caddr_t client_data; /* data from application */
421 caddr_t call_data; /* data from widget class */
422 {
423 Widget message_box;
424
425
426 /* Create font sample window.
427 */
428 message_box = CreateFontSample (w);
429
430
431 /* Display font sample window.
432 */
433 XtManageChild (message_box);
434 }
435
436
437
438 /*-------------------------------------------------------------
439 ** CloseCB - callback for close button
440 */
441 void CloseCB (w, client_data, call_data)
442 Widget w; /* widget id */
443 caddr_t client_data; /* font pointer */
444 caddr_t call_data; /* data from widget class */
445 {
446 XFontStruct *font = (XFontStruct *) client_data;
447 Widget message_box = XtParent (w);
448 Widget shell = XtParent (message_box);
449
450
451 /* Free font.
452 */
453 if (font) XFreeFont (XtDisplay (w), font);
454
455
456 /* Unmanage and destroy widgets.
457 */
458 XtUnmanageChild (message_box);
459 XtDestroyWidget (shell);
460 }
461
462
463
464 /*-------------------------------------------------------------
465 ** HelpCB - callback for help button
466 */
467 void HelpCB (w, client_data, call_data)
468 Widget w; /* widget id */
469 caddr_t client_data; /* data from application */
470 caddr_t call_data; /* data from widget class */
471 {
472 Widget message_box; /* MessageBox */
473
474
475 /* Create help window.
476 */
477 message_box = CreateHelp (w);
478
479
480 /* Display help window.
481 */
482 XtManageChild (message_box);
483 }
484
485
486
487 /*-------------------------------------------------------------
488 ** QuitCB - callback for quit button
489 */
490 void QuitCB (w, client_data, call_data)
491 Widget w; /* widget id */
492 caddr_t client_data; /* data from applicaiton */
493 caddr_t call_data; /* data from widget class */
494 {
495
496
497 /* Terminate the application.
498 */
499 exit (0);
500 }