]> git.draconx.ca Git - rrace.git/blob - src/motif_ui.c
5941834c86ae8b7bcba3ef35290615e5a068ccc0
[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 "motifgui.h"
26 #include "xcounter.h"
27 #include "version.h"
28
29 /* XXX generate this list? */
30 enum {
31         widgetMainWindow,
32         widgetForm,
33         widgetFrame,
34         widgetDrawingArea,
35         widgetCascadeButton,
36         widgetPushButton,
37         widgetMax,
38
39         /* Pseudo-widgets */
40         widgetMenuBar = widgetMax,
41         widgetMessageDialog,
42         widgetScrolledText,
43
44         widgetUnmanaged = 128
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 char *timer_text(int_fast32_t ms, char *buf)
68 {
69         unsigned min, sec;
70
71         sec = ms / 1000, ms %= 1000;
72         min = sec / 60, sec %= 60;
73         sprintf(buf, "Time: %u:%.2u.%.3u", min, sec, (unsigned)ms);
74
75         return buf;
76 }
77
78 void ui_timer_update(struct app_state *state, int_fast32_t ms)
79 {
80         char buf[100];
81
82         if (ms < 0) {
83                 xcounter_update(state->timer, "\n");
84                 return;
85         }
86
87         xcounter_update(state->timer, timer_text(ms, buf));
88 }
89
90 static void configure_mainwin(struct app_state *state, Widget form)
91 {
92         Widget gamearea = XtNameToWidget(form, &tree_strtab[gameArea]);
93         Widget goalarea = XtNameToWidget(form, &tree_strtab[goalArea]);
94         Widget timer = XtNameToWidget(form, &tree_strtab[timeDisplay]);
95         XtActionsRec resize_rec;
96         char xc_template[100];
97
98         assert(gamearea && goalarea && timer);
99
100         state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
101         XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
102                                 XmNtopAttachment, XmATTACH_FORM,
103                                 (char *)NULL);
104
105         state->goal = XtNameToWidget(goalarea, &tree_strtab[goalCanvas]);
106         XtVaSetValues(goalarea, XmNleftAttachment, XmATTACH_WIDGET,
107                                 XmNleftWidget, gamearea,
108                                 XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
109                                 XmNtopWidget, gamearea,
110                                 (char *)NULL);
111
112         XtVaSetValues(timer, XmNleftAttachment, XmATTACH_WIDGET,
113                              XmNleftWidget, gamearea,
114                              XmNtopAttachment, XmATTACH_WIDGET,
115                              XmNtopWidget, goalarea,
116                              XmNrightAttachment, XmATTACH_FORM,
117                              (char *)NULL);
118         state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
119         ui_timer_update(state, -1);
120
121         XtOverrideTranslations(form, XtParseTranslationTable(
122                 "<Configure>: ResizeGameArea()\n"
123                 "<Map>: ResizeGameArea()\n"
124         ));
125
126         /*
127          * Performing the initial update of the layout seems to avoid
128          * some weird initial sizing problems on Motif 2.1
129          */
130         XtCallActionProc(form, "ResizeGameArea", 0, 0, 0);
131 }
132
133 static Widget create_widget(const struct ui_widget *item, Widget parent,
134                             ArgList args, Cardinal num_args)
135 {
136         String name = (void *)&tree_strtab[item->name];
137         unsigned type = item->widget_type & widgetUnmanaged-1;
138
139         switch (type) {
140         case widgetMenuBar:
141                 return XmCreateMenuBar(parent, name, args, num_args);
142         case widgetMessageDialog:
143                 return XmCreateMessageDialog(parent, name, args, num_args);
144         case widgetScrolledText:
145                 return XmCreateScrolledText(parent, name, args, num_args);
146         }
147
148         assert(type < widgetMax);
149         return XtCreateWidget(name, *widgets[type], parent, args, num_args);
150 }
151
152 static void
153 construct_widgets(const struct ui_widget *root, Widget parent, unsigned i)
154 {
155         const struct ui_widget *item;
156
157         for (item = &root[i]; item->name; item++) {
158                 Widget w = create_widget(item, parent, NULL, 0);
159
160                 if (item->subtree)
161                         construct_widgets(root, w, item->subtree);
162
163                 if (!(item->widget_type & widgetUnmanaged))
164                         XtManageChild(w);
165         }
166 }
167
168 static void menu_cb(Widget w, void *data, void *cb_data)
169 {
170         XmRowColumnCallbackStruct *cbs = cb_data;
171         XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0);
172 }
173
174 static Widget create_pulldown(Widget parent)
175 {
176         Widget w;
177
178         w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0);
179         XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL);
180         XtAddCallback(w, XmNentryCallback, menu_cb, NULL);
181
182         return w;
183 }
184
185 static void
186 construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
187 {
188         const struct ui_menuitem *item;
189
190         for (item = &root[i]; item->w.name; item++) {
191                 const char *label = &strtab[item->label];
192                 unsigned n = 0;
193                 Arg args[2];
194                 XmString s;
195                 Widget w;
196
197                 if (XtClass(parent) == *widgets[widgetCascadeButton])
198                         parent = create_pulldown(parent);
199
200                 if (label[0] && label[1] == '|') {
201                         XtSetArg(args[n], XmNmnemonic, label[0]); n++;
202                         label += 2;
203                 }
204
205                 s = XmStringCreateLocalized((void *)label);
206                 XtSetArg(args[n], XmNlabelString, s); n++;
207
208                 w = create_widget(&item->w, parent, args, n);
209
210                 XmStringFree(s);
211
212                 if (item->w.subtree)
213                         construct_menu(root, w, item->w.subtree);
214
215                 XtManageChild(w);
216         }
217 }
218
219 /* Figure out which tiles intersect a rectangle. */
220 static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_sz)
221 {
222         return board_rect( e->x/tile_sz,
223                            e->y/tile_sz,
224                           (e->x+e->width-1)/tile_sz,
225                           (e->y+e->height-1)/tile_sz );
226 }
227
228 static void resize(Widget w, void *data, void *cb_data)
229 {
230         struct app_state *state = data;
231
232         x11_queue_render(data, -1, 1 << (w == state->game));
233 }
234
235 static void expose(Widget w, void *data, void *cb_data)
236 {
237         XmDrawingAreaCallbackStruct *cbs = cb_data;
238         XExposeEvent *e = &cbs->event->xexpose;
239         struct app_state *state = data;
240         uint_fast32_t mask;
241         Dimension tile_sz;
242
243         if (w == state->game) {
244                 uint_least32_t *gp = state->board.game;
245
246                 /*
247                  * Only draw exposed nonempty tiles; exposed areas are filled
248                  * with the background automatically and thus exposed empty
249                  * spaces don't need to be drawn again.
250                  */
251                 tile_sz = state->game_tile_sz;
252                 mask = gp[0] | gp[1] | gp[2];
253         } else {
254                 /* Goal area never has empty tiles. */
255                 tile_sz = state->goal_tile_sz;
256                 mask = -1;
257         }
258
259         if (tile_sz)
260                 mask &= x11_expose_mask(e, tile_sz);
261         x11_queue_render(state, mask, 1<<(w == state->game));
262 }
263
264 void ui_initialize(struct app_state *state, Widget shell)
265 {
266         Widget menubar, help;
267
268         construct_widgets(mainwin, shell, 0);
269
270         menubar = XtNameToWidget(shell, "*menuBar");
271         construct_menu(mainmenu, menubar, 0);
272
273         help = XtNameToWidget(menubar, "helpMenu");
274         XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL);
275
276         configure_mainwin(state, XtNameToWidget(shell, "*game"));
277
278         XtVaGetValues(state->game, XmNwidth, &state->game_sz[0],
279                                    XmNheight, &state->game_sz[1],
280                                    (char *)NULL);
281         XtAddCallback(state->game, XmNresizeCallback, resize, state);
282         XtAddCallback(state->game, XmNexposeCallback, expose, state);
283
284         XtVaGetValues(state->game, XmNwidth, &state->goal_sz[0],
285                                    XmNheight, &state->goal_sz[1],
286                                    (char *)NULL);
287         XtAddCallback(state->goal, XmNresizeCallback, resize, state);
288         XtAddCallback(state->goal, XmNexposeCallback, expose, state);
289 }
290
291 static void dialog_close(Widget w, void *data, void *cb_data)
292 {
293         XtDestroyWidget(w);
294 }
295
296 /*
297  * Expands to an XtVaSetValues argument list to set the given resource with
298  * a typed string value, which is internally converted to the appropriate
299  * resource type.
300  *
301  * Note that str is expanded twice and thus should not have side effects.
302  */
303 #define STRING_ARG(resource, str) \
304         XtVaTypedArg, (resource), XtRString, (str), strlen((str))+1
305
306 void ui_show_about(struct app_state *state, Widget shell)
307 {
308         static const struct ui_widget dialog[] = { ABOUTDIALOG_INITIALIZER };
309         Widget w, l;
310         char *msg;
311
312         construct_widgets(dialog, shell, 0);
313
314         w = XtNameToWidget(shell, "*aboutDialog");
315         XtUnmanageChild(XmMessageBoxGetChild(w, XmDIALOG_CANCEL_BUTTON));
316
317         XtAddCallback(w, XmNunmapCallback, dialog_close, NULL);
318
319         XtVaSetValues(w, STRING_ARG(XmNokLabelString, "Close"), (char *)NULL);
320
321         msg = version_format_head("rrace-motif");
322         l = XmMessageBoxGetChild(w, XmDIALOG_MESSAGE_LABEL);
323         XtVaSetValues(l, XmNlabelType, XmSTRING,
324 #if HAVE_MOTIF_PIXMAP_AND_STRING
325                          XmNlabelType, XmPIXMAP_AND_STRING,
326 #endif
327                          XmNlabelPixmap, state->icon_pixmap,
328                          STRING_ARG(XmNlabelString, msg),
329                          (char *)NULL);
330         free(msg);
331
332         l = XtNameToWidget(w, "*licenseBlurb");
333         XmTextSetString(l,
334 "This program is free software: you can redistribute it and/or modify\n"
335 "it under the terms of the GNU General Public License as published by\n"
336 "the Free Software Foundation, either version 3 of the License, or\n"
337 "(at your option) any later version.\n\n"
338 "This program is distributed in the hope that it will be useful,\n"
339 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
340 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
341 "GNU General Public License for more details.\n\n"
342 "You should have received a copy of the GNU General Public License\n"
343 "along with this program.  If not, see <https://www.gnu.org/licenses/>.");
344
345         XtVaSetValues(l, XmNeditMode, XmMULTI_LINE_EDIT,
346                          XmNeditable, FALSE,
347                          XmNresizeWidth, TRUE,
348                          XmNrows, 5,
349                          (char *)NULL);
350
351         XtManageChild(w);
352 }