shithub: puzzles

Download patch

ref: a8c8237bd0362e4fe44a451eb81544b451ae4444
parent: fa7ef572c782c9394f60202d950d3380dfdce5c3
author: Simon Tatham <anakin@pobox.com>
date: Thu Apr 29 15:23:08 EDT 2004

Added a status bar.

[originally from svn r4174]

--- a/Recipe
+++ b/Recipe
@@ -12,7 +12,7 @@
 !makefile vc Makefile.vc
 !makefile cygwin Makefile.cyg
 
-WINDOWS  = windows user32.lib gdi32.lib
+WINDOWS  = windows user32.lib gdi32.lib comctl32.lib
 COMMON   = midend misc malloc
 NET      = net random tree234
 
--- a/cube.c
+++ b/cube.c
@@ -788,7 +788,7 @@
 
     state->previous = state->current;
     state->angle = 0.0;
-    state->completed = FALSE;
+    state->completed = 0;
     state->movecount = 0;
 
     return state;
@@ -1068,7 +1068,7 @@
             if (ret->facecolours[i])
                 j++;
         if (j == ret->solid->nfaces)
-            ret->completed = TRUE;
+            ret->completed = ret->movecount;
     }
 
     sfree(poly);
@@ -1329,6 +1329,19 @@
 
     draw_update(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
                 (int)((bb.d-bb.u+2.0F) * GRID_SCALE));
+
+    /*
+     * Update the status bar.
+     */
+    {
+	char statusbuf[256];
+
+	sprintf(statusbuf, "%sMoves: %d",
+		(state->completed ? "COMPLETED! " : ""),
+		(state->completed ? state->completed : state->movecount));
+
+	status_bar(fe, statusbuf);
+    }
 }
 
 float game_anim_length(game_state *oldstate, game_state *newstate)
@@ -1339,4 +1352,9 @@
 float game_flash_length(game_state *oldstate, game_state *newstate)
 {
     return 0.0F;
+}
+
+int game_wants_statusbar(void)
+{
+    return TRUE;
 }
--- a/fifteen.c
+++ b/fifteen.c
@@ -42,6 +42,7 @@
     int *tiles;
     int gap_pos;
     int completed;
+    int movecount;
 };
 
 game_params *default_params(void)
@@ -224,7 +225,7 @@
     assert(!*p);
     assert(state->tiles[state->gap_pos] == 0);
 
-    state->completed = FALSE;
+    state->completed = state->movecount = 0;
 
     return state;
 }
@@ -240,6 +241,7 @@
     memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
     ret->gap_pos = state->gap_pos;
     ret->completed = state->completed;
+    ret->movecount = state->movecount;
 
     return ret;
 }
@@ -297,6 +299,7 @@
     for (p = from->gap_pos; p != ret->gap_pos; p += up) {
         assert(p >= 0 && p < from->n);
         ret->tiles[p] = from->tiles[p + up];
+	ret->movecount++;
     }
 
     /*
@@ -303,10 +306,10 @@
      * See if the game has been completed.
      */
     if (!ret->completed) {
-        ret->completed = TRUE;
+        ret->completed = ret->movecount;
         for (p = 0; p < ret->n; p++)
             if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
-                ret->completed = FALSE;
+                ret->completed = 0;
     }
 
     return ret;
@@ -544,6 +547,19 @@
         }
     }
     ds->bgcolour = bgcolour;
+
+    /*
+     * Update the status bar.
+     */
+    {
+	char statusbuf[256];
+
+	sprintf(statusbuf, "%sMoves: %d",
+		(state->completed ? "COMPLETED! " : ""),
+		(state->completed ? state->completed : state->movecount));
+
+	status_bar(fe, statusbuf);
+    }
 }
 
 float game_anim_length(game_state *oldstate, game_state *newstate)
@@ -557,4 +573,9 @@
         return 2 * FLASH_FRAME;
     else
         return 0.0F;
+}
+
+int game_wants_statusbar(void)
+{
+    return TRUE;
 }
--- a/gtk.c
+++ b/gtk.c
@@ -3,6 +3,7 @@
  */
 
 #include <stdio.h>
+#include <assert.h>
 #include <stdlib.h>
 #include <time.h>
 #include <stdarg.h>
@@ -50,6 +51,8 @@
 struct frontend {
     GtkWidget *window;
     GtkWidget *area;
+    GtkWidget *statusbar;
+    guint statusctx;
     GdkPixmap *pixmap;
     GdkColor *colours;
     int ncolours;
@@ -71,6 +74,14 @@
     output[2] = col.blue / 65535.0;
 }
 
