OSDN Git Service

3c974bcdccdf5881e2e0c7917ebc9bd4fab87a24
[nethackexpress/trunk.git] / win / win32 / mhsplash.c
1 /* Copyright (C) 2001 by Alex Kompel <shurikk@pacbell.net> */
2 /* NetHack may be freely redistributed.  See license for details. */
3
4 #include "winMS.h"
5 #include "resource.h"
6 #include "mhsplash.h"
7 #include "mhmsg.h"
8 #include "mhfont.h"
9 #include "patchlevel.h"
10 #include "dlb.h"
11
12 #define LLEN 128
13
14 PNHWinApp GetNHApp(void);
15
16 BOOL CALLBACK NHSplashWndProc(HWND, UINT, WPARAM, LPARAM);
17
18 #define SPLASH_WIDTH            440
19 #define SPLASH_HEIGHT  322
20 #define SPLASH_VERSION_X                290
21 #define SPLASH_VERSION_Y                10
22 #define SPLASH_OFFSET_X         10
23 #define SPLASH_OFFSET_Y         10
24
25 extern HFONT version_splash_font;
26
27 void mswin_display_splash_window (BOOL show_ver)
28 {
29         MSG msg;
30         HWND mapWnd;
31         int left, top;
32         RECT splashrt;
33         RECT clientrt;
34         RECT controlrt;
35         HWND hWnd;
36         int buttop;
37         int strsize = 0;
38         int bufsize = BUFSZ;
39
40         char *buf = malloc(bufsize);
41         if (buf == NULL)
42             panic("out of memory");
43         buf[0] = '\0';
44
45         hWnd = CreateDialog(GetNHApp()->hApp, MAKEINTRESOURCE(IDD_SPLASH),
46             GetNHApp()->hMainWnd, NHSplashWndProc);
47         if( !hWnd ) panic("Cannot create Splash window");
48         mswin_init_splashfonts(hWnd);
49         GetNHApp()->hPopupWnd = hWnd;
50         mapWnd = mswin_hwnd_from_winid(WIN_MAP);
51         if( !IsWindow(mapWnd) ) mapWnd = GetNHApp()->hMainWnd;
52         /* Get control size */
53         GetWindowRect (GetDlgItem(hWnd, IDOK), &controlrt);
54         controlrt.right -= controlrt.left;
55         controlrt.bottom -= controlrt.top;
56         /* Get current client area */
57         GetClientRect (hWnd, &clientrt);
58         /* Get window size */
59         GetWindowRect(hWnd, &splashrt);
60         splashrt.right -= splashrt.left;
61         splashrt.bottom -= splashrt.top;
62         /* Get difference between requested client area and current value */
63         splashrt.right += SPLASH_WIDTH + SPLASH_OFFSET_X * 2 - clientrt.right;
64         splashrt.bottom += SPLASH_HEIGHT + controlrt.bottom + SPLASH_OFFSET_Y * 3 - clientrt.bottom;
65         /* Place the window centered */
66         /* On the screen, not on the parent window */
67         left = (GetSystemMetrics(SM_CXSCREEN) - splashrt.right) / 2;
68         top = (GetSystemMetrics(SM_CYSCREEN) - splashrt.bottom) / 2;
69         MoveWindow(hWnd, left, top, splashrt.right, splashrt.bottom, TRUE);
70         /* Place the OK control */
71         GetClientRect (hWnd, &clientrt);
72         MoveWindow (GetDlgItem(hWnd, IDOK),
73             (clientrt.right - clientrt.left - controlrt.right) / 2,
74             clientrt.bottom - controlrt.bottom - SPLASH_OFFSET_Y,
75             controlrt.right, controlrt.bottom, TRUE);
76         buttop = clientrt.bottom - controlrt.bottom - SPLASH_OFFSET_Y;
77         /* Place the text control */
78         GetWindowRect (GetDlgItem(hWnd, IDC_EXTRAINFO), &controlrt);
79         controlrt.right -= controlrt.left;
80         controlrt.bottom -= controlrt.top;
81         GetClientRect (hWnd, &clientrt);
82         MoveWindow (GetDlgItem(hWnd, IDC_EXTRAINFO),
83             clientrt.left + SPLASH_OFFSET_X,
84             buttop - controlrt.bottom - SPLASH_OFFSET_Y,
85             clientrt.right - 2 * SPLASH_OFFSET_X, controlrt.bottom, TRUE);
86         /* Fill the text control */
87         Sprintf(buf, "%s\r\n%s\r\n%s\r\n\r\n", COPYRIGHT_BANNER_A, COPYRIGHT_BANNER_B,
88             COPYRIGHT_BANNER_C);
89         strsize = strlen(buf);
90         
91         if (show_ver) {
92             /* Show complete version information */
93             dlb *f;
94
95             getversionstring(buf + strsize);
96             strcat(buf, "\r\n\r\n");
97             strsize = strlen(buf);
98
99             /* Add compile options */
100             f = dlb_fopen(OPTIONS_USED, RDTMODE);
101             if (f) {
102                 char line[LLEN + 1];
103                 
104                 while (dlb_fgets(line, LLEN, f)) {
105                     size_t len;
106                     len = strlen(line);
107                     if (len > 0 && line[len - 1] == '\n') {
108                         line[len - 1] = '\r';
109                         line[len] = '\n';
110                         line[len + 1] = '\0';
111                         len++;
112                     }
113                     if (strsize + (int)len + 1 > bufsize)
114                     {
115                         bufsize += BUFSZ;
116                         buf = realloc(buf, bufsize);
117                         if (buf == NULL)
118                             panic("out of memory");
119                     }
120                     strcat(buf, line);
121                     strsize += len;
122                 }
123                 (void) dlb_fclose(f);
124             }
125         } else {
126             /* Show news, if any */
127             if (iflags.news) {      
128                 FILE *nf;
129                 
130                 iflags.news = 0; /* prevent newgame() from re-displaying news */
131                 nf = fopen(NEWS, "r");
132                 if (nf != NULL) {
133                     char line[LLEN + 1];
134                     
135                     while (fgets(line, LLEN, nf)) {
136                         size_t len;
137                         len = strlen(line);
138                         if (len > 0 && line[len - 1] == '\n') {
139                             line[len - 1] = '\r';
140                             line[len] = '\n';
141                             line[len + 1] = '\0';
142                             len++;
143                         }
144                         if (strsize + (int)len + 1 > bufsize)
145                         {
146                             bufsize += BUFSZ;
147                             buf = realloc(buf, bufsize);
148                             if (buf == NULL)
149                                 panic("out of memory");
150                         }
151                         strcat(buf, line);
152                         strsize += len;
153                     }
154                     (void) fclose(nf);
155                 } 
156                 else
157                 {
158                     strcat(buf, "No news.");
159                 }
160             }
161         }
162         SetWindowText(GetDlgItem(hWnd, IDC_EXTRAINFO), buf);
163         free(buf);
164         ShowWindow(hWnd, SW_SHOW);
165         
166         while( IsWindow(hWnd) &&
167             GetMessage(&msg, NULL, 0, 0)!=0 ) {
168             if( !IsDialogMessage(hWnd, &msg) ) {
169                 TranslateMessage(&msg);
170                 DispatchMessage(&msg);
171             }
172         }
173         
174         GetNHApp()->hPopupWnd = NULL;
175         mswin_destroy_splashfonts();
176 }
177
178 BOOL CALLBACK NHSplashWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
179 {
180         HDC hdc;
181         switch (message)
182         {
183         case WM_INITDIALOG:
184             /* set text control font */
185                 hdc = GetDC(hWnd);
186                 SendMessage(hWnd, WM_SETFONT,
187                         (WPARAM)mswin_get_font(NHW_TEXT, ATR_NONE, hdc, FALSE), 0);
188                 ReleaseDC(hWnd, hdc);
189
190                 SetFocus(GetDlgItem(hWnd, IDOK));
191         return FALSE;
192
193         case WM_PAINT:
194         {
195                 char VersionString[BUFSZ];
196                 RECT rt;
197                 HDC hdcBitmap;
198                 HANDLE OldBitmap;
199                 HANDLE OldFont;
200                 PAINTSTRUCT ps;
201
202                 hdc = BeginPaint (hWnd, &ps);
203                 /* Show splash graphic */
204
205                 hdcBitmap = CreateCompatibleDC(hdc);
206                 SetBkMode (hdc, OPAQUE);
207                 OldBitmap = SelectObject(hdcBitmap, GetNHApp()->bmpSplash);
208                 nhapply_image_transparent(hdc, SPLASH_OFFSET_X, SPLASH_OFFSET_Y,
209                     SPLASH_WIDTH, SPLASH_HEIGHT, 
210                     hdcBitmap, 0, 0, SPLASH_WIDTH, SPLASH_HEIGHT, 
211                     TILE_BK_COLOR);
212
213                 SelectObject (hdcBitmap, OldBitmap);
214                 DeleteDC (hdcBitmap);
215
216                 SetBkMode (hdc, TRANSPARENT);
217                 /* Print version number */
218
219                 SetTextColor (hdc, RGB(0, 0, 0));
220                 rt.right = rt.left = SPLASH_VERSION_X;
221                 rt.bottom = rt.top = SPLASH_VERSION_Y;
222                 Sprintf (VersionString, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR,
223                     PATCHLEVEL);
224                 OldFont = SelectObject(hdc, version_splash_font);
225                 DrawText (hdc, VersionString, strlen(VersionString), &rt,
226                     DT_LEFT | DT_NOPREFIX | DT_CALCRECT);
227                 DrawText (hdc, VersionString, strlen(VersionString), &rt,
228                     DT_LEFT | DT_NOPREFIX);
229
230                 EndPaint (hWnd, &ps);
231         }
232         break;
233
234         case WM_COMMAND:
235         switch (LOWORD(wParam))
236         {
237                 case IDOK:
238                         mswin_window_mark_dead(mswin_winid_from_handle(hWnd));
239                         if( GetNHApp()->hMainWnd==hWnd )
240                                 GetNHApp()->hMainWnd=NULL;
241                         DestroyWindow(hWnd);
242                         SetFocus(GetNHApp()->hMainWnd);
243                         return TRUE;
244                 }
245         break;
246         }
247         return FALSE;
248 }