]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
5056d9c7e59f6135c3fe7ddc0bbb4e2b6a8689d1
[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 x11_expose_mask(XExposeEvent *e, int tile_w, int tile_h)
262 {
263         return board_rect( e->x/tile_w,
264                            e->y/tile_h,
265                           (e->x+e->width-1)/tile_w,
266                           (e->y+e->height-1)/tile_h );
267 }
268
269 static void game_resize(Widget w, void *data, void *cb_data)
270 {
271         x11_queue_render(data, -1, 0);
272 }
273
274 static void goal_resize(Widget w, void *data, void *cb_data)
275 {
276         x11_queue_render(data, 0, -1);
277 }
278
279 static void expose(Widget w, void *data, void *cb_data)
280 {
281         XmDrawingAreaCallbackStruct *cbs = cb_data;
282         XExposeEvent *e = &cbs->event->xexpose;
283         struct app_state *state = data;
284         Dimension tile_w, tile_h;
285
286         XtVaGetValues(w, XmNwidth, &tile_w, XmNheight, &tile_h, (char *)NULL);
287         if (w == state->game) {
288                 uint_least32_t *gp = state->board.game;
289                 uint_fast32_t mask;
290
291                 if (!(tile_w /= 5) || !(tile_h /= 5))
292                         return;
293
294                 /*
295                  * Only draw exposed nonempty tiles; exposed areas are filled
296                  * with the background automatically and thus exposed empty
297                  * spaces don't need to be drawn again.
298                  */
299                 mask = gp[0] | gp[1] | gp[2];
300                 mask &= x11_expose_mask(e, tile_w, tile_h);
301
302                 x11_queue_render(state, mask, 0);
303         } else {
304                 if (!(tile_w /= 3) || !(tile_h /= 3))
305                         return;
306
307                 x11_queue_render(state, 0, x11_expose_mask(e, tile_w, tile_h));
308         }
309 }
310
311 void ui_initialize(struct app_state *state, Widget shell)
312 {
313         Widget menubar, help;
314
315         construct_widgets(mainwin, shell, 0);
316
317         menubar = XtNameToWidget(shell, "*menuBar");
318         construct_menu(mainmenu, menubar, 0);
319
320         help = XtNameToWidget(menubar, "helpMenu");
321         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
322
323         configure_mainwin(state, XtNameToWidget(shell, "*game"));
324
325         XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
326                                    XmNheight, &state->game_sz[1],
327                                    (char *)NULL);
328         XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
329         XtAddCallback(state->game, XmNexposeCallback, expose, state);
330
331         XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
332                                    XmNheight, &state->goal_sz[1],
333                                    (char *)NULL);
334         XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
335         XtAddCallback(state->goal, XmNexposeCallback, expose, state);
336 }
337
338 static void dialog_close(Widget w, void *data, void *cb_data)
339 {
340         XtDestroyWidget(w);
341 }
342
343 /*
344  * Expands to an XtVaSetValues argument list to set the given resource with
345  * a typed string value, which is internally converted to the appropriate
346  * resource type.
347  *
348  * Note that str is expanded twice and thus should not have side effects.
349  */
350 #define STRING_ARG(resource, str) \
351         XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
352
353 void ui_show_about(struct app_state *state, Widget shell)
354 {
355         static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
356         Widget w, l;
357         char *msg;
358
359         construct_widgets(dialog, shell, 0);
360
361         w = XtNameToWidget(shell, "*aboutDialog");
362         XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
363
364         XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
365
366         XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
367
368         msg = version_format_head("rrace-motif");
369         l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
370         XtVaSetValues(l, XmNlabelType, XmSTRING,
371 #if HAVE_MOTIF_PIXMAP_AND_STRING
372                          XmNlabelType, XmPIXMAP_AND_STRING,
373 #endif
374                          XmNlabelPixmap, state->icon_pixmap,
375                          STRING_ARG(XmNlabelString, msg),
376                          (char *)NULL);
377         free(msg);
378
379         l = XtNameToWidget(w, "*licenseBlurb");
380         XmTextSetString(l,
381 "This program is free software: you can redistribute it and/or modify\n"
382 "it under the terms of the GNU General Public License as published by\n"
383 "the Free Software Foundation, either version 3 of the License, or\n"
384 "(at your option) any later version.\n\n"
385 "This program is distributed in the hope that it will be useful,\n"
386 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
387 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
388 "GNU General Public License for more details.\n\n"
389 "You should have received a copy of the GNU General Public License\n"
390 "along with this program.  If not, see <https://www.gnu.org/licenses/>.");
391
392         XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
393                          XmNeditable, FALSE,
394                          XmNresizeWidth, TRUE,
395                          XmNrows, 5,
396                          (char *)NULL);
397
398         XtManageChild(w);
399 }