+void status_bar(frontend *fe, char *text)
+{
+    assert(fe->statusbar);
+
+    gtk_statusbar_pop(GTK_STATUSBAR(fe->statusbar), fe->statusctx);
+    gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx, text);
+}
+
 void start_draw(frontend *fe)
 {
     fe->gc = gdk_gc_new(fe->area->window);
@@ -299,6 +310,21 @@
     return TRUE;
 }
 
+static gint map_window(GtkWidget *widget, GdkEvent *event,
+		       gpointer data)
+{
+    frontend *fe = (frontend *)data;
+
+    /*
+     * Apparently we need to do this because otherwise the status
+     * bar will fail to update immediately. Annoying, but there we
+     * go.
+     */
+    gtk_widget_queue_draw(fe->window);
+
+    return TRUE;
+}
+
 static gint configure_area(GtkWidget *widget,
                            GdkEventConfigure *event, gpointer data)
 {
@@ -483,6 +509,17 @@
         }
     }
 
+    if (midend_wants_statusbar(fe->me)) {
+	fe->statusbar = gtk_statusbar_new();
+	gtk_box_pack_end(vbox, fe->statusbar, FALSE, FALSE, 0);
+	gtk_widget_show(fe->statusbar);
+	fe->statusctx = gtk_statusbar_get_context_id
+	    (GTK_STATUSBAR(fe->statusbar), "game");
+	gtk_statusbar_push(GTK_STATUSBAR(fe->statusbar), fe->statusctx,
+			   "");
+    } else
+	fe->statusbar = NULL;
+
     fe->area = gtk_drawing_area_new();
     midend_size(fe->me, &x, &y);
     gtk_drawing_area_size(GTK_DRAWING_AREA(fe->area), x, y);
@@ -505,6 +542,8 @@
 		       GTK_SIGNAL_FUNC(button_event), fe);
     gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
 		       GTK_SIGNAL_FUNC(expose_area), fe);
+    gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
+		       GTK_SIGNAL_FUNC(map_window), fe);
     gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
 		       GTK_SIGNAL_FUNC(configure_area), fe);
 
--- a/midend.c
+++ b/midend.c
@@ -294,3 +294,8 @@
     *name = me->preset_names[n];
     *params = me->presets[n];
 }
+
+int midend_wants_statusbar(midend_data *me)
+{
+    return game_wants_statusbar();
+}
--- a/net.c
+++ b/net.c
@@ -1189,6 +1189,24 @@
             }
         }
 
+    /*
+     * Update the status bar.
+     */
+    {
+	char statusbuf[256];
+	int i, n, a;
+
+	n = state->width * state->height;
+	for (i = a = 0; i < n; i++)
+	    if (active[i])
+		a++;
+
+	sprintf(statusbuf, "%sActive: %d/%d",
+		(state->completed ? "COMPLETED! " : ""), a, n);
+
+	status_bar(fe, statusbuf);
+    }
+
     sfree(active);
 }
 
@@ -1230,4 +1248,9 @@
     }
 
     return 0.0F;
+}
+
+int game_wants_statusbar(void)
+{
+    return TRUE;
 }
--- a/nullgame.c
+++ b/nullgame.c
@@ -144,3 +144,8 @@
 {
     return 0.0F;
 }
+
+int game_wants_statusbar(void)
+{
+    return FALSE;
+}
--- a/puzzles.h
+++ b/puzzles.h
@@ -65,6 +65,7 @@
 void end_draw(frontend *fe);
 void deactivate_timer(frontend *fe);
 void activate_timer(frontend *fe);
+void status_bar(frontend *fe, char *text);
 
 /*
  * midend.c
@@ -82,6 +83,7 @@
 int midend_num_presets(midend_data *me);
 void midend_fetch_preset(midend_data *me, int n,
                          char **name, game_params **params);
+int midend_wants_statusbar(midend_data *me);
 
 /*
  * malloc.c
@@ -130,5 +132,6 @@
                  game_state *newstate, float anim_time, float flash_time);
 float game_anim_length(game_state *oldstate, game_state *newstate);
 float game_flash_length(game_state *oldstate, game_state *newstate);
+int game_wants_statusbar(void);
 
 #endif /* PUZZLES_PUZZLES_H */
--- a/sixteen.c
+++ b/sixteen.c
@@ -43,6 +43,7 @@
     int w, h, n;
     int *tiles;
     int completed;
+    int movecount;
 };
 
 game_params *default_params(void)
@@ -231,7 +232,7 @@
     }
     assert(!*p);
 
