X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/821ab2c68e12d85d6bb2ed8b0d48c846a65d06a0..390cfad8fccd779eef64566015d891efc7d98d15:/windows.c diff --git a/windows.c b/windows.c index 50ca7c2..f4cbfe4 100644 --- a/windows.c +++ b/windows.c @@ -74,6 +74,11 @@ void debug_printf(char *fmt, ...) } #endif +#define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \ + (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED)) + +static void new_game_size(frontend *fe); + struct font { HFONT font; int type; @@ -116,7 +121,6 @@ struct frontend { HPEN oldpen; char *help_path; int help_has_contents; - char *laststatus; enum { DRAWING, PRINTING, NOTHING } drawstatus; DOCINFO di; int printcount, printw, printh, printsolns, printcurr, printcolour; @@ -175,18 +179,8 @@ void get_random_seed(void **randseed, int *randseedsize) static void win_status_bar(void *handle, char *text) { frontend *fe = (frontend *)handle; - char *rewritten; - - assert(fe->drawstatus == DRAWING); - rewritten = midend_rewrite_statusbar(fe->me, text); - if (!fe->laststatus || strcmp(rewritten, fe->laststatus)) { - SetWindowText(fe->statusbar, rewritten); - sfree(fe->laststatus); - fe->laststatus = rewritten; - } else { - sfree(rewritten); - } + SetWindowText(fe->statusbar, text); } static blitter *win_blitter_new(void *handle, int w, int h) @@ -367,7 +361,6 @@ static void win_set_pen(frontend *fe, int colour, int thin) int width = thin ? 0 : fe->linewidth; print_get_colour(fe->dr, colour, &hatch, &r, &g, &b); - /* FIXME: line thickness here */ if (fe->printcolour) pen = CreatePen(PS_SOLID, width, RGB(r * 255, g * 255, b * 255)); @@ -757,12 +750,7 @@ static void win_end_page(void *handle, int number) frontend *fe = (frontend *)handle; assert(fe->drawstatus != DRAWING); - /* - * The MSDN web site sample code doesn't bother to call EndDoc - * if an error occurs half way through printing. I expect doing - * so would cause the erroneous document to actually be - * printed, or something equally undesirable. - */ + if (fe->drawstatus == NOTHING) return; @@ -790,6 +778,12 @@ static void win_end_doc(void *handle) } fe->fontstart = 0; + /* + * The MSDN web site sample code doesn't bother to call EndDoc + * if an error occurs half way through printing. I expect doing + * so would cause the erroneous document to actually be + * printed, or something equally undesirable. + */ if (fe->drawstatus == NOTHING) return; @@ -885,8 +879,10 @@ void print(frontend *fe) pd.nFromPage = pd.nToPage = 0xFFFF; pd.nMinPage = pd.nMaxPage = 1; - if (!PrintDlg(&pd)) + if (!PrintDlg(&pd)) { + document_free(doc); return; + } /* * Now pd.hDC is a device context for the printer. @@ -908,7 +904,7 @@ void print(frontend *fe) fe->drawstatus = PRINTING; fe->hdc = pd.hDC; - fe->dr = drawing_init(&win_drawing, fe); + fe->dr = drawing_new(&win_drawing, NULL, fe); document_print(doc, fe->dr); drawing_free(fe->dr); fe->dr = NULL; @@ -916,6 +912,7 @@ void print(frontend *fe) fe->drawstatus = NOTHING; DeleteDC(pd.hDC); + document_free(doc); } void deactivate_timer(frontend *fe) @@ -1041,9 +1038,7 @@ static void check_window_size(frontend *fe, int *px, int *py) r.left = r.top = 0; r.right = x; r.bottom = y + sy; - AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ - (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), - TRUE, 0); + AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOMOVE | SWP_NOZORDER); } @@ -1058,12 +1053,35 @@ static void check_window_size(frontend *fe, int *px, int *py) *py = y; } +static void get_max_puzzle_size(frontend *fe, int *x, int *y) +{ + RECT r, sr; + + if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) { + *x = sr.right - sr.left; + *y = sr.bottom - sr.top; + r.left = 100; + r.right = 200; + r.top = 100; + r.bottom = 200; + AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); + *x -= r.right - r.left - 100; + *y -= r.bottom - r.top - 100; + } else { + *x = *y = INT_MAX; + } + + if (fe->statusbar != NULL) { + GetWindowRect(fe->statusbar, &sr); + *y -= sr.bottom - sr.top; + } +} + static frontend *new_window(HINSTANCE inst, char *game_id, char **error) { frontend *fe; int x, y; RECT r; - HDC hdc; fe = snew(frontend); @@ -1095,8 +1113,6 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) fe->fonts = NULL; fe->nfonts = fe->fontsize = 0; - fe->laststatus = NULL; - { int i, ncolours; float *colours; @@ -1114,17 +1130,24 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) fe->brushes[i] = CreateSolidBrush(fe->colours[i]); fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]); } + sfree(colours); } - x = y = INT_MAX; /* find puzzle's preferred size */ + if (midend_wants_statusbar(fe->me)) { + fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", + WS_CHILD | WS_VISIBLE, + 0, 0, 0, 0, /* status bar does these */ + NULL, NULL, inst, NULL); + } else + fe->statusbar = NULL; + + get_max_puzzle_size(fe, &x, &y); midend_size(fe->me, &x, &y, FALSE); r.left = r.top = 0; r.right = x; r.bottom = y; - AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ - (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED), - TRUE, 0); + AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name, WS_OVERLAPPEDWINDOW &~ @@ -1135,6 +1158,7 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) if (midend_wants_statusbar(fe->me)) { RECT sr; + DestroyWindow(fe->statusbar); fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, "ooh", WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, /* status bar does these */ @@ -1226,12 +1250,10 @@ static frontend *new_window(HINSTANCE inst, char *game_id, char **error) SetMenu(fe->hwnd, bar); } + fe->bitmap = NULL; + new_game_size(fe); /* initialises fe->bitmap */ check_window_size(fe, &x, &y); - hdc = GetDC(fe->hwnd); - fe->bitmap = CreateCompatibleBitmap(hdc, x, y); - ReleaseDC(fe->hwnd, hdc); - SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe); ShowWindow(fe->hwnd, SW_NORMAL); @@ -1846,16 +1868,13 @@ static void new_game_size(frontend *fe) HDC hdc; int x, y; - x = y = INT_MAX; + get_max_puzzle_size(fe, &x, &y); midend_size(fe->me, &x, &y, FALSE); r.left = r.top = 0; r.right = x; r.bottom = y; - AdjustWindowRectEx(&r, WS_OVERLAPPEDWINDOW &~ - (WS_THICKFRAME | WS_MAXIMIZEBOX | - WS_OVERLAPPED), - TRUE, 0); + AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0); if (fe->statusbar != NULL) { GetWindowRect(fe->statusbar, &sr); @@ -1873,7 +1892,7 @@ static void new_game_size(frontend *fe) SetWindowPos(fe->statusbar, NULL, 0, y, x, sr.bottom - sr.top, SWP_NOZORDER); - DeleteObject(fe->bitmap); + if (fe->bitmap) DeleteObject(fe->bitmap); hdc = GetDC(fe->hwnd); fe->bitmap = CreateCompatibleBitmap(hdc, x, y);