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 static void game_resize(Widget w, void *data, void *cb_data)
211 x11_redraw_game(data, -1);
214 static void goal_resize(Widget w, void *data, void *cb_data)
217 x11_redraw_goal(data, -1);
220 /* Figure out which tiles intersect a rectangle. */
222 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
223 int tile_w, int tile_h)
225 return board_right(rect_x / tile_w)
226 & board_below(rect_y / tile_h)
227 & board_above((rect_y + rect_h - 1) / tile_h)
228 & board_left((rect_x + rect_w - 1) / tile_w);
231 static void game_expose(Widget w, void *data, void *cb_data)
233 XmDrawingAreaCallbackStruct *cbs = cb_data;
234 XExposeEvent *e = &cbs->event->xexpose;
235 Dimension width, height;
238 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
239 if (!(width /= 5) || !(height /= 5))
242 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
243 x11_redraw_game(data, mask);
246 static void goal_expose(Widget w, void *data, void *cb_data)
248 XmDrawingAreaCallbackStruct *cbs = cb_data;
249 XExposeEvent *e = &cbs->event->xexpose;
250 Dimension width, height;
253 XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
254 if (!(width /= 3) || !(height /= 3))
257 mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
258 x11_redraw_goal(data, mask);
261 void ui_initialize(struct app_state *state, Widget shell)
263 Widget menubar, help;
265 construct_widgets(mainwin, shell, 0);
267 menubar = XtNameToWidget(shell, "*menuBar");
268 construct_menu(mainmenu, menubar, 0);
270 help = XtNameToWidget(menubar, "helpMenu");
271 XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
273 configure_mainwin(state, XtNameToWidget(shell, "*game"));
274 XtAddCallback(state->game, XmNresizeCallback, game_resize, state);
275 XtAddCallback(state->game, XmNexposeCallback, game_expose, state);
276 XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state);
277 XtAddCallback(state->goal, XmNexposeCallback, goal_expose, state);