OSDN Git Service

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