]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
Restructure motif code a bit.
[rrace.git] / src / motif_ui.c
1 /*
2  * X11 GUI for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <inttypes.h>
21 #include <assert.h>
22 #include <Xm/XmAll.h>
23
24 #include "motif.h"
25 #include "motifstr.h"
26 #include "motifgui.h"
27
28 #define SPLIT_NUMERATOR    75
29 #define SPLIT_DENOMINATOR 100
30
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32
33 /* XXX generate this list? */
34 enum {
35         widgetMainWindow,
36         widgetForm,
37         widgetFrame,
38         widgetDrawingArea,
39         widgetCascadeButton,
40         widgetPushButton,
41         widgetMax,
42
43         /* Pseudo-widgets */
44         widgetMenuBar = widgetMax
45 };
46
47 static WidgetClass * const widgets[widgetMax] = {
48         &xmMainWindowWidgetClass,
49         &xmFormWidgetClass,
50         &xmFrameWidgetClass,
51         &xmDrawingAreaWidgetClass,
52         &xmCascadeButtonWidgetClass,
53         &xmPushButtonWidgetClass
54 };
55
56 static const struct ui_widget {
57         uint_least16_t name;
58         uint_least8_t subtree;
59         uint_least8_t widget_type;
60 } mainwin[] = { MAINWIN_INITIALIZER };
61
62 static const struct ui_menuitem {
63         struct ui_widget w;
64         uint_least16_t label;
65 } mainmenu[] = { MAINMENU_INITIALIZER };
66
67 static void
68 ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
69 {
70         Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
71         Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
72         Dimension w, h, gamesz, gameborder, goalsz, goalborder;
73         int x, y, gap;
74
75         XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
76         XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
77         XtVaGetValues(goal, XmNshadowThickness, &goalborder,
78                             XmNleftOffset, &gap,
79                             (char *)NULL);
80
81         gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
82         gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
83
84         goalsz = MIN(gamesz*3/5, w - gamesz - gap);
85         goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
86
87         x = (w - gamesz - goalsz - gap) / 2;
88         if (x < 2) x = 0;
89
90         y = (h - gamesz) / 2;
91         if (y < 3) y = 0;
92
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);
96 }
97
98 static void configure_mainwin(struct app_state *state, Widget form)
99 {
100         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
101         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
102         XtActionsRec resize_rec;
103
104         assert(gamearea && goalarea);
105         XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
106
107         state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
108         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
109                                 XmNtopAttachment, XmATTACH_FORM,
110                                 (char *)NULL);
111
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,
117                                 (char *)NULL);
118
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"
125         ));
126 }
127
128 static Widget create_widget(const struct ui_widget *item, Widget parent,
129                             ArgList args, Cardinal num_args)
130 {
131         String name = (void *)&tree_strtab[item->name];
132         WidgetClass class;
133
134         if (item->widget_type == widgetMenuBar)
135                 return XmCreateMenuBar(parent, name, args, num_args);
136
137         assert(item->widget_type < widgetMax);
138         class = *widgets[item->widget_type];
139         return XtCreateWidget(name, class, parent, args, num_args);
140 }
141
142 static void
143 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
144 {
145         const struct ui_widget *item;
146
147         for (item = &root[i]; item->name; item++) {
148                 Widget w = create_widget(item, parent, NULL, 0);
149
150                 if (item->subtree)
151                         construct_widgets(root, w, item->subtree);
152
153                 XtManageChild(w);
154         }
155 }
156
157 static void menu_cb(Widget w, void *data, void *cb_data)
158 {
159         XmRowColumnCallbackStruct *cbs = cb_data;
160         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
161 }
162
163 static Widget create_pulldown(Widget parent)
164 {
165         Widget w;
166
167         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
168         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
169         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
170
171         return w;
172 }
173
174 static void
175 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
176 {
177         const struct ui_menuitem *item;
178
179         for (item = &root[i]; item->w.name; item++) {
180                 const char *label = &strtab[item->label];
181                 unsigned n = 0;
182                 Arg args[2];
183                 XmString s;
184                 Widget w;
185
186                 if (XtClass(parent) == *widgets[widgetCascadeButton])
187                         parent = create_pulldown(parent);
188
189                 if (label[0] && label[1] == '|') {
190                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
191                         label += 2;
192                 }
193
194                 s = XmStringCreateLocalized((void *)label);
195                 XtSetArg(args[n], XmNlabelString, s); n++;
196
197                 w = create_widget(&item->w, parent, args, n);
198
199                 XmStringFree(s);
200
201                 if (item->w.subtree)
202                         construct_menu(root, w, item->w.subtree);
203
204                 XtManageChild(w);
205         }
206 }
207
208 static void game_resize(Widget w, void *data, void *cb_data)
209 {
210         if (XtIsRealized(w))
211                 x11_redraw_game(data, -1);
212 }
213
214 static void goal_resize(Widget w, void *data, void *cb_data)
215 {
216         if (XtIsRealized(w))
217                 x11_redraw_goal(data, -1);
218 }
219
220 /* Figure out which tiles intersect a rectangle. */
221 static uint_fast32_t
222 expose_mask(int rect_x, int rect_y, int rect_w, int rect_h,
223                                     int tile_w, int tile_h)
224 {
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);
229 }
230
231 static void game_expose(Widget w, void *data, void *cb_data)
232 {
233         XmDrawingAreaCallbackStruct *cbs = cb_data;
234         XExposeEvent *e = &cbs->event->xexpose;
235         Dimension width, height;
236         uint_fast32_t mask;
237
238         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
239         if (!(width /= 5) || !(height /= 5))
240                 return;
241
242         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
243         x11_redraw_game(data, mask);
244 }
245
246 static void goal_expose(Widget w, void *data, void *cb_data)
247 {
248         XmDrawingAreaCallbackStruct *cbs = cb_data;
249         XExposeEvent *e = &cbs->event->xexpose;
250         Dimension width, height;
251         uint_fast32_t mask;
252
253         XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, (char *)NULL);
254         if (!(width /= 3) || !(height /= 3))
255                 return;
256
257         mask = expose_mask(e->x, e->y, e->width, e->height, width, height);
258         x11_redraw_goal(data, mask);
259 }
260
261 void ui_initialize(struct app_state *state, Widget shell)
262 {
263         Widget menubar, help;
264
265         construct_widgets(mainwin, shell, 0);
266
267         menubar = XtNameToWidget(shell, "*menuBar");
268         construct_menu(mainmenu, menubar, 0);
269
270         help = XtNameToWidget(menubar, "helpMenu");
271         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
272
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);
278 }