]> git.draconx.ca Git - rrace.git/blobdiff - src/motif_ui.c
motif: Avoid constantly recalculating tile size.
[rrace.git] / src / motif_ui.c
index 6abdbc9d6db5dbe0aca21eab9cb6d28a358056bf..5941834c86ae8b7bcba3ef35290615e5a068ccc0 100644 (file)
 #include "xcounter.h"
 #include "version.h"
 
-#define SPLIT_NUMERATOR    75
-#define SPLIT_DENOMINATOR 100
-
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-
 /* XXX generate this list? */
 enum {
        widgetMainWindow,
@@ -69,37 +64,6 @@ static const struct ui_menuitem {
        uint_least16_t label;
 } mainmenu[] = { MAINMENU_INITIALIZER };
 
-static void
-ResizeGameArea(Widget form, XEvent *e, String *args, Cardinal *num_args)
-{
-       Widget game = XtNameToWidget(form, &tree_strtab[gameArea]);
-       Widget goal = XtNameToWidget(form, &tree_strtab[goalArea]);
-       Dimension w, h, gamesz, gameborder, goalsz, goalborder;
-       int x, y, gap;
-
-       XtVaGetValues(form, XmNwidth, &w, XmNheight, &h, (char *)NULL);
-       XtVaGetValues(game, XmNshadowThickness, &gameborder, (char *)NULL);
-       XtVaGetValues(goal, XmNshadowThickness, &goalborder,
-                           XmNleftOffset, &gap,
-                           (char *)NULL);
-
-       gamesz = MIN(h, w * SPLIT_NUMERATOR / SPLIT_DENOMINATOR);
-       gamesz = 5*( (gamesz - 2*gameborder)/5 ) + 2*gameborder;
-
-       goalsz = MIN(gamesz*3/5, w - gamesz - gap);
-       goalsz = 3*( (goalsz - 2*goalborder)/3 ) + 2*goalborder;
-
-       x = (w - gamesz - goalsz - gap) / 2;
-       if (x < 2) x = 0;
-
-       y = (h - gamesz) / 2;
-       if (y < 3) y = 0;
-
-       XtVaSetValues(game, XmNleftOffset, x, XmNtopOffset, y, (char *)NULL);
-       XtVaSetValues(game, XmNwidth, gamesz, XmNheight, gamesz, (char *)NULL);
-       XtVaSetValues(goal, XmNwidth, goalsz, XmNheight, goalsz, (char *)NULL);
-}
-
 static char *timer_text(int_fast32_t ms, char *buf)
 {
        unsigned min, sec;
@@ -132,7 +96,6 @@ static void configure_mainwin(struct app_state *state, Widget form)
        char xc_template[100];
 
        assert(gamearea && goalarea && timer);
-       XtVaSetValues(form, XmNfractionBase, SPLIT_DENOMINATOR, (char *)NULL);
 
        state->game = XtNameToWidget(gamearea, &tree_strtab[gameCanvas]);
        XtVaSetValues(gamearea, XmNleftAttachment, XmATTACH_FORM,
@@ -155,9 +118,6 @@ static void configure_mainwin(struct app_state *state, Widget form)
        state->timer = xcounter_simple_init(timer, timer_text(0, xc_template));
        ui_timer_update(state, -1);
 
-       resize_rec.string = "ResizeGameArea";
-       resize_rec.proc = ResizeGameArea;
-       XtAppAddActions(XtWidgetToApplicationContext(form), &resize_rec, 1);
        XtOverrideTranslations(form, XtParseTranslationTable(
                "<Configure>: ResizeGameArea()\n"
                "<Map>: ResizeGameArea()\n"
@@ -165,9 +125,9 @@ static void configure_mainwin(struct app_state *state, Widget form)
 
        /*
         * Performing the initial update of the layout seems to avoid
-        * some weird problems on Motif 2.1
+        * some weird initial sizing problems on Motif 2.1
         */
-       ResizeGameArea(form, 0, 0, 0);
+       XtCallActionProc(form, "ResizeGameArea", 0, 0, 0);
 }
 
 static Widget create_widget(const struct ui_widget *item, Widget parent,
@@ -257,12 +217,12 @@ construct_menu(const struct ui_menuitem *root, Widget parent, unsigned i)
 }
 
 /* Figure out which tiles intersect a rectangle. */
-static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_w, int tile_h)
+static uint_fast32_t x11_expose_mask(XExposeEvent *e, int tile_sz)
 {
-       return board_rect( e->x/tile_w,
-                          e->y/tile_h,
-                         (e->x+e->width-1)/tile_w,
-                         (e->y+e->height-1)/tile_h );
+       return board_rect( e->x/tile_sz,
+                          e->y/tile_sz,
+                         (e->x+e->width-1)/tile_sz,
+                         (e->y+e->height-1)/tile_sz );
 }
 
 static void resize(Widget w, void *data, void *cb_data)
@@ -277,31 +237,27 @@ static void expose(Widget w, void *data, void *cb_data)
        XmDrawingAreaCallbackStruct *cbs = cb_data;
        XExposeEvent *e = &cbs->event->xexpose;
        struct app_state *state = data;
-       Dimension tile_w, tile_h;
        uint_fast32_t mask;
+       Dimension tile_sz;
 
-       XtVaGetValues(w, XmNwidth, &tile_w, XmNheight, &tile_h, (char *)NULL);
        if (w == state->game) {
                uint_least32_t *gp = state->board.game;
 
-               if (!(tile_w /= 5) || !(tile_h /= 5))
-                       return;
-
                /*
                 * Only draw exposed nonempty tiles; exposed areas are filled
                 * with the background automatically and thus exposed empty
                 * spaces don't need to be drawn again.
                 */
+               tile_sz = state->game_tile_sz;
                mask = gp[0] | gp[1] | gp[2];
        } else {
-               if (!(tile_w /= 3) || !(tile_h /= 3))
-                       return;
-
                /* Goal area never has empty tiles. */
+               tile_sz = state->goal_tile_sz;
                mask = -1;
        }
 
-       mask &= x11_expose_mask(e, tile_w, tile_h);
+       if (tile_sz)
+               mask &= x11_expose_mask(e, tile_sz);
        x11_queue_render(state, mask, 1<<(w == state->game));
 }