NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / win / win32 / mhrip.c
blobd305645b1abfd69c90831cf8c711a48f5534647d
1 /* aNetHack 0.0.1 mhrip.c $ANH-Date: 1432512812 2015/05/25 00:13:32 $ $ANH-Branch: master $:$ANH-Revision: 1.19 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 /* aNetHack may be freely redistributed. See license for details. */
5 #include "winMS.h"
6 #include "resource.h"
7 #include "mhrip.h"
8 #include "mhmsg.h"
9 #include "mhfont.h"
11 #define RIP_WIDTH 400
12 #define RIP_HEIGHT 200
14 #define RIP_GRAVE_HEIGHT 120
15 #define RIP_GRAVE_WIDTH 115
16 #define RIP_GRAVE_X 90
17 #define RIP_GRAVE_Y 60
19 #define RIP_OFFSET_X 10
20 #define RIP_OFFSET_Y 10
22 PNHWinApp GetNHApp(void);
24 typedef struct mswin_anethack_text_window {
25 HANDLE rip_bmp;
26 TCHAR *window_text;
27 TCHAR *rip_text;
28 } NHRIPWindow, *PNHRIPWindow;
30 INT_PTR CALLBACK NHRIPWndProc(HWND, UINT, WPARAM, LPARAM);
31 static void onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam);
33 HWND
34 mswin_init_RIP_window()
36 HWND ret;
37 PNHRIPWindow data;
39 ret = CreateDialog(GetNHApp()->hApp, MAKEINTRESOURCE(IDD_NHRIP),
40 GetNHApp()->hMainWnd, NHRIPWndProc);
41 if (!ret)
42 panic("Cannot create rip window");
44 data = (PNHRIPWindow) malloc(sizeof(NHRIPWindow));
45 if (!data)
46 panic("out of memory");
48 ZeroMemory(data, sizeof(NHRIPWindow));
49 SetWindowLongPtr(ret, GWLP_USERDATA, (LONG_PTR) data);
50 return ret;
53 void
54 mswin_display_RIP_window(HWND hWnd)
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;
66 data = (PNHRIPWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
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);
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;
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);
113 while (IsWindow(hWnd) && GetMessage(&msg, NULL, 0, 0) != 0) {
114 if (!IsDialogMessage(hWnd, &msg)) {
115 TranslateMessage(&msg);
116 DispatchMessage(&msg);
120 GetNHApp()->hPopupWnd = NULL;
123 INT_PTR CALLBACK
124 NHRIPWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
126 HDC hdc;
127 PNHRIPWindow data;
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),
137 ReleaseDC(hWnd, hdc);
139 SetFocus(GetDlgItem(hWnd, IDOK));
140 return FALSE;
142 case WM_MSNH_COMMAND:
143 onMSNHCommand(hWnd, wParam, lParam);
144 break;
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;
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);
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);
184 SelectObject(hdcBitmap, OldBitmap);
185 SelectObject(hdc, OldFont);
186 DeleteDC(hdcBitmap);
187 EndPaint(hWnd, &ps);
188 } break;
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;
200 break;
202 case WM_CLOSE:
203 /* if we get this here, we saved the bones so we can just force a quit
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;
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);
225 break;
227 return FALSE;
230 void
231 onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
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;
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]));
254 if (!data->window_text)
255 break;
257 _tcscat(data->window_text, NH_A2W(msg_data->text, wbuf, BUFSZ));
258 _tcscat(data->window_text, TEXT("\r\n"));
259 break;
261 case MSNH_MSG_DIED: {
262 data->rip_text = data->window_text;
263 data->window_text = NULL;
264 break;
269 void
270 mswin_finish_rip_text(winid wid)
272 SendMessage(mswin_hwnd_from_winid(wid), WM_MSNH_COMMAND, MSNH_MSG_DIED,