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/>.
28 #define SPLIT_NUMERATOR 75
29 #define SPLIT_DENOMINATOR 100
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
33 /* XXX generate this list? */
44 widgetMenuBar = widgetMax
47 static WidgetClass * const widgets[widgetMax] = {
48 &xmMainWindowWidgetClass,
51 &xmDrawingAreaWidgetClass,
52 &xmCascadeButtonWidgetClass,
53 &xmPushButtonWidgetClass
56 static const struct ui_widget {
58 uint_least8_t subtree;
59 uint_least8_t widget_type;
60 } mainwin[] = { MAINWIN_INITIALIZER };
62 static const struct ui_menuitem {
65 } mainmenu[] = { MAINMENU_INITIALIZER };
68 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
70 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
71 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
72 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
75 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
76 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
77 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
81 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
82 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
84 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
85 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
87 x = (w - gamesz - goalsz - gap) / 2;
93 XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
94 XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
95 XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
98 static void configure_mainwin(struct app_state *state, Widget form)
100 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
101 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
102 XtActionsRec resize_rec;
104 assert(gamearea && goalarea);
105 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
107 state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
108 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
109 XmNtopAttachment, XmATTACH_FORM,
112 state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
113 XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
114 XmNleftWidget, gamearea,
115 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
116 XmNtopWidget, gamearea,
119 resize_rec.string = "ResizeGameArea";
120 resize_rec.proc = ResizeGameArea;
121 XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
122 XtOverrideTranslations(form, XtParseTranslationTable(
123 "<Configure>: ResizeGameArea()\n"
124 "<Map>: ResizeGameArea()\n"
128 static Widget create_widget(const struct ui_widget *item, Widget parent,
129 ArgList args, Cardinal num_args)
131 String name = (void *)&tree_strtab[item->name];
134 if (item->widget_type == widgetMenuBar)
135 return XmCreateMenuBar(parent, name, args, num_args);
137 assert(item->widget_type < widgetMax);
138 class = *widgets[item->widget_type];
139 return XtCreateWidget(name, class, parent, args, num_args);
143 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
145 const struct ui_widget *item;
147 for (item = &root[i]; item->name; item++) {
148 Widget w = create_widget(item, parent, NULL, 0);
151 construct_widgets(root, w, item->subtree);
157 static void menu_cb(Widget w, void *data, void *cb_data)
159 XmRowColumnCallbackStruct *cbs = cb_data;
160 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
163 static Widget create_pulldown(Widget parent)
167 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
168 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
169 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
175 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
177 const struct ui_menuitem *item;
179 for (item = &root[i]; item->w.name; item++) {
180 const char *label = &strtab[item->label];
186 if (XtClass(parent) == *widgets[widgetCascadeButton])
187 parent = create_pulldown(parent);
189 if (label[0] && label[1] == '|') {
190 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
194 s = XmStringCreateLocalized((void *)label);
195 XtSetArg(args[n], XmNlabelString, s); n++;
197 w = create_widget(&item->w, parent, args, n);
202 construct_menu(root, w, item->w.subtree);
208 /* Figure out which tiles intersect a rectangle. */
210 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
211 int tile_w, int tile_h)
213 return board_right(rect_x / tile_w)
214 & board_below(rect_y / tile_h)
215 & board_above((rect_y + rect_h - 1) / tile_h)
216 & board_left((rect_x + rect_w - 1) / tile_w);
220 * Calculate which tiles need to be redrawn after resizing. This is the
221 * tiles which (at the new size) are fully contained in the previous area.
223 * When shrinking, this is all tiles, but when enlarging, the generated
224 * expose events will trigger drawing of the new area.
226 static uint_fast32_t resize_helper(Widget w, Dimension *oldsz, int gridsz)
228 Dimension new_w, new_h, common_w, common_h;
230 if (!XtIsRealized(w))
233 XtVaGetValues(w, XmNwidth, &new_w, XmNheight, &new_h, (char *)NULL);
234 common_w = MIN(new_w, oldsz[0]);
235 common_h = MIN(new_h, oldsz[1]);
241 /* Round down to multiple of tile dimensions */
242 common_w = common_w / new_w * new_w;
243 common_h = common_h / new_h * new_h;
245 if (new_w > 0 && new_h > 0)
246 return expose_mask(0, 0, common_w, common_h, new_w, new_h);
250 static void game_resize(Widget w, void *data, void *cb_data)
252 struct app_state *state = data;
255 mask = resize_helper(w, state->game_sz, 5);
257 x11_redraw_game(data, mask);
260 static void goal_resize(Widget w, void *data, void *cb_data)
262 struct app_state *state = data;
265 mask = resize_helper(w, state->goal_sz, 3);
267 x11_redraw_goal(data, mask);
270 static void game_expose(Widget w, void *data, void *cb_data)
272 XmDrawingAreaCallbackStruct *cbs = cb_data;
273 XExposeEvent *e = &cbs->event->xexpose;
274 Dimension width, height;
277 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
278 if (!(width /= 5) || !(height /= 5))
281 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
282 x11_redraw_game(data, mask);
285 static void goal_expose(Widget w, void *data, void *cb_data)
287 XmDrawingAreaCallbackStruct *cbs = cb_data;
288 XExposeEvent *e = &cbs->event->xexpose;
289 Dimension width, height;
292 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
293 if (!(width /= 3) || !(height /= 3))
296 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
297 x11_redraw_goal(data, mask);
300 void ui_initialize(struct app_state *state, Widget shell)
302 Widget menubar, help;
304 construct_widgets(mainwin, shell, 0);
306 menubar = XtNameToWidget(shell, "*menuBar");
307 construct_menu(mainmenu, menubar, 0);
309 help = XtNameToWidget(menubar, "helpMenu");
310 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
312 configure_mainwin(state, XtNameToWidget(shell, "*game"));
314 XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
315 XmNheight, &state->game_sz[1],
317 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
318 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
320 XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
321 XmNheight, &state->goal_sz[1],
323 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
324 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);