1 /*****************************************************************************
2 * XAscii by Ken Kirksey *
3 * *
4 * XAscii displays a table of ascii characters and their respective values. *
5 * The user choses which format the values are displayed in: either hex, *
6 * decimal or octal. *
7 * *
8 * BASICALLY THE WAY IT WORKS *
9 * In xascii.h four static arrays of strings are declared, one for *
10 * ascii characters, one for decimal values, one for hex values and one *
11 * for octal values. Each of these are broken up into five separate *
12 * strings, each of which make up a column (label widget) in the xascii *
13 * window. The ascii value columns remain static, they never change. *
14 * The label widgets that hold the values, however, change whenever the *
15 * user changes the notational mode (clicks on the dec ,hex, or octal *
16 * button). What happens when one of these buttons is clicked is that *
17 * an XtVaSetValues is done on the value label widgets to change their *
18 * label resource to display the values in the notation specified by the*
19 * button press. *
20 * *
21 * Send all kudos, complaints, suggestions, or bug reports to: *
22 * Ken Kirksey (kkirksey@eng.auburn.edu) *
23 * *
24 * BTW: I use 4 column tabs for coding, so if your text editor isn't set for *
25 * 4 column tabs, this program probably looks really funny. *
26 * *
27 ******************************************************************************
28 * Version 1.1 5 Aug 91 *
29 * *
30 * Removed the #include <strings.h> as this was a portability no-no. *
31 * Added an Imakefile (courtesy of Dave Elliot) to the release archive. * * *
32 ******************************************************************************
33 * Version 1.2 6 Aug 91 *
34 * *
35 * Fixed the problem that was causing my use of the valueFont resource to *
36 * kill the program. The bug was pointed out by Dave Brooks. *
37 * *
38 *****************************************************************************/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <X11/Intrinsic.h>
43 #include <X11/Shell.h>
44 #include <X11/Xos.h>
45 #include <X11/StringDefs.h>
46 #include <X11/Xaw/Command.h>
47 #include <X11/Xaw/Toggle.h>
48 #include <X11/Xaw/Label.h>
49 #include <X11/Xaw/Form.h>
50 #include "xascii.h"
51
52 /*===========================================================================+
53 | Function Declarations |
54 +===========================================================================*/
55 static void Quit(), /* Callbacks */
56 ToHex(),
57 ToDec(),
58 ToOct(),
59 Done(),
60
61 AboutXascii(), /* Actions */
62
63 Syntax(); /* Utilitie */
64
65
66 /*===========================================================================+
67 | Global Variable Declarations |
68 +===========================================================================*/
69
70 static XtAppContext appl_context;
71
72 static XtActionsRec about_box[]= {
73 {"about_xascii", AboutXascii},
74 };
75
76 static Widget toplevel,
77 base_win,
78 title_label,
79 quit_button,
80 toggles[3],
81 ascii_list[6],
82 value_list[6],
83 pshell,
84 about_base_win,
85 about_label,
86 done_button;
87
88 char **hex_columns,
89 **decimal_columns,
90 **octal_columns,
91 **ascii_columns;
92
93 /*===========================================================================+
94 | MAIN | +===========================================================================*/
main(argc,argv)95 main (argc,argv)
96 int argc;
97 char ** argv;
98 {
99 int i, j;
100
101 /*---------------------------------------------------------------------------+
102 | Break up the Ascii table and values into separate columns (22 rows, 6 cols)|
103 | for display. |
104 +---------------------------------------------------------------------------*/
105 ascii_columns = (char **)XtCalloc (6, sizeof (char *));
106 hex_columns = (char **) XtCalloc (6, sizeof (char *));
107 decimal_columns = (char **) XtCalloc (6, sizeof (char *));
108 octal_columns = (char **) XtCalloc (6, sizeof (char *));
109
110 for (i=0; i<6; i++)
111 {
112 ascii_columns[i] = (char *) XtCalloc (90, sizeof (char));
113 hex_columns[i] = (char *) XtCalloc (90, sizeof (char));
114 decimal_columns[i] = (char *) XtCalloc (90, sizeof (char));
115 octal_columns[i] = (char *) XtCalloc (90, sizeof (char));
116
117 for (j= (i*22); j < ((i+1) * 22); j++)
118 {
119 strcat (ascii_columns[i], ascii_values[j]);
120 strcat (ascii_columns[i], "\n");
121
122 strcat (hex_columns[i], hex_values[j]);
123 strcat (hex_columns[i], "\n");
124
125 strcat (decimal_columns[i], decimal_values[j]);
126 strcat (decimal_columns[i], "\n");
127
128 strcat (octal_columns[i], octal_values[j]);
129 strcat (octal_columns[i], "\n");
130 }
131 }
132
133 /*-----------------------------------------------------------------------+
134 | Begin Widget Initialization. |
135 +-----------------------------------------------------------------------*/
136 toplevel = XtAppInitialize(
137 &appl_context,
138 "XAscii",
139 options,
140 XtNumber(options),
141 &argc,argv,NULL, NULL,0);
142
143 /*-----------------------------------------------------------------------+
144 | Check for Invalid command line options |
145 +-----------------------------------------------------------------------*/
146 if (argc > 1)
147 Syntax (argc, argv);
148
149 XtVaGetApplicationResources (toplevel,
150 &app_data,
151 resources,
152 XtNumber (resources),
153 NULL);
154
155 base_win = XtVaCreateManagedWidget(
156 "base_win",
157 formWidgetClass,
158 toplevel,
159 NULL, 0);
160
161 /*----------------------------------------------------------------------+
162 | Note the translation that invokes that about box pop up on a button |
163 | click in this label widget. |
164 +----------------------------------------------------------------------*/
165 title_label = XtVaCreateManagedWidget (
166 "title_label",
167 labelWidgetClass,
168 base_win,
169 XtNforeground, app_data.highlight_color,
170 XtNlabel, "X ASCII Chart",
171 XtNwidth, 408,
172 XtNtranslations, XtParseTranslationTable
173 (defaultTranslations),
174 XtVaTypedArg,
175 XtNbackground,
176 XtRString,
177 white_bg,
178 sizeof (white_bg),
179 XtVaTypedArg,
180 XtNfont,
181 XtRString,
182 title_font,
183 sizeof (title_font),
184 NULL, 0);
185
186 /*-----------------------------------------------------------------------+
187 |Create the label widgets for chart display. Default value mode is dec. |
188 +-----------------------------------------------------------------------*/
189 for (i=0; i<6; i++)
190 {
191 value_list[i] = XtVaCreateManagedWidget(
192 "value_list",
193 labelWidgetClass,
194 base_win,
195 XtNforeground, app_data.highlight_color,
196 XtNfont, app_data.value_font,
197 XtNfromVert, title_label,
198 XtNlabel, decimal_columns[i],
199 XtNhorizDistance, 10,
200 XtNborderWidth, 0,
201 XtNvertDistance, 10,
202 XtVaTypedArg,
203 XtNbackground,
204 XtRString,
205 white_bg,
206 sizeof (white_bg),
207
208 NULL);
209
210 ascii_list[i] = XtVaCreateManagedWidget(
211 "ascii_list",
212 labelWidgetClass,
213 base_win,
214 XtNfont, app_data.value_font,
215 XtNfromVert, title_label,
216 XtNlabel, ascii_columns[i],
217 XtNfromHoriz, value_list[i],
218 XtNborderWidth, 0,
219 XtNvertDistance, 10,
220 XtNhorizDistance, 0,
221 XtVaTypedArg,
222 XtNbackground,
223 XtRString,
224 white_bg,
225 sizeof (white_bg),
226
227 NULL);
228
229 }
230
231 for (i=1; i<6; i++)
232 XtVaSetValues (value_list[i], XtNfromHoriz, ascii_list[i-1], NULL);
233 /*-----------------------------------------------------------------------+
234 | Create the buttons for value mode selection. Make them a radio group, |
235 | so that only one will be selected (highlighted) at a time. |
236 +-----------------------------------------------------------------------*/
237 for (i=0; i<3; i++)
238 {
239 toggles[i] = XtVaCreateManagedWidget (
240 "toggle",
241 toggleWidgetClass,
242 base_win,
243 XtNforeground, app_data.highlight_color,
244 XtNborderColor, app_data.highlight_color,
245 XtNfromVert, ascii_list[0],
246 XtNvertDistance, 15,
247 XtNwidth, 95,
248 XtNresize, FALSE,
249 XtNhorizDistance, 30,
250 XtVaTypedArg,
251 XtNbackground,
252 XtRString,
253 white_bg,
254 sizeof (white_bg),
255
256 NULL, 0);
257
258 if (i != 0)
259 XtVaSetValues(toggles[i], XtNfromHoriz, toggles[i-1], NULL);
260
261 XtVaSetValues (toggles[i], XtNradioGroup, toggles[0], NULL);
262 }
263
264 XtVaSetValues (toggles[0],
265 XtNlabel, "Hex",
266 XtNhorizDistance, 0,
267 XtNfromHoriz, value_list[0],
268 NULL);
269
270 XtVaSetValues (toggles[1],
271 XtNstate, TRUE,
272 XtNlabel, "Decimal",
273 NULL);
274
275 XtVaSetValues (toggles[2],
276 XtNlabel, "Octal",
277 NULL);
278
279 XtAddCallback (toggles[0], XtNcallback, ToHex, NULL);
280 XtAddCallback (toggles[1], XtNcallback, ToDec, NULL);
281 XtAddCallback (toggles[2], XtNcallback, ToOct, NULL);
282
283
284 quit_button = XtVaCreateManagedWidget (
285 "quit_button",
286 commandWidgetClass,
287 base_win,
288 XtNfromVert, toggles[0],
289 XtNfromHoriz, ascii_list[0],
290 XtNlabel, "Quit",
291 XtNwidth, 100,
292 XtNvertDistance, 15,
293 XtNhorizDistance, 95,
294 XtVaTypedArg,
295 XtNbackground,
296 XtRString,
297 white_bg,
298 sizeof (white_bg),
299
300 NULL, 0);
301
302 XtAddCallback (quit_button, XtNcallback, Quit, NULL);
303
304 /*-----------------------------------------------------------------------+
305 | Create the About Box. Invoked on click in title label. |
306 +-----------------------------------------------------------------------*/
307 pshell = XtVaCreatePopupShell (
308 "pshell",
309 transientShellWidgetClass,
310 toplevel,
311 NULL);
312
313 about_base_win = XtVaCreateManagedWidget(
314 "about_base_win",
315 formWidgetClass,
316 pshell,
317
318 NULL);
319
320 about_label = XtVaCreateManagedWidget(
321 "about_label",
322 labelWidgetClass,
323 about_base_win,
324 XtVaTypedArg,
325 XtNbackground,
326 XtRString,
327 white_bg,
328 sizeof (white_bg),
329 XtNlabel, about_text,
330 XtNforeground, app_data.highlight_color,
331 XtNborderColor, app_data.highlight_color,
332 XtNborderWidth, 4,
333 NULL);
334
335 done_button = XtVaCreateManagedWidget(
336 "done_button",
337 commandWidgetClass,
338 about_base_win,
339 XtVaTypedArg,
340 XtNbackground,
341 XtRString,
342 white_bg,
343 sizeof (white_bg),
344 XtNfromVert, about_label,
345 XtNvertDistance, 10,
346 XtNhorizDistance, 165,
347 XtNlabel, "Slainte!",
348 NULL);
349
350 XtAddCallback (done_button, XtNcallback, Done, NULL);
351
352 XtAppAddActions (appl_context, about_box, XtNumber(about_box) );
353
354 XtRealizeWidget(toplevel);
355 XtAppMainLoop(appl_context);
356 }
357
358 /*===========================================================================+
359 | CALLBACK FUNCTIONS | +===========================================================================*/
360
361 /*---------------------------------------------------------------------------+
362 | Quit |
363 | Quit the application. Invoked by click on quit button. |
Quit(w,ignore1,ignore2)364 +---------------------------------------------------------------------------*/ static void Quit (w,ignore1,ignore2)
365 Widget w; XtPointer ignore1, ignore2;
366 { XtDestroyApplicationContext(appl_context);
367 exit(0);
368 }
369
370 /*---------------------------------------------------------------------------+
371 | ToHex |
372 | Changes the ascii character values to Hex notation. |
373 +---------------------------------------------------------------------------*/
ToHex(w,ignore1,ignore2)374 static void ToHex (w,ignore1,ignore2)
375 Widget w; XtPointer ignore1, ignore2;
376 {
377 int i;
378
379 for (i=0; i<6; i++)
380 XtVaSetValues (value_list[i], XtNlabel, hex_columns[i], NULL);
381
382 }
383
384
385 /*---------------------------------------------------------------------------+
386 | ToDec |
387 | Changes the ascii character values to Decimal notation. |
388 +---------------------------------------------------------------------------*/
ToDec(w,ignore1,ignore2)389 static void ToDec (w,ignore1,ignore2)
390 Widget w; XtPointer ignore1, ignore2;
391 {
392 int i;
393
394 for (i=0; i<6; i++)
395 XtVaSetValues (value_list[i], XtNlabel, decimal_columns[i], NULL);
396
397 }
398
399
400 /*---------------------------------------------------------------------------+
401 | ToOct |
402 | Changest the ascii character values to Octal notation. |
403 +---------------------------------------------------------------------------*/
ToOct(w,ignore1,ignore2)404 static void ToOct (w,ignore1,ignore2)
405 Widget w; XtPointer ignore1, ignore2;
406 {
407 int i;
408
409 for (i=0; i<6; i++)
410 XtVaSetValues (value_list[i], XtNlabel, octal_columns[i], NULL);
411
412 }
413
414
415 /*---------------------------------------------------------------------------+
416 | Done |
417 | Pops down the about box popup shell. |
418 +---------------------------------------------------------------------------*/
Done(w,ignore1,ignore2)419 static void Done (w,ignore1,ignore2)
420 Widget w; XtPointer ignore1, ignore2;
421 {
422 int i;
423
424 XtPopdown (pshell);
425 }
426
427
428 /*===========================================================================+
429 | ACTION FUNCTIONS | +===========================================================================*/
430
431 /*---------------------------------------------------------------------------+
432 | AboutXascii |
433 | Positions and pops up the about box popup shell. |
434 +---------------------------------------------------------------------------*/
AboutXascii(w,event,nuffin1,nuffin2)435 static void AboutXascii (w, event, nuffin1, nuffin2)
436 Widget w;
437 XButtonEvent event;
438 String *nuffin1;
439 Cardinal *nuffin2;
440 {
441 Position x,
442 y;
443
444 Dimension width,
445 height;
446
447 int i;
448
449 XtVaGetValues (toplevel,
450 XtNwidth, &width,
451 XtNheight, &height,
452 NULL);
453
454 XtTranslateCoords (toplevel,
455 (Position) 0,
456 (Position) height/2,
457 &x, &y);
458
459 XtVaSetValues (pshell,
460 XtNx, x,
461 XtNy, y,
462 NULL);
463
464 XtPopup (pshell, XtGrabNonexclusive);
465 }
466
467 /*===========================================================================+
468 | UTILITY FUNCTIONS | +===========================================================================*/
469
470 /*---------------------------------------------------------------------------+
471 | Syntax |
472 | Parses off bad command line options (i.e. the stuff left over when |
473 | XtVaAppInitialize is through). |
Syntax(argc,argv)474 +---------------------------------------------------------------------------*/ static void Syntax (argc, argv)
475 int argc;
476 char **argv;
477 {
478 int i,
479 err = 0;
480
481 for (i=1; i < argc; i++)
482 {
483 if (!err++)
484 fprintf (stderr, "\nxascii: unknown command line option\n");
485
486 fprintf (stderr, "option: %s\n", argv[i]);
487 }
488
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503