OSDN Git Service

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