OSDN Git Service

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