1 /* NetHack 3.6 mhstatus.c $NHDT-Date: 1432512810 2015/05/25 00:13:30 $ $NHDT-Branch: master $:$NHDT-Revision: 1.22 $ */
2 /* Copyright (C) 2001 by Alex Kompel */
3 /* NetHack may be freely redistributed. See license for details. */
11 #define MAXWINDOWTEXT BUFSZ
13 extern COLORREF
nhcolor_to_RGB(int c
); /* from mhmap */
15 typedef struct mswin_nethack_status_window
{
17 char window_text
[NHSW_LINES
][MAXWINDOWTEXT
+ 1];
20 boolean
*activefields
;
22 } NHStatusWindow
, *PNHStatusWindow
;
24 #ifdef STATUS_VIA_WINDOWPORT
25 static int fieldorder1
[] = { BL_TITLE
, BL_STR
, BL_DX
, BL_CO
, BL_IN
,
26 BL_WI
, BL_CH
, BL_ALIGN
, BL_SCORE
, -1 };
27 static int fieldorder2
[] = { BL_LEVELDESC
, BL_GOLD
, BL_HP
, BL_HPMAX
,
28 BL_ENE
, BL_ENEMAX
, BL_AC
, BL_XP
,
29 BL_EXP
, BL_HD
, BL_TIME
, BL_HUNGER
,
30 BL_CAP
, BL_CONDITION
, -1 };
31 static int *fieldorders
[] = { fieldorder1
, fieldorder2
, NULL
};
32 #endif /* STATUS_VIA_WINDOWPORT */
34 static TCHAR szStatusWindowClass
[] = TEXT("MSNHStatusWndClass");
35 LRESULT CALLBACK
StatusWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
36 static void register_status_window_class(void);
37 static LRESULT
onWMPaint(HWND hWnd
, WPARAM wParam
, LPARAM lParam
);
39 #define DEFAULT_COLOR_BG_STATUS COLOR_WINDOW
40 #define DEFAULT_COLOR_FG_STATUS COLOR_WINDOWTEXT
43 mswin_init_status_window()
45 static int run_once
= 0;
51 register_status_window_class();
55 /* get window position */
56 if (GetNHApp()->bAutoLayout
) {
57 SetRect(&rt
, 0, 0, 0, 0);
59 mswin_get_window_placement(NHW_STATUS
, &rt
);
62 /* create status window object */
63 ret
= CreateWindow(szStatusWindowClass
, NULL
,
64 WS_CHILD
| WS_CLIPSIBLINGS
| WS_SIZEBOX
,
65 rt
.left
, /* horizontal position of window */
66 rt
.top
, /* vertical position of window */
67 rt
.right
- rt
.left
, /* window width */
68 rt
.bottom
- rt
.top
, /* window height */
69 GetNHApp()->hMainWnd
, NULL
, GetNHApp()->hApp
, NULL
);
71 panic("Cannot create status window");
73 /* Set window caption */
74 SetWindowText(ret
, "Status");
76 /* create window data */
77 data
= (PNHStatusWindow
) malloc(sizeof(NHStatusWindow
));
79 panic("out of memory");
81 ZeroMemory(data
, sizeof(NHStatusWindow
));
82 SetWindowLongPtr(ret
, GWLP_USERDATA
, (LONG_PTR
) data
);
84 mswin_apply_window_style(ret
);
90 register_status_window_class()
93 ZeroMemory(&wcex
, sizeof(wcex
));
95 wcex
.style
= CS_NOCLOSE
;
96 wcex
.lpfnWndProc
= (WNDPROC
) StatusWndProc
;
99 wcex
.hInstance
= GetNHApp()->hApp
;
101 wcex
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
102 wcex
.hbrBackground
= status_bg_brush
104 : SYSCLR_TO_BRUSH(DEFAULT_COLOR_BG_STATUS
);
105 wcex
.lpszMenuName
= NULL
;
106 wcex
.lpszClassName
= szStatusWindowClass
;
108 RegisterClass(&wcex
);
112 StatusWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
114 PNHStatusWindow data
;
116 data
= (PNHStatusWindow
) GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
118 case WM_MSNH_COMMAND
: {
120 case MSNH_MSG_PUTSTR
: {
121 PMSNHMsgPutstr msg_data
= (PMSNHMsgPutstr
) lParam
;
122 strncpy(data
->window_text
[data
->index
], msg_data
->text
,
124 data
->index
= (data
->index
+ 1) % NHSW_LINES
;
125 InvalidateRect(hWnd
, NULL
, TRUE
);
128 case MSNH_MSG_CLEAR_WINDOW
: {
130 ZeroMemory(data
->window_text
, sizeof(data
->window_text
));
131 InvalidateRect(hWnd
, NULL
, TRUE
);
134 case MSNH_MSG_GETTEXT
: {
135 PMSNHMsgGetText msg_data
= (PMSNHMsgGetText
) lParam
;
136 #ifdef STATUS_VIA_WINDOWPORT
139 msg_data
->buffer
[0] = '\0';
140 if (data
->n_fields
> 0) {
141 for (fop
= fieldorders
; *fop
; fop
++) {
142 for (f
= *fop
; *f
!= -1; f
++) {
143 if (data
->activefields
[*f
])
144 strncat(msg_data
->buffer
, data
->vals
[*f
],
146 - strlen(msg_data
->buffer
));
148 strncat(msg_data
->buffer
, "\r\n",
149 msg_data
->max_size
- strlen(msg_data
->buffer
));
152 #else /* STATUS_VIA_WINDOWPORT */
153 strncpy(msg_data
->buffer
, data
->window_text
[0],
155 strncat(msg_data
->buffer
, "\r\n",
156 msg_data
->max_size
- strlen(msg_data
->buffer
));
157 strncat(msg_data
->buffer
, data
->window_text
[1],
158 msg_data
->max_size
- strlen(msg_data
->buffer
));
159 #endif /* STATUS_VIA_WINDOWPORT */
162 case MSNH_MSG_UPDATE_STATUS
: {
163 PMSNHMsgUpdateStatus msg_data
= (PMSNHMsgUpdateStatus
) lParam
;
164 data
->n_fields
= msg_data
->n_fields
;
165 data
->vals
= msg_data
->vals
;
166 data
->activefields
= msg_data
->activefields
;
167 data
->colors
= msg_data
->colors
;
168 InvalidateRect(hWnd
, NULL
, TRUE
);
170 } /* end switch( wParam ) { */
174 return onWMPaint(hWnd
, wParam
, lParam
);
178 GetWindowRect(hWnd
, &rt
);
179 ScreenToClient(GetNHApp()->hMainWnd
, (LPPOINT
) &rt
);
180 ScreenToClient(GetNHApp()->hMainWnd
, ((LPPOINT
) &rt
) + 1);
181 mswin_update_window_placement(NHW_STATUS
, &rt
);
187 GetWindowRect(hWnd
, &rt
);
188 ScreenToClient(GetNHApp()->hMainWnd
, (LPPOINT
) &rt
);
189 ScreenToClient(GetNHApp()->hMainWnd
, ((LPPOINT
) &rt
) + 1);
190 mswin_update_window_placement(NHW_STATUS
, &rt
);
196 SetWindowLongPtr(hWnd
, GWLP_USERDATA
, (LONG_PTR
) 0);
200 SetFocus(GetNHApp()->hMainWnd
);
204 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
209 #ifdef STATUS_VIA_WINDOWPORT
211 onWMPaint(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
216 HGDIOBJ oldFont
, normalFont
, boldFont
;
218 COLORREF OldBg
, OldFg
, Bg
, Fg
;
222 PNHStatusWindow data
;
224 data
= (PNHStatusWindow
) GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
226 hdc
= BeginPaint(hWnd
, &ps
);
227 GetClientRect(hWnd
, &rt
);
229 normalFont
= mswin_get_font(NHW_STATUS
, ATR_NONE
, hdc
, FALSE
);
230 boldFont
= mswin_get_font(NHW_STATUS
, ATR_BOLD
, hdc
, FALSE
);
231 oldFont
= SelectObject(hdc
, normalFont
);
233 Bg
= status_bg_brush
? status_bg_color
234 : (COLORREF
) GetSysColor(DEFAULT_COLOR_BG_STATUS
);
235 OldBg
= SetBkColor(hdc
, Bg
);
237 Fg
= status_fg_brush
? status_fg_color
238 : (COLORREF
) GetSysColor(DEFAULT_COLOR_FG_STATUS
);
239 OldFg
= SetTextColor(hdc
, Fg
);
241 for (fop
= fieldorders
; *fop
; fop
++) {
245 for (f
= *fop
; *f
!= -1; f
++) {
246 if (((*f
) >= data
->n_fields
) || (!data
->activefields
[*f
]))
248 vlen
= strlen(data
->vals
[*f
]);
249 NH_A2W(data
->vals
[*f
], wbuf
, SIZE(wbuf
));
251 if (!iflags
.use_status_hilites
) {
252 SelectObject(hdc
, normalFont
);
254 SetTextColor(hdc
, Fg
);
255 } else if (data
->colors
[*f
] == CLR_MAX
256 || data
->colors
[*f
] == BL_HILITE_NONE
) {
257 SelectObject(hdc
, normalFont
);
259 SetTextColor(hdc
, Fg
);
260 } else if (data
->colors
[*f
] > 0) {
261 SelectObject(hdc
, normalFont
);
263 SetTextColor(hdc
, nhcolor_to_RGB(data
->colors
[*f
]));
264 } else if (data
->colors
[*f
] == BL_HILITE_INVERSE
) {
265 SelectObject(hdc
, normalFont
);
267 SetTextColor(hdc
, Bg
);
268 } else if (data
->colors
[*f
] == BL_HILITE_BOLD
) {
269 SelectObject(hdc
, boldFont
);
271 SetTextColor(hdc
, Fg
);
273 SelectObject(hdc
, normalFont
);
275 SetTextColor(hdc
, Fg
);
278 GetTextExtentPoint32(hdc
, wbuf
, vlen
, &sz
);
279 DrawText(hdc
, wbuf
, vlen
, &rt
, DT_LEFT
);
288 SelectObject(hdc
, oldFont
);
289 SetTextColor(hdc
, OldFg
);
290 SetBkColor(hdc
, OldBg
);
297 onWMPaint(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
303 COLORREF OldBg
, OldFg
;
307 PNHStatusWindow data
;
309 data
= (PNHStatusWindow
) GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
311 hdc
= BeginPaint(hWnd
, &ps
);
312 GetClientRect(hWnd
, &rt
);
315 SelectObject(hdc
, mswin_get_font(NHW_STATUS
, ATR_NONE
, hdc
, FALSE
));
317 OldBg
= SetBkColor(hdc
, status_bg_brush
? status_bg_color
318 : (COLORREF
) GetSysColor(
319 DEFAULT_COLOR_BG_STATUS
));
320 OldFg
= SetTextColor(hdc
, status_fg_brush
? status_fg_color
321 : (COLORREF
) GetSysColor(
322 DEFAULT_COLOR_FG_STATUS
));
324 for (i
= 0; i
< NHSW_LINES
; i
++) {
325 int wlen
= strlen(data
->window_text
[i
]);
326 NH_A2W(data
->window_text
[i
], wbuf
, SIZE(wbuf
));
327 GetTextExtentPoint32(hdc
, wbuf
, wlen
, &sz
);
328 DrawText(hdc
, wbuf
, wlen
, &rt
, DT_LEFT
| DT_END_ELLIPSIS
);
332 SelectObject(hdc
, oldFont
);
333 SetTextColor(hdc
, OldFg
);
334 SetBkColor(hdc
, OldBg
);
339 #endif /*STATUS_VIA_WINDOWPORT*/
342 mswin_status_window_size(HWND hWnd
, LPSIZE sz
)
347 PNHStatusWindow data
;
351 GetClientRect(hWnd
, &rt
);
353 data
= (PNHStatusWindow
) GetWindowLongPtr(hWnd
, GWLP_USERDATA
);
356 saveFont
= SelectObject(
357 hdc
, mswin_get_font(NHW_STATUS
, ATR_NONE
, hdc
, FALSE
));
358 GetTextExtentPoint32(hdc
, _T("W"), 1, &text_sz
);
359 GetTextMetrics(hdc
, &tm
);
361 rt
.bottom
= rt
.top
+ text_sz
.cy
* NHSW_LINES
;
363 SelectObject(hdc
, saveFont
);
364 ReleaseDC(hWnd
, hdc
);
366 AdjustWindowRect(&rt
, GetWindowLong(hWnd
, GWL_STYLE
), FALSE
);
367 sz
->cx
= rt
.right
- rt
.left
;
368 sz
->cy
= rt
.bottom
- rt
.top
;