2 * X11 GUI for slide puzzle game
3 * Copyright © 2022 Nick Bowler
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.
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.
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/>.
30 #define SPLIT_NUMERATOR 75
31 #define SPLIT_DENOMINATOR 100
33 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 /* XXX generate this list? */
46 widgetMenuBar = widgetMax,
53 static WidgetClass * const widgets[widgetMax] = {
54 &xmMainWindowWidgetClass,
57 &xmDrawingAreaWidgetClass,
58 &xmCascadeButtonWidgetClass,
59 &xmPushButtonWidgetClass
62 static const struct ui_widget {
64 uint_least8_t subtree;
65 uint_least8_t widget_type;
66 } mainwin[] = { MAINWIN_INITIALIZER };
68 static const struct ui_menuitem {
71 } mainmenu[] = { MAINMENU_INITIALIZER };
74 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
76 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
77 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
78 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
81 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
82 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
83 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
87 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
88 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
90 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
91 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
93 x = (w - gamesz - goalsz - gap) / 2;
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);
104 static char *timer_text(int_fast32_t ms, char *buf)
108 sec = ms / 1000, ms %= 1000;
109 min = sec / 60, sec %= 60;
110 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
115 void ui_timer_update(struct app_state *state, int_fast32_t ms)
120 xcounter_simple_update(state->timer, "\n");
124 xcounter_simple_update(state->timer, timer_text(ms, buf));
127 static void configure_mainwin(struct app_state *state, Widget form)
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];
134 assert(gamearea && goalarea);
135 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
137 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
138 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
139 XmNtopAttachment, XmATTACH_FORM,
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,
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,
157 xcounter_simple_setup(state->timer, timer_text(20000, xc_template));
158 ui_timer_update(state, -1);
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"
169 * Performing the initial update of the layout seems to avoid
170 * some weird problems on Motif 2.1
172 ResizeGameArea(form, 0, 0, 0);
175 static Widget create_widget(const struct ui_widget *item, Widget parent,
176 ArgList args, Cardinal num_args)
178 String name = (void *)&tree_strtab[item->name];
179 unsigned type = item->widget_type & widgetUnmanaged-1;
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);
190 assert(type < widgetMax);
191 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
195 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
197 const struct ui_widget *item;
199 for (item = &root[i]; item->name; item++) {
200 Widget w = create_widget(item, parent, NULL, 0);
203 construct_widgets(root, w, item->subtree);
205 if (!(item->widget_type & widgetUnmanaged))
210 static void menu_cb(Widget w, void *data, void *cb_data)
212 XmRowColumnCallbackStruct *cbs = cb_data;
213 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
216 static Widget create_pulldown(Widget parent)
220 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
221 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
222 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
228 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
230 const struct ui_menuitem *item;
232 for (item = &root[i]; item->w.name; item++) {
233 const char *label = &strtab[item->label];
239 if (XtClass(parent) == *widgets[widgetCascadeButton])
240 parent = create_pulldown(parent);
242 if (label[0] && label[1] == '|') {
243 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
247 s = XmStringCreateLocalized((void *)label);
248 XtSetArg(args[n], XmNlabelString, s); n++;
250 w = create_widget(&item->w, parent, args, n);
255 construct_menu(root, w, item->w.subtree);
261 /* Figure out which tiles intersect a rectangle. */
263 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
264 int tile_w, int tile_h)
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);
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.
276 * When shrinking, this is all tiles, but when enlarging, the generated
277 * expose events will trigger drawing of the new area.
279 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
281 Dimension new_w, new_h, common_w, common_h;
283 if (!XtIsRealized(w))
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]);
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;
298 if (new_w > 0 && new_h > 0)
299 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
303 static void game_resize(Widget w, void *data, void *cb_data)
305 struct app_state *state = data;
308 mask = resize_helper(w, state->game_sz, 5);
310 x11_redraw_game(data, mask);
313 static void goal_resize(Widget w, void *data, void *cb_data)
315 struct app_state *state = data;
318 mask = resize_helper(w, state->goal_sz, 3);
320 x11_redraw_goal(data, mask);
323 static void game_expose(Widget w, void *data, void *cb_data)
325 XmDrawingAreaCallbackStruct *cbs = cb_data;
326 XExposeEvent *e = &cbs->event->xexpose;
327 Dimension width, height;
330 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
331 if (!(width /= 5) || !(height /= 5))
334 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
335 x11_redraw_game(data, mask);
338 static void goal_expose(Widget w, void *data, void *cb_data)
340 XmDrawingAreaCallbackStruct *cbs = cb_data;
341 XExposeEvent *e = &cbs->event->xexpose;
342 Dimension width, height;
345 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
346 if (!(width /= 3) || !(height /= 3))
349 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
350 x11_redraw_goal(data, mask);
353 void ui_initialize(struct app_state *state, Widget shell)
355 Widget menubar, help;
357 construct_widgets(mainwin, shell, 0);
359 menubar = XtNameToWidget(shell, "*menuBar");
360 construct_menu(mainmenu, menubar, 0);
362 help = XtNameToWidget(menubar, "helpMenu");
363 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
365 configure_mainwin(state, XtNameToWidget(shell, "*game"));
367 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
368 XmNheight, &state->game_sz[1],
370 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
371 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
373 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
374 XmNheight, &state->goal_sz[1],
376 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
377 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
380 static void dialog_close(Widget w, void *data, void *cb_data)
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
390 * Note that str is expanded twice and thus should not have side effects.
392 #define STRING_ARG(resource, str) \
393 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
395 void ui_show_about(struct app_state *state, Widget shell)
397 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
401 construct_widgets(dialog, shell, 0);
403 w = XtNameToWidget(shell, "*aboutDialog");
404 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
406 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
408 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
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),
418 l = XtNameToWidget(w, "*licenseBlurb");
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/>.");
431 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
433 XmNresizeWidth, TRUE,