]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
279f550ffc80bd1806636e552ed0673adaaab47f
[rrace.git] / src / motif_ui.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20 #include <inttypes.h>
21 #include <assert.h>
22 #include <Xm/XmAll.h>
23
24 #include "motif.h"
25 #include "motifstr.h"
26 #include "motifgui.h"
27
28 #define SPLIT_NUMERATOR    75
29 #define SPLIT_DENOMINATOR 100
30
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32
33 static void game_configure(Widget w);
34
35 /* XXX generate this list? */
36 enum {
37         widgetMainWindow,
38         widgetForm,
39         widgetFrame,
40         widgetDrawingArea,
41         widgetCascadeButton,
42         widgetPushButton,
43         widgetMax,
44
45         /* Pseudo-widgets */
46         widgetMenuBar = widgetMax
47 };
48
49 static WidgetClass * const widgets[widgetMax] = {
50         &xmMainWindowWidgetClass,
51         &xmFormWidgetClass,
52         &xmFrameWidgetClass,
53         &xmDrawingAreaWidgetClass,
54         &xmCascadeButtonWidgetClass,
55         &xmPushButtonWidgetClass
56 };
57
58 static const struct ui_widget {
59         uint_least16_t name;
60         uint_least8_t subtree;
61         uint_least8_t widget_type;
62 } mainwin[] = { MAINWIN_INITIALIZER };
63
64 static const struct ui_menuitem {
65         struct ui_widget w;
66         uint_least16_t label;
67 } mainmenu[] = { MAINMENU_INITIALIZER };
68
69 static void
70 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
71 {
72         Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
73         Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
74         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
75         int x, y, gap;
76
77         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
78         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
79         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
80                             XmNleftOffset, &gap,
81                             (char *)NULL);
82
83         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
84         gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
85
86         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
87         goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
88
89         x = (w - gamesz - goalsz - gap) / 2;
90         if (x < 2) x = 0;
91
92         y = (h - gamesz) / 2;
93         if (y < 3) y = 0;
94
95         XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
96         XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
97         XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
98 }
99
100 static void game_configure(Widget form)
101 {
102         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
103         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
104         XtActionsRec resize_rec;
105
106         assert(gamearea && goalarea);
107         XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
108
109         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
110                                 XmNtopAttachment, XmATTACH_FORM,
111                                 (char *)NULL);
112
113         XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
114                                 XmNleftWidget, gamearea,
115                                 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
116                                 XmNtopWidget, gamearea,
117                                 (char *)NULL);
118
119         resize_rec.string = "ResizeGameArea";
120         resize_rec.proc = ResizeGameArea;
121         XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
122         XtOverrideTranslations(form, XtParseTranslationTable(
123                 "<Configure>: ResizeGameArea()\n"
124                 "<Map>: ResizeGameArea()\n"
125         ));
126 }
127
128 static Widget create_widget(const struct ui_widget *item, Widget parent,
129                             ArgList args, Cardinal num_args)
130 {
131         String name = (void *)&tree_strtab[item->name];
132         WidgetClass class;
133         Widget w;
134
135         if (item->widget_type == widgetMenuBar)
136                 return XmCreateMenuBar(parent, name, args, num_args);
137
138         assert(item->widget_type < widgetMax);
139         class = *widgets[item->widget_type];
140         return XtCreateWidget(name, class, parent, args, num_args);
141 }
142
143 static void
144 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
145 {
146         const struct ui_widget *item;
147
148         for (item = &root[i]; item->name; item++) {
149                 Widget w = create_widget(item, parent, NULL, 0);
150
151                 if (item->subtree)
152                         construct_widgets(root, w, item->subtree);
153
154                 XtManageChild(w);
155         }
156 }
157
158 static void menu_cb(Widget w, void *data, void *cb_data)
159 {
160         XmRowColumnCallbackStruct *cbs = cb_data;
161         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
162 }
163
164 static Widget create_pulldown(Widget parent)
165 {
166         Widget w;
167
168         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
169         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
170         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
171
172         return w;
173 }
174
175 static void
176 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
177 {
178         const struct ui_menuitem *item;
179
180         for (item = &root[i]; item->w.name; item++) {
181                 const char *label = &strtab[item->label];
182                 unsigned n = 0;
183                 Arg args[2];
184                 XmString s;
185                 Widget w;
186
187                 if (XtClass(parent) == *widgets[widgetCascadeButton])
188                         parent = create_pulldown(parent);
189
190                 if (label[0] && label[1] == '|') {
191                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
192                         label += 2;
193                 }
194
195                 s = XmStringCreateLocalized((void *)label);
196                 XtSetArg(args[n], XmNlabelString, s); n++;
197
198                 w = create_widget(&item->w, parent, args, n);
199
200                 XmStringFree(s);
201
202                 if (item->w.subtree)
203                         construct_menu(root, w, item->w.subtree);
204
205                 XtManageChild(w);
206         }
207 }
208
209 static void game_resize(Widget w, void *data, void *cb_data)
210 {
211         if (XtIsRealized(w))
212                 x11_redraw_game(data);
213 }
214
215 static void goal_resize(Widget w, void *data, void *cb_data)
216 {
217         if (XtIsRealized(w))
218                 x11_redraw_goal(data);
219 }
220
221 static void game_input(Widget w, void *data, void *cb_data)
222 {
223         XmDrawingAreaCallbackStruct *cbs = cb_data;
224         XButtonEvent *click = &cbs->event->xbutton;
225         struct app_state *state = data;
226         Dimension width, height;
227         unsigned x = -1, y = -1;
228
229         switch (cbs->event->type) {
230         case ButtonPress:
231                 XtVaGetValues(w, XmNwidth, &width,
232                                  XmNheight, &height,
233                                  (char *)NULL);
234                 x = click->x / (width/5);
235                 y = click->y / (width/5);
236         }
237
238         if (x > 4 || y > 4)
239                 return;
240
241         if (game_do_move(&state->board, x, y) == 0) {
242                 if (game_check_goal(&state->board)) {
243                         printf("You win!\n");
244                         game_finish(&state->board);
245                 }
246
247                 x11_redraw_game(state);
248         }
249 }
250
251 void ui_initialize(struct app_state *state, Widget shell)
252 {
253         Widget menubar, help;
254
255         construct_widgets(mainwin, shell, 0);
256
257         menubar = XtNameToWidget(shell, "*menuBar");
258         construct_menu(mainmenu, menubar, 0);
259
260         help = XtNameToWidget(menubar, "helpMenu");
261         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
262
263         game_configure(XtNameToWidget(shell, "*game"));
264
265         state->game = XtNameToWidget(shell, "*gameCanvas");
266         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
267         XtAddCallback(state->game, XmNexposeCallback, game_resize, state);
268         XtAddCallback(state->game, XmNinputCallback,  game_input,  state);
269
270         state->goal = XtNameToWidget(shell, "*goalCanvas");
271         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
272         XtAddCallback(state->goal, XmNexposeCallback, goal_resize, state);
273 }