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 static void game_configure(Widget w);
35 /* XXX generate this list? */
46 widgetMenuBar = widgetMax
49 static WidgetClass * const widgets[widgetMax] = {
50 &xmMainWindowWidgetClass,
53 &xmDrawingAreaWidgetClass,
54 &xmCascadeButtonWidgetClass,
55 &xmPushButtonWidgetClass
58 static const struct ui_widget {
60 uint_least8_t subtree;
61 uint_least8_t widget_type;
62 } mainwin[] = { MAINWIN_INITIALIZER };
64 static const struct ui_menuitem {
67 } mainmenu[] = { MAINMENU_INITIALIZER };
70 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
72 Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
73 Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
74 Dimension w, h, gamesz, gameborder, goalsz, goalborder;
77 XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
78 XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
79 XtVaGetValues(goal, XmNshadowThickness, &goalborder,
83 gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
84 gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
86 goalsz = MIN(gamesz*3/5, w - gamesz - gap);
87 goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
89 x = (w - gamesz - goalsz - gap) / 2;
95 XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
96 XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
97 XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
100 static void game_configure(Widget form)
102 Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
103 Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
104 XtActionsRec resize_rec;
106 assert(gamearea && goalarea);
107 XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
109 XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
110 XmNtopAttachment, XmATTACH_FORM,
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];
135 if (item->widget_type == widgetMenuBar)
136 return XmCreateMenuBar(parent, name, args, num_args);
138 assert(item->widget_type < widgetMax);
139 class = *widgets[item->widget_type];
140 return XtCreateWidget(name, class, parent, args, num_args);
144 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
146 const struct ui_widget *item;
148 for (item = &root[i]; item->name; item++) {
149 Widget w = create_widget(item, parent, NULL, 0);
152 construct_widgets(root, w, item->subtree);
158 static void menu_cb(Widget w, void *data, void *cb_data)
160 XmRowColumnCallbackStruct *cbs = cb_data;
161 XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
164 static Widget create_pulldown(Widget parent)
168 w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
169 XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
170 XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
176 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
178 const struct ui_menuitem *item;
180 for (item = &root[i]; item->w.name; item++) {
181 const char *label = &strtab[item->label];
187 if (XtClass(parent) == *widgets[widgetCascadeButton])
188 parent = create_pulldown(parent);
190 if (label[0] && label[1] == '|') {
191 XtSetArg(args[n], XmNmnemonic, label[0]); n++;
195 s = XmStringCreateLocalized((void *)label);
196 XtSetArg(args[n], XmNlabelString, s); n++;
198 w = create_widget(&item->w, parent, args, n);
203 construct_menu(root, w, item->w.subtree);
209 static void game_resize(Widget w, void *data, void *cb_data)
212 x11_redraw_game(data);
215 static void goal_resize(Widget w, void *data, void *cb_data)
218 x11_redraw_goal(data);
221 static void game_input(Widget w, void *data, void *cb_data)
223 XmDrawingAreaCallbackStruct *cbs = cb_data;
224 XButtonEvent *click = &cbs->event->xbutton;
225 struct app_state *state = data;
226 Dimension width, height;
227 unsigned x = -1, y = -1;
229 switch (cbs->event->type) {
231 XtVaGetValues(w, XmNwidth, &width,
234 x = click->x / (width/5);
235 y = click->y / (width/5);
241 if (game_do_move(&state->board, x, y) == 0) {
242 if (game_check_goal(&state->board)) {
243 printf("You win!\n");
244 game_finish(&state->board);
247 x11_redraw_game(state);
251 void ui_initialize(struct app_state *state, Widget shell)
253 Widget menubar, help;
255 construct_widgets(mainwin, shell, 0);
257 menubar = XtNameToWidget(shell, "*menuBar");
258 construct_menu(mainmenu, menubar, 0);
260 help = XtNameToWidget(menubar, "helpMenu");
261 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
263 game_configure(XtNameToWidget(shell, "*game"));
265 state->game = XtNameToWidget(shell, "*gameCanvas");
266 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
267 XtAddCallback(state->game, XmNexposeCallback, game_resize, state);
268 XtAddCallback(state->game, XmNinputCallback, game_input, state);
270 state->goal = XtNameToWidget(shell, "*goalCanvas");
271 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
272 XtAddCallback(state->goal, XmNexposeCallback, goal_resize, state);