OSDN Git Service

既に忘れた何かの理由によって、長い事tabをスペース8文字にする設定で
[hengband/hengband.git] / src / main-win.c
1 /* File: main-win.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, Skirmantas Kligys, and others
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.
9  */
10
11
12 /*
13  * This file helps Angband work with Windows computers.
14  *
15  * To use this file, use an appropriate "Makefile" or "Project File",
16  * make sure that "WINDOWS" and/or "WIN32" are defined somewhere, and
17  * make sure to obtain various extra files as described below.
18  *
19  * The official compilation uses the CodeWarrior Pro compiler, which
20  * includes a special project file and precompilable header file.
21  *
22  *
23  * See also "main-dos.c" and "main-ibm.c".
24  *
25  *
26  * The "lib/user/pref-win.prf" file contains keymaps, macro definitions,
27  * and/or color redefinitions.
28  *
29  * The "lib/user/font-win.prf" contains attr/char mappings for use with the
30  * normal "lib/xtra/font/*.fon" font files.
31  *
32  * The "lib/user/graf-win.prf" contains attr/char mappings for use with the
33  * special "lib/xtra/graf/*.bmp" bitmap files, which are activated by a menu
34  * item.
35  *
36  *
37  * Compiling this file, and using the resulting executable, requires
38  * several extra files not distributed with the standard Angband code.
39  * If "USE_GRAPHICS" is defined, then "readdib.h" and "readdib.c" must
40  * be placed into "src/", and the "8X8.BMP" bitmap file must be placed
41  * into "lib/xtra/graf".  In any case, some "*.fon" files (including
42  * "8X13.FON" if nothing else) must be placed into "lib/xtra/font/".
43  * If "USE_SOUND" is defined, then some special library (for example,
44  * "winmm.lib") may need to be linked in, and desired "*.WAV" sound
45  * files must be placed into "lib/xtra/sound/".  All of these extra
46  * files can be found in the "ext-win" archive.
47  *
48  *
49  * The "Term_xtra_win_clear()" function should probably do a low-level
50  * clear of the current window, and redraw the borders and other things,
51  * if only for efficiency.  XXX XXX XXX
52  *
53  * A simpler method is needed for selecting the "tile size" for windows.
54  * XXX XXX XXX
55  *
56  * The various "warning" messages assume the existance of the "screen.w"
57  * window, I think, and only a few calls actually check for its existance,
58  * this may be okay since "NULL" means "on top of all windows". (?)  The
59  * user must never be allowed to "hide" the main window, or the "menubar"
60  * will disappear.  XXX XXX XXX
61  *
62  * Special "Windows Help Files" can be placed into "lib/xtra/help/" for
63  * use with the "winhelp.exe" program.  These files *may* be available
64  * at the ftp site somewhere, but I have not seen them.  XXX XXX XXX
65  *
66  *
67  * Initial framework (and most code) by Ben Harrison (benh@phial.com).
68  *
69  * Original code by Skirmantas Kligys (kligys@scf.usc.edu).
70  *
71  * Additional code by Ross E Becker (beckerr@cis.ohio-state.edu),
72  * and Chris R. Martin (crm7479@tam2000.tamu.edu).
73  */
74
75 #include "angband.h"
76
77
78 #ifdef WINDOWS
79
80 #include <direct.h>
81
82 /*
83  * Extract the "WIN32" flag from the compiler
84  */
85 #if defined(__WIN32__) || defined(__WINNT__) || defined(__NT__)
86 # ifndef WIN32
87 #  define WIN32
88 # endif
89 #endif
90
91
92 /*
93  * Hack -- allow use of "screen saver" mode
94  */
95 #define USE_SAVER
96
97
98 /*
99  * Menu constants -- see "ANGBAND.RC"
100  */
101
102 #define IDM_FILE_NEW                    100
103 #define IDM_FILE_OPEN                   101
104 #define IDM_FILE_SAVE                   110
105 #define IDM_FILE_SCORE                  120
106 #define IDM_FILE_EXIT                   130
107
108 #define IDM_WINDOW_VIS_0                200
109 #define IDM_WINDOW_VIS_1                201
110 #define IDM_WINDOW_VIS_2                202
111 #define IDM_WINDOW_VIS_3                203
112 #define IDM_WINDOW_VIS_4                204
113 #define IDM_WINDOW_VIS_5                205
114 #define IDM_WINDOW_VIS_6                206
115 #define IDM_WINDOW_VIS_7                207
116
117 #define IDM_WINDOW_FONT_0               210
118 #define IDM_WINDOW_FONT_1               211
119 #define IDM_WINDOW_FONT_2               212
120 #define IDM_WINDOW_FONT_3               213
121 #define IDM_WINDOW_FONT_4               214
122 #define IDM_WINDOW_FONT_5               215
123 #define IDM_WINDOW_FONT_6               216
124 #define IDM_WINDOW_FONT_7               217
125
126 #define IDM_WINDOW_POS_0                220
127 #define IDM_WINDOW_POS_1                221
128 #define IDM_WINDOW_POS_2                222
129 #define IDM_WINDOW_POS_3                223
130 #define IDM_WINDOW_POS_4                224
131 #define IDM_WINDOW_POS_5                225
132 #define IDM_WINDOW_POS_6                226
133 #define IDM_WINDOW_POS_7                227
134
135 #define IDM_WINDOW_BIZ_0                230
136 #define IDM_WINDOW_BIZ_1                231
137 #define IDM_WINDOW_BIZ_2                232
138 #define IDM_WINDOW_BIZ_3                233
139 #define IDM_WINDOW_BIZ_4                234
140 #define IDM_WINDOW_BIZ_5                235
141 #define IDM_WINDOW_BIZ_6                236
142 #define IDM_WINDOW_BIZ_7                237
143
144 #define IDM_WINDOW_I_WID_0              240
145 #define IDM_WINDOW_I_WID_1              241
146 #define IDM_WINDOW_I_WID_2              242
147 #define IDM_WINDOW_I_WID_3              243
148 #define IDM_WINDOW_I_WID_4              244
149 #define IDM_WINDOW_I_WID_5              245
150 #define IDM_WINDOW_I_WID_6              246
151 #define IDM_WINDOW_I_WID_7              247
152
153 #define IDM_WINDOW_D_WID_0              250
154 #define IDM_WINDOW_D_WID_1              251
155 #define IDM_WINDOW_D_WID_2              252
156 #define IDM_WINDOW_D_WID_3              253
157 #define IDM_WINDOW_D_WID_4              254
158 #define IDM_WINDOW_D_WID_5              255
159 #define IDM_WINDOW_D_WID_6              256
160 #define IDM_WINDOW_D_WID_7              257
161
162 #define IDM_WINDOW_I_HGT_0              260
163 #define IDM_WINDOW_I_HGT_1              261
164 #define IDM_WINDOW_I_HGT_2              262
165 #define IDM_WINDOW_I_HGT_3              263
166 #define IDM_WINDOW_I_HGT_4              264
167 #define IDM_WINDOW_I_HGT_5              265
168 #define IDM_WINDOW_I_HGT_6              266
169 #define IDM_WINDOW_I_HGT_7              267
170
171 #define IDM_WINDOW_D_HGT_0              270
172 #define IDM_WINDOW_D_HGT_1              271
173 #define IDM_WINDOW_D_HGT_2              272
174 #define IDM_WINDOW_D_HGT_3              273
175 #define IDM_WINDOW_D_HGT_4              274
176 #define IDM_WINDOW_D_HGT_5              275
177 #define IDM_WINDOW_D_HGT_6              276
178 #define IDM_WINDOW_D_HGT_7              277
179
180 #define IDM_OPTIONS_NO_GRAPHICS  400
181 #define IDM_OPTIONS_OLD_GRAPHICS 401
182 #define IDM_OPTIONS_NEW_GRAPHICS 402
183 #define IDM_OPTIONS_BIGTILE             409
184 #define IDM_OPTIONS_SOUND               410
185 #define IDM_OPTIONS_SAVER               420
186 #define IDM_OPTIONS_MAP                 430
187 #define IDM_OPTIONS_BG                  440
188 #define IDM_OPTIONS_OPEN_BG             441
189
190 #define IDM_DUMP_SCREEN_HTML    450
191
192 #define IDM_HELP_CONTENTS       901
193
194
195
196 /*
197  * This may need to be removed for some compilers XXX XXX XXX
198  */
199 #define STRICT
200
201 /*
202  * Exclude parts of WINDOWS.H that are not needed
203  */
204 #define NOCOMM            /* Comm driver APIs and definitions */
205 #define NOLOGERROR        /* LogError() and related definitions */
206 #define NOPROFILER        /* Profiler APIs */
207 #define NOLFILEIO         /* _l* file I/O routines */
208 #define NOOPENFILE        /* OpenFile and related definitions */
209 #define NORESOURCE        /* Resource management */
210 #define NOATOM            /* Atom management */
211 #define NOLANGUAGE        /* Character test routines */
212 #define NOLSTRING         /* lstr* string management routines */
213 #define NODBCS            /* Double-byte character set routines */
214 #define NOKEYBOARDINFO    /* Keyboard driver routines */
215 #define NOCOLOR           /* COLOR_* color values */
216 #define NODRAWTEXT        /* DrawText() and related definitions */
217 #define NOSCALABLEFONT    /* Truetype scalable font support */
218 #define NOMETAFILE        /* Metafile support */
219 #define NOSYSTEMPARAMSINFO /* SystemParametersInfo() and SPI_* definitions */
220 #define NODEFERWINDOWPOS  /* DeferWindowPos and related definitions */
221 #define NOKEYSTATES       /* MK_* message key state flags */
222 #define NOWH              /* SetWindowsHook and related WH_* definitions */
223 #define NOCLIPBOARD       /* Clipboard APIs and definitions */
224 #define NOICONS           /* IDI_* icon IDs */
225 #define NOMDI             /* MDI support */
226 #define NOHELP            /* Help support */
227
228 /* Not defined since it breaks Borland C++ 5.5 */
229 /* #define NOCTLMGR */    /* Control management and controls */
230
231 /*
232  * Exclude parts of WINDOWS.H that are not needed (Win32)
233  */
234 #define WIN32_LEAN_AND_MEAN
235 #define NONLS             /* All NLS defines and routines */
236 #define NOSERVICE         /* All Service Controller routines, SERVICE_ equates, etc. */
237 #define NOKANJI           /* Kanji support stuff. */
238 #define NOMCX             /* Modem Configuration Extensions */
239
240 /*
241  * Include the "windows" support file
242  */
243 #include <windows.h>
244
245 /*
246  * Exclude parts of MMSYSTEM.H that are not needed
247  */
248 #define MMNODRV          /* Installable driver support */
249 #define MMNOWAVE         /* Waveform support */
250 #define MMNOMIDI         /* MIDI support */
251 #define MMNOAUX          /* Auxiliary audio support */
252 #define MMNOTIMER        /* Timer support */
253 #define MMNOJOY          /* Joystick support */
254 #define MMNOMCI          /* MCI support */
255 #define MMNOMMIO         /* Multimedia file I/O support */
256 #define MMNOMMSYSTEM     /* General MMSYSTEM functions */
257
258 /*
259  * Include some more files. Note: the Cygnus Cygwin compiler
260  * doesn't use mmsystem.h instead it includes the winmm library
261  * which performs a similar function.
262  */
263 #include <mmsystem.h>
264 #include <commdlg.h>
265
266 /*
267  * HTML-Help requires htmlhelp.h and htmlhelp.lib from Microsoft's
268  * HTML Workshop < http://msdn.microsoft.com/workshop/author/htmlhelp/ >.
269  */
270 /* #define HTML_HELP */
271
272 #ifdef HTML_HELP
273 #include <htmlhelp.h>
274 #endif /* HTML_HELP */
275
276 /*
277  * Include the support for loading bitmaps
278  */
279 #ifdef USE_GRAPHICS
280 # include "readdib.h"
281 #endif
282
283 /*
284  * Hack -- Fake declarations from "dos.h" XXX XXX XXX
285  */
286 #ifdef WIN32
287 #define INVALID_FILE_NAME (DWORD)0xFFFFFFFF
288 #else /* WIN32 */
289 #define FA_LABEL    0x08        /* Volume label */
290 #define FA_DIREC    0x10        /* Directory */
291 unsigned _cdecl _dos_getfileattr(const char *, unsigned *);
292 #endif /* WIN32 */
293
294 /*
295  * Silliness in WIN32 drawing routine
296  */
297 #ifdef WIN32
298 # define MoveTo(H,X,Y) MoveToEx(H, X, Y, NULL)
299 #endif /* WIN32 */
300
301 /*
302  * Silliness for Windows 95
303  */
304 #ifndef WS_EX_TOOLWINDOW
305 # define WS_EX_TOOLWINDOW 0
306 #endif
307
308 /*
309  * Foreground color bits (hard-coded by DOS)
310  */
311 #define VID_BLACK       0x00
312 #define VID_BLUE        0x01
313 #define VID_GREEN       0x02
314 #define VID_CYAN        0x03
315 #define VID_RED         0x04
316 #define VID_MAGENTA     0x05
317 #define VID_YELLOW      0x06
318 #define VID_WHITE       0x07
319
320 /*
321  * Bright text (hard-coded by DOS)
322  */
323 #define VID_BRIGHT      0x08
324
325 /*
326  * Background color bits (hard-coded by DOS)
327  */
328 #define VUD_BLACK       0x00
329 #define VUD_BLUE        0x10
330 #define VUD_GREEN       0x20
331 #define VUD_CYAN        0x30
332 #define VUD_RED         0x40
333 #define VUD_MAGENTA     0x50
334 #define VUD_YELLOW      0x60
335 #define VUD_WHITE       0x70
336
337 /*
338  * Blinking text (hard-coded by DOS)
339  */
340 #define VUD_BRIGHT      0x80
341
342
343 /*
344  * Forward declare
345  */
346 typedef struct _term_data term_data;
347
348 /*
349  * Extra "term" data
350  *
351  * Note the use of "font_want" for the names of the font file requested by
352  * the user, and the use of "font_file" for the currently active font file.
353  *
354  * The "font_file" is uppercased, and takes the form "8X13.FON", while
355  * "font_want" can be in almost any form as long as it could be construed
356  * as attempting to represent the name of a font.
357  */
358 struct _term_data
359 {
360         term t;
361
362         cptr s;
363
364         HWND w;
365
366         DWORD dwStyle;
367         DWORD dwExStyle;
368
369         uint keys;
370
371         uint rows;      /* int -> uint */
372         uint cols;
373
374         uint pos_x;
375         uint pos_y;
376         uint size_wid;
377         uint size_hgt;
378         uint size_ow1;
379         uint size_oh1;
380         uint size_ow2;
381         uint size_oh2;
382
383         bool size_hack;
384
385         bool xtra_hack;
386
387         bool visible;
388
389         bool bizarre;
390
391         cptr font_want;
392
393         cptr font_file;
394
395         HFONT font_id;
396
397         uint font_wid;
398         uint font_hgt;
399
400         uint tile_wid;
401         uint tile_hgt;
402
403         uint map_tile_wid;
404         uint map_tile_hgt;
405
406         bool map_active;
407 #ifdef JP
408         LOGFONT lf;
409 #endif
410
411         bool posfix;
412
413 /* bg */
414 #if 0
415         char *bgfile;
416         int use_bg;
417 #endif
418 };
419
420
421 /*
422  * Maximum number of windows XXX XXX XXX
423  */
424 #define MAX_TERM_DATA 8
425
426 /*
427  * An array of term_data's
428  */
429 static term_data data[MAX_TERM_DATA];
430
431 /*
432  * Hack -- global "window creation" pointer
433  */
434 static term_data *my_td;
435
436 /*
437  * game in progress
438  */
439 bool game_in_progress = FALSE;
440
441 /*
442  * note when "open"/"new" become valid
443  */
444 bool initialized = FALSE;
445
446 /*
447  * screen paletted, i.e. 256 colors
448  */
449 bool paletted = FALSE;
450
451 /*
452  * 16 colors screen, don't use RGB()
453  */
454 bool colors16 = FALSE;
455
456 /*
457  * Saved instance handle
458  */
459 static HINSTANCE hInstance;
460
461 /*
462  * Yellow brush for the cursor
463  */
464 static HBRUSH hbrYellow;
465
466 /*
467  * An icon
468  */
469 static HICON hIcon;
470
471 /*
472  * A palette
473  */
474 static HPALETTE hPal;
475
476 /* bg */
477 static HBITMAP hBG = NULL;
478 static int use_bg = 0;
479 static char bg_bitmap_file[1024] = "bg.bmp";
480
481 #ifdef USE_SAVER
482
483 /*
484  * The screen saver window
485  */
486 static HWND hwndSaver;
487
488 #endif /* USE_SAVER */
489
490
491 #ifdef USE_GRAPHICS
492
493 /*
494  * Flag set once "graphics" has been initialized
495  */
496 static bool can_use_graphics = FALSE;
497
498 /*
499  * The global bitmap
500  */
501 static DIBINIT infGraph;
502
503 #ifdef USE_TRANSPARENCY
504
505 /*
506  * The global bitmap mask
507  */
508 static DIBINIT infMask;
509
510 #endif /* USE_TRANSPARENCY */
511
512 #endif /* USE_GRAPHICS */
513
514
515 #ifdef USE_SOUND
516
517 /*
518  * Flag set once "sound" has been initialized
519  */
520 static bool can_use_sound = FALSE;
521
522 /*
523  * An array of sound file names
524  */
525 static cptr sound_file[SOUND_MAX];
526
527 #endif /* USE_SOUND */
528
529
530 /*
531  * Full path to ANGBAND.INI
532  */
533 static cptr ini_file = NULL;
534
535 /*
536  * Name of application
537  */
538 static cptr AppName = "ANGBAND";
539
540 /*
541  * Name of sub-window type
542  */
543 static cptr AngList = "AngList";
544
545 /*
546  * Directory names
547  */
548 static cptr ANGBAND_DIR_XTRA_FONT;
549 static cptr ANGBAND_DIR_XTRA_GRAF;
550 static cptr ANGBAND_DIR_XTRA_SOUND;
551 static cptr ANGBAND_DIR_XTRA_MUSIC;
552 static cptr ANGBAND_DIR_XTRA_HELP;
553
554
555 /*
556  * The "complex" color values
557  */
558 static COLORREF win_clr[256];
559
560
561 /*
562  * The "simple" color values
563  *
564  * See "main-ibm.c" for original table information
565  *
566  * The entries below are taken from the "color bits" defined above.
567  *
568  * Note that many of the choices below suck, but so do crappy monitors.
569  */
570 static BYTE win_pal[256] =
571 {
572         VID_BLACK,                                      /* Dark */
573         VID_WHITE,                                      /* White */
574         VID_CYAN,                                       /* Slate XXX */
575         VID_RED | VID_BRIGHT,           /* Orange XXX */
576         VID_RED,                                        /* Red */
577         VID_GREEN,                                      /* Green */
578         VID_BLUE,                                       /* Blue */
579         VID_YELLOW,                                     /* Umber XXX */
580         VID_BLACK | VID_BRIGHT,         /* Light Dark */
581         VID_CYAN | VID_BRIGHT,          /* Light Slate XXX */
582         VID_MAGENTA,                            /* Violet XXX */
583         VID_YELLOW | VID_BRIGHT,        /* Yellow */
584         VID_MAGENTA | VID_BRIGHT,       /* Light Red XXX */
585         VID_GREEN | VID_BRIGHT,         /* Light Green */
586         VID_BLUE | VID_BRIGHT,          /* Light Blue */
587         VID_YELLOW                                      /* Light Umber XXX */
588 };
589
590
591 /*
592  * Hack -- define which keys are "special"
593  */
594 static bool special_key[256];
595 static bool ignore_key[256];
596
597 #if 1
598 /*
599  * Hack -- initialization list for "special_key"
600  */
601 static byte special_key_list[] = {
602 VK_CLEAR,VK_PAUSE,VK_CAPITAL,VK_KANA,VK_JUNJA,VK_FINAL,VK_KANJI,
603 VK_CONVERT,VK_NONCONVERT,VK_ACCEPT,VK_MODECHANGE,
604 VK_PRIOR,VK_NEXT,VK_END,VK_HOME,VK_LEFT,VK_UP,VK_RIGHT,VK_DOWN,
605 VK_SELECT,VK_PRINT,VK_EXECUTE,VK_SNAPSHOT,VK_INSERT,VK_DELETE,
606 VK_HELP,VK_APPS,
607 VK_F1,VK_F2,VK_F3,VK_F4,VK_F5,VK_F6,VK_F7,VK_F8,VK_F9,VK_F10,
608 VK_F11,VK_F12,VK_F13,VK_F14,VK_F15,VK_F16,VK_F17,VK_F18,VK_F19,VK_F20,
609 VK_F21,VK_F22,VK_F23,VK_F24,VK_NUMLOCK,VK_SCROLL,
610 VK_ATTN,VK_CRSEL,VK_EXSEL,VK_EREOF,VK_PLAY,VK_ZOOM,VK_NONAME,
611 VK_PA1,0
612 };
613
614 static byte ignore_key_list[] = {
615 VK_ESCAPE,VK_TAB,VK_SPACE,
616 'F','W','O','H', /* these are menu characters.*/
617 VK_SHIFT,VK_CONTROL,VK_MENU,VK_LWIN,VK_RWIN,
618 VK_LSHIFT,VK_RSHIFT,VK_LCONTROL,VK_RCONTROL,VK_LMENU,VK_RMENU,0
619 };
620
621 #else
622 /*
623  * Hack -- initialization list for "special_key"
624  *
625  * We ignore the modifier keys (shift, control, alt, num lock, scroll lock),
626  * and the normal keys (escape, tab, return, letters, numbers, etc), but we
627  * catch the keypad keys (with and without numlock set, including keypad 5),
628  * the function keys (including the "menu" key which maps to F10), and the
629  * "pause" key (between scroll lock and numlock).  We also catch a few odd
630  * keys which I do not recognize, but which are listed among keys which we
631  * do catch, so they should be harmless to catch.
632  */
633 static byte special_key_list[] =
634 {
635         VK_CLEAR,               /* 0x0C (KP<5>) */
636
637         VK_PAUSE,               /* 0x13 (pause) */
638
639         VK_PRIOR,               /* 0x21 (KP<9>) */
640         VK_NEXT,                /* 0x22 (KP<3>) */
641         VK_END,                 /* 0x23 (KP<1>) */
642         VK_HOME,                /* 0x24 (KP<7>) */
643         VK_LEFT,                /* 0x25 (KP<4>) */
644         VK_UP,                  /* 0x26 (KP<8>) */
645         VK_RIGHT,               /* 0x27 (KP<6>) */
646         VK_DOWN,                /* 0x28 (KP<2>) */
647         VK_SELECT,              /* 0x29 (?????) */
648         VK_PRINT,               /* 0x2A (?????) */
649         VK_EXECUTE,             /* 0x2B (?????) */
650         VK_SNAPSHOT,    /* 0x2C (?????) */
651         VK_INSERT,              /* 0x2D (KP<0>) */
652         VK_DELETE,              /* 0x2E (KP<.>) */
653         VK_HELP,                /* 0x2F (?????) */
654
655 #if 0
656         VK_NUMPAD0,             /* 0x60 (KP<0>) */
657         VK_NUMPAD1,             /* 0x61 (KP<1>) */
658         VK_NUMPAD2,             /* 0x62 (KP<2>) */
659         VK_NUMPAD3,             /* 0x63 (KP<3>) */
660         VK_NUMPAD4,             /* 0x64 (KP<4>) */
661         VK_NUMPAD5,             /* 0x65 (KP<5>) */
662         VK_NUMPAD6,             /* 0x66 (KP<6>) */
663         VK_NUMPAD7,             /* 0x67 (KP<7>) */
664         VK_NUMPAD8,             /* 0x68 (KP<8>) */
665         VK_NUMPAD9,             /* 0x69 (KP<9>) */
666         VK_MULTIPLY,    /* 0x6A (KP<*>) */
667         VK_ADD,                 /* 0x6B (KP<+>) */
668         VK_SEPARATOR,   /* 0x6C (?????) */
669         VK_SUBTRACT,    /* 0x6D (KP<->) */
670         VK_DECIMAL,             /* 0x6E (KP<.>) */
671         VK_DIVIDE,              /* 0x6F (KP</>) */
672 #endif
673
674         VK_F1,                  /* 0x70 */
675         VK_F2,                  /* 0x71 */
676         VK_F3,                  /* 0x72 */
677         VK_F4,                  /* 0x73 */
678         VK_F5,                  /* 0x74 */
679         VK_F6,                  /* 0x75 */
680         VK_F7,                  /* 0x76 */
681         VK_F8,                  /* 0x77 */
682         VK_F9,                  /* 0x78 */
683         VK_F10,                 /* 0x79 */
684         VK_F11,                 /* 0x7A */
685         VK_F12,                 /* 0x7B */
686         VK_F13,                 /* 0x7C */
687         VK_F14,                 /* 0x7D */
688         VK_F15,                 /* 0x7E */
689         VK_F16,                 /* 0x7F */
690         VK_F17,                 /* 0x80 */
691         VK_F18,                 /* 0x81 */
692         VK_F19,                 /* 0x82 */
693         VK_F20,                 /* 0x83 */
694         VK_F21,                 /* 0x84 */
695         VK_F22,                 /* 0x85 */
696         VK_F23,                 /* 0x86 */
697         VK_F24,                 /* 0x87 */
698
699         0
700 };
701 #endif
702
703 /* bg */
704 static void delete_bg()
705 {
706         if (hBG != NULL)
707         {
708                 DeleteObject(hBG);
709                 hBG = NULL;
710         }
711 }
712
713 static int init_bg()
714 {
715         char * bmfile = bg_bitmap_file;
716
717         delete_bg();
718         if (use_bg == 0) return 0;
719
720         hBG = LoadImage(NULL, bmfile,  IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
721         if (!hBG) {
722                 plog_fmt("ÊÉ»æÍѥӥåȥޥåנ'%s' ¤òÆɤ߹þ¤á¤Þ¤»¤ó¡£", bmfile);
723                 use_bg = 0;
724                 return 0;
725         }
726 #if 0 /* gomi */
727         HDC wnddc, dcimage, dcbg;
728         HBITMAP bmimage, bmimage_old, bmbg_old;
729         int i, j;
730
731         delete_bg();
732         
733         wnddc = GetDC(hwnd);
734         dcimage = CreateCompatibleDC(wnddc);
735         dcbg = CreateCompatibleDC(wnddc);
736         
737         bmimage = LoadImage(NULL, "bg.bmp", LR_LOADFROMFILE, 0, 0, 0);
738         if (!bmimage) quit("bg.bmp¤¬Æɤߤ³¤á¤Ê¤¤¡ª");
739         bmimage_old = SelectObject(dcimage, bmimage);
740         
741         CreateCompatibleBitmap();
742
743         ReleaseDC(hwnd, wnddc);
744 #endif
745         use_bg = 1;
746         return 1;
747 }
748
749 static void DrawBG(HDC hdc, RECT *r)
750 {
751         HDC hdcSrc;
752         HBITMAP hOld;
753         BITMAP bm;
754         int x = r->left, y = r->top;
755         int nx, ny, sx, sy, swid, shgt, cwid, chgt;
756         
757         if (!use_bg || !hBG)
758                 return;
759
760         nx = x; ny = y;
761         GetObject(hBG, sizeof(bm), &bm);
762         swid = bm.bmWidth; shgt = bm.bmHeight;
763
764         hdcSrc = CreateCompatibleDC(hdc);
765         hOld = SelectObject(hdcSrc, hBG);
766
767         do {
768                 sx = nx % swid;
769                 cwid = MIN(swid - sx, r->right - nx);
770                 do {
771                         sy = ny % shgt;
772                         chgt = MIN(shgt - sy, r->bottom - ny);
773                                 BitBlt(hdc, nx, ny, cwid, chgt, hdcSrc, sx, sy, SRCCOPY);
774                         ny += chgt;
775                 } while (ny < r->bottom);
776                 ny = y;
777                 nx += cwid;
778         } while (nx < r->right);
779         
780         SelectObject(hdcSrc, hOld);
781         DeleteDC(hdcSrc);
782 }
783
784 /*
785  * Hack -- given a pathname, point at the filename
786  */
787 static cptr extract_file_name(cptr s)
788 {
789         cptr p;
790
791         /* Start at the end */
792         p = s + strlen(s) - 1;
793
794         /* Back up to divider */
795         while ((p >= s) && (*p != ':') && (*p != '\\')) p--;
796
797         /* Return file name */
798         return (p+1);
799 }
800
801
802 /*
803  * Hack -- given a simple filename, extract the "font size" info
804  *
805  * Return a pointer to a static buffer holding the capitalized base name.
806  */
807 #ifndef JP
808 static char *analyze_font(char *path, int *wp, int *hp)
809 {
810         int wid, hgt;
811
812         char *s, *p;
813
814         /* Start at the end */
815         p = path + strlen(path) - 1;
816
817         /* Back up to divider */
818         while ((p >= path) && (*p != ':') && (*p != '\\')) --p;
819
820         /* Advance to file name */
821         ++p;
822
823         /* Capitalize */
824         for (s = p; *s; ++s)
825         {
826                 /* Capitalize (be paranoid) */
827                 if (islower(*s)) *s = toupper(*s);
828         }
829
830         /* Find first 'X' */
831         s = strchr(p, 'X');
832
833         /* Extract font width */
834         wid = atoi(p);
835
836         /* Extract height */
837         hgt = s ? atoi(s+1) : 0;
838
839         /* Save results */
840         (*wp) = wid;
841         (*hp) = hgt;
842
843         /* Result */
844         return (p);
845 }
846 #endif
847
848
849 /*
850  * Check for existance of a file
851  */
852 static bool check_file(cptr s)
853 {
854         char path[1024];
855
856 #ifdef WIN32
857
858         DWORD attrib;
859
860 #else /* WIN32 */
861
862         unsigned int attrib;
863
864 #endif /* WIN32 */
865
866         /* Copy it */
867         strcpy(path, s);
868
869 #ifdef WIN32
870
871         /* Examine */
872         attrib = GetFileAttributes(path);
873
874         /* Require valid filename */
875         if (attrib == INVALID_FILE_NAME) return (FALSE);
876
877         /* Prohibit directory */
878         if (attrib & FILE_ATTRIBUTE_DIRECTORY) return (FALSE);
879
880 #else /* WIN32 */
881
882         /* Examine and verify */
883         if (_dos_getfileattr(path, &attrib)) return (FALSE);
884
885         /* Prohibit something */
886         if (attrib & FA_LABEL) return (FALSE);
887
888         /* Prohibit directory */
889         if (attrib & FA_DIREC) return (FALSE);
890
891 #endif /* WIN32 */
892
893         /* Success */
894         return (TRUE);
895 }
896
897
898 /*
899  * Check for existance of a directory
900  */
901 static bool check_dir(cptr s)
902 {
903         int i;
904
905         char path[1024];
906
907 #ifdef WIN32
908
909         DWORD attrib;
910
911 #else /* WIN32 */
912
913         unsigned int attrib;
914
915 #endif /* WIN32 */
916
917         /* Copy it */
918         strcpy(path, s);
919
920         /* Check length */
921         i = strlen(path);
922
923         /* Remove trailing backslash */
924         if (i && (path[i-1] == '\\')) path[--i] = '\0';
925
926 #ifdef WIN32
927
928         /* Examine */
929         attrib = GetFileAttributes(path);
930
931         /* Require valid filename */
932         if (attrib == INVALID_FILE_NAME) return (FALSE);
933
934         /* Require directory */
935         if (!(attrib & FILE_ATTRIBUTE_DIRECTORY)) return (FALSE);
936
937 #else /* WIN32 */
938
939         /* Examine and verify */
940         if (_dos_getfileattr(path, &attrib)) return (FALSE);
941
942         /* Prohibit something */
943         if (attrib & FA_LABEL) return (FALSE);
944
945         /* Require directory */
946         if (!(attrib & FA_DIREC)) return (FALSE);
947
948 #endif /* WIN32 */
949
950         /* Success */
951         return (TRUE);
952 }
953
954
955 /*
956  * Validate a file
957  */
958 static void validate_file(cptr s)
959 {
960         /* Verify or fail */
961         if (!check_file(s))
962         {
963 #ifdef JP
964                 quit_fmt("ɬÍפʥե¡¥¤¥ë[%s]¤¬¸«¤¢¤¿¤ê¤Þ¤»¤ó¡£", s);
965 #else
966                 quit_fmt("Cannot find required file:\n%s", s);
967 #endif
968
969         }
970 }
971
972
973 /*
974  * Validate a directory
975  */
976 static void validate_dir(cptr s, bool vital)
977 {
978         /* Verify or fail */
979         if (!check_dir(s))
980         {
981                 /* This directory contains needed data */
982                 if (vital)
983                 {
984 #ifdef JP
985                 quit_fmt("ɬÍפʥǥ£¥ì¥¯¥È¥ê[%s]¤¬¸«¤¢¤¿¤ê¤Þ¤»¤ó¡£", s);
986 #else
987                         quit_fmt("Cannot find required directory:\n%s", s);
988 #endif
989
990                 }
991                 /* Attempt to create this directory */
992                 else if (mkdir(s))
993                 {
994                         quit_fmt("Unable to create directory:\n%s", s);
995                 }
996         }
997 }
998
999
1000 /*
1001  * Get the "size" for a window
1002  */
1003 static void term_getsize(term_data *td)
1004 {
1005         RECT rc;
1006
1007         int wid, hgt;
1008
1009         /* Paranoia */
1010         if (td->cols < 1) td->cols = 1;
1011         if (td->rows < 1) td->rows = 1;
1012
1013         /* Window sizes */
1014         wid = td->cols * td->tile_wid + td->size_ow1 + td->size_ow2;
1015         hgt = td->rows * td->tile_hgt + td->size_oh1 + td->size_oh2;
1016
1017         /* Fake window size */
1018         rc.left = 0;
1019         rc.right = rc.left + wid;
1020         rc.top = 0;
1021         rc.bottom = rc.top + hgt;
1022
1023         /* XXX XXX XXX */
1024         /* rc.right += 1; */
1025         /* rc.bottom += 1; */
1026
1027         /* Adjust */
1028         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
1029
1030         /* Total size */
1031         td->size_wid = rc.right - rc.left;
1032         td->size_hgt = rc.bottom - rc.top;
1033
1034         /* See CreateWindowEx */
1035         if (!td->w) return;
1036
1037         /* Extract actual location */
1038         GetWindowRect(td->w, &rc);
1039
1040         /* Save the location */
1041         td->pos_x = rc.left;
1042         td->pos_y = rc.top;
1043 }
1044
1045
1046 /*
1047  * Write the "prefs" for a single term
1048  */
1049 static void save_prefs_aux(term_data *td, cptr sec_name)
1050 {
1051         char buf[1024];
1052
1053         RECT rc;
1054
1055         /* Paranoia */
1056         if (!td->w) return;
1057
1058         /* Visible */
1059         strcpy(buf, td->visible ? "1" : "0");
1060         WritePrivateProfileString(sec_name, "Visible", buf, ini_file);
1061
1062         /* Font */
1063 #ifdef JP
1064         strcpy(buf, td->lf.lfFaceName[0]!='\0' ? td->lf.lfFaceName : "£Í£Ó ¥´¥·¥Ã¥¯");
1065 #else
1066         strcpy(buf, td->font_file ? td->font_file : "8X13.FON");
1067 #endif
1068
1069         WritePrivateProfileString(sec_name, "Font", buf, ini_file);
1070
1071 #ifdef JP
1072         wsprintf(buf, "%d", td->lf.lfWidth);
1073         WritePrivateProfileString(sec_name, "FontWid", buf, ini_file);
1074         wsprintf(buf, "%d", td->lf.lfHeight);
1075         WritePrivateProfileString(sec_name, "FontHgt", buf, ini_file);
1076         wsprintf(buf, "%d", td->lf.lfWeight);
1077         WritePrivateProfileString(sec_name, "FontWgt", buf, ini_file);
1078 #endif
1079         /* Bizarre */
1080         strcpy(buf, td->bizarre ? "1" : "0");
1081         WritePrivateProfileString(sec_name, "Bizarre", buf, ini_file);
1082
1083         /* Tile size (x) */
1084         wsprintf(buf, "%d", td->tile_wid);
1085         WritePrivateProfileString(sec_name, "TileWid", buf, ini_file);
1086
1087         /* Tile size (y) */
1088         wsprintf(buf, "%d", td->tile_hgt);
1089         WritePrivateProfileString(sec_name, "TileHgt", buf, ini_file);
1090
1091         /* Window size (x) */
1092         wsprintf(buf, "%d", td->cols);
1093         WritePrivateProfileString(sec_name, "NumCols", buf, ini_file);
1094
1095         /* Window size (y) */
1096         wsprintf(buf, "%d", td->rows);
1097         WritePrivateProfileString(sec_name, "NumRows", buf, ini_file);
1098
1099         /* Acquire position */
1100         GetWindowRect(td->w, &rc);
1101
1102         /* Window position (x) */
1103         wsprintf(buf, "%d", rc.left);
1104         WritePrivateProfileString(sec_name, "PositionX", buf, ini_file);
1105
1106         /* Window position (y) */
1107         wsprintf(buf, "%d", rc.top);
1108         WritePrivateProfileString(sec_name, "PositionY", buf, ini_file);
1109
1110         /* Window Z position */
1111         strcpy(buf, td->posfix ? "1" : "0");
1112         WritePrivateProfileString(sec_name, "PositionFix", buf, ini_file);
1113 }
1114
1115
1116 /*
1117  * Write the "prefs"
1118  *
1119  * We assume that the windows have all been initialized
1120  */
1121 static void save_prefs(void)
1122 {
1123         int i;
1124
1125         char buf[128];
1126
1127         /* Save the "arg_graphics" flag */
1128         sprintf(buf, "%d", arg_graphics);
1129         WritePrivateProfileString("Angband", "Graphics", buf, ini_file);
1130
1131         /* Save the "arg_bigtile" flag */
1132         strcpy(buf, arg_bigtile ? "1" : "0");
1133         WritePrivateProfileString("Angband", "Bigtile", buf, ini_file);
1134
1135         /* Save the "arg_sound" flag */
1136         strcpy(buf, arg_sound ? "1" : "0");
1137         WritePrivateProfileString("Angband", "Sound", buf, ini_file);
1138
1139         /* bg */
1140         strcpy(buf, use_bg ? "1" : "0");
1141         WritePrivateProfileString("Angband", "BackGround", buf, ini_file);
1142         WritePrivateProfileString("Angband", "BackGroundBitmap", 
1143                 bg_bitmap_file[0] != '\0' ? bg_bitmap_file : "bg.bmp", ini_file);
1144
1145         /* Save window prefs */
1146         for (i = 0; i < MAX_TERM_DATA; ++i)
1147         {
1148                 term_data *td = &data[i];
1149
1150                 sprintf(buf, "Term-%d", i);
1151
1152                 save_prefs_aux(td, buf);
1153         }
1154 }
1155
1156
1157 /*
1158  * Load the "prefs" for a single term
1159  */
1160 static void load_prefs_aux(term_data *td, cptr sec_name)
1161 {
1162         char tmp[1024];
1163
1164         int wid, hgt;
1165
1166         /* Visible */
1167         td->visible = (GetPrivateProfileInt(sec_name, "Visible", td->visible, ini_file) != 0);
1168
1169         /* Desired font, with default */
1170 #ifdef JP
1171         GetPrivateProfileString(sec_name, "Font", "£Í£Ó ¥´¥·¥Ã¥¯", tmp, 127, ini_file);
1172 #else
1173         GetPrivateProfileString(sec_name, "Font", "8X13.FON", tmp, 127, ini_file);
1174 #endif
1175
1176
1177         /* Bizarre */
1178         td->bizarre = (GetPrivateProfileInt(sec_name, "Bizarre", td->bizarre, ini_file) != 0);
1179
1180         /* Analyze font, save desired font name */
1181 #ifdef JP
1182         td->font_want = string_make(tmp);
1183         hgt = 15; wid = 0;
1184         td->lf.lfWidth  = GetPrivateProfileInt(sec_name, "FontWid", wid, ini_file);
1185         td->lf.lfHeight = GetPrivateProfileInt(sec_name, "FontHgt", hgt, ini_file);
1186         td->lf.lfWeight = GetPrivateProfileInt(sec_name, "FontWgt", 0, ini_file);
1187 #else
1188         td->font_want = string_make(analyze_font(tmp, &wid, &hgt));
1189 #endif
1190
1191
1192         /* Tile size */
1193 #ifdef JP
1194         td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", td->lf.lfWidth, ini_file);
1195         td->tile_hgt = GetPrivateProfileInt(sec_name, "TileHgt", td->lf.lfHeight, ini_file);
1196 #else
1197         td->tile_wid = GetPrivateProfileInt(sec_name, "TileWid", wid, ini_file);
1198         td->tile_hgt = GetPrivateProfileInt(sec_name, "TileHgt", hgt, ini_file);
1199 #endif
1200
1201
1202         /* Window size */
1203         td->cols = GetPrivateProfileInt(sec_name, "NumCols", td->cols, ini_file);
1204         td->rows = GetPrivateProfileInt(sec_name, "NumRows", td->rows, ini_file);
1205
1206         /* Window position */
1207         td->pos_x = GetPrivateProfileInt(sec_name, "PositionX", td->pos_x, ini_file);
1208         td->pos_y = GetPrivateProfileInt(sec_name, "PositionY", td->pos_y, ini_file);
1209
1210         /* Window Z position */
1211         td->posfix = GetPrivateProfileInt(sec_name, "PositionFix", td->posfix, ini_file);
1212 }
1213
1214
1215 /*
1216  * Load the "prefs"
1217  */
1218 static void load_prefs(void)
1219 {
1220         int i;
1221
1222         char buf[1024];
1223
1224         /* Extract the "arg_graphics" flag */
1225         arg_graphics = GetPrivateProfileInt("Angband", "Graphics", GRAPHICS_NONE, ini_file);
1226
1227         /* Extract the "arg_bigtile" flag */
1228         arg_bigtile = GetPrivateProfileInt("Angband", "Bigtile", FALSE, ini_file);
1229         use_bigtile = arg_bigtile;
1230
1231         /* Extract the "arg_sound" flag */
1232         arg_sound = (GetPrivateProfileInt("Angband", "Sound", 0, ini_file) != 0);
1233
1234         /* bg */
1235         use_bg = GetPrivateProfileInt("Angband", "BackGround", 0, ini_file);
1236         GetPrivateProfileString("Angband", "BackGroundBitmap", "bg.bmp", bg_bitmap_file, 1023, ini_file);
1237
1238         /* Load window prefs */
1239         for (i = 0; i < MAX_TERM_DATA; ++i)
1240         {
1241                 term_data *td = &data[i];
1242
1243                 sprintf(buf, "Term-%d", i);
1244
1245                 load_prefs_aux(td, buf);
1246         }
1247 }
1248
1249
1250 /*
1251  * Create the new global palette based on the bitmap palette
1252  * (if any), and the standard 16 entry palette derived from
1253  * "win_clr[]" which is used for the basic 16 Angband colors.
1254  *
1255  * This function is never called before all windows are ready.
1256  *
1257  * This function returns FALSE if the new palette could not be
1258  * prepared, which should normally be a fatal error.  XXX XXX
1259  *
1260  * Note that only some machines actually use a "palette".
1261  */
1262 static int new_palette(void)
1263 {
1264         HPALETTE hBmPal;
1265         HPALETTE hNewPal;
1266         HDC hdc;
1267         int i, nEntries;
1268         int pLogPalSize;
1269         int lppeSize;
1270         LPLOGPALETTE pLogPal;
1271         LPPALETTEENTRY lppe;
1272
1273         term_data *td;
1274
1275
1276         /* This makes no sense */
1277         if (!paletted) return (TRUE);
1278
1279
1280         /* No bitmap */
1281         lppeSize = 0;
1282         lppe = NULL;
1283         nEntries = 0;
1284
1285 #ifdef USE_GRAPHICS
1286
1287         /* Check the bitmap palette */
1288         hBmPal = infGraph.hPalette;
1289
1290         /* Use the bitmap */
1291         if (hBmPal)
1292         {
1293                 lppeSize = 256 * sizeof(PALETTEENTRY);
1294                 lppe = (LPPALETTEENTRY)ralloc(lppeSize);
1295                 nEntries = GetPaletteEntries(hBmPal, 0, 255, lppe);
1296                 if ((nEntries == 0) || (nEntries > 220))
1297                 {
1298                         /* Warn the user */
1299 #ifdef JP
1300                         plog("²èÌ̤ò16¥Ó¥Ã¥È¤«24¥Ó¥Ã¥È¥«¥é¡¼¥â¡¼¥É¤Ë¤·¤Æ²¼¤µ¤¤¡£");
1301 #else
1302                         plog("Please switch to high- or true-color mode.");
1303 #endif
1304
1305
1306                         /* Cleanup */
1307                         rnfree(lppe, lppeSize);
1308
1309                         /* Fail */
1310                         return (FALSE);
1311                 }
1312         }
1313
1314 #endif /* USE_GRAPHICS */
1315
1316         /* Size of palette */
1317         pLogPalSize = sizeof(LOGPALETTE) + (nEntries + 16) * sizeof(PALETTEENTRY);
1318
1319         /* Allocate palette */
1320         pLogPal = (LPLOGPALETTE)ralloc(pLogPalSize);
1321
1322         /* Version */
1323         pLogPal->palVersion = 0x300;
1324
1325         /* Make room for bitmap and normal data */
1326         pLogPal->palNumEntries = nEntries + 16;
1327
1328         /* Save the bitmap data */
1329         for (i = 0; i < nEntries; i++)
1330         {
1331                 pLogPal->palPalEntry[i] = lppe[i];
1332         }
1333
1334         /* Save the normal data */
1335         for (i = 0; i < 16; i++)
1336         {
1337                 LPPALETTEENTRY p;
1338
1339                 /* Access the entry */
1340                 p = &(pLogPal->palPalEntry[i+nEntries]);
1341
1342                 /* Save the colors */
1343                 p->peRed = GetRValue(win_clr[i]);
1344                 p->peGreen = GetGValue(win_clr[i]);
1345                 p->peBlue = GetBValue(win_clr[i]);
1346
1347                 /* Save the flags */
1348                 p->peFlags = PC_NOCOLLAPSE;
1349         }
1350
1351         /* Free something */
1352         if (lppe) rnfree(lppe, lppeSize);
1353
1354         /* Create a new palette, or fail */
1355         hNewPal = CreatePalette(pLogPal);
1356 #ifdef JP
1357         if (!hNewPal) quit("¥Ñ¥ì¥Ã¥È¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¡ª");
1358 #else
1359         if (!hNewPal) quit("Cannot create palette!");
1360 #endif
1361
1362
1363         /* Free the palette */
1364         rnfree(pLogPal, pLogPalSize);
1365
1366         /* Main window */
1367         td = &data[0];
1368
1369         /* Realize the palette */
1370         hdc = GetDC(td->w);
1371         SelectPalette(hdc, hNewPal, 0);
1372         i = RealizePalette(hdc);
1373         ReleaseDC(td->w, hdc);
1374 #ifdef JP
1375         if (i == 0) quit("¥Ñ¥ì¥Ã¥È¤ò¥·¥¹¥Æ¥à¥¨¥ó¥È¥ê¤Ë¥Þ¥Ã¥×¤Ç¤­¤Þ¤»¤ó¡ª");
1376 #else
1377         if (i == 0) quit("Cannot realize palette!");
1378 #endif
1379
1380
1381         /* Sub-windows */
1382         for (i = 1; i < MAX_TERM_DATA; i++)
1383         {
1384                 td = &data[i];
1385
1386                 hdc = GetDC(td->w);
1387                 SelectPalette(hdc, hNewPal, 0);
1388                 ReleaseDC(td->w, hdc);
1389         }
1390
1391         /* Delete old palette */
1392         if (hPal) DeleteObject(hPal);
1393
1394         /* Save new palette */
1395         hPal = hNewPal;
1396
1397         /* Success */
1398         return (TRUE);
1399 }
1400
1401
1402 #ifdef USE_GRAPHICS
1403 /*
1404  * Initialize graphics
1405  */
1406 static bool init_graphics(void)
1407 {
1408         /* Initialize once */
1409         /* if (can_use_graphics != arg_graphics) */
1410         {
1411                 char buf[1024];
1412                 int wid, hgt;
1413                 cptr name;
1414
1415                 if (arg_graphics == GRAPHICS_ADAM_BOLT)
1416                 {
1417                         wid = 16;
1418                         hgt = 16;
1419
1420                         name = "16X16.BMP";
1421
1422                         ANGBAND_GRAF = "new";
1423                 }
1424                 else
1425                 {
1426                         wid = 8;
1427                         hgt = 8;
1428
1429                         name = "8X8.BMP";
1430                         ANGBAND_GRAF = "old";
1431                 }
1432
1433                 /* Access the bitmap file */
1434                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, name);
1435
1436                 /* Load the bitmap or quit */
1437                 if (!ReadDIB(data[0].w, buf, &infGraph))
1438                 {
1439 #ifdef JP
1440                         plog_fmt("¥Ó¥Ã¥È¥Þ¥Ã¥× '%s' ¤òÆɤ߹þ¤á¤Þ¤»¤ó¡£", name);
1441 #else
1442                         plog_fmt("Cannot read bitmap file '%s'", name);
1443 #endif
1444
1445                         return (FALSE);
1446                 }
1447
1448                 /* Save the new sizes */
1449                 infGraph.CellWidth = wid;
1450                 infGraph.CellHeight = hgt;
1451
1452 #ifdef USE_TRANSPARENCY
1453
1454                 if (arg_graphics == GRAPHICS_ADAM_BOLT)
1455                 {
1456                         /* Access the mask file */
1457                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_GRAF, "mask.bmp");
1458
1459                         /* Load the bitmap or quit */
1460                         if (!ReadDIB(data[0].w, buf, &infMask))
1461                         {
1462                                 plog_fmt("Cannot read bitmap file '%s'", buf);
1463                                 return (FALSE);
1464                         }
1465                 }
1466
1467 #endif /* USE_TRANSPARENCY */
1468
1469                 /* Activate a palette */
1470                 if (!new_palette())
1471                 {
1472                         /* Free bitmap XXX XXX XXX */
1473
1474                         /* Oops */
1475 #ifdef JP
1476                         plog("¥Ñ¥ì¥Ã¥È¤ò¼Â¸½¤Ç¤­¤Þ¤»¤ó¡ª");
1477 #else
1478                         plog("Cannot activate palette!");
1479 #endif
1480
1481                         return (FALSE);
1482                 }
1483
1484                 /* Graphics available */
1485                 can_use_graphics = arg_graphics;
1486         }
1487
1488         /* Result */
1489         return (can_use_graphics);
1490 }
1491 #endif /* USE_GRAPHICS */
1492
1493
1494 #ifdef USE_SOUND
1495 /*
1496  * Initialize sound
1497  */
1498 static bool init_sound(void)
1499 {
1500         /* Initialize once */
1501         if (!can_use_sound)
1502         {
1503                 int i;
1504
1505                 char wav[128];
1506                 char buf[1024];
1507
1508                 /* Prepare the sounds */
1509                 for (i = 1; i < SOUND_MAX; i++)
1510                 {
1511                         /* Extract name of sound file */
1512                         sprintf(wav, "%s.wav", angband_sound_name[i]);
1513
1514                         /* Access the sound */
1515                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_SOUND, wav);
1516
1517                         /* Save the sound filename, if it exists */
1518                         if (check_file(buf)) sound_file[i] = string_make(buf);
1519                 }
1520
1521                 /* Sound available */
1522                 can_use_sound = TRUE;
1523         }
1524
1525         /* Result */
1526         return (can_use_sound);
1527 }
1528 #endif /* USE_SOUND */
1529
1530
1531 /*
1532  * Resize a window
1533  */
1534 static void term_window_resize(term_data *td)
1535 {
1536         /* Require window */
1537         if (!td->w) return;
1538
1539         /* Resize the window */
1540         SetWindowPos(td->w, 0, 0, 0,
1541                      td->size_wid, td->size_hgt,
1542                      SWP_NOMOVE | SWP_NOZORDER);
1543
1544         /* Redraw later */
1545         InvalidateRect(td->w, NULL, TRUE);
1546 }
1547
1548
1549 /*
1550  * Force the use of a new "font file" for a term_data
1551  *
1552  * This function may be called before the "window" is ready
1553  *
1554  * This function returns zero only if everything succeeds.
1555  *
1556  * Note that the "font name" must be capitalized!!!
1557  */
1558 static errr term_force_font(term_data *td, cptr path)
1559 {
1560         int wid, hgt;
1561
1562 #ifndef JP
1563         int i;
1564         char *base;
1565         char buf[1024];
1566 #endif
1567
1568         /* Forget the old font (if needed) */
1569         if (td->font_id) DeleteObject(td->font_id);
1570
1571 #ifndef JP
1572         /* Forget old font */
1573         if (td->font_file)
1574         {
1575                 bool used = FALSE;
1576
1577                 /* Scan windows */
1578                 for (i = 0; i < MAX_TERM_DATA; i++)
1579                 {
1580                         /* Don't check when closing the application */
1581                         if (!path) break;
1582
1583                         /* Check "screen" */
1584                         if ((td != &data[i]) &&
1585                             (data[i].font_file) &&
1586                             (streq(data[i].font_file, td->font_file)))
1587                         {
1588                                 used = TRUE;
1589                         }
1590                 }
1591
1592                 /* Remove unused font resources */
1593                 if (!used) RemoveFontResource(td->font_file);
1594
1595                 /* Free the old name */
1596                 string_free(td->font_file);
1597
1598                 /* Forget it */
1599                 td->font_file = NULL;
1600         }
1601
1602
1603         /* No path given */
1604         if (!path) return (1);
1605
1606
1607         /* Local copy */
1608         strcpy(buf, path);
1609
1610         /* Analyze font path */
1611         base = analyze_font(buf, &wid, &hgt);
1612
1613         /* Verify suffix */
1614         if (!suffix(base, ".FON")) return (1);
1615
1616         /* Verify file */
1617         if (!check_file(buf)) return (1);
1618
1619         /* Load the new font */
1620         if (!AddFontResource(buf)) return (1);
1621
1622         /* Save new font name */
1623         td->font_file = string_make(base);
1624
1625         /* Remove the "suffix" */
1626         base[strlen(base)-4] = '\0';
1627 #endif
1628
1629         /* Create the font (using the 'base' of the font file name!) */
1630 #ifdef JP
1631         td->font_id = CreateFontIndirect(&(td->lf));
1632         wid = td->lf.lfWidth;
1633         hgt = td->lf.lfHeight;
1634         if (!td->font_id) return (1);
1635 #else
1636         td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0,
1637                                  ANSI_CHARSET, OUT_DEFAULT_PRECIS,
1638                                  CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1639                                  FIXED_PITCH | FF_DONTCARE, base);
1640 #endif
1641
1642
1643         /* Hack -- Unknown size */
1644         if (!wid || !hgt)
1645         {
1646                 HDC hdcDesktop;
1647                 HFONT hfOld;
1648                 TEXTMETRIC tm;
1649
1650                 /* all this trouble to get the cell size */
1651                 hdcDesktop = GetDC(HWND_DESKTOP);
1652                 hfOld = SelectObject(hdcDesktop, td->font_id);
1653                 GetTextMetrics(hdcDesktop, &tm);
1654                 SelectObject(hdcDesktop, hfOld);
1655                 ReleaseDC(HWND_DESKTOP, hdcDesktop);
1656
1657                 /* Font size info */
1658                 wid = tm.tmAveCharWidth;
1659                 hgt = tm.tmHeight;
1660         }
1661
1662         /* Save the size info */
1663         td->font_wid = wid;
1664         td->font_hgt = hgt;
1665
1666         /* Success */
1667         return (0);
1668 }
1669
1670
1671
1672 /*
1673  * Allow the user to change the font for this window.
1674  */
1675 static void term_change_font(term_data *td)
1676 {
1677 #ifdef JP
1678         CHOOSEFONT cf;
1679
1680         memset(&cf, 0, sizeof(cf));
1681         cf.lStructSize = sizeof(cf);
1682     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
1683     cf.lpLogFont = &(td->lf);
1684
1685         if (ChooseFont(&cf))
1686         {
1687                 /* Force the font */
1688                 term_force_font(td, NULL);
1689
1690                 /* Assume not bizarre */
1691                 td->bizarre = TRUE;
1692
1693                 /* Reset the tile info */
1694                 td->tile_wid = td->font_wid;
1695                 td->tile_hgt = td->font_hgt;
1696
1697                 /* Analyze the font */
1698                 term_getsize(td);
1699
1700                 /* Resize the window */
1701                 term_window_resize(td);
1702         }
1703
1704 #else
1705         OPENFILENAME ofn;
1706
1707         char tmp[1024] = "";
1708
1709         /* Extract a default if possible */
1710         if (td->font_file) strcpy(tmp, td->font_file);
1711
1712         /* Ask for a choice */
1713         memset(&ofn, 0, sizeof(ofn));
1714         ofn.lStructSize = sizeof(ofn);
1715         ofn.hwndOwner = data[0].w;
1716         ofn.lpstrFilter = "Angband Font Files (*.fon)\0*.fon\0";
1717         ofn.nFilterIndex = 1;
1718         ofn.lpstrFile = tmp;
1719         ofn.nMaxFile = 128;
1720         ofn.lpstrInitialDir = ANGBAND_DIR_XTRA_FONT;
1721         ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
1722         ofn.lpstrDefExt = "fon";
1723
1724         /* Force choice if legal */
1725         if (GetOpenFileName(&ofn))
1726         {
1727                 /* Force the font */
1728                 if (term_force_font(td, tmp))
1729                 {
1730                         /* Access the standard font file */
1731                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
1732
1733                         /* Force the use of that font */
1734                         (void)term_force_font(td, tmp);
1735                 }
1736
1737                 /* Assume not bizarre */
1738                 td->bizarre = FALSE;
1739
1740                 /* Reset the tile info */
1741                 td->tile_wid = td->font_wid;
1742                 td->tile_hgt = td->font_hgt;
1743
1744                 /* Analyze the font */
1745                 term_getsize(td);
1746
1747                 /* Resize the window */
1748                 term_window_resize(td);
1749         }
1750 #endif
1751
1752 }
1753
1754 /*
1755  * Allow the user to lock this window.
1756  */
1757 static void term_window_pos(term_data *td, HWND hWnd)
1758 {
1759         SetWindowPos(td->w, hWnd, 0, 0, 0, 0,
1760                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
1761 }
1762
1763 static void windows_map(void);
1764
1765 /*
1766  * Hack -- redraw a term_data
1767  */
1768 static void term_data_redraw(term_data *td)
1769 {
1770         if (td->map_active)
1771         {
1772                 /* Redraw the map */
1773                 windows_map();
1774         }
1775         else
1776         {
1777                 /* Activate the term */
1778                 Term_activate(&td->t);
1779
1780                 /* Redraw the contents */
1781                 Term_redraw();
1782
1783                 /* Restore the term */
1784                 Term_activate(term_screen);
1785         }
1786 }
1787
1788
1789
1790
1791
1792 /*** Function hooks needed by "Term" ***/
1793
1794
1795 #if 0
1796
1797 /*
1798  * Initialize a new Term
1799  */
1800 static void Term_init_win(term *t)
1801 {
1802         /* XXX Unused */
1803 }
1804
1805
1806 /*
1807  * Nuke an old Term
1808  */
1809 static void Term_nuke_win(term *t)
1810 {
1811         /* XXX Unused */
1812 }
1813
1814 #endif
1815
1816
1817 /*
1818  * Interact with the User
1819  */
1820 static errr Term_user_win(int n)
1821 {
1822         /* Success */
1823         return (0);
1824 }
1825
1826
1827 /*
1828  * React to global changes
1829  */
1830 static errr Term_xtra_win_react(void)
1831 {
1832         int i;
1833
1834
1835         /* Simple color */
1836         if (colors16)
1837         {
1838                 /* Save the default colors */
1839                 for (i = 0; i < 256; i++)
1840                 {
1841                         /* Simply accept the desired colors */
1842                         win_pal[i] = angband_color_table[i][0];
1843                 }
1844         }
1845
1846         /* Complex color */
1847         else
1848         {
1849                 COLORREF code;
1850
1851                 byte rv, gv, bv;
1852
1853                 bool change = FALSE;
1854
1855                 /* Save the default colors */
1856                 for (i = 0; i < 256; i++)
1857                 {
1858                         /* Extract desired values */
1859                         rv = angband_color_table[i][1];
1860                         gv = angband_color_table[i][2];
1861                         bv = angband_color_table[i][3];
1862
1863                         /* Extract a full color code */
1864                         code = PALETTERGB(rv, gv, bv);
1865
1866                         /* Activate changes */
1867                         if (win_clr[i] != code)
1868                         {
1869                                 /* Note the change */
1870                                 change = TRUE;
1871
1872                                 /* Apply the desired color */
1873                                 win_clr[i] = code;
1874                         }
1875                 }
1876
1877                 /* Activate the palette if needed */
1878                 if (change) (void)new_palette();
1879         }
1880
1881
1882 #ifdef USE_SOUND
1883
1884         /* Handle "arg_sound" */
1885         if (use_sound != arg_sound)
1886         {
1887                 /* Initialize (if needed) */
1888                 if (arg_sound && !init_sound())
1889                 {
1890                         /* Warning */
1891 #ifdef JP
1892                         plog("¥µ¥¦¥ó¥É¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó¡ª");
1893 #else
1894                         plog("Cannot initialize sound!");
1895 #endif
1896
1897
1898                         /* Cannot enable */
1899                         arg_sound = FALSE;
1900                 }
1901
1902                 /* Change setting */
1903                 use_sound = arg_sound;
1904         }
1905
1906 #endif
1907
1908
1909 #ifdef USE_GRAPHICS
1910
1911         /* Handle "arg_graphics" */
1912         if (use_graphics != arg_graphics)
1913         {
1914                 /* Initialize (if needed) */
1915                 if (arg_graphics && !init_graphics())
1916                 {
1917                         /* Warning */
1918 #ifdef JP
1919                         plog("¥°¥é¥Õ¥£¥Ã¥¯¥¹¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó!");
1920 #else
1921                         plog("Cannot initialize graphics!");
1922 #endif
1923
1924
1925                         /* Cannot enable */
1926                         arg_graphics = GRAPHICS_NONE;
1927                 }
1928
1929                 /* Change setting */
1930                 use_graphics = arg_graphics;
1931
1932                 /* Reset visuals */
1933 #ifdef ANGBAND_2_8_1
1934                 reset_visuals();
1935 #else /* ANGBAND_2_8_1 */
1936                 reset_visuals(TRUE);
1937 #endif /* ANGBAND_2_8_1 */
1938         }
1939
1940 #endif /* USE_GRAPHICS */
1941
1942
1943         /* Clean up windows */
1944         for (i = 0; i < MAX_TERM_DATA; i++)
1945         {
1946                 term *old = Term;
1947
1948                 term_data *td = &data[i];
1949
1950                 /* Update resized windows */
1951                 if ((td->cols != td->t.wid) || (td->rows != td->t.hgt))
1952                 {
1953                         /* Activate */
1954                         Term_activate(&td->t);
1955
1956                         /* Hack -- Resize the term */
1957                         Term_resize(td->cols, td->rows);
1958
1959                         /* Redraw the contents */
1960                         Term_redraw();
1961
1962                         /* Restore */
1963                         Term_activate(old);
1964                 }
1965         }
1966
1967
1968         /* Success */
1969         return (0);
1970 }
1971
1972
1973 /*
1974  * Process at least one event
1975  */
1976 static errr Term_xtra_win_event(int v)
1977 {
1978         MSG msg;
1979
1980         /* Wait for an event */
1981         if (v)
1982         {
1983                 /* Block */
1984                 if (GetMessage(&msg, NULL, 0, 0))
1985                 {
1986                         TranslateMessage(&msg);
1987                         DispatchMessage(&msg);
1988                 }
1989         }
1990
1991         /* Check for an event */
1992         else
1993         {
1994                 /* Check */
1995                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1996                 {
1997                         TranslateMessage(&msg);
1998                         DispatchMessage(&msg);
1999                 }
2000         }
2001
2002         /* Success */
2003         return 0;
2004 }
2005
2006
2007 /*
2008  * Process all pending events
2009  */
2010 static errr Term_xtra_win_flush(void)
2011 {
2012         MSG msg;
2013
2014         /* Process all pending events */
2015         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2016         {
2017                 TranslateMessage(&msg);
2018                 DispatchMessage(&msg);
2019         }
2020
2021         /* Success */
2022         return (0);
2023 }
2024
2025
2026 /*
2027  * Hack -- clear the screen
2028  *
2029  * Make this more efficient XXX XXX XXX
2030  */
2031 static errr Term_xtra_win_clear(void)
2032 {
2033         term_data *td = (term_data*)(Term->data);
2034
2035         HDC hdc;
2036         RECT rc;
2037
2038         /* Rectangle to erase */
2039         rc.left = td->size_ow1;
2040         rc.right = rc.left + td->cols * td->tile_wid;
2041         rc.top = td->size_oh1;
2042         rc.bottom = rc.top + td->rows * td->tile_hgt;
2043
2044         /* Erase it */
2045         hdc = GetDC(td->w);
2046         SetBkColor(hdc, RGB(0, 0, 0));
2047         SelectObject(hdc, td->font_id);
2048         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2049
2050         /* bg */
2051         if (use_bg)
2052         {
2053                 rc.left = 0; rc.top = 0;
2054                 DrawBG(hdc, &rc);
2055         }
2056         ReleaseDC(td->w, hdc);
2057
2058         /* Success */
2059         return 0;
2060 }
2061
2062
2063 /*
2064  * Hack -- make a noise
2065  */
2066 static errr Term_xtra_win_noise(void)
2067 {
2068         MessageBeep(MB_ICONASTERISK);
2069         return (0);
2070 }
2071
2072
2073 /*
2074  * Hack -- make a sound
2075  */
2076 static errr Term_xtra_win_sound(int v)
2077 {
2078         /* Sound disabled */
2079         if (!use_sound) return (1);
2080
2081         /* Illegal sound */
2082         if ((v < 0) || (v >= SOUND_MAX)) return (1);
2083
2084 #ifdef USE_SOUND
2085
2086         /* Unknown sound */
2087         if (!sound_file[v]) return (1);
2088
2089 #ifdef WIN32
2090
2091         /* Play the sound, catch errors */
2092         return (PlaySound(sound_file[v], 0, SND_FILENAME | SND_ASYNC));
2093
2094 #else /* WIN32 */
2095
2096         /* Play the sound, catch errors */
2097         return (sndPlaySound(sound_file[v], SND_ASYNC));
2098
2099 #endif /* WIN32 */
2100
2101 #else /* USE_SOUND */
2102
2103         /* Oops */
2104         return (1);
2105
2106 #endif /* USE_SOUND */
2107 }
2108
2109
2110 /*
2111  * Delay for "x" milliseconds
2112  */
2113 static int Term_xtra_win_delay(int v)
2114 {
2115
2116 #ifdef WIN32
2117
2118         /* Sleep */
2119         Sleep(v);
2120
2121 #else /* WIN32 */
2122
2123         DWORD t;
2124         MSG msg;
2125
2126         /* Final count */
2127         t = GetTickCount() + v;
2128
2129         /* Wait for it */
2130         while (GetTickCount() < t)
2131         {
2132                 /* Handle messages */
2133                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2134                 {
2135                         TranslateMessage(&msg);
2136                         DispatchMessage(&msg);
2137                 }
2138         }
2139
2140 #endif /* WIN32 */
2141
2142         /* Success */
2143         return (0);
2144 }
2145
2146
2147 /*
2148  * Do a "special thing"
2149  */
2150 static errr Term_xtra_win(int n, int v)
2151 {
2152         /* Handle a subset of the legal requests */
2153         switch (n)
2154         {
2155                 /* Make a bell sound */
2156                 case TERM_XTRA_NOISE:
2157                 {
2158                         return (Term_xtra_win_noise());
2159                 }
2160
2161                 /* Make a special sound */
2162                 case TERM_XTRA_SOUND:
2163                 {
2164                         return (Term_xtra_win_sound(v));
2165                 }
2166
2167                 /* Process random events */
2168                 case TERM_XTRA_BORED:
2169                 {
2170                         return (Term_xtra_win_event(0));
2171                 }
2172
2173                 /* Process an event */
2174                 case TERM_XTRA_EVENT:
2175                 {
2176                         return (Term_xtra_win_event(v));
2177                 }
2178
2179                 /* Flush all events */
2180                 case TERM_XTRA_FLUSH:
2181                 {
2182                         return (Term_xtra_win_flush());
2183                 }
2184
2185                 /* Clear the screen */
2186                 case TERM_XTRA_CLEAR:
2187                 {
2188                         return (Term_xtra_win_clear());
2189                 }
2190
2191                 /* React to global changes */
2192                 case TERM_XTRA_REACT:
2193                 {
2194                         return (Term_xtra_win_react());
2195                 }
2196
2197                 /* Delay for some milliseconds */
2198                 case TERM_XTRA_DELAY:
2199                 {
2200                         return (Term_xtra_win_delay(v));
2201                 }
2202         }
2203
2204         /* Oops */
2205         return 1;
2206 }
2207
2208
2209
2210 /*
2211  * Low level graphics (Assumes valid input).
2212  *
2213  * Draw a "cursor" at (x,y), using a "yellow box".
2214  */
2215 static errr Term_curs_win(int x, int y)
2216 {
2217         term_data *td = (term_data*)(Term->data);
2218
2219         RECT rc;
2220         HDC hdc;
2221
2222         int tile_wid, tile_hgt;
2223
2224         if (td->map_active)
2225         {
2226                 tile_wid = td->map_tile_wid;
2227                 tile_hgt = td->map_tile_hgt;
2228         }
2229         else
2230         {
2231                 tile_wid = td->tile_wid;
2232                 tile_hgt = td->tile_hgt;
2233         }
2234
2235         /* Frame the grid */
2236         rc.left = x * tile_wid + td->size_ow1;
2237         rc.right = rc.left + tile_wid;
2238         rc.top = y * tile_hgt + td->size_oh1;
2239         rc.bottom = rc.top + tile_hgt;
2240
2241         /* Cursor is done as a yellow "box" */
2242         hdc = GetDC(td->w);
2243         FrameRect(hdc, &rc, hbrYellow);
2244         ReleaseDC(td->w, hdc);
2245
2246         /* Success */
2247         return 0;
2248 }
2249
2250
2251 /*
2252  * Low level graphics (Assumes valid input).
2253  *
2254  * Draw a "big cursor" at (x,y), using a "yellow box".
2255  */
2256 static errr Term_bigcurs_win(int x, int y)
2257 {
2258         term_data *td = (term_data*)(Term->data);
2259
2260         RECT rc;
2261         HDC hdc;
2262
2263         int tile_wid, tile_hgt;
2264
2265         if (td->map_active)
2266         {
2267                 /* Normal cursor in map window */
2268                 Term_curs_win(x, y);
2269                 return 0;
2270         }
2271         else
2272         {
2273                 tile_wid = td->tile_wid;
2274                 tile_hgt = td->tile_hgt;
2275         }
2276
2277         /* Frame the grid */
2278         rc.left = x * tile_wid + td->size_ow1;
2279         rc.right = rc.left + 2 * tile_wid;
2280         rc.top = y * tile_hgt + td->size_oh1;
2281         rc.bottom = rc.top + tile_hgt;
2282
2283         /* Cursor is done as a yellow "box" */
2284         hdc = GetDC(td->w);
2285         FrameRect(hdc, &rc, hbrYellow);
2286         ReleaseDC(td->w, hdc);
2287
2288         /* Success */
2289         return 0;
2290 }
2291
2292
2293 /*
2294  * Low level graphics (Assumes valid input).
2295  *
2296  * Erase a "block" of "n" characters starting at (x,y).
2297  */
2298 static errr Term_wipe_win(int x, int y, int n)
2299 {
2300         term_data *td = (term_data*)(Term->data);
2301
2302         HDC hdc;
2303         RECT rc;
2304
2305         /* Rectangle to erase in client coords */
2306         rc.left = x * td->tile_wid + td->size_ow1;
2307         rc.right = rc.left + n * td->tile_wid;
2308         rc.top = y * td->tile_hgt + td->size_oh1;
2309         rc.bottom = rc.top + td->tile_hgt;
2310
2311         hdc = GetDC(td->w);
2312         SetBkColor(hdc, RGB(0, 0, 0));
2313         SelectObject(hdc, td->font_id);
2314         /* bg */
2315         if (use_bg)
2316                 DrawBG(hdc, &rc);
2317         else
2318                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2319         ReleaseDC(td->w, hdc);
2320
2321         /* Success */
2322         return 0;
2323 }
2324
2325
2326 /*
2327  * Low level graphics.  Assumes valid input.
2328  *
2329  * Draw several ("n") chars, with an attr, at a given location.
2330  *
2331  * All "graphic" data is handled by "Term_pict_win()", below.
2332  *
2333  * One would think there is a more efficient method for telling a window
2334  * what color it should be using to draw with, but perhaps simply changing
2335  * it every time is not too inefficient.  XXX XXX XXX
2336  */
2337 static errr Term_text_win(int x, int y, int n, byte a, const char *s)
2338 {
2339         term_data *td = (term_data*)(Term->data);
2340         RECT rc;
2341         HDC hdc;
2342
2343
2344 #ifdef JP
2345         static HBITMAP  WALL;
2346         static HBRUSH   myBrush, oldBrush;
2347         static HPEN     oldPen;
2348         static bool init_done = FALSE;
2349
2350         if (!init_done){
2351                 WALL = LoadBitmap(hInstance, AppName);
2352                 myBrush = CreatePatternBrush(WALL);
2353                 init_done = TRUE;
2354         }
2355 #endif
2356
2357         /* Total rectangle */
2358         rc.left = x * td->tile_wid + td->size_ow1;
2359         rc.right = rc.left + n * td->tile_wid;
2360         rc.top = y * td->tile_hgt + td->size_oh1;
2361         rc.bottom = rc.top + td->tile_hgt;
2362
2363         /* Acquire DC */
2364         hdc = GetDC(td->w);
2365
2366         /* Background color */
2367         SetBkColor(hdc, RGB(0, 0, 0));
2368
2369         /* Foreground color */
2370         if (colors16)
2371         {
2372                 SetTextColor(hdc, PALETTEINDEX(win_pal[a]));
2373         }
2374         else if (paletted)
2375         {
2376                 SetTextColor(hdc, win_clr[a&0x0F]);
2377         }
2378         else
2379         {
2380                 SetTextColor(hdc, win_clr[a]);
2381         }
2382
2383         /* Use the font */
2384         SelectObject(hdc, td->font_id);
2385         
2386         /* bg */
2387         if (use_bg) SetBkMode(hdc, TRANSPARENT);
2388
2389         /* Bizarre size */
2390         if (td->bizarre ||
2391             (td->tile_hgt != td->font_hgt) ||
2392             (td->tile_wid != td->font_wid))
2393         {
2394                 int i;
2395
2396                 /* Erase complete rectangle */
2397                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2398                 
2399                 /* bg */
2400                 if (use_bg) DrawBG(hdc, &rc);
2401
2402                 /* New rectangle */
2403                 rc.left += ((td->tile_wid - td->font_wid) / 2);
2404                 rc.right = rc.left + td->font_wid;
2405                 rc.top += ((td->tile_hgt - td->font_hgt) / 2);
2406                 rc.bottom = rc.top + td->font_hgt;
2407
2408                 /* Dump each character */
2409                 for (i = 0; i < n; i++)
2410                 {
2411 #ifdef JP
2412                         if (use_bigtile && *(s+i)=="¢£"[0] && *(s+i+1)=="¢£"[1])
2413                         {
2414                                 rc.right += td->font_wid;
2415
2416                                 oldBrush = SelectObject(hdc, myBrush);
2417                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2418
2419                                 /* Dump the wall */
2420                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2421
2422                                 SelectObject(hdc, oldBrush);
2423                                 SelectObject(hdc, oldPen);
2424                                 rc.right -= td->font_wid;
2425
2426                                 /* Advance */
2427                                 i++;
2428                                 rc.left += 2 * td->tile_wid;
2429                                 rc.right += 2 * td->tile_wid;
2430                         }
2431                         else if ( iskanji(*(s+i)) )  /*  £²¥Ð¥¤¥Èʸ»ú  */
2432                         {
2433                                 rc.right += td->font_wid;
2434                                 /* Dump the text */
2435                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2436                                        s+i, 2, NULL);
2437                                 rc.right -= td->font_wid;
2438
2439                                 /* Advance */
2440                                 i++;
2441                                 rc.left += 2 * td->tile_wid;
2442                                 rc.right += 2 * td->tile_wid;
2443                         } else if (*(s+i)==127){
2444                                 oldBrush = SelectObject(hdc, myBrush);
2445                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2446
2447                                 /* Dump the wall */
2448                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2449
2450                                 SelectObject(hdc, oldBrush);
2451                                 SelectObject(hdc, oldPen);
2452
2453                                 /* Advance */
2454                                 rc.left += td->tile_wid;
2455                                 rc.right += td->tile_wid;
2456                         } else {
2457                                 /* Dump the text */
2458                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2459                                        s+i, 1, NULL);
2460
2461                                 /* Advance */
2462                                 rc.left += td->tile_wid;
2463                                 rc.right += td->tile_wid;
2464                         }
2465 #else
2466                         /* Dump the text */
2467                         ExtTextOut(hdc, rc.left, rc.top, 0, &rc,
2468                                    s+i, 1, NULL);
2469
2470                         /* Advance */
2471                         rc.left += td->tile_wid;
2472                         rc.right += td->tile_wid;
2473 #endif
2474
2475                 }
2476         }
2477
2478         /* Normal size */
2479         else
2480         {
2481                 /* Dump the text */
2482                 ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
2483                            s, n, NULL);
2484         }
2485
2486         /* Release DC */
2487         ReleaseDC(td->w, hdc);
2488
2489         /* Success */
2490         return 0;
2491 }
2492
2493
2494 /*
2495  * Low level graphics.  Assumes valid input.
2496  *
2497  * Draw an array of "special" attr/char pairs at the given location.
2498  *
2499  * We use the "Term_pict_win()" function for "graphic" data, which are
2500  * encoded by setting the "high-bits" of both the "attr" and the "char"
2501  * data.  We use the "attr" to represent the "row" of the main bitmap,
2502  * and the "char" to represent the "col" of the main bitmap.  The use
2503  * of this function is induced by the "higher_pict" flag.
2504  *
2505  * If "graphics" is not available, we simply "wipe" the given grids.
2506  */
2507 # ifdef USE_TRANSPARENCY
2508 static errr Term_pict_win(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
2509 # else /* USE_TRANSPARENCY */
2510 static errr Term_pict_win(int x, int y, int n, const byte *ap, const char *cp)
2511 # endif /* USE_TRANSPARENCY */
2512 {
2513         term_data *td = (term_data*)(Term->data);
2514
2515 #ifdef USE_GRAPHICS
2516
2517         int i;
2518         int x1, y1, w1, h1;
2519         int x2, y2, w2, h2, tw2;
2520
2521 # ifdef USE_TRANSPARENCY
2522
2523         int x3, y3;
2524
2525         HDC hdcMask;
2526
2527 # endif /* USE_TRANSPARENCY */
2528
2529         HDC hdc;
2530         HDC hdcSrc;
2531         HBITMAP hbmSrcOld;
2532
2533         /* Paranoia */
2534         if (!use_graphics)
2535         {
2536                 /* Erase the grids */
2537                 return (Term_wipe_win(x, y, n));
2538         }
2539
2540         /* Size of bitmap cell */
2541         w1 = infGraph.CellWidth;
2542         h1 = infGraph.CellHeight;
2543
2544         /* Size of window cell */
2545         if (td->map_active)
2546         {
2547                 w2 = td->map_tile_wid;
2548                 h2 = td->map_tile_hgt;
2549         }
2550         else
2551         {
2552                 w2 = td->tile_wid;
2553                 h2 = td->tile_hgt;
2554                 tw2 = w2;
2555
2556                 /* big tile mode */
2557                 if (use_bigtile) tw2 *= 2;
2558         }
2559
2560         /* Location of window cell */
2561         x2 = x * w2 + td->size_ow1;
2562         y2 = y * h2 + td->size_oh1;
2563
2564         /* Info */
2565         hdc = GetDC(td->w);
2566
2567         /* More info */
2568         hdcSrc = CreateCompatibleDC(hdc);
2569         hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
2570
2571 # ifdef USE_TRANSPARENCY
2572
2573         if (arg_graphics == GRAPHICS_ADAM_BOLT)
2574         {
2575                 hdcMask = CreateCompatibleDC(hdc);
2576                 SelectObject(hdcMask, infMask.hBitmap);
2577         }
2578
2579 # endif /* USE_TRANSPARENCY */
2580
2581         /* Draw attr/char pairs */
2582         for (i = 0; i < n; i++, x2 += w2)
2583         {
2584                 byte a = ap[i];
2585                 char c = cp[i];
2586
2587                 /* Extract picture */
2588                 int row = (a & 0x7F);
2589                 int col = (c & 0x7F);
2590
2591                 /* Location of bitmap cell */
2592                 x1 = col * w1;
2593                 y1 = row * h1;
2594
2595 # ifdef USE_TRANSPARENCY
2596
2597                 if (arg_graphics == GRAPHICS_ADAM_BOLT)
2598                 {
2599                         x3 = (tcp[i] & 0x7F) * w1;
2600                         y3 = (tap[i] & 0x7F) * h1;
2601
2602                         /* Perfect size */
2603                         if ((w1 == tw2) && (h1 == h2))
2604                         {
2605                                 /* Copy the terrain picture from the bitmap to the window */
2606                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
2607
2608                                 /* Mask out the tile */
2609                                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
2610
2611                                 /* Draw the tile */
2612                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
2613                         }
2614
2615                         /* Need to stretch */
2616                         else
2617                         {
2618                                 /* Set the correct mode for stretching the tiles */
2619                                 SetStretchBltMode(hdc, COLORONCOLOR);
2620
2621                                 /* Copy the terrain picture from the bitmap to the window */
2622                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCCOPY);
2623
2624                                 /* Only draw if terrain and overlay are different */
2625                                 if ((x1 != x3) || (y1 != y3))
2626                                 {
2627                                         /* Mask out the tile */
2628                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
2629
2630                                         /* Draw the tile */
2631                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
2632                                 }
2633                         }
2634                 }
2635                 else
2636
2637 # endif /* USE_TRANSPARENCY */
2638
2639                 {
2640                         /* Perfect size */
2641                         if ((w1 == tw2) && (h1 == h2))
2642                         {
2643                                 /* Copy the picture from the bitmap to the window */
2644                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
2645                         }
2646
2647                         /* Need to stretch */
2648                         else
2649                         {
2650                                 /* Set the correct mode for stretching the tiles */
2651                                 SetStretchBltMode(hdc, COLORONCOLOR);
2652
2653                                 /* Copy the picture from the bitmap to the window */
2654                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
2655                         }
2656                 }
2657         }
2658
2659         /* Release */
2660         SelectObject(hdcSrc, hbmSrcOld);
2661         DeleteDC(hdcSrc);
2662
2663 # ifdef USE_TRANSPARENCY
2664
2665         if (arg_graphics == GRAPHICS_ADAM_BOLT)
2666         {
2667                 /* Release */
2668                 SelectObject(hdcMask, hbmSrcOld);
2669                 DeleteDC(hdcMask);
2670         }
2671
2672 # endif /* USE_TRANSPARENCY */
2673
2674         /* Release */
2675         ReleaseDC(td->w, hdc);
2676
2677 #else /* USE_GRAPHICS */
2678
2679         /* Just erase this grid */
2680         return (Term_wipe_win(x, y, n));
2681
2682 #endif /* USE_GRAPHICS */
2683
2684         /* Success */
2685         return 0;
2686 }
2687
2688
2689 static void windows_map(void)
2690 {
2691         term_data *td = &data[0];
2692         byte a, c;
2693         int x, min_x, max_x;
2694         int y, min_y, max_y;
2695
2696 #ifdef USE_TRANSPARENCY
2697         byte ta, tc;
2698 #endif
2699
2700         /* Only in graphics mode */
2701         if (!use_graphics) return;
2702
2703         /* Clear screen */
2704         Term_xtra_win_clear();
2705
2706         td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
2707         td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
2708         td->map_active = TRUE;
2709
2710         {
2711                 min_x = 0;
2712                 min_y = 0;
2713                 max_x = cur_wid;
2714                 max_y = cur_hgt;
2715         }
2716
2717         /* Draw the map */
2718         for (x = min_x; x < max_x; x++)
2719         {
2720                 for (y = min_y; y < max_y; y++)
2721                 {
2722 #ifdef USE_TRANSPARENCY
2723                         map_info(y, x, &a, (char*)&c, &ta, (char*)&tc);
2724 #else /* USE_TRANSPARENCY */
2725                         map_info(y, x, &a, (char*)&c);
2726 #endif /* USE_TRANSPARENCY */
2727
2728                         /* Ignore non-graphics */
2729                         if ((a & 0x80) && (c & 0x80))
2730                         {
2731 #ifdef USE_TRANSPARENCY
2732                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
2733 #else /* USE_TRANSPARENCY */
2734                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c);
2735 #endif /* USE_TRANSPARENCY */
2736                         }
2737                 }
2738         }
2739
2740         /* Hilite the player */
2741         Term_curs_win(px - min_x, py - min_y);
2742
2743         /* Wait for a keypress, flush key buffer */
2744         Term_inkey(&c, TRUE, TRUE);
2745         Term_flush();
2746
2747         /* Switch off the map display */
2748         td->map_active = FALSE;
2749
2750         /* Restore screen */
2751         Term_xtra_win_clear();
2752         Term_redraw();
2753 }
2754
2755
2756 /*** Other routines ***/
2757
2758
2759 /*
2760  * Create and initialize a "term_data" given a title
2761  */
2762 static void term_data_link(term_data *td)
2763 {
2764         term *t = &td->t;
2765
2766         /* Initialize the term */
2767         term_init(t, td->cols, td->rows, td->keys);
2768
2769         /* Use a "software" cursor */
2770         t->soft_cursor = TRUE;
2771
2772         /* Use "Term_pict" for "graphic" data */
2773         t->higher_pict = TRUE;
2774
2775         /* Erase with "white space" */
2776         t->attr_blank = TERM_WHITE;
2777         t->char_blank = ' ';
2778
2779 #if 0
2780         /* Prepare the init/nuke hooks */
2781         t->init_hook = Term_init_win;
2782         t->nuke_hook = Term_nuke_win;
2783 #endif
2784
2785         /* Prepare the template hooks */
2786         t->user_hook = Term_user_win;
2787         t->xtra_hook = Term_xtra_win;
2788         t->curs_hook = Term_curs_win;
2789         t->bigcurs_hook = Term_bigcurs_win;
2790         t->wipe_hook = Term_wipe_win;
2791         t->text_hook = Term_text_win;
2792         t->pict_hook = Term_pict_win;
2793
2794         /* Remember where we came from */
2795         t->data = (vptr)(td);
2796 }
2797
2798
2799 /*
2800  * Create the windows
2801  *
2802  * First, instantiate the "default" values, then read the "ini_file"
2803  * to over-ride selected values, then create the windows, and fonts.
2804  *
2805  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
2806  * must make active to realize the palette.  XXX XXX XXX
2807  */
2808 static void init_windows(void)
2809 {
2810         int i;
2811
2812         term_data *td;
2813
2814 #ifndef JP
2815         char buf[1024];
2816 #endif
2817
2818         /* Main window */
2819         td = &data[0];
2820         WIPE(td, term_data);
2821 #ifdef JP
2822         td->s = "ÊѶòÈÚÅÜ";
2823 #else
2824         td->s = angband_term_name[0];
2825 #endif
2826
2827         td->keys = 1024;
2828         td->rows = 24;
2829         td->cols = 80;
2830         td->visible = TRUE;
2831         td->size_ow1 = 2;
2832         td->size_ow2 = 2;
2833         td->size_oh1 = 2;
2834         td->size_oh2 = 2;
2835         td->pos_x = 7 * 30;
2836         td->pos_y = 7 * 20;
2837         td->posfix = FALSE;
2838
2839 #ifdef JP
2840         td->bizarre = TRUE;
2841 #endif
2842         /* Sub windows */
2843         for (i = 1; i < MAX_TERM_DATA; i++)
2844         {
2845                 td = &data[i];
2846                 WIPE(td, term_data);
2847                 td->s = angband_term_name[i];
2848                 td->keys = 16;
2849                 td->rows = 24;
2850                 td->cols = 80;
2851                 td->visible = FALSE;
2852                 td->size_ow1 = 1;
2853                 td->size_ow2 = 1;
2854                 td->size_oh1 = 1;
2855                 td->size_oh2 = 1;
2856                 td->pos_x = (7 - i) * 30;
2857                 td->pos_y = (7 - i) * 20;
2858                 td->posfix = FALSE;
2859 #ifdef JP
2860                         td->bizarre = TRUE;
2861 #endif
2862         }
2863
2864
2865         /* Load prefs */
2866         load_prefs();
2867
2868
2869         /* Main window (need these before term_getsize gets called) */
2870         td = &data[0];
2871         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
2872                        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
2873                        WS_VISIBLE);
2874         td->dwExStyle = 0;
2875         td->visible = TRUE;
2876
2877         /* Sub windows (need these before term_getsize gets called) */
2878         for (i = 1; i < MAX_TERM_DATA; i++)
2879         {
2880                 td = &data[i];
2881                 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
2882                 td->dwExStyle = (WS_EX_TOOLWINDOW);
2883         }
2884
2885
2886         /* All windows */
2887         for (i = 0; i < MAX_TERM_DATA; i++)
2888         {
2889                 td = &data[i];
2890
2891 #ifdef JP
2892                 strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
2893                 td->lf.lfHeight = td->tile_hgt;
2894                 td->lf.lfWidth  = td->tile_wid;
2895                 td->lf.lfCharSet = SHIFTJIS_CHARSET;
2896                 td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
2897                 /* Activate the chosen font */
2898                 term_force_font(td, NULL);
2899                 td->tile_wid = td->font_wid;
2900                 td->tile_hgt = td->font_hgt;
2901 #else
2902                 /* Access the standard font file */
2903                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, td->font_want);
2904
2905                 /* Activate the chosen font */
2906                 if (term_force_font(td, buf))
2907                 {
2908                         /* Access the standard font file */
2909                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
2910
2911                         /* Force the use of that font */
2912                         (void)term_force_font(td, buf);
2913
2914                         /* Oops */
2915                         td->tile_wid = 8;
2916                         td->tile_hgt = 13;
2917
2918                         /* Assume not bizarre */
2919                         td->bizarre = FALSE;
2920                 }
2921 #endif
2922
2923
2924                 /* Analyze the font */
2925                 term_getsize(td);
2926
2927                 /* Resize the window */
2928                 term_window_resize(td);
2929         }
2930
2931
2932         /* Sub windows (reverse order) */
2933         for (i = MAX_TERM_DATA - 1; i >= 1; --i)
2934         {
2935                 td = &data[i];
2936
2937                 my_td = td;
2938                 td->w = CreateWindowEx(td->dwExStyle, AngList,
2939                                        td->s, td->dwStyle,
2940                                        td->pos_x, td->pos_y,
2941                                        td->size_wid, td->size_hgt,
2942                                        HWND_DESKTOP, NULL, hInstance, NULL);
2943                 my_td = NULL;
2944 #ifdef JP
2945                 if (!td->w) quit("¥µ¥Ö¥¦¥£¥ó¥É¥¦¤ËºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
2946 #else
2947                 if (!td->w) quit("Failed to create sub-window");
2948 #endif
2949
2950
2951                 if (td->visible)
2952                 {
2953                         td->size_hack = TRUE;
2954                         ShowWindow(td->w, SW_SHOW);
2955                         td->size_hack = FALSE;
2956                 }
2957
2958                 term_data_link(td);
2959                 angband_term[i] = &td->t;
2960
2961                 if (td->visible)
2962                 {
2963                         /* Activate the window */
2964                         SetActiveWindow(td->w);
2965                 }
2966
2967                 if (data[i].posfix)
2968                 {
2969                         term_window_pos(&data[i], HWND_TOPMOST);
2970                 }
2971                 else
2972                 {
2973                         term_window_pos(&data[i], td->w);
2974                 }
2975         }
2976
2977
2978         /* Main window */
2979         td = &data[0];
2980
2981         /* Main window */
2982         my_td = td;
2983         td->w = CreateWindowEx(td->dwExStyle, AppName,
2984                                td->s, td->dwStyle,
2985                                td->pos_x, td->pos_y,
2986                                td->size_wid, td->size_hgt,
2987                                HWND_DESKTOP, NULL, hInstance, NULL);
2988         my_td = NULL;
2989 #ifdef JP
2990         if (!td->w) quit("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÎºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
2991 #else
2992         if (!td->w) quit("Failed to create Angband window");
2993 #endif
2994
2995
2996         term_data_link(td);
2997         angband_term[0] = &td->t;
2998
2999         /* Activate the main window */
3000         SetActiveWindow(td->w);
3001
3002         /* Bring main window back to top */
3003         SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3004
3005
3006         /* New palette XXX XXX XXX */
3007         (void)new_palette();
3008
3009
3010         /* Create a "brush" for drawing the "cursor" */
3011         hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
3012
3013
3014         /* Process pending messages */
3015         (void)Term_xtra_win_flush();
3016 }
3017
3018
3019
3020 /*
3021  * Prepare the menus
3022  */
3023 static void setup_menus(void)
3024 {
3025         int i;
3026
3027         HMENU hm = GetMenu(data[0].w);
3028
3029
3030         /* Menu "File", Disable all */
3031         EnableMenuItem(hm, IDM_FILE_NEW,
3032                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3033         EnableMenuItem(hm, IDM_FILE_OPEN,
3034                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3035         EnableMenuItem(hm, IDM_FILE_SAVE,
3036                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3037         EnableMenuItem(hm, IDM_FILE_EXIT,
3038                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3039         EnableMenuItem(hm, IDM_FILE_SCORE,
3040                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3041
3042
3043         /* No character available */
3044         if (!character_generated)
3045         {
3046                 /* Menu "File", Item "New" */
3047                 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
3048
3049                 /* Menu "File", Item "Open" */
3050                 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
3051         }
3052
3053         /* A character available */
3054         if (character_generated)
3055         {
3056                 /* Menu "File", Item "Save" */
3057                 EnableMenuItem(hm, IDM_FILE_SAVE,
3058                            MF_BYCOMMAND | MF_ENABLED);
3059         }
3060
3061         /* Menu "File", Item "Exit" */
3062         EnableMenuItem(hm, IDM_FILE_EXIT,
3063                        MF_BYCOMMAND | MF_ENABLED);
3064
3065         EnableMenuItem(hm, IDM_FILE_SCORE,
3066                        MF_BYCOMMAND | MF_ENABLED);
3067
3068
3069         /* Menu "Window::Visibility" */
3070         for (i = 0; i < MAX_TERM_DATA; i++)
3071         {
3072                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3073                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3074
3075                 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3076                               (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
3077
3078                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3079                                MF_BYCOMMAND | MF_ENABLED);
3080         }
3081
3082         /* Menu "Window::Font" */
3083         for (i = 0; i < MAX_TERM_DATA; i++)
3084         {
3085                 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3086                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3087
3088                 if (data[i].visible)
3089                 {
3090                         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3091                                        MF_BYCOMMAND | MF_ENABLED);
3092                 }
3093         }
3094
3095         /* Menu "Window::Window Position Fix" */
3096         for (i = 0; i < MAX_TERM_DATA; i++)
3097         {
3098                 EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3099                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3100
3101                 CheckMenuItem(hm, IDM_WINDOW_POS_0 + i,
3102                               (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
3103
3104                 if (data[i].visible)
3105                 {
3106                         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3107                                        MF_BYCOMMAND | MF_ENABLED);
3108                 }
3109         }
3110
3111         /* Menu "Window::Bizarre Display" */
3112         for (i = 0; i < MAX_TERM_DATA; i++)
3113         {
3114                 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3115                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3116
3117                 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3118                               (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
3119
3120                 if (data[i].visible)
3121                 {
3122                         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3123                                    MF_BYCOMMAND | MF_ENABLED);
3124
3125                 }
3126         }
3127
3128         /* Menu "Window::Increase Tile Width" */
3129         for (i = 0; i < MAX_TERM_DATA; i++)
3130         {
3131                 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3132                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3133
3134                 if (data[i].visible)
3135                 {
3136                         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3137                                    MF_BYCOMMAND | MF_ENABLED);
3138
3139                 }
3140         }
3141
3142         /* Menu "Window::Decrease Tile Width" */
3143         for (i = 0; i < MAX_TERM_DATA; i++)
3144         {
3145                 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3146                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3147
3148                 if (data[i].visible)
3149                 {
3150                         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3151                                    MF_BYCOMMAND | MF_ENABLED);
3152
3153                 }
3154         }
3155
3156         /* Menu "Window::Increase Tile Height" */
3157         for (i = 0; i < MAX_TERM_DATA; i++)
3158         {
3159                 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3160                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3161
3162                 if (data[i].visible)
3163                 {
3164                         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3165                                    MF_BYCOMMAND | MF_ENABLED);
3166
3167                 }
3168         }
3169
3170         /* Menu "Window::Decrease Tile Height" */
3171         for (i = 0; i < MAX_TERM_DATA; i++)
3172         {
3173                 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3174                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3175
3176                 if (data[i].visible)
3177                 {
3178                         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3179                                    MF_BYCOMMAND | MF_ENABLED);
3180
3181                 }
3182         }
3183
3184         /* Menu "Options", disable all */
3185         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3186                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3187         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3188                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3189         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3190                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3191         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE,
3192                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3193         EnableMenuItem(hm, IDM_OPTIONS_SOUND,
3194                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3195 #ifndef JP
3196         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3197                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3198 #endif
3199
3200         /* Menu "Options", Item "Map" */
3201         if (use_graphics != GRAPHICS_NONE)
3202                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
3203         else
3204                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3205                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3206
3207         /* Menu "Options", update all */
3208         CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3209                       (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
3210         CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3211                       (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
3212         CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3213                       (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
3214         CheckMenuItem(hm, IDM_OPTIONS_BIGTILE,
3215                       (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
3216         CheckMenuItem(hm, IDM_OPTIONS_SOUND,
3217                       (arg_sound ? MF_CHECKED : MF_UNCHECKED));
3218         CheckMenuItem(hm, IDM_OPTIONS_BG,
3219                       (use_bg ? MF_CHECKED : MF_UNCHECKED));
3220 #ifndef JP
3221         CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3222                       (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3223 #endif
3224
3225 #ifdef USE_GRAPHICS
3226         /* Menu "Options", Item "Graphics" */
3227         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
3228         /* Menu "Options", Item "Graphics" */
3229         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
3230         /* Menu "Options", Item "Graphics" */
3231         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
3232         /* Menu "Options", Item "Graphics" */
3233         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
3234 #endif /* USE_GRAPHICS */
3235
3236 #ifdef USE_SOUND
3237         /* Menu "Options", Item "Sound" */
3238         EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
3239 #endif /* USE_SOUND */
3240
3241 #ifdef USE_SAVER
3242         /* Menu "Options", Item "ScreenSaver" */
3243         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3244                        MF_BYCOMMAND | MF_ENABLED);
3245 #endif /* USE_SAVER */
3246 }
3247
3248
3249 /*
3250  * Check for double clicked (or dragged) savefile
3251  *
3252  * Apparently, Windows copies the entire filename into the first
3253  * piece of the "command line string".  Perhaps we should extract
3254  * the "basename" of that filename and append it to the "save" dir.
3255  */
3256 static void check_for_save_file(LPSTR cmd_line)
3257 {
3258         char *s;
3259
3260         /* First arg */
3261         s = cmd_line;
3262
3263         /* No args */
3264         if (!*s) return;
3265
3266         /* Extract filename */
3267         strcat(savefile, s);
3268
3269         /* Validate the file */
3270         validate_file(savefile);
3271
3272         /* Game in progress */
3273         game_in_progress = TRUE;
3274
3275         /* Play game */
3276         play_game(FALSE);
3277 }
3278
3279
3280 /*
3281  * Process a menu command
3282  */
3283 static void process_menus(WORD wCmd)
3284 {
3285         int i;
3286
3287         term_data *td;
3288
3289         OPENFILENAME ofn;
3290
3291         /* Analyze */
3292         switch (wCmd)
3293         {
3294                 /* New game */
3295                 case IDM_FILE_NEW:
3296                 {
3297                         if (!initialized)
3298                         {
3299 #ifdef JP
3300                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3301 #else
3302                                 plog("You cannot do that yet...");
3303 #endif
3304
3305                         }
3306                         else if (game_in_progress)
3307                         {
3308 #ifdef JP
3309                                 plog("¥×¥ì¥¤Ãæ¤Ï¿·¤·¤¤¥²¡¼¥à¤ò»Ï¤á¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3310 #else
3311                                 plog("You can't start a new game while you're still playing!");
3312 #endif
3313
3314                         }
3315                         else
3316                         {
3317                                 game_in_progress = TRUE;
3318                                 Term_flush();
3319                                 play_game(TRUE);
3320                                 quit(NULL);
3321                         }
3322                         break;
3323                 }
3324
3325                 /* Open game */
3326                 case IDM_FILE_OPEN:
3327                 {
3328                         if (!initialized)
3329                         {
3330 #ifdef JP
3331                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3332 #else
3333                                 plog("You cannot do that yet...");
3334 #endif
3335
3336                         }
3337                         else if (game_in_progress)
3338                         {
3339 #ifdef JP
3340                                 plog("¥×¥ì¥¤Ãæ¤Ï¥²¡¼¥à¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3341 #else
3342                                 plog("You can't open a new game while you're still playing!");
3343 #endif
3344
3345                         }
3346                         else
3347                         {
3348                                 memset(&ofn, 0, sizeof(ofn));
3349                                 ofn.lStructSize = sizeof(ofn);
3350                                 ofn.hwndOwner = data[0].w;
3351                                 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3352                                 ofn.nFilterIndex = 1;
3353                                 ofn.lpstrFile = savefile;
3354                                 ofn.nMaxFile = 1024;
3355                                 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3356                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
3357
3358                                 if (GetOpenFileName(&ofn))
3359                                 {
3360                                         /* Load 'savefile' */
3361                                         validate_file(savefile);
3362                                         game_in_progress = TRUE;
3363                                         Term_flush();
3364                                         play_game(FALSE);
3365                                         quit(NULL);
3366                                 }
3367                         }
3368                         break;
3369                 }
3370
3371                 /* Save game */
3372                 case IDM_FILE_SAVE:
3373                 {
3374                         if (game_in_progress && character_generated)
3375                         {
3376                                 /* Paranoia */
3377                                 if (!can_save)
3378                                 {
3379 #ifdef JP
3380                                         plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3381 #else
3382                                         plog("You may not do that right now.");
3383 #endif
3384
3385                                         break;
3386                                 }
3387
3388                                 /* Hack -- Forget messages */
3389                                 msg_flag = FALSE;
3390
3391                                 /* Save the game */
3392 #ifdef ZANGBAND
3393                                 do_cmd_save_game(FALSE);
3394 #else /* ZANGBAND */
3395                                 do_cmd_save_game();
3396 #endif /* ZANGBAND */
3397                         }
3398                         else
3399                         {
3400 #ifdef JP
3401                                 plog("º£¡¢¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3402 #else
3403                                 plog("You may not do that right now.");
3404 #endif
3405
3406                         }
3407                         break;
3408                 }
3409
3410                 /* Exit */
3411                 case IDM_FILE_EXIT:
3412                 {
3413                         if (game_in_progress && character_generated)
3414                         {
3415                                 /* Paranoia */
3416                                 if (!can_save)
3417                                 {
3418 #ifdef JP
3419                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
3420 #else
3421                                         plog("You may not do that right now.");
3422 #endif
3423
3424                                         break;
3425                                 }
3426
3427                                 /* Hack -- Forget messages */
3428                                 msg_flag = FALSE;
3429
3430                                 forget_lite();
3431                                 forget_view();
3432                                 clear_mon_lite();
3433
3434                                 /* Save the game */
3435 #ifdef ZANGBAND
3436                                 /* do_cmd_save_game(FALSE); */
3437 #else /* ZANGBAND */
3438                                 /* do_cmd_save_game(); */
3439 #endif /* ZANGBAND */
3440                                 Term_key_push(SPECIAL_KEY_QUIT);
3441                                 break;
3442                         }
3443                         quit(NULL);
3444                         break;
3445                 }
3446
3447                 /* Show scores */
3448                 case IDM_FILE_SCORE:
3449                 {
3450                         char buf[1024];
3451
3452                         /* Build the filename */
3453                         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
3454
3455                         /* Open the binary high score file, for reading */
3456                         highscore_fd = fd_open(buf, O_RDONLY);
3457
3458                         /* Paranoia -- No score file */
3459                         if (highscore_fd < 0)
3460                         {
3461                                 msg_print("Score file unavailable.");
3462                         }
3463                         else
3464                         {
3465                                 /* Save Screen */
3466                                 screen_save();
3467
3468                                 /* Clear screen */
3469                                 Term_clear();
3470
3471                                 /* Display the scores */
3472                                 display_scores_aux(0, MAX_HISCORES, -1, NULL);
3473
3474                                 /* Shut the high score file */
3475                                 (void)fd_close(highscore_fd);
3476
3477                                 /* Forget the high score fd */
3478                                 highscore_fd = -1;
3479
3480                                 /* Load screen */
3481                                 screen_load();
3482
3483                                 /* Hack - Flush it */
3484                                 Term_fresh();
3485                         }
3486
3487                         break;
3488                 }
3489
3490
3491                 case IDM_WINDOW_VIS_0:
3492                 {
3493 #ifdef JP
3494                         plog("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÏÈóɽ¼¨¤Ë¤Ç¤­¤Þ¤»¤ó¡ª");
3495 #else
3496                         plog("You are not allowed to do that!");
3497 #endif
3498
3499
3500                         break;
3501                 }
3502
3503                 /* Window visibility */
3504                 case IDM_WINDOW_VIS_1:
3505                 case IDM_WINDOW_VIS_2:
3506                 case IDM_WINDOW_VIS_3:
3507                 case IDM_WINDOW_VIS_4:
3508                 case IDM_WINDOW_VIS_5:
3509                 case IDM_WINDOW_VIS_6:
3510                 case IDM_WINDOW_VIS_7:
3511                 {
3512                         i = wCmd - IDM_WINDOW_VIS_0;
3513
3514                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3515
3516                         td = &data[i];
3517
3518                         if (!td->visible)
3519                         {
3520                                 td->visible = TRUE;
3521                                 ShowWindow(td->w, SW_SHOW);
3522                                 term_data_redraw(td);
3523                         }
3524                         else
3525                         {
3526                                 td->visible = FALSE;
3527                                 td->posfix = FALSE;
3528                                 ShowWindow(td->w, SW_HIDE);
3529                         }
3530
3531                         break;
3532                 }
3533
3534                 /* Window fonts */
3535                 case IDM_WINDOW_FONT_0:
3536                 case IDM_WINDOW_FONT_1:
3537                 case IDM_WINDOW_FONT_2:
3538                 case IDM_WINDOW_FONT_3:
3539                 case IDM_WINDOW_FONT_4:
3540                 case IDM_WINDOW_FONT_5:
3541                 case IDM_WINDOW_FONT_6:
3542                 case IDM_WINDOW_FONT_7:
3543                 {
3544                         i = wCmd - IDM_WINDOW_FONT_0;
3545
3546                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3547
3548                         td = &data[i];
3549
3550                         term_change_font(td);
3551
3552                         break;
3553                 }
3554
3555                 /* Window Z Position */
3556                 case IDM_WINDOW_POS_1:
3557                 case IDM_WINDOW_POS_2:
3558                 case IDM_WINDOW_POS_3:
3559                 case IDM_WINDOW_POS_4:
3560                 case IDM_WINDOW_POS_5:
3561                 case IDM_WINDOW_POS_6:
3562                 case IDM_WINDOW_POS_7:
3563                 {
3564                         i = wCmd - IDM_WINDOW_POS_0;
3565
3566                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3567
3568                         td = &data[i];
3569
3570                         if (!td->posfix && td->visible)
3571                         {
3572                                 td->posfix = TRUE;
3573                                 term_window_pos(td, HWND_TOPMOST);
3574                         }
3575                         else
3576                         {
3577                                 td->posfix = FALSE;
3578                                 term_window_pos(td, data[0].w);
3579                         }
3580
3581                         break;
3582                 }
3583
3584                 /* Bizarre Display */
3585                 case IDM_WINDOW_BIZ_0:
3586                 case IDM_WINDOW_BIZ_1:
3587                 case IDM_WINDOW_BIZ_2:
3588                 case IDM_WINDOW_BIZ_3:
3589                 case IDM_WINDOW_BIZ_4:
3590                 case IDM_WINDOW_BIZ_5:
3591                 case IDM_WINDOW_BIZ_6:
3592                 case IDM_WINDOW_BIZ_7:
3593                 {
3594                         i = wCmd - IDM_WINDOW_BIZ_0;
3595
3596                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3597
3598                         td = &data[i];
3599
3600                         td->bizarre = !td->bizarre;
3601
3602                         term_getsize(td);
3603
3604                         term_window_resize(td);
3605
3606                         break;
3607                 }
3608
3609                 /* Increase Tile Width */
3610                 case IDM_WINDOW_I_WID_0:
3611                 case IDM_WINDOW_I_WID_1:
3612                 case IDM_WINDOW_I_WID_2:
3613                 case IDM_WINDOW_I_WID_3:
3614                 case IDM_WINDOW_I_WID_4:
3615                 case IDM_WINDOW_I_WID_5:
3616                 case IDM_WINDOW_I_WID_6:
3617                 case IDM_WINDOW_I_WID_7:
3618                 {
3619                         i = wCmd - IDM_WINDOW_I_WID_0;
3620
3621                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3622
3623                         td = &data[i];
3624
3625                         td->tile_wid += 1;
3626
3627                         term_getsize(td);
3628
3629                         term_window_resize(td);
3630
3631                         break;
3632                 }
3633
3634                 /* Decrease Tile Height */
3635                 case IDM_WINDOW_D_WID_0:
3636                 case IDM_WINDOW_D_WID_1:
3637                 case IDM_WINDOW_D_WID_2:
3638                 case IDM_WINDOW_D_WID_3:
3639                 case IDM_WINDOW_D_WID_4:
3640                 case IDM_WINDOW_D_WID_5:
3641                 case IDM_WINDOW_D_WID_6:
3642                 case IDM_WINDOW_D_WID_7:
3643                 {
3644                         i = wCmd - IDM_WINDOW_D_WID_0;
3645
3646                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3647
3648                         td = &data[i];
3649
3650                         td->tile_wid -= 1;
3651
3652                         term_getsize(td);
3653
3654                         term_window_resize(td);
3655
3656                         break;
3657                 }
3658
3659                 /* Increase Tile Height */
3660                 case IDM_WINDOW_I_HGT_0:
3661                 case IDM_WINDOW_I_HGT_1:
3662                 case IDM_WINDOW_I_HGT_2:
3663                 case IDM_WINDOW_I_HGT_3:
3664                 case IDM_WINDOW_I_HGT_4:
3665                 case IDM_WINDOW_I_HGT_5:
3666                 case IDM_WINDOW_I_HGT_6:
3667                 case IDM_WINDOW_I_HGT_7:
3668                 {
3669                         i = wCmd - IDM_WINDOW_I_HGT_0;
3670
3671                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3672
3673                         td = &data[i];
3674
3675                         td->tile_hgt += 1;
3676
3677                         term_getsize(td);
3678
3679                         term_window_resize(td);
3680
3681                         break;
3682                 }
3683
3684                 /* Decrease Tile Height */
3685                 case IDM_WINDOW_D_HGT_0:
3686                 case IDM_WINDOW_D_HGT_1:
3687                 case IDM_WINDOW_D_HGT_2:
3688                 case IDM_WINDOW_D_HGT_3:
3689                 case IDM_WINDOW_D_HGT_4:
3690                 case IDM_WINDOW_D_HGT_5:
3691                 case IDM_WINDOW_D_HGT_6:
3692                 case IDM_WINDOW_D_HGT_7:
3693                 {
3694                         i = wCmd - IDM_WINDOW_D_HGT_0;
3695
3696                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3697
3698                         td = &data[i];
3699
3700                         td->tile_hgt -= 1;
3701
3702                         term_getsize(td);
3703
3704                         term_window_resize(td);
3705
3706                         break;
3707                 }
3708
3709                 case IDM_OPTIONS_NO_GRAPHICS:
3710                 {
3711                         /* Paranoia */
3712                         if (!inkey_flag)
3713                         {
3714                                 plog("You may not do that right now.");
3715                                 break;
3716                         }
3717
3718                         /* Toggle "arg_graphics" */
3719                         if (arg_graphics != GRAPHICS_NONE)
3720                         {
3721                                 arg_graphics = GRAPHICS_NONE;
3722
3723                                 /* React to changes */
3724                                 Term_xtra_win_react();
3725
3726                                 /* Hack -- Force redraw */
3727                                 Term_key_push(KTRL('R'));
3728                         }
3729
3730                         break;
3731                 }
3732
3733                 case IDM_OPTIONS_OLD_GRAPHICS:
3734                 {
3735                         /* Paranoia */
3736                         if (!inkey_flag)
3737                         {
3738                                 plog("You may not do that right now.");
3739                                 break;
3740                         }
3741
3742                         /* Toggle "arg_graphics" */
3743                         if (arg_graphics != GRAPHICS_ORIGINAL)
3744                         {
3745                                 arg_graphics = GRAPHICS_ORIGINAL;
3746
3747                                 /* React to changes */
3748                                 Term_xtra_win_react();
3749
3750                                 /* Hack -- Force redraw */
3751                                 Term_key_push(KTRL('R'));
3752                         }
3753
3754                         break;
3755                 }
3756
3757                 case IDM_OPTIONS_NEW_GRAPHICS:
3758                 {
3759                         /* Paranoia */
3760                         if (!inkey_flag)
3761                         {
3762                                 plog("You may not do that right now.");
3763                                 break;
3764                         }
3765
3766                         /* Toggle "arg_graphics" */
3767                         if (arg_graphics != GRAPHICS_ADAM_BOLT)
3768                         {
3769                                 arg_graphics = GRAPHICS_ADAM_BOLT;
3770
3771                                 /* React to changes */
3772                                 Term_xtra_win_react();
3773
3774                                 /* Hack -- Force redraw */
3775                                 Term_key_push(KTRL('R'));
3776                         }
3777
3778                         break;
3779                 }
3780
3781                 case IDM_OPTIONS_BIGTILE:
3782                 {
3783                         term_data *td = &data[0];
3784
3785                         /* Paranoia */
3786                         if (!inkey_flag)
3787                         {
3788                                 plog("You may not do that right now.");
3789                                 break;
3790                         }
3791
3792                         /* Toggle "arg_sound" */
3793                         arg_bigtile = !arg_bigtile;
3794
3795                         /* Activate */
3796                         Term_activate(&td->t);
3797
3798                         /* Resize the term */
3799                         Term_resize(td->cols, td->rows);
3800
3801                         /* Redraw later */
3802                         InvalidateRect(td->w, NULL, TRUE);
3803
3804                         break;
3805                 }
3806
3807                 case IDM_OPTIONS_SOUND:
3808                 {
3809                         /* Paranoia */
3810                         if (!inkey_flag)
3811                         {
3812                                 plog("You may not do that right now.");
3813                                 break;
3814                         }
3815
3816                         /* Toggle "arg_sound" */
3817                         arg_sound = !arg_sound;
3818
3819                         /* React to changes */
3820                         Term_xtra_win_react();
3821
3822                         /* Hack -- Force redraw */
3823                         Term_key_push(KTRL('R'));
3824
3825                         break;
3826                 }
3827
3828                 /* bg */
3829                 case IDM_OPTIONS_BG:
3830                 {
3831                         /* Paranoia */
3832                         if (!inkey_flag)
3833                         {
3834                                 plog("You may not do that right now.");
3835                                 break;
3836                         }
3837
3838                         /* Toggle "use_bg" */
3839                         use_bg = !use_bg;
3840
3841                         init_bg();
3842
3843                         /* React to changes */
3844                         Term_xtra_win_react();
3845
3846                         /* Hack -- Force redraw */
3847                         Term_key_push(KTRL('R'));
3848
3849                         break;
3850                 }
3851
3852                 /* bg */
3853                 case IDM_OPTIONS_OPEN_BG:
3854                 {
3855                         /* Paranoia */
3856                         if (!inkey_flag)
3857                         {
3858                                 plog("You may not do that right now.");
3859                                 break;
3860                         }
3861                         else
3862                         {
3863                                 memset(&ofn, 0, sizeof(ofn));
3864                                 ofn.lStructSize = sizeof(ofn);
3865                                 ofn.hwndOwner = data[0].w;
3866                                 ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
3867                                 ofn.nFilterIndex = 1;
3868                                 ofn.lpstrFile = bg_bitmap_file;
3869                                 ofn.nMaxFile = 1023;
3870                                 ofn.lpstrInitialDir = NULL;
3871 #ifdef JP
3872                                 ofn.lpstrTitle = "ÊÉ»æ¤òÁª¤ó¤Ç¤Í¡£";
3873 #else
3874                                 ofn.lpstrTitle = "Choose wall paper.";
3875 #endif
3876                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
3877
3878                                 if (GetOpenFileName(&ofn))
3879                                 {
3880                                         /* Load 'savefile' */
3881                                         use_bg = 1;
3882                                         init_bg();
3883                                 }
3884
3885                                 /* React to changes */
3886                                 Term_xtra_win_react();
3887
3888                                 /* Hack -- Force redraw */
3889                                 Term_key_push(KTRL('R'));
3890                         }
3891                         break;
3892                 }
3893
3894                 case IDM_DUMP_SCREEN_HTML:
3895                 {
3896                         static char buf[1024] = "";
3897                         memset(&ofn, 0, sizeof(ofn));
3898                         ofn.lStructSize = sizeof(ofn);
3899                         ofn.hwndOwner = data[0].w;
3900                         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
3901                         ofn.nFilterIndex = 1;
3902                         ofn.lpstrFile = buf;
3903                         ofn.nMaxFile = 1023;
3904                         ofn.lpstrDefExt = "html";
3905                         ofn.lpstrInitialDir = NULL;
3906 #ifdef JP
3907                         ofn.lpstrTitle = "HTML¤Ç¥¹¥¯¥ê¡¼¥ó¥À¥ó¥×¤òÊݸ";
3908 #else
3909                         ofn.lpstrTitle = "Save screen dump as HTML.";
3910 #endif
3911                         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
3912
3913                         if (GetSaveFileName(&ofn))
3914                         {
3915                                 do_cmd_save_screen_html_aux(buf, 0);
3916                         }
3917                         break;
3918                 }
3919
3920 #ifdef USE_SAVER
3921
3922                 case IDM_OPTIONS_SAVER:
3923                 {
3924                         if (hwndSaver)
3925                         {
3926                                 DestroyWindow(hwndSaver);
3927                                 hwndSaver = NULL;
3928                         }
3929                         else
3930                         {
3931                                 /* Create a screen scaver window */
3932                                 hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass",
3933                                                            "Angband Screensaver",
3934                                                            WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
3935                                                            0, 0, GetSystemMetrics(SM_CXSCREEN),
3936                                                            GetSystemMetrics(SM_CYSCREEN),
3937                                                            NULL, NULL, hInstance, NULL);
3938
3939                                 if (hwndSaver)
3940                                 {
3941                                         /* Push the window to the bottom XXX XXX XXX */
3942                                         SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3943                                 }
3944                                 else
3945                                 {
3946 #ifdef JP
3947                                         plog("¥¦¥£¥ó¥É¥¦¤òºîÀ®½ÐÍè¤Þ¤»¤ó");
3948 #else
3949                                         plog("Failed to create saver window");
3950 #endif
3951
3952                                 }
3953                         }
3954                         break;
3955                 }
3956
3957 #endif
3958
3959                 case IDM_OPTIONS_MAP:
3960                 {
3961                         windows_map();
3962                         break;
3963                 }
3964
3965                 case IDM_HELP_CONTENTS:
3966                 {
3967 #ifdef HTML_HELP
3968                         char tmp[1024];
3969                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.chm");
3970                         if (check_file(tmp))
3971                         {
3972                                 HtmlHelp(data[0].w, tmp, HH_DISPLAY_TOPIC, 0);
3973                         }
3974                         else
3975                         {
3976 #ifdef JP
3977                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
3978                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
3979 #else
3980                                 plog_fmt("Cannot find help file: %s", tmp);
3981                                 plog("Use the online help files instead.");
3982 #endif
3983
3984                         }
3985                         break;
3986 #else /* HTML_HELP */
3987                         char buf[1024];
3988                         char tmp[1024];
3989                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
3990                         if (check_file(tmp))
3991                         {
3992                                 sprintf(buf, "winhelp.exe %s", tmp);
3993                                 WinExec(buf, SW_NORMAL);
3994                         }
3995                         else
3996                         {
3997 #ifdef JP
3998                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
3999                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4000 #else
4001                                 plog_fmt("Cannot find help file: %s", tmp);
4002                                 plog("Use the online help files instead.");
4003 #endif
4004
4005                         }
4006                         break;
4007 #endif /* HTML_HELP */
4008                 }
4009         }
4010 }
4011
4012
4013
4014 #ifdef __MWERKS__
4015 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4016                                   WPARAM wParam, LPARAM lParam);
4017 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4018                                   WPARAM wParam, LPARAM lParam)
4019 #else /* __MWERKS__ */
4020 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4021                                           WPARAM wParam, LPARAM lParam)
4022 #endif /* __MWERKS__ */
4023 {
4024         PAINTSTRUCT ps;
4025         HDC hdc;
4026         term_data *td;
4027 #if 0
4028         MINMAXINFO FAR *lpmmi;
4029         RECT rc;
4030 #endif
4031         int i;
4032
4033
4034         /* Acquire proper "term_data" info */
4035         td = (term_data *)GetWindowLong(hWnd, 0);
4036
4037         /* Handle message */
4038         switch (uMsg)
4039         {
4040                 /* XXX XXX XXX */
4041                 case WM_NCCREATE:
4042                 {
4043                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4044                         break;
4045                 }
4046
4047                 /* XXX XXX XXX */
4048                 case WM_CREATE:
4049                 {
4050                         return 0;
4051                 }
4052
4053                 case WM_GETMINMAXINFO:
4054                 {
4055                         MINMAXINFO FAR *lpmmi;
4056                         RECT rc;
4057
4058                         lpmmi = (MINMAXINFO FAR *)lParam;
4059
4060                         /* this message was sent before WM_NCCREATE */
4061                         if (!td) return 1;
4062
4063                         /* Minimum window size is 80x24 */
4064                         rc.left = rc.top = 0;
4065                         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
4066                         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4067
4068                         /* Adjust */
4069                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4070
4071                         /* Save minimum size */
4072                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4073                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4074
4075                         return 0;
4076                 }
4077
4078                 case WM_PAINT:
4079                 {
4080                         BeginPaint(hWnd, &ps);
4081                         if (td) term_data_redraw(td);
4082                         EndPaint(hWnd, &ps);
4083                         ValidateRect(hWnd, NULL);
4084                         return 0;
4085                 }
4086
4087                 case WM_SYSKEYDOWN:
4088                 case WM_KEYDOWN:
4089                 {
4090                         bool mc = FALSE;
4091                         bool ms = FALSE;
4092                         bool ma = FALSE;
4093
4094                         /* Extract the modifiers */
4095                         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4096                         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4097                         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4098
4099                         /* Handle "special" keys */
4100                         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4101                         {
4102                                 /* Begin the macro trigger */
4103                                 Term_keypress(31);
4104
4105                                 /* Send the modifiers */
4106                                 if (mc) Term_keypress('C');
4107                                 if (ms) Term_keypress('S');
4108                                 if (ma) Term_keypress('A');
4109
4110                                 /* Extract "scan code" */
4111                                 i = LOBYTE(HIWORD(lParam));
4112
4113                                 /* Introduce the scan code */
4114                                 Term_keypress('x');
4115
4116                                 /* Encode the hexidecimal scan code */
4117                                 Term_keypress(hexsym[i/16]);
4118                                 Term_keypress(hexsym[i%16]);
4119
4120                                 /* End the macro trigger */
4121                                 Term_keypress(13);
4122
4123                                 return 0;
4124                         }
4125
4126                         break;
4127                 }
4128
4129                 case WM_CHAR:
4130                 {
4131                         Term_keypress(wParam);
4132                         return 0;
4133                 }
4134
4135                 case WM_INITMENU:
4136                 {
4137                         setup_menus();
4138                         return 0;
4139                 }
4140
4141                 case WM_CLOSE:
4142                 {
4143                         if (game_in_progress && character_generated)
4144                         {
4145                                 if (!can_save)
4146                                 {
4147 #ifdef JP
4148                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
4149 #else
4150                                         plog("You may not do that right now.");
4151 #endif
4152                                         return 0;
4153                                 }
4154
4155                                 /* Hack -- Forget messages */
4156                                 msg_flag = FALSE;
4157
4158                                 forget_lite();
4159                                 forget_view();
4160                                 clear_mon_lite();
4161
4162                                 /* Save the game */
4163 #ifdef ZANGBAND
4164                                 /* do_cmd_save_game(FALSE); */
4165 #else /* ZANGBAND */
4166                                 /* do_cmd_save_game(); */
4167 #endif /* ZANGBAND */
4168                                 Term_key_push(252);
4169                                 return 0;
4170                         }
4171                         quit(NULL);
4172                         return 0;
4173                 }
4174
4175                 case WM_QUIT:
4176                 {
4177                         quit(NULL);
4178                         return 0;
4179                 }
4180
4181                 case WM_COMMAND:
4182                 {
4183                         process_menus(LOWORD(wParam));
4184                         return 0;
4185                 }
4186
4187                 case WM_SIZE:
4188                 {
4189                         /* this message was sent before WM_NCCREATE */
4190                         if (!td) return 1;
4191
4192                         /* it was sent from inside CreateWindowEx */
4193                         if (!td->w) return 1;
4194
4195                         /* was sent from WM_SIZE */
4196                         if (td->size_hack) return 1;
4197
4198                         switch (wParam)
4199                         {
4200                                 case SIZE_MINIMIZED:
4201                                 {
4202                                         /* Hide sub-windows */
4203                                         for (i = 1; i < MAX_TERM_DATA; i++)
4204                                         {
4205                                                 if (data[i].visible) ShowWindow(data[i].w, SW_HIDE);
4206                                         }
4207                                         return 0;
4208                                 }
4209
4210                                 case SIZE_MAXIMIZED:
4211                                 {
4212                                         /* fall through XXX XXX XXX */
4213                                 }
4214
4215                                 case SIZE_RESTORED:
4216                                 {
4217                                         uint cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4218                                         uint rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4219
4220                                         /* New size */
4221                                         if ((td->cols != cols) || (td->rows != rows))
4222                                         {
4223                                                 /* Save the new size */
4224                                                 td->cols = cols;
4225                                                 td->rows = rows;
4226
4227                                                 /* Activate */
4228                                                 Term_activate(&td->t);
4229
4230                                                 /* Resize the term */
4231                                                 Term_resize(td->cols, td->rows);
4232
4233                                                 /* Redraw later */
4234                                                 InvalidateRect(td->w, NULL, TRUE);
4235                                         }
4236
4237                                         td->size_hack = TRUE;
4238
4239                                         /* Show sub-windows */
4240                                         for (i = 1; i < MAX_TERM_DATA; i++)
4241                                         {
4242                                                 if (data[i].visible) ShowWindow(data[i].w, SW_SHOW);
4243                                         }
4244
4245                                         td->size_hack = FALSE;
4246
4247                                         return 0;
4248                                 }
4249                         }
4250                         break;
4251                 }
4252
4253                 case WM_PALETTECHANGED:
4254                 {
4255                         /* Ignore if palette change caused by itself */
4256                         if ((HWND)wParam == hWnd) return 0;
4257
4258                         /* Fall through... */
4259                 }
4260
4261                 case WM_QUERYNEWPALETTE:
4262                 {
4263                         if (!paletted) return 0;
4264
4265                         hdc = GetDC(hWnd);
4266
4267                         SelectPalette(hdc, hPal, FALSE);
4268
4269                         i = RealizePalette(hdc);
4270
4271                         /* if any palette entries changed, repaint the window. */
4272                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4273
4274                         ReleaseDC(hWnd, hdc);
4275
4276                         return 0;
4277                 }
4278
4279                 case WM_ACTIVATE:
4280                 {
4281                         if (wParam && !HIWORD(lParam))
4282                         {
4283                                 /* Do something to sub-windows */
4284                                 for (i = 1; i < MAX_TERM_DATA; i++)
4285                                 {
4286                                         if (!data[i].posfix) term_window_pos(&data[i], hWnd);
4287                                 }
4288
4289                                 /* Focus on main window */
4290                                 SetFocus(hWnd);
4291
4292                                 return 0;
4293                         }
4294
4295                         break;
4296                 }
4297
4298                 case WM_ACTIVATEAPP:
4299                 {
4300                         for (i = 1; i < MAX_TERM_DATA; i++)
4301                         {
4302                                 if(data[i].visible)
4303                                 {
4304                                         if (wParam == TRUE)
4305                                         {
4306                                                 ShowWindow(data[i].w, SW_SHOW);
4307                                         }
4308                                         else
4309                                         {
4310                                                 ShowWindow(data[i].w, SW_HIDE);
4311                                         }
4312                                 }
4313                         }
4314                 }
4315         }
4316
4317         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4318 }
4319
4320
4321 #ifdef __MWERKS__
4322 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4323                                            WPARAM wParam, LPARAM lParam);
4324 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4325                                            WPARAM wParam, LPARAM lParam)
4326 #else /* __MWERKS__ */
4327 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4328                                            WPARAM wParam, LPARAM lParam)
4329 #endif /* __MWERKS__ */
4330 {
4331         term_data *td;
4332 #if 0
4333         MINMAXINFO FAR *lpmmi;
4334         RECT rc;
4335 #endif
4336         PAINTSTRUCT ps;
4337         HDC hdc;
4338         int i;
4339
4340
4341         /* Acquire proper "term_data" info */
4342         td = (term_data *)GetWindowLong(hWnd, 0);
4343
4344         /* Process message */
4345         switch (uMsg)
4346         {
4347                 /* XXX XXX XXX */
4348                 case WM_NCCREATE:
4349                 {
4350                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4351                         break;
4352                 }
4353
4354                 /* XXX XXX XXX */
4355                 case WM_CREATE:
4356                 {
4357                         return 0;
4358                 }
4359
4360                 case WM_GETMINMAXINFO:
4361                 {
4362                         MINMAXINFO FAR *lpmmi;
4363                         RECT rc;
4364
4365                         lpmmi = (MINMAXINFO FAR *)lParam;
4366
4367                         /* this message was sent before WM_NCCREATE */
4368                         if (!td) return 1;
4369
4370                         /* Minimum window size is 80x24 */
4371                         rc.left = rc.top = 0;
4372                         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
4373                         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4374
4375                         /* Adjust */
4376                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4377
4378                         /* Save minimum size */
4379                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4380                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4381
4382                         return 0;
4383                 }
4384
4385                 case WM_SIZE:
4386                 {
4387                         uint cols;
4388                         uint rows;
4389                         
4390                         /* this message was sent before WM_NCCREATE */
4391                         if (!td) return 1;
4392
4393                         /* it was sent from inside CreateWindowEx */
4394                         if (!td->w) return 1;
4395
4396                         /* was sent from inside WM_SIZE */
4397                         if (td->size_hack) return 1;
4398
4399                         td->size_hack = TRUE;
4400
4401                         cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4402                         rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4403
4404                         /* New size */
4405                         if ((td->cols != cols) || (td->rows != rows))
4406                         {
4407                                 /* Save old term */
4408                                 term *old_term = Term;
4409
4410                                 /* Save the new size */
4411                                 td->cols = cols;
4412                                 td->rows = rows;
4413
4414                                 /* Activate */
4415                                 Term_activate(&td->t);
4416
4417                                 /* Resize the term */
4418                                 Term_resize(td->cols, td->rows);
4419
4420                                 /* Activate */
4421                                 Term_activate(old_term);
4422
4423                                 /* Redraw later */
4424                                 InvalidateRect(td->w, NULL, TRUE);
4425
4426                                 /* HACK - Redraw all windows */
4427                                 p_ptr->window = 0xFFFFFFFF;
4428                                 window_stuff();
4429                         }
4430
4431                         td->size_hack = FALSE;
4432
4433                         return 0;
4434                 }
4435
4436                 case WM_PAINT:
4437                 {
4438                         BeginPaint(hWnd, &ps);
4439                         if (td) term_data_redraw(td);
4440                         EndPaint(hWnd, &ps);
4441                         return 0;
4442                 }
4443
4444                 case WM_SYSKEYDOWN:
4445                 case WM_KEYDOWN:
4446                 {
4447                         bool mc = FALSE;
4448                         bool ms = FALSE;
4449                         bool ma = FALSE;
4450
4451                         /* Extract the modifiers */
4452                         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4453                         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4454                         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4455
4456                         /* Handle "special" keys */
4457                         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4458                         {
4459                                 /* Begin the macro trigger */
4460                                 Term_keypress(31);
4461
4462                                 /* Send the modifiers */
4463                                 if (mc) Term_keypress('C');
4464                                 if (ms) Term_keypress('S');
4465                                 if (ma) Term_keypress('A');
4466
4467                                 /* Extract "scan code" */
4468                                 i = LOBYTE(HIWORD(lParam));
4469
4470                                 /* Introduce the scan code */
4471                                 Term_keypress('x');
4472
4473                                 /* Encode the hexidecimal scan code */
4474                                 Term_keypress(hexsym[i/16]);
4475                                 Term_keypress(hexsym[i%16]);
4476
4477                                 /* End the macro trigger */
4478                                 Term_keypress(13);
4479
4480                                 return 0;
4481                         }
4482
4483                         break;
4484                 }
4485
4486                 case WM_CHAR:
4487                 {
4488                         Term_keypress(wParam);
4489                         return 0;
4490                 }
4491
4492                 case WM_PALETTECHANGED:
4493                 {
4494                         /* ignore if palette change caused by itself */
4495                         if ((HWND)wParam == hWnd) return FALSE;
4496                         /* otherwise, fall through!!! */
4497                 }
4498
4499                 case WM_QUERYNEWPALETTE:
4500                 {
4501                         if (!paletted) return 0;
4502                         hdc = GetDC(hWnd);
4503                         SelectPalette(hdc, hPal, FALSE);
4504                         i = RealizePalette(hdc);
4505                         /* if any palette entries changed, repaint the window. */
4506                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4507                         ReleaseDC(hWnd, hdc);
4508                         return 0;
4509                 }
4510
4511                 case WM_NCLBUTTONDOWN:
4512                 {
4513
4514 #ifdef HTCLOSE
4515                         if (wParam == HTCLOSE) wParam = HTSYSMENU;
4516 #endif /* HTCLOSE */
4517
4518                         if (wParam == HTSYSMENU)
4519                         {
4520                                 if (td->visible)
4521                                 {
4522                                         td->visible = FALSE;
4523                                         ShowWindow(td->w, SW_HIDE);
4524                                 }
4525
4526                                 return 0;
4527                         }
4528
4529                         break;
4530                 }
4531         }
4532
4533         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4534 }
4535
4536
4537 #ifdef USE_SAVER
4538
4539 #define MOUSE_SENS 40
4540
4541 #ifdef __MWERKS__
4542 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4543                                     WPARAM wParam, LPARAM lParam);
4544 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4545                                     WPARAM wParam, LPARAM lParam)
4546 #else /* __MWERKS__ */
4547 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4548                                             WPARAM wParam, LPARAM lParam)
4549 #endif /* __MWERKS__ */
4550 {
4551         static int iMouse = 0;
4552         static WORD xMouse = 0;
4553         static WORD yMouse = 0;
4554
4555         int dx, dy;
4556
4557
4558         /* Process */
4559         switch (uMsg)
4560         {
4561                 /* XXX XXX XXX */
4562                 case WM_NCCREATE:
4563                 {
4564                         break;
4565                 }
4566
4567                 case WM_SETCURSOR:
4568                 {
4569                         SetCursor(NULL);
4570                         return 0;
4571                 }
4572
4573 #if 0
4574                 case WM_ACTIVATE:
4575                 {
4576                         if (LOWORD(wParam) == WA_INACTIVE) break;
4577
4578                         /* else fall through */
4579                 }
4580 #endif
4581
4582                 case WM_LBUTTONDOWN:
4583                 case WM_MBUTTONDOWN:
4584                 case WM_RBUTTONDOWN:
4585                 case WM_KEYDOWN:
4586                 {
4587                         SendMessage(hWnd, WM_CLOSE, 0, 0);
4588                         return 0;
4589                 }
4590
4591                 case WM_MOUSEMOVE:
4592                 {
4593                         if (iMouse)
4594                         {
4595                                 dx = LOWORD(lParam) - xMouse;
4596                                 dy = HIWORD(lParam) - yMouse;
4597
4598                                 if (dx < 0) dx = -dx;
4599                                 if (dy < 0) dy = -dy;
4600
4601                                 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
4602                                 {
4603                                         SendMessage(hWnd, WM_CLOSE, 0, 0);
4604                                 }
4605                         }
4606
4607                         /* Save last location */
4608                         iMouse = 1;
4609                         xMouse = LOWORD(lParam);
4610                         yMouse = HIWORD(lParam);
4611
4612                         return 0;
4613                 }
4614
4615                 case WM_CLOSE:
4616                 {
4617                         DestroyWindow(hwndSaver);
4618                         hwndSaver = NULL;
4619                         return 0;
4620                 }
4621         }
4622
4623         /* Oops */
4624         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4625 }
4626
4627 #endif /* USE_SAVER */
4628
4629
4630
4631
4632
4633 /*** Temporary Hooks ***/
4634
4635
4636 /*
4637  * Display warning message (see "z-util.c")
4638  */
4639 static void hack_plog(cptr str)
4640 {
4641         /* Give a warning */
4642         if (str)
4643         {
4644 #ifdef JP
4645                 MessageBox(NULL, str, "·Ù¹ð¡ª",
4646                            MB_ICONEXCLAMATION | MB_OK);
4647 #else
4648                 MessageBox(NULL, str, "Warning",
4649                            MB_ICONEXCLAMATION | MB_OK);
4650 #endif
4651
4652         }
4653 }
4654
4655
4656 /*
4657  * Display error message and quit (see "z-util.c")
4658  */
4659 static void hack_quit(cptr str)
4660 {
4661         /* Give a warning */
4662         if (str)
4663         {
4664 #ifdef JP
4665                 MessageBox(NULL, str, "¥¨¥é¡¼¡ª",
4666                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
4667 #else
4668                 MessageBox(NULL, str, "Error",
4669                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
4670 #endif
4671
4672         }
4673
4674         /* Unregister the classes */
4675         UnregisterClass(AppName, hInstance);
4676
4677         /* Destroy the icon */
4678         if (hIcon) DestroyIcon(hIcon);
4679
4680         /* Exit */
4681         exit(0);
4682 }
4683
4684
4685
4686 /*** Various hooks ***/
4687
4688
4689 /*
4690  * Display warning message (see "z-util.c")
4691  */
4692 static void hook_plog(cptr str)
4693 {
4694         /* Warning */
4695         if (str)
4696         {
4697 #ifdef JP
4698                 MessageBox(data[0].w, str, "·Ù¹ð¡ª",
4699                            MB_ICONEXCLAMATION | MB_OK);
4700 #else
4701                 MessageBox(data[0].w, str, "Warning",
4702                            MB_ICONEXCLAMATION | MB_OK);
4703 #endif
4704
4705         }
4706 }
4707
4708
4709 /*
4710  * Display error message and quit (see "z-util.c")
4711  */
4712 static void hook_quit(cptr str)
4713 {
4714         int i;
4715
4716
4717         /* Give a warning */
4718         if (str)
4719         {
4720 #ifdef JP
4721                 MessageBox(data[0].w, str, "¥¨¥é¡¼¡ª",
4722                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
4723 #else
4724                 MessageBox(data[0].w, str, "Error",
4725                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
4726 #endif
4727
4728         }
4729
4730
4731         /* Save the preferences */
4732         save_prefs();
4733
4734
4735         /*** Could use 'Term_nuke_win()' XXX XXX XXX */
4736
4737         /* Destroy all windows */
4738         for (i = MAX_TERM_DATA - 1; i >= 0; --i)
4739         {
4740                 term_force_font(&data[i], NULL);
4741                 if (data[i].font_want) string_free(data[i].font_want);
4742                 if (data[i].w) DestroyWindow(data[i].w);
4743                 data[i].w = 0;
4744         }
4745
4746         /* Free the bitmap stuff */
4747 #ifdef USE_GRAPHICS
4748         if (infGraph.hPalette) DeleteObject(infGraph.hPalette);
4749         if (infGraph.hBitmap) DeleteObject(infGraph.hBitmap);
4750
4751 #ifdef USE_TRANSPARENCY
4752         if (infMask.hPalette) DeleteObject(infMask.hPalette);
4753         if (infMask.hBitmap) DeleteObject(infMask.hBitmap);
4754 #endif /* USE_TRANSPARENCY */
4755
4756 #endif /* USE_GRAPHICS */
4757
4758         /*** Free some other stuff ***/
4759
4760         DeleteObject(hbrYellow);
4761
4762         /* bg */
4763         delete_bg();
4764
4765         if (hPal) DeleteObject(hPal);
4766
4767         UnregisterClass(AppName, hInstance);
4768
4769         if (hIcon) DestroyIcon(hIcon);
4770
4771         exit(0);
4772 }
4773
4774
4775
4776 /*** Initialize ***/
4777
4778
4779 /*
4780  * Init some stuff
4781  */
4782 static void init_stuff(void)
4783 {
4784         int i;
4785
4786         char path[1024];
4787
4788
4789         /* Get program name with full path */
4790         GetModuleFileName(hInstance, path, 512);
4791
4792         /* Save the "program name" XXX XXX XXX */
4793         argv0 = path;
4794
4795         /* Get the name of the "*.ini" file */
4796         strcpy(path + strlen(path) - 4, ".INI");
4797
4798         /* Save the the name of the ini-file */
4799         ini_file = string_make(path);
4800
4801         /* Analyze the path */
4802         i = strlen(path);
4803
4804         /* Get the path */
4805         for (; i > 0; i--)
4806         {
4807                 if (path[i] == '\\')
4808                 {
4809                         /* End of path */
4810                         break;
4811                 }
4812         }
4813
4814         /* Add "lib" to the path */
4815         strcpy(path + i + 1, "lib\\");
4816
4817         /* Validate the path */
4818         validate_dir(path, TRUE);
4819
4820         /* Init the file paths */
4821         init_file_paths(path);
4822
4823         /* Hack -- Validate the paths */
4824         validate_dir(ANGBAND_DIR_APEX, FALSE);
4825         validate_dir(ANGBAND_DIR_BONE, FALSE);
4826
4827         /* Allow missing 'edit' directory */
4828         if (!check_dir(ANGBAND_DIR_EDIT))
4829         {
4830                 /* Must have 'data'! */
4831                 validate_dir(ANGBAND_DIR_DATA, TRUE);
4832         }
4833         else
4834         {
4835                 /* Don't need 'data' */
4836                 validate_dir(ANGBAND_DIR_DATA, FALSE);
4837         }
4838
4839         validate_dir(ANGBAND_DIR_FILE, TRUE);
4840         validate_dir(ANGBAND_DIR_HELP, FALSE);
4841         validate_dir(ANGBAND_DIR_INFO, FALSE);
4842         validate_dir(ANGBAND_DIR_PREF, TRUE);
4843         validate_dir(ANGBAND_DIR_SAVE, FALSE);
4844         validate_dir(ANGBAND_DIR_USER, TRUE);
4845         validate_dir(ANGBAND_DIR_XTRA, TRUE);
4846
4847         /* Build the filename */
4848 #ifdef JP
4849         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news_j.txt");
4850 #else
4851         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news.txt");
4852 #endif
4853
4854
4855         /* Hack -- Validate the "news.txt" file */
4856         validate_file(path);
4857
4858
4859 #ifndef JP
4860         /* Build the "font" path */
4861         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font");
4862
4863         /* Allocate the path */
4864         ANGBAND_DIR_XTRA_FONT = string_make(path);
4865
4866         /* Validate the "font" directory */
4867         validate_dir(ANGBAND_DIR_XTRA_FONT, TRUE);
4868
4869         /* Build the filename */
4870         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
4871
4872         /* Hack -- Validate the basic font */
4873         validate_file(path);
4874 #endif
4875
4876
4877 #ifdef USE_GRAPHICS
4878
4879         /* Build the "graf" path */
4880         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
4881
4882         /* Allocate the path */
4883         ANGBAND_DIR_XTRA_GRAF = string_make(path);
4884
4885         /* Validate the "graf" directory */
4886         validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
4887
4888 #endif /* USE_GRAPHICS */
4889
4890
4891 #ifdef USE_SOUND
4892
4893         /* Build the "sound" path */
4894         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
4895
4896         /* Allocate the path */
4897         ANGBAND_DIR_XTRA_SOUND = string_make(path);
4898
4899         /* Validate the "sound" directory */
4900         validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
4901
4902 #endif /* USE_SOUND */
4903
4904 #ifdef USE_MUSIC
4905
4906         /* Build the "music" path */
4907         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
4908
4909         /* Allocate the path */
4910         ANGBAND_DIR_XTRA_MUSIC = string_make(path);
4911
4912         /* Validate the "music" directory */
4913         validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
4914
4915 #endif /* USE_MUSIC */
4916
4917         /* Build the "help" path */
4918         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
4919
4920         /* Allocate the path */
4921         ANGBAND_DIR_XTRA_HELP = string_make(path);
4922
4923         /* Validate the "help" directory */
4924         /* validate_dir(ANGBAND_DIR_XTRA_HELP); */
4925 }
4926
4927
4928 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
4929                        LPSTR lpCmdLine, int nCmdShow)
4930 {
4931         int i;
4932
4933         WNDCLASS wc;
4934         HDC hdc;
4935         MSG msg;
4936
4937         /* Save globally */
4938         hInstance = hInst;
4939
4940         /* Initialize */
4941         if (hPrevInst == NULL)
4942         {
4943                 wc.style         = CS_CLASSDC;
4944                 wc.lpfnWndProc   = AngbandWndProc;
4945                 wc.cbClsExtra    = 0;
4946                 wc.cbWndExtra    = 4; /* one long pointer to term_data */
4947                 wc.hInstance     = hInst;
4948                 wc.hIcon         = hIcon = LoadIcon(hInst, AppName);
4949                 wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
4950                 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
4951                 wc.lpszMenuName  = AppName;
4952                 wc.lpszClassName = AppName;
4953
4954                 if (!RegisterClass(&wc)) exit(1);
4955
4956                 wc.lpfnWndProc   = AngbandListProc;
4957                 wc.lpszMenuName  = NULL;
4958                 wc.lpszClassName = AngList;
4959
4960                 if (!RegisterClass(&wc)) exit(2);
4961
4962 #ifdef USE_SAVER
4963
4964                 wc.style          = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
4965                 wc.lpfnWndProc    = AngbandSaverProc;
4966                 wc.hCursor        = NULL;
4967                 wc.lpszMenuName   = NULL;
4968                 wc.lpszClassName  = "WindowsScreenSaverClass";
4969
4970                 if (!RegisterClass(&wc)) exit(3);
4971
4972 #endif
4973
4974         }
4975
4976         /* Temporary hooks */
4977         plog_aux = hack_plog;
4978         quit_aux = hack_quit;
4979         core_aux = hack_quit;
4980
4981         /* Prepare the filepaths */
4982         init_stuff();
4983
4984         /* Initialize the keypress analyzer */
4985         for (i = 0; special_key_list[i]; ++i)
4986         {
4987                 special_key[special_key_list[i]] = TRUE;
4988         }
4989         /* Initialize the keypress analyzer */
4990         for (i = 0; ignore_key_list[i]; ++i)
4991         {
4992                 ignore_key[ignore_key_list[i]] = TRUE;
4993         }
4994
4995         /* Determine if display is 16/256/true color */
4996         hdc = GetDC(NULL);
4997         colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
4998         paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
4999         ReleaseDC(NULL, hdc);
5000
5001         /* Initialize the colors */
5002         for (i = 0; i < 256; i++)
5003         {
5004                 byte rv, gv, bv;
5005
5006                 /* Extract desired values */
5007                 rv = angband_color_table[i][1];
5008                 gv = angband_color_table[i][2];
5009                 bv = angband_color_table[i][3];
5010
5011                 /* Extract the "complex" code */
5012                 win_clr[i] = PALETTERGB(rv, gv, bv);
5013
5014                 /* Save the "simple" code */
5015                 angband_color_table[i][0] = win_pal[i];
5016         }
5017
5018         /* Prepare the windows */
5019         init_windows();
5020
5021         /* bg */
5022         init_bg();
5023
5024         /* Activate hooks */
5025         plog_aux = hook_plog;
5026         quit_aux = hook_quit;
5027         core_aux = hook_quit;
5028
5029         /* Set the system suffix */
5030         ANGBAND_SYS = "win";
5031
5032         /* Set the keyboard suffix */
5033         if (7 != GetKeyboardType(0))
5034                 ANGBAND_KEYBOARD = "0";
5035         else
5036         {
5037                 /* Japanese keyboard */
5038                 switch (GetKeyboardType(1))
5039                 {
5040                 case 0x0D01: case 0x0D02:
5041                 case 0x0D03: case 0x0D04:
5042                 case 0x0D05: case 0x0D06:
5043                         /* NEC PC-98x1 */
5044                         ANGBAND_KEYBOARD = "NEC98";
5045                         break;
5046                 default:
5047                         /* PC/AT */
5048                         ANGBAND_KEYBOARD = "JAPAN";
5049                 }
5050         }
5051
5052
5053         /* Initialize */
5054         init_angband();
5055
5056         /* We are now initialized */
5057         initialized = TRUE;
5058
5059 #ifdef CHUUKEI
5060         if(lpCmdLine[0] == '-'){
5061           switch(lpCmdLine[1])
5062           {
5063           case 'p':
5064           case 'P':
5065             {
5066               if (!lpCmdLine[2]) break;
5067               chuukei_server = TRUE;
5068               if(connect_chuukei_server(&lpCmdLine[2])<0){
5069                 msg_print("connect fail");
5070                 return 0;
5071               }
5072               msg_print("connect");
5073               msg_print(NULL);
5074               break;
5075             }
5076
5077           case 'c':
5078           case 'C':
5079             {
5080               if (!lpCmdLine[2]) break;
5081               chuukei_client = TRUE;
5082               connect_chuukei_server(&lpCmdLine[2]);
5083               play_game(FALSE);
5084               quit(NULL);
5085               return 0;
5086             }
5087           }
5088         }
5089 #endif
5090
5091 #ifdef CHUUKEI
5092         /* Did the user double click on a save file? */
5093         if(!chuukei_server) check_for_save_file(lpCmdLine);
5094 #else
5095         /* Did the user double click on a save file? */
5096         check_for_save_file(lpCmdLine);
5097 #endif
5098
5099         /* Prompt the user */
5100 #ifdef JP
5101         prt("[¥Õ¥¡¥¤¥ë] ¥á¥Ë¥å¡¼¤Î [¿·µ¬] ¤Þ¤¿¤Ï [³«¤¯] ¤òÁªÂò¤·¤Æ¤¯¤À¤µ¤¤¡£", 23, 8);
5102 #else
5103         prt("[Choose 'New' or 'Open' from the 'File' menu]", 23, 17);
5104 #endif
5105
5106         Term_fresh();
5107
5108         /* Process messages forever */
5109         while (GetMessage(&msg, NULL, 0, 0))
5110         {
5111                 TranslateMessage(&msg);
5112                 DispatchMessage(&msg);
5113         }
5114
5115         /* Paranoia */
5116         quit(NULL);
5117
5118         /* Paranoia */
5119         return (0);
5120 }
5121
5122
5123 #endif /* WINDOWS */
5124