From 366b1a5719699e7b6daf2c4ebafbebab78cb5128 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 5 Mar 2022 02:56:20 -0500 Subject: [PATCH] Add a menu bar. Including a "new game" option to reset the board. --- Makefile.am | 14 +++- src/.gitignore | 3 +- src/motif.c | 40 ++++++++-- src/motif_ui.c | 124 ++++++++++++++++++++++++++---- src/motifgui.dat | 20 +++-- src/{options.opt => motifopt.opt} | 0 src/motifstr.str | 5 ++ 7 files changed, 173 insertions(+), 33 deletions(-) rename src/{options.opt => motifopt.opt} (100%) create mode 100644 src/motifstr.str diff --git a/Makefile.am b/Makefile.am index b0a7b75..c9c2816 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,18 +23,18 @@ rrace_motif_LDADD = $(libmotifmain_a_OBJECTS) $(libmotifui_a_OBJECTS) \ EXTRA_LIBRARIES += libmotifmain.a libmotifmain_a_SOURCES = src/motif.c -$(libmotifmain_a_OBJECTS): src/options.h +$(libmotifmain_a_OBJECTS): src/motifopt.h EXTRA_LIBRARIES += libmotifui.a libmotifui_a_SOURCES = src/motif_ui.c -$(libmotifui_a_OBJECTS): src/motifgui.h +$(libmotifui_a_OBJECTS): src/motifgui.h src/motifstr.h EXTRA_LIBRARIES += libglohelp.a libglohelp_a_SOURCES = common/src/help.c libglohelp_a_CFLAGS = -DHELP_GETOPT_LONG_ONLY libglohelp_a_SHORTNAME = glo -OPTFILES = src/options.opt +OPTFILES = src/motifopt.opt .opt.h: $(AM_V_GEN) $(AWK) -f $(DX_BASEDIR)/scripts/gen-options.awk $< >$@.tmp $(AM_V_at) mv -f $@.tmp $@ @@ -42,6 +42,14 @@ $(OPTFILES:.opt=.h): $(DX_BASEDIR)/scripts/gen-options.awk DISTCLEANFILES += $(OPTFILES:.opt=.h) EXTRA_DIST += $(DX_BASEDIR)/scripts/gen-options.awk $(OPTFILES) +STRFILES = src/motifstr.str +.str.h: + $(AM_V_GEN) $(AWK) -f $(DX_BASEDIR)/scripts/gen-strtab.awk $< >$@.tmp + $(AM_V_at) mv -f $@.tmp $@ +$(STRFILES:.str=.h): $(DX_BASEDIR)/scripts/gen-strtab.awk +DISTCLEANFILES += $(STRFILES:.str=.h) +EXTRA_DIST += $(DX_BASEDIR)/scripts/gen-strtab.awk $(GUIFILES) + GUIFILES = src/motifgui.dat .dat.h: $(AM_V_GEN) $(AWK) -f $(DX_BASEDIR)/scripts/gen-tree.awk $< >$@.tmp diff --git a/src/.gitignore b/src/.gitignore index 7537698..97c21ad 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,2 +1,3 @@ /motifgui.h -/options.h +/motifopt.h +/motifstr.h diff --git a/src/motif.c b/src/motif.c index 9ae4100..f9366f7 100644 --- a/src/motif.c +++ b/src/motif.c @@ -25,7 +25,7 @@ #include "help.h" #include "motif.h" -#include "options.h" +#include "motifopt.h" #include "game.h" #define PROGNAME "rrace" @@ -33,13 +33,18 @@ static const char *progname = PROGNAME; static const struct option lopts[] = { LOPTS_INITIALIZER, {0} }; static char * const default_resources[] = { - PROGNAME "*game.height: 371", // 365 + 2*shadowThickness - PROGNAME "*game.width: 498", + "*game.height: 371", // 365 + 2*shadowThickness + "*game.width: 498", - PROGNAME "*game.XmFrame.shadowThickness: 3", - PROGNAME "*game.XmFrame.shadowType: shadow_in", - PROGNAME "*goalArea.leftOffset: 1", - PROGNAME "*gameCanvas.background: #202020", + "*game.XmFrame.shadowThickness: 3", + "*game.XmFrame.shadowType: shadow_in", + "*goalArea.leftOffset: 1", + "*gameCanvas.background: #202020", + + "*gameNew.accelerator: CtrlN", + "*gameNew.acceleratorText: Ctrl+N", + "*gameExit.accelerator: CtrlQ", + "*gameExit.acceleratorText: Ctrl+Q", NULL }; @@ -122,9 +127,27 @@ static Widget early_setup(XtAppContext *app, int argc, char **argv) return shell; } +static struct app_state state; + +static void proc_exit(Widget w, XEvent *e, String *argv, Cardinal *argc) +{ + XtAppSetExitFlag(XtWidgetToApplicationContext(w)); +} + +static void proc_new_game(Widget w, XEvent *e, String *argv, Cardinal *argc) +{ + game_reset(&state.board); + x11_redraw_goal(&state); + x11_redraw_game(&state); +} + +static const XtActionsRec menu_actions[] = { + { "gameNew", proc_new_game }, + { "gameExit", proc_exit } +}; + static XtAppContext app_initialize(int argc, char **argv) { - static struct app_state state; XtAppContext app; Widget shell; @@ -132,6 +155,7 @@ static XtAppContext app_initialize(int argc, char **argv) progname = argv[0]; shell = early_setup(&app, argc, argv); + XtAppAddActions(app, (void *)menu_actions, XtNumber(menu_actions)); ui_initialize(&state, shell); x11_initialize(&state, XtScreen(shell)); game_reset(&state.board); diff --git a/src/motif_ui.c b/src/motif_ui.c index fbef33b..40f4632 100644 --- a/src/motif_ui.c +++ b/src/motif_ui.c @@ -22,6 +22,7 @@ #include #include "motif.h" +#include "motifstr.h" #include "motifgui.h" #define SPLIT_NUMERATOR 75 @@ -31,13 +32,40 @@ static void game_configure(Widget w); +/* XXX generate this list? */ +enum { + widgetMainWindow, + widgetForm, + widgetFrame, + widgetDrawingArea, + widgetCascadeButton, + widgetPushButton, + widgetMax, + + /* Pseudo-widgets */ + widgetMenuBar = widgetMax +}; + +static WidgetClass * const widgets[widgetMax] = { + &xmMainWindowWidgetClass, + &xmFormWidgetClass, + &xmFrameWidgetClass, + &xmDrawingAreaWidgetClass, + &xmCascadeButtonWidgetClass, + &xmPushButtonWidgetClass +}; + static const struct ui_widget { uint_least16_t name; - uint_least16_t subtree; - WidgetClass *class; - void (*configure)(Widget w); + uint_least8_t subtree; + uint_least8_t widget_type; } mainwin[] = { MAINWIN_INITIALIZER }; +static const struct ui_menuitem { + struct ui_widget w; + uint_least16_t label; +} mainmenu[] = { MAINMENU_INITIALIZER }; + static void ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args) { @@ -78,9 +106,7 @@ static void game_configure(Widget form) assert(gamearea && goalarea); XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL); - XtVaSetValues(gamearea, //XmNrightAttachment, XmATTACH_POSITION, - //XmNrightPosition, SPLIT_NUMERATOR, - XmNleftAttachment, XmATTACH_FORM, + XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM, XmNtopAttachment, XmATTACH_FORM, (char *)NULL); @@ -99,24 +125,83 @@ static void game_configure(Widget form) )); } +static Widget create_widget(const struct ui_widget *item, Widget parent, + ArgList args, Cardinal num_args) +{ + String name = (void *)&tree_strtab[item->name]; + WidgetClass class; + Widget w; + + if (item->widget_type == widgetMenuBar) + return XmCreateMenuBar(parent, name, args, num_args); + + assert(item->widget_type < widgetMax); + class = *widgets[item->widget_type]; + return XtCreateWidget(name, class, parent, args, num_args); +} + static void construct_widgets(const struct ui_widget *root, Widget parent, unsigned i) { const struct ui_widget *item; for (item = &root[i]; item->name; item++) { - Widget w; + Widget w = create_widget(item, parent, NULL, 0); - w = XtCreateWidget(&tree_strtab[item->name], *item->class, - parent, NULL, 0); - if (item->subtree) { + if (item->subtree) construct_widgets(root, w, item->subtree); - } - if (item->configure) { - item->configure(w); + XtManageChild(w); + } +} + +static void menu_cb(Widget w, void *data, void *cb_data) +{ + XmRowColumnCallbackStruct *cbs = cb_data; + XtCallActionProc(cbs->widget, XtName(cbs->widget), cbs->event, NULL, 0); +} + +static Widget create_pulldown(Widget parent) +{ + Widget w; + + w = XmCreatePulldownMenu(parent, XtName(parent), NULL, 0); + XtVaSetValues(parent, XmNsubMenuId, w, (char *)NULL); + XtAddCallback(w, XmNentryCallback, menu_cb, NULL); + + return w; +} + +static void +construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i) +{ + const struct ui_menuitem *item; + + for (item = &root[i]; item->w.name; item++) { + const char *label = &strtab[item->label]; + unsigned n = 0; + Arg args[2]; + XmString s; + Widget w; + + if (XtClass(parent) == *widgets[widgetCascadeButton]) + parent = create_pulldown(parent); + + if (label[0] && label[1] == '|') { + XtSetArg(args[n], XmNmnemonic, label[0]); n++; + label += 2; } + s = XmStringCreateLocalized((void *)label); + XtSetArg(args[n], XmNlabelString, s); n++; + + w = create_widget(&item->w, parent, args, n); + + XmStringFree(s); + + if (item->w.subtree) + construct_menu(root, w, item->w.subtree); + XtManageChild(w); } } @@ -160,15 +245,24 @@ static void game_input(Widget w, void *data, void *cb_data) void ui_initialize(struct app_state *state, Widget shell) { + Widget menubar, help; + construct_widgets(mainwin, shell, 0); - state->game = XtNameToWidget(shell, "*gameCanvas"); - state->goal = XtNameToWidget(shell, "*goalCanvas"); + menubar = XtNameToWidget(shell, "*menuBar"); + construct_menu(mainmenu, menubar, 0); + help = XtNameToWidget(menubar, "helpMenu"); + XtVaSetValues(menubar, XmNmenuHelpWidget, help, (char *)NULL); + + game_configure(XtNameToWidget(shell, "*game")); + + state->game = XtNameToWidget(shell, "*gameCanvas"); XtAddCallback(state->game, XmNresizeCallback, game_resize, state); XtAddCallback(state->game, XmNexposeCallback, game_resize, state); XtAddCallback(state->game, XmNinputCallback, game_input, state); + state->goal = XtNameToWidget(shell, "*goalCanvas"); XtAddCallback(state->goal, XmNresizeCallback, goal_resize, state); XtAddCallback(state->goal, XmNexposeCallback, goal_resize, state); } diff --git a/src/motifgui.dat b/src/motifgui.dat index a8d69cb..18583eb 100644 --- a/src/motifgui.dat +++ b/src/motifgui.dat @@ -1,7 +1,15 @@ MAINWIN - main, main_OFFSET, &xmMainWindowWidgetClass - game, game_OFFSET, &xmFormWidgetClass, game_configure - gameArea, gameArea_OFFSET, &xmFrameWidgetClass - gameCanvas, 0, &xmDrawingAreaWidgetClass - goalArea, goalArea_OFFSET, &xmFrameWidgetClass - goalCanvas, 0, &xmDrawingAreaWidgetClass + main, main_OFFSET, widgetMainWindow + menuBar, 0, widgetMenuBar + game, game_OFFSET, widgetForm + gameArea, gameArea_OFFSET, widgetFrame + gameCanvas, 0, widgetDrawingArea + goalArea, goalArea_OFFSET, widgetFrame + goalCanvas, 0, widgetDrawingArea + +MAINMENU + gameMenu, gameMenu_OFFSET, widgetCascadeButton, gameMenuLabel + gameNew, 0, widgetPushButton, gameNewLabel + gameExit, 0, widgetPushButton, gameExitLabel + helpMenu, helpMenu_OFFSET, widgetCascadeButton, helpMenuLabel + helpAbout, 0, widgetPushButton, helpAboutLabel diff --git a/src/options.opt b/src/motifopt.opt similarity index 100% rename from src/options.opt rename to src/motifopt.opt diff --git a/src/motifstr.str b/src/motifstr.str new file mode 100644 index 0000000..27ac167 --- /dev/null +++ b/src/motifstr.str @@ -0,0 +1,5 @@ +&gameMenuLabel G|Game +&gameNewLabel N|New +&gameExitLabel x|Exit +&helpMenuLabel H|Help +&helpAboutLabel A|About RRace -- 2.43.2