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 /* XXX generate this list? */
40 widgetMenuBar = widgetMax,
47 static WidgetClass * const widgets[widgetMax] = {
48 &xmMainWindowWidgetClass,
51 &xmDrawingAreaWidgetClass,
52 &xmCascadeButtonWidgetClass,
53 &xmPushButtonWidgetClass
56 static const struct ui_widget {
58 uint_least8_t subtree;
59 uint_least8_t widget_type;
60 } mainwin[] = { MAINWIN_INITIALIZER };
62 static const struct ui_menuitem {
65 } mainmenu[] = { MAINMENU_INITIALIZER };
67 static char *timer_text(int_fast32_t ms, char *buf)
71 sec = ms / 1000, ms %= 1000;
72 min = sec / 60, sec %= 60;
73 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
78 void ui_timer_update(struct app_state *state, int_fast32_t ms)
83 xcounter_update(state->timer, "\n");
87 xcounter_update(state->timer, timer_text(ms, buf));
90 static void configure_mainwin(struct app_state *state, Widget form)
92 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
93 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
94 Widget timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
95 XtActionsRec resize_rec;
96 char xc_template[100];
98 assert(gamearea && goalarea && timer);
100 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
101 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
102 XmNtopAttachment, XmATTACH_FORM,
105 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
106 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
107 XmNleftWidget, gamearea,
108 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
109 XmNtopWidget, gamearea,
112 XtVaSetValues(timer, XmNleftAttachment, XmATTACH_WIDGET,
113 XmNleftWidget, gamearea,
114 XmNtopAttachment, XmATTACH_WIDGET,
115 XmNtopWidget, goalarea,
116 XmNrightAttachment, XmATTACH_FORM,
118 state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
119 ui_timer_update(state, -1);
121 XtOverrideTranslations(form, XtParseTranslationTable(
122 "<Configure>: ResizeGameArea()\n"
123 "<Map>: ResizeGameArea()\n"
127 * Performing the initial update of the layout seems to avoid
128 * some weird initial sizing problems on Motif 2.1
130 XtCallActionProc(form, "ResizeGameArea", 0, 0, 0);
133 static Widget create_widget(const struct ui_widget *item, Widget parent,
134 ArgList args, Cardinal num_args)
136 String name = (void *)&tree_strtab[item->name];
137 unsigned type = item->widget_type & widgetUnmanaged-1;
141 return XmCreateMenuBar(parent, name, args, num_args);
142 case widgetMessageDialog:
143 return XmCreateMessageDialog(parent, name, args, num_args);
144 case widgetScrolledText:
145 return XmCreateScrolledText(parent, name, args, num_args);
148 assert(type < widgetMax);
149 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
153 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
155 const struct ui_widget *item;
157 for (item = &root[i]; item->name; item++) {
158 Widget w = create_widget(item, parent, NULL, 0);
161 construct_widgets(root, w, item->subtree);
163 if (!(item->widget_type & widgetUnmanaged))
168 static void menu_cb(Widget w, void *data, void *cb_data)
170 XmRowColumnCallbackStruct *cbs = cb_data;
171 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
174 static Widget create_pulldown(Widget parent)
178 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
179 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
180 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
186 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
188 const struct ui_menuitem *item;
190 for (item = &root[i]; item->w.name; item++) {
191 const char *label = &strtab[item->label];
197 if (XtClass(parent) == *widgets[widgetCascadeButton])
198 parent = create_pulldown(parent);
200 if (label[0] && label[1] == '|') {
201 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
205 s = XmStringCreateLocalized((void *)label);
206 XtSetArg(args[n], XmNlabelString, s); n++;
208 w = create_widget(&item->w, parent, args, n);
213 construct_menu(root, w, item->w.subtree);
219 /* Figure out which tiles intersect a rectangle. */
220 static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_sz)
222 return board_rect( e->x/tile_sz,
224 (e->x+e->width-1)/tile_sz,
225 (e->y+e->height-1)/tile_sz );
228 static void resize(Widget w, void *data, void *cb_data)
230 struct app_state *state = data;
232 x11_queue_render(data, -1, 1 << (w == state->game));
235 static void expose(Widget w, void *data, void *cb_data)
237 XmDrawingAreaCallbackStruct *cbs = cb_data;
238 XExposeEvent *e = &cbs->event->xexpose;
239 struct app_state *state = data;
243 if (w == state->game) {
244 uint_least32_t *gp = state->board.game;
247 * Only draw exposed nonempty tiles; exposed areas are filled
248 * with the background automatically and thus exposed empty
249 * spaces don't need to be drawn again.
251 tile_sz = state->game_tile_sz;
252 mask = gp[0] | gp[1] | gp[2];
254 /* Goal area never has empty tiles. */
255 tile_sz = state->goal_tile_sz;
260 mask &= x11_expose_mask(e, tile_sz);
261 x11_queue_render(state, mask, 1<<(w == state->game));
264 void ui_initialize(struct app_state *state, Widget shell)
266 Widget menubar, help;
268 construct_widgets(mainwin, shell, 0);
270 menubar = XtNameToWidget(shell, "*menuBar");
271 construct_menu(mainmenu, menubar, 0);
273 help = XtNameToWidget(menubar, "helpMenu");
274 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
276 configure_mainwin(state, XtNameToWidget(shell, "*game"));
278 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
279 XmNheight, &state->game_sz[1],
281 XtAddCallback(state->game, XmNresizeCallback, resize, state);
282 XtAddCallback(state->game, XmNexposeCallback, expose, state);
284 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
285 XmNheight, &state->goal_sz[1],
287 XtAddCallback(state->goal, XmNresizeCallback, resize, state);
288 XtAddCallback(state->goal, XmNexposeCallback, expose, state);
291 static void dialog_close(Widget w, void *data, void *cb_data)
297 * Expands to an XtVaSetValues argument list to set the given resource with
298 * a typed string value, which is internally converted to the appropriate
301 * Note that str is expanded twice and thus should not have side effects.
303 #define STRING_ARG(resource, str) \
304 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
306 void ui_show_about(struct app_state *state, Widget shell)
308 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
312 construct_widgets(dialog, shell, 0);
314 w = XtNameToWidget(shell, "*aboutDialog");
315 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
317 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
319 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
321 msg = version_format_head("rrace-motif");
322 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
323 XtVaSetValues(l, XmNlabelType, XmSTRING,
324 #if HAVE_MOTIF_PIXMAP_AND_STRING
325 XmNlabelType, XmPIXMAP_AND_STRING,
327 XmNlabelPixmap, state->icon_pixmap,
328 STRING_ARG(XmNlabelString, msg),
332 l = XtNameToWidget(w, "*licenseBlurb");
334 "This program is free software: you can redistribute it and/or modify\n"
335 "it under the terms of the GNU General Public License as published by\n"
336 "the Free Software Foundation, either version 3 of the License, or\n"
337 "(at your option) any later version.\n\n"
338 "This program is distributed in the hope that it will be useful,\n"
339 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
340 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
341 "GNU General Public License for more details.\n\n"
342 "You should have received a copy of the GNU General Public License\n"
343 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
345 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
347 XmNresizeWidth, TRUE,