-    state->completed = FALSE;
+    state->completed = state->movecount = 0;
 
     return state;
 }
@@ -246,6 +247,7 @@
     ret->tiles = snewn(state->w * state->h, int);
     memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
     ret->completed = state->completed;
+    ret->movecount = state->movecount;
 
     return ret;
 }
@@ -287,11 +289,13 @@
         ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
     } while (--n > 0);
 
+    ret->movecount++;
+
     /*
      * See if the game has been completed.
      */
     if (!ret->completed) {
-        ret->completed = TRUE;
+        ret->completed = ret->movecount;
         for (n = 0; n < ret->n; n++)
             if (ret->tiles[n] != n+1)
                 ret->completed = FALSE;
@@ -588,6 +592,19 @@
     unclip(fe);
 
     ds->bgcolour = bgcolour;
+
+    /*
+     * Update the status bar.
+     */
+    {
+	char statusbuf[256];
+
+	sprintf(statusbuf, "%sMoves: %d",
+		(state->completed ? "COMPLETED! " : ""),
+		(state->completed ? state->completed : state->movecount));
+
+	status_bar(fe, statusbuf);
+    }
 }
 
 float game_anim_length(game_state *oldstate, game_state *newstate)
@@ -601,4 +618,9 @@
         return 2 * FLASH_FRAME;
     else
         return 0.0F;
+}
+
+int game_wants_statusbar(void)
+{
+    return TRUE;
 }
--- a/windows.c
+++ b/windows.c
@@ -3,6 +3,7 @@
  */
 
 #include <windows.h>
+#include <commctrl.h>
 
 #include <stdio.h>
 #include <assert.h>
@@ -72,7 +73,7 @@
 
 struct frontend {
     midend_data *me;
-    HWND hwnd;
+    HWND hwnd, statusbar;
     HBITMAP bitmap, prevbm;
     HDC hdc_bm;
     COLORREF *colours;
@@ -100,6 +101,11 @@
     exit(1);
 }
 
+void status_bar(frontend *fe, char *text)
+{
+    SetWindowText(fe->statusbar, text);
+}
+
 void frontend_default_colour(frontend *fe, float *output)
 {
     DWORD c = GetSysColor(COLOR_MENU); /* ick */
@@ -291,7 +297,7 @@
 {
     frontend *fe;
     int x, y;
-    RECT r;
+    RECT r, sr;
     HDC hdc;
 
     fe = snew(frontend);
@@ -374,6 +380,21 @@
 	SetMenu(fe->hwnd, bar);
     }
 
+    if (midend_wants_statusbar(fe->me)) {
+	fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh",
+				       WS_CHILD | WS_VISIBLE,
+				       0, 0, 0, 0, /* status bar does these */
+				       fe->hwnd, NULL, inst, NULL);
+	GetWindowRect(fe->statusbar, &sr);
+	SetWindowPos(fe->hwnd, NULL, 0, 0,
+		     r.right - r.left, r.bottom - r.top + sr.bottom - sr.top,
+		     SWP_NOMOVE | SWP_NOZORDER);
+	SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top,
+		     SWP_NOZORDER);
+    } else {
+	fe->statusbar = NULL;
+    }
+
     hdc = GetDC(fe->hwnd);
     fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
     ReleaseDC(fe->hwnd, hdc);
@@ -424,7 +445,7 @@
 		int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
 
 		if (p >= 0 && p < fe->npresets) {
-		    RECT r;
+		    RECT r, sr;
 		    HDC hdc;
 		    int x, y;
 
@@ -440,9 +461,18 @@
 					WS_OVERLAPPED),
 				       TRUE, 0);
 
+		    if (fe->statusbar != NULL) {
+			GetWindowRect(fe->statusbar, &sr);
+		    } else {
+			sr.left = sr.right = sr.top = sr.bottom = 0;
+		    }
 		    SetWindowPos(fe->hwnd, NULL, 0, 0,
-				 r.right - r.left, r.bottom - r.top,
+				 r.right - r.left,
+				 r.bottom - r.top + sr.bottom - sr.top,
 				 SWP_NOMOVE | SWP_NOZORDER);
+		    if (fe->statusbar != NULL)
+			SetWindowPos(fe->statusbar, NULL, 0, y, x,
+				     sr.bottom - sr.top, SWP_NOZORDER);
 
 		    DeleteObject(fe->bitmap);
 
@@ -571,6 +601,8 @@
     MSG msg;
 
     srand(time(NULL));
+
+    InitCommonControls();
 
     if (!prev) {
 	WNDCLASS wndclass;