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/>.
31 #define tree_strtab strtab
33 /* XXX generate this list? */
44 widgetMenuBar = widgetMax,
51 static WidgetClass * const widgets[widgetMax] = {
52 &xmMainWindowWidgetClass,
55 &xmDrawingAreaWidgetClass,
56 &xmCascadeButtonWidgetClass,
57 &xmPushButtonWidgetClass
60 static const struct ui_widget {
62 uint_least8_t subtree;
63 uint_least8_t widget_type;
64 } mainwin[] = { MAINWIN_INITIALIZER };
66 static const struct ui_menuitem {
69 } mainmenu[] = { MAINMENU_INITIALIZER };
71 static char *timer_text(int_fast32_t ms, char *buf)
75 sec = ms / 1000, ms %= 1000;
76 min = sec / 60, sec %= 60;
77 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
82 void ui_timer_update(struct app_state *state, int_fast32_t ms)
87 xcounter_update(state->timer, "\n");
91 xcounter_update(state->timer, timer_text(ms, buf));
94 static void configure_mainwin(struct app_state *state, Widget form)
96 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
97 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
98 Widget timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
99 XtActionsRec resize_rec;
100 char xc_template[100];
102 assert(gamearea && goalarea && timer);
104 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
105 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
106 XmNtopAttachment, XmATTACH_FORM,
109 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
110 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
111 XmNleftWidget, gamearea,
112 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
113 XmNtopWidget, gamearea,
116 XtVaSetValues(timer, XmNleftAttachment, XmATTACH_WIDGET,
117 XmNleftWidget, gamearea,
118 XmNtopAttachment, XmATTACH_WIDGET,
119 XmNtopWidget, goalarea,
120 XmNrightAttachment, XmATTACH_FORM,
122 state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
123 ui_timer_update(state, -1);
125 XtOverrideTranslations(form, XtParseTranslationTable(
126 "<Configure>: ResizeGameArea()\n"
127 "<Map>: ResizeGameArea()\n"
131 * Performing the initial update of the layout seems to avoid
132 * some weird initial sizing problems on Motif 2.1
134 XtCallActionProc(form, "ResizeGameArea", 0, 0, 0);
137 static Widget create_widget(const struct ui_widget *item, Widget parent,
138 ArgList args, Cardinal num_args)
140 String name = (void *)&tree_strtab[item->name];
141 unsigned type = item->widget_type & widgetUnmanaged-1;
145 return XmCreateMenuBar(parent, name, args, num_args);
146 case widgetMessageDialog:
147 return XmCreateMessageDialog(parent, name, args, num_args);
148 case widgetScrolledText:
149 return XmCreateScrolledText(parent, name, args, num_args);
152 assert(type < widgetMax);
153 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
157 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
159 const struct ui_widget *item;
161 for (item = &root[i]; item->name; item++) {
162 Widget w = create_widget(item, parent, NULL, 0);
165 construct_widgets(root, w, item->subtree);
167 if (!(item->widget_type & widgetUnmanaged))
172 static void menu_cb(Widget w, void *data, void *cb_data)
174 XmRowColumnCallbackStruct *cbs = cb_data;
175 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
178 static Widget create_pulldown(Widget parent)
182 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
183 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
184 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
190 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
192 const struct ui_menuitem *item;
194 for (item = &root[i]; item->w.name; item++) {
195 const char *label = &strtab[item->label];
201 if (XtClass(parent) == *widgets[widgetCascadeButton])
202 parent = create_pulldown(parent);
204 if (label[0] && label[1] == '|') {
205 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
209 s = XmStringCreateLocalized((void *)label);
210 XtSetArg(args[n], XmNlabelString, s); n++;
212 w = create_widget(&item->w, parent, args, n);
217 construct_menu(root, w, item->w.subtree);
223 /* Figure out which tiles intersect a rectangle. */
224 static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_sz)
226 return board_rect( e->x/tile_sz,
228 (e->x+e->width-1)/tile_sz,
229 (e->y+e->height-1)/tile_sz );
232 static void resize(Widget w, void *data, void *cb_data)
234 struct app_state *state = data;
236 x11_queue_render(data, -1, 1 << (w == state->game));
239 static void expose(Widget w, void *data, void *cb_data)
241 XmDrawingAreaCallbackStruct *cbs = cb_data;
242 XExposeEvent *e = &cbs->event->xexpose;
243 struct app_state *state = data;
247 if (w == state->game) {
248 uint_least32_t *gp = state->board.game;
251 * Only draw exposed nonempty tiles; exposed areas are filled
252 * with the background automatically and thus exposed empty
253 * spaces don't need to be drawn again.
255 tile_sz = state->game_tile_sz;
256 mask = gp[0] | gp[1] | gp[2];
258 /* Goal area never has empty tiles. */
259 tile_sz = state->goal_tile_sz;
264 mask &= x11_expose_mask(e, tile_sz);
265 x11_queue_render(state, mask, 1<<(w == state->game));
268 void ui_initialize(struct app_state *state, Widget shell)
270 Widget menubar, help;
272 construct_widgets(mainwin, shell, 0);
274 menubar = XtNameToWidget(shell, &strtab[glob_menuBar]);
275 construct_menu(mainmenu, menubar, 0);
277 help = XtNameToWidget(menubar, tree_strtab+helpMenu);
278 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
280 configure_mainwin(state, XtNameToWidget(shell, &strtab[glob_game]));
282 XtAddCallback(state->game, XmNresizeCallback, resize, state);
283 XtAddCallback(state->game, XmNexposeCallback, expose, state);
284 XtAddCallback(state->goal, XmNresizeCallback, resize, state);
285 XtAddCallback(state->goal, XmNexposeCallback, expose, state);
288 static void dialog_close(Widget w, void *data, void *cb_data)
294 * Expands to an XtVaSetValues argument list to set the given resource with
295 * a typed string value, which is internally converted to the appropriate
298 * Note that str is expanded twice and thus should not have side effects.
300 #define STRING_ARG(resource, str) \
301 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
303 void ui_show_about(struct app_state *state, Widget shell)
305 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
309 construct_widgets(dialog, shell, 0);
311 w = XtNameToWidget(shell, &strtab[glob_aboutDialog]);
312 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
314 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
316 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
318 msg = version_format_head("rrace-motif");
319 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
320 XtVaSetValues(l, XmNlabelType, XmSTRING,
321 #if HAVE_MOTIF_PIXMAP_AND_STRING
322 XmNlabelType, XmPIXMAP_AND_STRING,
324 XmNlabelPixmap, state->icon_pixmap,
325 STRING_ARG(XmNlabelString, msg),
329 l = XtNameToWidget(w, &strtab[glob_licenseBlurb]);
331 "This program is free software: you can redistribute it and/or modify\n"
332 "it under the terms of the GNU General Public License as published by\n"
333 "the Free Software Foundation, either version 3 of the License, or\n"
334 "(at your option) any later version.\n\n"
335 "This program is distributed in the hope that it will be useful,\n"
336 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
337 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
338 "GNU General Public License for more details.\n\n"
339 "You should have received a copy of the GNU General Public License\n"
340 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
342 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
344 XmNresizeWidth, TRUE,