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
48 static WidgetClass * const widgets[widgetMax] = {
49 &xmMainWindowWidgetClass,
52 &xmDrawingAreaWidgetClass,
53 &xmCascadeButtonWidgetClass,
54 &xmPushButtonWidgetClass
57 static const struct ui_widget {
59 uint_least8_t subtree;
60 uint_least8_t widget_type;
61 } mainwin[] = { MAINWIN_INITIALIZER };
63 static const struct ui_menuitem {
66 } mainmenu[] = { MAINMENU_INITIALIZER };
69 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
71 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
72 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
73 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
76 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
77 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
78 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
82 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
83 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
85 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
86 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
88 x = (w - gamesz - goalsz - gap) / 2;
94 XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
95 XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
96 XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
99 static char *timer_text(int_fast32_t ms, char *buf)
103 sec = ms / 1000, ms %= 1000;
104 min = sec / 60, sec %= 60;
105 sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
110 void ui_timer_update(struct app_state *state, int_fast32_t ms)
115 xcounter_simple_update(state->timer, "\n");
119 xcounter_simple_update(state->timer, timer_text(ms, buf));
122 static void configure_mainwin(struct app_state *state, Widget form)
124 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
125 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
126 XtActionsRec resize_rec;
127 char xc_template[100];
129 assert(gamearea && goalarea);
130 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
132 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
133 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
134 XmNtopAttachment, XmATTACH_FORM,
137 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
138 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
139 XmNleftWidget, gamearea,
140 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
141 XmNtopWidget, gamearea,
144 state->timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
145 XtVaSetValues(state->timer, XmNleftAttachment, XmATTACH_WIDGET,
146 XmNleftWidget, gamearea,
147 XmNtopAttachment, XmATTACH_WIDGET,
148 XmNtopWidget, goalarea,
149 XmNrightAttachment, XmATTACH_FORM,
152 xcounter_simple_setup(state->timer, timer_text(20000, xc_template));
153 ui_timer_update(state, -1);
155 resize_rec.string = "ResizeGameArea";
156 resize_rec.proc = ResizeGameArea;
157 XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
158 XtOverrideTranslations(form, XtParseTranslationTable(
159 "<Configure>: ResizeGameArea()\n"
160 "<Map>: ResizeGameArea()\n"
164 * Performing the initial update of the layout seems to avoid
165 * some weird problems on Motif 2.1
167 ResizeGameArea(form, 0, 0, 0);
170 static Widget create_widget(const struct ui_widget *item, Widget parent,
171 ArgList args, Cardinal num_args)
173 String name = (void *)&tree_strtab[item->name];
176 if (item->widget_type == widgetMenuBar)
177 return XmCreateMenuBar(parent, name, args, num_args);
179 assert(item->widget_type < widgetMax);
180 class = *widgets[item->widget_type];
181 return XtCreateWidget(name, class, parent, args, num_args);
185 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
187 const struct ui_widget *item;
189 for (item = &root[i]; item->name; item++) {
190 Widget w = create_widget(item, parent, NULL, 0);
193 construct_widgets(root, w, item->subtree);
199 static void menu_cb(Widget w, void *data, void *cb_data)
201 XmRowColumnCallbackStruct *cbs = cb_data;
202 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
205 static Widget create_pulldown(Widget parent)
209 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
210 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
211 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
217 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
219 const struct ui_menuitem *item;
221 for (item = &root[i]; item->w.name; item++) {
222 const char *label = &strtab[item->label];
228 if (XtClass(parent) == *widgets[widgetCascadeButton])
229 parent = create_pulldown(parent);
231 if (label[0] && label[1] == '|') {
232 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
236 s = XmStringCreateLocalized((void *)label);
237 XtSetArg(args[n], XmNlabelString, s); n++;
239 w = create_widget(&item->w, parent, args, n);
244 construct_menu(root, w, item->w.subtree);
250 /* Figure out which tiles intersect a rectangle. */
252 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
253 int tile_w, int tile_h)
255 return board_right(rect_x / tile_w)
256 & board_below(rect_y / tile_h)
257 & board_above((rect_y + rect_h - 1) / tile_h)
258 & board_left((rect_x + rect_w - 1) / tile_w);
262 * Calculate which tiles need to be redrawn after resizing. This is the
263 * tiles which (at the new size) are fully contained in the previous area.
265 * When shrinking, this is all tiles, but when enlarging, the generated
266 * expose events will trigger drawing of the new area.
268 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
270 Dimension new_w, new_h, common_w, common_h;
272 if (!XtIsRealized(w))
275 XtVaGetValues(w, XmNwidth, &new_w, XmNheight, &new_h, (char *)NULL);
276 common_w = MIN(new_w, oldsz[0]);
277 common_h = MIN(new_h, oldsz[1]);
283 /* Round down to multiple of tile dimensions */
284 common_w = common_w / new_w * new_w;
285 common_h = common_h / new_h * new_h;
287 if (new_w > 0 && new_h > 0)
288 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
292 static void game_resize(Widget w, void *data, void *cb_data)
294 struct app_state *state = data;
297 mask = resize_helper(w, state->game_sz, 5);
299 x11_redraw_game(data, mask);
302 static void goal_resize(Widget w, void *data, void *cb_data)
304 struct app_state *state = data;
307 mask = resize_helper(w, state->goal_sz, 3);
309 x11_redraw_goal(data, mask);
312 static void game_expose(Widget w, void *data, void *cb_data)
314 XmDrawingAreaCallbackStruct *cbs = cb_data;
315 XExposeEvent *e = &cbs->event->xexpose;
316 Dimension width, height;
319 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
320 if (!(width /= 5) || !(height /= 5))
323 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
324 x11_redraw_game(data, mask);
327 static void goal_expose(Widget w, void *data, void *cb_data)
329 XmDrawingAreaCallbackStruct *cbs = cb_data;
330 XExposeEvent *e = &cbs->event->xexpose;
331 Dimension width, height;
334 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
335 if (!(width /= 3) || !(height /= 3))
338 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
339 x11_redraw_goal(data, mask);
342 void ui_initialize(struct app_state *state, Widget shell)
344 Widget menubar, help;
346 construct_widgets(mainwin, shell, 0);
348 menubar = XtNameToWidget(shell, "*menuBar");
349 construct_menu(mainmenu, menubar, 0);
351 help = XtNameToWidget(menubar, "helpMenu");
352 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
354 configure_mainwin(state, XtNameToWidget(shell, "*game"));
356 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
357 XmNheight, &state->game_sz[1],
359 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
360 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
362 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
363 XmNheight, &state->goal_sz[1],
365 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
366 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);