OSDN Git Service

add translation
[jnethack/source.git] / win / win32 / mhrip.c
1 /* NetHack 3.6  mhrip.c $NHDT-Date: 1432512812 2015/05/25 00:13:32 $  $NHDT-Branch: master $:$NHDT-Revision: 1.19 $ */
2 /* Copyright (C) 2001 by Alex Kompel     */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 #include "winMS.h"
6 #include "resource.h"
7 #include "mhrip.h"
8 #include "mhmsg.h"
9 #include "mhfont.h"
10
11 #define RIP_WIDTH 400
12 #define RIP_HEIGHT 200
13
14 #define RIP_GRAVE_HEIGHT 120
15 #define RIP_GRAVE_WIDTH 115
16 #define RIP_GRAVE_X 90
17 #define RIP_GRAVE_Y 60
18
19 #define RIP_OFFSET_X 10
20 #define RIP_OFFSET_Y 10
21
22 PNHWinApp GetNHApp(void);
23
24 typedef struct mswin_nethack_text_window {
25     HANDLE rip_bmp;
26     TCHAR *window_text;
27     TCHAR *rip_text;
28 } NHRIPWindow, *PNHRIPWindow;
29
30 INT_PTR CALLBACK NHRIPWndProc(HWND, UINT, WPARAM, LPARAM);
31 static void onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
32
33 HWND
34 mswin_init_RIP_window()
35 {
36     HWND ret;
37     PNHRIPWindow data;
38
39     ret = CreateDialog(GetNHApp()->hApp, MAKEINTRESOURCE(IDD_NHRIP),
40                        GetNHApp()->hMainWnd, NHRIPWndProc);
41     if (!ret)
42         panic("Cannot create rip window");
43
44     data = (PNHRIPWindow) malloc(sizeof(NHRIPWindow));
45     if (!data)
46         panic("out of memory");
47
48     ZeroMemory(data, sizeof(NHRIPWindow));
49     SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data);
50     return ret;
51 }
52
53 void
54 mswin_display_RIP_window(HWND hWnd)
55 {
56     MSG msg;
57     RECT rt;
58     PNHRIPWindow data;
59     HWND mapWnd;
60     RECT riprt;
61     RECT clientrect;
62     RECT textrect;
63     HDC hdc;
64     HFONT OldFont;
65
66     data = (PNHRIPWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
67
68     GetNHApp()->hPopupWnd = hWnd;
69     mapWnd = mswin_hwnd_from_winid(WIN_MAP);
70     if (!IsWindow(mapWnd))
71         mapWnd = GetNHApp()->hMainWnd;
72     GetWindowRect(mapWnd, &rt);
73     GetWindowRect(hWnd, &riprt);
74     GetClientRect(hWnd, &clientrect);
75     textrect = clientrect;
76     textrect.top += RIP_OFFSET_Y;
77     textrect.left += RIP_OFFSET_X;
78     textrect.right -= RIP_OFFSET_X;
79     if (data->window_text) {
80         hdc = GetDC(hWnd);
81         OldFont = SelectObject(hdc, mswin_get_font(NHW_TEXT, 0, hdc, FALSE));
82         DrawText(hdc, data->window_text, strlen(data->window_text), &textrect,
83                  DT_LEFT | DT_NOPREFIX | DT_CALCRECT);
84         SelectObject(hdc, OldFont);
85         ReleaseDC(hWnd, hdc);
86     }
87     if (textrect.right - textrect.left > RIP_WIDTH)
88         clientrect.right = textrect.right + RIP_OFFSET_X - clientrect.right;
89     else
90         clientrect.right =
91             textrect.left + 2 * RIP_OFFSET_X + RIP_WIDTH - clientrect.right;
92     clientrect.bottom =
93         textrect.bottom + RIP_HEIGHT + RIP_OFFSET_Y - clientrect.bottom;
94     GetWindowRect(GetDlgItem(hWnd, IDOK), &textrect);
95     textrect.right -= textrect.left;
96     textrect.bottom -= textrect.top;
97     clientrect.bottom += textrect.bottom + RIP_OFFSET_Y;
98     riprt.right -= riprt.left;
99     riprt.bottom -= riprt.top;
100     riprt.right += clientrect.right;
101     riprt.bottom += clientrect.bottom;
102     rt.left += (rt.right - rt.left - riprt.right) / 2;
103     rt.top += (rt.bottom - rt.top - riprt.bottom) / 2;
104
105     MoveWindow(hWnd, rt.left, rt.top, riprt.right, riprt.bottom, TRUE);
106     GetClientRect(hWnd, &clientrect);
107     MoveWindow(GetDlgItem(hWnd, IDOK),
108                (clientrect.right - clientrect.left - textrect.right) / 2,
109                clientrect.bottom - textrect.bottom - RIP_OFFSET_Y,
110                textrect.right, textrect.bottom, TRUE);
111     ShowWindow(hWnd, SW_SHOW);
112
113     while (IsWindow(hWnd) && GetMessage(&msg, NULL, 0, 0) != 0) {
114         if (!IsDialogMessage(hWnd, &msg)) {
115             TranslateMessage(&msg);
116             DispatchMessage(&msg);
117         }
118     }
119
120     GetNHApp()->hPopupWnd = NULL;
121 }
122
123 INT_PTR CALLBACK
124 NHRIPWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
125 {
126     HDC hdc;
127     PNHRIPWindow data;
128
129     data = (PNHRIPWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
130     switch (message) {
131     case WM_INITDIALOG:
132         /* set text control font */
133         hdc = GetDC(hWnd);
134         SendMessage(hWnd, WM_SETFONT,
135                     (WPARAM) mswin_get_font(NHW_TEXT, ATR_NONE, hdc, FALSE),
136                     0);
137         ReleaseDC(hWnd, hdc);
138
139         SetFocus(GetDlgItem(hWnd, IDOK));
140         return FALSE;
141
142     case WM_MSNH_COMMAND:
143         onMSNHCommand(hWnd, wParam, lParam);
144         break;
145
146     case WM_PAINT: {
147         int bitmap_offset;
148         RECT clientrect;
149         RECT textrect;
150         HDC hdcBitmap;
151         HANDLE OldBitmap;
152         PAINTSTRUCT ps;
153         HFONT OldFont;
154
155         hdc = BeginPaint(hWnd, &ps);
156         OldFont = SelectObject(hdc, mswin_get_font(NHW_TEXT, 0, hdc, FALSE));
157         hdcBitmap = CreateCompatibleDC(hdc);
158         SetBkMode(hdc, TRANSPARENT);
159         GetClientRect(hWnd, &clientrect);
160         textrect = clientrect;
161         textrect.top += RIP_OFFSET_Y;
162         textrect.left += RIP_OFFSET_X;
163         textrect.right -= RIP_OFFSET_X;
164         if (data->window_text) {
165             DrawText(hdc, data->window_text, strlen(data->window_text),
166                      &textrect, DT_LEFT | DT_NOPREFIX | DT_CALCRECT);
167             DrawText(hdc, data->window_text, strlen(data->window_text),
168                      &textrect, DT_LEFT | DT_NOPREFIX);
169         }
170         OldBitmap = SelectObject(hdcBitmap, GetNHApp()->bmpRip);
171         SetBkMode(hdc, OPAQUE);
172         bitmap_offset = (textrect.right - textrect.left - RIP_WIDTH) / 2;
173         BitBlt(hdc, textrect.left + bitmap_offset, textrect.bottom, RIP_WIDTH,
174                RIP_HEIGHT, hdcBitmap, 0, 0, SRCCOPY);
175         SetBkMode(hdc, TRANSPARENT);
176         if (data->rip_text) {
177             textrect.left += RIP_GRAVE_X + bitmap_offset;
178             textrect.top = textrect.bottom + RIP_GRAVE_Y;
179             textrect.right = textrect.left + RIP_GRAVE_WIDTH;
180             textrect.bottom = textrect.top + RIP_GRAVE_HEIGHT;
181             DrawText(hdc, data->rip_text, strlen(data->rip_text), &textrect,
182                      DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_WORDBREAK);
183         }
184         SelectObject(hdcBitmap, OldBitmap);
185         SelectObject(hdc, OldFont);
186         DeleteDC(hdcBitmap);
187         EndPaint(hWnd, &ps);
188     } break;
189
190     case WM_COMMAND:
191         switch (LOWORD(wParam)) {
192         case IDOK:
193             mswin_window_mark_dead(mswin_winid_from_handle(hWnd));
194             if (GetNHApp()->hMainWnd == hWnd)
195                 GetNHApp()->hMainWnd = NULL;
196             DestroyWindow(hWnd);
197             SetFocus(GetNHApp()->hMainWnd);
198             return TRUE;
199         }
200         break;
201
202     case WM_CLOSE:
203         /* if we get this here, we saved the bones so we can just force a quit
204          */
205
206         mswin_window_mark_dead(mswin_winid_from_handle(hWnd));
207         if (GetNHApp()->hMainWnd == hWnd)
208             GetNHApp()->hMainWnd = NULL;
209         DestroyWindow(hWnd);
210         SetFocus(GetNHApp()->hMainWnd);
211         program_state.stopprint++;
212         return TRUE;
213
214     case WM_DESTROY:
215         if (data) {
216             if (data->window_text)
217                 free(data->window_text);
218             if (data->rip_text)
219                 free(data->rip_text);
220             if (data->rip_bmp != NULL)
221                 DeleteObject(data->rip_bmp);
222             free(data);
223             SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) 0);
224         }
225         break;
226     }
227     return FALSE;
228 }
229
230 void
231 onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
232 {
233     PNHRIPWindow data;
234     static int InRipText = 1;
235     data = (PNHRIPWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
236     switch (wParam) {
237     case MSNH_MSG_PUTSTR: {
238         PMSNHMsgPutstr msg_data = (PMSNHMsgPutstr) lParam;
239         TCHAR wbuf[BUFSZ];
240         size_t text_size;
241
242         if (!data->window_text) {
243             text_size = strlen(msg_data->text) + 4;
244             data->window_text =
245                 (TCHAR *) malloc(text_size * sizeof(data->window_text[0]));
246             ZeroMemory(data->window_text,
247                        text_size * sizeof(data->window_text[0]));
248         } else {
249             text_size =
250                 _tcslen(data->window_text) + strlen(msg_data->text) + 4;
251             data->window_text = (TCHAR *) realloc(
252                 data->window_text, text_size * sizeof(data->window_text[0]));
253         }
254         if (!data->window_text)
255             break;
256
257         _tcscat(data->window_text, NH_A2W(msg_data->text, wbuf, BUFSZ));
258         _tcscat(data->window_text, TEXT("\r\n"));
259         break;
260     }
261     case MSNH_MSG_DIED: {
262         data->rip_text = data->window_text;
263         data->window_text = NULL;
264         break;
265     }
266     }
267 }
268
269 void
270 mswin_finish_rip_text(winid wid)
271 {
272     SendMessage(mswin_hwnd_from_winid(wid), WM_MSNH_COMMAND, MSNH_MSG_DIED,
273                 0);
274 }