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