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