OSDN Git Service

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