OSDN Git Service

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