OSDN Git Service

upgrade to 3.6.2
[jnethack/source.git] / win / win32 / mhsplash.c
1 /* NetHack 3.6  mhsplash.c      $NHDT-Date: 1449751714 2015/12/10 12:48:34 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.27 $ */
2 /* Copyright (C) 2001 by Alex Kompel     */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 #include "win10.h"
6 #include "winMS.h"
7 #include "resource.h"
8 #include "mhsplash.h"
9 #include "mhmsg.h"
10 #include "mhfont.h"
11 #include "date.h"
12 #include "patchlevel.h"
13 #include "dlb.h"
14
15 #define LLEN 128
16
17 PNHWinApp GetNHApp(void);
18
19 INT_PTR CALLBACK NHSplashWndProc(HWND, UINT, WPARAM, LPARAM);
20
21 #define SPLASH_WIDTH_96DPI 440
22 #define SPLASH_HEIGHT_96DPI 322
23 #define SPLASH_OFFSET_X_96DPI 10
24 #define SPLASH_OFFSET_Y_96DPI 10
25 #define SPLASH_VERSION_X_96DPI 280
26 #define SPLASH_VERSION_Y_96DPI 0
27
28 typedef struct {
29     int boarder_width;
30     int boarder_height;
31     int client_width;
32     int client_height;
33     int ok_control_width;
34     int ok_control_height;
35     int ok_control_offset_x;
36     int ok_control_offset_y;
37     int text_control_width;
38     int text_control_height;
39     int text_control_offset_x;
40     int text_control_offset_y;
41     int window_width;
42     int window_height;
43     int width;
44     int height;
45     int offset_x;
46     int offset_y;
47     int version_x;
48     int version_y;
49     HFONT hFont;
50 } SplashData;
51
52 static void
53 mswin_set_splash_data(HWND hWnd, SplashData * sd, double scale)
54 {
55     RECT client_rect;
56     RECT window_rect;
57     RECT ok_control_rect;
58     RECT text_control_rect;
59
60     GetClientRect(hWnd, &client_rect);
61     GetWindowRect(hWnd, &window_rect);
62     GetWindowRect(GetDlgItem(hWnd, IDOK), &ok_control_rect);
63     GetWindowRect(GetDlgItem(hWnd, IDC_EXTRAINFO), &text_control_rect);
64
65     sd->boarder_width = (window_rect.right - window_rect.left) -
66                                 (client_rect.right - client_rect.left);
67     sd->boarder_height = (window_rect.bottom - window_rect.top) -
68         (client_rect.bottom - client_rect.top);
69
70     sd->ok_control_width = ok_control_rect.right - ok_control_rect.left;
71     sd->ok_control_height = ok_control_rect.bottom - ok_control_rect.top;
72
73     sd->width = (int)(scale * SPLASH_WIDTH_96DPI);
74     sd->height = (int)(scale * SPLASH_HEIGHT_96DPI);
75     sd->offset_x = (int)(scale * SPLASH_OFFSET_X_96DPI);
76     sd->offset_y = (int)(scale * SPLASH_OFFSET_Y_96DPI);
77     sd->version_x = (int)(scale * SPLASH_VERSION_X_96DPI);
78     sd->version_y = (int)(scale * SPLASH_VERSION_Y_96DPI);
79
80     sd->client_width = sd->width + sd->offset_x * 2;
81     sd->client_height = sd->height + sd->ok_control_height +
82         sd->offset_y * 3;
83
84     sd->window_width = sd->client_width + sd->boarder_width;
85     sd->window_height = sd->client_height + sd->boarder_height;
86
87     sd->ok_control_offset_x = (sd->client_width - sd->ok_control_width) / 2;
88     sd->ok_control_offset_y = sd->client_height - sd->ok_control_height - sd->offset_y;
89
90     sd->text_control_width = sd->client_width - sd->offset_x * 2;
91     sd->text_control_height = text_control_rect.bottom - text_control_rect.top;
92
93     sd->text_control_offset_x = sd->offset_x;
94     sd->text_control_offset_y = sd->ok_control_offset_y - sd->offset_y -
95                                sd->text_control_height;
96
97     if (sd->hFont != NULL)
98         DeleteObject(sd->hFont);
99
100     sd->hFont = mswin_create_splashfont(hWnd);
101
102     MoveWindow(hWnd, window_rect.left, window_rect.top,
103         sd->window_width, sd->window_height, TRUE);
104
105     MoveWindow(GetDlgItem(hWnd, IDOK),
106         sd->ok_control_offset_x, sd->ok_control_offset_y,
107         sd->ok_control_width, sd->ok_control_height, TRUE);
108
109     MoveWindow(GetDlgItem(hWnd, IDC_EXTRAINFO),
110         sd->text_control_offset_x, sd->text_control_offset_y,
111         sd->text_control_width, sd->text_control_height, TRUE);
112
113 }
114
115 void
116 mswin_display_splash_window(BOOL show_ver)
117 {
118     MSG msg;
119     strbuf_t strbuf;
120
121     strbuf_init(&strbuf);
122
123     HWND hWnd = CreateDialog(GetNHApp()->hApp, MAKEINTRESOURCE(IDD_SPLASH),
124                         GetNHApp()->hMainWnd, NHSplashWndProc);
125     if (!hWnd)
126         panic("Cannot create Splash window");
127
128     MonitorInfo monitorInfo;
129     win10_monitor_info(hWnd, &monitorInfo);
130
131     SplashData splashData;
132     memset(&splashData, 0, sizeof(splashData));
133     mswin_set_splash_data(hWnd, &splashData, monitorInfo.scale);
134  
135     SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) &splashData);
136     
137     GetNHApp()->hPopupWnd = hWnd;
138
139     int left = monitorInfo.left + (monitorInfo.width - splashData.window_width) / 2;
140     int top = monitorInfo.top + (monitorInfo.height - splashData.window_height) / 2;
141     MoveWindow(hWnd, left, top, splashData.window_width, splashData.window_height, TRUE);
142
143     /* Fill the text control */
144     strbuf_reserve(&strbuf, BUFSIZ);
145     Sprintf(strbuf.str, "%s\n%s\n%s\n%s\n\n", COPYRIGHT_BANNER_A,
146             COPYRIGHT_BANNER_B, COPYRIGHT_BANNER_C, COPYRIGHT_BANNER_D);
147
148     if (show_ver) {
149         /* Show complete version information */
150         dlb *f;
151         char verbuf[BUFSZ];
152         int verstrsize = 0;
153  
154         getversionstring(verbuf);
155         strbuf_append(&strbuf, verbuf);
156         strbuf_append(&strbuf, "\n\n");
157             
158         /* Add compile options */
159         f = dlb_fopen(OPTIONS_USED, RDTMODE);
160         if (f) {
161             char line[LLEN + 1];
162
163             while (dlb_fgets(line, LLEN, f))
164                 strbuf_append(&strbuf, line);
165             (void) dlb_fclose(f);
166         }
167     } else {
168         /* Show news, if any */
169         if (iflags.news) {
170             FILE *nf;
171
172             iflags.news = 0; /* prevent newgame() from re-displaying news */
173             nf = fopen(NEWS, "r");
174             if (nf != NULL) {
175                 char line[LLEN + 1];
176
177                 while (fgets(line, LLEN, nf))
178                     strbuf_append(&strbuf, line);
179                 (void) fclose(nf);
180             } else {
181                 strbuf_append(&strbuf, "No news.");
182             }
183         }
184     }
185
186     strbuf_nl_to_crlf(&strbuf);
187     SetWindowText(GetDlgItem(hWnd, IDC_EXTRAINFO), strbuf.str);
188     strbuf_empty(&strbuf);
189     ShowWindow(hWnd, SW_SHOW);
190
191     while (IsWindow(hWnd) && GetMessage(&msg, NULL, 0, 0) != 0) {
192         if (!IsDialogMessage(hWnd, &msg)) {
193             TranslateMessage(&msg);
194             DispatchMessage(&msg);
195         }
196     }
197
198     GetNHApp()->hPopupWnd = NULL;
199     DeleteObject(splashData.hFont);
200 }
201
202 INT_PTR CALLBACK
203 NHSplashWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
204 {
205     UNREFERENCED_PARAMETER(lParam);
206
207     switch (message) {
208     case WM_INITDIALOG: {
209         HDC hdc = GetDC(hWnd);
210         cached_font * font = mswin_get_font(NHW_TEXT, ATR_NONE, hdc, FALSE);
211         /* set text control font */
212         SendMessage(hWnd, WM_SETFONT, (WPARAM)font->hFont,  0);
213         ReleaseDC(hWnd, hdc);
214
215         SetFocus(GetDlgItem(hWnd, IDOK));
216     } break;
217
218     case WM_PAINT: {
219         char VersionString[BUFSZ];
220         RECT rt;
221         HDC hdcBitmap;
222         HANDLE OldBitmap;
223         HANDLE OldFont;
224         PAINTSTRUCT ps;
225
226         SplashData *splashData = (SplashData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
227
228         HDC hdc = BeginPaint(hWnd, &ps);
229         /* Show splash graphic */
230
231         hdcBitmap = CreateCompatibleDC(hdc);
232         SetBkMode(hdc, OPAQUE);
233         OldBitmap = SelectObject(hdcBitmap, GetNHApp()->bmpSplash);
234         (*GetNHApp()->lpfnTransparentBlt)(hdc,
235             splashData->offset_x, splashData->offset_y,
236             splashData->width, splashData->height, hdcBitmap,
237             0, 0, SPLASH_WIDTH_96DPI, SPLASH_HEIGHT_96DPI,
238             TILE_BK_COLOR);
239
240         SelectObject(hdcBitmap, OldBitmap);
241         DeleteDC(hdcBitmap);
242
243         SetBkMode(hdc, TRANSPARENT);
244         /* Print version number */
245
246         SetTextColor(hdc, RGB(0, 0, 0));
247         rt.right = rt.left = splashData->offset_x + splashData->version_x;
248         rt.bottom = rt.top = splashData->offset_y + splashData->version_y;
249         Sprintf(VersionString, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR,
250                 PATCHLEVEL);
251         OldFont = SelectObject(hdc, splashData->hFont);
252         DrawText(hdc, VersionString, strlen(VersionString), &rt,
253                  DT_LEFT | DT_NOPREFIX | DT_CALCRECT);
254         DrawText(hdc, VersionString, strlen(VersionString), &rt,
255                  DT_LEFT | DT_NOPREFIX);
256         EndPaint(hWnd, &ps);
257     } break;
258
259     case WM_COMMAND:
260         switch (LOWORD(wParam)) {
261         case IDOK:
262             mswin_window_mark_dead(mswin_winid_from_handle(hWnd));
263             if (GetNHApp()->hMainWnd == hWnd)
264                 GetNHApp()->hMainWnd = NULL;
265             DestroyWindow(hWnd);
266             SetFocus(GetNHApp()->hMainWnd);
267             return TRUE;
268         }
269         break;
270
271     case WM_DPICHANGED: {
272         SplashData *splashData = (SplashData *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
273
274         MonitorInfo monitorInfo;
275         win10_monitor_info(hWnd, &monitorInfo);
276
277         mswin_set_splash_data(hWnd, splashData, monitorInfo.scale);
278
279         InvalidateRect(hWnd, NULL, TRUE);
280     } break;
281
282     }
283     return FALSE;
284 }