We know how to extract the button name within the callback.
[super-star-trek.git] / src / xio.c
1 #include <stdlib.h>
2 #include <X11/Intrinsic.h>
3 #include <X11/StringDefs.h>
4 #include <X11/Shell.h>
5 #include <X11/Xaw/Box.h>
6 #include <X11/Xaw/Form.h>
7 #include <X11/Xaw/Command.h>
8 #include <X11/Xaw/AsciiText.h>
9 #include "sst.h"
10
11 static XtAppContext app_context;
12 static Widget toplevel, text, form; 
13 static Widget navigation, weapons, status, planets, misc; 
14 static Widget navlabel, weaplabel, statlabel, planlabel, misclabel;
15
16 static String fallback[] = {
17     /* text window resources */
18     "*text.resizable: true",
19     "*text.resize: ResizeBoth",
20     NULL,
21 };
22
23 struct cmd_t {
24     char *name;
25     void (*callback)(Widget, XtPointer, XtPointer);
26     Widget *parent;
27     int enable;
28     Widget widget;
29
30 };
31
32 static void quit_proc(Widget w, XtPointer client_data, XtPointer call_data)
33
34     XtDestroyApplicationContext (app_context);
35     exit (0);
36 }
37
38 static void noargs_proc(Widget w, XtPointer client_data, XtPointer call_data)
39 /* use this for commands that take no arguments */
40 {
41     /* currently a stub */
42     printf("Button %s pressed\n", XtName(w));
43 }
44
45 static struct cmd_t commands[] = {
46     {"Move",            NULL,           &navigation,    0},
47     {"Dock",            noargs_proc,    &navigation,    0},
48     {"Chart",           noargs_proc,    &navigation,    0},
49     {"Impulse",         NULL,           &navigation,    0},
50     {"Rest",            NULL,           &navigation,    0},
51     {"Warp",            NULL,           &navigation,    0},
52     {"Probe",           NULL,           &navigation,    OPTION_PROBE},
53
54     {"Phasers",         NULL,           &weapons,       0},
55     {"Torpedo",         NULL,           &weapons,       0},
56     {"Shields",         NULL,           &weapons,       0},
57     {"Damages",         noargs_proc,    &weapons,       0},
58     {"Abandon",         noargs_proc,    &weapons,       0},
59     {"Destruct",        noargs_proc,    &weapons,       0},
60     {"Deathray",        noargs_proc,    &weapons,       0},
61     {"Mayday",          noargs_proc,    &weapons,       0},
62
63     {"Score",           noargs_proc,    &status,        0},
64     {"Report",          noargs_proc,    &status,        0},
65     {"Computer",        noargs_proc,    &status,        0},
66
67     {"Sensors",         noargs_proc,    &planets,       OPTION_PLANETS},
68     {"Orbit",           noargs_proc,    &planets,       OPTION_PLANETS},
69     {"Transport",       noargs_proc,    &planets,       OPTION_PLANETS},
70     {"Mine",            noargs_proc,    &planets,       OPTION_PLANETS},
71     {"Crystals",        noargs_proc,    &planets,       OPTION_PLANETS},
72     {"Shuttle",         noargs_proc,    &planets,       OPTION_PLANETS},
73     {"Planets",         noargs_proc,    &planets,       OPTION_PLANETS},
74
75     {"Emexit",          noargs_proc,    &misc,          0},
76     {"Save",            NULL,           &misc,          0},
77     {"Quit",            quit_proc,      &misc,          0},
78     {"Help",            noargs_proc,    &misc,          0},
79 };
80
81 #define MAXWIDTH        640
82 #define TEXTHEIGHT      200
83
84 static void instantiate_main(int argc, char **argv)
85
86     struct cmd_t *cp;
87
88     toplevel = XtVaOpenApplication(&app_context, "sst2k", NULL, 0, &argc,
89                                     argv, fallback, 
90                                     applicationShellWidgetClass,
91                                     XtNallowShellResize, True, NULL);
92     form = XtVaCreateManagedWidget("form", formWidgetClass, toplevel, NULL);
93     /* the command window */
94     text = XtVaCreateManagedWidget("text", asciiTextWidgetClass, form, 
95                                    XtNwidth, MAXWIDTH, XtNheight, TEXTHEIGHT,
96                                    NULL);
97     XtVaSetValues(text, XtNeditType,XawtextRead, XtNdisplayCaret,False, NULL);
98     /* The button panels */
99     navigation  = XtVaCreateManagedWidget("navigation", 
100                                           boxWidgetClass, form,
101                                           XtNborderWidth, 0,
102                                           XtNfromVert, text,
103                                           XtNorientation, XtorientHorizontal,
104                                           NULL); 
105     navlabel  = XtVaCreateManagedWidget("Navigation:   ", 
106                                         labelWidgetClass, navigation,
107                                         XtNborderWidth, 0,
108                                         NULL); 
109     weapons  = XtVaCreateManagedWidget("weapons", 
110                                        boxWidgetClass, form,
111                                        XtNborderWidth, 0,
112                                        XtNfromVert, navigation, 
113                                        XtNorientation, XtorientHorizontal,
114                                        NULL); 
115     weaplabel  = XtVaCreateManagedWidget("Weapons:      ", 
116                                          labelWidgetClass, weapons,
117                                          XtNborderWidth, 0,
118                                          NULL); 
119     status   = XtVaCreateManagedWidget("status", 
120                                        boxWidgetClass, form,
121                                        XtNborderWidth, 0,
122                                        XtNfromVert, weapons, 
123                                        XtNorientation, XtorientHorizontal,
124                                        NULL); 
125     statlabel  = XtVaCreateManagedWidget("Status:       ", 
126                                          labelWidgetClass, status,
127                                          XtNborderWidth, 0,
128                                          NULL); 
129     planets  = XtVaCreateManagedWidget("planets", 
130                                        boxWidgetClass, form,
131                                        XtNborderWidth, 0,
132                                        XtNfromVert, status, 
133                                        XtNorientation, XtorientHorizontal,
134                                        NULL); 
135     planlabel  = XtVaCreateManagedWidget("Planets:      ", 
136                                          labelWidgetClass, planets,
137                                          XtNborderWidth, 0,
138                                          NULL); 
139     misc  = XtVaCreateManagedWidget("misc", 
140                                        boxWidgetClass, form,
141                                        XtNborderWidth, 0,
142                                        XtNfromVert, planets, 
143                                        XtNorientation, XtorientHorizontal,
144                                        NULL); 
145     misclabel  = XtVaCreateManagedWidget("Miscellaneous:", 
146                                          labelWidgetClass, misc,
147                                          XtNborderWidth, 0,
148                                          NULL); 
149     for (cp = commands; cp < commands + sizeof(commands)/sizeof(commands[0]); cp++) {
150         cp->widget = XtVaCreateManagedWidget(cp->name, 
151                                              commandWidgetClass, 
152                                              *cp->parent, 
153                                              XtNlabel, cp->name,
154                                              NULL);
155         if (cp->callback)
156             XtAddCallback (cp->widget, XtNcallback, cp->callback, NULL);
157     }
158     XtRealizeWidget(toplevel);
159     XtAppMainLoop(app_context);
160     /* loop may be interrupted */
161     XtDestroyApplicationContext(app_context);
162 }
163
164 int main(int argc, char **argv)
165 {
166     instantiate_main(argc, argv);
167     exit(0);
168 }