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 "mhstatus.h"
6 #include "mhmsg.h"
7 #include "mhfont.h"
8 
9 #define NHSW_LINES    2
10 #define MAXWINDOWTEXT BUFSZ
11 
12 typedef struct mswin_nethack_status_window {
13 	int   index;
14 	char  window_text[NHSW_LINES][MAXWINDOWTEXT+1];
15 } NHStatusWindow, *PNHStatusWindow;
16 
17 static TCHAR szStatusWindowClass[] = TEXT("MSNHStatusWndClass");
18 LRESULT CALLBACK	StatusWndProc(HWND, UINT, WPARAM, LPARAM);
19 static void register_status_window_class(void);
20 
21 #define DEFAULT_COLOR_BG_STATUS	COLOR_WINDOW
22 #define DEFAULT_COLOR_FG_STATUS	COLOR_WINDOWTEXT
23 
mswin_init_status_window()24 HWND mswin_init_status_window () {
25 	static int run_once = 0;
26 	HWND ret;
27 	NHStatusWindow* data;
28 
29 	if( !run_once ) {
30 		register_status_window_class( );
31 		run_once = 1;
32 	}
33 
34 	ret = CreateWindow(
35 			szStatusWindowClass,
36 			NULL,
37 			WS_CHILD | WS_DISABLED | WS_CLIPSIBLINGS,
38 			0,  /* x position */
39 			0,  /* y position */
40 			0,  /* x-size - we will set it later */
41 			0,  /* y-size - we will set it later */
42 			GetNHApp()->hMainWnd,
43 			NULL,
44 			GetNHApp()->hApp,
45 			NULL );
46 	if( !ret ) panic("Cannot create status window");
47 
48 	EnableWindow(ret, FALSE);
49 
50 	data = (PNHStatusWindow)malloc(sizeof(NHStatusWindow));
51 	if( !data ) panic("out of memory");
52 
53 	ZeroMemory(data, sizeof(NHStatusWindow));
54 	SetWindowLong(ret, GWL_USERDATA, (LONG)data);
55 	return ret;
56 }
57 
register_status_window_class()58 void register_status_window_class()
59 {
60 	WNDCLASS wcex;
61 	ZeroMemory( &wcex, sizeof(wcex));
62 
63 	wcex.style			= CS_NOCLOSE;
64 	wcex.lpfnWndProc	= (WNDPROC)StatusWndProc;
65 	wcex.cbClsExtra		= 0;
66 	wcex.cbWndExtra		= 0;
67 	wcex.hInstance		= GetNHApp()->hApp;
68 	wcex.hIcon			= NULL;
69 	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
70 	wcex.hbrBackground	= status_bg_brush
71 		? status_bg_brush : SYSCLR_TO_BRUSH(DEFAULT_COLOR_BG_STATUS);
72 	wcex.lpszMenuName	= NULL;
73 	wcex.lpszClassName	= szStatusWindowClass;
74 
75 	RegisterClass(&wcex);
76 }
77 
78 
StatusWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)79 LRESULT CALLBACK StatusWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
80 {
81 	RECT rt;
82 	PAINTSTRUCT ps;
83 	HDC hdc;
84 	PNHStatusWindow data;
85 
86 	data = (PNHStatusWindow)GetWindowLong(hWnd, GWL_USERDATA);
87 	switch (message)
88 	{
89 	case WM_MSNH_COMMAND: {
90 		switch( wParam ) {
91 		case MSNH_MSG_PUTSTR: {
92 			PMSNHMsgPutstr msg_data = (PMSNHMsgPutstr)lParam;
93 			strncpy(data->window_text[data->index], msg_data->text,
94 				    MAXWINDOWTEXT);
95 			strncat(data->window_text[data->index], "\n",
96 				    MAXWINDOWTEXT-strlen(data->window_text[data->index]));
97 			data->index = (data->index+1) % NHSW_LINES;
98 			InvalidateRect(hWnd, NULL, TRUE);
99 			break;
100 		}
101 		case MSNH_MSG_CLEAR_WINDOW:
102 			data->index = 0;
103 			ZeroMemory(data->window_text, sizeof(data->window_text));
104 			InvalidateRect(hWnd, NULL, TRUE);
105 			break;
106 		}
107 	} break;
108 
109 	case WM_PAINT: {
110 		    int i;
111 			SIZE sz;
112 			HGDIOBJ oldFont;
113 			TCHAR wbuf[BUFSZ];
114 			COLORREF OldBg, OldFg;
115 
116 			hdc = BeginPaint(hWnd, &ps);
117 			GetClientRect(hWnd, &rt);
118 
119 			oldFont = SelectObject(hdc, mswin_get_font(NHW_STATUS, ATR_NONE, hdc, FALSE));
120 
121 			OldBg = SetBkColor(hdc, status_bg_brush
122 				? status_bg_color : (COLORREF)GetSysColor(DEFAULT_COLOR_BG_STATUS));
123 			OldFg = SetTextColor(hdc, status_fg_brush
124 				? status_fg_color : (COLORREF)GetSysColor(DEFAULT_COLOR_FG_STATUS));
125 
126 			for(i=0; i<NHSW_LINES; i++ ) {
127 				GetTextExtentPoint32(hdc, NH_A2W(data->window_text[i], wbuf, sizeof(wbuf)), strlen(data->window_text[i]), &sz);
128 				NH_A2W(data->window_text[i], wbuf, BUFSZ);
129 				DrawText(hdc, wbuf, strlen(data->window_text[i]), &rt, DT_LEFT | DT_END_ELLIPSIS);
130 				rt.top += sz.cy;
131 			}
132 
133 			SelectObject(hdc, oldFont);
134 			SetTextColor (hdc, OldFg);
135 			SetBkColor (hdc, OldBg);
136 			EndPaint(hWnd, &ps);
137 		} break;
138 
139 	case WM_DESTROY:
140 		free(data);
141 		SetWindowLong(hWnd, GWL_USERDATA, (LONG)0);
142 		break;
143 
144 	case WM_SETFOCUS:
145 		SetFocus(GetNHApp()->hMainWnd);
146 		break;
147 
148 	default:
149 		return DefWindowProc(hWnd, message, wParam, lParam);
150    }
151    return 0;
152 }
153 
mswin_status_window_size(HWND hWnd,LPSIZE sz)154 void mswin_status_window_size (HWND hWnd, LPSIZE sz)
155 {
156     TEXTMETRIC tm;
157 	HGDIOBJ saveFont;
158 	HDC hdc;
159 	PNHStatusWindow data;
160 	RECT rt;
161 	GetWindowRect(hWnd, &rt);
162 	sz->cx = rt.right - rt.left;
163 	sz->cy = rt.bottom - rt.top;
164 
165 	data = (PNHStatusWindow)GetWindowLong(hWnd, GWL_USERDATA);
166 	if(data) {
167 		hdc = GetDC(hWnd);
168 		saveFont = SelectObject(hdc, mswin_get_font(NHW_STATUS, ATR_NONE, hdc, FALSE));
169 		GetTextMetrics(hdc, &tm);
170 
171 		sz->cy = tm.tmHeight * NHSW_LINES;
172 
173 		SelectObject(hdc, saveFont);
174 		ReleaseDC(hWnd, hdc);
175 	}
176 }
177