2 * X11 GUI for slide puzzle game
3 * Copyright © 2022-2023 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 tree_strtab strtab
31 /* XXX generate this list? */
42 widgetMenuBar = widgetMax,
49 static WidgetClass * const widgets[widgetMax] = {
50 &xmMainWindowWidgetClass,
53 &xmDrawingAreaWidgetClass,
54 &xmCascadeButtonWidgetClass,
55 &xmPushButtonWidgetClass
58 static const struct ui_widget {
60 uint_least8_t subtree;
61 uint_least8_t widget_type;
62 } mainwin[] = { MAINWIN_INITIALIZER };
64 static const struct ui_menuitem {
67 } mainmenu[] = { MAINMENU_INITIALIZER };
69 static char *timer_text(int_fast32_t ms, char *buf)
73 sec = ms / 1000, ms %= 1000;
74 min = sec / 60, sec %= 60;
75 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
80 void ui_timer_update(struct app_state *state, int_fast32_t ms)
85 xcounter_update(state->timer, "\n");
89 xcounter_update(state->timer, timer_text(ms, buf));
92 static void configure_mainwin(struct app_state *state, Widget form)
94 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
95 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
96 Widget timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
97 XtActionsRec resize_rec;
98 char xc_template[100];
100 assert(gamearea && goalarea && timer);
102 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
103 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
104 XmNtopAttachment, XmATTACH_FORM,
107 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
108 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
109 XmNleftWidget, gamearea,
110 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
111 XmNtopWidget, gamearea,
114 XtVaSetValues(timer, XmNleftAttachment, XmATTACH_WIDGET,
115 XmNleftWidget, gamearea,
116 XmNtopAttachment, XmATTACH_WIDGET,
117 XmNtopWidget, goalarea,
118 XmNrightAttachment, XmATTACH_FORM,
120 state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
121 ui_timer_update(state, -1);
123 XtOverrideTranslations(form, XtParseTranslationTable(
124 "<Configure>: ResizeGameArea()\n"
125 "<Map>: ResizeGameArea()\n"
129 * Performing the initial update of the layout seems to avoid
130 * some weird initial sizing problems on Motif 2.1
132 XtCallActionProc(form, "ResizeGameArea", 0, 0, 0);
135 static Widget create_widget(const struct ui_widget *item, Widget parent,
136 ArgList args, Cardinal num_args)
138 String name = (void *)&tree_strtab[item->name];
139 unsigned type = item->widget_type & widgetUnmanaged-1;
143 return XmCreateMenuBar(parent, name, args, num_args);
144 case widgetMessageDialog:
145 return XmCreateMessageDialog(parent, name, args, num_args);
146 case widgetScrolledText:
147 return XmCreateScrolledText(parent, name, args, num_args);
150 assert(type < widgetMax);
151 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
155 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
157 const struct ui_widget *item;
159 for (item = &root[i]; item->name; item++) {
160 Widget w = create_widget(item, parent, NULL, 0);
163 construct_widgets(root, w, item->subtree);
165 if (!(item->widget_type & widgetUnmanaged))
170 static void menu_cb(Widget w, void *data, void *cb_data)
172 XmRowColumnCallbackStruct *cbs = cb_data;
173 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
176 static Widget create_pulldown(Widget parent)
180 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
181 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
182 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
188 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
190 const struct ui_menuitem *item;
192 for (item = &root[i]; item->w.name; item++) {
193 const char *label = &strtab[item->label];
199 if (XtClass(parent) == *widgets[widgetCascadeButton])
200 parent = create_pulldown(parent);
202 if (label[0] && label[1] == '|') {
203 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
207 s = XmStringCreateLocalized((void *)label);
208 XtSetArg(args[n], XmNlabelString, s); n++;
210 w = create_widget(&item->w, parent, args, n);
215 construct_menu(root, w, item->w.subtree);
221 /* Figure out which tiles intersect a rectangle. */
222 static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_sz)
224 return board_rect( e->x/tile_sz,
226 (e->x+e->width-1)/tile_sz,
227 (e->y+e->height-1)/tile_sz );
230 static void resize(Widget w, void *data, void *cb_data)
232 struct app_state *state = data;
234 x11_queue_render(data, -1, 1 << (w == state->game));
237 static void expose(Widget w, void *data, void *cb_data)
239 XmDrawingAreaCallbackStruct *cbs = cb_data;
240 XExposeEvent *e = &cbs->event->xexpose;
241 struct app_state *state = data;
245 if (w == state->game) {
246 uint_least32_t *gp = state->board.game;
249 * Only draw exposed nonempty tiles; exposed areas are filled
250 * with the background automatically and thus exposed empty
251 * spaces don't need to be drawn again.
253 tile_sz = state->game_tile_sz;
254 mask = gp[0] | gp[1] | gp[2];
256 /* Goal area never has empty tiles. */
257 tile_sz = state->goal_tile_sz;
262 mask &= x11_expose_mask(e, tile_sz);
263 x11_queue_render(state, mask, 1<<(w == state->game));
266 void ui_initialize(struct app_state *state, Widget shell)
268 Widget menubar, help;
270 construct_widgets(mainwin, shell, 0);
272 menubar = XtNameToWidget(shell, &strtab[glob_menuBar]);
273 construct_menu(mainmenu, menubar, 0);
275 help = XtNameToWidget(menubar, tree_strtab+helpMenu);
276 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
278 configure_mainwin(state, XtNameToWidget(shell, &strtab[glob_game]));
280 XtAddCallback(state->game, XmNresizeCallback, resize, state);
281 XtAddCallback(state->game, XmNexposeCallback, expose, state);
282 XtAddCallback(state->goal, XmNresizeCallback, resize, state);
283 XtAddCallback(state->goal, XmNexposeCallback, expose, state);
286 static void dialog_close(Widget w, void *data, void *cb_data)
292 * Expands to an XtVaSetValues argument list to set the given resource with
293 * a typed string value, which is internally converted to the appropriate
296 * Note that str is expanded twice and thus should not have side effects.
298 #define STRING_ARG(resource, str) \
299 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
301 void ui_show_about(struct app_state *state, Widget shell)
303 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
307 construct_widgets(dialog, shell, 0);
309 w = XtNameToWidget(shell, &strtab[glob_aboutDialog]);
310 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
312 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
314 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
316 msg = version_format_head("rrace-motif");
317 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
318 XtVaSetValues(l, XmNlabelType, XmSTRING,
319 #if HAVE_MOTIF_PIXMAP_AND_STRING
320 XmNlabelType, XmPIXMAP_AND_STRING,
322 XmNlabelPixmap, state->icon_pixmap,
323 STRING_ARG(XmNlabelString, msg),
327 l = XtNameToWidget(w, &strtab[glob_licenseBlurb]);
329 "This program is free software: you can redistribute it and/or modify\n"
330 "it under the terms of the GNU General Public License as published by\n"
331 "the Free Software Foundation, either version 3 of the License, or\n"
332 "(at your option) any later version.\n\n"
333 "This program is distributed in the hope that it will be useful,\n"
334 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
335 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
336 "GNU General Public License for more details.\n\n"
337 "You should have received a copy of the GNU General Public License\n"
338 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
340 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
342 XmNresizeWidth, TRUE,