]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
a49dfec0717bd58ca985bcebcb48ae8b4128b00b
[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 #include "xcounter.h"
28
29 #define SPLIT_NUMERATOR    75
30 #define SPLIT_DENOMINATOR 100
31
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33
34 /* XXX generate this list? */
35 enum {
36         widgetMainWindow,
37         widgetForm,
38         widgetFrame,
39         widgetDrawingArea,
40         widgetCascadeButton,
41         widgetPushButton,
42         widgetMax,
43
44         /* Pseudo-widgets */
45         widgetMenuBar = widgetMax
46 };
47
48 static WidgetClass * const widgets[widgetMax] = {
49         &xmMainWindowWidgetClass,
50         &xmFormWidgetClass,
51         &xmFrameWidgetClass,
52         &xmDrawingAreaWidgetClass,
53         &xmCascadeButtonWidgetClass,
54         &xmPushButtonWidgetClass
55 };
56
57 static const struct ui_widget {
58         uint_least16_t name;
59         uint_least8_t subtree;
60         uint_least8_t widget_type;
61 } mainwin[] = { MAINWIN_INITIALIZER };
62
63 static const struct ui_menuitem {
64         struct ui_widget w;
65         uint_least16_t label;
66 } mainmenu[] = { MAINMENU_INITIALIZER };
67
68 static void
69 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
70 {
71         Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
72         Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
73         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
74         int x, y, gap;
75
76         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
77         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
78         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
79                             XmNleftOffset, &gap,
80                             (char *)NULL);
81
82         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
83         gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
84
85         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
86         goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
87
88         x = (w - gamesz - goalsz - gap) / 2;
89         if (x < 2) x = 0;
90
91         y = (h - gamesz) / 2;
92         if (y < 3) y = 0;
93
94         XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
95         XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
96         XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
97 }
98
99 static char *timer_text(int_fast32_t ms, char *buf)
100 {
101         unsigned min, sec;
102
103         sec = ms / 1000, ms %= 1000;
104         min = sec / 60, sec %= 60;
105         sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
106
107         return buf;
108 }
109
110 void ui_timer_update(struct app_state *state, int_fast32_t ms)
111 {
112         char buf[100];
113
114         if (ms < 0) {
115                 xcounter_simple_update(state->timer, "\n");
116                 return;
117         }
118
119         xcounter_simple_update(state->timer, timer_text(ms, buf));
120 }
121
122 static void configure_mainwin(struct app_state *state, Widget form)
123 {
124         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
125         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
126         XtActionsRec resize_rec;
127         char xc_template[100];
128
129         assert(gamearea && goalarea);
130         XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
131
132         state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
133         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
134                                 XmNtopAttachment, XmATTACH_FORM,
135                                 (char *)NULL);
136
137         state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
138         XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
139                                 XmNleftWidget, gamearea,
140                                 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
141                                 XmNtopWidget, gamearea,
142                                 (char *)NULL);
143
144         state->timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
145         XtVaSetValues(state->timer, XmNleftAttachment, XmATTACH_WIDGET,
146                                     XmNleftWidget, gamearea,
147                                     XmNtopAttachment, XmATTACH_WIDGET,
148                                     XmNtopWidget, goalarea,
149                                     XmNrightAttachment, XmATTACH_FORM,
150                                     (char *)NULL);
151
152         xcounter_simple_setup(state->timer, timer_text(20000, xc_template));
153         ui_timer_update(state, -1);
154
155         resize_rec.string = "ResizeGameArea";
156         resize_rec.proc = ResizeGameArea;
157         XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
158         XtOverrideTranslations(form, XtParseTranslationTable(
159                 "<Configure>: ResizeGameArea()\n"
160                 "<Map>: ResizeGameArea()\n"
161         ));
162
163         /*
164          * Performing the initial update of the layout seems to avoid
165          * some weird problems on Motif 2.1
166          */
167         ResizeGameArea(form, 0, 0, 0);
168 }
169
170 static Widget create_widget(const struct ui_widget *item, Widget parent,
171                             ArgList args, Cardinal num_args)
172 {
173         String name = (void *)&tree_strtab[item->name];
174         WidgetClass class;
175
176         if (item->widget_type == widgetMenuBar)
177                 return XmCreateMenuBar(parent, name, args, num_args);
178
179         assert(item->widget_type < widgetMax);
180         class = *widgets[item->widget_type];
181         return XtCreateWidget(name, class, parent, args, num_args);
182 }
183
184 static void
185 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
186 {
187         const struct ui_widget *item;
188
189         for (item = &root[i]; item->name; item++) {
190                 Widget w = create_widget(item, parent, NULL, 0);
191
192                 if (item->subtree)
193                         construct_widgets(root, w, item->subtree);
194
195                 XtManageChild(w);
196         }
197 }
198
199 static void menu_cb(Widget w, void *data, void *cb_data)
200 {
201         XmRowColumnCallbackStruct *cbs = cb_data;
202         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
203 }
204
205 static Widget create_pulldown(Widget parent)
206 {
207         Widget w;
208
209         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
210         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
211         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
212
213         return w;
214 }
215
216 static void
217 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
218 {
219         const struct ui_menuitem *item;
220
221         for (item = &root[i]; item->w.name; item++) {
222                 const char *label = &strtab[item->label];
223                 unsigned n = 0;
224                 Arg args[2];
225                 XmString s;
226                 Widget w;
227
228                 if (XtClass(parent) == *widgets[widgetCascadeButton])
229                         parent = create_pulldown(parent);
230
231                 if (label[0] && label[1] == '|') {
232                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
233                         label += 2;
234                 }
235
236                 s = XmStringCreateLocalized((void *)label);
237                 XtSetArg(args[n], XmNlabelString, s); n++;
238
239                 w = create_widget(&item->w, parent, args, n);
240
241                 XmStringFree(s);
242
243                 if (item->w.subtree)
244                         construct_menu(root, w, item->w.subtree);
245
246                 XtManageChild(w);
247         }
248 }
249
250 /* Figure out which tiles intersect a rectangle. */
251 static uint_fast32_t
252 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
253                                     int tile_w, int tile_h)
254 {
255         return board_right(rect_x / tile_w)
256              & board_below(rect_y / tile_h)
257              & board_above((rect_y + rect_h - 1) / tile_h)
258              & board_left((rect_x + rect_w - 1) / tile_w);
259 }
260
261 /*
262  * Calculate which tiles need to be redrawn after resizing.  This is the
263  * tiles which (at the new size) are fully contained in the previous area.
264  *
265  * When shrinking, this is all tiles, but when enlarging, the generated
266  * expose events will trigger drawing of the new area.
267  */
268 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
269 {
270         Dimension new_w, new_h, common_w, common_h;
271
272         if (!XtIsRealized(w))
273                 return 0;
274
275         XtVaGetValues(w, XmNwidth, &new_w, XmNheight, &new_h, (char *)NULL);
276         common_w = MIN(new_w, oldsz[0]);
277         common_h = MIN(new_h, oldsz[1]);
278         oldsz[0] = new_w;
279         oldsz[1] = new_h;
280         new_w /= gridsz;
281         new_h /= gridsz;
282
283         /* Round down to multiple of tile dimensions */
284         common_w = common_w / new_w * new_w;
285         common_h = common_h / new_h * new_h;
286
287         if (new_w > 0 && new_h > 0)
288                 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
289         return 0;
290 }
291
292 static void game_resize(Widget w, void *data, void *cb_data)
293 {
294         struct app_state *state = data;
295         uint_fast32_t mask;
296
297         mask = resize_helper(w, state->game_sz, 5);
298         if (mask)
299                 x11_redraw_game(data, mask);
300 }
301
302 static void goal_resize(Widget w, void *data, void *cb_data)
303 {
304         struct app_state *state = data;
305         uint_fast32_t mask;
306
307         mask = resize_helper(w, state->goal_sz, 3);
308         if (mask)
309                 x11_redraw_goal(data, mask);
310 }
311
312 static void game_expose(Widget w, void *data, void *cb_data)
313 {
314         XmDrawingAreaCallbackStruct *cbs = cb_data;
315         XExposeEvent *e = &cbs->event->xexpose;
316         Dimension width, height;
317         uint_fast32_t mask;
318
319         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
320         if (!(width /= 5) || !(height /= 5))
321                 return;
322
323         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
324         x11_redraw_game(data, mask);
325 }
326
327 static void goal_expose(Widget w, void *data, void *cb_data)
328 {
329         XmDrawingAreaCallbackStruct *cbs = cb_data;
330         XExposeEvent *e = &cbs->event->xexpose;
331         Dimension width, height;
332         uint_fast32_t mask;
333
334         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
335         if (!(width /= 3) || !(height /= 3))
336                 return;
337
338         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
339         x11_redraw_goal(data, mask);
340 }
341
342 void ui_initialize(struct app_state *state, Widget shell)
343 {
344         Widget menubar, help;
345
346         construct_widgets(mainwin, shell, 0);
347
348         menubar = XtNameToWidget(shell, "*menuBar");
349         construct_menu(mainmenu, menubar, 0);
350
351         help = XtNameToWidget(menubar, "helpMenu");
352         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
353
354         configure_mainwin(state, XtNameToWidget(shell, "*game"));
355
356         XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
357                                    XmNheight, &state->game_sz[1],
358                                    (char *)NULL);
359         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
360         XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
361
362         XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
363                                    XmNheight, &state->goal_sz[1],
364                                    (char *)NULL);
365         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
366         XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
367 }