OSDN Git Service

Initial Import
[nethackexpress/trunk.git] / sys / wince / mhstatus.c
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 #include "mhcolor.h"
9
10 #define MAXWINDOWTEXT 255
11
12 #define NHSTAT_LINES_2  2
13 #define NHSTAT_LINES_4  4
14 typedef struct mswin_nethack_status_window {
15         int             nhstat_format;
16         char    window_text[MAXWINDOWTEXT];
17 } NHStatusWindow, *PNHStatusWindow;
18
19 static TCHAR szStatusWindowClass[] = TEXT("MSNHStatusWndClass");
20 LRESULT CALLBACK        StatusWndProc(HWND, UINT, WPARAM, LPARAM);
21 static void register_status_window_class(void);
22 static void FormatStatusString(char* text, int format);
23
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         data->nhstat_format = NHSTAT_LINES_4;
55         SetWindowLong(ret, GWL_USERDATA, (LONG)data);
56         return ret;
57 }
58
59 void register_status_window_class()
60 {
61         WNDCLASS wcex;
62         
63         ZeroMemory( &wcex, sizeof(wcex));
64         wcex.style                      = CS_NOCLOSE;
65         wcex.lpfnWndProc        = (WNDPROC)StatusWndProc;
66         wcex.cbClsExtra         = 0;
67         wcex.cbWndExtra         = 0;
68         wcex.hInstance          = GetNHApp()->hApp;
69         wcex.hIcon                      = NULL;
70         wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
71         wcex.hbrBackground      = mswin_get_brush(NHW_STATUS, MSWIN_COLOR_BG);
72         wcex.lpszMenuName       = NULL;
73         wcex.lpszClassName      = szStatusWindowClass;
74
75         RegisterClass(&wcex);
76 }
77     
78     
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                 
92                 case MSNH_MSG_PUTSTR:
93                 case MSNH_MSG_CLEAR_WINDOW:
94                         ZeroMemory(data->window_text, sizeof(data->window_text));
95                         FormatStatusString(data->window_text, data->nhstat_format);
96                         break;
97
98                 case MSNH_MSG_CURSOR: {
99                         PMSNHMsgCursor msg_data = (PMSNHMsgCursor)lParam;
100                         if( msg_data->y==0 ) {
101                                 InvalidateRect(hWnd, NULL, TRUE);
102                         }
103                 } break;
104                 }
105         } break;
106
107         case WM_PAINT: {
108                         HGDIOBJ oldFont;
109                         TCHAR wbuf[MAXWINDOWTEXT];
110                         COLORREF OldBg, OldFg;
111
112                         hdc = BeginPaint(hWnd, &ps);
113                         GetClientRect(hWnd, &rt);
114                         
115                         oldFont = SelectObject(hdc, mswin_get_font(NHW_STATUS, ATR_NONE, hdc, FALSE));
116                         OldBg = SetBkColor(hdc, mswin_get_color(NHW_STATUS, MSWIN_COLOR_BG));
117                         OldFg = SetTextColor(hdc, mswin_get_color(NHW_STATUS, MSWIN_COLOR_FG)); 
118
119                         DrawText(hdc, 
120                                          NH_A2W(data->window_text, wbuf, MAXWINDOWTEXT),
121                                          strlen(data->window_text), 
122                                          &rt, 
123                                          DT_LEFT | DT_NOPREFIX);
124
125                         SetTextColor (hdc, OldFg);
126                         SetBkColor (hdc, OldBg);
127                         SelectObject(hdc, oldFont);
128                         EndPaint(hWnd, &ps);
129                 } break;
130
131         case WM_DESTROY:
132                 free(data);
133                 SetWindowLong(hWnd, GWL_USERDATA, (LONG)0);
134                 break;
135
136         case WM_SETFOCUS:
137                 SetFocus(GetNHApp()->hMainWnd);
138                 break;
139
140         default:
141                 return DefWindowProc(hWnd, message, wParam, lParam);
142    }
143    return 0;
144 }
145
146 void mswin_status_window_size (HWND hWnd, LPSIZE sz)
147 {
148     TEXTMETRIC tm;
149         HGDIOBJ saveFont;
150         HDC hdc;
151         PNHStatusWindow data;
152         RECT rt;
153         GetWindowRect(hWnd, &rt);
154         sz->cx = rt.right - rt.left;
155         sz->cy = rt.bottom - rt.top;
156
157         data = (PNHStatusWindow)GetWindowLong(hWnd, GWL_USERDATA);
158         if(data) {
159                 hdc = GetDC(hWnd);
160                 saveFont = SelectObject(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 ) data->nhstat_format = NHSTAT_LINES_4;
165                 else                                                     data->nhstat_format = NHSTAT_LINES_2;
166
167                 /* set height of the status box */
168                 sz->cy = tm.tmHeight * data->nhstat_format;
169
170                 SelectObject(hdc, saveFont);
171                 ReleaseDC(hWnd, hdc);
172         }
173 }
174 extern const char *hu_stat[];   /* defined in eat.c */
175 extern const char *enc_stat[]; /* define in botl.c */
176 void FormatStatusString(char* text, int format)
177 {
178         register char *nb;
179         int hp, hpmax;
180         int cap = near_capacity();
181
182         Strcpy(text, plname);
183         if('a' <= text[0] && text[0] <= 'z') text[0] += 'A'-'a';
184         text[10] = 0;
185         Sprintf(nb = eos(text)," the ");
186
187         if (Upolyd) {
188                 char mbot[BUFSZ];
189                 int k = 0;
190
191                 Strcpy(mbot, mons[u.umonnum].mname);
192                 while(mbot[k] != 0) {
193                     if ((k == 0 || (k > 0 && mbot[k-1] == ' ')) &&
194                                         'a' <= mbot[k] && mbot[k] <= 'z')
195                         mbot[k] += 'A' - 'a';
196                     k++;
197                 }
198                 Sprintf(nb = eos(nb), mbot);
199         } else
200                 Sprintf(nb = eos(nb), rank_of(u.ulevel, Role_switch, flags.female));
201
202         if( format==NHSTAT_LINES_4 ) Sprintf(nb = eos(nb),"\r\n");
203
204         if (ACURR(A_STR) > 18) {
205                 if (ACURR(A_STR) > STR18(100))
206                     Sprintf(nb = eos(nb),"St:%2d ",ACURR(A_STR)-100);
207                 else if (ACURR(A_STR) < STR18(100))
208                     Sprintf(nb = eos(nb), "St:18/%02d ",ACURR(A_STR)-18);
209                 else
210                     Sprintf(nb = eos(nb),"St:18/** ");
211         } else
212                 Sprintf(nb = eos(nb), "St:%-1d ",ACURR(A_STR));
213         Sprintf(nb = eos(nb),
214                 "Dx:%-1d Co:%-1d In:%-1d Wi:%-1d Ch:%-1d",
215                 ACURR(A_DEX), ACURR(A_CON), ACURR(A_INT), ACURR(A_WIS), ACURR(A_CHA));
216         Sprintf(nb = eos(nb), (u.ualign.type == A_CHAOTIC) ? "  Chaotic" :
217                         (u.ualign.type == A_NEUTRAL) ? "  Neutral" : "  Lawful");
218 #ifdef SCORE_ON_BOTL
219         if (flags.showscore)
220             Sprintf(nb = eos(nb), " S:%ld", botl_score());
221 #endif
222         if( format==NHSTAT_LINES_4 ||
223                 format==NHSTAT_LINES_2 ) strcat(text, "\r\n");
224
225         /* third line */
226         hp = Upolyd ? u.mh : u.uhp;
227         hpmax = Upolyd ? u.mhmax : u.uhpmax;
228
229         if(hp < 0) hp = 0;
230         (void) describe_level(nb=eos(nb));
231         Sprintf(nb = eos(nb),
232                 "%c:%-2ld HP:%d(%d) Pw:%d(%d) AC:%-2d", oc_syms[COIN_CLASS],
233 #ifndef GOLDOBJ
234                 u.ugold,
235 #else
236                 money_cnt(invent),
237 #endif
238                 hp, hpmax, u.uen, u.uenmax, u.uac);
239
240         if (Upolyd)
241                 Sprintf(nb = eos(nb), " HD:%d", mons[u.umonnum].mlevel);
242 #ifdef EXP_ON_BOTL
243         else if(flags.showexp)
244                 Sprintf(nb = eos(nb), " Xp:%u/%-1ld", u.ulevel,u.uexp);
245 #endif
246         else
247                 Sprintf(nb = eos(nb), " Exp:%u", u.ulevel);
248         if( format==NHSTAT_LINES_4 ) strcat(text, "\r\n");
249         else                         strcat(text, " ");
250
251         /* forth line */
252         if(flags.time)
253             Sprintf(nb = eos(nb), "T:%ld ", moves);
254
255         if(strcmp(hu_stat[u.uhs], "        ")) {
256                 Strcat(text, hu_stat[u.uhs]);
257                 Sprintf(nb = eos(nb), " ");
258         }
259         if(Confusion)      Sprintf(nb = eos(nb), "Conf");
260         if(Sick) {
261                 if (u.usick_type & SICK_VOMITABLE)
262                            Sprintf(nb = eos(nb), " FoodPois");
263                 if (u.usick_type & SICK_NONVOMITABLE)
264                            Sprintf(nb = eos(nb), " Ill");
265         }
266         if(Blind)          Sprintf(nb = eos(nb), " Blind");
267         if(Stunned)        Sprintf(nb = eos(nb), " Stun");
268         if(Hallucination)  Sprintf(nb = eos(nb), " Hallu");
269         if(Slimed)         Sprintf(nb = eos(nb), " Slime");
270         if(cap > UNENCUMBERED)
271                 Sprintf(nb = eos(nb), " %s", enc_stat[cap]);
272 }
273