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_simple_update(state->timer, "\n");
123 xcounter_simple_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 XtActionsRec resize_rec;
131 char xc_template[100];
133 assert(gamearea && goalarea);
134 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
136 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
137 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
138 XmNtopAttachment, XmATTACH_FORM,
141 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
142 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
143 XmNleftWidget, gamearea,
144 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
145 XmNtopWidget, gamearea,
148 state->timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
149 XtVaSetValues(state->timer, XmNleftAttachment, XmATTACH_WIDGET,
150 XmNleftWidget, gamearea,
151 XmNtopAttachment, XmATTACH_WIDGET,
152 XmNtopWidget, goalarea,
153 XmNrightAttachment, XmATTACH_FORM,
156 xcounter_simple_setup(state->timer, timer_text(20000, xc_template));
157 ui_timer_update(state, -1);
159 resize_rec.string = "ResizeGameArea";
160 resize_rec.proc = ResizeGameArea;
161 XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
162 XtOverrideTranslations(form, XtParseTranslationTable(
163 "<Configure>: ResizeGameArea()\n"
164 "<Map>: ResizeGameArea()\n"
168 * Performing the initial update of the layout seems to avoid
169 * some weird problems on Motif 2.1
171 ResizeGameArea(form, 0, 0, 0);
174 static Widget create_widget(const struct ui_widget *item, Widget parent,
175 ArgList args, Cardinal num_args)
177 String name = (void *)&tree_strtab[item->name];
178 unsigned type = item->widget_type & widgetUnmanaged-1;
182 return XmCreateMenuBar(parent, name, args, num_args);
183 case widgetMessageDialog:
184 return XmCreateMessageDialog(parent, name, args, num_args);
185 case widgetScrolledText:
186 return XmCreateScrolledText(parent, name, args, num_args);
189 assert(type < widgetMax);
190 return XtCreateWidget(name, *widgets[type], parent, args, num_args);
194 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
196 const struct ui_widget *item;
198 for (item = &root[i]; item->name; item++) {
199 Widget w = create_widget(item, parent, NULL, 0);
202 construct_widgets(root, w, item->subtree);
204 if (!(item->widget_type & widgetUnmanaged))
209 static void menu_cb(Widget w, void *data, void *cb_data)
211 XmRowColumnCallbackStruct *cbs = cb_data;
212 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
215 static Widget create_pulldown(Widget parent)
219 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
220 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
221 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
227 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
229 const struct ui_menuitem *item;
231 for (item = &root[i]; item->w.name; item++) {
232 const char *label = &strtab[item->label];
238 if (XtClass(parent) == *widgets[widgetCascadeButton])
239 parent = create_pulldown(parent);
241 if (label[0] && label[1] == '|') {
242 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
246 s = XmStringCreateLocalized((void *)label);
247 XtSetArg(args[n], XmNlabelString, s); n++;
249 w = create_widget(&item->w, parent, args, n);
254 construct_menu(root, w, item->w.subtree);
260 /* Figure out which tiles intersect a rectangle. */
262 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
263 int tile_w, int tile_h)
265 return board_right(rect_x / tile_w)
266 & board_below(rect_y / tile_h)
267 & board_above((rect_y + rect_h - 1) / tile_h)
268 & board_left((rect_x + rect_w - 1) / tile_w);
272 * Calculate which tiles need to be redrawn after resizing. This is the
273 * tiles which (at the new size) are fully contained in the previous area.
275 * When shrinking, this is all tiles, but when enlarging, the generated
276 * expose events will trigger drawing of the new area.
278 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
280 Dimension new_w, new_h, common_w, common_h;
282 if (!XtIsRealized(w))
285 XtVaGetValues(w, XmNwidth, &new_w, XmNheight, &new_h, (char *)NULL);
286 common_w = MIN(new_w, oldsz[0]);
287 common_h = MIN(new_h, oldsz[1]);
293 /* Round down to multiple of tile dimensions */
294 common_w = common_w / new_w * new_w;
295 common_h = common_h / new_h * new_h;
297 if (new_w > 0 && new_h > 0)
298 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
302 static void game_resize(Widget w, void *data, void *cb_data)
304 struct app_state *state = data;
307 mask = resize_helper(w, state->game_sz, 5);
309 x11_redraw_game(data, mask);
312 static void goal_resize(Widget w, void *data, void *cb_data)
314 struct app_state *state = data;
317 mask = resize_helper(w, state->goal_sz, 3);
319 x11_redraw_goal(data, mask);
322 static void game_expose(Widget w, void *data, void *cb_data)
324 XmDrawingAreaCallbackStruct *cbs = cb_data;
325 XExposeEvent *e = &cbs->event->xexpose;
326 Dimension width, height;
329 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
330 if (!(width /= 5) || !(height /= 5))
333 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
334 x11_redraw_game(data, mask);
337 static void goal_expose(Widget w, void *data, void *cb_data)
339 XmDrawingAreaCallbackStruct *cbs = cb_data;
340 XExposeEvent *e = &cbs->event->xexpose;
341 Dimension width, height;
344 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
345 if (!(width /= 3) || !(height /= 3))
348 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
349 x11_redraw_goal(data, mask);
352 void ui_initialize(struct app_state *state, Widget shell)
354 Widget menubar, help;
356 construct_widgets(mainwin, shell, 0);
358 menubar = XtNameToWidget(shell, "*menuBar");
359 construct_menu(mainmenu, menubar, 0);
361 help = XtNameToWidget(menubar, "helpMenu");
362 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
364 configure_mainwin(state, XtNameToWidget(shell, "*game"));
366 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
367 XmNheight, &state->game_sz[1],
369 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
370 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
372 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
373 XmNheight, &state->goal_sz[1],
375 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
376 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
379 static void dialog_close(Widget w, void *data, void *cb_data)
385 * Expands to an XtVaSetValues argument list to set the given resource with
386 * a typed string value, which is internally converted to the appropriate
389 * Note that str is expanded twice and thus should not have side effects.
391 #define STRING_ARG(resource, str) \
392 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
394 void ui_show_about(struct app_state *state, Widget shell)
396 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
400 construct_widgets(dialog, shell, 0);
402 w = XtNameToWidget(shell, "*aboutDialog");
403 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
405 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
407 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
409 msg = version_format_head("rrace-motif");
410 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
411 XtVaSetValues(l, XmNlabelType, XmSTRING,
412 #if HAVE_MOTIF_PIXMAP_AND_STRING
413 XmNlabelType, XmPIXMAP_AND_STRING,
415 XmNlabelPixmap, state->icon_pixmap,
416 STRING_ARG(XmNlabelString, msg),
420 l = XtNameToWidget(w, "*licenseBlurb");
422 "This program is free software: you can redistribute it and/or modify\n"
423 "it under the terms of the GNU General Public License as published by\n"
424 "the Free Software Foundation, either version 3 of the License, or\n"
425 "(at your option) any later version.\n\n"
426 "This program is distributed in the hope that it will be useful,\n"
427 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
428 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
429 "GNU General Public License for more details.\n\n"
430 "You should have received a copy of the GNU General Public License\n"
431 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
433 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
435 XmNresizeWidth, TRUE,