OSDN Git Service

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