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