OSDN Git Service

Fix undefined variable type.
[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 #endif /* USE_SOUND */
1772
1773 #ifdef USE_SOUND
1774 /*
1775  * Initialize sound
1776  */
1777 static bool init_sound(void)
1778 {
1779         /* Initialize once */
1780         if (!can_use_sound)
1781         {
1782                 /* Load the prefs */
1783                 load_sound_prefs();
1784
1785                 /* Sound available */
1786                 can_use_sound = TRUE;
1787         }
1788
1789         /* Result */
1790         return (can_use_sound);
1791 }
1792 #endif /* USE_SOUND */
1793
1794
1795 /*
1796  * Resize a window
1797  */
1798 static void term_window_resize(term_data *td)
1799 {
1800         /* Require window */
1801         if (!td->w) return;
1802
1803         /* Resize the window */
1804         SetWindowPos(td->w, 0, 0, 0,
1805                      td->size_wid, td->size_hgt,
1806                      SWP_NOMOVE | SWP_NOZORDER);
1807
1808         /* Redraw later */
1809         InvalidateRect(td->w, NULL, TRUE);
1810 }
1811
1812
1813 /*
1814  * Force the use of a new "font file" for a term_data
1815  *
1816  * This function may be called before the "window" is ready
1817  *
1818  * This function returns zero only if everything succeeds.
1819  *
1820  * Note that the "font name" must be capitalized!!!
1821  */
1822 static errr term_force_font(term_data *td, cptr path)
1823 {
1824         int wid, hgt;
1825
1826 #if 0 /* #ifndef JP */
1827         int i;
1828         char *base;
1829         char buf[1024];
1830 #endif
1831
1832         /* Forget the old font (if needed) */
1833         if (td->font_id) DeleteObject(td->font_id);
1834
1835 #if 1 /* #ifdef JP */
1836         /* Unused */
1837         (void)path;
1838
1839         /* Create the font (using the 'base' of the font file name!) */
1840         td->font_id = CreateFontIndirect(&(td->lf));
1841         wid = td->lf.lfWidth;
1842         hgt = td->lf.lfHeight;
1843         if (!td->font_id) return (1);
1844 #else
1845         /* Forget old font */
1846         if (td->font_file)
1847         {
1848                 bool used = FALSE;
1849
1850                 /* Scan windows */
1851                 for (i = 0; i < MAX_TERM_DATA; i++)
1852                 {
1853                         /* Don't check when closing the application */
1854                         if (!path) break;
1855
1856                         /* Check "screen" */
1857                         if ((td != &data[i]) &&
1858                             (data[i].font_file) &&
1859                             (streq(data[i].font_file, td->font_file)))
1860                         {
1861                                 used = TRUE;
1862                         }
1863                 }
1864
1865                 /* Remove unused font resources */
1866                 if (!used) RemoveFontResource(td->font_file);
1867
1868                 /* Free the old name */
1869                 string_free(td->font_file);
1870
1871                 /* Forget it */
1872                 td->font_file = NULL;
1873         }
1874
1875         /* No path given */
1876         if (!path) return (1);
1877
1878         /* Local copy */
1879         strcpy(buf, path);
1880
1881         /* Analyze font path */
1882         base = analyze_font(buf, &wid, &hgt);
1883
1884         /* Verify suffix */
1885         if (!suffix(base, ".FON")) return (1);
1886
1887         /* Verify file */
1888         if (!check_file(buf)) return (1);
1889
1890         /* Load the new font */
1891         if (!AddFontResource(buf)) return (1);
1892
1893         /* Save new font name */
1894         td->font_file = string_make(base);
1895
1896         /* Remove the "suffix" */
1897         base[strlen(base)-4] = '\0';
1898
1899         /* Create the font (using the 'base' of the font file name!) */
1900         td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0,
1901                                  ANSI_CHARSET, OUT_DEFAULT_PRECIS,
1902                                  CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1903                                  FIXED_PITCH | FF_DONTCARE, base);
1904 #endif
1905
1906         /* Hack -- Unknown size */
1907         if (!wid || !hgt)
1908         {
1909                 HDC hdcDesktop;
1910                 HFONT hfOld;
1911                 TEXTMETRIC tm;
1912
1913                 /* all this trouble to get the cell size */
1914                 hdcDesktop = GetDC(HWND_DESKTOP);
1915                 hfOld = SelectObject(hdcDesktop, td->font_id);
1916                 GetTextMetrics(hdcDesktop, &tm);
1917                 SelectObject(hdcDesktop, hfOld);
1918                 ReleaseDC(HWND_DESKTOP, hdcDesktop);
1919
1920                 /* Font size info */
1921                 wid = tm.tmAveCharWidth;
1922                 hgt = tm.tmHeight;
1923         }
1924
1925         /* Save the size info */
1926         td->font_wid = wid;
1927         td->font_hgt = hgt;
1928
1929         /* Success */
1930         return (0);
1931 }
1932
1933
1934
1935 /*
1936  * Allow the user to change the font for this window.
1937  */
1938 static void term_change_font(term_data *td)
1939 {
1940 #if 1 /* #ifdef JP */
1941         CHOOSEFONT cf;
1942
1943         memset(&cf, 0, sizeof(cf));
1944         cf.lStructSize = sizeof(cf);
1945     cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_NOVERTFONTS | CF_INITTOLOGFONTSTRUCT;
1946     cf.lpLogFont = &(td->lf);
1947
1948         if (ChooseFont(&cf))
1949         {
1950                 /* Force the font */
1951                 term_force_font(td, NULL);
1952
1953                 /* Assume not bizarre */
1954                 td->bizarre = TRUE;
1955
1956                 /* Reset the tile info */
1957                 td->tile_wid = td->font_wid;
1958                 td->tile_hgt = td->font_hgt;
1959
1960                 /* Analyze the font */
1961                 term_getsize(td);
1962
1963                 /* Resize the window */
1964                 term_window_resize(td);
1965         }
1966
1967 #else
1968         OPENFILENAME ofn;
1969
1970         char tmp[1024] = "";
1971
1972         /* Extract a default if possible */
1973         if (td->font_file) strcpy(tmp, td->font_file);
1974
1975         /* Ask for a choice */
1976         memset(&ofn, 0, sizeof(ofn));
1977         ofn.lStructSize = sizeof(ofn);
1978         ofn.hwndOwner = data[0].w;
1979         ofn.lpstrFilter = "Angband Font Files (*.fon)\0*.fon\0";
1980         ofn.nFilterIndex = 1;
1981         ofn.lpstrFile = tmp;
1982         ofn.nMaxFile = 128;
1983         ofn.lpstrInitialDir = ANGBAND_DIR_XTRA_FONT;
1984         ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
1985         ofn.lpstrDefExt = "fon";
1986
1987         /* Force choice if legal */
1988         if (GetOpenFileName(&ofn))
1989         {
1990                 /* Force the font */
1991                 if (term_force_font(td, tmp))
1992                 {
1993                         /* Access the standard font file */
1994                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
1995
1996                         /* Force the use of that font */
1997                         (void)term_force_font(td, tmp);
1998                 }
1999
2000                 /* Assume not bizarre */
2001                 td->bizarre = FALSE;
2002
2003                 /* Reset the tile info */
2004                 td->tile_wid = td->font_wid;
2005                 td->tile_hgt = td->font_hgt;
2006
2007                 /* Analyze the font */
2008                 term_getsize(td);
2009
2010                 /* Resize the window */
2011                 term_window_resize(td);
2012         }
2013 #endif
2014
2015 }
2016
2017 /*
2018  * Allow the user to lock this window.
2019  */
2020 static void term_window_pos(term_data *td, HWND hWnd)
2021 {
2022         SetWindowPos(td->w, hWnd, 0, 0, 0, 0,
2023                         SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
2024 }
2025
2026 static void windows_map(void);
2027
2028 /*
2029  * Hack -- redraw a term_data
2030  */
2031 static void term_data_redraw(term_data *td)
2032 {
2033         if (td->map_active)
2034         {
2035                 /* Redraw the map */
2036                 windows_map();
2037         }
2038         else
2039         {
2040                 /* Activate the term */
2041                 Term_activate(&td->t);
2042
2043                 /* Redraw the contents */
2044                 Term_redraw();
2045
2046                 /* Restore the term */
2047                 Term_activate(term_screen);
2048         }
2049 }
2050
2051
2052 void Term_inversed_area(HWND hWnd, int x, int y, int w, int h)
2053 {
2054         HDC hdc;
2055         HPEN oldPen;
2056         HBRUSH myBrush, oldBrush;
2057
2058         term_data *td = (term_data *)GetWindowLong(hWnd, 0);
2059         int tx = td->size_ow1 + x * td->tile_wid;
2060         int ty = td->size_oh1 + y * td->tile_hgt;
2061         int tw = w * td->tile_wid - 1;
2062         int th = h * td->tile_hgt - 1;
2063
2064         hdc = GetDC(hWnd);
2065         myBrush = CreateSolidBrush(RGB(255, 255, 255));
2066         oldBrush = SelectObject(hdc, myBrush);
2067         oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2068
2069         PatBlt(hdc, tx, ty, tw, th, PATINVERT);
2070
2071         SelectObject(hdc, oldBrush);
2072         SelectObject(hdc, oldPen);
2073 }
2074
2075
2076
2077 /*** Function hooks needed by "Term" ***/
2078
2079
2080 #if 0
2081
2082 /*
2083  * Initialize a new Term
2084  */
2085 static void Term_init_win(term *t)
2086 {
2087         /* XXX Unused */
2088 }
2089
2090
2091 /*
2092  * Nuke an old Term
2093  */
2094 static void Term_nuke_win(term *t)
2095 {
2096         /* XXX Unused */
2097 }
2098
2099 #endif
2100
2101
2102 /*
2103  * Interact with the User
2104  */
2105 static errr Term_user_win(int n)
2106 {
2107         /* Unused */
2108         (void)n;
2109
2110         /* Success */
2111         return (0);
2112 }
2113
2114
2115 /*
2116  * React to global changes
2117  */
2118 static errr Term_xtra_win_react(void)
2119 {
2120         int i;
2121
2122
2123         /* Simple color */
2124         if (colors16)
2125         {
2126                 /* Save the default colors */
2127                 for (i = 0; i < 256; i++)
2128                 {
2129                         /* Simply accept the desired colors */
2130                         win_pal[i] = angband_color_table[i][0];
2131                 }
2132         }
2133
2134         /* Complex color */
2135         else
2136         {
2137                 COLORREF code;
2138
2139                 byte rv, gv, bv;
2140
2141                 bool change = FALSE;
2142
2143                 /* Save the default colors */
2144                 for (i = 0; i < 256; i++)
2145                 {
2146                         /* Extract desired values */
2147                         rv = angband_color_table[i][1];
2148                         gv = angband_color_table[i][2];
2149                         bv = angband_color_table[i][3];
2150
2151                         /* Extract a full color code */
2152                         code = PALETTERGB(rv, gv, bv);
2153
2154                         /* Activate changes */
2155                         if (win_clr[i] != code)
2156                         {
2157                                 /* Note the change */
2158                                 change = TRUE;
2159
2160                                 /* Apply the desired color */
2161                                 win_clr[i] = code;
2162                         }
2163                 }
2164
2165                 /* Activate the palette if needed */
2166                 if (change) (void)new_palette();
2167         }
2168
2169
2170 #ifdef USE_SOUND
2171
2172         /* Handle "arg_sound" */
2173         if (use_sound != arg_sound)
2174         {
2175                 /* Initialize (if needed) */
2176                 if (arg_sound && !init_sound())
2177                 {
2178                         /* Warning */
2179 #ifdef JP
2180                         plog("¥µ¥¦¥ó¥É¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó¡ª");
2181 #else
2182                         plog("Cannot initialize sound!");
2183 #endif
2184
2185
2186                         /* Cannot enable */
2187                         arg_sound = FALSE;
2188                 }
2189
2190                 /* Change setting */
2191                 use_sound = arg_sound;
2192         }
2193
2194 #endif
2195
2196 #ifdef USE_MUSIC
2197
2198         /* Handle "arg_sound" */
2199         if (use_music != arg_music)
2200         {
2201                 /* Initialize (if needed) */
2202                 if (arg_music && !init_music())
2203                 {
2204                         /* Warning */
2205 #ifdef JP
2206                         plog("BGM¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó¡ª");
2207 #else
2208                         plog("Cannot initialize BGM!");
2209 #endif
2210                         /* Cannot enable */
2211                         arg_music = FALSE;
2212                 }
2213
2214                 /* Change setting */
2215                 use_music = arg_music;
2216         }
2217
2218 #endif
2219
2220
2221 #ifdef USE_GRAPHICS
2222
2223         /* Handle "arg_graphics" */
2224         if (use_graphics != arg_graphics)
2225         {
2226                 /* Initialize (if needed) */
2227                 if (arg_graphics && !init_graphics())
2228                 {
2229                         /* Warning */
2230 #ifdef JP
2231                         plog("¥°¥é¥Õ¥£¥Ã¥¯¥¹¤ò½é´ü²½¤Ç¤­¤Þ¤»¤ó!");
2232 #else
2233                         plog("Cannot initialize graphics!");
2234 #endif
2235
2236
2237                         /* Cannot enable */
2238                         arg_graphics = GRAPHICS_NONE;
2239                 }
2240
2241                 /* Change setting */
2242                 use_graphics = arg_graphics;
2243
2244                 /* Reset visuals */
2245 #ifdef ANGBAND_2_8_1
2246                 reset_visuals();
2247 #else /* ANGBAND_2_8_1 */
2248                 reset_visuals(TRUE);
2249 #endif /* ANGBAND_2_8_1 */
2250         }
2251
2252 #endif /* USE_GRAPHICS */
2253
2254
2255         /* Clean up windows */
2256         for (i = 0; i < MAX_TERM_DATA; i++)
2257         {
2258                 term *old = Term;
2259
2260                 term_data *td = &data[i];
2261
2262                 /* Update resized windows */
2263                 if ((td->cols != (uint)td->t.wid) || (td->rows != (uint)td->t.hgt))
2264                 {
2265                         /* Activate */
2266                         Term_activate(&td->t);
2267
2268                         /* Hack -- Resize the term */
2269                         Term_resize(td->cols, td->rows);
2270
2271                         /* Redraw the contents */
2272                         Term_redraw();
2273
2274                         /* Restore */
2275                         Term_activate(old);
2276                 }
2277         }
2278
2279
2280         /* Success */
2281         return (0);
2282 }
2283
2284
2285 /*
2286  * Process at least one event
2287  */
2288 static errr Term_xtra_win_event(int v)
2289 {
2290         MSG msg;
2291
2292         /* Wait for an event */
2293         if (v)
2294         {
2295                 /* Block */
2296                 if (GetMessage(&msg, NULL, 0, 0))
2297                 {
2298                         TranslateMessage(&msg);
2299                         DispatchMessage(&msg);
2300                 }
2301         }
2302
2303         /* Check for an event */
2304         else
2305         {
2306                 /* Check */
2307                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2308                 {
2309                         TranslateMessage(&msg);
2310                         DispatchMessage(&msg);
2311                 }
2312         }
2313
2314         /* Success */
2315         return 0;
2316 }
2317
2318
2319 /*
2320  * Process all pending events
2321  */
2322 static errr Term_xtra_win_flush(void)
2323 {
2324         MSG msg;
2325
2326         /* Process all pending events */
2327         while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2328         {
2329                 TranslateMessage(&msg);
2330                 DispatchMessage(&msg);
2331         }
2332
2333         /* Success */
2334         return (0);
2335 }
2336
2337
2338 /*
2339  * Hack -- clear the screen
2340  *
2341  * Make this more efficient XXX XXX XXX
2342  */
2343 static errr Term_xtra_win_clear(void)
2344 {
2345         term_data *td = (term_data*)(Term->data);
2346
2347         HDC hdc;
2348         RECT rc;
2349
2350         /* Rectangle to erase */
2351         rc.left = td->size_ow1;
2352         rc.right = rc.left + td->cols * td->tile_wid;
2353         rc.top = td->size_oh1;
2354         rc.bottom = rc.top + td->rows * td->tile_hgt;
2355
2356         /* Erase it */
2357         hdc = GetDC(td->w);
2358         SetBkColor(hdc, RGB(0, 0, 0));
2359         SelectObject(hdc, td->font_id);
2360         ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2361
2362         /* bg */
2363         if (use_bg)
2364         {
2365                 rc.left = 0; rc.top = 0;
2366                 DrawBG(hdc, &rc);
2367         }
2368         ReleaseDC(td->w, hdc);
2369
2370         /* Success */
2371         return 0;
2372 }
2373
2374
2375 /*
2376  * Hack -- make a noise
2377  */
2378 static errr Term_xtra_win_noise(void)
2379 {
2380         MessageBeep(MB_ICONASTERISK);
2381         return (0);
2382 }
2383
2384
2385 /*
2386  * Hack -- make a sound
2387  */
2388 static errr Term_xtra_win_sound(int v)
2389 {
2390 #ifdef USE_SOUND
2391         int i;
2392         char buf[1024];
2393 #endif /* USE_SOUND */
2394
2395         /* Sound disabled */
2396         if (!use_sound) return (1);
2397
2398         /* Illegal sound */
2399         if ((v < 0) || (v >= SOUND_MAX)) return (1);
2400
2401 #ifdef USE_SOUND
2402
2403         /* Count the samples */
2404         for (i = 0; i < SAMPLE_MAX; i++)
2405         {
2406                 if (!sound_file[v][i])
2407                         break;
2408         }
2409
2410         /* No sample */
2411         if (i == 0) return (1);
2412
2413         /* Build the path */
2414         path_build(buf, 1024, ANGBAND_DIR_XTRA_SOUND, sound_file[v][Rand_external(i)]);
2415
2416 #ifdef WIN32
2417
2418         /* Play the sound, catch errors */
2419         return (PlaySound(buf, 0, SND_FILENAME | SND_ASYNC));
2420
2421 #else /* WIN32 */
2422
2423         /* Play the sound, catch errors */
2424         return (sndPlaySound(buf, SND_ASYNC));
2425
2426 #endif /* WIN32 */
2427
2428 #else /* USE_SOUND */
2429
2430         /* Oops */
2431         return (1);
2432
2433 #endif /* USE_SOUND */
2434 }
2435
2436 /*
2437  * Hack -- play a music
2438  */
2439 static errr Term_xtra_win_music(int v)
2440 {
2441 #ifdef USE_MUSIC
2442         int i;
2443         char buf[1024];
2444 #endif /* USE_MUSIC */
2445
2446         /* Sound disabled */
2447         if (!use_music) return (1);
2448
2449         /* Illegal sound */
2450         if ((v < 0) || (v >= MUSIC_BASIC_MAX)) return (1);
2451
2452 #ifdef USE_MUSIC
2453
2454         /* Count the samples */
2455         for (i = 0; i < SAMPLE_MAX; i++)
2456         {
2457                 if (!music_file[v][i]) break;
2458         }
2459
2460         /* No sample */
2461         if (i == 0)
2462         {
2463                 mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2464                 mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2465                 return (1);
2466         }
2467
2468         /* Build the path */
2469         path_build(buf, 1024, ANGBAND_DIR_XTRA_MUSIC, music_file[v][Rand_external(i)]);
2470
2471 #endif /* USE_MUSIC */
2472
2473 #ifdef WIN32
2474
2475         mop.lpstrDeviceType = mci_device_type;  
2476         mop.lpstrElementName = buf;
2477         mciSendCommand(mop.wDeviceID, MCI_STOP, 0, 0);
2478         mciSendCommand(mop.wDeviceID, MCI_CLOSE, 0, 0);
2479         mciSendCommand(mop.wDeviceID, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mop);
2480         mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
2481         mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
2482         return (0);
2483
2484 #else /* USE_MUSIC */
2485
2486         /* Oops */
2487         return (1);
2488
2489 #endif /* USE_MUSIC */
2490 }
2491
2492
2493 /*
2494  * Delay for "x" milliseconds
2495  */
2496 static int Term_xtra_win_delay(int v)
2497 {
2498
2499 #ifdef WIN32
2500
2501         /* Sleep */
2502         Sleep(v);
2503
2504 #else /* WIN32 */
2505
2506         DWORD t;
2507         MSG msg;
2508
2509         /* Final count */
2510         t = GetTickCount() + v;
2511
2512         /* Wait for it */
2513         while (GetTickCount() < t)
2514         {
2515                 /* Handle messages */
2516                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
2517                 {
2518                         TranslateMessage(&msg);
2519                         DispatchMessage(&msg);
2520                 }
2521         }
2522
2523 #endif /* WIN32 */
2524
2525         /* Success */
2526         return (0);
2527 }
2528
2529
2530 /*
2531  * Do a "special thing"
2532  */
2533 static errr Term_xtra_win(int n, int v)
2534 {
2535         /* Handle a subset of the legal requests */
2536         switch (n)
2537         {
2538                 /* Make a bell sound */
2539                 case TERM_XTRA_NOISE:
2540                 {
2541                         return (Term_xtra_win_noise());
2542                 }
2543
2544                 /* Play a music */
2545                 case TERM_XTRA_MUSIC_BASIC:
2546                 {
2547                         return (Term_xtra_win_music(v));
2548                 }
2549
2550                 /* Make a special sound */
2551                 case TERM_XTRA_SOUND:
2552                 {
2553                         return (Term_xtra_win_sound(v));
2554                 }
2555
2556                 /* Process random events */
2557                 case TERM_XTRA_BORED:
2558                 {
2559                         return (Term_xtra_win_event(0));
2560                 }
2561
2562                 /* Process an event */
2563                 case TERM_XTRA_EVENT:
2564                 {
2565                         return (Term_xtra_win_event(v));
2566                 }
2567
2568                 /* Flush all events */
2569                 case TERM_XTRA_FLUSH:
2570                 {
2571                         return (Term_xtra_win_flush());
2572                 }
2573
2574                 /* Clear the screen */
2575                 case TERM_XTRA_CLEAR:
2576                 {
2577                         return (Term_xtra_win_clear());
2578                 }
2579
2580                 /* React to global changes */
2581                 case TERM_XTRA_REACT:
2582                 {
2583                         return (Term_xtra_win_react());
2584                 }
2585
2586                 /* Delay for some milliseconds */
2587                 case TERM_XTRA_DELAY:
2588                 {
2589                         return (Term_xtra_win_delay(v));
2590                 }
2591         }
2592
2593         /* Oops */
2594         return 1;
2595 }
2596
2597
2598
2599 /*
2600  * Low level graphics (Assumes valid input).
2601  *
2602  * Draw a "cursor" at (x,y), using a "yellow box".
2603  */
2604 static errr Term_curs_win(int x, int y)
2605 {
2606         term_data *td = (term_data*)(Term->data);
2607
2608         RECT rc;
2609         HDC hdc;
2610
2611         int tile_wid, tile_hgt;
2612
2613         if (td->map_active)
2614         {
2615                 tile_wid = td->map_tile_wid;
2616                 tile_hgt = td->map_tile_hgt;
2617         }
2618         else
2619         {
2620                 tile_wid = td->tile_wid;
2621                 tile_hgt = td->tile_hgt;
2622         }
2623
2624         /* Frame the grid */
2625         rc.left = x * tile_wid + td->size_ow1;
2626         rc.right = rc.left + tile_wid;
2627         rc.top = y * tile_hgt + td->size_oh1;
2628         rc.bottom = rc.top + tile_hgt;
2629
2630         /* Cursor is done as a yellow "box" */
2631         hdc = GetDC(td->w);
2632         FrameRect(hdc, &rc, hbrYellow);
2633         ReleaseDC(td->w, hdc);
2634
2635         /* Success */
2636         return 0;
2637 }
2638
2639
2640 /*
2641  * Low level graphics (Assumes valid input).
2642  *
2643  * Draw a "big cursor" at (x,y), using a "yellow box".
2644  */
2645 static errr Term_bigcurs_win(int x, int y)
2646 {
2647         term_data *td = (term_data*)(Term->data);
2648
2649         RECT rc;
2650         HDC hdc;
2651
2652         int tile_wid, tile_hgt;
2653
2654         if (td->map_active)
2655         {
2656                 /* Normal cursor in map window */
2657                 Term_curs_win(x, y);
2658                 return 0;
2659         }
2660         else
2661         {
2662                 tile_wid = td->tile_wid;
2663                 tile_hgt = td->tile_hgt;
2664         }
2665
2666         /* Frame the grid */
2667         rc.left = x * tile_wid + td->size_ow1;
2668         rc.right = rc.left + 2 * tile_wid;
2669         rc.top = y * tile_hgt + td->size_oh1;
2670         rc.bottom = rc.top + tile_hgt;
2671
2672         /* Cursor is done as a yellow "box" */
2673         hdc = GetDC(td->w);
2674         FrameRect(hdc, &rc, hbrYellow);
2675         ReleaseDC(td->w, hdc);
2676
2677         /* Success */
2678         return 0;
2679 }
2680
2681
2682 /*
2683  * Low level graphics (Assumes valid input).
2684  *
2685  * Erase a "block" of "n" characters starting at (x,y).
2686  */
2687 static errr Term_wipe_win(int x, int y, int n)
2688 {
2689         term_data *td = (term_data*)(Term->data);
2690
2691         HDC hdc;
2692         RECT rc;
2693
2694         /* Rectangle to erase in client coords */
2695         rc.left = x * td->tile_wid + td->size_ow1;
2696         rc.right = rc.left + n * td->tile_wid;
2697         rc.top = y * td->tile_hgt + td->size_oh1;
2698         rc.bottom = rc.top + td->tile_hgt;
2699
2700         hdc = GetDC(td->w);
2701         SetBkColor(hdc, RGB(0, 0, 0));
2702         SelectObject(hdc, td->font_id);
2703         /* bg */
2704         if (use_bg)
2705                 DrawBG(hdc, &rc);
2706         else
2707                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2708         ReleaseDC(td->w, hdc);
2709
2710         /* Success */
2711         return 0;
2712 }
2713
2714
2715 /*
2716  * Low level graphics.  Assumes valid input.
2717  *
2718  * Draw several ("n") chars, with an attr, at a given location.
2719  *
2720  * All "graphic" data is handled by "Term_pict_win()", below.
2721  *
2722  * One would think there is a more efficient method for telling a window
2723  * what color it should be using to draw with, but perhaps simply changing
2724  * it every time is not too inefficient.  XXX XXX XXX
2725  */
2726 static errr Term_text_win(int x, int y, int n, byte a, const char *s)
2727 {
2728         term_data *td = (term_data*)(Term->data);
2729         RECT rc;
2730         HDC hdc;
2731
2732 #if 1 /* #ifdef JP */
2733         static HBITMAP  WALL;
2734         static HBRUSH   myBrush, oldBrush;
2735         static HPEN     oldPen;
2736         static bool init_done = FALSE;
2737
2738         if (!init_done){
2739                 WALL = LoadBitmap(hInstance, AppName);
2740                 myBrush = CreatePatternBrush(WALL);
2741                 init_done = TRUE;
2742         }
2743 #endif
2744
2745         /* Total rectangle */
2746         rc.left = x * td->tile_wid + td->size_ow1;
2747         rc.right = rc.left + n * td->tile_wid;
2748         rc.top = y * td->tile_hgt + td->size_oh1;
2749         rc.bottom = rc.top + td->tile_hgt;
2750
2751         /* Acquire DC */
2752         hdc = GetDC(td->w);
2753
2754         /* Background color */
2755         SetBkColor(hdc, RGB(0, 0, 0));
2756
2757         /* Foreground color */
2758         if (colors16)
2759         {
2760                 SetTextColor(hdc, PALETTEINDEX(win_pal[a]));
2761         }
2762         else if (paletted)
2763         {
2764                 SetTextColor(hdc, win_clr[a&0x0F]);
2765         }
2766         else
2767         {
2768                 SetTextColor(hdc, win_clr[a]);
2769         }
2770
2771         /* Use the font */
2772         SelectObject(hdc, td->font_id);
2773         
2774         /* bg */
2775         if (use_bg) SetBkMode(hdc, TRANSPARENT);
2776
2777         /* Bizarre size */
2778         if (td->bizarre ||
2779             (td->tile_hgt != td->font_hgt) ||
2780             (td->tile_wid != td->font_wid))
2781         {
2782                 int i;
2783
2784                 /* Erase complete rectangle */
2785                 ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
2786                 
2787                 /* bg */
2788                 if (use_bg) DrawBG(hdc, &rc);
2789
2790                 /* New rectangle */
2791                 rc.left += ((td->tile_wid - td->font_wid) / 2);
2792                 rc.right = rc.left + td->font_wid;
2793                 rc.top += ((td->tile_hgt - td->font_hgt) / 2);
2794                 rc.bottom = rc.top + td->font_hgt;
2795
2796                 /* Dump each character */
2797                 for (i = 0; i < n; i++)
2798                 {
2799 #ifdef JP
2800                         if (use_bigtile && *(s+i)=="¢£"[0] && *(s+i+1)=="¢£"[1])
2801                         {
2802                                 rc.right += td->font_wid;
2803
2804                                 oldBrush = SelectObject(hdc, myBrush);
2805                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2806
2807                                 /* Dump the wall */
2808                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2809
2810                                 SelectObject(hdc, oldBrush);
2811                                 SelectObject(hdc, oldPen);
2812                                 rc.right -= td->font_wid;
2813
2814                                 /* Advance */
2815                                 i++;
2816                                 rc.left += 2 * td->tile_wid;
2817                                 rc.right += 2 * td->tile_wid;
2818                         }
2819                         else if ( iskanji(*(s+i)) )  /*  £²¥Ð¥¤¥Èʸ»ú  */
2820                         {
2821                                 rc.right += td->font_wid;
2822                                 /* Dump the text */
2823                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2824                                        s+i, 2, NULL);
2825                                 rc.right -= td->font_wid;
2826
2827                                 /* Advance */
2828                                 i++;
2829                                 rc.left += 2 * td->tile_wid;
2830                                 rc.right += 2 * td->tile_wid;
2831                         } else if (*(s+i)==127){
2832                                 oldBrush = SelectObject(hdc, myBrush);
2833                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2834
2835                                 /* Dump the wall */
2836                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2837
2838                                 SelectObject(hdc, oldBrush);
2839                                 SelectObject(hdc, oldPen);
2840
2841                                 /* Advance */
2842                                 rc.left += td->tile_wid;
2843                                 rc.right += td->tile_wid;
2844                         } else {
2845                                 /* Dump the text */
2846                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc, s+i, 1, NULL);
2847
2848                                 /* Advance */
2849                                 rc.left += td->tile_wid;
2850                                 rc.right += td->tile_wid;
2851                         }
2852 #else
2853 #if 1
2854                         if (*(s+i)==127){
2855                                 oldBrush = SelectObject(hdc, myBrush);
2856                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2857
2858                                 /* Dump the wall */
2859                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2860
2861                                 SelectObject(hdc, oldBrush);
2862                                 SelectObject(hdc, oldPen);
2863
2864                                 /* Advance */
2865                                 rc.left += td->tile_wid;
2866                                 rc.right += td->tile_wid;
2867                         } else {
2868                                 /* Dump the text */
2869                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2870                                        s+i, 1, NULL);
2871
2872                                 /* Advance */
2873                                 rc.left += td->tile_wid;
2874                                 rc.right += td->tile_wid;
2875                         }
2876 #else
2877                         /* Dump the text */
2878                         ExtTextOut(hdc, rc.left, rc.top, 0, &rc,
2879                                    s+i, 1, NULL);
2880
2881                         /* Advance */
2882                         rc.left += td->tile_wid;
2883                         rc.right += td->tile_wid;
2884 #endif
2885 #endif
2886
2887                 }
2888         }
2889
2890         /* Normal size */
2891         else
2892         {
2893                 /* Dump the text */
2894                 ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
2895                            s, n, NULL);
2896         }
2897
2898         /* Release DC */
2899         ReleaseDC(td->w, hdc);
2900
2901         /* Success */
2902         return 0;
2903 }
2904
2905
2906 /*
2907  * Low level graphics.  Assumes valid input.
2908  *
2909  * Draw an array of "special" attr/char pairs at the given location.
2910  *
2911  * We use the "Term_pict_win()" function for "graphic" data, which are
2912  * encoded by setting the "high-bits" of both the "attr" and the "char"
2913  * data.  We use the "attr" to represent the "row" of the main bitmap,
2914  * and the "char" to represent the "col" of the main bitmap.  The use
2915  * of this function is induced by the "higher_pict" flag.
2916  *
2917  * If "graphics" is not available, we simply "wipe" the given grids.
2918  */
2919 static errr Term_pict_win(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
2920 {
2921         term_data *td = (term_data*)(Term->data);
2922
2923 #ifdef USE_GRAPHICS
2924
2925         int i;
2926         int x1, y1, w1, h1;
2927         int x2, y2, w2, h2, tw2;
2928         int x3, y3;
2929
2930         HDC hdcMask;
2931
2932         HDC hdc;
2933         HDC hdcSrc;
2934         HBITMAP hbmSrcOld;
2935
2936         /* Paranoia */
2937         if (!use_graphics)
2938         {
2939                 /* Erase the grids */
2940                 return (Term_wipe_win(x, y, n));
2941         }
2942
2943         /* Size of bitmap cell */
2944         w1 = infGraph.CellWidth;
2945         h1 = infGraph.CellHeight;
2946
2947         /* Size of window cell */
2948         if (td->map_active)
2949         {
2950                 w2 = td->map_tile_wid;
2951                 h2 = td->map_tile_hgt;
2952         }
2953         else
2954         {
2955                 w2 = td->tile_wid;
2956                 h2 = td->tile_hgt;
2957                 tw2 = w2;
2958
2959                 /* big tile mode */
2960                 if (use_bigtile) tw2 *= 2;
2961         }
2962
2963         /* Location of window cell */
2964         x2 = x * w2 + td->size_ow1;
2965         y2 = y * h2 + td->size_oh1;
2966
2967         /* Info */
2968         hdc = GetDC(td->w);
2969
2970         /* More info */
2971         hdcSrc = CreateCompatibleDC(hdc);
2972         hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
2973
2974         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
2975         {
2976                 hdcMask = CreateCompatibleDC(hdc);
2977                 SelectObject(hdcMask, infMask.hBitmap);
2978         }
2979
2980         /* Draw attr/char pairs */
2981         for (i = 0; i < n; i++, x2 += w2)
2982         {
2983                 byte a = ap[i];
2984                 char c = cp[i];
2985
2986                 /* Extract picture */
2987                 int row = (a & 0x7F);
2988                 int col = (c & 0x7F);
2989
2990                 /* Location of bitmap cell */
2991                 x1 = col * w1;
2992                 y1 = row * h1;
2993
2994                 if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
2995                 {
2996                         x3 = (tcp[i] & 0x7F) * w1;
2997                         y3 = (tap[i] & 0x7F) * h1;
2998
2999                         /* Perfect size */
3000                         if ((w1 == tw2) && (h1 == h2))
3001                         {
3002                                 /* Copy the terrain picture from the bitmap to the window */
3003                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
3004
3005                                 /* Mask out the tile */
3006                                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
3007
3008                                 /* Draw the tile */
3009                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
3010                         }
3011
3012                         /* Need to stretch */
3013                         else
3014                         {
3015                                 /* Set the correct mode for stretching the tiles */
3016                                 SetStretchBltMode(hdc, COLORONCOLOR);
3017
3018                                 /* Copy the terrain picture from the bitmap to the window */
3019                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCCOPY);
3020
3021                                 /* Only draw if terrain and overlay are different */
3022                                 if ((x1 != x3) || (y1 != y3))
3023                                 {
3024                                         /* Mask out the tile */
3025                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
3026
3027                                         /* Draw the tile */
3028                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
3029                                 }
3030                         }
3031                 }
3032                 else
3033                 {
3034                         /* Perfect size */
3035                         if ((w1 == tw2) && (h1 == h2))
3036                         {
3037                                 /* Copy the picture from the bitmap to the window */
3038                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
3039                         }
3040
3041                         /* Need to stretch */
3042                         else
3043                         {
3044                                 /* Set the correct mode for stretching the tiles */
3045                                 SetStretchBltMode(hdc, COLORONCOLOR);
3046
3047                                 /* Copy the picture from the bitmap to the window */
3048                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
3049                         }
3050                 }
3051         }
3052
3053         /* Release */
3054         SelectObject(hdcSrc, hbmSrcOld);
3055         DeleteDC(hdcSrc);
3056
3057         if (arg_graphics == GRAPHICS_ADAM_BOLT || arg_graphics == GRAPHICS_HENGBAND)
3058         {
3059                 /* Release */
3060                 SelectObject(hdcMask, hbmSrcOld);
3061                 DeleteDC(hdcMask);
3062         }
3063
3064         /* Release */
3065         ReleaseDC(td->w, hdc);
3066
3067 #else /* USE_GRAPHICS */
3068
3069         /* Just erase this grid */
3070         return (Term_wipe_win(x, y, n));
3071
3072 #endif /* USE_GRAPHICS */
3073
3074         /* Success */
3075         return 0;
3076 }
3077
3078
3079 static void windows_map(void)
3080 {
3081         term_data *td = &data[0];
3082         byte a, c;
3083         int x, min_x, max_x;
3084         int y, min_y, max_y;
3085
3086         byte ta, tc;
3087
3088         /* Only in graphics mode */
3089         if (!use_graphics) return;
3090
3091         /* Clear screen */
3092         Term_xtra_win_clear();
3093
3094         td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
3095         td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
3096         td->map_active = TRUE;
3097
3098         {
3099                 min_x = 0;
3100                 min_y = 0;
3101                 max_x = cur_wid;
3102                 max_y = cur_hgt;
3103         }
3104
3105         /* Draw the map */
3106         for (x = min_x; x < max_x; x++)
3107         {
3108                 for (y = min_y; y < max_y; y++)
3109                 {
3110                         map_info(y, x, &a, (char*)&c, &ta, (char*)&tc);
3111
3112                         /* Ignore non-graphics */
3113                         if ((a & 0x80) && (c & 0x80))
3114                         {
3115                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
3116                         }
3117                 }
3118         }
3119
3120         /* Hilite the player */
3121         Term_curs_win(px - min_x, py - min_y);
3122
3123         /* Wait for a keypress, flush key buffer */
3124         Term_inkey(&c, TRUE, TRUE);
3125         Term_flush();
3126
3127         /* Switch off the map display */
3128         td->map_active = FALSE;
3129
3130         /* Restore screen */
3131         Term_xtra_win_clear();
3132         Term_redraw();
3133 }
3134
3135
3136 /*** Other routines ***/
3137
3138
3139 /*
3140  * Create and initialize a "term_data" given a title
3141  */
3142 static void term_data_link(term_data *td)
3143 {
3144         term *t = &td->t;
3145
3146         /* Initialize the term */
3147         term_init(t, td->cols, td->rows, td->keys);
3148
3149         /* Use a "software" cursor */
3150         t->soft_cursor = TRUE;
3151
3152         /* Use "Term_pict" for "graphic" data */
3153         t->higher_pict = TRUE;
3154
3155         /* Erase with "white space" */
3156         t->attr_blank = TERM_WHITE;
3157         t->char_blank = ' ';
3158
3159 #if 0
3160         /* Prepare the init/nuke hooks */
3161         t->init_hook = Term_init_win;
3162         t->nuke_hook = Term_nuke_win;
3163 #endif
3164
3165         /* Prepare the template hooks */
3166         t->user_hook = Term_user_win;
3167         t->xtra_hook = Term_xtra_win;
3168         t->curs_hook = Term_curs_win;
3169         t->bigcurs_hook = Term_bigcurs_win;
3170         t->wipe_hook = Term_wipe_win;
3171         t->text_hook = Term_text_win;
3172         t->pict_hook = Term_pict_win;
3173
3174         /* Remember where we came from */
3175         t->data = (vptr)(td);
3176 }
3177
3178
3179 /*
3180  * Create the windows
3181  *
3182  * First, instantiate the "default" values, then read the "ini_file"
3183  * to over-ride selected values, then create the windows, and fonts.
3184  *
3185  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
3186  * must make active to realize the palette.  XXX XXX XXX
3187  */
3188 static void init_windows(void)
3189 {
3190         int i;
3191
3192         term_data *td;
3193
3194 #if 0 /* #ifndef JP */
3195         char buf[1024];
3196 #endif
3197
3198         /* Main window */
3199         td = &data[0];
3200         WIPE(td, term_data);
3201 #ifdef JP
3202         td->s = "ÊѶòÈÚÅÜ";
3203 #else
3204         td->s = angband_term_name[0];
3205 #endif
3206
3207         td->keys = 1024;
3208         td->rows = 24;
3209         td->cols = 80;
3210         td->visible = TRUE;
3211         td->size_ow1 = 2;
3212         td->size_ow2 = 2;
3213         td->size_oh1 = 2;
3214         td->size_oh2 = 2;
3215         td->pos_x = 7 * 30;
3216         td->pos_y = 7 * 20;
3217         td->posfix = FALSE;
3218 #if 1 /* #ifdef JP */
3219         td->bizarre = TRUE;
3220 #endif
3221         /* Sub windows */
3222         for (i = 1; i < MAX_TERM_DATA; i++)
3223         {
3224                 td = &data[i];
3225                 WIPE(td, term_data);
3226                 td->s = angband_term_name[i];
3227                 td->keys = 16;
3228                 td->rows = 24;
3229                 td->cols = 80;
3230                 td->visible = FALSE;
3231                 td->size_ow1 = 1;
3232                 td->size_ow2 = 1;
3233                 td->size_oh1 = 1;
3234                 td->size_oh2 = 1;
3235                 td->pos_x = (7 - i) * 30;
3236                 td->pos_y = (7 - i) * 20;
3237                 td->posfix = FALSE;
3238 #if 1 /* #ifdef JP */
3239                         td->bizarre = TRUE;
3240 #endif
3241         }
3242
3243
3244         /* Load prefs */
3245         load_prefs();
3246
3247
3248         /* Main window (need these before term_getsize gets called) */
3249         td = &data[0];
3250         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
3251                        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
3252                        WS_VISIBLE);
3253         td->dwExStyle = 0;
3254         td->visible = TRUE;
3255
3256         /* Sub windows (need these before term_getsize gets called) */
3257         for (i = 1; i < MAX_TERM_DATA; i++)
3258         {
3259                 td = &data[i];
3260                 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
3261                 td->dwExStyle = (WS_EX_TOOLWINDOW);
3262         }
3263
3264
3265         /* All windows */
3266         for (i = 0; i < MAX_TERM_DATA; i++)
3267         {
3268                 td = &data[i];
3269
3270 #if 1 /* #ifdef JP */
3271                 strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
3272                 td->lf.lfCharSet = DEFAULT_CHARSET;
3273                 td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
3274                 /* Activate the chosen font */
3275                 term_force_font(td, NULL);
3276                 td->tile_wid = td->font_wid;
3277                 td->tile_hgt = td->font_hgt;
3278 #else
3279                 /* Access the standard font file */
3280                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, td->font_want);
3281
3282                 /* Activate the chosen font */
3283                 if (term_force_font(td, buf))
3284                 {
3285                         /* Access the standard font file */
3286                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
3287
3288                         /* Force the use of that font */
3289                         (void)term_force_font(td, buf);
3290
3291                         /* Oops */
3292                         td->tile_wid = 8;
3293                         td->tile_hgt = 13;
3294
3295                         /* Assume not bizarre */
3296                         td->bizarre = FALSE;
3297                 }
3298 #endif
3299
3300
3301                 /* Analyze the font */
3302                 term_getsize(td);
3303
3304                 /* Resize the window */
3305                 term_window_resize(td);
3306         }
3307
3308
3309         /* Sub windows (reverse order) */
3310         for (i = MAX_TERM_DATA - 1; i >= 1; --i)
3311         {
3312                 td = &data[i];
3313
3314                 my_td = td;
3315                 td->w = CreateWindowEx(td->dwExStyle, AngList,
3316                                        td->s, td->dwStyle,
3317                                        td->pos_x, td->pos_y,
3318                                        td->size_wid, td->size_hgt,
3319                                        HWND_DESKTOP, NULL, hInstance, NULL);
3320                 my_td = NULL;
3321 #ifdef JP
3322                 if (!td->w) quit("¥µ¥Ö¥¦¥£¥ó¥É¥¦¤ËºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3323 #else
3324                 if (!td->w) quit("Failed to create sub-window");
3325 #endif
3326
3327
3328                 if (td->visible)
3329                 {
3330                         td->size_hack = TRUE;
3331                         ShowWindow(td->w, SW_SHOW);
3332                         td->size_hack = FALSE;
3333                 }
3334
3335                 term_data_link(td);
3336                 angband_term[i] = &td->t;
3337
3338                 if (td->visible)
3339                 {
3340                         /* Activate the window */
3341                         SetActiveWindow(td->w);
3342                 }
3343
3344                 if (data[i].posfix)
3345                 {
3346                         term_window_pos(&data[i], HWND_TOPMOST);
3347                 }
3348                 else
3349                 {
3350                         term_window_pos(&data[i], td->w);
3351                 }
3352         }
3353
3354
3355         /* Main window */
3356         td = &data[0];
3357
3358         /* Main window */
3359         my_td = td;
3360         td->w = CreateWindowEx(td->dwExStyle, AppName,
3361                                td->s, td->dwStyle,
3362                                td->pos_x, td->pos_y,
3363                                td->size_wid, td->size_hgt,
3364                                HWND_DESKTOP, NULL, hInstance, NULL);
3365         my_td = NULL;
3366 #ifdef JP
3367         if (!td->w) quit("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÎºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3368 #else
3369         if (!td->w) quit("Failed to create Angband window");
3370 #endif
3371
3372
3373         term_data_link(td);
3374         angband_term[0] = &td->t;
3375         normsize.x = td->cols;
3376         normsize.y = td->rows;
3377
3378         /* Activate the main window */
3379         if (win_maximized) ShowWindow(td->w, SW_SHOWMAXIMIZED);
3380         else ShowWindow(td->w, SW_SHOW);
3381
3382         /* Bring main window back to top */
3383         SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3384
3385
3386         /* New palette XXX XXX XXX */
3387         (void)new_palette();
3388
3389
3390         /* Create a "brush" for drawing the "cursor" */
3391         hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
3392
3393
3394         /* Process pending messages */
3395         (void)Term_xtra_win_flush();
3396 }
3397
3398
3399
3400 /*
3401  * Prepare the menus
3402  */
3403 static void setup_menus(void)
3404 {
3405         int i;
3406
3407         HMENU hm = GetMenu(data[0].w);
3408
3409
3410         /* Menu "File", Disable all */
3411         EnableMenuItem(hm, IDM_FILE_NEW,
3412                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3413         EnableMenuItem(hm, IDM_FILE_OPEN,
3414                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3415         EnableMenuItem(hm, IDM_FILE_SAVE,
3416                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3417         EnableMenuItem(hm, IDM_FILE_EXIT,
3418                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3419         EnableMenuItem(hm, IDM_FILE_SCORE,
3420                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3421
3422
3423         /* No character available */
3424         if (!character_generated)
3425         {
3426                 /* Menu "File", Item "New" */
3427                 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
3428
3429                 /* Menu "File", Item "Open" */
3430                 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
3431         }
3432
3433         /* A character available */
3434         if (character_generated)
3435         {
3436                 /* Menu "File", Item "Save" */
3437                 EnableMenuItem(hm, IDM_FILE_SAVE,
3438                            MF_BYCOMMAND | MF_ENABLED);
3439         }
3440
3441         /* Menu "File", Item "Exit" */
3442         EnableMenuItem(hm, IDM_FILE_EXIT,
3443                        MF_BYCOMMAND | MF_ENABLED);
3444
3445         EnableMenuItem(hm, IDM_FILE_SCORE,
3446                        MF_BYCOMMAND | MF_ENABLED);
3447
3448
3449         /* Menu "Window::Visibility" */
3450         for (i = 0; i < MAX_TERM_DATA; i++)
3451         {
3452                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3453                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3454
3455                 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3456                               (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
3457
3458                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3459                                MF_BYCOMMAND | MF_ENABLED);
3460         }
3461
3462         /* Menu "Window::Font" */
3463         for (i = 0; i < MAX_TERM_DATA; i++)
3464         {
3465                 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3466                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3467
3468                 if (data[i].visible)
3469                 {
3470                         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3471                                        MF_BYCOMMAND | MF_ENABLED);
3472                 }
3473         }
3474
3475         /* Menu "Window::Window Position Fix" */
3476         for (i = 0; i < MAX_TERM_DATA; i++)
3477         {
3478                 EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3479                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3480
3481                 CheckMenuItem(hm, IDM_WINDOW_POS_0 + i,
3482                               (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
3483
3484                 if (data[i].visible)
3485                 {
3486                         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3487                                        MF_BYCOMMAND | MF_ENABLED);
3488                 }
3489         }
3490
3491         /* Menu "Window::Bizarre Display" */
3492         for (i = 0; i < MAX_TERM_DATA; i++)
3493         {
3494                 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3495                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3496
3497                 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3498                               (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
3499
3500                 if (data[i].visible)
3501                 {
3502                         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3503                                    MF_BYCOMMAND | MF_ENABLED);
3504
3505                 }
3506         }
3507
3508         /* Menu "Window::Increase Tile Width" */
3509         for (i = 0; i < MAX_TERM_DATA; i++)
3510         {
3511                 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3512                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3513
3514                 if (data[i].visible)
3515                 {
3516                         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3517                                    MF_BYCOMMAND | MF_ENABLED);
3518
3519                 }
3520         }
3521
3522         /* Menu "Window::Decrease Tile Width" */
3523         for (i = 0; i < MAX_TERM_DATA; i++)
3524         {
3525                 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3526                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3527
3528                 if (data[i].visible)
3529                 {
3530                         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3531                                    MF_BYCOMMAND | MF_ENABLED);
3532
3533                 }
3534         }
3535
3536         /* Menu "Window::Increase Tile Height" */
3537         for (i = 0; i < MAX_TERM_DATA; i++)
3538         {
3539                 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3540                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3541
3542                 if (data[i].visible)
3543                 {
3544                         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3545                                    MF_BYCOMMAND | MF_ENABLED);
3546
3547                 }
3548         }
3549
3550         /* Menu "Window::Decrease Tile Height" */
3551         for (i = 0; i < MAX_TERM_DATA; i++)
3552         {
3553                 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3554                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3555
3556                 if (data[i].visible)
3557                 {
3558                         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3559                                    MF_BYCOMMAND | MF_ENABLED);
3560
3561                 }
3562         }
3563
3564         /* Menu "Options", disable all */
3565         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3566                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3567         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3568                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3569         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3570                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3571         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE,
3572                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3573         EnableMenuItem(hm, IDM_OPTIONS_SOUND,
3574                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3575 #ifndef JP
3576         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3577                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3578 #endif
3579
3580         /* Menu "Options", Item "Map" */
3581         if (use_graphics != GRAPHICS_NONE)
3582                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
3583         else
3584                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3585                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3586
3587         /* Menu "Options", update all */
3588         CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3589                       (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
3590         CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3591                       (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
3592         CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3593                       (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
3594         CheckMenuItem(hm, IDM_OPTIONS_NEW2_GRAPHICS,
3595                       (arg_graphics == GRAPHICS_HENGBAND ? MF_CHECKED : MF_UNCHECKED));
3596         CheckMenuItem(hm, IDM_OPTIONS_BIGTILE,
3597                       (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
3598         CheckMenuItem(hm, IDM_OPTIONS_MUSIC,
3599                       (arg_music ? MF_CHECKED : MF_UNCHECKED));
3600         CheckMenuItem(hm, IDM_OPTIONS_SOUND,
3601                       (arg_sound ? MF_CHECKED : MF_UNCHECKED));
3602         CheckMenuItem(hm, IDM_OPTIONS_BG,
3603                       (use_bg ? MF_CHECKED : MF_UNCHECKED));
3604 #ifndef JP
3605         CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3606                       (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3607 #endif
3608
3609 #ifdef USE_GRAPHICS
3610         /* Menu "Options", Item "Graphics" */
3611         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
3612         /* Menu "Options", Item "Graphics" */
3613         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
3614         /* Menu "Options", Item "Graphics" */
3615         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
3616         /* Menu "Options", Item "Graphics" */
3617         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
3618 #endif /* USE_GRAPHICS */
3619
3620 #ifdef USE_SOUND
3621         /* Menu "Options", Item "Sound" */
3622         EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
3623 #endif /* USE_SOUND */
3624
3625 #ifdef USE_SAVER
3626         /* Menu "Options", Item "ScreenSaver" */
3627         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3628                        MF_BYCOMMAND | MF_ENABLED);
3629 #endif /* USE_SAVER */
3630 }
3631
3632
3633 /*
3634  * Check for double clicked (or dragged) savefile
3635  *
3636  * Apparently, Windows copies the entire filename into the first
3637  * piece of the "command line string".  Perhaps we should extract
3638  * the "basename" of that filename and append it to the "save" dir.
3639  */
3640 static void check_for_save_file(LPSTR cmd_line)
3641 {
3642         char *s;
3643
3644         /* First arg */
3645         s = cmd_line;
3646
3647         /* No args */
3648         if (!*s) return;
3649
3650         /* Extract filename */
3651         strcat(savefile, s);
3652
3653         /* Validate the file */
3654         validate_file(savefile);
3655
3656         /* Game in progress */
3657         game_in_progress = TRUE;
3658
3659         /* Play game */
3660         play_game(FALSE);
3661 }
3662
3663
3664 /*
3665  * Process a menu command
3666  */
3667 static void process_menus(WORD wCmd)
3668 {
3669         int i;
3670
3671         term_data *td;
3672
3673         OPENFILENAME ofn;
3674
3675         /* Analyze */
3676         switch (wCmd)
3677         {
3678                 /* New game */
3679                 case IDM_FILE_NEW:
3680                 {
3681                         if (!initialized)
3682                         {
3683 #ifdef JP
3684                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3685 #else
3686                                 plog("You cannot do that yet...");
3687 #endif
3688
3689                         }
3690                         else if (game_in_progress)
3691                         {
3692 #ifdef JP
3693                                 plog("¥×¥ì¥¤Ãæ¤Ï¿·¤·¤¤¥²¡¼¥à¤ò»Ï¤á¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3694 #else
3695                                 plog("You can't start a new game while you're still playing!");
3696 #endif
3697
3698                         }
3699                         else
3700                         {
3701                                 game_in_progress = TRUE;
3702                                 Term_flush();
3703                                 play_game(TRUE);
3704                                 quit(NULL);
3705                         }
3706                         break;
3707                 }
3708
3709                 /* Open game */
3710                 case IDM_FILE_OPEN:
3711                 {
3712                         if (!initialized)
3713                         {
3714 #ifdef JP
3715                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3716 #else
3717                                 plog("You cannot do that yet...");
3718 #endif
3719
3720                         }
3721                         else if (game_in_progress)
3722                         {
3723 #ifdef JP
3724                                 plog("¥×¥ì¥¤Ãæ¤Ï¥²¡¼¥à¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3725 #else
3726                                 plog("You can't open a new game while you're still playing!");
3727 #endif
3728
3729                         }
3730                         else
3731                         {
3732                                 memset(&ofn, 0, sizeof(ofn));
3733                                 ofn.lStructSize = sizeof(ofn);
3734                                 ofn.hwndOwner = data[0].w;
3735                                 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3736                                 ofn.nFilterIndex = 1;
3737                                 ofn.lpstrFile = savefile;
3738                                 ofn.nMaxFile = 1024;
3739                                 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3740                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
3741
3742                                 if (GetOpenFileName(&ofn))
3743                                 {
3744                                         /* Load 'savefile' */
3745                                         validate_file(savefile);
3746                                         game_in_progress = TRUE;
3747                                         Term_flush();
3748                                         play_game(FALSE);
3749                                         quit(NULL);
3750                                 }
3751                         }
3752                         break;
3753                 }
3754
3755                 /* Save game */
3756                 case IDM_FILE_SAVE:
3757                 {
3758                         if (game_in_progress && character_generated)
3759                         {
3760                                 /* Paranoia */
3761                                 if (!can_save)
3762                                 {
3763 #ifdef JP
3764                                         plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3765 #else
3766                                         plog("You may not do that right now.");
3767 #endif
3768
3769                                         break;
3770                                 }
3771
3772                                 /* Hack -- Forget messages */
3773                                 msg_flag = FALSE;
3774
3775                                 /* Save the game */
3776 #ifdef ZANGBAND
3777                                 do_cmd_save_game(FALSE);
3778 #else /* ZANGBAND */
3779                                 do_cmd_save_game();
3780 #endif /* ZANGBAND */
3781                         }
3782                         else
3783                         {
3784 #ifdef JP
3785                                 plog("º£¡¢¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3786 #else
3787                                 plog("You may not do that right now.");
3788 #endif
3789
3790                         }
3791                         break;
3792                 }
3793
3794                 /* Exit */
3795                 case IDM_FILE_EXIT:
3796                 {
3797                         if (game_in_progress && character_generated)
3798                         {
3799                                 /* Paranoia */
3800                                 if (!can_save)
3801                                 {
3802 #ifdef JP
3803                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
3804 #else
3805                                         plog("You may not do that right now.");
3806 #endif
3807
3808                                         break;
3809                                 }
3810
3811                                 /* Hack -- Forget messages */
3812                                 msg_flag = FALSE;
3813
3814                                 forget_lite();
3815                                 forget_view();
3816                                 clear_mon_lite();
3817
3818                                 /* Save the game */
3819 #ifdef ZANGBAND
3820                                 /* do_cmd_save_game(FALSE); */
3821 #else /* ZANGBAND */
3822                                 /* do_cmd_save_game(); */
3823 #endif /* ZANGBAND */
3824                                 Term_key_push(SPECIAL_KEY_QUIT);
3825                                 break;
3826                         }
3827                         quit(NULL);
3828                         break;
3829                 }
3830
3831                 /* Show scores */
3832                 case IDM_FILE_SCORE:
3833                 {
3834                         char buf[1024];
3835
3836                         /* Build the filename */
3837                         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
3838
3839                         /* Open the binary high score file, for reading */
3840                         highscore_fd = fd_open(buf, O_RDONLY);
3841
3842                         /* Paranoia -- No score file */
3843                         if (highscore_fd < 0)
3844                         {
3845                                 msg_print("Score file unavailable.");
3846                         }
3847                         else
3848                         {
3849                                 /* Save Screen */
3850                                 screen_save();
3851
3852                                 /* Clear screen */
3853                                 Term_clear();
3854
3855                                 /* Display the scores */
3856                                 display_scores_aux(0, MAX_HISCORES, -1, NULL);
3857
3858                                 /* Shut the high score file */
3859                                 (void)fd_close(highscore_fd);
3860
3861                                 /* Forget the high score fd */
3862                                 highscore_fd = -1;
3863
3864                                 /* Load screen */
3865                                 screen_load();
3866
3867                                 /* Hack - Flush it */
3868                                 Term_fresh();
3869                         }
3870
3871                         break;
3872                 }
3873
3874                 /* Open game */
3875                 case IDM_FILE_MOVIE:
3876                 {
3877                         if (!initialized)
3878                         {
3879 #ifdef JP
3880                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3881 #else
3882                                 plog("You cannot do that yet...");
3883 #endif
3884                         }
3885                         else if (game_in_progress)
3886                         {
3887 #ifdef JP
3888                                 plog("¥×¥ì¥¤Ãæ¤Ï¥à¡¼¥Ó¡¼¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3889 #else
3890                                 plog("You can't open a movie while you're playing!");
3891 #endif
3892                         }
3893                         else
3894                         {
3895                                 memset(&ofn, 0, sizeof(ofn));
3896                                 ofn.lStructSize = sizeof(ofn);
3897                                 ofn.hwndOwner = data[0].w;
3898                                 ofn.lpstrFilter = "Angband Movie Files (*.amv)\0*.amv\0";
3899                                 ofn.nFilterIndex = 1;
3900                                 ofn.lpstrFile = savefile;
3901                                 ofn.nMaxFile = 1024;
3902                                 ofn.lpstrInitialDir = ANGBAND_DIR_USER;
3903                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
3904
3905                                 if (GetOpenFileName(&ofn))
3906                                 {
3907                                         /* Load 'savefile' */
3908                                         prepare_browse_movie_aux(savefile);
3909                                         play_game(FALSE);
3910                                         quit(NULL);
3911                                         return;
3912                                 }
3913                         }
3914                         break;
3915                 }
3916
3917
3918                 case IDM_WINDOW_VIS_0:
3919                 {
3920 #ifdef JP
3921                         plog("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÏÈóɽ¼¨¤Ë¤Ç¤­¤Þ¤»¤ó¡ª");
3922 #else
3923                         plog("You are not allowed to do that!");
3924 #endif
3925
3926
3927                         break;
3928                 }
3929
3930                 /* Window visibility */
3931                 case IDM_WINDOW_VIS_1:
3932                 case IDM_WINDOW_VIS_2:
3933                 case IDM_WINDOW_VIS_3:
3934                 case IDM_WINDOW_VIS_4:
3935                 case IDM_WINDOW_VIS_5:
3936                 case IDM_WINDOW_VIS_6:
3937                 case IDM_WINDOW_VIS_7:
3938                 {
3939                         i = wCmd - IDM_WINDOW_VIS_0;
3940
3941                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3942
3943                         td = &data[i];
3944
3945                         if (!td->visible)
3946                         {
3947                                 td->visible = TRUE;
3948                                 ShowWindow(td->w, SW_SHOW);
3949                                 term_data_redraw(td);
3950                         }
3951                         else
3952                         {
3953                                 td->visible = FALSE;
3954                                 td->posfix = FALSE;
3955                                 ShowWindow(td->w, SW_HIDE);
3956                         }
3957
3958                         break;
3959                 }
3960
3961                 /* Window fonts */
3962                 case IDM_WINDOW_FONT_0:
3963                 case IDM_WINDOW_FONT_1:
3964                 case IDM_WINDOW_FONT_2:
3965                 case IDM_WINDOW_FONT_3:
3966                 case IDM_WINDOW_FONT_4:
3967                 case IDM_WINDOW_FONT_5:
3968                 case IDM_WINDOW_FONT_6:
3969                 case IDM_WINDOW_FONT_7:
3970                 {
3971                         i = wCmd - IDM_WINDOW_FONT_0;
3972
3973                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3974
3975                         td = &data[i];
3976
3977                         term_change_font(td);
3978
3979                         break;
3980                 }
3981
3982                 /* Window Z Position */
3983                 case IDM_WINDOW_POS_1:
3984                 case IDM_WINDOW_POS_2:
3985                 case IDM_WINDOW_POS_3:
3986                 case IDM_WINDOW_POS_4:
3987                 case IDM_WINDOW_POS_5:
3988                 case IDM_WINDOW_POS_6:
3989                 case IDM_WINDOW_POS_7:
3990                 {
3991                         i = wCmd - IDM_WINDOW_POS_0;
3992
3993                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3994
3995                         td = &data[i];
3996
3997                         if (!td->posfix && td->visible)
3998                         {
3999                                 td->posfix = TRUE;
4000                                 term_window_pos(td, HWND_TOPMOST);
4001                         }
4002                         else
4003                         {
4004                                 td->posfix = FALSE;
4005                                 term_window_pos(td, data[0].w);
4006                         }
4007
4008                         break;
4009                 }
4010
4011                 /* Bizarre Display */
4012                 case IDM_WINDOW_BIZ_0:
4013                 case IDM_WINDOW_BIZ_1:
4014                 case IDM_WINDOW_BIZ_2:
4015                 case IDM_WINDOW_BIZ_3:
4016                 case IDM_WINDOW_BIZ_4:
4017                 case IDM_WINDOW_BIZ_5:
4018                 case IDM_WINDOW_BIZ_6:
4019                 case IDM_WINDOW_BIZ_7:
4020                 {
4021                         i = wCmd - IDM_WINDOW_BIZ_0;
4022
4023                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4024
4025                         td = &data[i];
4026
4027                         td->bizarre = !td->bizarre;
4028
4029                         term_getsize(td);
4030
4031                         term_window_resize(td);
4032
4033                         break;
4034                 }
4035
4036                 /* Increase Tile Width */
4037                 case IDM_WINDOW_I_WID_0:
4038                 case IDM_WINDOW_I_WID_1:
4039                 case IDM_WINDOW_I_WID_2:
4040                 case IDM_WINDOW_I_WID_3:
4041                 case IDM_WINDOW_I_WID_4:
4042                 case IDM_WINDOW_I_WID_5:
4043                 case IDM_WINDOW_I_WID_6:
4044                 case IDM_WINDOW_I_WID_7:
4045                 {
4046                         i = wCmd - IDM_WINDOW_I_WID_0;
4047
4048                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4049
4050                         td = &data[i];
4051
4052                         td->tile_wid += 1;
4053
4054                         term_getsize(td);
4055
4056                         term_window_resize(td);
4057
4058                         break;
4059                 }
4060
4061                 /* Decrease Tile Height */
4062                 case IDM_WINDOW_D_WID_0:
4063                 case IDM_WINDOW_D_WID_1:
4064                 case IDM_WINDOW_D_WID_2:
4065                 case IDM_WINDOW_D_WID_3:
4066                 case IDM_WINDOW_D_WID_4:
4067                 case IDM_WINDOW_D_WID_5:
4068                 case IDM_WINDOW_D_WID_6:
4069                 case IDM_WINDOW_D_WID_7:
4070                 {
4071                         i = wCmd - IDM_WINDOW_D_WID_0;
4072
4073                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4074
4075                         td = &data[i];
4076
4077                         td->tile_wid -= 1;
4078
4079                         term_getsize(td);
4080
4081                         term_window_resize(td);
4082
4083                         break;
4084                 }
4085
4086                 /* Increase Tile Height */
4087                 case IDM_WINDOW_I_HGT_0:
4088                 case IDM_WINDOW_I_HGT_1:
4089                 case IDM_WINDOW_I_HGT_2:
4090                 case IDM_WINDOW_I_HGT_3:
4091                 case IDM_WINDOW_I_HGT_4:
4092                 case IDM_WINDOW_I_HGT_5:
4093                 case IDM_WINDOW_I_HGT_6:
4094                 case IDM_WINDOW_I_HGT_7:
4095                 {
4096                         i = wCmd - IDM_WINDOW_I_HGT_0;
4097
4098                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4099
4100                         td = &data[i];
4101
4102                         td->tile_hgt += 1;
4103
4104                         term_getsize(td);
4105
4106                         term_window_resize(td);
4107
4108                         break;
4109                 }
4110
4111                 /* Decrease Tile Height */
4112                 case IDM_WINDOW_D_HGT_0:
4113                 case IDM_WINDOW_D_HGT_1:
4114                 case IDM_WINDOW_D_HGT_2:
4115                 case IDM_WINDOW_D_HGT_3:
4116                 case IDM_WINDOW_D_HGT_4:
4117                 case IDM_WINDOW_D_HGT_5:
4118                 case IDM_WINDOW_D_HGT_6:
4119                 case IDM_WINDOW_D_HGT_7:
4120                 {
4121                         i = wCmd - IDM_WINDOW_D_HGT_0;
4122
4123                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
4124
4125                         td = &data[i];
4126
4127                         td->tile_hgt -= 1;
4128
4129                         term_getsize(td);
4130
4131                         term_window_resize(td);
4132
4133                         break;
4134                 }
4135
4136                 case IDM_OPTIONS_NO_GRAPHICS:
4137                 {
4138                         /* Paranoia */
4139                         if (!inkey_flag)
4140                         {
4141                                 plog("You may not do that right now.");
4142                                 break;
4143                         }
4144
4145                         /* Toggle "arg_graphics" */
4146                         if (arg_graphics != GRAPHICS_NONE)
4147                         {
4148                                 arg_graphics = GRAPHICS_NONE;
4149
4150                                 /* React to changes */
4151                                 Term_xtra_win_react();
4152
4153                                 /* Hack -- Force redraw */
4154                                 Term_key_push(KTRL('R'));
4155                         }
4156
4157                         break;
4158                 }
4159
4160                 case IDM_OPTIONS_OLD_GRAPHICS:
4161                 {
4162                         /* Paranoia */
4163                         if (!inkey_flag)
4164                         {
4165                                 plog("You may not do that right now.");
4166                                 break;
4167                         }
4168
4169                         /* Toggle "arg_graphics" */
4170                         if (arg_graphics != GRAPHICS_ORIGINAL)
4171                         {
4172                                 arg_graphics = GRAPHICS_ORIGINAL;
4173
4174                                 /* React to changes */
4175                                 Term_xtra_win_react();
4176
4177                                 /* Hack -- Force redraw */
4178                                 Term_key_push(KTRL('R'));
4179                         }
4180
4181                         break;
4182                 }
4183
4184                 case IDM_OPTIONS_NEW_GRAPHICS:
4185                 {
4186                         /* Paranoia */
4187                         if (!inkey_flag)
4188                         {
4189                                 plog("You may not do that right now.");
4190                                 break;
4191                         }
4192
4193                         /* Toggle "arg_graphics" */
4194                         if (arg_graphics != GRAPHICS_ADAM_BOLT)
4195                         {
4196                                 arg_graphics = GRAPHICS_ADAM_BOLT;
4197
4198                                 /* React to changes */
4199                                 Term_xtra_win_react();
4200
4201                                 /* Hack -- Force redraw */
4202                                 Term_key_push(KTRL('R'));
4203                         }
4204
4205                         break;
4206                 }
4207
4208                 case IDM_OPTIONS_NEW2_GRAPHICS:
4209                 {
4210                         /* Paranoia */
4211                         if (!inkey_flag)
4212                         {
4213                                 plog("You may not do that right now.");
4214                                 break;
4215                         }
4216
4217                         /* Toggle "arg_graphics" */
4218                         if (arg_graphics != GRAPHICS_HENGBAND)
4219                         {
4220                                 arg_graphics = GRAPHICS_HENGBAND;
4221
4222                                 /* React to changes */
4223                                 Term_xtra_win_react();
4224
4225                                 /* Hack -- Force redraw */
4226                                 Term_key_push(KTRL('R'));
4227                         }
4228
4229                         break;
4230                 }
4231
4232                 case IDM_OPTIONS_BIGTILE:
4233                 {
4234                         term_data *td = &data[0];
4235
4236                         /* Paranoia */
4237                         if (!inkey_flag)
4238                         {
4239                                 plog("You may not do that right now.");
4240                                 break;
4241                         }
4242
4243                         /* Toggle "arg_sound" */
4244                         arg_bigtile = !arg_bigtile;
4245
4246                         /* Activate */
4247                         Term_activate(&td->t);
4248
4249                         /* Resize the term */
4250                         Term_resize(td->cols, td->rows);
4251
4252                         /* Redraw later */
4253                         InvalidateRect(td->w, NULL, TRUE);
4254
4255                         break;
4256                 }
4257
4258                 case IDM_OPTIONS_MUSIC:
4259                 {
4260                         /* Paranoia */
4261                         if (!inkey_flag)
4262                         {
4263                                 plog("You may not do that right now.");
4264                                 break;
4265                         }
4266
4267                         /* Toggle "arg_sound" */
4268                         arg_music = !arg_music;
4269
4270                         /* React to changes */
4271                         Term_xtra_win_react();
4272
4273                         /* Hack -- Force redraw */
4274                         Term_key_push(KTRL('R'));
4275
4276                         break;
4277                 }
4278
4279                 case IDM_OPTIONS_SOUND:
4280                 {
4281                         /* Paranoia */
4282                         if (!inkey_flag)
4283                         {
4284                                 plog("You may not do that right now.");
4285                                 break;
4286                         }
4287
4288                         /* Toggle "arg_sound" */
4289                         arg_sound = !arg_sound;
4290
4291                         /* React to changes */
4292                         Term_xtra_win_react();
4293
4294                         /* Hack -- Force redraw */
4295                         Term_key_push(KTRL('R'));
4296
4297                         break;
4298                 }
4299
4300                 /* bg */
4301                 case IDM_OPTIONS_BG:
4302                 {
4303                         /* Paranoia */
4304                         if (!inkey_flag)
4305                         {
4306                                 plog("You may not do that right now.");
4307                                 break;
4308                         }
4309
4310                         /* Toggle "use_bg" */
4311                         use_bg = !use_bg;
4312
4313                         init_bg();
4314
4315                         /* React to changes */
4316                         Term_xtra_win_react();
4317
4318                         /* Hack -- Force redraw */
4319                         Term_key_push(KTRL('R'));
4320
4321                         break;
4322                 }
4323
4324                 /* bg */
4325                 case IDM_OPTIONS_OPEN_BG:
4326                 {
4327                         /* Paranoia */
4328                         if (!inkey_flag)
4329                         {
4330                                 plog("You may not do that right now.");
4331                                 break;
4332                         }
4333                         else
4334                         {
4335                                 memset(&ofn, 0, sizeof(ofn));
4336                                 ofn.lStructSize = sizeof(ofn);
4337                                 ofn.hwndOwner = data[0].w;
4338                                 ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
4339                                 ofn.nFilterIndex = 1;
4340                                 ofn.lpstrFile = bg_bitmap_file;
4341                                 ofn.nMaxFile = 1023;
4342                                 ofn.lpstrInitialDir = NULL;
4343 #ifdef JP
4344                                 ofn.lpstrTitle = "ÊÉ»æ¤òÁª¤ó¤Ç¤Í¡£";
4345 #else
4346                                 ofn.lpstrTitle = "Choose wall paper.";
4347 #endif
4348                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
4349
4350                                 if (GetOpenFileName(&ofn))
4351                                 {
4352                                         /* Load 'savefile' */
4353                                         use_bg = 1;
4354                                         init_bg();
4355                                 }
4356
4357                                 /* React to changes */
4358                                 Term_xtra_win_react();
4359
4360                                 /* Hack -- Force redraw */
4361                                 Term_key_push(KTRL('R'));
4362                         }
4363                         break;
4364                 }
4365
4366                 case IDM_DUMP_SCREEN_HTML:
4367                 {
4368                         static char buf[1024] = "";
4369                         memset(&ofn, 0, sizeof(ofn));
4370                         ofn.lStructSize = sizeof(ofn);
4371                         ofn.hwndOwner = data[0].w;
4372                         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
4373                         ofn.nFilterIndex = 1;
4374                         ofn.lpstrFile = buf;
4375                         ofn.nMaxFile = 1023;
4376                         ofn.lpstrDefExt = "html";
4377                         ofn.lpstrInitialDir = NULL;
4378 #ifdef JP
4379                         ofn.lpstrTitle = "HTML¤Ç¥¹¥¯¥ê¡¼¥ó¥À¥ó¥×¤òÊݸ";
4380 #else
4381                         ofn.lpstrTitle = "Save screen dump as HTML.";
4382 #endif
4383                         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
4384
4385                         if (GetSaveFileName(&ofn))
4386                         {
4387                                 do_cmd_save_screen_html_aux(buf, 0);
4388                         }
4389                         break;
4390                 }
4391
4392 #ifdef USE_SAVER
4393
4394                 case IDM_OPTIONS_SAVER:
4395                 {
4396                         if (hwndSaver)
4397                         {
4398                                 DestroyWindow(hwndSaver);
4399                                 hwndSaver = NULL;
4400                         }
4401                         else
4402                         {
4403                                 /* Create a screen scaver window */
4404                                 hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass",
4405                                                            "Angband Screensaver",
4406                                                            WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4407                                                            0, 0, GetSystemMetrics(SM_CXSCREEN),
4408                                                            GetSystemMetrics(SM_CYSCREEN),
4409                                                            NULL, NULL, hInstance, NULL);
4410
4411                                 if (hwndSaver)
4412                                 {
4413                                         /* Push the window to the bottom XXX XXX XXX */
4414                                         SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
4415                                 }
4416                                 else
4417                                 {
4418 #ifdef JP
4419                                         plog("¥¦¥£¥ó¥É¥¦¤òºîÀ®½ÐÍè¤Þ¤»¤ó");
4420 #else
4421                                         plog("Failed to create saver window");
4422 #endif
4423
4424                                 }
4425                         }
4426                         break;
4427                 }
4428
4429 #endif
4430
4431                 case IDM_OPTIONS_MAP:
4432                 {
4433                         windows_map();
4434                         break;
4435                 }
4436
4437                 case IDM_HELP_CONTENTS:
4438                 {
4439 #ifdef HTML_HELP
4440                         char tmp[1024];
4441                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.chm");
4442                         if (check_file(tmp))
4443                         {
4444                                 HtmlHelp(data[0].w, tmp, HH_DISPLAY_TOPIC, 0);
4445                         }
4446                         else
4447                         {
4448 #ifdef JP
4449                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4450                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4451 #else
4452                                 plog_fmt("Cannot find help file: %s", tmp);
4453                                 plog("Use the online help files instead.");
4454 #endif
4455
4456                         }
4457                         break;
4458 #else /* HTML_HELP */
4459                         char buf[1024];
4460                         char tmp[1024];
4461                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
4462                         if (check_file(tmp))
4463                         {
4464                                 sprintf(buf, "winhelp.exe %s", tmp);
4465                                 WinExec(buf, SW_NORMAL);
4466                         }
4467                         else
4468                         {
4469 #ifdef JP
4470                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4471                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4472 #else
4473                                 plog_fmt("Cannot find help file: %s", tmp);
4474                                 plog("Use the online help files instead.");
4475 #endif
4476
4477                         }
4478                         break;
4479 #endif /* HTML_HELP */
4480                 }
4481         }
4482 }
4483
4484
4485 static bool process_keydown(WPARAM wParam, LPARAM lParam)
4486 {
4487         int i;
4488         bool mc = FALSE;
4489         bool ms = FALSE;
4490         bool ma = FALSE;
4491
4492         /* Extract the modifiers */
4493         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4494         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4495         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4496
4497         Term_no_press = (ma) ? TRUE : FALSE;
4498
4499         /* Handle "special" keys */
4500         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4501         {
4502                 bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
4503                 bool numpad = FALSE;
4504
4505                 /* Begin the macro trigger */
4506                 Term_keypress(31);
4507
4508                 /* Send the modifiers */
4509                 if (mc) Term_keypress('C');
4510                 if (ms) Term_keypress('S');
4511                 if (ma) Term_keypress('A');
4512
4513                 /* Extract "scan code" */
4514                 i = LOBYTE(HIWORD(lParam));
4515
4516                 /* Introduce the scan code */
4517                 Term_keypress('x');
4518
4519                 /* Extended key bit */
4520                 switch (wParam)
4521                 {
4522                         /* Numpad Enter and '/' are extended key */
4523                 case VK_DIVIDE:
4524                         Term_no_press = TRUE;
4525                 case VK_RETURN: /* Enter */
4526                         numpad = ext_key;
4527                         break;
4528                         /* Other extended keys are on full keyboard */
4529                 case VK_NUMPAD0:
4530                 case VK_NUMPAD1:
4531                 case VK_NUMPAD2:
4532                 case VK_NUMPAD3:
4533                 case VK_NUMPAD4:
4534                 case VK_NUMPAD5:
4535                 case VK_NUMPAD6:
4536                 case VK_NUMPAD7:
4537                 case VK_NUMPAD8:
4538                 case VK_NUMPAD9:
4539                 case VK_ADD:
4540                 case VK_MULTIPLY:
4541                 case VK_SUBTRACT:
4542                 case VK_SEPARATOR:
4543                 case VK_DECIMAL:
4544                         Term_no_press = TRUE;
4545                 case VK_CLEAR:
4546                 case VK_HOME:
4547                 case VK_END:
4548                 case VK_PRIOR:  /* Page Up */
4549                 case VK_NEXT:   /* Page Down */
4550                 case VK_INSERT:
4551                 case VK_DELETE:
4552                 case VK_UP:
4553                 case VK_DOWN:
4554                 case VK_LEFT:
4555                 case VK_RIGHT:
4556                         numpad = !ext_key;
4557                 }
4558
4559                 /* Special modifiers for keypad keys */
4560                 if (numpad) Term_keypress('K');
4561
4562                 /* Encode the hexidecimal scan code */
4563                 Term_keypress(hexsym[i/16]);
4564                 Term_keypress(hexsym[i%16]);
4565
4566                 /* End the macro trigger */
4567                 Term_keypress(13);
4568
4569                 return 1;
4570         }
4571
4572         return 0;
4573 }
4574
4575
4576 #ifdef __MWERKS__
4577 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4578                                   WPARAM wParam, LPARAM lParam);
4579 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4580                                   WPARAM wParam, LPARAM lParam)
4581 #else /* __MWERKS__ */
4582 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4583                                           WPARAM wParam, LPARAM lParam)
4584 #endif /* __MWERKS__ */
4585 {
4586         PAINTSTRUCT ps;
4587         HDC hdc;
4588         term_data *td;
4589 #if 0
4590         MINMAXINFO FAR *lpmmi;
4591         RECT rc;
4592 #endif
4593         int i;
4594
4595
4596         /* Acquire proper "term_data" info */
4597         td = (term_data *)GetWindowLong(hWnd, 0);
4598
4599         /* Handle message */
4600         switch (uMsg)
4601         {
4602                 /* XXX XXX XXX */
4603                 case WM_NCCREATE:
4604                 {
4605                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4606                         break;
4607                 }
4608
4609                 /* XXX XXX XXX */
4610                 case WM_CREATE:
4611                 {
4612                         mop.dwCallback=(DWORD)hWnd;
4613                         return 0;
4614                 }
4615
4616                 case WM_GETMINMAXINFO:
4617                 {
4618                         MINMAXINFO FAR *lpmmi;
4619                         RECT rc;
4620
4621                         lpmmi = (MINMAXINFO FAR *)lParam;
4622
4623                         /* this message was sent before WM_NCCREATE */
4624                         if (!td) return 1;
4625
4626                         /* Minimum window size is 80x24 */
4627                         rc.left = rc.top = 0;
4628                         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
4629                         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4630
4631                         /* Adjust */
4632                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4633
4634                         /* Save minimum size */
4635                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4636                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4637
4638                         return 0;
4639                 }
4640
4641                 case WM_PAINT:
4642                 {
4643                         BeginPaint(hWnd, &ps);
4644                         if (td) term_data_redraw(td);
4645                         EndPaint(hWnd, &ps);
4646                         ValidateRect(hWnd, NULL);
4647                         return 0;
4648                 }
4649
4650 #ifdef USE_MUSIC
4651                 case MM_MCINOTIFY:
4652                 {
4653                         if(wParam == MCI_NOTIFY_SUCCESSFUL)
4654                         {
4655                                 mciSendCommand(mop.wDeviceID, MCI_SEEK, MCI_SEEK_TO_START, 0);
4656                                 mciSendCommand(mop.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mop);
4657                         }
4658                         return 0;
4659                 }
4660 #endif
4661
4662                 case WM_SYSKEYDOWN:
4663                 case WM_KEYDOWN:
4664                 {
4665                         if (process_keydown(wParam, lParam))
4666                                 return 0;
4667                         break;
4668                 }
4669
4670                 case WM_CHAR:
4671                 {
4672                         if (Term_no_press) Term_no_press = FALSE;
4673                         else Term_keypress(wParam);
4674                         return 0;
4675                 }
4676
4677                 case WM_LBUTTONDOWN:
4678                 {
4679                         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4680                         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4681                         mouse_down = TRUE;
4682                         oldx = mousex;
4683                         oldy = mousey;
4684                         return 0;
4685                 }
4686
4687                 case WM_LBUTTONUP:
4688                 {
4689                         HGLOBAL hGlobal;
4690                         LPSTR lpStr;
4691                         int i, j, sz;
4692                         int dx = abs(oldx - mousex) + 1;
4693                         int dy = abs(oldy - mousey) + 1;
4694                         int ox = (oldx > mousex) ? mousex : oldx;
4695                         int oy = (oldy > mousey) ? mousey : oldy;
4696
4697                         mouse_down = FALSE;
4698                         paint_rect = FALSE;
4699
4700 #ifdef JP
4701                         sz = (dx + 3) * dy;
4702 #else
4703                         sz = (dx + 2) * dy;
4704 #endif
4705                         hGlobal = GlobalAlloc(GHND, sz + 1);
4706                         if (hGlobal == NULL) return 0;
4707                         lpStr = (LPSTR)GlobalLock(hGlobal);
4708
4709                         for (i = 0; i < dy; i++)
4710                         {
4711 #ifdef JP
4712                                 char *s;
4713                                 char **scr = data[0].t.scr->c;
4714
4715                                 C_MAKE(s, (dx + 1), char);
4716                                 strncpy(s, &scr[oy + i][ox], dx);
4717
4718                                 if (ox > 0)
4719                                 {
4720                                         if (iskanji(scr[oy + i][ox - 1])) s[0] = ' ';
4721                                 }
4722
4723                                 if (ox + dx < data[0].cols)
4724                                 {
4725                                         if (iskanji(scr[oy + i][ox + dx - 1])) s[dx - 1] = ' ';
4726                                 }
4727
4728                                 for (j = 0; j < dx; j++)
4729                                 {
4730                                         if (s[j] == 127) s[j] = '#';
4731                                         *lpStr++ = s[j];
4732                                 }
4733 #else
4734                                 for (j = 0; j < dx; j++)
4735                                 {
4736                                         *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
4737                                 }
4738 #endif
4739                                 if (dy > 1)
4740                                 {
4741                                         *lpStr++ = '\r';
4742                                         *lpStr++ = '\n';
4743                                 }
4744                         }
4745
4746                         GlobalUnlock(hGlobal);
4747                         if (OpenClipboard(hWnd) == 0)
4748                         {
4749                                 GlobalFree(hGlobal);
4750                                 return 0;
4751                         }
4752                         EmptyClipboard();
4753                         SetClipboardData(CF_TEXT, hGlobal);
4754                         CloseClipboard();
4755
4756                         Term_redraw();
4757
4758                         return 0;
4759                 }
4760
4761                 case WM_MOUSEMOVE:
4762                 {
4763                         if (mouse_down)
4764                         {
4765                                 int dx, dy;
4766                                 int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4767                                 int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4768                                 int ox, oy;
4769
4770                                 if (paint_rect)
4771                                 {
4772                                         dx = abs(oldx - mousex) + 1;
4773                                         dy = abs(oldy - mousey) + 1;
4774                                         ox = (oldx > mousex) ? mousex : oldx;
4775                                         oy = (oldy > mousey) ? mousey : oldy;
4776                                         Term_inversed_area(hWnd, ox, oy, dx, dy);
4777                                 }
4778                                 else
4779                                 {
4780                                         paint_rect = TRUE;
4781                                 }
4782
4783                                 dx = abs(cx - mousex) + 1;
4784                                 dy = abs(cy - mousey) + 1;
4785                                 ox = (cx > mousex) ? mousex : cx;
4786                                 oy = (cy > mousey) ? mousey : cy;
4787                                 Term_inversed_area(hWnd, ox, oy, dx, dy);
4788
4789                                 oldx = cx;
4790                                 oldy = cy;
4791                         }
4792                         return 0;
4793                 }
4794
4795                 case WM_INITMENU:
4796                 {
4797                         setup_menus();
4798                         return 0;
4799                 }
4800
4801                 case WM_CLOSE:
4802                 {
4803                         if (game_in_progress && character_generated)
4804                         {
4805                                 if (!can_save)
4806                                 {
4807 #ifdef JP
4808                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
4809 #else
4810                                         plog("You may not do that right now.");
4811 #endif
4812                                         return 0;
4813                                 }
4814
4815                                 /* Hack -- Forget messages */
4816                                 msg_flag = FALSE;
4817
4818                                 forget_lite();
4819                                 forget_view();
4820                                 clear_mon_lite();
4821
4822                                 /* Save the game */
4823 #ifdef ZANGBAND
4824                                 /* do_cmd_save_game(FALSE); */
4825 #else /* ZANGBAND */
4826                                 /* do_cmd_save_game(); */
4827 #endif /* ZANGBAND */
4828                                 Term_key_push(SPECIAL_KEY_QUIT);
4829                                 return 0;
4830                         }
4831                         quit(NULL);
4832                         return 0;
4833                 }
4834
4835                 case WM_QUERYENDSESSION:
4836                 {
4837                         if (game_in_progress && character_generated)
4838                         {
4839                                 /* Hack -- Forget messages */
4840                                 msg_flag = FALSE;
4841
4842                                 /* Mega-Hack -- Delay death */
4843                                 if (p_ptr->chp < 0) p_ptr->is_dead = FALSE;
4844
4845 #ifdef JP
4846                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "----¥²¡¼¥àÃæÃÇ----");
4847 #else
4848                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "---- Save and Exit Game ----");
4849 #endif
4850
4851                                 /* Hardcode panic save */
4852                                 p_ptr->panic_save = 1;
4853
4854                                 /* Forbid suspend */
4855                                 signals_ignore_tstp();
4856
4857                                 /* Indicate panic save */
4858 #ifdef JP
4859                                 (void)strcpy(p_ptr->died_from, "(¶ÛµÞ¥»¡¼¥Ö)");
4860 #else
4861                                 (void)strcpy(p_ptr->died_from, "(panic save)");
4862 #endif
4863
4864                                 /* Panic save */
4865                                 (void)save_player();
4866                         }
4867                         quit(NULL);
4868                         return 0;
4869                 }
4870
4871                 case WM_QUIT:
4872                 {
4873                         quit(NULL);
4874                         return 0;
4875                 }
4876
4877                 case WM_COMMAND:
4878                 {
4879                         process_menus(LOWORD(wParam));
4880                         return 0;
4881                 }
4882
4883                 case WM_SIZE:
4884                 {
4885                         /* this message was sent before WM_NCCREATE */
4886                         if (!td) return 1;
4887
4888                         /* it was sent from inside CreateWindowEx */
4889                         if (!td->w) return 1;
4890
4891                         /* was sent from WM_SIZE */
4892                         if (td->size_hack) return 1;
4893
4894                         switch (wParam)
4895                         {
4896                                 case SIZE_MINIMIZED:
4897                                 {
4898                                         /* Hide sub-windows */
4899                                         for (i = 1; i < MAX_TERM_DATA; i++)
4900                                         {
4901                                                 if (data[i].visible) ShowWindow(data[i].w, SW_HIDE);
4902                                         }
4903                                         return 0;
4904                                 }
4905
4906                                 case SIZE_MAXIMIZED:
4907                                 {
4908                                         /* fall through XXX XXX XXX */
4909                                 }
4910
4911                                 case SIZE_RESTORED:
4912                                 {
4913                                         uint cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4914                                         uint rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4915
4916                                         /* New size */
4917                                         if ((td->cols != cols) || (td->rows != rows))
4918                                         {
4919                                                 /* Save the new size */
4920                                                 td->cols = cols;
4921                                                 td->rows = rows;
4922
4923                                                 if (!IsZoomed(td->w) && !IsIconic(td->w))
4924                                                 {
4925                                                         normsize.x = td->cols;
4926                                                         normsize.y = td->rows;
4927                                                 }
4928
4929                                                 /* Activate */
4930                                                 Term_activate(&td->t);
4931
4932                                                 /* Resize the term */
4933                                                 Term_resize(td->cols, td->rows);
4934
4935                                                 /* Redraw later */
4936                                                 InvalidateRect(td->w, NULL, TRUE);
4937                                         }
4938
4939                                         td->size_hack = TRUE;
4940
4941                                         /* Show sub-windows */
4942                                         for (i = 1; i < MAX_TERM_DATA; i++)
4943                                         {
4944                                                 if (data[i].visible) ShowWindow(data[i].w, SW_SHOW);
4945                                         }
4946
4947                                         td->size_hack = FALSE;
4948
4949                                         return 0;
4950                                 }
4951                         }
4952                         break;
4953                 }
4954
4955                 case WM_PALETTECHANGED:
4956                 {
4957                         /* Ignore if palette change caused by itself */
4958                         if ((HWND)wParam == hWnd) return 0;
4959
4960                         /* Fall through... */
4961                 }
4962
4963                 case WM_QUERYNEWPALETTE:
4964                 {
4965                         if (!paletted) return 0;
4966
4967                         hdc = GetDC(hWnd);
4968
4969                         SelectPalette(hdc, hPal, FALSE);
4970
4971                         i = RealizePalette(hdc);
4972
4973                         /* if any palette entries changed, repaint the window. */
4974                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4975
4976                         ReleaseDC(hWnd, hdc);
4977
4978                         return 0;
4979                 }
4980
4981                 case WM_ACTIVATE:
4982                 {
4983                         if (wParam && !HIWORD(lParam))
4984                         {
4985                                 /* Do something to sub-windows */
4986                                 for (i = 1; i < MAX_TERM_DATA; i++)
4987                                 {
4988                                         if (!data[i].posfix) term_window_pos(&data[i], hWnd);
4989                                 }
4990
4991                                 /* Focus on main window */
4992                                 SetFocus(hWnd);
4993
4994                                 return 0;
4995                         }
4996
4997                         break;
4998                 }
4999
5000                 case WM_ACTIVATEAPP:
5001                 {
5002                         if (IsIconic(td->w)) break;
5003
5004                         for (i = 1; i < MAX_TERM_DATA; i++)
5005                         {
5006                                 if(data[i].visible)
5007                                 {
5008                                         if (wParam == TRUE)
5009                                         {
5010                                                 ShowWindow(data[i].w, SW_SHOW);
5011                                         }
5012                                         else
5013                                         {
5014                                                 ShowWindow(data[i].w, SW_HIDE);
5015                                         }
5016                                 }
5017                         }
5018                 }
5019         }
5020
5021         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5022 }
5023
5024
5025 #ifdef __MWERKS__
5026 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5027                                            WPARAM wParam, LPARAM lParam);
5028 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5029                                            WPARAM wParam, LPARAM lParam)
5030 #else /* __MWERKS__ */
5031 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
5032                                            WPARAM wParam, LPARAM lParam)
5033 #endif /* __MWERKS__ */
5034 {
5035         term_data *td;
5036 #if 0
5037         MINMAXINFO FAR *lpmmi;
5038         RECT rc;
5039 #endif
5040         PAINTSTRUCT ps;
5041         HDC hdc;
5042         int i;
5043
5044
5045         /* Acquire proper "term_data" info */
5046         td = (term_data *)GetWindowLong(hWnd, 0);
5047
5048         /* Process message */
5049         switch (uMsg)
5050         {
5051                 /* XXX XXX XXX */
5052                 case WM_NCCREATE:
5053                 {
5054                         SetWindowLong(hWnd, 0, (LONG)(my_td));
5055                         break;
5056                 }
5057
5058                 /* XXX XXX XXX */
5059                 case WM_CREATE:
5060                 {
5061                         return 0;
5062                 }
5063
5064                 case WM_GETMINMAXINFO:
5065                 {
5066                         MINMAXINFO FAR *lpmmi;
5067                         RECT rc;
5068
5069                         lpmmi = (MINMAXINFO FAR *)lParam;
5070
5071                         /* this message was sent before WM_NCCREATE */
5072                         if (!td) return 1;
5073
5074                         /* Minimum window size is 80x24 */
5075                         rc.left = rc.top = 0;
5076                         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
5077                         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
5078
5079                         /* Adjust */
5080                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
5081
5082                         /* Save minimum size */
5083                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
5084                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
5085
5086                         return 0;
5087                 }
5088
5089                 case WM_SIZE:
5090                 {
5091                         uint cols;
5092                         uint rows;
5093                         
5094                         /* this message was sent before WM_NCCREATE */
5095                         if (!td) return 1;
5096
5097                         /* it was sent from inside CreateWindowEx */
5098                         if (!td->w) return 1;
5099
5100                         /* was sent from inside WM_SIZE */
5101                         if (td->size_hack) return 1;
5102
5103                         td->size_hack = TRUE;
5104
5105                         cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
5106                         rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
5107
5108                         /* New size */
5109                         if ((td->cols != cols) || (td->rows != rows))
5110                         {
5111                                 /* Save old term */
5112                                 term *old_term = Term;
5113
5114                                 /* Save the new size */
5115                                 td->cols = cols;
5116                                 td->rows = rows;
5117
5118                                 /* Activate */
5119                                 Term_activate(&td->t);
5120
5121                                 /* Resize the term */
5122                                 Term_resize(td->cols, td->rows);
5123
5124                                 /* Activate */
5125                                 Term_activate(old_term);
5126
5127                                 /* Redraw later */
5128                                 InvalidateRect(td->w, NULL, TRUE);
5129
5130                                 /* HACK - Redraw all windows */
5131                                 p_ptr->window = 0xFFFFFFFF;
5132                                 window_stuff();
5133                         }
5134
5135                         td->size_hack = FALSE;
5136
5137                         return 0;
5138                 }
5139
5140                 case WM_PAINT:
5141                 {
5142                         BeginPaint(hWnd, &ps);
5143                         if (td) term_data_redraw(td);
5144                         EndPaint(hWnd, &ps);
5145                         return 0;
5146                 }
5147
5148                 case WM_SYSKEYDOWN:
5149                 case WM_KEYDOWN:
5150                 {
5151                         if (process_keydown(wParam, lParam))
5152                                 return 0;
5153                         break;
5154                 }
5155
5156                 case WM_CHAR:
5157                 {
5158                         if (Term_no_press) Term_no_press = FALSE;
5159                         else Term_keypress(wParam);
5160                         return 0;
5161                 }
5162
5163                 case WM_PALETTECHANGED:
5164                 {
5165                         /* ignore if palette change caused by itself */
5166                         if ((HWND)wParam == hWnd) return FALSE;
5167                         /* otherwise, fall through!!! */
5168                 }
5169
5170                 case WM_QUERYNEWPALETTE:
5171                 {
5172                         if (!paletted) return 0;
5173                         hdc = GetDC(hWnd);
5174                         SelectPalette(hdc, hPal, FALSE);
5175                         i = RealizePalette(hdc);
5176                         /* if any palette entries changed, repaint the window. */
5177                         if (i) InvalidateRect(hWnd, NULL, TRUE);
5178                         ReleaseDC(hWnd, hdc);
5179                         return 0;
5180                 }
5181
5182                 case WM_NCLBUTTONDOWN:
5183                 {
5184
5185 #ifdef HTCLOSE
5186                         if (wParam == HTCLOSE) wParam = HTSYSMENU;
5187 #endif /* HTCLOSE */
5188
5189                         if (wParam == HTSYSMENU)
5190                         {
5191                                 if (td->visible)
5192                                 {
5193                                         td->visible = FALSE;
5194                                         ShowWindow(td->w, SW_HIDE);
5195                                 }
5196
5197                                 return 0;
5198                         }
5199
5200                         break;
5201                 }
5202         }
5203
5204         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5205 }
5206
5207
5208 #ifdef USE_SAVER
5209
5210 #define MOUSE_SENS 40
5211
5212 #ifdef __MWERKS__
5213 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5214                                     WPARAM wParam, LPARAM lParam);
5215 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5216                                     WPARAM wParam, LPARAM lParam)
5217 #else /* __MWERKS__ */
5218 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
5219                                             WPARAM wParam, LPARAM lParam)
5220 #endif /* __MWERKS__ */
5221 {
5222         static int iMouse = 0;
5223         static WORD xMouse = 0;
5224         static WORD yMouse = 0;
5225
5226         int dx, dy;
5227
5228
5229         /* Process */
5230         switch (uMsg)
5231         {
5232                 /* XXX XXX XXX */
5233                 case WM_NCCREATE:
5234                 {
5235                         break;
5236                 }
5237
5238                 case WM_SETCURSOR:
5239                 {
5240                         SetCursor(NULL);
5241                         return 0;
5242                 }
5243
5244 #if 0
5245                 case WM_ACTIVATE:
5246                 {
5247                         if (LOWORD(wParam) == WA_INACTIVE) break;
5248
5249                         /* else fall through */
5250                 }
5251 #endif
5252
5253                 case WM_LBUTTONDOWN:
5254                 case WM_MBUTTONDOWN:
5255                 case WM_RBUTTONDOWN:
5256                 case WM_KEYDOWN:
5257                 {
5258                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5259                         return 0;
5260                 }
5261
5262                 case WM_MOUSEMOVE:
5263                 {
5264                         if (iMouse)
5265                         {
5266                                 dx = LOWORD(lParam) - xMouse;
5267                                 dy = HIWORD(lParam) - yMouse;
5268
5269                                 if (dx < 0) dx = -dx;
5270                                 if (dy < 0) dy = -dy;
5271
5272                                 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
5273                                 {
5274                                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5275                                 }
5276                         }
5277
5278                         /* Save last location */
5279                         iMouse = 1;
5280                         xMouse = LOWORD(lParam);
5281                         yMouse = HIWORD(lParam);
5282
5283                         return 0;
5284                 }
5285
5286                 case WM_CLOSE:
5287                 {
5288                         DestroyWindow(hwndSaver);
5289                         hwndSaver = NULL;
5290                         return 0;
5291                 }
5292         }
5293
5294         /* Oops */
5295         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5296 }
5297
5298 #endif /* USE_SAVER */
5299
5300
5301
5302
5303
5304 /*** Temporary Hooks ***/
5305
5306
5307 /*
5308  * Display warning message (see "z-util.c")
5309  */
5310 static void hack_plog(cptr str)
5311 {
5312         /* Give a warning */
5313         if (str)
5314         {
5315 #ifdef JP
5316                 MessageBox(NULL, str, "·Ù¹ð¡ª",
5317                            MB_ICONEXCLAMATION | MB_OK);
5318 #else
5319                 MessageBox(NULL, str, "Warning",
5320                            MB_ICONEXCLAMATION | MB_OK);
5321 #endif
5322
5323         }
5324 }
5325
5326
5327 /*
5328  * Display error message and quit (see "z-util.c")
5329  */
5330 static void hack_quit(cptr str)
5331 {
5332         /* Give a warning */
5333         if (str)
5334         {
5335 #ifdef JP
5336                 MessageBox(NULL, str, "¥¨¥é¡¼¡ª",
5337                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5338 #else
5339                 MessageBox(NULL, str, "Error",
5340                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5341 #endif
5342
5343         }
5344
5345         /* Unregister the classes */
5346         UnregisterClass(AppName, hInstance);
5347
5348         /* Destroy the icon */
5349         if (hIcon) DestroyIcon(hIcon);
5350
5351         /* Exit */
5352         exit(0);
5353 }
5354
5355
5356
5357 /*** Various hooks ***/
5358
5359
5360 /*
5361  * Display warning message (see "z-util.c")
5362  */
5363 static void hook_plog(cptr str)
5364 {
5365         /* Warning */
5366         if (str)
5367         {
5368 #ifdef JP
5369                 MessageBox(data[0].w, str, "·Ù¹ð¡ª",
5370                            MB_ICONEXCLAMATION | MB_OK);
5371 #else
5372                 MessageBox(data[0].w, str, "Warning",
5373                            MB_ICONEXCLAMATION | MB_OK);
5374 #endif
5375
5376         }
5377 }
5378
5379
5380 /*
5381  * Display error message and quit (see "z-util.c")
5382  */
5383 static void hook_quit(cptr str)
5384 {
5385         int i;
5386
5387
5388         /* Give a warning */
5389         if (str)
5390         {
5391 #ifdef JP
5392                 MessageBox(data[0].w, str, "¥¨¥é¡¼¡ª",
5393                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5394 #else
5395                 MessageBox(data[0].w, str, "Error",
5396                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5397 #endif
5398
5399         }
5400
5401
5402         /* Save the preferences */
5403         save_prefs();
5404
5405
5406         /*** Could use 'Term_nuke_win()' XXX XXX XXX */
5407
5408         /* Destroy all windows */
5409         for (i = MAX_TERM_DATA - 1; i >= 0; --i)
5410         {
5411                 term_force_font(&data[i], NULL);
5412                 if (data[i].font_want) string_free(data[i].font_want);
5413                 if (data[i].w) DestroyWindow(data[i].w);
5414                 data[i].w = 0;
5415         }
5416
5417         /* Free the bitmap stuff */
5418 #ifdef USE_GRAPHICS
5419         if (infGraph.hPalette) DeleteObject(infGraph.hPalette);
5420         if (infGraph.hBitmap) DeleteObject(infGraph.hBitmap);
5421
5422         if (infMask.hPalette) DeleteObject(infMask.hPalette);
5423         if (infMask.hBitmap) DeleteObject(infMask.hBitmap);
5424
5425 #endif /* USE_GRAPHICS */
5426
5427         /*** Free some other stuff ***/
5428
5429         DeleteObject(hbrYellow);
5430
5431         /* bg */
5432         delete_bg();
5433
5434         if (hPal) DeleteObject(hPal);
5435
5436         UnregisterClass(AppName, hInstance);
5437
5438         if (hIcon) DestroyIcon(hIcon);
5439
5440         exit(0);
5441 }
5442
5443
5444
5445 /*** Initialize ***/
5446
5447
5448 /*
5449  * Init some stuff
5450  */
5451 static void init_stuff(void)
5452 {
5453         int i;
5454
5455         char path[1024];
5456
5457
5458         /* Get program name with full path */
5459         GetModuleFileName(hInstance, path, 512);
5460
5461         /* Save the "program name" XXX XXX XXX */
5462         argv0 = path;
5463
5464         /* Get the name of the "*.ini" file */
5465         strcpy(path + strlen(path) - 4, ".INI");
5466
5467         /* Save the the name of the ini-file */
5468         ini_file = string_make(path);
5469
5470         /* Analyze the path */
5471         i = strlen(path);
5472
5473         /* Get the path */
5474         for (; i > 0; i--)
5475         {
5476                 if (path[i] == '\\')
5477                 {
5478                         /* End of path */
5479                         break;
5480                 }
5481         }
5482
5483         /* Add "lib" to the path */
5484         strcpy(path + i + 1, "lib\\");
5485
5486         /* Validate the path */
5487         validate_dir(path, TRUE);
5488
5489         /* Init the file paths */
5490         init_file_paths(path);
5491
5492         /* Hack -- Validate the paths */
5493         validate_dir(ANGBAND_DIR_APEX, FALSE);
5494         validate_dir(ANGBAND_DIR_BONE, FALSE);
5495
5496         /* Allow missing 'edit' directory */
5497         if (!check_dir(ANGBAND_DIR_EDIT))
5498         {
5499                 /* Must have 'data'! */
5500                 validate_dir(ANGBAND_DIR_DATA, TRUE);
5501         }
5502         else
5503         {
5504                 /* Don't need 'data' */
5505                 validate_dir(ANGBAND_DIR_DATA, FALSE);
5506         }
5507
5508         validate_dir(ANGBAND_DIR_FILE, TRUE);
5509         validate_dir(ANGBAND_DIR_HELP, FALSE);
5510         validate_dir(ANGBAND_DIR_INFO, FALSE);
5511         validate_dir(ANGBAND_DIR_PREF, TRUE);
5512         validate_dir(ANGBAND_DIR_SAVE, FALSE);
5513         validate_dir(ANGBAND_DIR_USER, TRUE);
5514         validate_dir(ANGBAND_DIR_XTRA, TRUE);
5515
5516         /* Build the filename */
5517 #ifdef JP
5518         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news_j.txt");
5519 #else
5520         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news.txt");
5521 #endif
5522
5523
5524         /* Hack -- Validate the "news.txt" file */
5525         validate_file(path);
5526
5527
5528 #if 0 /* #ifndef JP */
5529         /* Build the "font" path */
5530         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font");
5531
5532         /* Allocate the path */
5533         ANGBAND_DIR_XTRA_FONT = string_make(path);
5534
5535         /* Validate the "font" directory */
5536         validate_dir(ANGBAND_DIR_XTRA_FONT, TRUE);
5537
5538         /* Build the filename */
5539         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
5540
5541         /* Hack -- Validate the basic font */
5542         validate_file(path);
5543 #endif
5544
5545
5546 #ifdef USE_GRAPHICS
5547
5548         /* Build the "graf" path */
5549         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
5550
5551         /* Allocate the path */
5552         ANGBAND_DIR_XTRA_GRAF = string_make(path);
5553
5554         /* Validate the "graf" directory */
5555         validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
5556
5557 #endif /* USE_GRAPHICS */
5558
5559
5560 #ifdef USE_SOUND
5561
5562         /* Build the "sound" path */
5563         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
5564
5565         /* Allocate the path */
5566         ANGBAND_DIR_XTRA_SOUND = string_make(path);
5567
5568         /* Validate the "sound" directory */
5569         validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
5570
5571 #endif /* USE_SOUND */
5572
5573 #ifdef USE_MUSIC
5574
5575         /* Build the "music" path */
5576         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
5577
5578         /* Allocate the path */
5579         ANGBAND_DIR_XTRA_MUSIC = string_make(path);
5580
5581         /* Validate the "music" directory */
5582         validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
5583
5584 #endif /* USE_MUSIC */
5585
5586         /* Build the "help" path */
5587         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
5588
5589         /* Allocate the path */
5590         ANGBAND_DIR_XTRA_HELP = string_make(path);
5591
5592         /* Validate the "help" directory */
5593         /* validate_dir(ANGBAND_DIR_XTRA_HELP); */
5594 }
5595
5596 bool is_already_running()
5597 {
5598         bool result = FALSE;
5599         HANDLE hMutex;
5600
5601         hMutex = CreateMutex(NULL, TRUE, VERSION_NAME);
5602         if (GetLastError() == ERROR_ALREADY_EXISTS)
5603         {
5604                 result = TRUE;
5605         }
5606         return result;
5607 }
5608
5609
5610 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
5611                        LPSTR lpCmdLine, int nCmdShow)
5612 {
5613         int i;
5614
5615         WNDCLASS wc;
5616         HDC hdc;
5617         MSG msg;
5618
5619         setlocale( LC_ALL, "ja_JP" );
5620
5621         /* Unused */
5622         (void)nCmdShow;
5623
5624         /* Save globally */
5625         hInstance = hInst;
5626         
5627         
5628         /* Prevent multiple run */
5629         if (is_already_running())
5630         {
5631                 MessageBox(NULL,
5632                                 _("ÊѶòÈÚÅܤϤ¹¤Ç¤Ëµ¯Æ°¤·¤Æ¤¤¤Þ¤¹¡£", "Hengband is already running."), 
5633                                 _("¥¨¥é¡¼¡ª", "Error") ,
5634                                 MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5635                 return FALSE;
5636         }
5637
5638         /* Initialize */
5639         if (hPrevInst == NULL)
5640         {
5641                 wc.style         = CS_CLASSDC;
5642                 wc.lpfnWndProc   = AngbandWndProc;
5643                 wc.cbClsExtra    = 0;
5644                 wc.cbWndExtra    = 4; /* one long pointer to term_data */
5645                 wc.hInstance     = hInst;
5646                 wc.hIcon         = hIcon = LoadIcon(hInst, AppName);
5647                 wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
5648                 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
5649                 wc.lpszMenuName  = AppName;
5650                 wc.lpszClassName = AppName;
5651
5652                 if (!RegisterClass(&wc)) exit(1);
5653
5654                 wc.lpfnWndProc   = AngbandListProc;
5655                 wc.lpszMenuName  = NULL;
5656                 wc.lpszClassName = AngList;
5657
5658                 if (!RegisterClass(&wc)) exit(2);
5659
5660 #ifdef USE_SAVER
5661
5662                 wc.style          = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
5663                 wc.lpfnWndProc    = AngbandSaverProc;
5664                 wc.hCursor        = NULL;
5665                 wc.lpszMenuName   = NULL;
5666                 wc.lpszClassName  = "WindowsScreenSaverClass";
5667
5668                 if (!RegisterClass(&wc)) exit(3);
5669
5670 #endif
5671
5672         }
5673
5674         /* Temporary hooks */
5675         plog_aux = hack_plog;
5676         quit_aux = hack_quit;
5677         core_aux = hack_quit;
5678
5679         /* Prepare the filepaths */
5680         init_stuff();
5681
5682         /* Initialize the keypress analyzer */
5683         for (i = 0; special_key_list[i]; ++i)
5684         {
5685                 special_key[special_key_list[i]] = TRUE;
5686         }
5687         /* Initialize the keypress analyzer */
5688         for (i = 0; ignore_key_list[i]; ++i)
5689         {
5690                 ignore_key[ignore_key_list[i]] = TRUE;
5691         }
5692
5693         /* Determine if display is 16/256/true color */
5694         hdc = GetDC(NULL);
5695         colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
5696         paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
5697         ReleaseDC(NULL, hdc);
5698
5699         /* Initialize the colors */
5700         for (i = 0; i < 256; i++)
5701         {
5702                 byte rv, gv, bv;
5703
5704                 /* Extract desired values */
5705                 rv = angband_color_table[i][1];
5706                 gv = angband_color_table[i][2];
5707                 bv = angband_color_table[i][3];
5708
5709                 /* Extract the "complex" code */
5710                 win_clr[i] = PALETTERGB(rv, gv, bv);
5711
5712                 /* Save the "simple" code */
5713                 angband_color_table[i][0] = win_pal[i];
5714         }
5715
5716         /* Prepare the windows */
5717         init_windows();
5718
5719         /* bg */
5720         init_bg();
5721
5722         /* Activate hooks */
5723         plog_aux = hook_plog;
5724         quit_aux = hook_quit;
5725         core_aux = hook_quit;
5726
5727         /* Set the system suffix */
5728         ANGBAND_SYS = "win";
5729
5730         /* Set the keyboard suffix */
5731         if (7 != GetKeyboardType(0))
5732                 ANGBAND_KEYBOARD = "0";
5733         else
5734         {
5735                 /* Japanese keyboard */
5736                 switch (GetKeyboardType(1))
5737                 {
5738                 case 0x0D01: case 0x0D02:
5739                 case 0x0D03: case 0x0D04:
5740                 case 0x0D05: case 0x0D06:
5741                         /* NEC PC-98x1 */
5742                         ANGBAND_KEYBOARD = "NEC98";
5743                         break;
5744                 default:
5745                         /* PC/AT */
5746                         ANGBAND_KEYBOARD = "JAPAN";
5747                 }
5748         }
5749
5750         /* Catch nasty signals */
5751         signals_init();
5752
5753         /* Initialize */
5754         init_angband();
5755
5756         /* We are now initialized */
5757         initialized = TRUE;
5758 #ifdef CHUUKEI
5759         if(lpCmdLine[0] == '-'){
5760           switch(lpCmdLine[1])
5761           {
5762           case 'p':
5763           case 'P':
5764             {
5765               if (!lpCmdLine[2]) break;
5766               chuukei_server = TRUE;
5767               if(connect_chuukei_server(&lpCmdLine[2])<0){
5768                 msg_print("connect fail");
5769                 return 0;
5770               }
5771               msg_print("connect");
5772               msg_print(NULL);
5773               break;
5774             }
5775
5776           case 'c':
5777           case 'C':
5778             {
5779               if (!lpCmdLine[2]) break;
5780               chuukei_client = TRUE;
5781               connect_chuukei_server(&lpCmdLine[2]);
5782               play_game(FALSE);
5783               quit(NULL);
5784               return 0;
5785             }
5786           case 'X':
5787           case 'x':
5788             {
5789               if (!lpCmdLine[2]) break;
5790               prepare_browse_movie(&lpCmdLine[2]);
5791               play_game(FALSE);
5792               quit(NULL);
5793               return 0;
5794             }
5795           }
5796         }
5797 #endif
5798
5799 #ifdef CHUUKEI
5800         /* Did the user double click on a save file? */
5801         if(!chuukei_server) check_for_save_file(lpCmdLine);
5802 #else
5803         /* Did the user double click on a save file? */
5804         check_for_save_file(lpCmdLine);
5805 #endif
5806
5807         /* Prompt the user */
5808 #ifdef JP
5809         prt("[¥Õ¥¡¥¤¥ë] ¥á¥Ë¥å¡¼¤Î [¿·µ¬] ¤Þ¤¿¤Ï [³«¤¯] ¤òÁªÂò¤·¤Æ¤¯¤À¤µ¤¤¡£", 23, 8);
5810 #else
5811         prt("[Choose 'New' or 'Open' from the 'File' menu]", 23, 17);
5812 #endif
5813
5814         Term_fresh();
5815
5816         /* Process messages forever */
5817         while (GetMessage(&msg, NULL, 0, 0))
5818         {
5819                 TranslateMessage(&msg);
5820                 DispatchMessage(&msg);
5821         }
5822
5823         /* Paranoia */
5824         quit(NULL);
5825
5826         /* Paranoia */
5827         return (0);
5828 }
5829
5830
5831 #endif /* WINDOWS */
5832