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