OSDN Git Service

dungeon_turnの過剰経過による、NASTY_MONSTER発生率と階層上昇を有効な形に実装し直した上で、若干調整。
[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, s+i, 1, NULL);
2649
2650                                 /* Advance */
2651                                 rc.left += td->tile_wid;
2652                                 rc.right += td->tile_wid;
2653                         }
2654 #else
2655 #if 1
2656                         if (*(s+i)==127){
2657                                 oldBrush = SelectObject(hdc, myBrush);
2658                                 oldPen = SelectObject(hdc, GetStockObject(NULL_PEN) );
2659
2660                                 /* Dump the wall */
2661                                 Rectangle(hdc, rc.left, rc.top, rc.right+1, rc.bottom+1);
2662
2663                                 SelectObject(hdc, oldBrush);
2664                                 SelectObject(hdc, oldPen);
2665
2666                                 /* Advance */
2667                                 rc.left += td->tile_wid;
2668                                 rc.right += td->tile_wid;
2669                         } else {
2670                                 /* Dump the text */
2671                                 ExtTextOut(hdc, rc.left, rc.top, ETO_CLIPPED, &rc,
2672                                        s+i, 1, NULL);
2673
2674                                 /* Advance */
2675                                 rc.left += td->tile_wid;
2676                                 rc.right += td->tile_wid;
2677                         }
2678 #else
2679                         /* Dump the text */
2680                         ExtTextOut(hdc, rc.left, rc.top, 0, &rc,
2681                                    s+i, 1, NULL);
2682
2683                         /* Advance */
2684                         rc.left += td->tile_wid;
2685                         rc.right += td->tile_wid;
2686 #endif
2687 #endif
2688
2689                 }
2690         }
2691
2692         /* Normal size */
2693         else
2694         {
2695                 /* Dump the text */
2696                 ExtTextOut(hdc, rc.left, rc.top, ETO_OPAQUE | ETO_CLIPPED, &rc,
2697                            s, n, NULL);
2698         }
2699
2700         /* Release DC */
2701         ReleaseDC(td->w, hdc);
2702
2703         /* Success */
2704         return 0;
2705 }
2706
2707
2708 /*
2709  * Low level graphics.  Assumes valid input.
2710  *
2711  * Draw an array of "special" attr/char pairs at the given location.
2712  *
2713  * We use the "Term_pict_win()" function for "graphic" data, which are
2714  * encoded by setting the "high-bits" of both the "attr" and the "char"
2715  * data.  We use the "attr" to represent the "row" of the main bitmap,
2716  * and the "char" to represent the "col" of the main bitmap.  The use
2717  * of this function is induced by the "higher_pict" flag.
2718  *
2719  * If "graphics" is not available, we simply "wipe" the given grids.
2720  */
2721 static errr Term_pict_win(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp)
2722 {
2723         term_data *td = (term_data*)(Term->data);
2724
2725 #ifdef USE_GRAPHICS
2726
2727         int i;
2728         int x1, y1, w1, h1;
2729         int x2, y2, w2, h2, tw2;
2730         int x3, y3;
2731
2732         HDC hdcMask;
2733
2734         HDC hdc;
2735         HDC hdcSrc;
2736         HBITMAP hbmSrcOld;
2737
2738         /* Paranoia */
2739         if (!use_graphics)
2740         {
2741                 /* Erase the grids */
2742                 return (Term_wipe_win(x, y, n));
2743         }
2744
2745         /* Size of bitmap cell */
2746         w1 = infGraph.CellWidth;
2747         h1 = infGraph.CellHeight;
2748
2749         /* Size of window cell */
2750         if (td->map_active)
2751         {
2752                 w2 = td->map_tile_wid;
2753                 h2 = td->map_tile_hgt;
2754         }
2755         else
2756         {
2757                 w2 = td->tile_wid;
2758                 h2 = td->tile_hgt;
2759                 tw2 = w2;
2760
2761                 /* big tile mode */
2762                 if (use_bigtile) tw2 *= 2;
2763         }
2764
2765         /* Location of window cell */
2766         x2 = x * w2 + td->size_ow1;
2767         y2 = y * h2 + td->size_oh1;
2768
2769         /* Info */
2770         hdc = GetDC(td->w);
2771
2772         /* More info */
2773         hdcSrc = CreateCompatibleDC(hdc);
2774         hbmSrcOld = SelectObject(hdcSrc, infGraph.hBitmap);
2775
2776         if (arg_graphics == GRAPHICS_ADAM_BOLT)
2777         {
2778                 hdcMask = CreateCompatibleDC(hdc);
2779                 SelectObject(hdcMask, infMask.hBitmap);
2780         }
2781
2782         /* Draw attr/char pairs */
2783         for (i = 0; i < n; i++, x2 += w2)
2784         {
2785                 byte a = ap[i];
2786                 char c = cp[i];
2787
2788                 /* Extract picture */
2789                 int row = (a & 0x7F);
2790                 int col = (c & 0x7F);
2791
2792                 /* Location of bitmap cell */
2793                 x1 = col * w1;
2794                 y1 = row * h1;
2795
2796                 if (arg_graphics == GRAPHICS_ADAM_BOLT)
2797                 {
2798                         x3 = (tcp[i] & 0x7F) * w1;
2799                         y3 = (tap[i] & 0x7F) * h1;
2800
2801                         /* Perfect size */
2802                         if ((w1 == tw2) && (h1 == h2))
2803                         {
2804                                 /* Copy the terrain picture from the bitmap to the window */
2805                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, SRCCOPY);
2806
2807                                 /* Mask out the tile */
2808                                 BitBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, SRCAND);
2809
2810                                 /* Draw the tile */
2811                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCPAINT);
2812                         }
2813
2814                         /* Need to stretch */
2815                         else
2816                         {
2817                                 /* Set the correct mode for stretching the tiles */
2818                                 SetStretchBltMode(hdc, COLORONCOLOR);
2819
2820                                 /* Copy the terrain picture from the bitmap to the window */
2821                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x3, y3, w1, h1, SRCCOPY);
2822
2823                                 /* Only draw if terrain and overlay are different */
2824                                 if ((x1 != x3) || (y1 != y3))
2825                                 {
2826                                         /* Mask out the tile */
2827                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcMask, x1, y1, w1, h1, SRCAND);
2828
2829                                         /* Draw the tile */
2830                                         StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCPAINT);
2831                                 }
2832                         }
2833                 }
2834                 else
2835                 {
2836                         /* Perfect size */
2837                         if ((w1 == tw2) && (h1 == h2))
2838                         {
2839                                 /* Copy the picture from the bitmap to the window */
2840                                 BitBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, SRCCOPY);
2841                         }
2842
2843                         /* Need to stretch */
2844                         else
2845                         {
2846                                 /* Set the correct mode for stretching the tiles */
2847                                 SetStretchBltMode(hdc, COLORONCOLOR);
2848
2849                                 /* Copy the picture from the bitmap to the window */
2850                                 StretchBlt(hdc, x2, y2, tw2, h2, hdcSrc, x1, y1, w1, h1, SRCCOPY);
2851                         }
2852                 }
2853         }
2854
2855         /* Release */
2856         SelectObject(hdcSrc, hbmSrcOld);
2857         DeleteDC(hdcSrc);
2858
2859         if (arg_graphics == GRAPHICS_ADAM_BOLT)
2860         {
2861                 /* Release */
2862                 SelectObject(hdcMask, hbmSrcOld);
2863                 DeleteDC(hdcMask);
2864         }
2865
2866         /* Release */
2867         ReleaseDC(td->w, hdc);
2868
2869 #else /* USE_GRAPHICS */
2870
2871         /* Just erase this grid */
2872         return (Term_wipe_win(x, y, n));
2873
2874 #endif /* USE_GRAPHICS */
2875
2876         /* Success */
2877         return 0;
2878 }
2879
2880
2881 static void windows_map(void)
2882 {
2883         term_data *td = &data[0];
2884         byte a, c;
2885         int x, min_x, max_x;
2886         int y, min_y, max_y;
2887
2888         byte ta, tc;
2889
2890         /* Only in graphics mode */
2891         if (!use_graphics) return;
2892
2893         /* Clear screen */
2894         Term_xtra_win_clear();
2895
2896         td->map_tile_wid = (td->tile_wid * td->cols) / MAX_WID;
2897         td->map_tile_hgt = (td->tile_hgt * td->rows) / MAX_HGT;
2898         td->map_active = TRUE;
2899
2900         {
2901                 min_x = 0;
2902                 min_y = 0;
2903                 max_x = cur_wid;
2904                 max_y = cur_hgt;
2905         }
2906
2907         /* Draw the map */
2908         for (x = min_x; x < max_x; x++)
2909         {
2910                 for (y = min_y; y < max_y; y++)
2911                 {
2912                         map_info(y, x, &a, (char*)&c, &ta, (char*)&tc);
2913
2914                         /* Ignore non-graphics */
2915                         if ((a & 0x80) && (c & 0x80))
2916                         {
2917                                 Term_pict_win(x - min_x, y - min_y, 1, &a, &c, &ta, &tc);
2918                         }
2919                 }
2920         }
2921
2922         /* Hilite the player */
2923         Term_curs_win(px - min_x, py - min_y);
2924
2925         /* Wait for a keypress, flush key buffer */
2926         Term_inkey(&c, TRUE, TRUE);
2927         Term_flush();
2928
2929         /* Switch off the map display */
2930         td->map_active = FALSE;
2931
2932         /* Restore screen */
2933         Term_xtra_win_clear();
2934         Term_redraw();
2935 }
2936
2937
2938 /*** Other routines ***/
2939
2940
2941 /*
2942  * Create and initialize a "term_data" given a title
2943  */
2944 static void term_data_link(term_data *td)
2945 {
2946         term *t = &td->t;
2947
2948         /* Initialize the term */
2949         term_init(t, td->cols, td->rows, td->keys);
2950
2951         /* Use a "software" cursor */
2952         t->soft_cursor = TRUE;
2953
2954         /* Use "Term_pict" for "graphic" data */
2955         t->higher_pict = TRUE;
2956
2957         /* Erase with "white space" */
2958         t->attr_blank = TERM_WHITE;
2959         t->char_blank = ' ';
2960
2961 #if 0
2962         /* Prepare the init/nuke hooks */
2963         t->init_hook = Term_init_win;
2964         t->nuke_hook = Term_nuke_win;
2965 #endif
2966
2967         /* Prepare the template hooks */
2968         t->user_hook = Term_user_win;
2969         t->xtra_hook = Term_xtra_win;
2970         t->curs_hook = Term_curs_win;
2971         t->bigcurs_hook = Term_bigcurs_win;
2972         t->wipe_hook = Term_wipe_win;
2973         t->text_hook = Term_text_win;
2974         t->pict_hook = Term_pict_win;
2975
2976         /* Remember where we came from */
2977         t->data = (vptr)(td);
2978 }
2979
2980
2981 /*
2982  * Create the windows
2983  *
2984  * First, instantiate the "default" values, then read the "ini_file"
2985  * to over-ride selected values, then create the windows, and fonts.
2986  *
2987  * Must use SW_SHOW not SW_SHOWNA, since on 256 color display
2988  * must make active to realize the palette.  XXX XXX XXX
2989  */
2990 static void init_windows(void)
2991 {
2992         int i;
2993
2994         term_data *td;
2995
2996 #if 0 /* #ifndef JP */
2997         char buf[1024];
2998 #endif
2999
3000         /* Main window */
3001         td = &data[0];
3002         WIPE(td, term_data);
3003 #ifdef JP
3004         td->s = "ÊѶòÈÚÅÜ";
3005 #else
3006         td->s = angband_term_name[0];
3007 #endif
3008
3009         td->keys = 1024;
3010         td->rows = 24;
3011         td->cols = 80;
3012         td->visible = TRUE;
3013         td->size_ow1 = 2;
3014         td->size_ow2 = 2;
3015         td->size_oh1 = 2;
3016         td->size_oh2 = 2;
3017         td->pos_x = 7 * 30;
3018         td->pos_y = 7 * 20;
3019         td->posfix = FALSE;
3020 #if 1 /* #ifdef JP */
3021         td->bizarre = TRUE;
3022 #endif
3023         /* Sub windows */
3024         for (i = 1; i < MAX_TERM_DATA; i++)
3025         {
3026                 td = &data[i];
3027                 WIPE(td, term_data);
3028                 td->s = angband_term_name[i];
3029                 td->keys = 16;
3030                 td->rows = 24;
3031                 td->cols = 80;
3032                 td->visible = FALSE;
3033                 td->size_ow1 = 1;
3034                 td->size_ow2 = 1;
3035                 td->size_oh1 = 1;
3036                 td->size_oh2 = 1;
3037                 td->pos_x = (7 - i) * 30;
3038                 td->pos_y = (7 - i) * 20;
3039                 td->posfix = FALSE;
3040 #if 1 /* #ifdef JP */
3041                         td->bizarre = TRUE;
3042 #endif
3043         }
3044
3045
3046         /* Load prefs */
3047         load_prefs();
3048
3049
3050         /* Main window (need these before term_getsize gets called) */
3051         td = &data[0];
3052         td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU |
3053                        WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION |
3054                        WS_VISIBLE);
3055         td->dwExStyle = 0;
3056         td->visible = TRUE;
3057
3058         /* Sub windows (need these before term_getsize gets called) */
3059         for (i = 1; i < MAX_TERM_DATA; i++)
3060         {
3061                 td = &data[i];
3062                 td->dwStyle = (WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU);
3063                 td->dwExStyle = (WS_EX_TOOLWINDOW);
3064         }
3065
3066
3067         /* All windows */
3068         for (i = 0; i < MAX_TERM_DATA; i++)
3069         {
3070                 td = &data[i];
3071
3072 #if 1 /* #ifdef JP */
3073                 strncpy(td->lf.lfFaceName, td->font_want, LF_FACESIZE);
3074                 td->lf.lfCharSet = DEFAULT_CHARSET;
3075                 td->lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
3076                 /* Activate the chosen font */
3077                 term_force_font(td, NULL);
3078                 td->tile_wid = td->font_wid;
3079                 td->tile_hgt = td->font_hgt;
3080 #else
3081                 /* Access the standard font file */
3082                 path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, td->font_want);
3083
3084                 /* Activate the chosen font */
3085                 if (term_force_font(td, buf))
3086                 {
3087                         /* Access the standard font file */
3088                         path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
3089
3090                         /* Force the use of that font */
3091                         (void)term_force_font(td, buf);
3092
3093                         /* Oops */
3094                         td->tile_wid = 8;
3095                         td->tile_hgt = 13;
3096
3097                         /* Assume not bizarre */
3098                         td->bizarre = FALSE;
3099                 }
3100 #endif
3101
3102
3103                 /* Analyze the font */
3104                 term_getsize(td);
3105
3106                 /* Resize the window */
3107                 term_window_resize(td);
3108         }
3109
3110
3111         /* Sub windows (reverse order) */
3112         for (i = MAX_TERM_DATA - 1; i >= 1; --i)
3113         {
3114                 td = &data[i];
3115
3116                 my_td = td;
3117                 td->w = CreateWindowEx(td->dwExStyle, AngList,
3118                                        td->s, td->dwStyle,
3119                                        td->pos_x, td->pos_y,
3120                                        td->size_wid, td->size_hgt,
3121                                        HWND_DESKTOP, NULL, hInstance, NULL);
3122                 my_td = NULL;
3123 #ifdef JP
3124                 if (!td->w) quit("¥µ¥Ö¥¦¥£¥ó¥É¥¦¤ËºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3125 #else
3126                 if (!td->w) quit("Failed to create sub-window");
3127 #endif
3128
3129
3130                 if (td->visible)
3131                 {
3132                         td->size_hack = TRUE;
3133                         ShowWindow(td->w, SW_SHOW);
3134                         td->size_hack = FALSE;
3135                 }
3136
3137                 term_data_link(td);
3138                 angband_term[i] = &td->t;
3139
3140                 if (td->visible)
3141                 {
3142                         /* Activate the window */
3143                         SetActiveWindow(td->w);
3144                 }
3145
3146                 if (data[i].posfix)
3147                 {
3148                         term_window_pos(&data[i], HWND_TOPMOST);
3149                 }
3150                 else
3151                 {
3152                         term_window_pos(&data[i], td->w);
3153                 }
3154         }
3155
3156
3157         /* Main window */
3158         td = &data[0];
3159
3160         /* Main window */
3161         my_td = td;
3162         td->w = CreateWindowEx(td->dwExStyle, AppName,
3163                                td->s, td->dwStyle,
3164                                td->pos_x, td->pos_y,
3165                                td->size_wid, td->size_hgt,
3166                                HWND_DESKTOP, NULL, hInstance, NULL);
3167         my_td = NULL;
3168 #ifdef JP
3169         if (!td->w) quit("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÎºîÀ®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿");
3170 #else
3171         if (!td->w) quit("Failed to create Angband window");
3172 #endif
3173
3174
3175         term_data_link(td);
3176         angband_term[0] = &td->t;
3177         normsize.x = td->cols;
3178         normsize.y = td->rows;
3179
3180         /* Activate the main window */
3181         if (win_maximized) ShowWindow(td->w, SW_SHOWMAXIMIZED);
3182         else ShowWindow(td->w, SW_SHOW);
3183
3184         /* Bring main window back to top */
3185         SetWindowPos(td->w, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
3186
3187
3188         /* New palette XXX XXX XXX */
3189         (void)new_palette();
3190
3191
3192         /* Create a "brush" for drawing the "cursor" */
3193         hbrYellow = CreateSolidBrush(win_clr[TERM_YELLOW]);
3194
3195
3196         /* Process pending messages */
3197         (void)Term_xtra_win_flush();
3198 }
3199
3200
3201
3202 /*
3203  * Prepare the menus
3204  */
3205 static void setup_menus(void)
3206 {
3207         int i;
3208
3209         HMENU hm = GetMenu(data[0].w);
3210
3211
3212         /* Menu "File", Disable all */
3213         EnableMenuItem(hm, IDM_FILE_NEW,
3214                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3215         EnableMenuItem(hm, IDM_FILE_OPEN,
3216                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3217         EnableMenuItem(hm, IDM_FILE_SAVE,
3218                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3219         EnableMenuItem(hm, IDM_FILE_EXIT,
3220                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3221         EnableMenuItem(hm, IDM_FILE_SCORE,
3222                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3223
3224
3225         /* No character available */
3226         if (!character_generated)
3227         {
3228                 /* Menu "File", Item "New" */
3229                 EnableMenuItem(hm, IDM_FILE_NEW, MF_BYCOMMAND | MF_ENABLED);
3230
3231                 /* Menu "File", Item "Open" */
3232                 EnableMenuItem(hm, IDM_FILE_OPEN, MF_BYCOMMAND | MF_ENABLED);
3233         }
3234
3235         /* A character available */
3236         if (character_generated)
3237         {
3238                 /* Menu "File", Item "Save" */
3239                 EnableMenuItem(hm, IDM_FILE_SAVE,
3240                            MF_BYCOMMAND | MF_ENABLED);
3241         }
3242
3243         /* Menu "File", Item "Exit" */
3244         EnableMenuItem(hm, IDM_FILE_EXIT,
3245                        MF_BYCOMMAND | MF_ENABLED);
3246
3247         EnableMenuItem(hm, IDM_FILE_SCORE,
3248                        MF_BYCOMMAND | MF_ENABLED);
3249
3250
3251         /* Menu "Window::Visibility" */
3252         for (i = 0; i < MAX_TERM_DATA; i++)
3253         {
3254                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3255                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3256
3257                 CheckMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3258                               (data[i].visible ? MF_CHECKED : MF_UNCHECKED));
3259
3260                 EnableMenuItem(hm, IDM_WINDOW_VIS_0 + i,
3261                                MF_BYCOMMAND | MF_ENABLED);
3262         }
3263
3264         /* Menu "Window::Font" */
3265         for (i = 0; i < MAX_TERM_DATA; i++)
3266         {
3267                 EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3268                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3269
3270                 if (data[i].visible)
3271                 {
3272                         EnableMenuItem(hm, IDM_WINDOW_FONT_0 + i,
3273                                        MF_BYCOMMAND | MF_ENABLED);
3274                 }
3275         }
3276
3277         /* Menu "Window::Window Position Fix" */
3278         for (i = 0; i < MAX_TERM_DATA; i++)
3279         {
3280                 EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3281                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3282
3283                 CheckMenuItem(hm, IDM_WINDOW_POS_0 + i,
3284                               (data[i].posfix ? MF_CHECKED : MF_UNCHECKED));
3285
3286                 if (data[i].visible)
3287                 {
3288                         EnableMenuItem(hm, IDM_WINDOW_POS_0 + i,
3289                                        MF_BYCOMMAND | MF_ENABLED);
3290                 }
3291         }
3292
3293         /* Menu "Window::Bizarre Display" */
3294         for (i = 0; i < MAX_TERM_DATA; i++)
3295         {
3296                 EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3297                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3298
3299                 CheckMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3300                               (data[i].bizarre ? MF_CHECKED : MF_UNCHECKED));
3301
3302                 if (data[i].visible)
3303                 {
3304                         EnableMenuItem(hm, IDM_WINDOW_BIZ_0 + i,
3305                                    MF_BYCOMMAND | MF_ENABLED);
3306
3307                 }
3308         }
3309
3310         /* Menu "Window::Increase Tile Width" */
3311         for (i = 0; i < MAX_TERM_DATA; i++)
3312         {
3313                 EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3314                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3315
3316                 if (data[i].visible)
3317                 {
3318                         EnableMenuItem(hm, IDM_WINDOW_I_WID_0 + i,
3319                                    MF_BYCOMMAND | MF_ENABLED);
3320
3321                 }
3322         }
3323
3324         /* Menu "Window::Decrease Tile Width" */
3325         for (i = 0; i < MAX_TERM_DATA; i++)
3326         {
3327                 EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3328                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3329
3330                 if (data[i].visible)
3331                 {
3332                         EnableMenuItem(hm, IDM_WINDOW_D_WID_0 + i,
3333                                    MF_BYCOMMAND | MF_ENABLED);
3334
3335                 }
3336         }
3337
3338         /* Menu "Window::Increase Tile Height" */
3339         for (i = 0; i < MAX_TERM_DATA; i++)
3340         {
3341                 EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3342                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3343
3344                 if (data[i].visible)
3345                 {
3346                         EnableMenuItem(hm, IDM_WINDOW_I_HGT_0 + i,
3347                                    MF_BYCOMMAND | MF_ENABLED);
3348
3349                 }
3350         }
3351
3352         /* Menu "Window::Decrease Tile Height" */
3353         for (i = 0; i < MAX_TERM_DATA; i++)
3354         {
3355                 EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3356                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3357
3358                 if (data[i].visible)
3359                 {
3360                         EnableMenuItem(hm, IDM_WINDOW_D_HGT_0 + i,
3361                                    MF_BYCOMMAND | MF_ENABLED);
3362
3363                 }
3364         }
3365
3366         /* Menu "Options", disable all */
3367         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3368                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3369         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3370                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3371         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3372                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3373         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE,
3374                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3375         EnableMenuItem(hm, IDM_OPTIONS_SOUND,
3376                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3377 #ifndef JP
3378         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3379                        MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3380 #endif
3381
3382         /* Menu "Options", Item "Map" */
3383         if (use_graphics != GRAPHICS_NONE)
3384                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP, MF_BYCOMMAND | MF_ENABLED);
3385         else
3386                 EnableMenuItem(GetMenu(data[0].w), IDM_OPTIONS_MAP,
3387                                MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
3388
3389         /* Menu "Options", update all */
3390         CheckMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS,
3391                       (arg_graphics == GRAPHICS_NONE ? MF_CHECKED : MF_UNCHECKED));
3392         CheckMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS,
3393                       (arg_graphics == GRAPHICS_ORIGINAL ? MF_CHECKED : MF_UNCHECKED));
3394         CheckMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS,
3395                       (arg_graphics == GRAPHICS_ADAM_BOLT ? MF_CHECKED : MF_UNCHECKED));
3396         CheckMenuItem(hm, IDM_OPTIONS_BIGTILE,
3397                       (arg_bigtile ? MF_CHECKED : MF_UNCHECKED));
3398         CheckMenuItem(hm, IDM_OPTIONS_SOUND,
3399                       (arg_sound ? MF_CHECKED : MF_UNCHECKED));
3400         CheckMenuItem(hm, IDM_OPTIONS_BG,
3401                       (use_bg ? MF_CHECKED : MF_UNCHECKED));
3402 #ifndef JP
3403         CheckMenuItem(hm, IDM_OPTIONS_SAVER,
3404                       (hwndSaver ? MF_CHECKED : MF_UNCHECKED));
3405 #endif
3406
3407 #ifdef USE_GRAPHICS
3408         /* Menu "Options", Item "Graphics" */
3409         EnableMenuItem(hm, IDM_OPTIONS_NO_GRAPHICS, MF_ENABLED);
3410         /* Menu "Options", Item "Graphics" */
3411         EnableMenuItem(hm, IDM_OPTIONS_OLD_GRAPHICS, MF_ENABLED);
3412         /* Menu "Options", Item "Graphics" */
3413         EnableMenuItem(hm, IDM_OPTIONS_NEW_GRAPHICS, MF_ENABLED);
3414         /* Menu "Options", Item "Graphics" */
3415         EnableMenuItem(hm, IDM_OPTIONS_BIGTILE, MF_ENABLED);
3416 #endif /* USE_GRAPHICS */
3417
3418 #ifdef USE_SOUND
3419         /* Menu "Options", Item "Sound" */
3420         EnableMenuItem(hm, IDM_OPTIONS_SOUND, MF_ENABLED);
3421 #endif /* USE_SOUND */
3422
3423 #ifdef USE_SAVER
3424         /* Menu "Options", Item "ScreenSaver" */
3425         EnableMenuItem(hm, IDM_OPTIONS_SAVER,
3426                        MF_BYCOMMAND | MF_ENABLED);
3427 #endif /* USE_SAVER */
3428 }
3429
3430
3431 /*
3432  * Check for double clicked (or dragged) savefile
3433  *
3434  * Apparently, Windows copies the entire filename into the first
3435  * piece of the "command line string".  Perhaps we should extract
3436  * the "basename" of that filename and append it to the "save" dir.
3437  */
3438 static void check_for_save_file(LPSTR cmd_line)
3439 {
3440         char *s;
3441
3442         /* First arg */
3443         s = cmd_line;
3444
3445         /* No args */
3446         if (!*s) return;
3447
3448         /* Extract filename */
3449         strcat(savefile, s);
3450
3451         /* Validate the file */
3452         validate_file(savefile);
3453
3454         /* Game in progress */
3455         game_in_progress = TRUE;
3456
3457         /* Play game */
3458         play_game(FALSE);
3459 }
3460
3461
3462 /*
3463  * Process a menu command
3464  */
3465 static void process_menus(WORD wCmd)
3466 {
3467         int i;
3468
3469         term_data *td;
3470
3471         OPENFILENAME ofn;
3472
3473         /* Analyze */
3474         switch (wCmd)
3475         {
3476                 /* New game */
3477                 case IDM_FILE_NEW:
3478                 {
3479                         if (!initialized)
3480                         {
3481 #ifdef JP
3482                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3483 #else
3484                                 plog("You cannot do that yet...");
3485 #endif
3486
3487                         }
3488                         else if (game_in_progress)
3489                         {
3490 #ifdef JP
3491                                 plog("¥×¥ì¥¤Ãæ¤Ï¿·¤·¤¤¥²¡¼¥à¤ò»Ï¤á¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3492 #else
3493                                 plog("You can't start a new game while you're still playing!");
3494 #endif
3495
3496                         }
3497                         else
3498                         {
3499                                 game_in_progress = TRUE;
3500                                 Term_flush();
3501                                 play_game(TRUE);
3502                                 quit(NULL);
3503                         }
3504                         break;
3505                 }
3506
3507                 /* Open game */
3508                 case IDM_FILE_OPEN:
3509                 {
3510                         if (!initialized)
3511                         {
3512 #ifdef JP
3513                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3514 #else
3515                                 plog("You cannot do that yet...");
3516 #endif
3517
3518                         }
3519                         else if (game_in_progress)
3520                         {
3521 #ifdef JP
3522                                 plog("¥×¥ì¥¤Ãæ¤Ï¥²¡¼¥à¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3523 #else
3524                                 plog("You can't open a new game while you're still playing!");
3525 #endif
3526
3527                         }
3528                         else
3529                         {
3530                                 memset(&ofn, 0, sizeof(ofn));
3531                                 ofn.lStructSize = sizeof(ofn);
3532                                 ofn.hwndOwner = data[0].w;
3533                                 ofn.lpstrFilter = "Save Files (*.)\0*\0";
3534                                 ofn.nFilterIndex = 1;
3535                                 ofn.lpstrFile = savefile;
3536                                 ofn.nMaxFile = 1024;
3537                                 ofn.lpstrInitialDir = ANGBAND_DIR_SAVE;
3538                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
3539
3540                                 if (GetOpenFileName(&ofn))
3541                                 {
3542                                         /* Load 'savefile' */
3543                                         validate_file(savefile);
3544                                         game_in_progress = TRUE;
3545                                         Term_flush();
3546                                         play_game(FALSE);
3547                                         quit(NULL);
3548                                 }
3549                         }
3550                         break;
3551                 }
3552
3553                 /* Save game */
3554                 case IDM_FILE_SAVE:
3555                 {
3556                         if (game_in_progress && character_generated)
3557                         {
3558                                 /* Paranoia */
3559                                 if (!can_save)
3560                                 {
3561 #ifdef JP
3562                                         plog("º£¤Ï¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3563 #else
3564                                         plog("You may not do that right now.");
3565 #endif
3566
3567                                         break;
3568                                 }
3569
3570                                 /* Hack -- Forget messages */
3571                                 msg_flag = FALSE;
3572
3573                                 /* Save the game */
3574 #ifdef ZANGBAND
3575                                 do_cmd_save_game(FALSE);
3576 #else /* ZANGBAND */
3577                                 do_cmd_save_game();
3578 #endif /* ZANGBAND */
3579                         }
3580                         else
3581                         {
3582 #ifdef JP
3583                                 plog("º£¡¢¥»¡¼¥Ö¤¹¤ë¤³¤È¤Ï½ÐÍè¤Þ¤»¤ó¡£");
3584 #else
3585                                 plog("You may not do that right now.");
3586 #endif
3587
3588                         }
3589                         break;
3590                 }
3591
3592                 /* Exit */
3593                 case IDM_FILE_EXIT:
3594                 {
3595                         if (game_in_progress && character_generated)
3596                         {
3597                                 /* Paranoia */
3598                                 if (!can_save)
3599                                 {
3600 #ifdef JP
3601                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
3602 #else
3603                                         plog("You may not do that right now.");
3604 #endif
3605
3606                                         break;
3607                                 }
3608
3609                                 /* Hack -- Forget messages */
3610                                 msg_flag = FALSE;
3611
3612                                 forget_lite();
3613                                 forget_view();
3614                                 clear_mon_lite();
3615
3616                                 /* Save the game */
3617 #ifdef ZANGBAND
3618                                 /* do_cmd_save_game(FALSE); */
3619 #else /* ZANGBAND */
3620                                 /* do_cmd_save_game(); */
3621 #endif /* ZANGBAND */
3622                                 Term_key_push(SPECIAL_KEY_QUIT);
3623                                 break;
3624                         }
3625                         quit(NULL);
3626                         break;
3627                 }
3628
3629                 /* Show scores */
3630                 case IDM_FILE_SCORE:
3631                 {
3632                         char buf[1024];
3633
3634                         /* Build the filename */
3635                         path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
3636
3637                         /* Open the binary high score file, for reading */
3638                         highscore_fd = fd_open(buf, O_RDONLY);
3639
3640                         /* Paranoia -- No score file */
3641                         if (highscore_fd < 0)
3642                         {
3643                                 msg_print("Score file unavailable.");
3644                         }
3645                         else
3646                         {
3647                                 /* Save Screen */
3648                                 screen_save();
3649
3650                                 /* Clear screen */
3651                                 Term_clear();
3652
3653                                 /* Display the scores */
3654                                 display_scores_aux(0, MAX_HISCORES, -1, NULL);
3655
3656                                 /* Shut the high score file */
3657                                 (void)fd_close(highscore_fd);
3658
3659                                 /* Forget the high score fd */
3660                                 highscore_fd = -1;
3661
3662                                 /* Load screen */
3663                                 screen_load();
3664
3665                                 /* Hack - Flush it */
3666                                 Term_fresh();
3667                         }
3668
3669                         break;
3670                 }
3671
3672                 /* Open game */
3673                 case IDM_FILE_MOVIE:
3674                 {
3675                         if (!initialized)
3676                         {
3677 #ifdef JP
3678                                 plog("¤Þ¤À½é´ü²½Ãæ¤Ç¤¹...");
3679 #else
3680                                 plog("You cannot do that yet...");
3681 #endif
3682                         }
3683                         else if (game_in_progress)
3684                         {
3685 #ifdef JP
3686                                 plog("¥×¥ì¥¤Ãæ¤Ï¥à¡¼¥Ó¡¼¤ò¥í¡¼¥É¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¡ª");
3687 #else
3688                                 plog("You can't open a movie while you're playing!");
3689 #endif
3690                         }
3691                         else
3692                         {
3693                                 memset(&ofn, 0, sizeof(ofn));
3694                                 ofn.lStructSize = sizeof(ofn);
3695                                 ofn.hwndOwner = data[0].w;
3696                                 ofn.lpstrFilter = "Angband Movie Files (*.amv)\0*.amv\0";
3697                                 ofn.nFilterIndex = 1;
3698                                 ofn.lpstrFile = savefile;
3699                                 ofn.nMaxFile = 1024;
3700                                 ofn.lpstrInitialDir = ANGBAND_DIR_USER;
3701                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
3702
3703                                 if (GetOpenFileName(&ofn))
3704                                 {
3705                                         /* Load 'savefile' */
3706                                         prepare_browse_movie_aux(savefile);
3707                                         play_game(FALSE);
3708                                         quit(NULL);
3709                                         return;
3710                                 }
3711                         }
3712                         break;
3713                 }
3714
3715
3716                 case IDM_WINDOW_VIS_0:
3717                 {
3718 #ifdef JP
3719                         plog("¥á¥¤¥ó¥¦¥£¥ó¥É¥¦¤ÏÈóɽ¼¨¤Ë¤Ç¤­¤Þ¤»¤ó¡ª");
3720 #else
3721                         plog("You are not allowed to do that!");
3722 #endif
3723
3724
3725                         break;
3726                 }
3727
3728                 /* Window visibility */
3729                 case IDM_WINDOW_VIS_1:
3730                 case IDM_WINDOW_VIS_2:
3731                 case IDM_WINDOW_VIS_3:
3732                 case IDM_WINDOW_VIS_4:
3733                 case IDM_WINDOW_VIS_5:
3734                 case IDM_WINDOW_VIS_6:
3735                 case IDM_WINDOW_VIS_7:
3736                 {
3737                         i = wCmd - IDM_WINDOW_VIS_0;
3738
3739                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3740
3741                         td = &data[i];
3742
3743                         if (!td->visible)
3744                         {
3745                                 td->visible = TRUE;
3746                                 ShowWindow(td->w, SW_SHOW);
3747                                 term_data_redraw(td);
3748                         }
3749                         else
3750                         {
3751                                 td->visible = FALSE;
3752                                 td->posfix = FALSE;
3753                                 ShowWindow(td->w, SW_HIDE);
3754                         }
3755
3756                         break;
3757                 }
3758
3759                 /* Window fonts */
3760                 case IDM_WINDOW_FONT_0:
3761                 case IDM_WINDOW_FONT_1:
3762                 case IDM_WINDOW_FONT_2:
3763                 case IDM_WINDOW_FONT_3:
3764                 case IDM_WINDOW_FONT_4:
3765                 case IDM_WINDOW_FONT_5:
3766                 case IDM_WINDOW_FONT_6:
3767                 case IDM_WINDOW_FONT_7:
3768                 {
3769                         i = wCmd - IDM_WINDOW_FONT_0;
3770
3771                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3772
3773                         td = &data[i];
3774
3775                         term_change_font(td);
3776
3777                         break;
3778                 }
3779
3780                 /* Window Z Position */
3781                 case IDM_WINDOW_POS_1:
3782                 case IDM_WINDOW_POS_2:
3783                 case IDM_WINDOW_POS_3:
3784                 case IDM_WINDOW_POS_4:
3785                 case IDM_WINDOW_POS_5:
3786                 case IDM_WINDOW_POS_6:
3787                 case IDM_WINDOW_POS_7:
3788                 {
3789                         i = wCmd - IDM_WINDOW_POS_0;
3790
3791                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3792
3793                         td = &data[i];
3794
3795                         if (!td->posfix && td->visible)
3796                         {
3797                                 td->posfix = TRUE;
3798                                 term_window_pos(td, HWND_TOPMOST);
3799                         }
3800                         else
3801                         {
3802                                 td->posfix = FALSE;
3803                                 term_window_pos(td, data[0].w);
3804                         }
3805
3806                         break;
3807                 }
3808
3809                 /* Bizarre Display */
3810                 case IDM_WINDOW_BIZ_0:
3811                 case IDM_WINDOW_BIZ_1:
3812                 case IDM_WINDOW_BIZ_2:
3813                 case IDM_WINDOW_BIZ_3:
3814                 case IDM_WINDOW_BIZ_4:
3815                 case IDM_WINDOW_BIZ_5:
3816                 case IDM_WINDOW_BIZ_6:
3817                 case IDM_WINDOW_BIZ_7:
3818                 {
3819                         i = wCmd - IDM_WINDOW_BIZ_0;
3820
3821                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3822
3823                         td = &data[i];
3824
3825                         td->bizarre = !td->bizarre;
3826
3827                         term_getsize(td);
3828
3829                         term_window_resize(td);
3830
3831                         break;
3832                 }
3833
3834                 /* Increase Tile Width */
3835                 case IDM_WINDOW_I_WID_0:
3836                 case IDM_WINDOW_I_WID_1:
3837                 case IDM_WINDOW_I_WID_2:
3838                 case IDM_WINDOW_I_WID_3:
3839                 case IDM_WINDOW_I_WID_4:
3840                 case IDM_WINDOW_I_WID_5:
3841                 case IDM_WINDOW_I_WID_6:
3842                 case IDM_WINDOW_I_WID_7:
3843                 {
3844                         i = wCmd - IDM_WINDOW_I_WID_0;
3845
3846                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3847
3848                         td = &data[i];
3849
3850                         td->tile_wid += 1;
3851
3852                         term_getsize(td);
3853
3854                         term_window_resize(td);
3855
3856                         break;
3857                 }
3858
3859                 /* Decrease Tile Height */
3860                 case IDM_WINDOW_D_WID_0:
3861                 case IDM_WINDOW_D_WID_1:
3862                 case IDM_WINDOW_D_WID_2:
3863                 case IDM_WINDOW_D_WID_3:
3864                 case IDM_WINDOW_D_WID_4:
3865                 case IDM_WINDOW_D_WID_5:
3866                 case IDM_WINDOW_D_WID_6:
3867                 case IDM_WINDOW_D_WID_7:
3868                 {
3869                         i = wCmd - IDM_WINDOW_D_WID_0;
3870
3871                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3872
3873                         td = &data[i];
3874
3875                         td->tile_wid -= 1;
3876
3877                         term_getsize(td);
3878
3879                         term_window_resize(td);
3880
3881                         break;
3882                 }
3883
3884                 /* Increase Tile Height */
3885                 case IDM_WINDOW_I_HGT_0:
3886                 case IDM_WINDOW_I_HGT_1:
3887                 case IDM_WINDOW_I_HGT_2:
3888                 case IDM_WINDOW_I_HGT_3:
3889                 case IDM_WINDOW_I_HGT_4:
3890                 case IDM_WINDOW_I_HGT_5:
3891                 case IDM_WINDOW_I_HGT_6:
3892                 case IDM_WINDOW_I_HGT_7:
3893                 {
3894                         i = wCmd - IDM_WINDOW_I_HGT_0;
3895
3896                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3897
3898                         td = &data[i];
3899
3900                         td->tile_hgt += 1;
3901
3902                         term_getsize(td);
3903
3904                         term_window_resize(td);
3905
3906                         break;
3907                 }
3908
3909                 /* Decrease Tile Height */
3910                 case IDM_WINDOW_D_HGT_0:
3911                 case IDM_WINDOW_D_HGT_1:
3912                 case IDM_WINDOW_D_HGT_2:
3913                 case IDM_WINDOW_D_HGT_3:
3914                 case IDM_WINDOW_D_HGT_4:
3915                 case IDM_WINDOW_D_HGT_5:
3916                 case IDM_WINDOW_D_HGT_6:
3917                 case IDM_WINDOW_D_HGT_7:
3918                 {
3919                         i = wCmd - IDM_WINDOW_D_HGT_0;
3920
3921                         if ((i < 0) || (i >= MAX_TERM_DATA)) break;
3922
3923                         td = &data[i];
3924
3925                         td->tile_hgt -= 1;
3926
3927                         term_getsize(td);
3928
3929                         term_window_resize(td);
3930
3931                         break;
3932                 }
3933
3934                 case IDM_OPTIONS_NO_GRAPHICS:
3935                 {
3936                         /* Paranoia */
3937                         if (!inkey_flag)
3938                         {
3939                                 plog("You may not do that right now.");
3940                                 break;
3941                         }
3942
3943                         /* Toggle "arg_graphics" */
3944                         if (arg_graphics != GRAPHICS_NONE)
3945                         {
3946                                 arg_graphics = GRAPHICS_NONE;
3947
3948                                 /* React to changes */
3949                                 Term_xtra_win_react();
3950
3951                                 /* Hack -- Force redraw */
3952                                 Term_key_push(KTRL('R'));
3953                         }
3954
3955                         break;
3956                 }
3957
3958                 case IDM_OPTIONS_OLD_GRAPHICS:
3959                 {
3960                         /* Paranoia */
3961                         if (!inkey_flag)
3962                         {
3963                                 plog("You may not do that right now.");
3964                                 break;
3965                         }
3966
3967                         /* Toggle "arg_graphics" */
3968                         if (arg_graphics != GRAPHICS_ORIGINAL)
3969                         {
3970                                 arg_graphics = GRAPHICS_ORIGINAL;
3971
3972                                 /* React to changes */
3973                                 Term_xtra_win_react();
3974
3975                                 /* Hack -- Force redraw */
3976                                 Term_key_push(KTRL('R'));
3977                         }
3978
3979                         break;
3980                 }
3981
3982                 case IDM_OPTIONS_NEW_GRAPHICS:
3983                 {
3984                         /* Paranoia */
3985                         if (!inkey_flag)
3986                         {
3987                                 plog("You may not do that right now.");
3988                                 break;
3989                         }
3990
3991                         /* Toggle "arg_graphics" */
3992                         if (arg_graphics != GRAPHICS_ADAM_BOLT)
3993                         {
3994                                 arg_graphics = GRAPHICS_ADAM_BOLT;
3995
3996                                 /* React to changes */
3997                                 Term_xtra_win_react();
3998
3999                                 /* Hack -- Force redraw */
4000                                 Term_key_push(KTRL('R'));
4001                         }
4002
4003                         break;
4004                 }
4005
4006                 case IDM_OPTIONS_BIGTILE:
4007                 {
4008                         term_data *td = &data[0];
4009
4010                         /* Paranoia */
4011                         if (!inkey_flag)
4012                         {
4013                                 plog("You may not do that right now.");
4014                                 break;
4015                         }
4016
4017                         /* Toggle "arg_sound" */
4018                         arg_bigtile = !arg_bigtile;
4019
4020                         /* Activate */
4021                         Term_activate(&td->t);
4022
4023                         /* Resize the term */
4024                         Term_resize(td->cols, td->rows);
4025
4026                         /* Redraw later */
4027                         InvalidateRect(td->w, NULL, TRUE);
4028
4029                         break;
4030                 }
4031
4032                 case IDM_OPTIONS_SOUND:
4033                 {
4034                         /* Paranoia */
4035                         if (!inkey_flag)
4036                         {
4037                                 plog("You may not do that right now.");
4038                                 break;
4039                         }
4040
4041                         /* Toggle "arg_sound" */
4042                         arg_sound = !arg_sound;
4043
4044                         /* React to changes */
4045                         Term_xtra_win_react();
4046
4047                         /* Hack -- Force redraw */
4048                         Term_key_push(KTRL('R'));
4049
4050                         break;
4051                 }
4052
4053                 /* bg */
4054                 case IDM_OPTIONS_BG:
4055                 {
4056                         /* Paranoia */
4057                         if (!inkey_flag)
4058                         {
4059                                 plog("You may not do that right now.");
4060                                 break;
4061                         }
4062
4063                         /* Toggle "use_bg" */
4064                         use_bg = !use_bg;
4065
4066                         init_bg();
4067
4068                         /* React to changes */
4069                         Term_xtra_win_react();
4070
4071                         /* Hack -- Force redraw */
4072                         Term_key_push(KTRL('R'));
4073
4074                         break;
4075                 }
4076
4077                 /* bg */
4078                 case IDM_OPTIONS_OPEN_BG:
4079                 {
4080                         /* Paranoia */
4081                         if (!inkey_flag)
4082                         {
4083                                 plog("You may not do that right now.");
4084                                 break;
4085                         }
4086                         else
4087                         {
4088                                 memset(&ofn, 0, sizeof(ofn));
4089                                 ofn.lStructSize = sizeof(ofn);
4090                                 ofn.hwndOwner = data[0].w;
4091                                 ofn.lpstrFilter = "Bitmap Files (*.bmp)\0*.bmp\0";
4092                                 ofn.nFilterIndex = 1;
4093                                 ofn.lpstrFile = bg_bitmap_file;
4094                                 ofn.nMaxFile = 1023;
4095                                 ofn.lpstrInitialDir = NULL;
4096 #ifdef JP
4097                                 ofn.lpstrTitle = "ÊÉ»æ¤òÁª¤ó¤Ç¤Í¡£";
4098 #else
4099                                 ofn.lpstrTitle = "Choose wall paper.";
4100 #endif
4101                                 ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
4102
4103                                 if (GetOpenFileName(&ofn))
4104                                 {
4105                                         /* Load 'savefile' */
4106                                         use_bg = 1;
4107                                         init_bg();
4108                                 }
4109
4110                                 /* React to changes */
4111                                 Term_xtra_win_react();
4112
4113                                 /* Hack -- Force redraw */
4114                                 Term_key_push(KTRL('R'));
4115                         }
4116                         break;
4117                 }
4118
4119                 case IDM_DUMP_SCREEN_HTML:
4120                 {
4121                         static char buf[1024] = "";
4122                         memset(&ofn, 0, sizeof(ofn));
4123                         ofn.lStructSize = sizeof(ofn);
4124                         ofn.hwndOwner = data[0].w;
4125                         ofn.lpstrFilter = "HTML Files (*.html)\0*.html\0";
4126                         ofn.nFilterIndex = 1;
4127                         ofn.lpstrFile = buf;
4128                         ofn.nMaxFile = 1023;
4129                         ofn.lpstrDefExt = "html";
4130                         ofn.lpstrInitialDir = NULL;
4131 #ifdef JP
4132                         ofn.lpstrTitle = "HTML¤Ç¥¹¥¯¥ê¡¼¥ó¥À¥ó¥×¤òÊݸ";
4133 #else
4134                         ofn.lpstrTitle = "Save screen dump as HTML.";
4135 #endif
4136                         ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
4137
4138                         if (GetSaveFileName(&ofn))
4139                         {
4140                                 do_cmd_save_screen_html_aux(buf, 0);
4141                         }
4142                         break;
4143                 }
4144
4145 #ifdef USE_SAVER
4146
4147                 case IDM_OPTIONS_SAVER:
4148                 {
4149                         if (hwndSaver)
4150                         {
4151                                 DestroyWindow(hwndSaver);
4152                                 hwndSaver = NULL;
4153                         }
4154                         else
4155                         {
4156                                 /* Create a screen scaver window */
4157                                 hwndSaver = CreateWindowEx(WS_EX_TOPMOST, "WindowsScreenSaverClass",
4158                                                            "Angband Screensaver",
4159                                                            WS_POPUP | WS_MAXIMIZE | WS_VISIBLE,
4160                                                            0, 0, GetSystemMetrics(SM_CXSCREEN),
4161                                                            GetSystemMetrics(SM_CYSCREEN),
4162                                                            NULL, NULL, hInstance, NULL);
4163
4164                                 if (hwndSaver)
4165                                 {
4166                                         /* Push the window to the bottom XXX XXX XXX */
4167                                         SetWindowPos(hwndSaver, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
4168                                 }
4169                                 else
4170                                 {
4171 #ifdef JP
4172                                         plog("¥¦¥£¥ó¥É¥¦¤òºîÀ®½ÐÍè¤Þ¤»¤ó");
4173 #else
4174                                         plog("Failed to create saver window");
4175 #endif
4176
4177                                 }
4178                         }
4179                         break;
4180                 }
4181
4182 #endif
4183
4184                 case IDM_OPTIONS_MAP:
4185                 {
4186                         windows_map();
4187                         break;
4188                 }
4189
4190                 case IDM_HELP_CONTENTS:
4191                 {
4192 #ifdef HTML_HELP
4193                         char tmp[1024];
4194                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.chm");
4195                         if (check_file(tmp))
4196                         {
4197                                 HtmlHelp(data[0].w, tmp, HH_DISPLAY_TOPIC, 0);
4198                         }
4199                         else
4200                         {
4201 #ifdef JP
4202                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4203                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4204 #else
4205                                 plog_fmt("Cannot find help file: %s", tmp);
4206                                 plog("Use the online help files instead.");
4207 #endif
4208
4209                         }
4210                         break;
4211 #else /* HTML_HELP */
4212                         char buf[1024];
4213                         char tmp[1024];
4214                         path_build(tmp, sizeof(tmp), ANGBAND_DIR_XTRA_HELP, "zangband.hlp");
4215                         if (check_file(tmp))
4216                         {
4217                                 sprintf(buf, "winhelp.exe %s", tmp);
4218                                 WinExec(buf, SW_NORMAL);
4219                         }
4220                         else
4221                         {
4222 #ifdef JP
4223                                 plog_fmt("¥Ø¥ë¥×¥Õ¥¡¥¤¥ë[%s]¤¬¸«ÉÕ¤«¤ê¤Þ¤»¤ó¡£", tmp);
4224                                 plog("Âå¤ï¤ê¤Ë¥ª¥ó¥é¥¤¥ó¥Ø¥ë¥×¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤¡£");
4225 #else
4226                                 plog_fmt("Cannot find help file: %s", tmp);
4227                                 plog("Use the online help files instead.");
4228 #endif
4229
4230                         }
4231                         break;
4232 #endif /* HTML_HELP */
4233                 }
4234         }
4235 }
4236
4237
4238 static bool process_keydown(WPARAM wParam, LPARAM lParam)
4239 {
4240         int i;
4241         bool mc = FALSE;
4242         bool ms = FALSE;
4243         bool ma = FALSE;
4244
4245         /* Extract the modifiers */
4246         if (GetKeyState(VK_CONTROL) & 0x8000) mc = TRUE;
4247         if (GetKeyState(VK_SHIFT)   & 0x8000) ms = TRUE;
4248         if (GetKeyState(VK_MENU)    & 0x8000) ma = TRUE;
4249
4250         Term_no_press = (ma) ? TRUE : FALSE;
4251
4252         /* Handle "special" keys */
4253         if (special_key[(byte)(wParam)] || (ma && !ignore_key[(byte)(wParam)]) )
4254         {
4255                 bool ext_key = (lParam & 0x1000000L) ? TRUE : FALSE;
4256                 bool numpad = FALSE;
4257
4258                 /* Begin the macro trigger */
4259                 Term_keypress(31);
4260
4261                 /* Send the modifiers */
4262                 if (mc) Term_keypress('C');
4263                 if (ms) Term_keypress('S');
4264                 if (ma) Term_keypress('A');
4265
4266                 /* Extract "scan code" */
4267                 i = LOBYTE(HIWORD(lParam));
4268
4269                 /* Introduce the scan code */
4270                 Term_keypress('x');
4271
4272                 /* Extended key bit */
4273                 switch (wParam)
4274                 {
4275                         /* Numpad Enter and '/' are extended key */
4276                 case VK_DIVIDE:
4277                         Term_no_press = TRUE;
4278                 case VK_RETURN: /* Enter */
4279                         numpad = ext_key;
4280                         break;
4281                         /* Other extended keys are on full keyboard */
4282                 case VK_NUMPAD0:
4283                 case VK_NUMPAD1:
4284                 case VK_NUMPAD2:
4285                 case VK_NUMPAD3:
4286                 case VK_NUMPAD4:
4287                 case VK_NUMPAD5:
4288                 case VK_NUMPAD6:
4289                 case VK_NUMPAD7:
4290                 case VK_NUMPAD8:
4291                 case VK_NUMPAD9:
4292                 case VK_ADD:
4293                 case VK_MULTIPLY:
4294                 case VK_SUBTRACT:
4295                 case VK_SEPARATOR:
4296                 case VK_DECIMAL:
4297                         Term_no_press = TRUE;
4298                 case VK_CLEAR:
4299                 case VK_HOME:
4300                 case VK_END:
4301                 case VK_PRIOR:  /* Page Up */
4302                 case VK_NEXT:   /* Page Down */
4303                 case VK_INSERT:
4304                 case VK_DELETE:
4305                 case VK_UP:
4306                 case VK_DOWN:
4307                 case VK_LEFT:
4308                 case VK_RIGHT:
4309                         numpad = !ext_key;
4310                 }
4311
4312                 /* Special modifiers for keypad keys */
4313                 if (numpad) Term_keypress('K');
4314
4315                 /* Encode the hexidecimal scan code */
4316                 Term_keypress(hexsym[i/16]);
4317                 Term_keypress(hexsym[i%16]);
4318
4319                 /* End the macro trigger */
4320                 Term_keypress(13);
4321
4322                 return 1;
4323         }
4324
4325         return 0;
4326 }
4327
4328
4329 #ifdef __MWERKS__
4330 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4331                                   WPARAM wParam, LPARAM lParam);
4332 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4333                                   WPARAM wParam, LPARAM lParam)
4334 #else /* __MWERKS__ */
4335 LRESULT FAR PASCAL AngbandWndProc(HWND hWnd, UINT uMsg,
4336                                           WPARAM wParam, LPARAM lParam)
4337 #endif /* __MWERKS__ */
4338 {
4339         PAINTSTRUCT ps;
4340         HDC hdc;
4341         term_data *td;
4342 #if 0
4343         MINMAXINFO FAR *lpmmi;
4344         RECT rc;
4345 #endif
4346         int i;
4347
4348
4349         /* Acquire proper "term_data" info */
4350         td = (term_data *)GetWindowLong(hWnd, 0);
4351
4352         /* Handle message */
4353         switch (uMsg)
4354         {
4355                 /* XXX XXX XXX */
4356                 case WM_NCCREATE:
4357                 {
4358                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4359                         break;
4360                 }
4361
4362                 /* XXX XXX XXX */
4363                 case WM_CREATE:
4364                 {
4365                         return 0;
4366                 }
4367
4368                 case WM_GETMINMAXINFO:
4369                 {
4370                         MINMAXINFO FAR *lpmmi;
4371                         RECT rc;
4372
4373                         lpmmi = (MINMAXINFO FAR *)lParam;
4374
4375                         /* this message was sent before WM_NCCREATE */
4376                         if (!td) return 1;
4377
4378                         /* Minimum window size is 80x24 */
4379                         rc.left = rc.top = 0;
4380                         rc.right = rc.left + 80 * td->tile_wid + td->size_ow1 + td->size_ow2;
4381                         rc.bottom = rc.top + 24 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4382
4383                         /* Adjust */
4384                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4385
4386                         /* Save minimum size */
4387                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4388                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4389
4390                         return 0;
4391                 }
4392
4393                 case WM_PAINT:
4394                 {
4395                         BeginPaint(hWnd, &ps);
4396                         if (td) term_data_redraw(td);
4397                         EndPaint(hWnd, &ps);
4398                         ValidateRect(hWnd, NULL);
4399                         return 0;
4400                 }
4401
4402                 case WM_SYSKEYDOWN:
4403                 case WM_KEYDOWN:
4404                 {
4405                         if (process_keydown(wParam, lParam))
4406                                 return 0;
4407                         break;
4408                 }
4409
4410                 case WM_CHAR:
4411                 {
4412                         if (Term_no_press) Term_no_press = FALSE;
4413                         else Term_keypress(wParam);
4414                         return 0;
4415                 }
4416
4417                 case WM_LBUTTONDOWN:
4418                 {
4419                         mousex = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4420                         mousey = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4421                         mouse_down = TRUE;
4422                         oldx = mousex;
4423                         oldy = mousey;
4424                         return 0;
4425                 }
4426
4427                 case WM_LBUTTONUP:
4428                 {
4429                         HGLOBAL hGlobal;
4430                         LPSTR lpStr;
4431                         int i, j, sz;
4432                         int dx = abs(oldx - mousex) + 1;
4433                         int dy = abs(oldy - mousey) + 1;
4434                         int ox = (oldx > mousex) ? mousex : oldx;
4435                         int oy = (oldy > mousey) ? mousey : oldy;
4436
4437                         mouse_down = FALSE;
4438                         paint_rect = FALSE;
4439
4440 #ifdef JP
4441                         sz = (dx + 3) * dy;
4442 #else
4443                         sz = (dx + 2) * dy;
4444 #endif
4445                         hGlobal = GlobalAlloc(GHND, sz + 1);
4446                         if (hGlobal == NULL) return 0;
4447                         lpStr = (LPSTR)GlobalLock(hGlobal);
4448
4449                         for (i = 0; i < dy; i++)
4450                         {
4451 #ifdef JP
4452                                 char *s;
4453                                 char **scr = data[0].t.scr->c;
4454
4455                                 C_MAKE(s, (dx + 1), char);
4456                                 strncpy(s, &scr[oy + i][ox], dx);
4457
4458                                 if (ox > 0)
4459                                 {
4460                                         if (iskanji(scr[oy + i][ox - 1])) s[0] = ' ';
4461                                 }
4462
4463                                 if (ox + dx < data[0].cols)
4464                                 {
4465                                         if (iskanji(scr[oy + i][ox + dx - 1])) s[dx - 1] = ' ';
4466                                 }
4467
4468                                 for (j = 0; j < dx; j++)
4469                                 {
4470                                         if (s[j] == 127) s[j] = '#';
4471                                         *lpStr++ = s[j];
4472                                 }
4473 #else
4474                                 for (j = 0; j < dx; j++)
4475                                 {
4476                                         *lpStr++ = data[0].t.scr->c[oy + i][ox + j];
4477                                 }
4478 #endif
4479                                 if (dy > 1)
4480                                 {
4481                                         *lpStr++ = '\r';
4482                                         *lpStr++ = '\n';
4483                                 }
4484                         }
4485
4486                         GlobalUnlock(hGlobal);
4487                         if (OpenClipboard(hWnd) == 0)
4488                         {
4489                                 GlobalFree(hGlobal);
4490                                 return 0;
4491                         }
4492                         EmptyClipboard();
4493                         SetClipboardData(CF_TEXT, hGlobal);
4494                         CloseClipboard();
4495
4496                         Term_redraw();
4497
4498                         return 0;
4499                 }
4500
4501                 case WM_MOUSEMOVE:
4502                 {
4503                         if (mouse_down)
4504                         {
4505                                 int dx, dy;
4506                                 int cx = MIN(LOWORD(lParam) / td->tile_wid, td->cols - 1);
4507                                 int cy = MIN(HIWORD(lParam) / td->tile_hgt, td->rows - 1);
4508                                 int ox, oy;
4509
4510                                 if (paint_rect)
4511                                 {
4512                                         dx = abs(oldx - mousex) + 1;
4513                                         dy = abs(oldy - mousey) + 1;
4514                                         ox = (oldx > mousex) ? mousex : oldx;
4515                                         oy = (oldy > mousey) ? mousey : oldy;
4516                                         Term_inversed_area(hWnd, ox, oy, dx, dy);
4517                                 }
4518                                 else
4519                                 {
4520                                         paint_rect = TRUE;
4521                                 }
4522
4523                                 dx = abs(cx - mousex) + 1;
4524                                 dy = abs(cy - mousey) + 1;
4525                                 ox = (cx > mousex) ? mousex : cx;
4526                                 oy = (cy > mousey) ? mousey : cy;
4527                                 Term_inversed_area(hWnd, ox, oy, dx, dy);
4528
4529                                 oldx = cx;
4530                                 oldy = cy;
4531                         }
4532                         return 0;
4533                 }
4534
4535                 case WM_INITMENU:
4536                 {
4537                         setup_menus();
4538                         return 0;
4539                 }
4540
4541                 case WM_CLOSE:
4542                 {
4543                         if (game_in_progress && character_generated)
4544                         {
4545                                 if (!can_save)
4546                                 {
4547 #ifdef JP
4548                                         plog("º£¤Ï½ªÎ»¤Ç¤­¤Þ¤»¤ó¡£");
4549 #else
4550                                         plog("You may not do that right now.");
4551 #endif
4552                                         return 0;
4553                                 }
4554
4555                                 /* Hack -- Forget messages */
4556                                 msg_flag = FALSE;
4557
4558                                 forget_lite();
4559                                 forget_view();
4560                                 clear_mon_lite();
4561
4562                                 /* Save the game */
4563 #ifdef ZANGBAND
4564                                 /* do_cmd_save_game(FALSE); */
4565 #else /* ZANGBAND */
4566                                 /* do_cmd_save_game(); */
4567 #endif /* ZANGBAND */
4568                                 Term_key_push(SPECIAL_KEY_QUIT);
4569                                 return 0;
4570                         }
4571                         quit(NULL);
4572                         return 0;
4573                 }
4574
4575                 case WM_QUERYENDSESSION:
4576                 {
4577                         if (game_in_progress && character_generated)
4578                         {
4579                                 /* Hack -- Forget messages */
4580                                 msg_flag = FALSE;
4581
4582                                 /* Mega-Hack -- Delay death */
4583                                 if (p_ptr->chp < 0) p_ptr->is_dead = FALSE;
4584
4585 #ifdef JP
4586                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "----¥²¡¼¥àÃæÃÇ----");
4587 #else
4588                                 do_cmd_write_nikki(NIKKI_GAMESTART, 0, "---- Save and Exit Game ----");
4589 #endif
4590
4591                                 /* Hardcode panic save */
4592                                 p_ptr->panic_save = 1;
4593
4594                                 /* Forbid suspend */
4595                                 signals_ignore_tstp();
4596
4597                                 /* Indicate panic save */
4598 #ifdef JP
4599                                 (void)strcpy(p_ptr->died_from, "(¶ÛµÞ¥»¡¼¥Ö)");
4600 #else
4601                                 (void)strcpy(p_ptr->died_from, "(panic save)");
4602 #endif
4603
4604                                 /* Panic save */
4605                                 (void)save_player();
4606                         }
4607                         quit(NULL);
4608                         return 0;
4609                 }
4610
4611                 case WM_QUIT:
4612                 {
4613                         quit(NULL);
4614                         return 0;
4615                 }
4616
4617                 case WM_COMMAND:
4618                 {
4619                         process_menus(LOWORD(wParam));
4620                         return 0;
4621                 }
4622
4623                 case WM_SIZE:
4624                 {
4625                         /* this message was sent before WM_NCCREATE */
4626                         if (!td) return 1;
4627
4628                         /* it was sent from inside CreateWindowEx */
4629                         if (!td->w) return 1;
4630
4631                         /* was sent from WM_SIZE */
4632                         if (td->size_hack) return 1;
4633
4634                         switch (wParam)
4635                         {
4636                                 case SIZE_MINIMIZED:
4637                                 {
4638                                         /* Hide sub-windows */
4639                                         for (i = 1; i < MAX_TERM_DATA; i++)
4640                                         {
4641                                                 if (data[i].visible) ShowWindow(data[i].w, SW_HIDE);
4642                                         }
4643                                         return 0;
4644                                 }
4645
4646                                 case SIZE_MAXIMIZED:
4647                                 {
4648                                         /* fall through XXX XXX XXX */
4649                                 }
4650
4651                                 case SIZE_RESTORED:
4652                                 {
4653                                         uint cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4654                                         uint rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4655
4656                                         /* New size */
4657                                         if ((td->cols != cols) || (td->rows != rows))
4658                                         {
4659                                                 /* Save the new size */
4660                                                 td->cols = cols;
4661                                                 td->rows = rows;
4662
4663                                                 if (!IsZoomed(td->w) && !IsIconic(td->w))
4664                                                 {
4665                                                         normsize.x = td->cols;
4666                                                         normsize.y = td->rows;
4667                                                 }
4668
4669                                                 /* Activate */
4670                                                 Term_activate(&td->t);
4671
4672                                                 /* Resize the term */
4673                                                 Term_resize(td->cols, td->rows);
4674
4675                                                 /* Redraw later */
4676                                                 InvalidateRect(td->w, NULL, TRUE);
4677                                         }
4678
4679                                         td->size_hack = TRUE;
4680
4681                                         /* Show sub-windows */
4682                                         for (i = 1; i < MAX_TERM_DATA; i++)
4683                                         {
4684                                                 if (data[i].visible) ShowWindow(data[i].w, SW_SHOW);
4685                                         }
4686
4687                                         td->size_hack = FALSE;
4688
4689                                         return 0;
4690                                 }
4691                         }
4692                         break;
4693                 }
4694
4695                 case WM_PALETTECHANGED:
4696                 {
4697                         /* Ignore if palette change caused by itself */
4698                         if ((HWND)wParam == hWnd) return 0;
4699
4700                         /* Fall through... */
4701                 }
4702
4703                 case WM_QUERYNEWPALETTE:
4704                 {
4705                         if (!paletted) return 0;
4706
4707                         hdc = GetDC(hWnd);
4708
4709                         SelectPalette(hdc, hPal, FALSE);
4710
4711                         i = RealizePalette(hdc);
4712
4713                         /* if any palette entries changed, repaint the window. */
4714                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4715
4716                         ReleaseDC(hWnd, hdc);
4717
4718                         return 0;
4719                 }
4720
4721                 case WM_ACTIVATE:
4722                 {
4723                         if (wParam && !HIWORD(lParam))
4724                         {
4725                                 /* Do something to sub-windows */
4726                                 for (i = 1; i < MAX_TERM_DATA; i++)
4727                                 {
4728                                         if (!data[i].posfix) term_window_pos(&data[i], hWnd);
4729                                 }
4730
4731                                 /* Focus on main window */
4732                                 SetFocus(hWnd);
4733
4734                                 return 0;
4735                         }
4736
4737                         break;
4738                 }
4739
4740                 case WM_ACTIVATEAPP:
4741                 {
4742                         if (IsIconic(td->w)) break;
4743
4744                         for (i = 1; i < MAX_TERM_DATA; i++)
4745                         {
4746                                 if(data[i].visible)
4747                                 {
4748                                         if (wParam == TRUE)
4749                                         {
4750                                                 ShowWindow(data[i].w, SW_SHOW);
4751                                         }
4752                                         else
4753                                         {
4754                                                 ShowWindow(data[i].w, SW_HIDE);
4755                                         }
4756                                 }
4757                         }
4758                 }
4759         }
4760
4761         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4762 }
4763
4764
4765 #ifdef __MWERKS__
4766 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4767                                            WPARAM wParam, LPARAM lParam);
4768 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4769                                            WPARAM wParam, LPARAM lParam)
4770 #else /* __MWERKS__ */
4771 LRESULT FAR PASCAL AngbandListProc(HWND hWnd, UINT uMsg,
4772                                            WPARAM wParam, LPARAM lParam)
4773 #endif /* __MWERKS__ */
4774 {
4775         term_data *td;
4776 #if 0
4777         MINMAXINFO FAR *lpmmi;
4778         RECT rc;
4779 #endif
4780         PAINTSTRUCT ps;
4781         HDC hdc;
4782         int i;
4783
4784
4785         /* Acquire proper "term_data" info */
4786         td = (term_data *)GetWindowLong(hWnd, 0);
4787
4788         /* Process message */
4789         switch (uMsg)
4790         {
4791                 /* XXX XXX XXX */
4792                 case WM_NCCREATE:
4793                 {
4794                         SetWindowLong(hWnd, 0, (LONG)(my_td));
4795                         break;
4796                 }
4797
4798                 /* XXX XXX XXX */
4799                 case WM_CREATE:
4800                 {
4801                         return 0;
4802                 }
4803
4804                 case WM_GETMINMAXINFO:
4805                 {
4806                         MINMAXINFO FAR *lpmmi;
4807                         RECT rc;
4808
4809                         lpmmi = (MINMAXINFO FAR *)lParam;
4810
4811                         /* this message was sent before WM_NCCREATE */
4812                         if (!td) return 1;
4813
4814                         /* Minimum window size is 80x24 */
4815                         rc.left = rc.top = 0;
4816                         rc.right = rc.left + 20 * td->tile_wid + td->size_ow1 + td->size_ow2;
4817                         rc.bottom = rc.top + 3 * td->tile_hgt + td->size_oh1 + td->size_oh2 + 1;
4818
4819                         /* Adjust */
4820                         AdjustWindowRectEx(&rc, td->dwStyle, TRUE, td->dwExStyle);
4821
4822                         /* Save minimum size */
4823                         lpmmi->ptMinTrackSize.x = rc.right - rc.left;
4824                         lpmmi->ptMinTrackSize.y = rc.bottom - rc.top;
4825
4826                         return 0;
4827                 }
4828
4829                 case WM_SIZE:
4830                 {
4831                         uint cols;
4832                         uint rows;
4833                         
4834                         /* this message was sent before WM_NCCREATE */
4835                         if (!td) return 1;
4836
4837                         /* it was sent from inside CreateWindowEx */
4838                         if (!td->w) return 1;
4839
4840                         /* was sent from inside WM_SIZE */
4841                         if (td->size_hack) return 1;
4842
4843                         td->size_hack = TRUE;
4844
4845                         cols = (LOWORD(lParam) - td->size_ow1) / td->tile_wid;
4846                         rows = (HIWORD(lParam) - td->size_oh1) / td->tile_hgt;
4847
4848                         /* New size */
4849                         if ((td->cols != cols) || (td->rows != rows))
4850                         {
4851                                 /* Save old term */
4852                                 term *old_term = Term;
4853
4854                                 /* Save the new size */
4855                                 td->cols = cols;
4856                                 td->rows = rows;
4857
4858                                 /* Activate */
4859                                 Term_activate(&td->t);
4860
4861                                 /* Resize the term */
4862                                 Term_resize(td->cols, td->rows);
4863
4864                                 /* Activate */
4865                                 Term_activate(old_term);
4866
4867                                 /* Redraw later */
4868                                 InvalidateRect(td->w, NULL, TRUE);
4869
4870                                 /* HACK - Redraw all windows */
4871                                 p_ptr->window = 0xFFFFFFFF;
4872                                 window_stuff();
4873                         }
4874
4875                         td->size_hack = FALSE;
4876
4877                         return 0;
4878                 }
4879
4880                 case WM_PAINT:
4881                 {
4882                         BeginPaint(hWnd, &ps);
4883                         if (td) term_data_redraw(td);
4884                         EndPaint(hWnd, &ps);
4885                         return 0;
4886                 }
4887
4888                 case WM_SYSKEYDOWN:
4889                 case WM_KEYDOWN:
4890                 {
4891                         if (process_keydown(wParam, lParam))
4892                                 return 0;
4893                         break;
4894                 }
4895
4896                 case WM_CHAR:
4897                 {
4898                         if (Term_no_press) Term_no_press = FALSE;
4899                         else Term_keypress(wParam);
4900                         return 0;
4901                 }
4902
4903                 case WM_PALETTECHANGED:
4904                 {
4905                         /* ignore if palette change caused by itself */
4906                         if ((HWND)wParam == hWnd) return FALSE;
4907                         /* otherwise, fall through!!! */
4908                 }
4909
4910                 case WM_QUERYNEWPALETTE:
4911                 {
4912                         if (!paletted) return 0;
4913                         hdc = GetDC(hWnd);
4914                         SelectPalette(hdc, hPal, FALSE);
4915                         i = RealizePalette(hdc);
4916                         /* if any palette entries changed, repaint the window. */
4917                         if (i) InvalidateRect(hWnd, NULL, TRUE);
4918                         ReleaseDC(hWnd, hdc);
4919                         return 0;
4920                 }
4921
4922                 case WM_NCLBUTTONDOWN:
4923                 {
4924
4925 #ifdef HTCLOSE
4926                         if (wParam == HTCLOSE) wParam = HTSYSMENU;
4927 #endif /* HTCLOSE */
4928
4929                         if (wParam == HTSYSMENU)
4930                         {
4931                                 if (td->visible)
4932                                 {
4933                                         td->visible = FALSE;
4934                                         ShowWindow(td->w, SW_HIDE);
4935                                 }
4936
4937                                 return 0;
4938                         }
4939
4940                         break;
4941                 }
4942         }
4943
4944         return DefWindowProc(hWnd, uMsg, wParam, lParam);
4945 }
4946
4947
4948 #ifdef USE_SAVER
4949
4950 #define MOUSE_SENS 40
4951
4952 #ifdef __MWERKS__
4953 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4954                                     WPARAM wParam, LPARAM lParam);
4955 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4956                                     WPARAM wParam, LPARAM lParam)
4957 #else /* __MWERKS__ */
4958 LRESULT FAR PASCAL AngbandSaverProc(HWND hWnd, UINT uMsg,
4959                                             WPARAM wParam, LPARAM lParam)
4960 #endif /* __MWERKS__ */
4961 {
4962         static int iMouse = 0;
4963         static WORD xMouse = 0;
4964         static WORD yMouse = 0;
4965
4966         int dx, dy;
4967
4968
4969         /* Process */
4970         switch (uMsg)
4971         {
4972                 /* XXX XXX XXX */
4973                 case WM_NCCREATE:
4974                 {
4975                         break;
4976                 }
4977
4978                 case WM_SETCURSOR:
4979                 {
4980                         SetCursor(NULL);
4981                         return 0;
4982                 }
4983
4984 #if 0
4985                 case WM_ACTIVATE:
4986                 {
4987                         if (LOWORD(wParam) == WA_INACTIVE) break;
4988
4989                         /* else fall through */
4990                 }
4991 #endif
4992
4993                 case WM_LBUTTONDOWN:
4994                 case WM_MBUTTONDOWN:
4995                 case WM_RBUTTONDOWN:
4996                 case WM_KEYDOWN:
4997                 {
4998                         SendMessage(hWnd, WM_CLOSE, 0, 0);
4999                         return 0;
5000                 }
5001
5002                 case WM_MOUSEMOVE:
5003                 {
5004                         if (iMouse)
5005                         {
5006                                 dx = LOWORD(lParam) - xMouse;
5007                                 dy = HIWORD(lParam) - yMouse;
5008
5009                                 if (dx < 0) dx = -dx;
5010                                 if (dy < 0) dy = -dy;
5011
5012                                 if ((dx > MOUSE_SENS) || (dy > MOUSE_SENS))
5013                                 {
5014                                         SendMessage(hWnd, WM_CLOSE, 0, 0);
5015                                 }
5016                         }
5017
5018                         /* Save last location */
5019                         iMouse = 1;
5020                         xMouse = LOWORD(lParam);
5021                         yMouse = HIWORD(lParam);
5022
5023                         return 0;
5024                 }
5025
5026                 case WM_CLOSE:
5027                 {
5028                         DestroyWindow(hwndSaver);
5029                         hwndSaver = NULL;
5030                         return 0;
5031                 }
5032         }
5033
5034         /* Oops */
5035         return DefWindowProc(hWnd, uMsg, wParam, lParam);
5036 }
5037
5038 #endif /* USE_SAVER */
5039
5040
5041
5042
5043
5044 /*** Temporary Hooks ***/
5045
5046
5047 /*
5048  * Display warning message (see "z-util.c")
5049  */
5050 static void hack_plog(cptr str)
5051 {
5052         /* Give a warning */
5053         if (str)
5054         {
5055 #ifdef JP
5056                 MessageBox(NULL, str, "·Ù¹ð¡ª",
5057                            MB_ICONEXCLAMATION | MB_OK);
5058 #else
5059                 MessageBox(NULL, str, "Warning",
5060                            MB_ICONEXCLAMATION | MB_OK);
5061 #endif
5062
5063         }
5064 }
5065
5066
5067 /*
5068  * Display error message and quit (see "z-util.c")
5069  */
5070 static void hack_quit(cptr str)
5071 {
5072         /* Give a warning */
5073         if (str)
5074         {
5075 #ifdef JP
5076                 MessageBox(NULL, str, "¥¨¥é¡¼¡ª",
5077                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5078 #else
5079                 MessageBox(NULL, str, "Error",
5080                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5081 #endif
5082
5083         }
5084
5085         /* Unregister the classes */
5086         UnregisterClass(AppName, hInstance);
5087
5088         /* Destroy the icon */
5089         if (hIcon) DestroyIcon(hIcon);
5090
5091         /* Exit */
5092         exit(0);
5093 }
5094
5095
5096
5097 /*** Various hooks ***/
5098
5099
5100 /*
5101  * Display warning message (see "z-util.c")
5102  */
5103 static void hook_plog(cptr str)
5104 {
5105         /* Warning */
5106         if (str)
5107         {
5108 #ifdef JP
5109                 MessageBox(data[0].w, str, "·Ù¹ð¡ª",
5110                            MB_ICONEXCLAMATION | MB_OK);
5111 #else
5112                 MessageBox(data[0].w, str, "Warning",
5113                            MB_ICONEXCLAMATION | MB_OK);
5114 #endif
5115
5116         }
5117 }
5118
5119
5120 /*
5121  * Display error message and quit (see "z-util.c")
5122  */
5123 static void hook_quit(cptr str)
5124 {
5125         int i;
5126
5127
5128         /* Give a warning */
5129         if (str)
5130         {
5131 #ifdef JP
5132                 MessageBox(data[0].w, str, "¥¨¥é¡¼¡ª",
5133                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5134 #else
5135                 MessageBox(data[0].w, str, "Error",
5136                            MB_ICONEXCLAMATION | MB_OK | MB_ICONSTOP);
5137 #endif
5138
5139         }
5140
5141
5142         /* Save the preferences */
5143         save_prefs();
5144
5145
5146         /*** Could use 'Term_nuke_win()' XXX XXX XXX */
5147
5148         /* Destroy all windows */
5149         for (i = MAX_TERM_DATA - 1; i >= 0; --i)
5150         {
5151                 term_force_font(&data[i], NULL);
5152                 if (data[i].font_want) string_free(data[i].font_want);
5153                 if (data[i].w) DestroyWindow(data[i].w);
5154                 data[i].w = 0;
5155         }
5156
5157         /* Free the bitmap stuff */
5158 #ifdef USE_GRAPHICS
5159         if (infGraph.hPalette) DeleteObject(infGraph.hPalette);
5160         if (infGraph.hBitmap) DeleteObject(infGraph.hBitmap);
5161
5162         if (infMask.hPalette) DeleteObject(infMask.hPalette);
5163         if (infMask.hBitmap) DeleteObject(infMask.hBitmap);
5164
5165 #endif /* USE_GRAPHICS */
5166
5167         /*** Free some other stuff ***/
5168
5169         DeleteObject(hbrYellow);
5170
5171         /* bg */
5172         delete_bg();
5173
5174         if (hPal) DeleteObject(hPal);
5175
5176         UnregisterClass(AppName, hInstance);
5177
5178         if (hIcon) DestroyIcon(hIcon);
5179
5180         exit(0);
5181 }
5182
5183
5184
5185 /*** Initialize ***/
5186
5187
5188 /*
5189  * Init some stuff
5190  */
5191 static void init_stuff(void)
5192 {
5193         int i;
5194
5195         char path[1024];
5196
5197
5198         /* Get program name with full path */
5199         GetModuleFileName(hInstance, path, 512);
5200
5201         /* Save the "program name" XXX XXX XXX */
5202         argv0 = path;
5203
5204         /* Get the name of the "*.ini" file */
5205         strcpy(path + strlen(path) - 4, ".INI");
5206
5207         /* Save the the name of the ini-file */
5208         ini_file = string_make(path);
5209
5210         /* Analyze the path */
5211         i = strlen(path);
5212
5213         /* Get the path */
5214         for (; i > 0; i--)
5215         {
5216                 if (path[i] == '\\')
5217                 {
5218                         /* End of path */
5219                         break;
5220                 }
5221         }
5222
5223         /* Add "lib" to the path */
5224         strcpy(path + i + 1, "lib\\");
5225
5226         /* Validate the path */
5227         validate_dir(path, TRUE);
5228
5229         /* Init the file paths */
5230         init_file_paths(path);
5231
5232         /* Hack -- Validate the paths */
5233         validate_dir(ANGBAND_DIR_APEX, FALSE);
5234         validate_dir(ANGBAND_DIR_BONE, FALSE);
5235
5236         /* Allow missing 'edit' directory */
5237         if (!check_dir(ANGBAND_DIR_EDIT))
5238         {
5239                 /* Must have 'data'! */
5240                 validate_dir(ANGBAND_DIR_DATA, TRUE);
5241         }
5242         else
5243         {
5244                 /* Don't need 'data' */
5245                 validate_dir(ANGBAND_DIR_DATA, FALSE);
5246         }
5247
5248         validate_dir(ANGBAND_DIR_FILE, TRUE);
5249         validate_dir(ANGBAND_DIR_HELP, FALSE);
5250         validate_dir(ANGBAND_DIR_INFO, FALSE);
5251         validate_dir(ANGBAND_DIR_PREF, TRUE);
5252         validate_dir(ANGBAND_DIR_SAVE, FALSE);
5253         validate_dir(ANGBAND_DIR_USER, TRUE);
5254         validate_dir(ANGBAND_DIR_XTRA, TRUE);
5255
5256         /* Build the filename */
5257 #ifdef JP
5258         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news_j.txt");
5259 #else
5260         path_build(path, sizeof(path), ANGBAND_DIR_FILE, "news.txt");
5261 #endif
5262
5263
5264         /* Hack -- Validate the "news.txt" file */
5265         validate_file(path);
5266
5267
5268 #if 0 /* #ifndef JP */
5269         /* Build the "font" path */
5270         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "font");
5271
5272         /* Allocate the path */
5273         ANGBAND_DIR_XTRA_FONT = string_make(path);
5274
5275         /* Validate the "font" directory */
5276         validate_dir(ANGBAND_DIR_XTRA_FONT, TRUE);
5277
5278         /* Build the filename */
5279         path_build(path, sizeof(path), ANGBAND_DIR_XTRA_FONT, "8X13.FON");
5280
5281         /* Hack -- Validate the basic font */
5282         validate_file(path);
5283 #endif
5284
5285
5286 #ifdef USE_GRAPHICS
5287
5288         /* Build the "graf" path */
5289         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "graf");
5290
5291         /* Allocate the path */
5292         ANGBAND_DIR_XTRA_GRAF = string_make(path);
5293
5294         /* Validate the "graf" directory */
5295         validate_dir(ANGBAND_DIR_XTRA_GRAF, TRUE);
5296
5297 #endif /* USE_GRAPHICS */
5298
5299
5300 #ifdef USE_SOUND
5301
5302         /* Build the "sound" path */
5303         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "sound");
5304
5305         /* Allocate the path */
5306         ANGBAND_DIR_XTRA_SOUND = string_make(path);
5307
5308         /* Validate the "sound" directory */
5309         validate_dir(ANGBAND_DIR_XTRA_SOUND, FALSE);
5310
5311 #endif /* USE_SOUND */
5312
5313 #ifdef USE_MUSIC
5314
5315         /* Build the "music" path */
5316         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "music");
5317
5318         /* Allocate the path */
5319         ANGBAND_DIR_XTRA_MUSIC = string_make(path);
5320
5321         /* Validate the "music" directory */
5322         validate_dir(ANGBAND_DIR_XTRA_MUSIC, FALSE);
5323
5324 #endif /* USE_MUSIC */
5325
5326         /* Build the "help" path */
5327         path_build(path, sizeof(path), ANGBAND_DIR_XTRA, "help");
5328
5329         /* Allocate the path */
5330         ANGBAND_DIR_XTRA_HELP = string_make(path);
5331
5332         /* Validate the "help" directory */
5333         /* validate_dir(ANGBAND_DIR_XTRA_HELP); */
5334 }
5335
5336
5337 int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
5338                        LPSTR lpCmdLine, int nCmdShow)
5339 {
5340         int i;
5341
5342         WNDCLASS wc;
5343         HDC hdc;
5344         MSG msg;
5345
5346         /* Unused */
5347         (void)nCmdShow;
5348
5349         /* Save globally */
5350         hInstance = hInst;
5351
5352         /* Initialize */
5353         if (hPrevInst == NULL)
5354         {
5355                 wc.style         = CS_CLASSDC;
5356                 wc.lpfnWndProc   = AngbandWndProc;
5357                 wc.cbClsExtra    = 0;
5358                 wc.cbWndExtra    = 4; /* one long pointer to term_data */
5359                 wc.hInstance     = hInst;
5360                 wc.hIcon         = hIcon = LoadIcon(hInst, AppName);
5361                 wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
5362                 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
5363                 wc.lpszMenuName  = AppName;
5364                 wc.lpszClassName = AppName;
5365
5366                 if (!RegisterClass(&wc)) exit(1);
5367
5368                 wc.lpfnWndProc   = AngbandListProc;
5369                 wc.lpszMenuName  = NULL;
5370                 wc.lpszClassName = AngList;
5371
5372                 if (!RegisterClass(&wc)) exit(2);
5373
5374 #ifdef USE_SAVER
5375
5376                 wc.style          = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS;
5377                 wc.lpfnWndProc    = AngbandSaverProc;
5378                 wc.hCursor        = NULL;
5379                 wc.lpszMenuName   = NULL;
5380                 wc.lpszClassName  = "WindowsScreenSaverClass";
5381
5382                 if (!RegisterClass(&wc)) exit(3);
5383
5384 #endif
5385
5386         }
5387
5388         /* Temporary hooks */
5389         plog_aux = hack_plog;
5390         quit_aux = hack_quit;
5391         core_aux = hack_quit;
5392
5393         /* Prepare the filepaths */
5394         init_stuff();
5395
5396         /* Initialize the keypress analyzer */
5397         for (i = 0; special_key_list[i]; ++i)
5398         {
5399                 special_key[special_key_list[i]] = TRUE;
5400         }
5401         /* Initialize the keypress analyzer */
5402         for (i = 0; ignore_key_list[i]; ++i)
5403         {
5404                 ignore_key[ignore_key_list[i]] = TRUE;
5405         }
5406
5407         /* Determine if display is 16/256/true color */
5408         hdc = GetDC(NULL);
5409         colors16 = (GetDeviceCaps(hdc, BITSPIXEL) == 4);
5410         paletted = ((GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) ? TRUE : FALSE);
5411         ReleaseDC(NULL, hdc);
5412
5413         /* Initialize the colors */
5414         for (i = 0; i < 256; i++)
5415         {
5416                 byte rv, gv, bv;
5417
5418                 /* Extract desired values */
5419                 rv = angband_color_table[i][1];
5420                 gv = angband_color_table[i][2];
5421                 bv = angband_color_table[i][3];
5422
5423                 /* Extract the "complex" code */
5424                 win_clr[i] = PALETTERGB(rv, gv, bv);
5425
5426                 /* Save the "simple" code */
5427                 angband_color_table[i][0] = win_pal[i];
5428         }
5429
5430         /* Prepare the windows */
5431         init_windows();
5432
5433         /* bg */
5434         init_bg();
5435
5436         /* Activate hooks */
5437         plog_aux = hook_plog;
5438         quit_aux = hook_quit;
5439         core_aux = hook_quit;
5440
5441         /* Set the system suffix */
5442         ANGBAND_SYS = "win";
5443
5444         /* Set the keyboard suffix */
5445         if (7 != GetKeyboardType(0))
5446                 ANGBAND_KEYBOARD = "0";
5447         else
5448         {
5449                 /* Japanese keyboard */
5450                 switch (GetKeyboardType(1))
5451                 {
5452                 case 0x0D01: case 0x0D02:
5453                 case 0x0D03: case 0x0D04:
5454                 case 0x0D05: case 0x0D06:
5455                         /* NEC PC-98x1 */
5456                         ANGBAND_KEYBOARD = "NEC98";
5457                         break;
5458                 default:
5459                         /* PC/AT */
5460                         ANGBAND_KEYBOARD = "JAPAN";
5461                 }
5462         }
5463
5464         /* Catch nasty signals */
5465         signals_init();
5466
5467         /* Initialize */
5468         init_angband();
5469
5470         /* We are now initialized */
5471         initialized = TRUE;
5472 #ifdef CHUUKEI
5473         if(lpCmdLine[0] == '-'){
5474           switch(lpCmdLine[1])
5475           {
5476           case 'p':
5477           case 'P':
5478             {
5479               if (!lpCmdLine[2]) break;
5480               chuukei_server = TRUE;
5481               if(connect_chuukei_server(&lpCmdLine[2])<0){
5482                 msg_print("connect fail");
5483                 return 0;
5484               }
5485               msg_print("connect");
5486               msg_print(NULL);
5487               break;
5488             }
5489
5490           case 'c':
5491           case 'C':
5492             {
5493               if (!lpCmdLine[2]) break;
5494               chuukei_client = TRUE;
5495               connect_chuukei_server(&lpCmdLine[2]);
5496               play_game(FALSE);
5497               quit(NULL);
5498               return 0;
5499             }
5500           case 'X':
5501           case 'x':
5502             {
5503               if (!lpCmdLine[2]) break;
5504               prepare_browse_movie(&lpCmdLine[2]);
5505               play_game(FALSE);
5506               quit(NULL);
5507               return 0;
5508             }
5509           }
5510         }
5511 #endif
5512
5513 #ifdef CHUUKEI
5514         /* Did the user double click on a save file? */
5515         if(!chuukei_server) check_for_save_file(lpCmdLine);
5516 #else
5517         /* Did the user double click on a save file? */
5518         check_for_save_file(lpCmdLine);
5519 #endif
5520
5521         /* Prompt the user */
5522 #ifdef JP
5523         prt("[¥Õ¥¡¥¤¥ë] ¥á¥Ë¥å¡¼¤Î [¿·µ¬] ¤Þ¤¿¤Ï [³«¤¯] ¤òÁªÂò¤·¤Æ¤¯¤À¤µ¤¤¡£", 23, 8);
5524 #else
5525         prt("[Choose 'New' or 'Open' from the 'File' menu]", 23, 17);
5526 #endif
5527
5528         Term_fresh();
5529
5530         /* Process messages forever */
5531         while (GetMessage(&msg, NULL, 0, 0))
5532         {
5533                 TranslateMessage(&msg);
5534                 DispatchMessage(&msg);
5535         }
5536
5537         /* Paranoia */
5538         quit(NULL);
5539
5540         /* Paranoia */
5541         return (0);
5542 }
5543
5544
5545 #endif /* WINDOWS */
5546