OSDN Git Service

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