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_rect( rect_x/tile_w,
267 (rect_x+rect_w-1)/tile_w,
268 (rect_y+rect_h-1)/tile_h );
271 static void game_resize(Widget w, void *data, void *cb_data)
273 x11_queue_render(data, -1, 0);
276 static void goal_resize(Widget w, void *data, void *cb_data)
278 x11_queue_render(data, 0, -1);
281 static void game_expose(Widget w, void *data, void *cb_data)
283 XmDrawingAreaCallbackStruct *cbs = cb_data;
284 XExposeEvent *e = &cbs->event->xexpose;
285 struct app_state *state = data;
286 uint_least32_t *gp = state->board.game;
287 Dimension width, height;
290 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
291 if (!(width /= 5) || !(height /= 5))
295 * Only draw exposed nonempty tiles; exposed areas are filled with the
296 * background automatically and thus exposed empty spaces don't need
299 mask = gp[0] | gp[1] | gp[2];
300 mask &= expose_mask(e->x, e->y, e->width, e->height, width, height);
301 x11_queue_render(state, mask, 0);
304 static void goal_expose(Widget w, void *data, void *cb_data)
306 XmDrawingAreaCallbackStruct *cbs = cb_data;
307 XExposeEvent *e = &cbs->event->xexpose;
308 Dimension width, height;
311 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
312 if (!(width /= 3) || !(height /= 3))
315 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
316 x11_queue_render(data, 0, mask);
319 void ui_initialize(struct app_state *state, Widget shell)
321 Widget menubar, help;
323 construct_widgets(mainwin, shell, 0);
325 menubar = XtNameToWidget(shell, "*menuBar");
326 construct_menu(mainmenu, menubar, 0);
328 help = XtNameToWidget(menubar, "helpMenu");
329 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
331 configure_mainwin(state, XtNameToWidget(shell, "*game"));
333 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
334 XmNheight, &state->game_sz[1],
336 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
337 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
339 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
340 XmNheight, &state->goal_sz[1],
342 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
343 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);
346 static void dialog_close(Widget w, void *data, void *cb_data)
352 * Expands to an XtVaSetValues argument list to set the given resource with
353 * a typed string value, which is internally converted to the appropriate
356 * Note that str is expanded twice and thus should not have side effects.
358 #define STRING_ARG(resource, str) \
359 XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
361 void ui_show_about(struct app_state *state, Widget shell)
363 static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
367 construct_widgets(dialog, shell, 0);
369 w = XtNameToWidget(shell, "*aboutDialog");
370 XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
372 XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
374 XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
376 msg = version_format_head("rrace-motif");
377 l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
378 XtVaSetValues(l, XmNlabelType, XmSTRING,
379 #if HAVE_MOTIF_PIXMAP_AND_STRING
380 XmNlabelType, XmPIXMAP_AND_STRING,
382 XmNlabelPixmap, state->icon_pixmap,
383 STRING_ARG(XmNlabelString, msg),
387 l = XtNameToWidget(w, "*licenseBlurb");
389 "This program is free software: you can redistribute it and/or modify\n"
390 "it under the terms of the GNU General Public License as published by\n"
391 "the Free Software Foundation, either version 3 of the License, or\n"
392 "(at your option) any later version.\n\n"
393 "This program is distributed in the hope that it will be useful,\n"
394 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
395 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
396 "GNU General Public License for more details.\n\n"
397 "You should have received a copy of the GNU General Public License\n"
398 "along with this program. If not, see <https://www.gnu.org/licenses/>.");
400 XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
402 XmNresizeWidth, TRUE,