]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
05721e09a95b1515dcf79feb25b7f54320f43c04
[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 #include "version.h"
29
30 #define SPLIT_NUMERATOR    75
31 #define SPLIT_DENOMINATOR 100
32
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
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         widgetMessageDialog,
48         widgetScrolledText,
49
50         widgetUnmanaged = 128
51 };
52
53 static WidgetClass * const widgets[widgetMax] = {
54         &xmMainWindowWidgetClass,
55         &xmFormWidgetClass,
56         &xmFrameWidgetClass,
57         &xmDrawingAreaWidgetClass,
58         &xmCascadeButtonWidgetClass,
59         &xmPushButtonWidgetClass
60 };
61
62 static const struct ui_widget {
63         uint_least16_t name;
64         uint_least8_t subtree;
65         uint_least8_t widget_type;
66 } mainwin[] = { MAINWIN_INITIALIZER };
67
68 static const struct ui_menuitem {
69         struct ui_widget w;
70         uint_least16_t label;
71 } mainmenu[] = { MAINMENU_INITIALIZER };
72
73 static void
74 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
75 {
76         Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
77         Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
78         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
79         int x, y, gap;
80
81         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
82         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
83         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
84                             XmNleftOffset, &gap,
85                             (char *)NULL);
86
87         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
88         gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
89
90         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
91         goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
92
93         x = (w - gamesz - goalsz - gap) / 2;
94         if (x < 2) x = 0;
95
96         y = (h - gamesz) / 2;
97         if (y < 3) y = 0;
98
99         XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
100         XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
101         XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
102 }
103
104 static char *timer_text(int_fast32_t ms, char *buf)
105 {
106         unsigned min, sec;
107
108         sec = ms / 1000, ms %= 1000;
109         min = sec / 60, sec %= 60;
110         sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
111
112         return buf;
113 }
114
115 void ui_timer_update(struct app_state *state, int_fast32_t ms)
116 {
117         char buf[100];
118
119         if (ms < 0) {
120                 xcounter_simple_update(state->timer, "\n");
121                 return;
122         }
123
124         xcounter_simple_update(state->timer, timer_text(ms, buf));
125 }
126
127 static void configure_mainwin(struct app_state *state, Widget form)
128 {
129         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
130         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
131         XtActionsRec resize_rec;
132         char xc_template[100];
133
134         assert(gamearea && goalarea);
135         XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
136
137         state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
138         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
139                                 XmNtopAttachment, XmATTACH_FORM,
140                                 (char *)NULL);
141
142         state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
143         XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
144                                 XmNleftWidget, gamearea,
145                                 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
146                                 XmNtopWidget, gamearea,
147                                 (char *)NULL);
148
149         state->timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
150         XtVaSetValues(state->timer, XmNleftAttachment, XmATTACH_WIDGET,
151                                     XmNleftWidget, gamearea,
152                                     XmNtopAttachment, XmATTACH_WIDGET,
153                                     XmNtopWidget, goalarea,
154                                     XmNrightAttachment, XmATTACH_FORM,
155                                     (char *)NULL);
156
157         xcounter_simple_setup(state->timer, timer_text(20000, xc_template));
158         ui_timer_update(state, -1);
159
160         resize_rec.string = "ResizeGameArea";
161         resize_rec.proc = ResizeGameArea;
162         XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
163         XtOverrideTranslations(form, XtParseTranslationTable(
164                 "<Configure>: ResizeGameArea()\n"
165                 "<Map>: ResizeGameArea()\n"
166         ));
167
168         /*
169          * Performing the initial update of the layout seems to avoid
170          * some weird problems on Motif 2.1
171          */
172         ResizeGameArea(form, 0, 0, 0);
173 }
174
175 static Widget create_widget(const struct ui_widget *item, Widget parent,
176                             ArgList args, Cardinal num_args)
177 {
178         String name = (void *)&tree_strtab[item->name];
179         unsigned type = item->widget_type & widgetUnmanaged-1;
180
181         switch (type) {
182         case widgetMenuBar:
183                 return XmCreateMenuBar(parent, name, args, num_args);
184         case widgetMessageDialog:
185                 return XmCreateMessageDialog(parent, name, args, num_args);
186         case widgetScrolledText:
187                 return XmCreateScrolledText(parent, name, args, num_args);
188         }
189
190         assert(type < widgetMax);
191         return XtCreateWidget(name, *widgets[type], parent, args, num_args);
192 }
193
194 static void
195 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
196 {
197         const struct ui_widget *item;
198
199         for (item = &root[i]; item->name; item++) {
200                 Widget w = create_widget(item, parent, NULL, 0);
201
202                 if (item->subtree)
203                         construct_widgets(root, w, item->subtree);
204
205                 if (!(item->widget_type & widgetUnmanaged))
206                         XtManageChild(w);
207         }
208 }
209
210 static void menu_cb(Widget w, void *data, void *cb_data)
211 {
212         XmRowColumnCallbackStruct *cbs = cb_data;
213         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
214 }
215
216 static Widget create_pulldown(Widget parent)
217 {
218         Widget w;
219
220         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
221         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
222         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
223
224         return w;
225 }
226
227 static void
228 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
229 {
230         const struct ui_menuitem *item;
231
232         for (item = &root[i]; item->w.name; item++) {
233                 const char *label = &strtab[item->label];
234                 unsigned n = 0;
235                 Arg args[2];
236                 XmString s;
237                 Widget w;
238
239                 if (XtClass(parent) == *widgets[widgetCascadeButton])
240                         parent = create_pulldown(parent);
241
242                 if (label[0] && label[1] == '|') {
243                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
244                         label += 2;
245                 }
246
247                 s = XmStringCreateLocalized((void *)label);
248                 XtSetArg(args[n], XmNlabelString, s); n++;
249
250                 w = create_widget(&item->w, parent, args, n);
251
252                 XmStringFree(s);
253
254                 if (item->w.subtree)
255                         construct_menu(root, w, item->w.subtree);
256
257                 XtManageChild(w);
258         }
259 }
260
261 /* Figure out which tiles intersect a rectangle. */
262 static uint_fast32_t
263 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
264                                     int tile_w, int tile_h)
265 {
266         return board_right(rect_x / tile_w)
267              & board_below(rect_y / tile_h)
268              & board_above((rect_y + rect_h - 1) / tile_h)
269              & board_left((rect_x + rect_w - 1) / tile_w);
270 }
271
272 /*
273  * Calculate which tiles need to be redrawn after resizing.  This is the
274  * tiles which (at the new size) are fully contained in the previous area.
275  *
276  * When shrinking, this is all tiles, but when enlarging, the generated
277  * expose events will trigger drawing of the new area.
278  */
279 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
280 {
281         Dimension new_w, new_h, common_w, common_h;
282
283         if (!XtIsRealized(w))
284                 return 0;
285
286         XtVaGetValues(w, XmNwidth, &new_w, XmNheight, &new_h, (char *)NULL);
287         common_w = MIN(new_w, oldsz[0]);
288         common_h = MIN(new_h, oldsz[1]);
289         oldsz[0] = new_w;
290         oldsz[1] = new_h;
291         new_w /= gridsz;
292         new_h /= gridsz;
293
294         /* Round down to multiple of tile dimensions */
295         common_w = common_w / new_w * new_w;
296         common_h = common_h / new_h * new_h;
297
298         if (new_w > 0 && new_h > 0)
299                 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
300         return 0;
301 }
302
303 static void game_resize(Widget w, void *data, void *cb_data)
304 {
305         struct app_state *state = data;
306         uint_fast32_t mask;
307
308         mask = resize_helper(w, state->game_sz, 5);
309         if (mask)
310                 x11_redraw_game(data, mask);
311 }
312
313 static void goal_resize(Widget w, void *data, void *cb_data)
314 {
315         struct app_state *state = data;
316         uint_fast32_t mask;
317
318         mask = resize_helper(w, state->goal_sz, 3);
319         if (mask)
320                 x11_redraw_goal(data, mask);
321 }
322
323 static void game_expose(Widget w, void *data, void *cb_data)
324 {
325         XmDrawingAreaCallbackStruct *cbs = cb_data;
326         XExposeEvent *e = &cbs->event->xexpose;
327         Dimension width, height;
328         uint_fast32_t mask;
329
330         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
331         if (!(width /= 5) || !(height /= 5))
332                 return;
333
334         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
335         x11_redraw_game(data, mask);
336 }
337
338 static void goal_expose(Widget w, void *data, void *cb_data)
339 {
340         XmDrawingAreaCallbackStruct *cbs = cb_data;
341         XExposeEvent *e = &cbs->event->xexpose;
342         Dimension width, height;
343         uint_fast32_t mask;
344
345         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
346         if (!(width /= 3) || !(height /= 3))
347                 return;
348
349         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
350         x11_redraw_goal(data, mask);
351 }
352
353 void ui_initialize(struct app_state *state, Widget shell)
354 {
355         Widget menubar, help;
356
357         construct_widgets(mainwin, shell, 0);
358
359         menubar = XtNameToWidget(shell, "*menuBar");
360         construct_menu(mainmenu, menubar, 0);
361
362         help = XtNameToWidget(menubar, "helpMenu");
363         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
364
365         configure_mainwin(state, XtNameToWidget(shell, "*game"));
366
367         XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
368                                    XmNheight, &state->game_sz[1],
369                                    (char *)NULL);
370         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
371         XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
372
373         XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
374                                    XmNheight, &state->goal_sz[1],
375                                    (char *)NULL);
376         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
377         XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
378 }
379
380 static void dialog_close(Widget w, void *data, void *cb_data)
381 {
382         XtDestroyWidget(w);
383 }
384
385 /*
386  * Expands to an XtVaSetValues argument list to set the given resource with
387  * a typed string value, which is internally converted to the appropriate
388  * resource type.
389  *
390  * Note that str is expanded twice and thus should not have side effects.
391  */
392 #define STRING_ARG(resource, str) \
393         XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
394
395 void ui_show_about(struct app_state *state, Widget shell)
396 {
397         static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
398         Widget w, l;
399         char *msg;
400
401         construct_widgets(dialog, shell, 0);
402
403         w = XtNameToWidget(shell, "*aboutDialog");
404         XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
405
406         XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
407
408         XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
409
410         msg = version_format_head("rrace-motif");
411         l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
412         XtVaSetValues(l, XmNlabelType, XmPIXMAP_AND_STRING,
413                          XmNlabelPixmap, state->icon_pixmap,
414                          STRING_ARG(XmNlabelString, msg),
415                          (char *)NULL);
416         free(msg);
417
418         l = XtNameToWidget(w, "*licenseBlurb");
419         XmTextSetString(l,
420 "This program is free software: you can redistribute it and/or modify\n"
421 "it under the terms of the GNU General Public License as published by\n"
422 "the Free Software Foundation, either version 3 of the License, or\n"
423 "(at your option) any later version.\n\n"
424 "This program is distributed in the hope that it will be useful,\n"
425 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
426 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
427 "GNU General Public License for more details.\n\n"
428 "You should have received a copy of the GNU General Public License\n"
429 "along with this program.  If not, see <https://www.gnu.org/licenses/>.");
430
431         XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
432                          XmNeditable, FALSE,
433                          XmNresizeWidth, TRUE,
434                          XmNrows, 5,
435                          (char *)NULL);
436
437         XtManageChild(w);
438 }