OSDN Git Service

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