OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / sys / wince / mhstatus.c
1 /* NetHack 3.6  mhstatus.c      $NHDT-Date: 1432512798 2015/05/25 00:13:18 $  $NHDT-Branch: master $:$NHDT-Revision: 1.17 $ */
2 /* Copyright (C) 2001 by Alex Kompel     */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 #include "winMS.h"
6 #include "mhstatus.h"
7 #include "mhmsg.h"
8 #include "mhfont.h"
9 #include "mhcolor.h"
10
11 #define MAXWINDOWTEXT 255
12
13 #define NHSTAT_LINES_2 2
14 #define NHSTAT_LINES_4 4
15 typedef struct mswin_nethack_status_window {
16     int nhstat_format;
17     char window_text[MAXWINDOWTEXT];
18 } NHStatusWindow, *PNHStatusWindow;
19
20 static TCHAR szStatusWindowClass[] = TEXT("MSNHStatusWndClass");
21 LRESULT CALLBACK StatusWndProc(HWND, UINT, WPARAM, LPARAM);
22 static void register_status_window_class(void);
23 static void FormatStatusString(char *text, int format);
24
25 HWND
26 mswin_init_status_window()
27 {
28     static int run_once = 0;
29     HWND ret;
30     NHStatusWindow *data;
31
32     if (!run_once) {
33         register_status_window_class();
34         run_once = 1;
35     }
36
37     ret = CreateWindow(szStatusWindowClass, NULL,
38                        WS_CHILD | WS_DISABLED | WS_CLIPSIBLINGS,
39                        0, /* x position */
40                        0, /* y position */
41                        0, /* x-size - we will set it later */
42                        0, /* y-size - we will set it later */
43                        GetNHApp()->hMainWnd, NULL, GetNHApp()->hApp, NULL);
44     if (!ret)
45         panic("Cannot create status window");
46
47     EnableWindow(ret, FALSE);
48
49     data = (PNHStatusWindow) malloc(sizeof(NHStatusWindow));
50     if (!data)
51         panic("out of memory");
52
53     ZeroMemory(data, sizeof(NHStatusWindow));
54     data->nhstat_format = NHSTAT_LINES_4;
55     SetWindowLong(ret, GWL_USERDATA, (LONG) data);
56     return ret;
57 }
58
59 void
60 register_status_window_class()
61 {
62     WNDCLASS wcex;
63
64     ZeroMemory(&wcex, sizeof(wcex));
65     wcex.style = CS_NOCLOSE;
66     wcex.lpfnWndProc = (WNDPROC) StatusWndProc;
67     wcex.cbClsExtra = 0;
68     wcex.cbWndExtra = 0;
69     wcex.hInstance = GetNHApp()->hApp;
70     wcex.hIcon = NULL;
71     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
72     wcex.hbrBackground = mswin_get_brush(NHW_STATUS, MSWIN_COLOR_BG);
73     wcex.lpszMenuName = NULL;
74     wcex.lpszClassName = szStatusWindowClass;
75
76     RegisterClass(&wcex);
77 }
78
79 LRESULT CALLBACK
80 StatusWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
81 {
82     RECT rt;
83     PAINTSTRUCT ps;
84     HDC hdc;
85     PNHStatusWindow data;
86
87     data = (PNHStatusWindow) GetWindowLong(hWnd, GWL_USERDATA);
88     switch (message) {
89     case WM_MSNH_COMMAND: {
90         switch (wParam) {
91         case MSNH_MSG_PUTSTR:
92         case MSNH_MSG_CLEAR_WINDOW:
93             ZeroMemory(data->window_text, sizeof(data->window_text));
94             FormatStatusString(data->window_text, data->nhstat_format);
95             break;
96
97         case MSNH_MSG_CURSOR: {
98             PMSNHMsgCursor msg_data = (PMSNHMsgCursor) lParam;
99             if (msg_data->y == 0) {
100                 InvalidateRect(hWnd, NULL, TRUE);
101             }
102         } break;
103         }
104     } break;
105
106     case WM_PAINT: {
107         HGDIOBJ oldFont;
108         TCHAR wbuf[MAXWINDOWTEXT];
109         COLORREF OldBg, OldFg;
110
111         hdc = BeginPaint(hWnd, &ps);
112         GetClientRect(hWnd, &rt);
113
114         oldFont = SelectObject(
115             hdc, mswin_get_font(NHW_STATUS, ATR_NONE, hdc, FALSE));
116         OldBg = SetBkColor(hdc, mswin_get_color(NHW_STATUS, MSWIN_COLOR_BG));
117         OldFg =
118             SetTextColor(hdc, mswin_get_color(NHW_STATUS, MSWIN_COLOR_FG));
119
120         DrawText(hdc, NH_A2W(data->window_text, wbuf, MAXWINDOWTEXT),
121                  strlen(data->window_text), &rt, DT_LEFT | DT_NOPREFIX);
122
123         SetTextColor(hdc, OldFg);
124         SetBkColor(hdc, OldBg);
125         SelectObject(hdc, oldFont);
126         EndPaint(hWnd, &ps);
127     } break;
128
129     case WM_DESTROY:
130         free(data);
131         SetWindowLong(hWnd, GWL_USERDATA, (LONG) 0);
132         break;
133
134     case WM_SETFOCUS:
135         SetFocus(GetNHApp()->hMainWnd);
136         break;
137
138     default:
139         return DefWindowProc(hWnd, message, wParam, lParam);
140     }
141     return 0;
142 }
143
144 void
145 mswin_status_window_size(HWND hWnd, LPSIZE sz)
146 {
147     TEXTMETRIC tm;
148     HGDIOBJ saveFont;
149     HDC hdc;
150     PNHStatusWindow data;
151     RECT rt;
152     GetWindowRect(hWnd, &rt);
153     sz->cx = rt.right - rt.left;
154     sz->cy = rt.bottom - rt.top;
155
156     data = (PNHStatusWindow) GetWindowLong(hWnd, GWL_USERDATA);
157     if (data) {
158         hdc = GetDC(hWnd);
159         saveFont = SelectObject(
160             hdc, mswin_get_font(NHW_STATUS, ATR_NONE, hdc, FALSE));
161         GetTextMetrics(hdc, &tm);
162
163         /* see if the status window can fit 80 characters per line */
164         if ((80 * tm.tmMaxCharWidth) >= sz->cx)
165             data->nhstat_format = NHSTAT_LINES_4;
166         else
167             data->nhstat_format = NHSTAT_LINES_2;
168
169         /* set height of the status box */
170         sz->cy = tm.tmHeight * data->nhstat_format;
171
172         SelectObject(hdc, saveFont);
173         ReleaseDC(hWnd, hdc);
174     }
175 }
176 extern const char *hu_stat[];  /* defined in eat.c */
177 extern const char *enc_stat[]; /* define in botl.c */
178 void
179 FormatStatusString(char *text, int format)
180 {
181     register char *nb;
182     int hp, hpmax;
183     int cap = near_capacity();
184
185     Strcpy(text, plname);
186     if ('a' <= text[0] && text[0] <= 'z')
187         text[0] += 'A' - 'a';
188     text[10] = 0;
189     Sprintf(nb = eos(text), " the ");
190
191     if (Upolyd) {
192         char mbot[BUFSZ];
193         int k = 0;
194
195         Strcpy(mbot, mons[u.umonnum].mname);
196         while (mbot[k] != 0) {
197             if ((k == 0 || (k > 0 && mbot[k - 1] == ' ')) && 'a' <= mbot[k]
198                 && mbot[k] <= 'z')
199                 mbot[k] += 'A' - 'a';
200             k++;
201         }
202         Sprintf(nb = eos(nb), mbot);
203     } else
204         Sprintf(nb = eos(nb), rank_of(u.ulevel, Role_switch, flags.female));
205
206     if (format == NHSTAT_LINES_4)
207         Sprintf(nb = eos(nb), "\r\n");
208
209     if (ACURR(A_STR) > 18) {
210         if (ACURR(A_STR) > STR18(100))
211             Sprintf(nb = eos(nb), "St:%2d ", ACURR(A_STR) - 100);
212         else if (ACURR(A_STR) < STR18(100))
213             Sprintf(nb = eos(nb), "St:18/%02d ", ACURR(A_STR) - 18);
214         else
215             Sprintf(nb = eos(nb), "St:18/** ");
216     } else
217         Sprintf(nb = eos(nb), "St:%-1d ", ACURR(A_STR));
218     Sprintf(nb = eos(nb), "Dx:%-1d Co:%-1d In:%-1d Wi:%-1d Ch:%-1d",
219             ACURR(A_DEX), ACURR(A_CON), ACURR(A_INT), ACURR(A_WIS),
220             ACURR(A_CHA));
221     Sprintf(nb = eos(nb),
222             (u.ualign.type == A_CHAOTIC)
223                 ? "  Chaotic"
224                 : (u.ualign.type == A_NEUTRAL) ? "  Neutral" : "  Lawful");
225 #ifdef SCORE_ON_BOTL
226     if (flags.showscore)
227         Sprintf(nb = eos(nb), " S:%ld", botl_score());
228 #endif
229     if (format == NHSTAT_LINES_4 || format == NHSTAT_LINES_2)
230         strcat(text, "\r\n");
231
232     /* third line */
233     hp = Upolyd ? u.mh : u.uhp;
234     hpmax = Upolyd ? u.mhmax : u.uhpmax;
235
236     if (hp < 0)
237         hp = 0;
238     (void) describe_level(nb = eos(nb));
239     Sprintf(nb = eos(nb), "%c:%-2ld HP:%d(%d) Pw:%d(%d) AC:%-2d",
240             showsyms[COIN_CLASS + SYM_OFF_O], money_cnt(invent), hp, hpmax,
241             u.uen, u.uenmax, u.uac);
242
243     if (Upolyd)
244         Sprintf(nb = eos(nb), " HD:%d", mons[u.umonnum].mlevel);
245     else if (flags.showexp)
246         Sprintf(nb = eos(nb), " Xp:%u/%-1ld", u.ulevel, u.uexp);
247     else
248         Sprintf(nb = eos(nb), " Exp:%u", u.ulevel);
249     if (format == NHSTAT_LINES_4)
250         strcat(text, "\r\n");
251     else
252         strcat(text, " ");
253
254     /* forth line */
255     if (flags.time)
256         Sprintf(nb = eos(nb), "T:%ld ", moves);
257
258     if (strcmp(hu_stat[u.uhs], "        ")) {
259         Strcat(text, hu_stat[u.uhs]);
260         Sprintf(nb = eos(nb), " ");
261     }
262     if (Confusion)
263         Sprintf(nb = eos(nb), "Conf");
264     if (Sick) {
265         if (u.usick_type & SICK_VOMITABLE)
266             Sprintf(nb = eos(nb), " FoodPois");
267         if (u.usick_type & SICK_NONVOMITABLE)
268             Sprintf(nb = eos(nb), " Ill");
269     }
270     if (Blind)
271         Sprintf(nb = eos(nb), " Blind");
272     if (Stunned)
273         Sprintf(nb = eos(nb), " Stun");
274     if (Hallucination)
275         Sprintf(nb = eos(nb), " Hallu");
276     if (Slimed)
277         Sprintf(nb = eos(nb), " Slime");
278     if (cap > UNENCUMBERED)
279         Sprintf(nb = eos(nb), " %s", enc_stat[cap]);
280 }