126da025166b1ba5de6ca7c9ea4a89f12f3814e7
[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 #undef length   /* ugh -- must get rid of ugly #defines */
12
13 static XtAppContext app_context;
14 static Widget toplevel, text, form; 
15 static Widget navigation, weapons, planets, misc; 
16 static Widget navlabel, weaplabel, planlabel, misclabel;
17
18 static String fallback[] = {
19     "*text.resizable: true",
20     "*text.resize: ResizeBoth",
21     "*text.width: 640",
22     "*text.height: 200",
23     "*text.autoFill: True",
24     "*text.scrollVertical: Always",
25     "*text.scrollHorizontal: WhenNeeded",
26     "*text.displayCaret: True",
27     "*navigation.fromVert: text",
28     "*navigation.borderWidth: 0",
29     "*navlabel.label: Navigation:   ",
30     "*navlabel.borderWidth: 0",
31     "*weapons.fromVert: navigation",
32     "*weapons.borderWidth: 0",
33     "*weaplabel.label: Weapons:      ",
34     "*weaplabel.borderWidth: 0",
35     "*planets.fromVert: weapons",
36     "*planets.borderWidth: 0",
37     "*planlabel.label: Planets:      ",
38     "*planlabel.borderWidth: 0",
39     "*misc.fromVert: planets",
40     "*misc.borderWidth: 0",
41     "*misclabel.label: Miscellaneous:",
42     "*misclabel.borderWidth: 0",
43     NULL,
44 };
45
46 struct cmd_t {
47     char *name;
48     void (*callback)(Widget, XtPointer, XtPointer);
49     Widget *parent;
50     int enable;
51     Widget widget;
52
53 };
54
55 static void quit_proc(Widget w, XtPointer client_data, XtPointer call_data)
56
57     XtDestroyApplicationContext (app_context);
58     exit (0);
59 }
60
61 static void text_append_to(Widget w, String str)
62 /* append text to a specified text widget */
63 {
64     XawTextBlock txtblk;
65     XawTextPosition textend = XawTextGetInsertionPoint(w);
66
67     txtblk.ptr = str;
68     txtblk.length = strlen(str);
69     txtblk.firstPos = 0;
70     txtblk.format = FMT8BIT;
71
72     XawTextReplace(w, textend, textend, &txtblk);
73 }
74
75 static void noargs_proc(Widget w, XtPointer client_data, XtPointer call_data)
76 /* use this for commands that take no arguments */
77 {
78     /* currently a stub */
79     text_append_to(w, XtName(w));
80     printf("Button %s pressed\n", XtName(w));
81 }
82
83 static struct cmd_t commands[] = {
84     {"Move",            NULL,           &navigation,    0},
85     {"Dock",            noargs_proc,    &navigation,    0},
86     {"Chart",           noargs_proc,    &navigation,    0},
87     {"Impulse",         NULL,           &navigation,    0},
88     {"Rest",            NULL,           &navigation,    0},
89     {"Warp",            NULL,           &navigation,    0},
90     {"Probe",           NULL,           &navigation,    OPTION_PROBE},
91
92     {"Phasers",         NULL,           &weapons,       0},
93     {"Torpedo",         NULL,           &weapons,       0},
94     {"Shields",         NULL,           &weapons,       0},
95     {"Damages",         noargs_proc,    &weapons,       0},
96     {"Abandon",         noargs_proc,    &weapons,       0},
97     {"Destruct",        noargs_proc,    &weapons,       0},
98     {"Deathray",        noargs_proc,    &weapons,       0},
99     {"Mayday",          noargs_proc,    &weapons,       0},
100
101     {"Sensors",         noargs_proc,    &planets,       OPTION_PLANETS},
102     {"Orbit",           noargs_proc,    &planets,       OPTION_PLANETS},
103     {"Transport",       noargs_proc,    &planets,       OPTION_PLANETS},
104     {"Mine",            noargs_proc,    &planets,       OPTION_PLANETS},
105     {"Crystals",        noargs_proc,    &planets,       OPTION_PLANETS},
106     {"Shuttle",         noargs_proc,    &planets,       OPTION_PLANETS},
107     {"Planets",         noargs_proc,    &planets,       OPTION_PLANETS},
108
109     {"Score",           noargs_proc,    &misc,          0},
110     {"Report",          noargs_proc,    &misc,          0},
111     {"Computer",        noargs_proc,    &misc,          0},
112     {"Save",            NULL,           &misc,          0},
113     {"Quit",            quit_proc,      &misc,          0},
114     {"Help",            noargs_proc,    &misc,          0},
115 };
116
117 static void instantiate_main(int argc, char **argv)
118
119     struct cmd_t *cp;
120
121     toplevel = XtVaOpenApplication(&app_context, "sst2k", NULL, 0, &argc,
122                                     argv, fallback, 
123                                     applicationShellWidgetClass,
124                                     XtNallowShellResize, True, NULL);
125     form = XtVaCreateManagedWidget("form", formWidgetClass, toplevel, NULL);
126     /* the command window */
127     text = XtVaCreateManagedWidget("text", 
128                                    asciiTextWidgetClass, form,
129                                    XtNeditType, XawtextEdit,
130                                    NULL);
131     /* The button panels */
132     navigation  = XtVaCreateManagedWidget("navigation", 
133                                           boxWidgetClass, form,
134                                           XtNorientation, XtorientHorizontal,
135                                           NULL); 
136     navlabel  = XtVaCreateManagedWidget("navlabel", 
137                                         labelWidgetClass, navigation,
138                                         NULL); 
139     weapons  = XtVaCreateManagedWidget("weapons", 
140                                        boxWidgetClass, form,
141                                        XtNorientation, XtorientHorizontal,
142                                        NULL); 
143     weaplabel  = XtVaCreateManagedWidget("weaplabel", 
144                                          labelWidgetClass, weapons,
145                                          NULL); 
146     planets  = XtVaCreateManagedWidget("planets", 
147                                        boxWidgetClass, form,
148                                        XtNorientation, XtorientHorizontal,
149                                        NULL); 
150     planlabel  = XtVaCreateManagedWidget("planlabel", 
151                                          labelWidgetClass, planets,
152                                          XtNborderWidth, 0,
153                                          NULL); 
154     misc  = XtVaCreateManagedWidget("misc", 
155                                        boxWidgetClass, form,
156                                        XtNorientation, XtorientHorizontal,
157                                        NULL); 
158     misclabel  = XtVaCreateManagedWidget("misclabel", 
159                                          labelWidgetClass, misc,
160                                          NULL); 
161     for (cp = commands; cp < commands + sizeof(commands)/sizeof(commands[0]); cp++) {
162         cp->widget = XtVaCreateManagedWidget(cp->name, 
163                                              commandWidgetClass, 
164                                              *cp->parent, 
165                                              XtNlabel, cp->name,
166                                              NULL);
167         if (cp->callback)
168             XtAddCallback (cp->widget, XtNcallback, cp->callback, NULL);
169     }
170     XtRealizeWidget(toplevel);
171     XtAppMainLoop(app_context);
172     /* loop may be interrupted */
173     XtDestroyApplicationContext(app_context);
174 }
175
176 int main(int argc, char **argv)
177 {
178     instantiate_main(argc, argv);
179     exit(0);
180 }