2 * windows.c: Windows front end for my puzzle collection.
9 #endif /* NO_HTMLHELP */
30 #define IDM_NEW 0x0010
31 #define IDM_RESTART 0x0020
32 #define IDM_UNDO 0x0030
33 #define IDM_REDO 0x0040
34 #define IDM_COPY 0x0050
35 #define IDM_SOLVE 0x0060
36 #define IDM_QUIT 0x0070
37 #define IDM_CONFIG 0x0080
38 #define IDM_DESC 0x0090
39 #define IDM_SEED 0x00A0
40 #define IDM_HELPC 0x00B0
41 #define IDM_GAMEHELP 0x00C0
42 #define IDM_ABOUT 0x00D0
43 #define IDM_SAVE 0x00E0
44 #define IDM_LOAD 0x00F0
45 #define IDM_PRINT 0x0100
46 #define IDM_PRESETS 0x0110
48 #define IDM_KEYEMUL 0x0400
50 #define HELP_FILE_NAME "puzzles.hlp"
51 #define HELP_CNT_NAME "puzzles.cnt"
53 #define CHM_FILE_NAME "puzzles.chm"
54 #endif /* NO_HTMLHELP */
57 typedef HWND (CALLBACK *htmlhelp_t)(HWND, LPCSTR, UINT, DWORD);
58 static htmlhelp_t htmlhelp;
59 static HINSTANCE hh_dll;
60 #endif /* NO_HTMLHELP */
61 enum { NONE, HLP, CHM } help_type;
63 const char *help_topic;
64 int help_has_contents;
67 #define FILENAME_MAX (260)
71 #define HGDI_ERROR ((HANDLE)GDI_ERROR)
77 * Wrapper implementations of functions not supplied by the
81 #define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU)
85 int MessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
90 MultiByteToWideChar (CP_ACP, 0, lpText, -1, wText, 2048);
91 MultiByteToWideChar (CP_ACP, 0, lpCaption, -1, wCaption, 2048);
93 return MessageBoxW (hWnd, wText, wCaption, uType);
96 BOOL SetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPCSTR lpString)
100 MultiByteToWideChar (CP_ACP, 0, lpString, -1, wText, 256);
101 return SetDlgItemTextW(hDlg, nIDDlgItem, wText);
104 LPCSTR getenv(LPCSTR buf)
109 BOOL GetKeyboardState(PBYTE pb)
114 static TCHAR wGameName[256];
119 static FILE *debug_fp = NULL;
120 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
121 static int debug_got_console = 0;
123 void dputs(char *buf)
127 if (!debug_got_console) {
128 if (AllocConsole()) {
129 debug_got_console = 1;
130 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
134 debug_fp = fopen("debug.log", "w");
137 if (debug_hdl != INVALID_HANDLE_VALUE) {
138 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
140 fputs(buf, debug_fp);
144 void debug_printf(char *fmt, ...)
150 _vsnprintf(buf, 4095, fmt, ap);
157 #define WINFLAGS (WS_OVERLAPPEDWINDOW &~ \
158 (WS_THICKFRAME | WS_MAXIMIZEBOX | WS_OVERLAPPED))
160 #define WINFLAGS (WS_CAPTION | WS_SYSMENU)
163 static void new_game_size(frontend *fe);
181 enum { CFG_PRINT = CFG_FRONTEND_SPECIFIC };
185 HWND hwnd, statusbar, cfgbox;
187 HWND numpad; /* window handle for the numeric pad */
190 HBITMAP bitmap, prevbm;
191 RECT bitmapPosition; /* game bitmap position within game window */
198 DWORD timer_last_tickcount;
200 game_params **presets;
202 int nfonts, fontsize;
204 struct cfg_aux *cfgaux;
205 int cfg_which, dlg_done;
210 enum { DRAWING, PRINTING, NOTHING } drawstatus;
212 int printcount, printw, printh, printsolns, printcurr, printcolour;
214 int printoffsetx, printoffsety;
215 float printpixelscale;
221 void fatal(char *fmt, ...)
227 vsprintf(buf, fmt, ap);
230 MessageBox(NULL, buf, "Fatal error", MB_ICONEXCLAMATION | MB_OK);
235 char *geterrstr(void)
238 DWORD dw = GetLastError();
242 FORMAT_MESSAGE_ALLOCATE_BUFFER |
243 FORMAT_MESSAGE_FROM_SYSTEM,
246 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
250 ret = dupstr(lpMsgBuf);
257 void get_random_seed(void **randseed, int *randseedsize)
259 SYSTEMTIME *st = snew(SYSTEMTIME);
263 *randseed = (void *)st;
264 *randseedsize = sizeof(SYSTEMTIME);
267 static void win_status_bar(void *handle, char *text)
272 frontend *fe = (frontend *)handle;
275 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 255);
276 SendMessage(fe->statusbar, SB_SETTEXT,
277 (WPARAM) 255 | SBT_NOBORDERS,
280 SetWindowText(fe->statusbar, text);
284 static blitter *win_blitter_new(void *handle, int w, int h)
286 blitter *bl = snew(blitter);
288 memset(bl, 0, sizeof(blitter));
296 static void win_blitter_free(void *handle, blitter *bl)
298 if (bl->bitmap) DeleteObject(bl->bitmap);
302 static void blitter_mkbitmap(frontend *fe, blitter *bl)
304 HDC hdc = GetDC(fe->hwnd);
305 bl->bitmap = CreateCompatibleBitmap(hdc, bl->w, bl->h);
306 ReleaseDC(fe->hwnd, hdc);
309 /* BitBlt(dstDC, dstX, dstY, dstW, dstH, srcDC, srcX, srcY, dType) */
311 static void win_blitter_save(void *handle, blitter *bl, int x, int y)
313 frontend *fe = (frontend *)handle;
314 HDC hdc_win, hdc_blit;
317 assert(fe->drawstatus == DRAWING);
319 if (!bl->bitmap) blitter_mkbitmap(fe, bl);
321 bl->x = x; bl->y = y;
323 hdc_win = GetDC(fe->hwnd);
324 hdc_blit = CreateCompatibleDC(hdc_win);
325 if (!hdc_blit) fatal("hdc_blit failed: 0x%x", GetLastError());
327 prev_blit = SelectObject(hdc_blit, bl->bitmap);
328 if (prev_blit == NULL || prev_blit == HGDI_ERROR)
329 fatal("SelectObject for hdc_main failed: 0x%x", GetLastError());
331 if (!BitBlt(hdc_blit, 0, 0, bl->w, bl->h,
332 fe->hdc, x, y, SRCCOPY))
333 fatal("BitBlt failed: 0x%x", GetLastError());
335 SelectObject(hdc_blit, prev_blit);
337 ReleaseDC(fe->hwnd, hdc_win);
340 static void win_blitter_load(void *handle, blitter *bl, int x, int y)
342 frontend *fe = (frontend *)handle;
343 HDC hdc_win, hdc_blit;
346 assert(fe->drawstatus == DRAWING);
348 assert(bl->bitmap); /* we should always have saved before loading */
350 if (x == BLITTER_FROMSAVED) x = bl->x;
351 if (y == BLITTER_FROMSAVED) y = bl->y;
353 hdc_win = GetDC(fe->hwnd);
354 hdc_blit = CreateCompatibleDC(hdc_win);
356 prev_blit = SelectObject(hdc_blit, bl->bitmap);
358 BitBlt(fe->hdc, x, y, bl->w, bl->h,
359 hdc_blit, 0, 0, SRCCOPY);
361 SelectObject(hdc_blit, prev_blit);
363 ReleaseDC(fe->hwnd, hdc_win);
366 void frontend_default_colour(frontend *fe, float *output)
368 DWORD c = GetSysColor(COLOR_MENU); /* ick */
370 output[0] = (float)(GetRValue(c) / 255.0);
371 output[1] = (float)(GetGValue(c) / 255.0);
372 output[2] = (float)(GetBValue(c) / 255.0);
375 static POINT win_transform_point(frontend *fe, int x, int y)
379 assert(fe->drawstatus != NOTHING);
381 if (fe->drawstatus == PRINTING) {
382 ret.x = (int)(fe->printoffsetx + fe->printpixelscale * x);
383 ret.y = (int)(fe->printoffsety + fe->printpixelscale * y);
392 static void win_text_colour(frontend *fe, int colour)
394 assert(fe->drawstatus != NOTHING);
396 if (fe->drawstatus == PRINTING) {
399 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
401 SetTextColor(fe->hdc, RGB(r * 255, g * 255, b * 255));
403 SetTextColor(fe->hdc,
404 hatch == HATCH_CLEAR ? RGB(255,255,255) : RGB(0,0,0));
406 SetTextColor(fe->hdc, fe->colours[colour]);
410 static void win_set_brush(frontend *fe, int colour)
413 assert(fe->drawstatus != NOTHING);
415 if (fe->drawstatus == PRINTING) {
418 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
420 if (fe->printcolour) {
421 br = CreateSolidBrush(RGB(r * 255, g * 255, b * 255));
422 } else if (hatch == HATCH_SOLID) {
423 br = CreateSolidBrush(RGB(0,0,0));
424 } else if (hatch == HATCH_CLEAR) {
425 br = CreateSolidBrush(RGB(255,255,255));
429 * This is only ever required during printing, and the
430 * PocketPC port doesn't support printing.
432 fatal("CreateHatchBrush not supported");
434 br = CreateHatchBrush(hatch == HATCH_BACKSLASH ? HS_FDIAGONAL :
435 hatch == HATCH_SLASH ? HS_BDIAGONAL :
436 hatch == HATCH_HORIZ ? HS_HORIZONTAL :
437 hatch == HATCH_VERT ? HS_VERTICAL :
438 hatch == HATCH_PLUS ? HS_CROSS :
439 /* hatch == HATCH_X ? */ HS_DIAGCROSS,
444 br = fe->brushes[colour];
446 fe->oldbr = SelectObject(fe->hdc, br);
449 static void win_reset_brush(frontend *fe)
453 assert(fe->drawstatus != NOTHING);
455 br = SelectObject(fe->hdc, fe->oldbr);
456 if (fe->drawstatus == PRINTING)
460 static void win_set_pen(frontend *fe, int colour, int thin)
463 assert(fe->drawstatus != NOTHING);
465 if (fe->drawstatus == PRINTING) {
468 int width = thin ? 0 : fe->linewidth;
470 print_get_colour(fe->dr, colour, &hatch, &r, &g, &b);
472 pen = CreatePen(PS_SOLID, width,
473 RGB(r * 255, g * 255, b * 255));
474 else if (hatch == HATCH_SOLID)
475 pen = CreatePen(PS_SOLID, width, RGB(0, 0, 0));
476 else if (hatch == HATCH_CLEAR)
477 pen = CreatePen(PS_SOLID, width, RGB(255,255,255));
479 assert(!"This shouldn't happen");
480 pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
483 pen = fe->pens[colour];
485 fe->oldpen = SelectObject(fe->hdc, pen);
488 static void win_reset_pen(frontend *fe)
492 assert(fe->drawstatus != NOTHING);
494 pen = SelectObject(fe->hdc, fe->oldpen);
495 if (fe->drawstatus == PRINTING)
499 static void win_clip(void *handle, int x, int y, int w, int h)
501 frontend *fe = (frontend *)handle;
504 if (fe->drawstatus == NOTHING)
507 p = win_transform_point(fe, x, y);
508 q = win_transform_point(fe, x+w, y+h);
509 IntersectClipRect(fe->hdc, p.x, p.y, q.x, q.y);
512 static void win_unclip(void *handle)
514 frontend *fe = (frontend *)handle;
516 if (fe->drawstatus == NOTHING)
519 SelectClipRgn(fe->hdc, NULL);
522 static void win_draw_text(void *handle, int x, int y, int fonttype,
523 int fontsize, int align, int colour, char *text)
525 frontend *fe = (frontend *)handle;
530 if (fe->drawstatus == NOTHING)
533 if (fe->drawstatus == PRINTING)
534 fontsize = (int)(fontsize * fe->printpixelscale);
536 xy = win_transform_point(fe, x, y);
539 * Find or create the font.
541 for (i = fe->fontstart; i < fe->nfonts; i++)
542 if (fe->fonts[i].type == fonttype && fe->fonts[i].size == fontsize)
545 if (i == fe->nfonts) {
546 if (fe->fontsize <= fe->nfonts) {
547 fe->fontsize = fe->nfonts + 10;
548 fe->fonts = sresize(fe->fonts, fe->fontsize, struct font);
553 fe->fonts[i].type = fonttype;
554 fe->fonts[i].size = fontsize;
556 memset (&lf, 0, sizeof(LOGFONT));
557 lf.lfHeight = -fontsize;
558 lf.lfWeight = (fe->drawstatus == PRINTING ? 0 : FW_BOLD);
559 lf.lfCharSet = DEFAULT_CHARSET;
560 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
561 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
562 lf.lfQuality = DEFAULT_QUALITY;
563 lf.lfPitchAndFamily = (fonttype == FONT_FIXED ?
564 FIXED_PITCH | FF_DONTCARE :
565 VARIABLE_PITCH | FF_SWISS);
567 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
570 fe->fonts[i].font = CreateFontIndirect(&lf);
574 * Position and draw the text.
582 MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 256);
585 oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
586 if (GetTextMetrics(fe->hdc, &tm)) {
587 if (align & ALIGN_VCENTRE)
588 xy.y -= (tm.tmAscent+tm.tmDescent)/2;
593 if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size)) {
595 if (GetTextExtentPoint32(fe->hdc, wText, wcslen(wText), &size)) {
597 if (align & ALIGN_HCENTRE)
599 else if (align & ALIGN_HRIGHT)
602 SetBkMode(fe->hdc, TRANSPARENT);
603 win_text_colour(fe, colour);
605 TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
607 ExtTextOut(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
609 SelectObject(fe->hdc, oldfont);
613 static void win_draw_rect(void *handle, int x, int y, int w, int h, int colour)
615 frontend *fe = (frontend *)handle;
618 if (fe->drawstatus == NOTHING)
621 if (fe->drawstatus == DRAWING && w == 1 && h == 1) {
623 * Rectangle() appears to get uppity if asked to draw a 1x1
624 * rectangle, presumably on the grounds that that's beneath
625 * its dignity and you ought to be using SetPixel instead.
628 SetPixel(fe->hdc, x, y, fe->colours[colour]);
630 win_set_brush(fe, colour);
631 win_set_pen(fe, colour, TRUE);
632 p = win_transform_point(fe, x, y);
633 q = win_transform_point(fe, x+w, y+h);
634 Rectangle(fe->hdc, p.x, p.y, q.x, q.y);
640 static void win_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
642 frontend *fe = (frontend *)handle;
645 if (fe->drawstatus == NOTHING)
648 win_set_pen(fe, colour, FALSE);
649 pp[0] = win_transform_point(fe, x1, y1);
650 pp[1] = win_transform_point(fe, x2, y2);
651 Polyline(fe->hdc, pp, 2);
652 if (fe->drawstatus == DRAWING)
653 SetPixel(fe->hdc, pp[1].x, pp[1].y, fe->colours[colour]);
657 static void win_draw_circle(void *handle, int cx, int cy, int radius,
658 int fillcolour, int outlinecolour)
660 frontend *fe = (frontend *)handle;
663 assert(outlinecolour >= 0);
665 if (fe->drawstatus == NOTHING)
669 win_set_brush(fe, fillcolour);
671 fe->oldbr = SelectObject(fe->hdc, GetStockObject(NULL_BRUSH));
673 win_set_pen(fe, outlinecolour, FALSE);
674 p = win_transform_point(fe, cx - radius, cy - radius);
675 q = win_transform_point(fe, cx + radius, cy + radius);
676 Ellipse(fe->hdc, p.x, p.y, q.x+1, q.y+1);
681 static void win_draw_polygon(void *handle, int *coords, int npoints,
682 int fillcolour, int outlinecolour)
684 frontend *fe = (frontend *)handle;
688 if (fe->drawstatus == NOTHING)
691 pts = snewn(npoints+1, POINT);
693 for (i = 0; i <= npoints; i++) {
694 int j = (i < npoints ? i : 0);
695 pts[i] = win_transform_point(fe, coords[j*2], coords[j*2+1]);
698 assert(outlinecolour >= 0);
700 if (fillcolour >= 0) {
701 win_set_brush(fe, fillcolour);
702 win_set_pen(fe, outlinecolour, FALSE);
703 Polygon(fe->hdc, pts, npoints);
707 win_set_pen(fe, outlinecolour, FALSE);
708 Polyline(fe->hdc, pts, npoints+1);
715 static void win_start_draw(void *handle)
717 frontend *fe = (frontend *)handle;
720 assert(fe->drawstatus == NOTHING);
722 hdc_win = GetDC(fe->hwnd);
723 fe->hdc = CreateCompatibleDC(hdc_win);
724 fe->prevbm = SelectObject(fe->hdc, fe->bitmap);
725 ReleaseDC(fe->hwnd, hdc_win);
728 SetMapMode(fe->hdc, MM_TEXT);
730 fe->drawstatus = DRAWING;
733 static void win_draw_update(void *handle, int x, int y, int w, int h)
735 frontend *fe = (frontend *)handle;
738 if (fe->drawstatus != DRAWING)
746 OffsetRect(&r, fe->bitmapPosition.left, fe->bitmapPosition.top);
747 InvalidateRect(fe->hwnd, &r, FALSE);
750 static void win_end_draw(void *handle)
752 frontend *fe = (frontend *)handle;
753 assert(fe->drawstatus == DRAWING);
754 SelectObject(fe->hdc, fe->prevbm);
757 DeleteObject(fe->clip);
760 fe->drawstatus = NOTHING;
763 static void win_line_width(void *handle, float width)
765 frontend *fe = (frontend *)handle;
767 assert(fe->drawstatus != DRAWING);
768 if (fe->drawstatus == NOTHING)
771 fe->linewidth = (int)(width * fe->printpixelscale);
774 static void win_begin_doc(void *handle, int pages)
776 frontend *fe = (frontend *)handle;
778 assert(fe->drawstatus != DRAWING);
779 if (fe->drawstatus == NOTHING)
782 if (StartDoc(fe->hdc, &fe->di) <= 0) {
783 char *e = geterrstr();
784 MessageBox(fe->hwnd, e, "Error starting to print",
785 MB_ICONERROR | MB_OK);
787 fe->drawstatus = NOTHING;
791 * Push a marker on the font stack so that we won't use the
792 * same fonts for printing and drawing. (This is because
793 * drawing seems to look generally better in bold, but printing
794 * is better not in bold.)
796 fe->fontstart = fe->nfonts;
799 static void win_begin_page(void *handle, int number)
801 frontend *fe = (frontend *)handle;
803 assert(fe->drawstatus != DRAWING);
804 if (fe->drawstatus == NOTHING)
807 if (StartPage(fe->hdc) <= 0) {
808 char *e = geterrstr();
809 MessageBox(fe->hwnd, e, "Error starting a page",
810 MB_ICONERROR | MB_OK);
812 fe->drawstatus = NOTHING;
816 static void win_begin_puzzle(void *handle, float xm, float xc,
817 float ym, float yc, int pw, int ph, float wmm)
819 frontend *fe = (frontend *)handle;
820 int ppw, pph, pox, poy;
821 float mmpw, mmph, mmox, mmoy;
824 assert(fe->drawstatus != DRAWING);
825 if (fe->drawstatus == NOTHING)
828 ppw = GetDeviceCaps(fe->hdc, HORZRES);
829 pph = GetDeviceCaps(fe->hdc, VERTRES);
830 mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
831 mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
834 * Compute the puzzle's position on the logical page.
836 mmox = xm * mmpw + xc;
837 mmoy = ym * mmph + yc;
840 * Work out what that comes to in pixels.
842 pox = (int)(mmox * (float)ppw / mmpw);
843 poy = (int)(mmoy * (float)ppw / mmpw);
846 * And determine the scale.
848 * I need a scale such that the maximum puzzle-coordinate
849 * extent of the rectangle (pw * scale) is equal to the pixel
850 * equivalent of the puzzle's millimetre width (wmm * ppw /
853 scale = (wmm * ppw) / (mmpw * pw);
856 * Now store pox, poy and scale for use in the main drawing
859 fe->printoffsetx = pox;
860 fe->printoffsety = poy;
861 fe->printpixelscale = scale;
866 static void win_end_puzzle(void *handle)
868 /* Nothing needs to be done here. */
871 static void win_end_page(void *handle, int number)
873 frontend *fe = (frontend *)handle;
875 assert(fe->drawstatus != DRAWING);
877 if (fe->drawstatus == NOTHING)
880 if (EndPage(fe->hdc) <= 0) {
881 char *e = geterrstr();
882 MessageBox(fe->hwnd, e, "Error finishing a page",
883 MB_ICONERROR | MB_OK);
885 fe->drawstatus = NOTHING;
889 static void win_end_doc(void *handle)
891 frontend *fe = (frontend *)handle;
893 assert(fe->drawstatus != DRAWING);
896 * Free all the fonts created since we began printing.
898 while (fe->nfonts > fe->fontstart) {
900 DeleteObject(fe->fonts[fe->nfonts].font);
905 * The MSDN web site sample code doesn't bother to call EndDoc
906 * if an error occurs half way through printing. I expect doing
907 * so would cause the erroneous document to actually be
908 * printed, or something equally undesirable.
910 if (fe->drawstatus == NOTHING)
913 if (EndDoc(fe->hdc) <= 0) {
914 char *e = geterrstr();
915 MessageBox(fe->hwnd, e, "Error finishing printing",
916 MB_ICONERROR | MB_OK);
918 fe->drawstatus = NOTHING;
922 const struct drawing_api win_drawing = {
947 void print(frontend *fe)
953 midend *nme = NULL; /* non-interactive midend for bulk puzzle generation */
958 * Create our document structure and fill it up with puzzles.
960 doc = document_new(fe->printw, fe->printh, fe->printscale / 100.0F);
961 for (i = 0; i < fe->printcount; i++) {
962 if (i == 0 && fe->printcurr) {
963 err = midend_print_puzzle(fe->me, doc, fe->printsolns);
968 nme = midend_new(NULL, &thegame, NULL, NULL);
971 * Set the non-interactive mid-end to have the same
972 * parameters as the standard one.
974 params = midend_get_params(fe->me);
975 midend_set_params(nme, params);
976 thegame.free_params(params);
979 midend_new_game(nme);
980 err = midend_print_puzzle(nme, doc, fe->printsolns);
989 MessageBox(fe->hwnd, err, "Error preparing puzzles for printing",
990 MB_ICONERROR | MB_OK);
995 memset(&pd, 0, sizeof(pd));
996 pd.lStructSize = sizeof(pd);
997 pd.hwndOwner = fe->hwnd;
1000 pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC |
1001 PD_NOPAGENUMS | PD_NOSELECTION;
1003 pd.nFromPage = pd.nToPage = 0xFFFF;
1004 pd.nMinPage = pd.nMaxPage = 1;
1006 if (!PrintDlg(&pd)) {
1012 * Now pd.hDC is a device context for the printer.
1016 * FIXME: IWBNI we put up an Abort box here.
1019 memset(&fe->di, 0, sizeof(fe->di));
1020 fe->di.cbSize = sizeof(fe->di);
1021 sprintf(doctitle, "Printed puzzles from %s (from Simon Tatham's"
1022 " Portable Puzzle Collection)", thegame.name);
1023 fe->di.lpszDocName = doctitle;
1024 fe->di.lpszOutput = NULL;
1025 fe->di.lpszDatatype = NULL;
1028 fe->drawstatus = PRINTING;
1031 fe->dr = drawing_new(&win_drawing, NULL, fe);
1032 document_print(doc, fe->dr);
1033 drawing_free(fe->dr);
1036 fe->drawstatus = NOTHING;
1043 void deactivate_timer(frontend *fe)
1046 return; /* for non-interactive midend */
1047 if (fe->hwnd) KillTimer(fe->hwnd, fe->timer);
1051 void activate_timer(frontend *fe)
1054 return; /* for non-interactive midend */
1056 fe->timer = SetTimer(fe->hwnd, 1, 20, NULL);
1057 fe->timer_last_tickcount = GetTickCount();
1061 void write_clip(HWND hwnd, char *data)
1069 * Windows expects CRLF in the clipboard, so we must convert
1070 * any \n that has come out of the puzzle backend.
1073 for (i = 0; data[i]; i++) {
1074 if (data[i] == '\n')
1078 data2 = snewn(len+1, char);
1080 for (i = 0; data[i]; i++) {
1081 if (data[i] == '\n')
1083 data2[j++] = data[i];
1088 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
1091 lock = GlobalLock(clipdata);
1094 memcpy(lock, data2, len);
1095 ((unsigned char *) lock)[len] = 0;
1096 GlobalUnlock(clipdata);
1098 if (OpenClipboard(hwnd)) {
1100 SetClipboardData(CF_TEXT, clipdata);
1103 GlobalFree(clipdata);
1109 * Set up Help and see if we can find a help file.
1111 static void init_help(void)
1114 char b[2048], *p, *q, *r;
1118 * Find the executable file path, so we can look alongside
1119 * it for help files. Trim the filename off the end.
1121 GetModuleFileName(NULL, b, sizeof(b) - 1);
1123 p = strrchr(b, '\\');
1124 if (p && p >= r) r = p+1;
1125 q = strrchr(b, ':');
1126 if (q && q >= r) r = q+1;
1130 * Try HTML Help first.
1132 strcpy(r, CHM_FILE_NAME);
1133 if ( (fp = fopen(b, "r")) != NULL) {
1137 * We have a .CHM. See if we can use it.
1139 hh_dll = LoadLibrary("hhctrl.ocx");
1141 htmlhelp = (htmlhelp_t)GetProcAddress(hh_dll, "HtmlHelpA");
1143 FreeLibrary(hh_dll);
1146 help_path = dupstr(b);
1148 help_topic = thegame.htmlhelp_topic;
1152 #endif /* NO_HTMLHELP */
1155 * Now try old-style .HLP.
1157 strcpy(r, HELP_FILE_NAME);
1158 if ( (fp = fopen(b, "r")) != NULL) {
1161 help_path = dupstr(b);
1164 help_topic = thegame.winhelp_topic;
1167 * See if there's a .CNT file alongside it.
1169 strcpy(r, HELP_CNT_NAME);
1170 if ( (fp = fopen(b, "r")) != NULL) {
1172 help_has_contents = TRUE;
1174 help_has_contents = FALSE;
1179 help_type = NONE; /* didn't find any */
1188 static void start_help(frontend *fe, const char *topic)
1193 switch (help_type) {
1197 str = snewn(10+strlen(topic), char);
1198 sprintf(str, "JI(`',`%s')", topic);
1200 } else if (help_has_contents) {
1203 cmd = HELP_CONTENTS;
1205 WinHelp(fe->hwnd, help_path, cmd, (DWORD)str);
1206 fe->help_running = TRUE;
1213 str = snewn(20 + strlen(topic) + strlen(help_path), char);
1214 sprintf(str, "%s::/%s.html>main", help_path, topic);
1216 str = dupstr(help_path);
1218 htmlhelp(fe->hwnd, str, HH_DISPLAY_TOPIC, 0);
1219 fe->help_running = TRUE;
1221 #endif /* NO_HTMLHELP */
1223 assert(!"This shouldn't happen");
1231 * Stop Help on window cleanup.
1233 static void stop_help(frontend *fe)
1235 if (fe->help_running) {
1236 switch (help_type) {
1238 WinHelp(fe->hwnd, help_path, HELP_QUIT, 0);
1243 htmlhelp(NULL, NULL, HH_CLOSE_ALL, 0);
1245 #endif /* NO_HTMLHELP */
1247 assert(!"This shouldn't happen");
1250 fe->help_running = FALSE;
1257 * Terminate Help on process exit.
1259 static void cleanup_help(void)
1261 /* Nothing to do currently.
1262 * (If we were running HTML Help single-threaded, this is where we'd
1263 * call HH_UNINITIALIZE.) */
1266 static void check_window_size(frontend *fe, int *px, int *py)
1271 if (fe->statusbar) {
1273 GetWindowRect(fe->statusbar, &sr);
1274 sy = sr.bottom - sr.top;
1280 * See if we actually got the window size we wanted, and adjust
1281 * the puzzle size if not.
1283 GetClientRect(fe->hwnd, &r);
1284 x = r.right - r.left;
1285 y = r.bottom - r.top - sy;
1286 midend_size(fe->me, &x, &y, FALSE);
1287 if (x != r.right - r.left || y != r.bottom - r.top) {
1289 * Resize the window, now we know what size we _really_
1295 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1297 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
1298 SWP_NOMOVE | SWP_NOZORDER);
1302 if (fe->statusbar) {
1303 GetClientRect(fe->hwnd, &r);
1305 SetWindowPos(fe->statusbar, NULL, 0, r.bottom-r.top-sy, r.right-r.left,
1314 static void get_max_puzzle_size(frontend *fe, int *x, int *y)
1318 if (SystemParametersInfo(SPI_GETWORKAREA, 0, &sr, FALSE)) {
1319 *x = sr.right - sr.left;
1320 *y = sr.bottom - sr.top;
1325 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1326 *x -= r.right - r.left - 100;
1327 *y -= r.bottom - r.top - 100;
1332 if (fe->statusbar != NULL) {
1333 GetWindowRect(fe->statusbar, &sr);
1334 *y -= sr.bottom - sr.top;
1339 /* Toolbar buttons on the numeric pad */
1340 static TBBUTTON tbNumpadButtons[] =
1342 {0, IDM_KEYEMUL + '1', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1343 {1, IDM_KEYEMUL + '2', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1344 {2, IDM_KEYEMUL + '3', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1345 {3, IDM_KEYEMUL + '4', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1346 {4, IDM_KEYEMUL + '5', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1347 {5, IDM_KEYEMUL + '6', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1348 {6, IDM_KEYEMUL + '7', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1349 {7, IDM_KEYEMUL + '8', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1350 {8, IDM_KEYEMUL + '9', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1},
1351 {9, IDM_KEYEMUL + ' ', TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, -1}
1355 static frontend *new_window(HINSTANCE inst, char *game_id, char **error)
1361 fe = snew(frontend);
1363 fe->me = midend_new(fe, &thegame, &win_drawing, fe);
1366 *error = midend_game_id(fe->me, game_id);
1368 midend_free(fe->me);
1379 fe->help_running = FALSE;
1381 fe->drawstatus = NOTHING;
1385 midend_new_game(fe->me);
1388 fe->nfonts = fe->fontsize = 0;
1394 colours = midend_colours(fe->me, &ncolours);
1396 fe->colours = snewn(ncolours, COLORREF);
1397 fe->brushes = snewn(ncolours, HBRUSH);
1398 fe->pens = snewn(ncolours, HPEN);
1400 for (i = 0; i < ncolours; i++) {
1401 fe->colours[i] = RGB(255 * colours[i*3+0],
1402 255 * colours[i*3+1],
1403 255 * colours[i*3+2]);
1404 fe->brushes[i] = CreateSolidBrush(fe->colours[i]);
1405 fe->pens[i] = CreatePen(PS_SOLID, 1, fe->colours[i]);
1410 if (midend_wants_statusbar(fe->me)) {
1411 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1412 WS_CHILD | WS_VISIBLE,
1413 0, 0, 0, 0, /* status bar does these */
1414 NULL, NULL, inst, NULL);
1416 fe->statusbar = NULL;
1418 get_max_puzzle_size(fe, &x, &y);
1419 midend_size(fe->me, &x, &y, FALSE);
1424 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
1427 fe->hwnd = CreateWindowEx(0, wGameName, wGameName,
1429 CW_USEDEFAULT, CW_USEDEFAULT,
1430 CW_USEDEFAULT, CW_USEDEFAULT,
1431 NULL, NULL, inst, NULL);
1435 RECT rc, rcBar, rcTB, rcClient;
1437 memset (&mbi, 0, sizeof(SHMENUBARINFO));
1438 mbi.cbSize = sizeof(SHMENUBARINFO);
1439 mbi.hwndParent = fe->hwnd;
1440 mbi.nToolBarId = IDR_MENUBAR1;
1441 mbi.hInstRes = inst;
1443 SHCreateMenuBar(&mbi);
1445 GetWindowRect(fe->hwnd, &rc);
1446 GetWindowRect(mbi.hwndMB, &rcBar);
1447 rc.bottom -= rcBar.bottom - rcBar.top;
1448 MoveWindow(fe->hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
1450 if (thegame.flags & REQUIRE_NUMPAD)
1452 fe->numpad = CreateToolbarEx (fe->hwnd,
1453 WS_VISIBLE | WS_CHILD | CCS_NOPARENTALIGN | TBSTYLE_FLAT,
1454 0, 10, inst, IDR_PADTOOLBAR,
1455 tbNumpadButtons, sizeof (tbNumpadButtons) / sizeof (TBBUTTON),
1456 0, 0, 14, 15, sizeof (TBBUTTON));
1457 GetWindowRect(fe->numpad, &rcTB);
1458 GetClientRect(fe->hwnd, &rcClient);
1459 MoveWindow(fe->numpad,
1461 rcClient.bottom - (rcTB.bottom - rcTB.top) - 1,
1463 rcTB.bottom - rcTB.top,
1465 SendMessage(fe->numpad, TB_SETINDENT, (rcClient.right - (10 * 21)) / 2, 0);
1471 fe->hwnd = CreateWindowEx(0, thegame.name, thegame.name,
1472 WS_OVERLAPPEDWINDOW &~
1473 (WS_THICKFRAME | WS_MAXIMIZEBOX),
1474 CW_USEDEFAULT, CW_USEDEFAULT,
1475 r.right - r.left, r.bottom - r.top,
1476 NULL, NULL, inst, NULL);
1479 if (midend_wants_statusbar(fe->me)) {
1481 DestroyWindow(fe->statusbar);
1482 fe->statusbar = CreateWindowEx(0, STATUSCLASSNAME, TEXT("ooh"),
1483 WS_CHILD | WS_VISIBLE,
1484 0, 0, 0, 0, /* status bar does these */
1485 fe->hwnd, NULL, inst, NULL);
1487 /* Flat status bar looks better on the Pocket PC */
1488 SendMessage(fe->statusbar, SB_SIMPLE, (WPARAM) TRUE, 0);
1489 SendMessage(fe->statusbar, SB_SETTEXT,
1490 (WPARAM) 255 | SBT_NOBORDERS,
1495 * Now resize the window to take account of the status bar.
1497 GetWindowRect(fe->statusbar, &sr);
1498 GetWindowRect(fe->hwnd, &r);
1500 SetWindowPos(fe->hwnd, NULL, 0, 0, r.right - r.left,
1501 r.bottom - r.top + sr.bottom - sr.top,
1502 SWP_NOMOVE | SWP_NOZORDER);
1505 fe->statusbar = NULL;
1510 HMENU bar = CreateMenu();
1511 HMENU menu = CreateMenu();
1513 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, "Game");
1515 HMENU menu = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_GAME);
1516 DeleteMenu(menu, 0, MF_BYPOSITION);
1518 AppendMenu(menu, MF_ENABLED, IDM_NEW, TEXT("New"));
1519 AppendMenu(menu, MF_ENABLED, IDM_RESTART, TEXT("Restart"));
1521 AppendMenu(menu, MF_ENABLED, IDM_DESC, TEXT("Specific..."));
1522 AppendMenu(menu, MF_ENABLED, IDM_SEED, TEXT("Random Seed..."));
1525 if ((fe->npresets = midend_num_presets(fe->me)) > 0 ||
1526 thegame.can_configure) {
1529 HMENU sub = CreateMenu();
1531 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)sub, "Type");
1533 HMENU sub = SHGetSubMenu(SHFindMenuBar(fe->hwnd), ID_TYPE);
1534 DeleteMenu(sub, 0, MF_BYPOSITION);
1536 fe->presets = snewn(fe->npresets, game_params *);
1538 for (i = 0; i < fe->npresets; i++) {
1544 midend_fetch_preset(fe->me, i, &name, &fe->presets[i]);
1547 * FIXME: we ought to go through and do something
1548 * with ampersands here.
1552 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, name);
1554 MultiByteToWideChar (CP_ACP, 0, name, -1, wName, 255);
1555 AppendMenu(sub, MF_ENABLED, IDM_PRESETS + 0x10 * i, wName);
1558 if (thegame.can_configure) {
1559 AppendMenu(sub, MF_ENABLED, IDM_CONFIG, TEXT("Custom..."));
1563 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1565 AppendMenu(menu, MF_ENABLED, IDM_LOAD, TEXT("Load..."));
1566 AppendMenu(menu, MF_ENABLED, IDM_SAVE, TEXT("Save..."));
1567 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1568 if (thegame.can_print) {
1569 AppendMenu(menu, MF_ENABLED, IDM_PRINT, TEXT("Print..."));
1570 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1573 AppendMenu(menu, MF_ENABLED, IDM_UNDO, TEXT("Undo"));
1574 AppendMenu(menu, MF_ENABLED, IDM_REDO, TEXT("Redo"));
1576 if (thegame.can_format_as_text) {
1577 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1578 AppendMenu(menu, MF_ENABLED, IDM_COPY, TEXT("Copy"));
1581 if (thegame.can_solve) {
1582 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1583 AppendMenu(menu, MF_ENABLED, IDM_SOLVE, TEXT("Solve"));
1585 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1587 AppendMenu(menu, MF_ENABLED, IDM_QUIT, TEXT("Exit"));
1588 menu = CreateMenu();
1589 AppendMenu(bar, MF_ENABLED|MF_POPUP, (UINT)menu, TEXT("Help"));
1591 AppendMenu(menu, MF_ENABLED, IDM_ABOUT, TEXT("About"));
1593 if (help_type != NONE) {
1594 AppendMenu(menu, MF_SEPARATOR, 0, 0);
1595 AppendMenu(menu, MF_ENABLED, IDM_HELPC, TEXT("Contents"));
1598 assert(thegame.name);
1599 item = snewn(9+strlen(thegame.name), char); /*ick*/
1600 sprintf(item, "Help on %s", thegame.name);
1601 AppendMenu(menu, MF_ENABLED, IDM_GAMEHELP, item);
1605 SetMenu(fe->hwnd, bar);
1610 new_game_size(fe); /* initialises fe->bitmap */
1611 check_window_size(fe, &x, &y);
1613 SetWindowLong(fe->hwnd, GWL_USERDATA, (LONG)fe);
1615 ShowWindow(fe->hwnd, SW_SHOWNORMAL);
1616 SetForegroundWindow(fe->hwnd);
1618 midend_redraw(fe->me);
1624 static HFONT dialog_title_font()
1626 static HFONT hf = NULL;
1632 memset (&lf, 0, sizeof(LOGFONT));
1633 lf.lfHeight = -11; /* - ((8 * GetDeviceCaps(hdc, LOGPIXELSY)) / 72) */
1634 lf.lfWeight = FW_BOLD;
1635 wcscpy(lf.lfFaceName, TEXT("Tahoma"));
1637 return hf = CreateFontIndirect(&lf);
1640 static void make_dialog_full_screen(HWND hwnd)
1642 SHINITDLGINFO shidi;
1644 /* Make dialog full screen */
1645 shidi.dwMask = SHIDIM_FLAGS;
1646 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN |
1649 SHInitDialog(&shidi);
1653 static int CALLBACK AboutDlgProc(HWND hwnd, UINT msg,
1654 WPARAM wParam, LPARAM lParam)
1656 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1664 make_dialog_full_screen(hwnd);
1666 sprintf(title, "About %.250s", thegame.name);
1667 SetDlgItemTextA(hwnd, IDC_ABOUT_CAPTION, title);
1669 SendDlgItemMessage(hwnd, IDC_ABOUT_CAPTION, WM_SETFONT,
1670 (WPARAM) dialog_title_font(), 0);
1672 SetDlgItemTextA(hwnd, IDC_ABOUT_GAME, thegame.name);
1673 SetDlgItemTextA(hwnd, IDC_ABOUT_VERSION, ver);
1679 if (LOWORD(wParam) == IDOK)
1700 * Wrappers on midend_{get,set}_config, which extend the CFG_*
1701 * enumeration to add CFG_PRINT.
1703 static config_item *frontend_get_config(frontend *fe, int which,
1706 if (which < CFG_FRONTEND_SPECIFIC) {
1707 return midend_get_config(fe->me, which, wintitle);
1708 } else if (which == CFG_PRINT) {
1712 *wintitle = snewn(40 + strlen(thegame.name), char);
1713 sprintf(*wintitle, "%s print setup", thegame.name);
1715 ret = snewn(8, config_item);
1719 ret[i].name = "Number of puzzles to print";
1720 ret[i].type = C_STRING;
1721 ret[i].sval = dupstr("1");
1725 ret[i].name = "Number of puzzles across the page";
1726 ret[i].type = C_STRING;
1727 ret[i].sval = dupstr("1");
1731 ret[i].name = "Number of puzzles down the page";
1732 ret[i].type = C_STRING;
1733 ret[i].sval = dupstr("1");
1737 ret[i].name = "Percentage of standard size";
1738 ret[i].type = C_STRING;
1739 ret[i].sval = dupstr("100.0");
1743 ret[i].name = "Include currently shown puzzle";
1744 ret[i].type = C_BOOLEAN;
1749 ret[i].name = "Print solutions";
1750 ret[i].type = C_BOOLEAN;
1752 ret[i].ival = FALSE;
1755 if (thegame.can_print_in_colour) {
1756 ret[i].name = "Print in colour";
1757 ret[i].type = C_BOOLEAN;
1759 ret[i].ival = FALSE;
1764 ret[i].type = C_END;
1771 assert(!"We should never get here");
1776 static char *frontend_set_config(frontend *fe, int which, config_item *cfg)
1778 if (which < CFG_FRONTEND_SPECIFIC) {
1779 return midend_set_config(fe->me, which, cfg);
1780 } else if (which == CFG_PRINT) {
1781 if ((fe->printcount = atoi(cfg[0].sval)) <= 0)
1782 return "Number of puzzles to print should be at least one";
1783 if ((fe->printw = atoi(cfg[1].sval)) <= 0)
1784 return "Number of puzzles across the page should be at least one";
1785 if ((fe->printh = atoi(cfg[2].sval)) <= 0)
1786 return "Number of puzzles down the page should be at least one";
1787 if ((fe->printscale = (float)atof(cfg[3].sval)) <= 0)
1788 return "Print size should be positive";
1789 fe->printcurr = cfg[4].ival;
1790 fe->printsolns = cfg[5].ival;
1791 fe->printcolour = thegame.can_print_in_colour && cfg[6].ival;
1794 assert(!"We should never get here");
1795 return "Internal error";
1800 /* Separate version of mkctrl function for the Pocket PC. */
1801 /* Control coordinates should be specified in dialog units. */
1802 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
1803 LPCTSTR wclass, int wstyle,
1804 int exstyle, const char *wtext, int wid)
1809 /* Convert dialog units into pixels */
1810 rc.left = x1; rc.right = x2;
1811 rc.top = y1; rc.bottom = y2;
1812 MapDialogRect(fe->cfgbox, &rc);
1814 MultiByteToWideChar (CP_ACP, 0, wtext, -1, wwtext, 256);
1816 return CreateWindowEx(exstyle, wclass, wwtext,
1817 wstyle | WS_CHILD | WS_VISIBLE,
1819 rc.right - rc.left, rc.bottom - rc.top,
1820 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
1823 static void create_config_controls(frontend * fe)
1826 int col1l, col1r, col2l, col2r, y;
1831 /* Control placement done in dialog units */
1832 col1l = 4; col1r = 96; /* Label column */
1833 col2l = 100; col2r = 154; /* Input column (edit boxes and combo boxes) */
1836 * Count the controls so we can allocate cfgaux.
1838 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
1840 fe->cfgaux = snewn(nctrls, struct cfg_aux);
1843 y = 22; /* Leave some room for the dialog title */
1844 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1848 * Edit box with a label beside it.
1850 mkctrl(fe, col1l, col1r, y + 1, y + 11,
1851 TEXT("Static"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
1852 mkctrl(fe, col2l, col2r, y, y + 12,
1853 TEXT("EDIT"), WS_BORDER | WS_TABSTOP | ES_AUTOHSCROLL,
1854 0, "", (j->ctlid = id++));
1855 SetDlgItemTextA(fe->cfgbox, j->ctlid, i->sval);
1862 mkctrl(fe, col1l, col2r, y + 1, y + 11, TEXT("BUTTON"),
1863 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
1864 0, i->name, (j->ctlid = id++));
1865 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
1870 * Drop-down list with a label beside it.
1872 mkctrl(fe, col1l, col1r, y + 1, y + 11,
1873 TEXT("STATIC"), SS_LEFTNOWORDWRAP, 0, i->name, id++);
1874 ctl = mkctrl(fe, col2l, col2r, y, y + 48,
1875 TEXT("COMBOBOX"), WS_BORDER | WS_TABSTOP |
1876 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
1877 0, "", (j->ctlid = id++));
1879 char c, *p, *q, *str;
1885 while (*q && *q != c) q++;
1886 str = snewn(q-p+1, char);
1887 strncpy(str, p, q-p);
1891 MultiByteToWideChar (CP_ACP, 0, str, -1, ws, 50);
1892 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)ws);
1900 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
1910 static int CALLBACK ConfigDlgProc(HWND hwnd, UINT msg,
1911 WPARAM wParam, LPARAM lParam)
1913 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
1923 fe = (frontend *) lParam;
1924 SetWindowLong(hwnd, GWL_USERDATA, lParam);
1927 fe->cfg = frontend_get_config(fe, fe->cfg_which, &title);
1929 make_dialog_full_screen(hwnd);
1931 SetDlgItemTextA(hwnd, IDC_CONFIG_CAPTION, title);
1932 SendDlgItemMessage(hwnd, IDC_CONFIG_CAPTION, WM_SETFONT,
1933 (WPARAM) dialog_title_font(), 0);
1935 create_config_controls(fe);
1942 * OK and Cancel are special cases.
1944 if ((LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)) {
1945 if (LOWORD(wParam) == IDOK) {
1946 char *err = frontend_set_config(fe, fe->cfg_which, fe->cfg);
1949 MessageBox(hwnd, err, "Validation error",
1950 MB_ICONERROR | MB_OK);
1969 * First find the control whose id this is.
1971 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
1972 if (j->ctlid == LOWORD(wParam))
1975 if (i->type == C_END)
1976 return 0; /* not our problem */
1978 if (i->type == C_STRING && HIWORD(wParam) == EN_CHANGE) {
1981 TCHAR wBuffer[4096];
1982 GetDlgItemText(fe->cfgbox, j->ctlid, wBuffer, 4096);
1983 WideCharToMultiByte(CP_ACP, 0, wBuffer, -1, buffer, 4096, NULL, NULL);
1985 GetDlgItemText(fe->cfgbox, j->ctlid, buffer, lenof(buffer));
1987 buffer[lenof(buffer)-1] = '\0';
1989 i->sval = dupstr(buffer);
1990 } else if (i->type == C_BOOLEAN &&
1991 (HIWORD(wParam) == BN_CLICKED ||
1992 HIWORD(wParam) == BN_DBLCLK)) {
1993 i->ival = IsDlgButtonChecked(fe->cfgbox, j->ctlid);
1994 } else if (i->type == C_CHOICES &&
1995 HIWORD(wParam) == CBN_SELCHANGE) {
1996 i->ival = SendDlgItemMessage(fe->cfgbox, j->ctlid,
1997 CB_GETCURSEL, 0, 0);
2011 HWND mkctrl(frontend *fe, int x1, int x2, int y1, int y2,
2012 char *wclass, int wstyle,
2013 int exstyle, const char *wtext, int wid)
2016 ret = CreateWindowEx(exstyle, wclass, wtext,
2017 wstyle | WS_CHILD | WS_VISIBLE, x1, y1, x2-x1, y2-y1,
2018 fe->cfgbox, (HMENU) wid, fe->inst, NULL);
2019 SendMessage(ret, WM_SETFONT, (WPARAM)fe->cfgfont, MAKELPARAM(TRUE, 0));
2024 static void about(frontend *fe)
2027 DialogBox(fe->inst, MAKEINTRESOURCE(IDD_ABOUT), fe->hwnd, AboutDlgProc);
2037 int winwidth, winheight, y;
2038 int height, width, maxwid;
2039 const char *strings[16];
2044 sprintf(titlebuf, "About %.250s", thegame.name);
2046 strings[nstrings++] = thegame.name;
2047 strings[nstrings++] = "from Simon Tatham's Portable Puzzle Collection";
2048 strings[nstrings++] = ver;
2050 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2051 wc.lpfnWndProc = DefDlgProc;
2053 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2054 wc.hInstance = fe->inst;
2056 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2057 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2058 wc.lpszMenuName = NULL;
2059 wc.lpszClassName = "GameAboutBox";
2062 hdc = GetDC(fe->hwnd);
2063 SetMapMode(hdc, MM_TEXT);
2065 fe->dlg_done = FALSE;
2067 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2069 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2070 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2075 oldfont = SelectObject(hdc, fe->cfgfont);
2076 if (GetTextMetrics(hdc, &tm)) {
2077 height = tm.tmAscent + tm.tmDescent;
2078 width = tm.tmAveCharWidth;
2080 height = width = 30;
2084 * Figure out the layout of the About box by measuring the
2085 * length of each piece of text.
2088 winheight = height/2;
2090 for (i = 0; i < nstrings; i++) {
2091 if (GetTextExtentPoint32(hdc, strings[i], strlen(strings[i]), &size))
2092 lengths[i] = size.cx;
2094 lengths[i] = 0; /* *shrug* */
2095 if (maxwid < lengths[i])
2096 maxwid = lengths[i];
2097 winheight += height * 3 / 2 + (height / 2);
2100 winheight += height + height * 7 / 4; /* OK button */
2101 winwidth = maxwid + 4*width;
2103 SelectObject(hdc, oldfont);
2104 ReleaseDC(fe->hwnd, hdc);
2107 * Create the dialog, now that we know its size.
2114 r.bottom = winheight;
2116 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2117 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2118 WS_CAPTION | WS_SYSMENU*/) &~
2119 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2123 * Centre the dialog on its parent window.
2127 GetWindowRect(fe->hwnd, &r2);
2128 r.left = (r2.left + r2.right - r.right) / 2;
2129 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2133 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, titlebuf,
2134 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2135 WS_CAPTION | WS_SYSMENU,
2137 r.right-r.left, r.bottom-r.top,
2138 fe->hwnd, NULL, fe->inst, NULL);
2141 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2143 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
2144 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)AboutDlgProc);
2148 for (i = 0; i < nstrings; i++) {
2149 int border = width*2 + (maxwid - lengths[i]) / 2;
2150 mkctrl(fe, border, border+lengths[i], y+height*1/8, y+height*9/8,
2151 "Static", 0, 0, strings[i], id++);
2154 assert(y < winheight);
2158 y += height/2; /* extra space before OK */
2159 mkctrl(fe, width*2, maxwid+width*2, y, y+height*7/4, "BUTTON",
2160 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2163 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2165 EnableWindow(fe->hwnd, FALSE);
2166 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2167 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2168 if (!IsDialogMessage(fe->cfgbox, &msg))
2169 DispatchMessage(&msg);
2173 EnableWindow(fe->hwnd, TRUE);
2174 SetForegroundWindow(fe->hwnd);
2175 DestroyWindow(fe->cfgbox);
2176 DeleteObject(fe->cfgfont);
2180 static int get_config(frontend *fe, int which)
2183 fe->cfg_which = which;
2185 return DialogBoxParam(fe->inst,
2186 MAKEINTRESOURCE(IDD_CONFIG),
2187 fe->hwnd, ConfigDlgProc,
2201 int winwidth, winheight, col1l, col1r, col2l, col2r, y;
2202 int height, width, maxlabel, maxcheckbox;
2204 wc.style = CS_DBLCLKS | CS_SAVEBITS;
2205 wc.lpfnWndProc = DefDlgProc;
2207 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
2208 wc.hInstance = fe->inst;
2210 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
2211 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
2212 wc.lpszMenuName = NULL;
2213 wc.lpszClassName = "GameConfigBox";
2216 hdc = GetDC(fe->hwnd);
2217 SetMapMode(hdc, MM_TEXT);
2219 fe->dlg_done = FALSE;
2221 fe->cfgfont = CreateFont(-MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72),
2223 FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2224 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2229 oldfont = SelectObject(hdc, fe->cfgfont);
2230 if (GetTextMetrics(hdc, &tm)) {
2231 height = tm.tmAscent + tm.tmDescent;
2232 width = tm.tmAveCharWidth;
2234 height = width = 30;
2237 fe->cfg = frontend_get_config(fe, which, &title);
2238 fe->cfg_which = which;
2241 * Figure out the layout of the config box by measuring the
2242 * length of each piece of text.
2244 maxlabel = maxcheckbox = 0;
2245 winheight = height/2;
2247 for (i = fe->cfg; i->type != C_END; i++) {
2252 * Both these control types have a label filling only
2253 * the left-hand column of the box.
2255 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2258 winheight += height * 3 / 2 + (height / 2);
2263 * Checkboxes take up the whole of the box width.
2265 if (GetTextExtentPoint32(hdc, i->name, strlen(i->name), &size) &&
2266 maxcheckbox < size.cx)
2267 maxcheckbox = size.cx;
2268 winheight += height + (height / 2);
2273 winheight += height + height * 7 / 4; /* OK / Cancel buttons */
2276 col1r = col1l + maxlabel;
2277 col2l = col1r + 2*width;
2278 col2r = col2l + 30*width;
2279 if (col2r < col1l+2*height+maxcheckbox)
2280 col2r = col1l+2*height+maxcheckbox;
2281 winwidth = col2r + 2*width;
2283 SelectObject(hdc, oldfont);
2284 ReleaseDC(fe->hwnd, hdc);
2287 * Create the dialog, now that we know its size.
2294 r.bottom = winheight;
2296 AdjustWindowRectEx(&r, (WS_OVERLAPPEDWINDOW /*|
2297 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2298 WS_CAPTION | WS_SYSMENU*/) &~
2299 (WS_MAXIMIZEBOX | WS_OVERLAPPED),
2303 * Centre the dialog on its parent window.
2307 GetWindowRect(fe->hwnd, &r2);
2308 r.left = (r2.left + r2.right - r.right) / 2;
2309 r.top = (r2.top + r2.bottom - r.bottom) / 2;
2313 fe->cfgbox = CreateWindowEx(0, wc.lpszClassName, title,
2314 DS_MODALFRAME | WS_POPUP | WS_VISIBLE |
2315 WS_CAPTION | WS_SYSMENU,
2317 r.right-r.left, r.bottom-r.top,
2318 fe->hwnd, NULL, fe->inst, NULL);
2322 SendMessage(fe->cfgbox, WM_SETFONT, (WPARAM)fe->cfgfont, FALSE);
2324 SetWindowLong(fe->cfgbox, GWL_USERDATA, (LONG)fe);
2325 SetWindowLong(fe->cfgbox, DWL_DLGPROC, (LONG)ConfigDlgProc);
2328 * Count the controls so we can allocate cfgaux.
2330 for (nctrls = 0, i = fe->cfg; i->type != C_END; i++)
2332 fe->cfgaux = snewn(nctrls, struct cfg_aux);
2336 for (i = fe->cfg, j = fe->cfgaux; i->type != C_END; i++, j++) {
2340 * Edit box with a label beside it.
2342 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2343 "Static", 0, 0, i->name, id++);
2344 ctl = mkctrl(fe, col2l, col2r, y, y+height*3/2,
2345 "EDIT", WS_TABSTOP | ES_AUTOHSCROLL,
2346 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2347 SetWindowText(ctl, i->sval);
2355 mkctrl(fe, col1l, col2r, y, y+height, "BUTTON",
2356 BS_NOTIFY | BS_AUTOCHECKBOX | WS_TABSTOP,
2357 0, i->name, (j->ctlid = id++));
2358 CheckDlgButton(fe->cfgbox, j->ctlid, (i->ival != 0));
2364 * Drop-down list with a label beside it.
2366 mkctrl(fe, col1l, col1r, y+height*1/8, y+height*9/8,
2367 "STATIC", 0, 0, i->name, id++);
2368 ctl = mkctrl(fe, col2l, col2r, y, y+height*41/2,
2369 "COMBOBOX", WS_TABSTOP |
2370 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
2371 WS_EX_CLIENTEDGE, "", (j->ctlid = id++));
2373 char c, *p, *q, *str;
2375 SendMessage(ctl, CB_RESETCONTENT, 0, 0);
2380 while (*q && *q != c) q++;
2381 str = snewn(q-p+1, char);
2382 strncpy(str, p, q-p);
2384 SendMessage(ctl, CB_ADDSTRING, 0, (LPARAM)str);
2391 SendMessage(ctl, CB_SETCURSEL, i->ival, 0);
2397 assert(y < winheight);
2401 y += height/2; /* extra space before OK and Cancel */
2402 mkctrl(fe, col1l, (col1l+col2r)/2-width, y, y+height*7/4, "BUTTON",
2403 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP | BS_DEFPUSHBUTTON, 0,
2405 mkctrl(fe, (col1l+col2r)/2+width, col2r, y, y+height*7/4, "BUTTON",
2406 BS_PUSHBUTTON | BS_NOTIFY | WS_TABSTOP, 0, "Cancel", IDCANCEL);
2408 SendMessage(fe->cfgbox, WM_INITDIALOG, 0, 0);
2410 EnableWindow(fe->hwnd, FALSE);
2411 ShowWindow(fe->cfgbox, SW_SHOWNORMAL);
2412 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
2413 if (!IsDialogMessage(fe->cfgbox, &msg))
2414 DispatchMessage(&msg);
2418 EnableWindow(fe->hwnd, TRUE);
2419 SetForegroundWindow(fe->hwnd);
2420 DestroyWindow(fe->cfgbox);
2421 DeleteObject(fe->cfgfont);
2426 return (fe->dlg_done == 2);
2431 static void calculate_bitmap_position(frontend *fe, int x, int y)
2433 /* Pocket PC - center the game in the full screen window */
2437 GetClientRect(fe->hwnd, &rcClient);
2438 fe->bitmapPosition.left = (rcClient.right - x) / 2;
2439 yMargin = rcClient.bottom - y;
2441 if (fe->numpad != NULL) {
2443 GetWindowRect(fe->numpad, &rcPad);
2444 yMargin -= rcPad.bottom - rcPad.top;
2447 if (fe->statusbar != NULL) {
2449 GetWindowRect(fe->statusbar, &rcStatus);
2450 yMargin -= rcStatus.bottom - rcStatus.top;
2453 fe->bitmapPosition.top = yMargin / 2;
2455 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2456 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2459 static void calculate_bitmap_position(frontend *fe, int x, int y)
2461 /* Plain Windows - position the game in the upper-left corner */
2462 fe->bitmapPosition.left = 0;
2463 fe->bitmapPosition.top = 0;
2464 fe->bitmapPosition.right = fe->bitmapPosition.left + x;
2465 fe->bitmapPosition.bottom = fe->bitmapPosition.top + y;
2469 static void new_game_size(frontend *fe)
2475 get_max_puzzle_size(fe, &x, &y);
2476 midend_size(fe->me, &x, &y, FALSE);
2481 AdjustWindowRectEx(&r, WINFLAGS, TRUE, 0);
2483 if (fe->statusbar != NULL) {
2484 GetWindowRect(fe->statusbar, &sr);
2486 sr.left = sr.right = sr.top = sr.bottom = 0;
2489 SetWindowPos(fe->hwnd, NULL, 0, 0,
2491 r.bottom - r.top + sr.bottom - sr.top,
2492 SWP_NOMOVE | SWP_NOZORDER);
2495 check_window_size(fe, &x, &y);
2498 if (fe->statusbar != NULL)
2499 SetWindowPos(fe->statusbar, NULL, 0, y, x,
2500 sr.bottom - sr.top, SWP_NOZORDER);
2503 if (fe->bitmap) DeleteObject(fe->bitmap);
2505 hdc = GetDC(fe->hwnd);
2506 fe->bitmap = CreateCompatibleBitmap(hdc, x, y);
2507 calculate_bitmap_position(fe, x, y);
2508 ReleaseDC(fe->hwnd, hdc);
2511 InvalidateRect(fe->hwnd, NULL, TRUE);
2513 midend_redraw(fe->me);
2516 static void new_game_type(frontend *fe)
2518 midend_new_game(fe->me);
2522 static int is_alt_pressed(void)
2525 int r = GetKeyboardState(keystate);
2528 if (keystate[VK_MENU] & 0x80)
2530 if (keystate[VK_RMENU] & 0x80)
2535 static void savefile_write(void *wctx, void *buf, int len)
2537 FILE *fp = (FILE *)wctx;
2538 fwrite(buf, 1, len, fp);
2541 static int savefile_read(void *wctx, void *buf, int len)
2543 FILE *fp = (FILE *)wctx;
2546 ret = fread(buf, 1, len, fp);
2547 return (ret == len);
2550 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2551 WPARAM wParam, LPARAM lParam)
2553 frontend *fe = (frontend *)GetWindowLong(hwnd, GWL_USERDATA);
2558 DestroyWindow(hwnd);
2562 /* Numeric pad sends WM_COMMAND messages */
2563 if ((wParam >= IDM_KEYEMUL) && (wParam < IDM_KEYEMUL + 256))
2565 midend_process_key(fe->me, 0, 0, wParam - IDM_KEYEMUL);
2568 cmd = wParam & ~0xF; /* low 4 bits reserved to Windows */
2571 if (!midend_process_key(fe->me, 0, 0, 'n'))
2575 midend_restart_game(fe->me);
2578 if (!midend_process_key(fe->me, 0, 0, 'u'))
2582 if (!midend_process_key(fe->me, 0, 0, '\x12'))
2587 char *text = midend_text_format(fe->me);
2589 write_clip(hwnd, text);
2591 MessageBeep(MB_ICONWARNING);
2597 char *msg = midend_solve(fe->me);
2599 MessageBox(hwnd, msg, "Unable to solve",
2600 MB_ICONERROR | MB_OK);
2604 if (!midend_process_key(fe->me, 0, 0, 'q'))
2608 if (get_config(fe, CFG_SETTINGS))
2612 if (get_config(fe, CFG_SEED))
2616 if (get_config(fe, CFG_DESC))
2620 if (get_config(fe, CFG_PRINT))
2630 char filename[FILENAME_MAX];
2633 memset(&of, 0, sizeof(of));
2634 of.hwndOwner = hwnd;
2635 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
2636 of.lpstrCustomFilter = NULL;
2637 of.nFilterIndex = 1;
2638 of.lpstrFile = filename;
2640 of.nMaxFile = lenof(filename);
2641 of.lpstrFileTitle = NULL;
2642 of.lpstrTitle = (cmd == IDM_SAVE ?
2643 "Enter name of game file to save" :
2644 "Enter name of saved game file to load");
2646 #ifdef OPENFILENAME_SIZE_VERSION_400
2647 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
2649 of.lStructSize = sizeof(of);
2651 of.lpstrInitialDir = NULL;
2653 if (cmd == IDM_SAVE)
2654 ret = GetSaveFileName(&of);
2656 ret = GetOpenFileName(&of);
2659 if (cmd == IDM_SAVE) {
2662 if ((fp = fopen(filename, "r")) != NULL) {
2663 char buf[256 + FILENAME_MAX];
2667 sprintf(buf, "Are you sure you want to overwrite"
2668 " the file \"%.*s\"?",
2669 FILENAME_MAX, filename);
2670 if (MessageBox(hwnd, buf, "Question",
2671 MB_YESNO | MB_ICONQUESTION)
2676 fp = fopen(filename, "w");
2679 MessageBox(hwnd, "Unable to open save file",
2680 "Error", MB_ICONERROR | MB_OK);
2684 midend_serialise(fe->me, savefile_write, fp);
2688 FILE *fp = fopen(filename, "r");
2692 MessageBox(hwnd, "Unable to open saved game file",
2693 "Error", MB_ICONERROR | MB_OK);
2697 err = midend_deserialise(fe->me, savefile_read, fp);
2702 MessageBox(hwnd, err, "Error", MB_ICONERROR|MB_OK);
2714 start_help(fe, NULL);
2717 start_help(fe, help_topic);
2722 int p = ((wParam &~ 0xF) - IDM_PRESETS) / 0x10;
2724 if (p >= 0 && p < fe->npresets) {
2725 midend_set_params(fe->me, fe->presets[p]);
2745 hdc = BeginPaint(hwnd, &p);
2746 hdc2 = CreateCompatibleDC(hdc);
2747 prevbm = SelectObject(hdc2, fe->bitmap);
2749 FillRect(hdc, &(p.rcPaint), (HBRUSH) GetStockObject(WHITE_BRUSH));
2751 IntersectRect(&rcDest, &(fe->bitmapPosition), &(p.rcPaint));
2753 rcDest.left, rcDest.top,
2754 rcDest.right - rcDest.left,
2755 rcDest.bottom - rcDest.top,
2757 rcDest.left - fe->bitmapPosition.left,
2758 rcDest.top - fe->bitmapPosition.top,
2760 SelectObject(hdc2, prevbm);
2769 int r = GetKeyboardState(keystate);
2770 int shift = (r && (keystate[VK_SHIFT] & 0x80)) ? MOD_SHFT : 0;
2771 int ctrl = (r && (keystate[VK_CONTROL] & 0x80)) ? MOD_CTRL : 0;
2775 if (!(lParam & 0x01000000))
2776 key = MOD_NUM_KEYPAD | '4';
2778 key = shift | ctrl | CURSOR_LEFT;
2781 if (!(lParam & 0x01000000))
2782 key = MOD_NUM_KEYPAD | '6';
2784 key = shift | ctrl | CURSOR_RIGHT;
2787 if (!(lParam & 0x01000000))
2788 key = MOD_NUM_KEYPAD | '8';
2790 key = shift | ctrl | CURSOR_UP;
2793 if (!(lParam & 0x01000000))
2794 key = MOD_NUM_KEYPAD | '2';
2796 key = shift | ctrl | CURSOR_DOWN;
2799 * Diagonal keys on the numeric keypad.
2802 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '9';
2805 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '3';
2808 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '7';
2811 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '1';
2814 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '0';
2817 if (!(lParam & 0x01000000)) key = MOD_NUM_KEYPAD | '5';
2820 * Numeric keypad keys with Num Lock on.
2822 case VK_NUMPAD4: key = MOD_NUM_KEYPAD | '4'; break;
2823 case VK_NUMPAD6: key = MOD_NUM_KEYPAD | '6'; break;
2824 case VK_NUMPAD8: key = MOD_NUM_KEYPAD | '8'; break;
2825 case VK_NUMPAD2: key = MOD_NUM_KEYPAD | '2'; break;
2826 case VK_NUMPAD5: key = MOD_NUM_KEYPAD | '5'; break;
2827 case VK_NUMPAD9: key = MOD_NUM_KEYPAD | '9'; break;
2828 case VK_NUMPAD3: key = MOD_NUM_KEYPAD | '3'; break;
2829 case VK_NUMPAD7: key = MOD_NUM_KEYPAD | '7'; break;
2830 case VK_NUMPAD1: key = MOD_NUM_KEYPAD | '1'; break;
2831 case VK_NUMPAD0: key = MOD_NUM_KEYPAD | '0'; break;
2835 if (!midend_process_key(fe->me, 0, 0, key))
2840 m.message = WM_KEYDOWN;
2842 m.lParam = lParam & 0xdfff;
2843 TranslateMessage(&m);
2847 case WM_LBUTTONDOWN:
2848 case WM_RBUTTONDOWN:
2849 case WM_MBUTTONDOWN:
2854 * Shift-clicks count as middle-clicks, since otherwise
2855 * two-button Windows users won't have any kind of
2856 * middle click to use.
2858 if (message == WM_MBUTTONDOWN || (wParam & MK_SHIFT))
2859 button = MIDDLE_BUTTON;
2860 else if (message == WM_RBUTTONDOWN || is_alt_pressed())
2861 button = RIGHT_BUTTON;
2864 button = LEFT_BUTTON;
2866 if ((thegame.flags & REQUIRE_RBUTTON) == 0)
2867 button = LEFT_BUTTON;
2872 shrgi.cbSize = sizeof(SHRGINFO);
2873 shrgi.hwndClient = hwnd;
2874 shrgi.ptDown.x = (signed short)LOWORD(lParam);
2875 shrgi.ptDown.y = (signed short)HIWORD(lParam);
2876 shrgi.dwFlags = SHRG_RETURNCMD;
2878 if (GN_CONTEXTMENU == SHRecognizeGesture(&shrgi))
2879 button = RIGHT_BUTTON;
2881 button = LEFT_BUTTON;
2885 if (!midend_process_key(fe->me,
2886 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
2887 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
2901 * Shift-clicks count as middle-clicks, since otherwise
2902 * two-button Windows users won't have any kind of
2903 * middle click to use.
2905 if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
2906 button = MIDDLE_RELEASE;
2907 else if (message == WM_RBUTTONUP || is_alt_pressed())
2908 button = RIGHT_RELEASE;
2910 button = LEFT_RELEASE;
2912 if (!midend_process_key(fe->me,
2913 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
2914 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
2925 if (wParam & (MK_MBUTTON | MK_SHIFT))
2926 button = MIDDLE_DRAG;
2927 else if (wParam & MK_RBUTTON || is_alt_pressed())
2928 button = RIGHT_DRAG;
2932 if (!midend_process_key(fe->me,
2933 (signed short)LOWORD(lParam) - fe->bitmapPosition.left,
2934 (signed short)HIWORD(lParam) - fe->bitmapPosition.top,
2940 if (!midend_process_key(fe->me, 0, 0, (unsigned char)wParam))
2945 DWORD now = GetTickCount();
2946 float elapsed = (float) (now - fe->timer_last_tickcount) * 0.001F;
2947 midend_timer(fe->me, elapsed);
2948 fe->timer_last_tickcount = now;
2953 return DefWindowProc(hwnd, message, wParam, lParam);
2957 static int FindPreviousInstance()
2959 /* Check if application is running. If it's running then focus on the window */
2960 HWND hOtherWnd = NULL;
2962 hOtherWnd = FindWindow (wGameName, wGameName);
2965 SetForegroundWindow (hOtherWnd);
2973 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
2979 MultiByteToWideChar (CP_ACP, 0, thegame.name, -1, wGameName, 256);
2980 if (FindPreviousInstance ())
2984 InitCommonControls();
2990 wndclass.lpfnWndProc = WndProc;
2991 wndclass.cbClsExtra = 0;
2992 wndclass.cbWndExtra = 0;
2993 wndclass.hInstance = inst;
2994 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(200));
2996 if (!wndclass.hIcon) /* in case resource file is absent */
2997 wndclass.hIcon = LoadIcon(inst, IDI_APPLICATION);
2999 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
3000 wndclass.hbrBackground = NULL;
3001 wndclass.lpszMenuName = NULL;
3003 wndclass.lpszClassName = wGameName;
3005 wndclass.lpszClassName = thegame.name;
3008 RegisterClass(&wndclass);
3011 while (*cmdline && isspace((unsigned char)*cmdline))
3016 if (!new_window(inst, *cmdline ? cmdline : NULL, &error)) {
3018 sprintf(buf, "%.100s Error", thegame.name);
3019 MessageBox(NULL, error, buf, MB_OK|MB_ICONERROR);
3023 while (GetMessage(&msg, NULL, 0, 0)) {
3024 DispatchMessage(&msg);