OSDN Git Service

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