1 /* NetHack 3.6 mhcmd.c $NHDT-Date: 1524689383 2018/04/25 20:49:43 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.15 $ */
2 /* Copyright (c) 2009 by Michael Allison */
3 /* NetHack may be freely redistributed. See license for details. */
11 static TCHAR szNHCmdWindowClass[] = TEXT("MSNethackCmdWndClass");
14 #define C(c) (0x1f & (c))
18 #define NH_CST_CHECKED 1
21 #define NH_CMDPAD_FONT_NORMAL 0
22 #define NH_CMDPAD_FONT_MAX 0
24 /* type of the cell */
26 #define NH_CELL_CTRL 1
28 #define NH_CELL_SHIFT 3
29 #define NH_CELL_LAYOUT_NEW 4
30 #define NH_CELL_LAYOUT_MENU 5
32 #define NH_CMDSET_MAXSIZE 64
34 /* Keypad cell information
36 NHCmdPadCell.cell_type NHCmdPadCell.data
37 ----------- ----------
38 NH_CELL_REG (int)>=0 - index in the
39 current keypad layout set (loads a new layout)
40 -1 - restore default (saved) layout
43 NH_CELL_SHIFT not used
44 NH_CELL_LAYOUT_NEW pointer to the new keypad layout
46 NH_CELL_LAYOUT_MENU pointer to the layout set (NHCmdSet* - if
47 NULL then nhcmdset_default is used)
49 typedef struct t_NHCmdPadCell {
50 UINT cmd_code; /* Windows command code (menu processing - not implemented
52 char f_char[16]; /* nethack char */
53 char text[16]; /* display text */
54 int image; /* >0 - image ID in IDB_KEYPAD bitmap
55 <=0 - absolute index of the font table */
56 int type; /* cell type */
57 int mult; /* cell width multiplier */
58 void *data; /* internal data for the cell type */
59 } NHCmdPadCell, *PNHCmdPadCell;
62 typedef struct t_NHCmdLayout {
67 } NHCmdLayout, *PNHCmdLayout;
69 /* set of command layouts */
70 typedef struct t_NHCmdSet {
72 struct t_NHCmdSetElem {
75 } elements[NH_CMDSET_MAXSIZE];
76 } NHCmdSet, *PNHCmdSet;
78 /* display cell layout */
79 typedef struct t_NHCmdPadLayoutCell {
80 POINT orig; /* origin of the cell rect */
81 BYTE type; /* cell type */
82 int state; /* cell state */
83 } NHCmdPadLayoutCell, *PNHCmdPadLayoutCell;
85 /* command window data */
86 typedef struct mswin_nethack_cmd_window {
87 SIZE cell_size; /* cell size */
88 HFONT font[NH_CMDPAD_FONT_MAX + 1]; /* fonts for cell text */
89 HBITMAP images; /* key images map */
90 int active_cell; /* current active cell */
92 boolean is_caps; /* is CAPS selected */
93 boolean is_ctrl; /* is CRTL selected */
94 boolean is_shift; /* is SHIFT selected */
96 PNHCmdLayout layout_current; /* current layout */
97 PNHCmdLayout layout_save; /* saved layout */
98 PNHCmdPadLayoutCell cells; /* display cells */
100 #if defined(WIN_CE_SMARTPHONE)
101 PNHCmdLayout layout_selected; /* since we use
103 command for menu also
104 we need to store the layout
109 } NHCmdWindow, *PNHCmdWindow;
111 LRESULT CALLBACK NHCommandWndProc(HWND, UINT, WPARAM, LPARAM);
112 static void register_command_window_class();
113 static void LayoutCmdWindow(HWND hWnd);
114 static void SetCmdWindowLayout(HWND hWnd, PNHCmdLayout layout);
115 static int CellFromPoint(PNHCmdWindow data, POINT pt);
116 static void CalculateCellSize(HWND hWnd, LPSIZE pSize, LPSIZE windowSize);
117 static void HighlightCell(HWND hWnd, int cell, BOOL isSelected);
118 static void ActivateCell(HWND hWnd, int cell);
119 static void PushNethackCommand(const char *cmd_char_str, int is_ctrl);
121 /*------------------- keyboard keys layout functions -----------------------*/
122 PNHCmdLayout nhcmdlayout_create(const char *name, int rows, int columns);
123 void nhcmdlayout_init(PNHCmdLayout p, PNHCmdPadCell cells);
124 #define nhcmdlayout_rows(p) ((p)->rows)
125 #define nhcmdlayout_columns(p) ((p)->columns)
126 #define nhcmdlayout_row(p, x) (&((p)->cells[(p)->columns * (x)]))
127 #define nhcmdlayout_cell(p, x, y) (&((p)->cells[(p)->columns * (x) + (y)]))
128 #define nhcmdlayout_cell_direct(p, i) (&((p)->cells[(i)]))
129 void nhcmdlayout_destroy(PNHCmdLayout p);
131 /*----------------- keyboard keys layout set functions ---------------------*/
132 PNHCmdSet nhcmdset_create();
133 int nhcmdset_count(PNHCmdSet p);
134 PNHCmdLayout nhcmdset_get(PNHCmdSet p, int index);
135 const char *nhcmdset_get_name(PNHCmdSet p, int index);
136 void nhcmdset_add(PNHCmdSet p, PNHCmdLayout layout);
137 void nhcmdset_destroy(PNHCmdSet p);
139 /*-------------------- message handlers -----------------------------------*/
140 static void onPaint(HWND hWnd); // on WM_PAINT
141 static void onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam); // on WM_CREATE
142 static void onMouseDown(HWND hWnd, WPARAM wParam,
143 LPARAM lParam); // on WM_LBUTTONDOWN
144 static void onMouseMove(HWND hWnd, WPARAM wParam,
145 LPARAM lParam); // on WM_MOUSEMOVE
146 static void onMouseUp(HWND hWnd, WPARAM wParam,
147 LPARAM lParam); // on WM_LBUTTONUP
149 /*----------------------- static data -------------------------------------*/
150 static PNHCmdSet nhcmdset_current = 0;
151 static PNHCmdSet nhcmdset_default = 0;
153 /*---------------------- Pre-definde keyboard layouts --------------------*/
154 #ifdef WIN_CE_SMARTPHONE
156 /* dimensions of the command pad */
157 #define NH_CMDPAD_ROWS 4
158 #define NH_CMDPAD_COLS 3
159 #define NH_CMDPAD_CELLNUM (NH_CMDPAD_COLS * NH_CMDPAD_ROWS)
162 #define NH_LAYOUT_GENERAL 0
163 #define NH_LAYOUT_MOVEMENT 1
164 #define NH_LAYOUT_ATTACK 2
165 #define NH_LAYOUT_ITEM_HANDLING 3
166 #define NH_LAYOUT_CONTROLS 4
167 #define NH_LAYOUT_ADV_MOVEMENT 5
168 #define NH_LAYOUT_ITEM_LOOKUP 6
169 #define NH_LAYOUT_WIZARD 7
171 /* template menu layout */
172 NHCmdPadCell cells_layout_menu[NH_CMDPAD_CELLNUM] = {
173 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
174 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
175 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
176 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
177 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
178 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
179 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
180 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
181 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
182 { -1, "", "<<", -NH_CMDPAD_FONT_NORMAL, NH_CELL_LAYOUT_NEW, 1, NULL },
183 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
184 { -1, "", ">>", -NH_CMDPAD_FONT_NORMAL, NH_CELL_LAYOUT_NEW, 1, NULL }
187 /* movement layout */
188 NHCmdPadCell cells_layout_movement[NH_CMDPAD_CELLNUM] = {
189 { -1, "7", "7", 1, NH_CELL_REG, 1, (void *) - 1 },
190 { -1, "8", "8", 2, NH_CELL_REG, 1, (void *) - 1 },
191 { -1, "9", "9", 3, NH_CELL_REG, 1, (void *) - 1 },
192 { -1, "4", "4", 4, NH_CELL_REG, 1, (void *) - 1 },
193 { -1, ".", ".", 5, NH_CELL_REG, 1, (void *) - 1 },
194 { -1, "6", "6", 6, NH_CELL_REG, 1, (void *) - 1 },
195 { -1, "1", "1", 7, NH_CELL_REG, 1, (void *) - 1 },
196 { -1, "2", "2", 8, NH_CELL_REG, 1, (void *) - 1 },
197 { -1, "3", "3", 9, NH_CELL_REG, 1, (void *) - 1 },
198 { -1, "<", "<", 10, NH_CELL_REG, 1, (void *) - 1 },
199 { -1, ">", ">", 12, NH_CELL_REG, 1, (void *) - 1 },
200 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
204 NHCmdPadCell cells_layout_attack[NH_CMDPAD_CELLNUM] = {
205 { -1, "t", "t", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
206 { -1, "w", "w", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
207 { -1, "x", "x", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
208 { -1, "f", "f", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
209 { -1, "z", "z", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
210 { -1, "Z", "Z", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
211 { -1, "r", "r", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
212 { -1, "a", "a", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
213 { -1, "q", "q", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
214 { -1, "\x04", "^D", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
216 { -1, "F", "F", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
217 (void *) NH_LAYOUT_MOVEMENT },
218 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
221 /* item handling layout */
222 NHCmdPadCell cells_layout_item_handling[NH_CMDPAD_CELLNUM] = {
223 { -1, "W", "W", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
224 { -1, "P", "P", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
225 { -1, "d", "d", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
226 { -1, "T", "T", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
227 { -1, "R", "R", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
228 { -1, "D", "D", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
229 { -1, "=", "=", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
230 { -1, "i", "i", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
231 { -1, "Q", "Q", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
232 { -1, "A", "A", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
233 { -1, "I", "I", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
234 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
238 NHCmdPadCell cells_layout_general[NH_CMDPAD_CELLNUM] = {
239 { -1, "q", "q", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
240 { -1, "e", "e", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
241 { -1, "l", "l", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
242 { -1, "s", "s", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
243 { -1, "E", "E", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
244 { -1, "\x01", "^A", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
246 { -1, "c", "c", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
247 { -1, "o", "o", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
248 { -1, "p", "p", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
249 { -1, ":", ":", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
250 { -1, ",", ",", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
251 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
254 /* game controls layout */
255 NHCmdPadCell cells_layout_game[NH_CMDPAD_CELLNUM] = {
256 { -1, "S", "S", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
257 { -1, "h", "h", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
258 { -1, "C", "C", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
259 { -1, "@", "@", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
260 { -1, "\\", "\\", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
261 { -1, "O", "O", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
262 { -1, "&", "&", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
263 { -1, "\x18", "^X", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
265 { -1, "\x10", "^P", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
267 { -1, "X", "X", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
268 { -1, "#", "#", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
269 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
272 /* advanced movement layout */
273 NHCmdPadCell cells_layout_adv_movement[NH_CMDPAD_CELLNUM] = {
274 { -1, "g", "g", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
275 (void *) NH_LAYOUT_MOVEMENT },
276 { -1, "G", "G", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
277 (void *) NH_LAYOUT_MOVEMENT },
278 { -1, "m", "m", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
279 (void *) NH_LAYOUT_MOVEMENT },
280 { -1, "M", "M", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
281 (void *) NH_LAYOUT_MOVEMENT },
282 { -1, "_", "_", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
283 { -1, "\x14", "^T", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
285 { -1, "j", "j", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
286 { -1, "<", "<", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
287 { -1, ">", ">", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
288 { -1, "^", "^", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
289 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
290 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
293 /* item lookup layout */
294 NHCmdPadCell cells_layout_lookup[NH_CMDPAD_CELLNUM] = {
295 { -1, ";", ";", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
296 { -1, "^", "^", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
297 { -1, "]", "]", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
298 { -1, ")", ")", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
299 { -1, "=", "=", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
300 { -1, "*", "*", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
301 { -1, "(", "(", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
302 { -1, "\"", "\"", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
303 { -1, "$", "$", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
304 { -1, "+", "+", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
305 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
306 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
309 /* wizard mode layout */
310 NHCmdPadCell cells_layout_wizard[NH_CMDPAD_CELLNUM] = {
311 { -1, "\x05", "^e", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
313 { -1, "\x06", "^f", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
315 { -1, "\x07", "^g", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
317 { -1, "\x09", "^i", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
319 { -1, "\x0f", "^o", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
321 { -1, "\x16", "^v", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
323 { -1, "\x17", "^w", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
325 { -1, "\x14", "^T", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1,
327 { -1, "#", "#", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
328 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
329 { -1, "", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
330 { -1, "X", "X", 13, NH_CELL_LAYOUT_MENU, 1, 0 }
333 #else /* !WIN_CE_SMARTPHONE */
335 /* dimensions of the command pad */
336 #define NH_CMDPAD_ROWS 4
337 #define NH_CMDPAD_COLS 14
338 #define NH_CMDPAD_CELLNUM (NH_CMDPAD_COLS * NH_CMDPAD_ROWS)
340 /* lowercase layout */
341 NHCmdPadCell cells_layout_mod1[NH_CMDPAD_ROWS * NH_CMDPAD_COLS] = {
342 { -1, "7", "7", 1, NH_CELL_REG, 1, (void *) - 1 },
343 { -1, "8", "8", 2, NH_CELL_REG, 1, (void *) - 1 },
344 { -1, "9", "9", 3, NH_CELL_REG, 1, (void *) - 1 },
345 { -1, "\x1b", "Esc", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 2, NULL },
346 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
347 NULL }, /* complement for ESC */
348 { -1, "?", "?", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
349 { -1, "*", "*", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
350 { -1, ",", ",", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
351 { -1, "/", "/", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
352 { -1, ":", ":", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
353 { -1, ";", ";", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
354 { -1, "-", "-", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
355 { -1, "#", "#", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
356 { -1, "^", "^", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
358 { -1, "4", "4", 4, NH_CELL_REG, 1, (void *) - 1 },
359 { -1, "5", "5", 5, NH_CELL_REG, 1, (void *) - 1 },
360 { -1, "6", "6", 6, NH_CELL_REG, 1, (void *) - 1 },
361 { -1, " ", "CAP", -NH_CMDPAD_FONT_NORMAL, NH_CELL_CAP, 2, NULL },
362 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
363 NULL }, /* complement for CAPS */
364 { -1, "a", "a", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
365 { -1, "b", "b", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
366 { -1, "c", "c", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
367 { -1, "d", "d", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
368 { -1, "e", "e", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
369 { -1, "f", "f", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
370 { -1, "g", "g", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
371 { -1, "h", "h", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
372 { -1, "i", "i", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
374 { -1, "1", "1", 7, NH_CELL_REG, 1, (void *) - 1 },
375 { -1, "2", "2", 8, NH_CELL_REG, 1, (void *) - 1 },
376 { -1, "3", "3", 9, NH_CELL_REG, 1, (void *) - 1 },
377 { -1, " ", "Shft", -NH_CMDPAD_FONT_NORMAL, NH_CELL_SHIFT, 2, NULL },
378 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
379 NULL }, /* complement for shift */
380 { -1, "j", "j", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
381 { -1, "k", "k", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
382 { -1, "l", "l", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
383 { -1, "m", "m", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
384 { -1, "n", "n", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
385 { -1, "o", "o", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
386 { -1, "p", "p", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
387 { -1, "q", "q", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
388 { -1, "r", "r", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
390 { -1, "<", "<", 10, NH_CELL_REG, 1, (void *) - 1 },
391 { -1, ".", ".", 11, NH_CELL_REG, 1, (void *) - 1 },
392 { -1, ">", ">", 12, NH_CELL_REG, 1, (void *) - 1 },
393 { -1, " ", "Ctrl", -NH_CMDPAD_FONT_NORMAL, NH_CELL_CTRL, 2, NULL },
394 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
395 NULL }, /* complement for CTRL */
396 { -1, "s", "s", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
397 { -1, "t", "t", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
398 { -1, "u", "u", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
399 { -1, "v", "v", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
400 { -1, "w", "w", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
401 { -1, "x", "x", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
402 { -1, "y", "y", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
403 { -1, "z", "z", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
404 { -1, "\\", "\\", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 }
407 /* uppercase layout */
408 NHCmdPadCell cells_layout_mod2[-NH_CMDPAD_ROWS * -NH_CMDPAD_COLS] = {
409 { -1, "7", "7", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
410 { -1, "8", "8", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
411 { -1, "9", "9", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
412 { -1, "\x1b", "Esc", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 2, NULL },
413 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
414 NULL }, /* complement for ESC */
415 { -1, "?", "?", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
416 { -1, "*", "*", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
417 { -1, "[", "[", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
418 { -1, "(", "(", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
419 { -1, ")", ")", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
420 { -1, "+", "+", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
421 { -1, "=", "=", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
422 { -1, "\"", "\"", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
423 { -1, "$", "$", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
425 { -1, "4", "4", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
426 { -1, "5", "5", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
427 { -1, "6", "6", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
428 { -1, " ", "CAP", -NH_CMDPAD_FONT_NORMAL, NH_CELL_CAP, 2, NULL },
429 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
430 NULL }, /* complement for CAPS */
431 { -1, "A", "A", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
432 { -1, "B", "B", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
433 { -1, "C", "C", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
434 { -1, "D", "D", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
435 { -1, "E", "E", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
436 { -1, "F", "F", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
437 { -1, "G", "G", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
438 { -1, "H", "H", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
439 { -1, "I", "I", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
441 { -1, "1", "1", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
442 { -1, "2", "2", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
443 { -1, "3", "3", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
444 { -1, " ", "Shft", -NH_CMDPAD_FONT_NORMAL, NH_CELL_SHIFT, 2, NULL },
445 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
446 NULL }, /* complement for shift */
447 { -1, "J", "J", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
448 { -1, "K", "K", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
449 { -1, "L", "L", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
450 { -1, "M", "M", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
451 { -1, "N", "N", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
452 { -1, "O", "O", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
453 { -1, "P", "P", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
454 { -1, "Q", "Q", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
455 { -1, "R", "R", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
457 { -1, "<", "<", 10, NH_CELL_REG, 1, (void *) - 1 },
458 { -1, "0", "0", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
459 { -1, ">", ">", 12, NH_CELL_REG, 1, (void *) - 1 },
460 { -1, " ", "Ctrl", -NH_CMDPAD_FONT_NORMAL, NH_CELL_CTRL, 2, NULL },
461 { -1, " ", "", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 0,
462 NULL }, /* complement for CTRL */
463 { -1, "S", "S", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
464 { -1, "T", "T", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
465 { -1, "U", "U", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
466 { -1, "V", "V", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
467 { -1, "W", "W", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
468 { -1, "X", "X", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
469 { -1, "Y", "Y", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
470 { -1, "Z", "Z", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 },
471 { -1, "@", "@", -NH_CMDPAD_FONT_NORMAL, NH_CELL_REG, 1, (void *) - 1 }
474 #endif /* !WIN_CE_SMARTPHONE */
476 /*-------------------------------------------------------------------------*/
478 mswin_init_command_window()
480 static int run_once = 0;
483 /* register window class */
485 register_command_window_class();
491 szNHCmdWindowClass, /* registered class name */
492 NULL, /* window name */
493 WS_CHILD | WS_CLIPSIBLINGS, /* window style */
494 0, /* horizontal position of window - set it later */
495 0, /* vertical position of window - set it later */
496 0, /* window width - set it later */
497 0, /* window height - set it later*/
498 GetNHApp()->hMainWnd, /* handle to parent or owner window */
499 NULL, /* menu handle or child identifier */
500 GetNHApp()->hApp, /* handle to application instance */
501 NULL); /* window-creation data */
503 panic("Cannot create command window");
507 /*-------------------------------------------------------------------------*/
508 /* calculate mimimum window size */
510 mswin_command_window_size(HWND hwnd, LPSIZE sz)
514 data = (PNHCmdWindow) GetWindowLong(hwnd, GWL_USERDATA);
518 CalculateCellSize(hwnd, &cell_size, sz);
519 sz->cx = max(cell_size.cx * nhcmdlayout_columns(data->layout_current)
520 + 2 * GetSystemMetrics(SM_CXBORDER),
522 sz->cy = max(cell_size.cy * nhcmdlayout_rows(data->layout_current)
523 + 2 * GetSystemMetrics(SM_CYBORDER),
527 /*-------------------------------------------------------------------------*/
529 register_command_window_class()
533 ZeroMemory(&wcex, sizeof(wcex));
536 wcex.style = CS_NOCLOSE;
537 wcex.lpfnWndProc = (WNDPROC) NHCommandWndProc;
540 wcex.hInstance = GetNHApp()->hApp;
542 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
543 wcex.hbrBackground = mswin_get_brush(NHW_KEYPAD, MSWIN_COLOR_BG);
544 wcex.lpszMenuName = NULL;
545 wcex.lpszClassName = szNHCmdWindowClass;
547 if (!RegisterClass(&wcex)) {
548 panic("cannot register Map window class");
551 /* create default command set */
552 nhcmdset_current = nhcmdset_default = nhcmdset_create();
554 #ifdef WIN_CE_SMARTPHONE
555 plt = nhcmdlayout_create("General", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
556 nhcmdlayout_init(plt, cells_layout_general);
557 nhcmdset_add(nhcmdset_current, plt);
559 plt = nhcmdlayout_create("Movement", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
560 nhcmdlayout_init(plt, cells_layout_movement);
561 nhcmdset_add(nhcmdset_current, plt);
563 plt = nhcmdlayout_create("Attack", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
564 nhcmdlayout_init(plt, cells_layout_attack);
565 nhcmdset_add(nhcmdset_current, plt);
567 plt = nhcmdlayout_create("Item Handling", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
568 nhcmdlayout_init(plt, cells_layout_item_handling);
569 nhcmdset_add(nhcmdset_current, plt);
571 plt = nhcmdlayout_create("Game Controls", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
572 nhcmdlayout_init(plt, cells_layout_game);
573 nhcmdset_add(nhcmdset_current, plt);
575 plt = nhcmdlayout_create("Advanced Movement", NH_CMDPAD_ROWS,
577 nhcmdlayout_init(plt, cells_layout_adv_movement);
578 nhcmdset_add(nhcmdset_current, plt);
580 plt = nhcmdlayout_create("Item Lookup", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
581 nhcmdlayout_init(plt, cells_layout_lookup);
582 nhcmdset_add(nhcmdset_current, plt);
586 nhcmdlayout_create("Wizard Mode", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
587 nhcmdlayout_init(plt, cells_layout_wizard);
588 nhcmdset_add(nhcmdset_current, plt);
591 #else /* ! WIN_CE_SMARTPHONE */
592 plt = nhcmdlayout_create("lowercase", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
593 nhcmdlayout_init(plt, cells_layout_mod1);
594 nhcmdset_add(nhcmdset_current, plt);
596 plt = nhcmdlayout_create("uppercase", NH_CMDPAD_ROWS, NH_CMDPAD_COLS);
597 nhcmdlayout_init(plt, cells_layout_mod2);
598 nhcmdset_add(nhcmdset_current, plt);
599 #endif /* WIN_CE_SMARTPHONE */
601 /*-------------------------------------------------------------------------*/
603 NHCommandWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
610 onCreate(hWnd, wParam, lParam);
618 LayoutCmdWindow(hWnd);
622 onMouseDown(hWnd, wParam, lParam);
626 /* proceed only if if have mouse focus (set in onMouseDown() -
627 left mouse button is pressed) */
628 if (GetCapture() == hWnd) {
629 onMouseMove(hWnd, wParam, lParam);
637 /* proceed only if if have mouse focus (set in onMouseDown()) */
638 if (GetCapture() == hWnd) {
639 onMouseUp(hWnd, wParam, lParam);
647 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
648 for (i = 0; i <= NH_CMDPAD_FONT_MAX; i++)
650 DeleteObject(data->font[i]);
652 SetWindowLong(hWnd, GWL_USERDATA, (LONG) 0);
656 return DefWindowProc(hWnd, message, wParam, lParam);
660 /*-------------------------------------------------------------------------*/
673 /* get window data */
674 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
676 hDC = BeginPaint(hWnd, &ps);
678 if (!IsRectEmpty(&ps.rcPaint)) {
681 COLORREF OldBg, OldFg;
685 saveFont = SelectObject(hDC, data->font[NH_CMDPAD_FONT_NORMAL]);
686 OldBg = SetBkColor(hDC, mswin_get_color(NHW_KEYPAD, MSWIN_COLOR_BG));
688 SetTextColor(hDC, mswin_get_color(NHW_KEYPAD, MSWIN_COLOR_FG));
690 GetObject(data->images, sizeof(BITMAP), (LPVOID) &bm);
692 hbrPattern = CreatePatternBrush(data->images);
693 hPen = CreatePen(PS_SOLID, 1,
694 mswin_get_color(NHW_KEYPAD, MSWIN_COLOR_FG));
696 for (x = 0, cell_index = 0;
697 x < nhcmdlayout_rows(data->layout_current); x++)
698 for (y = 0; y < nhcmdlayout_columns(data->layout_current);
702 PNHCmdPadCell p_cell_data;
705 nhcmdlayout_cell_direct(data->layout_current, cell_index);
707 /* calculate the cell rectangle */
708 cell_rt.left = data->cells[cell_index].orig.x;
709 cell_rt.top = data->cells[cell_index].orig.y;
710 cell_rt.right = data->cells[cell_index].orig.x
711 + data->cell_size.cx * p_cell_data->mult;
713 data->cells[cell_index].orig.y + data->cell_size.cy;
716 hOldPen = SelectObject(hDC, hPen);
717 pt[0].x = cell_rt.left;
718 pt[0].y = cell_rt.top;
719 pt[1].x = cell_rt.right;
720 pt[1].y = cell_rt.top;
721 pt[2].x = cell_rt.right;
722 pt[2].y = cell_rt.bottom;
723 pt[3].x = cell_rt.left;
724 pt[3].y = cell_rt.bottom;
725 pt[4].x = cell_rt.left;
726 pt[4].y = cell_rt.top;
727 Polyline(hDC, pt, 5);
728 SelectObject(hDC, hOldPen);
730 /* calculate clipping rectangle for the text */
736 /* draw the cell text */
737 if (p_cell_data->image <= 0) {
738 SelectObject(hDC, data->font[-p_cell_data->image]);
739 DrawText(hDC, NH_A2W(p_cell_data->text, wbuf, BUFSZ),
740 strlen(p_cell_data->text), &cell_rt,
741 DT_CENTER | DT_VCENTER | DT_SINGLELINE
748 bmOffset = (p_cell_data->image - 1) * bm.bmHeight;
751 ((cell_rt.left + cell_rt.right)
752 - min(bm.bmHeight, (cell_rt.right - cell_rt.left)))
755 ((cell_rt.bottom + cell_rt.top)
756 - min(bm.bmHeight, (cell_rt.bottom - cell_rt.top)))
760 + min(bm.bmHeight, (cell_rt.right - cell_rt.left));
763 + min(bm.bmHeight, (cell_rt.bottom - cell_rt.top));
765 SetBrushOrgEx(hDC, bitmap_rt.left - bmOffset,
766 bitmap_rt.top, NULL);
767 oldBr = SelectObject(hDC, hbrPattern);
768 PatBlt(hDC, bitmap_rt.left, bitmap_rt.top,
769 bitmap_rt.right - bitmap_rt.left,
770 bitmap_rt.bottom - bitmap_rt.top, PATCOPY);
771 SelectObject(hDC, oldBr);
774 /* invert the cell if it is selected */
775 if (data->cells[cell_index].state == NH_CST_CHECKED) {
776 IntersectRect(&cell_rt, &cell_rt, &ps.rcPaint);
777 PatBlt(hDC, cell_rt.left, cell_rt.top,
778 cell_rt.right - cell_rt.left,
779 cell_rt.bottom - cell_rt.top, DSTINVERT);
783 SetTextColor(hDC, OldFg);
784 SetBkColor(hDC, OldBg);
785 SelectObject(hDC, saveFont);
786 DeleteObject(hbrPattern);
791 /*-------------------------------------------------------------------------*/
793 onCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
797 /* set window data */
798 data = (PNHCmdWindow) malloc(sizeof(NHCmdWindow));
800 panic("out of memory");
802 ZeroMemory(data, sizeof(NHCmdWindow));
803 SetWindowLong(hWnd, GWL_USERDATA, (LONG) data);
805 data->active_cell = -1;
807 /* load images bitmap */
808 data->images = LoadBitmap(GetNHApp()->hApp, MAKEINTRESOURCE(IDB_KEYPAD));
810 panic("cannot load keypad bitmap");
812 /* create default layouts */
813 data->layout_current = 0;
814 data->layout_save = 0;
817 #if defined(WIN_CE_SMARTPHONE)
818 data->layout_selected = nhcmdset_get(nhcmdset_current, 0);
821 /* set default layout */
822 SetCmdWindowLayout(hWnd, nhcmdset_get(nhcmdset_current, 0));
824 /*-------------------------------------------------------------------------*/
826 LayoutCmdWindow(HWND hWnd)
836 GetClientRect(hWnd, &clrt);
837 if (IsRectEmpty(&clrt))
840 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
841 if (!data->layout_current)
844 /* calculate cell size */
845 windowSize.cx = clrt.right - clrt.left;
846 windowSize.cy = clrt.bottom - clrt.top;
847 CalculateCellSize(hWnd, &data->cell_size, &windowSize);
849 /* initialize display cells aray */
852 for (i = 0, index = 0; i < nhcmdlayout_rows(data->layout_current); i++) {
853 for (j = 0; j < nhcmdlayout_columns(data->layout_current);
855 data->cells[index].orig.x = x;
856 data->cells[index].orig.y = y;
857 data->cells[index].type =
858 nhcmdlayout_cell_direct(data->layout_current, index)->type;
860 switch (data->cells[index].type) {
862 data->cells[index].state = data->is_ctrl ? NH_CST_CHECKED : 0;
865 data->cells[index].state = data->is_caps ? NH_CST_CHECKED : 0;
868 data->cells[index].state =
869 data->is_shift ? NH_CST_CHECKED : 0;
872 data->cells[index].state = 0;
875 x += data->cell_size.cx
876 * nhcmdlayout_cell_direct(data->layout_current, index)->mult;
879 y += data->cell_size.cy;
882 /* create font for display cell text */
883 for (i = 0; i <= NH_CMDPAD_FONT_MAX; i++)
885 DeleteObject(data->font[i]);
887 ZeroMemory(&lgfnt, sizeof(lgfnt));
888 lgfnt.lfHeight = data->cell_size.cy; // height of font
889 lgfnt.lfWidth = 0; // average character width
890 lgfnt.lfEscapement = 0; // angle of escapement
891 lgfnt.lfOrientation = 0; // base-line orientation angle
892 lgfnt.lfWeight = FW_NORMAL; // font weight
893 lgfnt.lfItalic = FALSE; // italic attribute option
894 lgfnt.lfUnderline = FALSE; // underline attribute option
895 lgfnt.lfStrikeOut = FALSE; // strikeout attribute option
896 lgfnt.lfCharSet = ANSI_CHARSET; // character set identifier
897 lgfnt.lfOutPrecision = OUT_DEFAULT_PRECIS; // output precision
898 lgfnt.lfClipPrecision = CLIP_CHARACTER_PRECIS; // clipping precision
899 lgfnt.lfQuality = DEFAULT_QUALITY; // output quality
900 if (iflags.wc_font_message && *iflags.wc_font_message) {
901 lgfnt.lfPitchAndFamily = DEFAULT_PITCH; // pitch and family
902 NH_A2W(iflags.wc_font_message, lgfnt.lfFaceName, LF_FACESIZE);
904 lgfnt.lfPitchAndFamily = VARIABLE_PITCH; // pitch and family
906 data->font[NH_CMDPAD_FONT_NORMAL] = CreateFontIndirect(&lgfnt);
908 InvalidateRect(hWnd, NULL, TRUE);
910 /*-------------------------------------------------------------------------*/
912 SetCmdWindowLayout(HWND hWnd, PNHCmdLayout layout)
917 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
918 if (data->layout_current == layout)
921 data->layout_current = layout;
922 size = sizeof(NHCmdPadLayoutCell) * nhcmdlayout_rows(layout)
923 * nhcmdlayout_columns(layout);
924 data->cells = (PNHCmdPadLayoutCell) realloc(data->cells, size);
925 ZeroMemory(data->cells, size);
926 LayoutCmdWindow(hWnd);
928 /*-------------------------------------------------------------------------*/
930 onMouseDown(HWND hWnd, WPARAM wParam, LPARAM lParam)
935 /* get mouse coordinates */
936 mpt.x = LOWORD(lParam);
937 mpt.y = HIWORD(lParam);
939 /* map mouse coordinates to the display cell */
940 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
941 data->active_cell = CellFromPoint(data, mpt);
942 if (data->active_cell == -1)
945 /* set mouse focus to the current window */
948 /* invert the selection */
949 HighlightCell(hWnd, data->active_cell,
950 (data->cells[data->active_cell].state != NH_CST_CHECKED));
952 /*-------------------------------------------------------------------------*/
954 onMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
960 /* get mouse coordinates */
961 mpt.x = LOWORD(lParam);
962 mpt.y = HIWORD(lParam);
964 /* map mouse coordinates to the display cell */
965 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
966 newActiveCell = CellFromPoint(data, mpt);
967 if (data->active_cell == -1)
970 /* if mouse is within orginal display cell - select the cell otherwise
971 clear the selection */
972 switch (nhcmdlayout_cell_direct(data->layout_current, data->active_cell)
975 HighlightCell(hWnd, data->active_cell,
976 (newActiveCell == data->active_cell));
980 HighlightCell(hWnd, data->active_cell,
981 ((newActiveCell == data->active_cell) ? !data->is_ctrl
986 HighlightCell(hWnd, data->active_cell,
987 ((newActiveCell == data->active_cell) ? !data->is_caps
992 /*-------------------------------------------------------------------------*/
994 onMouseUp(HWND hWnd, WPARAM wParam, LPARAM lParam)
998 /* release mouse capture */
1001 /* get active display cell */
1002 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
1003 if (data->active_cell == -1)
1006 ActivateCell(hWnd, data->active_cell);
1008 data->active_cell = -1;
1010 /*-------------------------------------------------------------------------*/
1012 ActivateCell(HWND hWnd, int cell)
1015 PNHCmdPadCell p_cell_data;
1018 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
1021 p_cell_data = nhcmdlayout_cell_direct(data->layout_current, cell);
1023 /* act depending on the cell type:
1024 CAPS - change layout
1025 CTRL - modify CTRL status
1026 REG - place keyboard event on the nethack input queue
1028 switch (p_cell_data->type) {
1030 if (data->is_ctrl) {
1031 PushNethackCommand(p_cell_data->f_char, 1);
1034 for (i = 0; i < nhcmdlayout_rows(data->layout_current)
1035 * nhcmdlayout_columns(data->layout_current);
1037 if (nhcmdlayout_cell_direct(data->layout_current, i)->type
1039 HighlightCell(hWnd, i, data->is_ctrl);
1043 PushNethackCommand(p_cell_data->f_char, 0);
1045 HighlightCell(hWnd, cell, FALSE);
1047 // select a new layout if present
1048 i = (int) p_cell_data->data;
1050 if (data->layout_save)
1051 SetCmdWindowLayout(hWnd, data->layout_save);
1052 data->layout_save = NULL;
1054 if (!data->layout_save)
1055 data->layout_save = data->layout_current;
1056 SetCmdWindowLayout(hWnd, nhcmdset_get(nhcmdset_current, i));
1059 if (!data->is_shift)
1061 // else fall through and reset the shift
1064 data->is_shift = !data->is_shift;
1065 SetCmdWindowLayout(hWnd, (data->is_shift ^ data->is_caps)
1066 ? nhcmdset_get(nhcmdset_current, 1)
1067 : nhcmdset_get(nhcmdset_current, 0));
1068 data->cells[cell].state = data->is_shift ? NH_CST_CHECKED : 0;
1069 InvalidateRect(hWnd, NULL, TRUE);
1073 data->is_ctrl = !data->is_ctrl;
1074 HighlightCell(hWnd, cell, data->is_ctrl);
1078 data->is_caps = !data->is_caps;
1079 SetCmdWindowLayout(hWnd, (data->is_shift ^ data->is_caps)
1080 ? nhcmdset_get(nhcmdset_current, 1)
1081 : nhcmdset_get(nhcmdset_current, 0));
1082 data->cells[cell].state = data->is_caps ? NH_CST_CHECKED : 0;
1083 InvalidateRect(hWnd, NULL, TRUE);
1086 case NH_CELL_LAYOUT_NEW: {
1087 PNHCmdLayout pLayout;
1089 HighlightCell(hWnd, cell, FALSE);
1091 pLayout = (PNHCmdLayout) p_cell_data->data;
1093 SetCmdWindowLayout(hWnd, pLayout);
1097 case NH_CELL_LAYOUT_MENU: {
1101 menu_item *selected = 0;
1104 HighlightCell(hWnd, cell, FALSE);
1106 pSet = (PNHCmdSet) p_cell_data->data;
1108 pSet = nhcmdset_default;
1110 wid = mswin_create_nhwindow(NHW_MENU);
1111 mswin_start_menu(wid);
1112 for (i = 0; i < nhcmdset_count(pSet); i++) {
1113 any.a_void = nhcmdset_get(pSet, i);
1114 mswin_add_menu(wid, NO_GLYPH, &any, 'a' + i, 0, ATR_NONE,
1115 nhcmdset_get_name(pSet, i), FALSE);
1117 mswin_end_menu(wid, "Select keypad layout");
1118 i = select_menu(wid, PICK_ONE, &selected);
1119 mswin_destroy_nhwindow(wid);
1122 #if defined(WIN_CE_SMARTPHONE)
1123 data->layout_selected = (PNHCmdLayout) selected[0].item.a_void;
1125 SetCmdWindowLayout(hWnd, (PNHCmdLayout) selected[0].item.a_void);
1130 /*-------------------------------------------------------------------------*/
1132 CellFromPoint(PNHCmdWindow data, POINT pt)
1135 for (i = 0; i < nhcmdlayout_rows(data->layout_current)
1136 * nhcmdlayout_columns(data->layout_current);
1139 cell_rt.left = data->cells[i].orig.x;
1140 cell_rt.top = data->cells[i].orig.y;
1142 data->cells[i].orig.x
1143 + data->cell_size.cx
1144 * nhcmdlayout_cell_direct(data->layout_current, i)->mult;
1145 cell_rt.bottom = data->cells[i].orig.y + data->cell_size.cy;
1146 if (PtInRect(&cell_rt, pt))
1151 /*-------------------------------------------------------------------------*/
1153 CalculateCellSize(HWND hWnd, LPSIZE pSize, LPSIZE pWindowSize)
1157 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
1163 /* if windows size is specified - attempt ro stretch cells across
1164 the the window size. If not - make default cell size based on
1165 10 points font. Make sure that cell cesize does not exceeds 20 points
1167 if (pWindowSize->cx > 0)
1169 pWindowSize->cx / nhcmdlayout_columns(data->layout_current);
1171 pSize->cx = 10 * GetDeviceCaps(hdc, LOGPIXELSX) / 72;
1172 pSize->cx = min(pSize->cx, 20 * GetDeviceCaps(hdc, LOGPIXELSX) / 72);
1174 if (pWindowSize->cy > 0)
1175 pSize->cy = pWindowSize->cy / nhcmdlayout_rows(data->layout_current);
1177 pSize->cy = 10 * GetDeviceCaps(hdc, LOGPIXELSY) / 72;
1178 pSize->cy = min(pSize->cy, 20 * GetDeviceCaps(hdc, LOGPIXELSY) / 72);
1180 ReleaseDC(hWnd, hdc);
1182 /*-------------------------------------------------------------------------*/
1184 HighlightCell(HWND hWnd, int cell, BOOL isSelected)
1190 data = (PNHCmdWindow) GetWindowLong(hWnd, GWL_USERDATA);
1191 prevState = data->cells[cell].state;
1192 data->cells[cell].state = (isSelected) ? NH_CST_CHECKED : 0;
1194 if (prevState != data->cells[cell].state) {
1196 PatBlt(hDC, data->cells[cell].orig.x + 1,
1197 data->cells[cell].orig.y + 1,
1199 * nhcmdlayout_cell_direct(data->layout_current, cell)
1202 data->cell_size.cy - 2, DSTINVERT);
1203 ReleaseDC(hWnd, hDC);
1206 /*-------------------------------------------------------------------------*/
1208 PushNethackCommand(const char *cmd_char_str, int is_ctrl)
1210 while (*cmd_char_str) {
1212 NHEVENT_KBD(C(*cmd_char_str));
1214 NHEVENT_KBD(*cmd_char_str);
1220 /*-------------------------------------------------------------------------*/
1221 /*------------------- keyboard keys layout functions ----------------------*/
1222 /*-------------------------------------------------------------------------*/
1224 nhcmdlayout_create(const char *name, int rows, int columns)
1229 i = sizeof(NHCmdLayout) + rows * columns * sizeof(NHCmdPadCell);
1230 p = (PNHCmdLayout) malloc(i);
1233 p->columns = columns;
1234 strncpy(p->name, name, sizeof(p->name) - 1);
1235 for (i = 0; i < rows * columns; i++) {
1236 p->cells[i].cmd_code = -1;
1237 p->cells[i].image = -NH_CMDPAD_FONT_NORMAL;
1238 p->cells[i].type = 1;
1239 p->cells[i].mult = 1;
1243 /*-------------------------------------------------------------------------*/
1245 nhcmdlayout_init(PNHCmdLayout p, PNHCmdPadCell cells)
1247 memcpy(p->cells, cells, p->rows * p->columns * sizeof(NHCmdPadCell));
1251 nhcmdlayout_destroy(PNHCmdLayout p)
1256 /*-------------------------------------------------------------------------*/
1257 /*----------------- keyboard keys layout set functions --------------------*/
1258 /*-------------------------------------------------------------------------*/
1263 p = (PNHCmdSet) malloc(sizeof(NHCmdSet));
1264 ZeroMemory(p, sizeof(NHCmdSet));
1267 /*-------------------------------------------------------------------------*/
1269 nhcmdset_count(PNHCmdSet p)
1274 /*-------------------------------------------------------------------------*/
1276 nhcmdset_get(PNHCmdSet p, int index)
1279 assert(index >= 0 && index < p->count);
1280 return p->elements[index].layout;
1282 /*-------------------------------------------------------------------------*/
1284 nhcmdset_get_name(PNHCmdSet p, int index)
1287 assert(index >= 0 && index < p->count);
1288 return p->elements[index].layout->name;
1290 /*-------------------------------------------------------------------------*/
1292 nhcmdset_add(PNHCmdSet p, PNHCmdLayout layout)
1295 assert(p->count < NH_CMDSET_MAXSIZE);
1296 p->elements[p->count].layout = layout;
1297 p->elements[p->count].free_on_destroy = 0;
1300 /*-------------------------------------------------------------------------*/
1302 nhcmdset_destroy(PNHCmdSet p)
1306 for (i = 0; i < p->count; i++) {
1307 if (p->elements[i].free_on_destroy) {
1308 nhcmdlayout_destroy(p->elements[i].layout);
1313 /*-------------------------------------------------------------------------*/
1315 #if defined(WIN_CE_SMARTPHONE)
1316 /* special keypad input handling for SmartPhone
1317 the phone keypad maps to VK_* as shown below.
1318 some keys might not be present, e.g. VK_TFLIP
1319 sofkey1 softkey2 VK_TSOFT1, VK_TSOFT2
1321 < + > VK_TLEFT, VK_TACTION, VK_TRIGHT
1323 home back VK_THOME, VK_TBACK
1324 talk end VK_TTALK, VK_TEND
1328 * 0 # VK_TSTAR, VK_TPOUND
1329 other buttons include
1331 VK_TPOWER, VK_TVOLUMEUP, VK_TVOLUMEDOWN
1335 NHSPhoneTranslateKbdMessage(WPARAM wParam, LPARAM lParam, BOOL keyDown)
1340 /* get window data */
1341 data = (PNHCmdWindow) GetWindowLong(GetNHApp()->hCmdWnd, GWL_USERDATA);
1396 HighlightCell(GetNHApp()->hCmdWnd, index, keyDown);
1398 ActivateCell(GetNHApp()->hCmdWnd, index);
1404 /*-------------------------------------------------------------------------*/
1406 NHSPhoneSetKeypadFromString(const char *str)
1410 PNHCmdLayout layout_prev = 0;
1411 PNHCmdLayout layout_cur = 0;
1416 assert(NH_CMDPAD_ROWS == 4);
1418 data = (PNHCmdWindow) GetWindowLong(GetNHApp()->hCmdWnd, GWL_USERDATA);
1422 p = nhcmdset_create();
1424 ZeroMemory(buf, sizeof(buf));
1425 if (sscanf(str, "%s or %s", buf[1], buf[0]) != 2) {
1426 ZeroMemory(buf, sizeof(buf));
1427 strncpy(buf[0], str, sizeof(buf[0]) - 1);
1430 lcount = 10; /* create new layout on the first iteration */
1431 for (i = 0; i < 2; i++) {
1434 char c_start, c_end, c_char;
1436 /* parse character ranges */
1437 if (isalnum((c_start = s[0])) && s[1] == '-'
1438 && isalnum((c_end = s[2]))) {
1441 c_start = c_end = *s;
1444 for (c_char = c_start; c_char <= c_end; c_char++) {
1448 layout_prev = layout_cur;
1449 layout_cur = nhcmdlayout_create("noname", NH_CMDPAD_ROWS,
1451 nhcmdlayout_init(layout_cur, cells_layout_menu);
1453 nhcmdlayout_cell(layout_cur, 3, 0)->data = layout_prev;
1454 nhcmdlayout_cell(layout_cur, 3, 2)->data = 0;
1456 nhcmdset_add(p, layout_cur);
1457 p->elements[p->count - 1].free_on_destroy = 1;
1460 nhcmdlayout_cell(layout_prev, 3, 2)->data =
1466 lcount = 10; // skip '#'
1467 nhcmdlayout_cell_direct(layout_cur, lcount)->f_char[0] =
1469 if (c_char == '\033') {
1470 strcpy(nhcmdlayout_cell_direct(layout_cur, lcount)->text,
1472 nhcmdlayout_cell_direct(layout_cur, lcount)->image =
1473 14; /* 14 is a ESC symbol in IDB_KEYPAD */
1475 nhcmdlayout_cell_direct(layout_cur, lcount)->text[0] =
1477 nhcmdlayout_cell_direct(layout_cur, lcount)->text[1] =
1481 /* increment character count in the current layout */
1485 /* prepareg next charcter from the source string */
1490 /* install the new set */
1491 if (nhcmdset_current != nhcmdset_default)
1492 nhcmdset_destroy(nhcmdset_current);
1493 nhcmdset_current = p;
1494 SetCmdWindowLayout(GetNHApp()->hCmdWnd,
1495 nhcmdset_get(nhcmdset_current, 0));
1497 /*-------------------------------------------------------------------------*/
1499 NHSPhoneSetKeypadDirection()
1503 data = (PNHCmdWindow) GetWindowLong(GetNHApp()->hCmdWnd, GWL_USERDATA);
1507 if (nhcmdset_current != nhcmdset_default)
1508 nhcmdset_destroy(nhcmdset_current);
1509 nhcmdset_current = nhcmdset_default;
1510 SetCmdWindowLayout(GetNHApp()->hCmdWnd,
1511 nhcmdset_get(nhcmdset_current, NH_LAYOUT_MOVEMENT));
1513 /*-------------------------------------------------------------------------*/
1515 NHSPhoneSetKeypadDefault()
1519 data = (PNHCmdWindow) GetWindowLong(GetNHApp()->hCmdWnd, GWL_USERDATA);
1523 if (nhcmdset_current != nhcmdset_default)
1524 nhcmdset_destroy(nhcmdset_current);
1525 nhcmdset_current = nhcmdset_default;
1526 SetCmdWindowLayout(GetNHApp()->hCmdWnd,
1527 data->layout_selected
1528 ? data->layout_selected
1529 : nhcmdset_get(nhcmdset_current, 0));
1532 #endif /* defined (WIN_CE_SMARTHPONE) */