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