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/>.
29 #define SPLIT_NUMERATOR 75
30 #define SPLIT_DENOMINATOR 100
32 #define MIN(a, b) ((a) < (b) ? (a) : (b))
34 /* XXX generate this list? */
45 widgetMenuBar = widgetMax,
52 static WidgetClass * const widgets[widgetMax] = {
53 &xmMainWindowWidgetClass,
56 &xmDrawingAreaWidgetClass,
57 &xmCascadeButtonWidgetClass,
58 &xmPushButtonWidgetClass
61 static const struct ui_widget {
63 uint_least8_t subtree;
64 uint_least8_t widget_type;
65 } mainwin[] = { MAINWIN_INITIALIZER };
67 static const struct ui_menuitem {
70 } mainmenu[] = { MAINMENU_INITIALIZER };
73 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
75 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
76 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
77 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
80 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
81 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
82 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
86 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
87 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
89 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
90 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
92 x = (w - gamesz - goalsz - gap) / 2;
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);
103 static char *timer_text(int_fast32_t ms, char *buf)
107 sec = ms / 1000, ms %= 1000;
108 min = sec / 60, sec %= 60;
109 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
114 void ui_timer_update(struct app_state *state, int_fast32_t ms)
119 xcounter_update(state->timer, "\n");
123 xcounter_update(state->timer, timer_text(ms, buf));
126 static void configure_mainwin(struct app_state *state, Widget form)
128 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
129 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
130 Widget timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
131 XtActionsRec resize_rec;
132 char xc_template[100];
134 assert(gamearea && goalarea && timer);
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 XtVaSetValues(timer, XmNleftAttachment, XmATTACH_WIDGET,
150 XmNleftWidget, gamearea,
151 XmNtopAttachment, XmATTACH_WIDGET,
152 XmNtopWidget, goalarea,
153 XmNrightAttachment, XmATTACH_FORM,
155 state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
156 ui_timer_update(state, -1);
158 resize_rec.string = "ResizeGameArea";
159 resize_rec.proc = ResizeGameArea;
160 XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
161 XtOverrideTranslations(form, XtParseTranslationTable(
162 "<Configure>: ResizeGameArea()\n"
163 "<Map>: ResizeGameArea()\n"
167 * Performing the initial update of the layout seems to avoid
168 * some weird problems on Motif 2.1
170 ResizeGameArea(form, 0, 0, 0);
173 static Widget create_widget(const struct ui_widget *item, Widget parent,
174 ArgList args, Cardinal num_args)
176 String name = (void *)&tree_strtab[item->name];
177 unsigned type = item->widget_type & widgetUnmanaged-1;
181 return XmCreateMenuBar(parent, name, args, num_args);
182 case widgetMessageDialog:
183 return XmCreateMessageDialog(parent, name, args, num_args);
184 case widgetScrolledText:
185 return XmCreateScrolledText(parent, name, args, num_args);
188 assert(type < widgetMax);
189 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
193 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
195 const struct ui_widget *item;
197 for (item = &root[i]; item->name; item++) {
198 Widget w = create_widget(item, parent, NULL, 0);
201 construct_widgets(root, w, item->subtree);
203 if (!(item->widget_type & widgetUnmanaged))
208 static void menu_cb(Widget w, void *data, void *cb_data)
210 XmRowColumnCallbackStruct *cbs = cb_data;
211 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
214 static Widget create_pulldown(Widget parent)
218 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
219 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
220 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
226 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
228 const struct ui_menuitem *item;
230 for (item = &root[i]; item->w.name; item++) {
231 const char *label = &strtab[item->label];
237 if (XtClass(parent) == *widgets[widgetCascadeButton])
238 parent = create_pulldown(parent);
240 if (label[0] && label[1] == '|') {
241 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
245 s = XmStringCreateLocalized((void *)label);
246 XtSetArg(args[n], XmNlabelString, s); n++;
248 w = create_widget(&item->w, parent, args, n);
253 construct_menu(root, w, item->w.subtree);
259 /* Figure out which tiles intersect a rectangle. */
260 static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_w, int tile_h)
262 return board_rect( e->x/tile_w,
264 (e->x+e->width-1)/tile_w,
265 (e->y+e->height-1)/tile_h );
268 static void resize(Widget w, void *data, void *cb_data)
270 struct app_state *state = data;
272 x11_queue_render(data, -1, 1 << (w == state->game));
275 static void expose(Widget w, void *data, void *cb_data)
277 XmDrawingAreaCallbackStruct *cbs = cb_data;
278 XExposeEvent *e = &cbs->event->xexpose;
279 struct app_state *state = data;
280 Dimension tile_w, tile_h;
283 XtVaGetValues(w, XmNwidth, &tile_w, XmNheight, &tile_h, (char *)NULL);
284 if (w == state->game) {
285 uint_least32_t *gp = state->board.game;
287 if (!(tile_w /= 5) || !(tile_h /= 5))
291 * Only draw exposed nonempty tiles; exposed areas are filled
292 * with the background automatically and thus exposed empty
293 * spaces don't need to be drawn again.
295 mask = gp[0] | gp[1] | gp[2];
297 if (!(tile_w /= 3) || !(tile_h /= 3))
300 /* Goal area never has empty tiles. */
304 mask &= x11_expose_mask(e, tile_w, tile_h);
305 x11_queue_render(state, mask, 1<<(w == state->game));
308 void ui_initialize(struct app_state *state, Widget shell)
310 Widget menubar, help;
312 construct_widgets(mainwin, shell, 0);
314 menubar = XtNameToWidget(shell, "*menuBar");
315 construct_menu(mainmenu, menubar, 0);
317 help = XtNameToWidget(menubar, "helpMenu");
318 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
320 configure_mainwin(state, XtNameToWidget(shell, "*game"));
322 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
323 XmNheight, &state->game_sz[1],
325 XtAddCallback(state->game, XmNresizeCallback, resize, state);
326 XtAddCallback(state->game, XmNexposeCallback, expose, state);
328 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
329 XmNheight, &state->goal_sz[1],
331 XtAddCallback(state->goal, XmNresizeCallback, resize, state);
332 XtAddCallback(state->goal, XmNexposeCallback, expose, state);
335 static void dialog_close(Widget w, void *data, void *cb_data)
341 * Expands to an XtVaSetValues argument list to set the given resource with
342 * a typed string value, which is internally converted to the appropriate
345 * Note that str is expanded twice and thus should not have side effects.
347 #define STRING_ARG(resource, str) \
348 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
350 void ui_show_about(struct app_state *state, Widget shell)
352 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
356 construct_widgets(dialog, shell, 0);
358 w = XtNameToWidget(shell, "*aboutDialog");
359 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
361 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
363 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
365 msg = version_format_head("rrace-motif");
366 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
367 XtVaSetValues(l, XmNlabelType, XmSTRING,
368 #if HAVE_MOTIF_PIXMAP_AND_STRING
369 XmNlabelType, XmPIXMAP_AND_STRING,
371 XmNlabelPixmap, state->icon_pixmap,
372 STRING_ARG(XmNlabelString, msg),
376 l = XtNameToWidget(w, "*licenseBlurb");
378 "This program is free software: you can redistribute it and/or modify\n"
379 "it under the terms of the GNU General Public License as published by\n"
380 "the Free Software Foundation, either version 3 of the License, or\n"
381 "(at your option) any later version.\n\n"
382 "This program is distributed in the hope that it will be useful,\n"
383 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
384 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
385 "GNU General Public License for more details.\n\n"
386 "You should have received a copy of the GNU General Public License\n"
387 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
389 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
391 XmNresizeWidth, TRUE,