MS SDK >= feb. 2003
[xboard.git] / winboard / winboard.c
1 /* 
2  * WinBoard.c -- Windows NT front end to XBoard
3  * $Id$
4  *
5  * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
6  * Enhancements Copyright 1992-2001 Free Software Foundation, Inc.
7  *
8  * XBoard borrows its colors and the bitmaps.xchess bitmap set from XChess,
9  * which was written and is copyrighted by Wayne Christopher.
10  *
11  * The following terms apply to Digital Equipment Corporation's copyright
12  * interest in XBoard:
13  * ------------------------------------------------------------------------
14  * All Rights Reserved
15  *
16  * Permission to use, copy, modify, and distribute this software and its
17  * documentation for any purpose and without fee is hereby granted,
18  * provided that the above copyright notice appear in all copies and that
19  * both that copyright notice and this permission notice appear in
20  * supporting documentation, and that the name of Digital not be
21  * used in advertising or publicity pertaining to distribution of the
22  * software without specific, written prior permission.
23  *
24  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
26  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
27  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30  * SOFTWARE.
31  * ------------------------------------------------------------------------
32  *
33  * The following terms apply to the enhanced version of XBoard distributed
34  * by the Free Software Foundation:
35  * ------------------------------------------------------------------------
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of the GNU General Public License as published by
38  * the Free Software Foundation; either version 2 of the License, or
39  * (at your option) any later version.
40  *
41  * This program is distributed in the hope that it will be useful,
42  * but WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44  * GNU General Public License for more details.
45  *
46  * You should have received a copy of the GNU General Public License
47  * along with this program; if not, write to the Free Software
48  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
49  * ------------------------------------------------------------------------
50  */
51
52 #include "config.h"
53
54 #include <windows.h>
55 #include <winuser.h>
56 #include <winsock.h>
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <malloc.h>
61 #include <sys/stat.h>
62 #include <fcntl.h>
63 #include <math.h>
64 #include <commdlg.h>
65 #include <dlgs.h>
66 #include <richedit.h>
67 #include <mmsystem.h>
68
69 #if __GNUC__
70 #include <errno.h>
71 #include <string.h>
72 #endif
73
74 #include "common.h"
75 #include "winboard.h"
76 #include "frontend.h"
77 #include "backend.h"
78 #include "moves.h"
79 #include "wclipbrd.h"
80 #include "wgamelist.h"
81 #include "wedittags.h"
82 #include "woptions.h"
83 #include "wsockerr.h"
84 #include "defaults.h"
85
86 typedef struct {
87   ChessSquare piece;  
88   POINT pos;      /* window coordinates of current pos */
89   POINT lastpos;  /* window coordinates of last pos - used for clipping */
90   POINT from;     /* board coordinates of the piece's orig pos */
91   POINT to;       /* board coordinates of the piece's new pos */
92 } AnimInfo;
93
94 static AnimInfo animInfo = { EmptySquare, {-1,-1}, {-1,-1}, {-1,-1} };
95
96 typedef struct {
97   POINT start;    /* window coordinates of start pos */
98   POINT pos;      /* window coordinates of current pos */
99   POINT lastpos;  /* window coordinates of last pos - used for clipping */
100   POINT from;     /* board coordinates of the piece's orig pos */
101 } DragInfo;
102
103 static DragInfo dragInfo = { {-1,-1}, {-1,-1}, {-1,-1}, {-1,-1} };
104
105 typedef struct {
106   POINT sq[2];    /* board coordinates of from, to squares */
107 } HighlightInfo;
108
109 static HighlightInfo highlightInfo        = { {{-1, -1}, {-1, -1}} };
110 static HighlightInfo premoveHighlightInfo = { {{-1, -1}, {-1, -1}} };
111
112 /* Window class names */
113 char szAppName[] = "WinBoard";
114 char szConsoleName[] = "WBConsole";
115
116 /* Title bar text */
117 char szTitle[] = "WinBoard";
118 char szConsoleTitle[] = "ICS Interaction";
119
120 char *programName;
121 char *settingsFileName;
122 BOOLEAN saveSettingsOnExit;
123 char installDir[MSG_SIZ];
124
125 BoardSize boardSize;
126 BOOLEAN chessProgram;
127 static int boardX, boardY, consoleX, consoleY, consoleW, consoleH;
128 static int squareSize, lineGap;
129 static int winWidth, winHeight;
130 static RECT messageRect, whiteRect, blackRect;
131 static char messageText[MESSAGE_TEXT_MAX];
132 static int clockTimerEvent = 0;
133 static int loadGameTimerEvent = 0;
134 static int analysisTimerEvent = 0;
135 static DelayedEventCallback delayedTimerCallback;
136 static int delayedTimerEvent = 0;
137 static int buttonCount = 2;
138 char *icsTextMenuString;
139 char *icsNames;
140 char *firstChessProgramNames;
141 char *secondChessProgramNames;
142
143 #define ARG_MAX 20000
144
145 #define PALETTESIZE 256
146
147 HINSTANCE hInst;          /* current instance */
148 HWND hwndMain = NULL;        /* root window*/
149 HWND hwndConsole = NULL;
150 BOOLEAN alwaysOnTop = FALSE;
151 RECT boardRect;
152 COLORREF lightSquareColor, darkSquareColor, whitePieceColor, 
153   blackPieceColor, highlightSquareColor, premoveHighlightColor;
154 HPALETTE hPal;
155 ColorClass currentColorClass;
156
157 HWND hCommPort = NULL;    /* currently open comm port */
158 static HWND hwndPause;    /* pause button */
159 static HBITMAP pieceBitmap[3][(int) WhiteKing + 1];
160 static HBRUSH lightSquareBrush, darkSquareBrush,
161   whitePieceBrush, blackPieceBrush, iconBkgndBrush, outlineBrush;
162 static POINT gridEndpoints[(BOARD_SIZE + 1) * 4];
163 static DWORD gridVertexCounts[(BOARD_SIZE + 1) * 2];
164 static HPEN gridPen = NULL;
165 static HPEN highlightPen = NULL;
166 static HPEN premovePen = NULL;
167 static NPLOGPALETTE pLogPal;
168 static BOOL paletteChanged = FALSE;
169 static HICON iconWhite, iconBlack, iconCurrent;
170 static int doingSizing = FALSE;
171 static int lastSizing = 0;
172 static int prevStderrPort;
173
174 #if __GNUC__ && !defined(_winmajor)
175 #define oldDialog 0 /* cygwin doesn't define _winmajor; mingw does */
176 #else
177 #define oldDialog (_winmajor < 4)
178 #endif
179
180 char *defaultTextAttribs[] = 
181 {
182   COLOR_SHOUT, COLOR_SSHOUT, COLOR_CHANNEL1, COLOR_CHANNEL, COLOR_KIBITZ,
183   COLOR_TELL, COLOR_CHALLENGE, COLOR_REQUEST, COLOR_SEEK, COLOR_NORMAL,
184   COLOR_NONE
185 };
186
187 typedef struct {
188   char *name;
189   int squareSize;
190   int lineGap;
191   int smallLayout;
192   int tinyLayout;
193   int cliWidth, cliHeight;
194 } SizeInfo;
195
196 SizeInfo sizeInfo[] = 
197 {
198   { "tiny",     21, 0, 1, 1, 0, 0 },
199   { "teeny",    25, 1, 1, 1, 0, 0 },
200   { "dinky",    29, 1, 1, 1, 0, 0 },
201   { "petite",   33, 1, 1, 1, 0, 0 },
202   { "slim",     37, 2, 1, 0, 0, 0 },
203   { "small",    40, 2, 1, 0, 0, 0 },
204   { "mediocre", 45, 2, 1, 0, 0, 0 },
205   { "middling", 49, 2, 0, 0, 0, 0 },
206   { "average",  54, 2, 0, 0, 0, 0 },
207   { "moderate", 58, 3, 0, 0, 0, 0 },
208   { "medium",   64, 3, 0, 0, 0, 0 },
209   { "bulky",    72, 3, 0, 0, 0, 0 },
210   { "large",    80, 3, 0, 0, 0, 0 },
211   { "big",      87, 3, 0, 0, 0, 0 },
212   { "huge",     95, 3, 0, 0, 0, 0 },
213   { "giant",    108, 3, 0, 0, 0, 0 },
214   { "colossal", 116, 4, 0, 0, 0, 0 },
215   { "titanic",  129, 4, 0, 0, 0, 0 },
216   { NULL, 0, 0, 0, 0, 0, 0 }
217 };
218
219 #define MF(x) {x, {0, }, {0, }, 0}
220 MyFont fontRec[NUM_SIZES][NUM_FONTS] =
221 {
222   { MF(CLOCK_FONT_TINY), MF(MESSAGE_FONT_TINY), 
223     MF(COORD_FONT_TINY), MF(CONSOLE_FONT_TINY),
224     MF(COMMENT_FONT_TINY), MF(EDITTAGS_FONT_TINY) },
225   { MF(CLOCK_FONT_TEENY), MF(MESSAGE_FONT_TEENY), 
226     MF(COORD_FONT_TEENY), MF(CONSOLE_FONT_TEENY),
227     MF(COMMENT_FONT_TEENY), MF(EDITTAGS_FONT_TEENY) },
228   { MF(CLOCK_FONT_DINKY), MF(MESSAGE_FONT_DINKY),
229     MF(COORD_FONT_DINKY), MF(CONSOLE_FONT_DINKY),
230     MF(COMMENT_FONT_DINKY), MF(EDITTAGS_FONT_DINKY) },
231   { MF(CLOCK_FONT_PETITE), MF(MESSAGE_FONT_PETITE),
232     MF(COORD_FONT_PETITE), MF(CONSOLE_FONT_PETITE),
233     MF(COMMENT_FONT_PETITE), MF(EDITTAGS_FONT_PETITE) },
234   { MF(CLOCK_FONT_SLIM), MF(MESSAGE_FONT_SLIM),
235     MF(COORD_FONT_SLIM), MF(CONSOLE_FONT_SLIM),
236     MF(COMMENT_FONT_SLIM), MF(EDITTAGS_FONT_SLIM) },
237   { MF(CLOCK_FONT_SMALL), MF(MESSAGE_FONT_SMALL),
238     MF(COORD_FONT_SMALL), MF(CONSOLE_FONT_SMALL),
239     MF(COMMENT_FONT_SMALL), MF(EDITTAGS_FONT_SMALL) },
240   { MF(CLOCK_FONT_MEDIOCRE), MF(MESSAGE_FONT_MEDIOCRE),
241     MF(COORD_FONT_MEDIOCRE), MF(CONSOLE_FONT_MEDIOCRE),
242     MF(COMMENT_FONT_MEDIOCRE), MF(EDITTAGS_FONT_MEDIOCRE) },
243   { MF(CLOCK_FONT_MIDDLING), MF(MESSAGE_FONT_MIDDLING),
244     MF(COORD_FONT_MIDDLING), MF(CONSOLE_FONT_MIDDLING),
245     MF(COMMENT_FONT_MIDDLING), MF(EDITTAGS_FONT_MIDDLING) },
246   { MF(CLOCK_FONT_AVERAGE), MF(MESSAGE_FONT_AVERAGE),
247     MF(COORD_FONT_AVERAGE), MF(CONSOLE_FONT_AVERAGE),
248     MF(COMMENT_FONT_AVERAGE), MF(EDITTAGS_FONT_AVERAGE) },
249   { MF(CLOCK_FONT_MODERATE), MF(MESSAGE_FONT_MODERATE),
250     MF(COORD_FONT_MODERATE), MF(CONSOLE_FONT_MODERATE),
251     MF(COMMENT_FONT_MODERATE), MF(EDITTAGS_FONT_MODERATE) },
252   { MF(CLOCK_FONT_MEDIUM), MF(MESSAGE_FONT_MEDIUM),
253     MF(COORD_FONT_MEDIUM), MF(CONSOLE_FONT_MEDIUM),
254     MF(COMMENT_FONT_MEDIUM), MF(EDITTAGS_FONT_MEDIUM) },
255   { MF(CLOCK_FONT_BULKY), MF(MESSAGE_FONT_BULKY),
256     MF(COORD_FONT_BULKY), MF(CONSOLE_FONT_BULKY),
257     MF(COMMENT_FONT_BULKY), MF(EDITTAGS_FONT_BULKY) },
258   { MF(CLOCK_FONT_LARGE), MF(MESSAGE_FONT_LARGE),
259     MF(COORD_FONT_LARGE), MF(CONSOLE_FONT_LARGE),
260     MF(COMMENT_FONT_LARGE), MF(EDITTAGS_FONT_LARGE) },
261   { MF(CLOCK_FONT_BIG), MF(MESSAGE_FONT_BIG),
262     MF(COORD_FONT_BIG), MF(CONSOLE_FONT_BIG),
263     MF(COMMENT_FONT_BIG), MF(EDITTAGS_FONT_BIG) },
264   { MF(CLOCK_FONT_HUGE), MF(MESSAGE_FONT_HUGE),
265     MF(COORD_FONT_HUGE), MF(CONSOLE_FONT_HUGE),
266     MF(COMMENT_FONT_HUGE), MF(EDITTAGS_FONT_HUGE) },
267   { MF(CLOCK_FONT_GIANT), MF(MESSAGE_FONT_GIANT),
268     MF(COORD_FONT_GIANT), MF(CONSOLE_FONT_GIANT),
269     MF(COMMENT_FONT_GIANT), MF(EDITTAGS_FONT_GIANT) },
270   { MF(CLOCK_FONT_COLOSSAL), MF(MESSAGE_FONT_COLOSSAL),
271     MF(COORD_FONT_COLOSSAL), MF(CONSOLE_FONT_COLOSSAL),
272     MF(COMMENT_FONT_COLOSSAL), MF(EDITTAGS_FONT_COLOSSAL) },
273   { MF(CLOCK_FONT_TITANIC), MF(MESSAGE_FONT_TITANIC),
274     MF(COORD_FONT_TITANIC), MF(CONSOLE_FONT_TITANIC),
275     MF(COMMENT_FONT_TITANIC), MF(EDITTAGS_FONT_TITANIC) },
276 };
277
278 MyFont *font[NUM_SIZES][NUM_FONTS];
279
280 typedef struct {
281   char *label;
282   int id;
283   HWND hwnd;
284   WNDPROC wndproc;
285 } MyButtonDesc;
286
287 #define BUTTON_WIDTH (tinyLayout ? 16 : 32)
288 #define N_BUTTONS 5
289
290 MyButtonDesc buttonDesc[N_BUTTONS] =
291 {
292   {"<<", IDM_ToStart, NULL, NULL},
293   {"<", IDM_Backward, NULL, NULL},
294   {"P", IDM_Pause, NULL, NULL},
295   {">", IDM_Forward, NULL, NULL},
296   {">>", IDM_ToEnd, NULL, NULL},
297 };
298
299 int tinyLayout = 0, smallLayout = 0;
300 #define MENU_BAR_ITEMS 6
301 char *menuBarText[2][MENU_BAR_ITEMS+1] = {
302   { "&File", "&Mode", "&Action", "&Step", "&Options", "&Help", NULL },
303   { "&F", "&M", "&A", "&S", "&O", "&H", NULL },
304 };
305
306
307 MySound sounds[(int)NSoundClasses];
308 MyTextAttribs textAttribs[(int)NColorClasses];
309
310 MyColorizeAttribs colorizeAttribs[] = {
311   { (COLORREF)0, 0, "Shout Text" },
312   { (COLORREF)0, 0, "SShout/CShout" },
313   { (COLORREF)0, 0, "Channel 1 Text" },
314   { (COLORREF)0, 0, "Channel Text" },
315   { (COLORREF)0, 0, "Kibitz Text" },
316   { (COLORREF)0, 0, "Tell Text" },
317   { (COLORREF)0, 0, "Challenge Text" },
318   { (COLORREF)0, 0, "Request Text" },
319   { (COLORREF)0, 0, "Seek Text" },
320   { (COLORREF)0, 0, "Normal Text" },
321   { (COLORREF)0, 0, "None" }
322 };
323
324
325
326 static char *commentTitle;
327 static char *commentText;
328 static int commentIndex;
329 static Boolean editComment = FALSE;
330 HWND commentDialog = NULL;
331 BOOLEAN commentDialogUp = FALSE;
332 static int commentX, commentY, commentH, commentW;
333
334 static char *analysisTitle;
335 static char *analysisText;
336 HWND analysisDialog = NULL;
337 BOOLEAN analysisDialogUp = FALSE;
338 static int analysisX, analysisY, analysisH, analysisW;
339
340 char errorTitle[MSG_SIZ];
341 char errorMessage[2*MSG_SIZ];
342 HWND errorDialog = NULL;
343 BOOLEAN moveErrorMessageUp = FALSE;
344 BOOLEAN consoleEcho = TRUE;
345 CHARFORMAT consoleCF;
346 COLORREF consoleBackgroundColor;
347
348 char *programVersion;
349
350 #define CPReal 1
351 #define CPComm 2
352 #define CPSock 3
353 #define CPRcmd 4
354 typedef int CPKind;
355
356 typedef struct {
357   CPKind kind;
358   HANDLE hProcess;
359   DWORD pid;
360   HANDLE hTo;
361   HANDLE hFrom;
362   SOCKET sock;
363   SOCKET sock2;  /* stderr socket for OpenRcmd */
364 } ChildProc;
365
366 #define INPUT_SOURCE_BUF_SIZE 4096
367
368 typedef struct _InputSource {
369   CPKind kind;
370   HANDLE hFile;
371   SOCKET sock;
372   int lineByLine;
373   HANDLE hThread;
374   DWORD id;
375   char buf[INPUT_SOURCE_BUF_SIZE];
376   char *next;
377   DWORD count;
378   int error;
379   InputCallback func;
380   struct _InputSource *second;  /* for stderr thread on CPRcmd */
381   VOIDSTAR closure;
382 } InputSource;
383
384 InputSource *consoleInputSource;
385
386 DCB dcb;
387
388 /* forward */
389 VOID ConsoleOutput(char* data, int length, int forceVisible);
390 VOID ConsoleCreate();
391 LRESULT CALLBACK
392   ConsoleWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
393 VOID ColorizeTextPopup(HWND hwnd, ColorClass cc);
394 VOID PrintCommSettings(FILE *f, char *name, DCB *dcb);
395 VOID ParseCommSettings(char *arg, DCB *dcb);
396 LRESULT CALLBACK
397   StartupDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
398 VOID APIENTRY MenuPopup(HWND hwnd, POINT pt, HMENU hmenu, UINT def);
399 void ParseIcsTextMenu(char *icsTextMenuString);
400 VOID PopUpMoveDialog(char firstchar);
401 VOID UpdateSampleText(HWND hDlg, int id, MyColorizeAttribs *mca);
402
403 /*
404  * Setting "frozen" should disable all user input other than deleting
405  * the window.  We do this while engines are initializing themselves.
406  */
407 static int frozen = 0;
408 static int oldMenuItemState[MENU_BAR_ITEMS];
409 void FreezeUI()
410 {
411   HMENU hmenu;
412   int i;
413
414   if (frozen) return;
415   frozen = 1;
416   hmenu = GetMenu(hwndMain);
417   for (i=0; i<MENU_BAR_ITEMS; i++) {
418     oldMenuItemState[i] = EnableMenuItem(hmenu, i, MF_BYPOSITION|MF_GRAYED);
419   }
420   DrawMenuBar(hwndMain);
421 }
422
423 /* Undo a FreezeUI */
424 void ThawUI()
425 {
426   HMENU hmenu;
427   int i;
428
429   if (!frozen) return;
430   frozen = 0;
431   hmenu = GetMenu(hwndMain);
432   for (i=0; i<MENU_BAR_ITEMS; i++) {
433     EnableMenuItem(hmenu, i, MF_BYPOSITION|oldMenuItemState[i]);
434   }
435   DrawMenuBar(hwndMain);
436 }
437
438 /*---------------------------------------------------------------------------*\
439  *
440  * WinMain
441  *
442 \*---------------------------------------------------------------------------*/
443
444 int APIENTRY
445 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
446         LPSTR lpCmdLine, int nCmdShow)
447 {
448   MSG msg;
449   HANDLE hAccelMain, hAccelNoAlt;
450
451   debugFP = stderr;
452
453   LoadLibrary("RICHED32.DLL");
454   consoleCF.cbSize = sizeof(CHARFORMAT);
455
456   if (!InitApplication(hInstance)) {
457     return (FALSE);
458   }
459   if (!InitInstance(hInstance, nCmdShow, lpCmdLine)) {
460     return (FALSE);
461   }
462
463   hAccelMain = LoadAccelerators (hInstance, szAppName);
464   hAccelNoAlt = LoadAccelerators (hInstance, "NO_ALT");
465
466   /* Acquire and dispatch messages until a WM_QUIT message is received. */
467
468   while (GetMessage(&msg, /* message structure */
469                     NULL, /* handle of window receiving the message */
470                     0,    /* lowest message to examine */
471                     0))   /* highest message to examine */
472     {
473       if (!(commentDialog && IsDialogMessage(commentDialog, &msg)) &&
474           !(editTagsDialog && IsDialogMessage(editTagsDialog, &msg)) &&
475           !(gameListDialog && IsDialogMessage(gameListDialog, &msg)) &&
476           !(errorDialog && IsDialogMessage(errorDialog, &msg)) &&
477           !(!frozen && TranslateAccelerator(hwndMain, hAccelMain, &msg)) &&
478           !(!hwndConsole && TranslateAccelerator(hwndMain, hAccelNoAlt, &msg))) {
479         TranslateMessage(&msg); /* Translates virtual key codes */
480         DispatchMessage(&msg);  /* Dispatches message to window */
481       }
482     }
483
484
485   return (msg.wParam);  /* Returns the value from PostQuitMessage */
486 }
487
488 /*---------------------------------------------------------------------------*\
489  *
490  * Initialization functions
491  *
492 \*---------------------------------------------------------------------------*/
493
494 BOOL
495 InitApplication(HINSTANCE hInstance)
496 {
497   WNDCLASS wc;
498
499   /* Fill in window class structure with parameters that describe the */
500   /* main window. */
501
502   wc.style         = CS_HREDRAW | CS_VREDRAW; /* Class style(s). */
503   wc.lpfnWndProc   = (WNDPROC)WndProc;  /* Window Procedure */
504   wc.cbClsExtra    = 0;                 /* No per-class extra data. */
505   wc.cbWndExtra    = 0;                 /* No per-window extra data. */
506   wc.hInstance     = hInstance;         /* Owner of this class */
507   wc.hIcon         = LoadIcon(hInstance, "icon_white");
508   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);       /* Cursor */
509   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);  /* Default color */
510   wc.lpszMenuName  = szAppName;                 /* Menu name from .RC */
511   wc.lpszClassName = szAppName;                 /* Name to register as */
512
513   /* Register the window class and return success/failure code. */
514   if (!RegisterClass(&wc)) return FALSE;
515
516   wc.style         = CS_HREDRAW | CS_VREDRAW;
517   wc.lpfnWndProc   = (WNDPROC)ConsoleWndProc;
518   wc.cbClsExtra    = 0;
519   wc.cbWndExtra    = DLGWINDOWEXTRA;
520   wc.hInstance     = hInstance;
521   wc.hIcon         = LoadIcon(hInstance, "icon_white");
522   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
523   wc.hbrBackground = (HBRUSH)(COLOR_MENU+1);
524   wc.lpszMenuName  = NULL;
525   wc.lpszClassName = szConsoleName;
526
527   if (!RegisterClass(&wc)) return FALSE;
528   return TRUE;
529 }
530
531
532 /* Set by InitInstance, used by EnsureOnScreen */
533 int screenHeight, screenWidth;
534
535 void
536 EnsureOnScreen(int *x, int *y)
537 {
538   /* Be sure window at (x,y) is not off screen (or even mostly off screen) */
539   if (*x > screenWidth - 32) *x = 0;
540   if (*y > screenHeight - 32) *y = 0;
541 }
542
543 BOOL
544 InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine)
545 {
546   HWND hwnd; /* Main window handle. */
547   int ibs;
548   WINDOWPLACEMENT wp;
549   char *filepart;
550
551   hInst = hInstance;    /* Store instance handle in our global variable */
552
553   if (SearchPath(NULL, "WinBoard.exe", NULL, MSG_SIZ, installDir, &filepart)) {
554     *filepart = NULLCHAR;
555   } else {
556     GetCurrentDirectory(MSG_SIZ, installDir);
557   }
558   InitAppData(lpCmdLine);      /* Get run-time parameters */
559   if (appData.debugMode) {
560     debugFP = fopen("winboard.debug", "w");
561     setbuf(debugFP, NULL);
562   }
563
564   InitBackEnd1();
565
566   /* Create a main window for this application instance. */
567   hwnd = CreateWindow(szAppName, szTitle,
568                       (WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX),
569                       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
570                       NULL, NULL, hInstance, NULL);
571   hwndMain = hwnd;
572
573   /* If window could not be created, return "failure" */
574   if (!hwnd) {
575     return (FALSE);
576   }
577
578   iconWhite = LoadIcon(hInstance, "icon_white");
579   iconBlack = LoadIcon(hInstance, "icon_black");
580   iconCurrent = iconWhite;
581   InitDrawingColors();
582   screenHeight = GetSystemMetrics(SM_CYSCREEN);
583   screenWidth = GetSystemMetrics(SM_CXSCREEN);
584   for (ibs = (int) NUM_SIZES - 1; ibs >= 0; ibs--) {
585     /* Compute window size for each board size, and use the largest
586        size that fits on this screen as the default. */
587     InitDrawingSizes((BoardSize)ibs, 0);
588     if (boardSize == (BoardSize)-1 &&
589         winHeight <= screenHeight && winWidth <= screenWidth) {
590       boardSize = (BoardSize)ibs;
591     }
592   }
593   InitDrawingSizes(boardSize, 0);
594   InitMenuChecks();
595   buttonCount = GetSystemMetrics(SM_CMOUSEBUTTONS);
596
597   InitBackEnd2();
598
599   /* Make the window visible; update its client area; and return "success" */
600   EnsureOnScreen(&boardX, &boardY);
601   wp.length = sizeof(WINDOWPLACEMENT);
602   wp.flags = 0;
603   wp.showCmd = nCmdShow;
604   wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
605   wp.rcNormalPosition.left = boardX;
606   wp.rcNormalPosition.right = boardX + winWidth;
607   wp.rcNormalPosition.top = boardY;
608   wp.rcNormalPosition.bottom = boardY + winHeight;
609   SetWindowPlacement(hwndMain, &wp);
610
611   SetWindowPos(hwndMain, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
612                0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
613   if (hwndConsole) {
614 #if AOT_CONSOLE
615     SetWindowPos(hwndConsole, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
616                  0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
617 #endif
618     ShowWindow(hwndConsole, nCmdShow);
619   }
620   UpdateWindow(hwnd);
621
622   return TRUE;
623
624 }
625
626
627 typedef enum {
628   ArgString, ArgInt, ArgFloat, ArgBoolean, ArgTrue, ArgFalse, ArgNone, 
629   ArgColor, ArgAttribs, ArgFilename, ArgBoardSize, ArgFont, ArgCommSettings,
630   ArgSettingsFilename
631 } ArgType;
632
633 typedef struct {
634   char *argName;
635   ArgType argType;
636   /***
637   union {
638     String *pString;       // ArgString
639     int *pInt;             // ArgInt
640     float *pFloat;         // ArgFloat
641     Boolean *pBoolean;     // ArgBoolean
642     COLORREF *pColor;      // ArgColor
643     ColorClass cc;         // ArgAttribs
644     String *pFilename;     // ArgFilename
645     BoardSize *pBoardSize; // ArgBoardSize
646     int whichFont;         // ArgFont
647     DCB *pDCB;             // ArgCommSettings
648     String *pFilename;     // ArgSettingsFilename
649   } argLoc;
650   ***/
651   LPVOID argLoc;
652   BOOL save;
653 } ArgDescriptor;
654
655 int junk;
656 ArgDescriptor argDescriptors[] = {
657   /* positional arguments */
658   { "loadGameFile", ArgFilename, (LPVOID) &appData.loadGameFile, FALSE },
659   { "", ArgNone, NULL },
660   /* keyword arguments */
661   { "whitePieceColor", ArgColor, (LPVOID) &whitePieceColor, TRUE },
662   { "wpc", ArgColor, (LPVOID) &whitePieceColor, FALSE },
663   { "blackPieceColor", ArgColor, (LPVOID) &blackPieceColor, TRUE },
664   { "bpc", ArgColor, (LPVOID) &blackPieceColor, FALSE },
665   { "lightSquareColor", ArgColor, (LPVOID) &lightSquareColor, TRUE },
666   { "lsc", ArgColor, (LPVOID) &lightSquareColor, FALSE },
667   { "darkSquareColor", ArgColor, (LPVOID) &darkSquareColor, TRUE },
668   { "dsc", ArgColor, (LPVOID) &darkSquareColor, FALSE },
669   { "highlightSquareColor", ArgColor, (LPVOID) &highlightSquareColor, TRUE },
670   { "hsc", ArgColor, (LPVOID) &highlightSquareColor, FALSE },
671   { "premoveHighlightColor", ArgColor, (LPVOID) &premoveHighlightColor, TRUE },
672   { "phc", ArgColor, (LPVOID) &premoveHighlightColor, FALSE },
673   { "movesPerSession", ArgInt, (LPVOID) &appData.movesPerSession, TRUE },
674   { "mps", ArgInt, (LPVOID) &appData.movesPerSession, FALSE },
675   { "initString", ArgString, (LPVOID) &appData.initString, FALSE },
676   { "firstInitString", ArgString, (LPVOID) &appData.initString, FALSE },
677   { "secondInitString", ArgString, (LPVOID) &appData.secondInitString, FALSE },
678   { "firstComputerString", ArgString, (LPVOID) &appData.firstComputerString,
679     FALSE },
680   { "secondComputerString", ArgString, (LPVOID) &appData.secondComputerString,
681     FALSE },
682   { "firstChessProgram", ArgFilename, (LPVOID) &appData.firstChessProgram,
683     FALSE },
684   { "fcp", ArgFilename, (LPVOID) &appData.firstChessProgram, FALSE },
685   { "secondChessProgram", ArgFilename, (LPVOID) &appData.secondChessProgram,
686     FALSE },
687   { "scp", ArgFilename, (LPVOID) &appData.secondChessProgram, FALSE },
688   { "firstPlaysBlack", ArgBoolean, (LPVOID) &appData.firstPlaysBlack, FALSE },
689   { "fb", ArgTrue, (LPVOID) &appData.firstPlaysBlack, FALSE },
690   { "xfb", ArgFalse, (LPVOID) &appData.firstPlaysBlack, FALSE },
691   { "-fb", ArgFalse, (LPVOID) &appData.firstPlaysBlack, FALSE },
692   { "noChessProgram", ArgBoolean, (LPVOID) &appData.noChessProgram, FALSE },
693   { "ncp", ArgTrue, (LPVOID) &appData.noChessProgram, FALSE },
694   { "xncp", ArgFalse, (LPVOID) &appData.noChessProgram, FALSE },
695   { "-ncp", ArgFalse, (LPVOID) &appData.noChessProgram, FALSE },
696   { "firstHost", ArgString, (LPVOID) &appData.firstHost, FALSE },
697   { "fh", ArgString, (LPVOID) &appData.firstHost, FALSE },
698   { "secondHost", ArgString, (LPVOID) &appData.secondHost, FALSE },
699   { "sh", ArgString, (LPVOID) &appData.secondHost, FALSE },
700   { "firstDirectory", ArgFilename, (LPVOID) &appData.firstDirectory, FALSE },
701   { "fd", ArgFilename, (LPVOID) &appData.firstDirectory, FALSE },
702   { "secondDirectory", ArgFilename, (LPVOID) &appData.secondDirectory, FALSE },
703   { "sd", ArgFilename, (LPVOID) &appData.secondDirectory, FALSE },
704   /*!!bitmapDirectory?*/
705   { "remoteShell", ArgFilename, (LPVOID) &appData.remoteShell, FALSE },
706   { "rsh", ArgFilename, (LPVOID) &appData.remoteShell, FALSE },
707   { "remoteUser", ArgString, (LPVOID) &appData.remoteUser, FALSE },
708   { "ruser", ArgString, (LPVOID) &appData.remoteUser, FALSE },
709   { "timeDelay", ArgFloat, (LPVOID) &appData.timeDelay, TRUE },
710   { "td", ArgFloat, (LPVOID) &appData.timeDelay, FALSE },
711   { "timeControl", ArgString, (LPVOID) &appData.timeControl, TRUE },
712   { "tc", ArgString, (LPVOID) &appData.timeControl, FALSE },
713   { "timeIncrement", ArgInt, (LPVOID) &appData.timeIncrement, TRUE },
714   { "inc", ArgInt, (LPVOID) &appData.timeIncrement, FALSE },
715   { "internetChessServerMode", ArgBoolean, (LPVOID) &appData.icsActive, FALSE },
716   { "ics", ArgTrue, (LPVOID) &appData.icsActive, FALSE },
717   { "xics", ArgFalse, (LPVOID) &appData.icsActive, FALSE },
718   { "-ics", ArgFalse, (LPVOID) &appData.icsActive, FALSE },
719   { "internetChessServerHost", ArgString, (LPVOID) &appData.icsHost, FALSE },
720   { "icshost", ArgString, (LPVOID) &appData.icsHost, FALSE },
721   { "internetChessServerPort", ArgString, (LPVOID) &appData.icsPort, FALSE },
722   { "icsport", ArgString, (LPVOID) &appData.icsPort, FALSE },
723   { "internetChessServerCommPort", ArgString, (LPVOID) &appData.icsCommPort, FALSE },
724   { "icscomm", ArgString, (LPVOID) &appData.icsCommPort, FALSE },
725   { "internetChessServerComPort", ArgString, (LPVOID) &appData.icsCommPort, FALSE },
726   { "icscom", ArgString, (LPVOID) &appData.icsCommPort, FALSE },
727   { "internetChessServerLogonScript", ArgFilename, (LPVOID) &appData.icsLogon, FALSE },
728   { "icslogon", ArgFilename, (LPVOID) &appData.icsLogon, FALSE },
729   { "useTelnet", ArgBoolean, (LPVOID) &appData.useTelnet, FALSE },
730   { "telnet", ArgTrue, (LPVOID) &appData.useTelnet, FALSE },
731   { "xtelnet", ArgFalse, (LPVOID) &appData.useTelnet, FALSE },
732   { "-telnet", ArgFalse, (LPVOID) &appData.useTelnet, FALSE },
733   { "telnetProgram", ArgFilename, (LPVOID) &appData.telnetProgram, FALSE },
734   { "icshelper", ArgFilename, (LPVOID) &appData.icsHelper, FALSE },
735   { "gateway", ArgString, (LPVOID) &appData.gateway, FALSE },
736   { "loadGameFile", ArgFilename, (LPVOID) &appData.loadGameFile, FALSE },
737   { "lgf", ArgFilename, (LPVOID) &appData.loadGameFile, FALSE },
738   { "loadGameIndex", ArgInt, (LPVOID) &appData.loadGameIndex, FALSE },
739   { "lgi", ArgInt, (LPVOID) &appData.loadGameIndex, FALSE },
740   { "saveGameFile", ArgFilename, (LPVOID) &appData.saveGameFile, TRUE },
741   { "sgf", ArgFilename, (LPVOID) &appData.saveGameFile, FALSE },
742   { "autoSaveGames", ArgBoolean, (LPVOID) &appData.autoSaveGames, TRUE },
743   { "autosave", ArgTrue, (LPVOID) &appData.autoSaveGames, FALSE },
744   { "xautosave", ArgFalse, (LPVOID) &appData.autoSaveGames, FALSE },
745   { "-autosave", ArgFalse, (LPVOID) &appData.autoSaveGames, FALSE },
746   { "loadPositionFile", ArgFilename, (LPVOID) &appData.loadPositionFile, FALSE },
747   { "lpf", ArgFilename, (LPVOID) &appData.loadPositionFile, FALSE },
748   { "loadPositionIndex", ArgInt, (LPVOID) &appData.loadPositionIndex, FALSE },
749   { "lpi", ArgInt, (LPVOID) &appData.loadPositionIndex, FALSE },
750   { "savePositionFile", ArgFilename, (LPVOID) &appData.savePositionFile, FALSE },
751   { "spf", ArgFilename, (LPVOID) &appData.savePositionFile, FALSE },
752   { "matchMode", ArgBoolean, (LPVOID) &appData.matchMode, FALSE },
753   { "mm", ArgTrue, (LPVOID) &appData.matchMode, FALSE },
754   { "xmm", ArgFalse, (LPVOID) &appData.matchMode, FALSE },
755   { "-mm", ArgFalse, (LPVOID) &appData.matchMode, FALSE },
756   { "matchGames", ArgInt, (LPVOID) &appData.matchGames, FALSE },
757   { "mg", ArgInt, (LPVOID) &appData.matchGames, FALSE },
758   { "monoMode", ArgBoolean, (LPVOID) &appData.monoMode, TRUE },
759   { "mono", ArgTrue, (LPVOID) &appData.monoMode, FALSE },
760   { "xmono", ArgFalse, (LPVOID) &appData.monoMode, FALSE },
761   { "-mono", ArgFalse, (LPVOID) &appData.monoMode, FALSE },
762   { "debugMode", ArgBoolean, (LPVOID) &appData.debugMode, FALSE },
763   { "debug", ArgTrue, (LPVOID) &appData.debugMode, FALSE },
764   { "xdebug", ArgFalse, (LPVOID) &appData.debugMode, FALSE },
765   { "-debug", ArgFalse, (LPVOID) &appData.debugMode, FALSE },
766   { "clockMode", ArgBoolean, (LPVOID) &appData.clockMode, FALSE },
767   { "clock", ArgTrue, (LPVOID) &appData.clockMode, FALSE },
768   { "xclock", ArgFalse, (LPVOID) &appData.clockMode, FALSE },
769   { "-clock", ArgFalse, (LPVOID) &appData.clockMode, FALSE },
770   { "searchTime", ArgString, (LPVOID) &appData.searchTime, FALSE },
771   { "st", ArgString, (LPVOID) &appData.searchTime, FALSE },
772   { "searchDepth", ArgInt, (LPVOID) &appData.searchDepth, FALSE },
773   { "depth", ArgInt, (LPVOID) &appData.searchDepth, FALSE },
774   { "showCoords", ArgBoolean, (LPVOID) &appData.showCoords, TRUE },
775   { "coords", ArgTrue, (LPVOID) &appData.showCoords, FALSE },
776   { "xcoords", ArgFalse, (LPVOID) &appData.showCoords, FALSE },
777   { "-coords", ArgFalse, (LPVOID) &appData.showCoords, FALSE },
778   { "showThinking", ArgBoolean, (LPVOID) &appData.showThinking, TRUE },
779   { "thinking", ArgTrue, (LPVOID) &appData.showThinking, FALSE },
780   { "xthinking", ArgFalse, (LPVOID) &appData.showThinking, FALSE },
781   { "-thinking", ArgFalse, (LPVOID) &appData.showThinking, FALSE },
782   { "ponderNextMove", ArgBoolean, (LPVOID) &appData.ponderNextMove, TRUE },
783   { "ponder", ArgTrue, (LPVOID) &appData.ponderNextMove, FALSE },
784   { "xponder", ArgFalse, (LPVOID) &appData.ponderNextMove, FALSE },
785   { "-ponder", ArgFalse, (LPVOID) &appData.ponderNextMove, FALSE },
786   { "periodicUpdates", ArgBoolean, (LPVOID) &appData.periodicUpdates, TRUE },
787   { "periodic", ArgTrue, (LPVOID) &appData.periodicUpdates, FALSE },
788   { "xperiodic", ArgFalse, (LPVOID) &appData.periodicUpdates, FALSE },
789   { "-periodic", ArgFalse, (LPVOID) &appData.periodicUpdates, FALSE },
790   { "popupExitMessage", ArgBoolean, (LPVOID) &appData.popupExitMessage, TRUE },
791   { "exit", ArgTrue, (LPVOID) &appData.popupExitMessage, FALSE },
792   { "xexit", ArgFalse, (LPVOID) &appData.popupExitMessage, FALSE },
793   { "-exit", ArgFalse, (LPVOID) &appData.popupExitMessage, FALSE },
794   { "popupMoveErrors", ArgBoolean, (LPVOID) &appData.popupMoveErrors, TRUE },
795   { "popup", ArgTrue, (LPVOID) &appData.popupMoveErrors, FALSE },
796   { "xpopup", ArgFalse, (LPVOID) &appData.popupMoveErrors, FALSE },
797   { "-popup", ArgFalse, (LPVOID) &appData.popupMoveErrors, FALSE },
798   { "popUpErrors", ArgBoolean, (LPVOID) &appData.popupMoveErrors, 
799     FALSE }, /* only so that old WinBoard.ini files from betas can be read */
800   { "clockFont", ArgFont, (LPVOID) CLOCK_FONT, TRUE },
801   { "messageFont", ArgFont, (LPVOID) MESSAGE_FONT, TRUE },
802   { "coordFont", ArgFont, (LPVOID) COORD_FONT, TRUE },
803   { "tagsFont", ArgFont, (LPVOID) EDITTAGS_FONT, TRUE },
804   { "commentFont", ArgFont, (LPVOID) COMMENT_FONT, TRUE },
805   { "icsFont", ArgFont, (LPVOID) CONSOLE_FONT, TRUE },
806   { "boardSize", ArgBoardSize, (LPVOID) &boardSize,
807     TRUE }, /* must come after all fonts */
808   { "size", ArgBoardSize, (LPVOID) &boardSize, FALSE },
809   { "ringBellAfterMoves", ArgBoolean, (LPVOID) &appData.ringBellAfterMoves,
810     FALSE }, /* historical; kept only so old winboard.ini files will parse */
811   { "alwaysOnTop", ArgBoolean, (LPVOID) &alwaysOnTop, TRUE },
812   { "top", ArgTrue, (LPVOID) &alwaysOnTop, FALSE },
813   { "xtop", ArgFalse, (LPVOID) &alwaysOnTop, FALSE },
814   { "-top", ArgFalse, (LPVOID) &alwaysOnTop, FALSE },
815   { "autoCallFlag", ArgBoolean, (LPVOID) &appData.autoCallFlag, TRUE },
816   { "autoflag", ArgTrue, (LPVOID) &appData.autoCallFlag, FALSE },
817   { "xautoflag", ArgFalse, (LPVOID) &appData.autoCallFlag, FALSE },
818   { "-autoflag", ArgFalse, (LPVOID) &appData.autoCallFlag, FALSE },
819   { "autoComment", ArgBoolean, (LPVOID) &appData.autoComment, TRUE },
820   { "autocomm", ArgTrue, (LPVOID) &appData.autoComment, FALSE },
821   { "xautocomm", ArgFalse, (LPVOID) &appData.autoComment, FALSE },
822   { "-autocomm", ArgFalse, (LPVOID) &appData.autoComment, FALSE },
823   { "autoObserve", ArgBoolean, (LPVOID) &appData.autoObserve, TRUE },
824   { "autobs", ArgTrue, (LPVOID) &appData.autoObserve, FALSE },
825   { "xautobs", ArgFalse, (LPVOID) &appData.autoObserve, FALSE },
826   { "-autobs", ArgFalse, (LPVOID) &appData.autoObserve, FALSE },
827   { "flipView", ArgBoolean, (LPVOID) &appData.flipView, FALSE },
828   { "flip", ArgTrue, (LPVOID) &appData.flipView, FALSE },
829   { "xflip", ArgFalse, (LPVOID) &appData.flipView, FALSE },
830   { "-flip", ArgFalse, (LPVOID) &appData.flipView, FALSE },
831   { "autoFlipView", ArgBoolean, (LPVOID) &appData.autoFlipView, TRUE },
832   { "autoflip", ArgTrue, (LPVOID) &appData.autoFlipView, FALSE },
833   { "xautoflip", ArgFalse, (LPVOID) &appData.autoFlipView, FALSE },
834   { "-autoflip", ArgFalse, (LPVOID) &appData.autoFlipView, FALSE },
835   { "autoRaiseBoard", ArgBoolean, (LPVOID) &appData.autoRaiseBoard, TRUE },
836   { "autoraise", ArgTrue, (LPVOID) &appData.autoRaiseBoard, FALSE },
837   { "xautoraise", ArgFalse, (LPVOID) &appData.autoRaiseBoard, FALSE },
838   { "-autoraise", ArgFalse, (LPVOID) &appData.autoRaiseBoard, FALSE },
839 #if 0
840   { "cmailGameName", ArgString, (LPVOID) &appData.cmailGameName, FALSE },
841   { "cmail", ArgString, (LPVOID) &appData.cmailGameName, FALSE },
842 #endif
843   { "alwaysPromoteToQueen", ArgBoolean, (LPVOID) &appData.alwaysPromoteToQueen, TRUE },
844   { "queen", ArgTrue, (LPVOID) &appData.alwaysPromoteToQueen, FALSE },
845   { "xqueen", ArgFalse, (LPVOID) &appData.alwaysPromoteToQueen, FALSE },
846   { "-queen", ArgFalse, (LPVOID) &appData.alwaysPromoteToQueen, FALSE },
847   { "oldSaveStyle", ArgBoolean, (LPVOID) &appData.oldSaveStyle, TRUE },
848   { "oldsave", ArgTrue, (LPVOID) &appData.oldSaveStyle, FALSE },
849   { "xoldsave", ArgFalse, (LPVOID) &appData.oldSaveStyle, FALSE },
850   { "-oldsave", ArgFalse, (LPVOID) &appData.oldSaveStyle, FALSE },
851   { "quietPlay", ArgBoolean, (LPVOID) &appData.quietPlay, TRUE },
852   { "quiet", ArgTrue, (LPVOID) &appData.quietPlay, FALSE },
853   { "xquiet", ArgFalse, (LPVOID) &appData.quietPlay, FALSE },
854   { "-quiet", ArgFalse, (LPVOID) &appData.quietPlay, FALSE },
855   { "getMoveList", ArgBoolean, (LPVOID) &appData.getMoveList, TRUE },
856   { "moves", ArgTrue, (LPVOID) &appData.getMoveList, FALSE },
857   { "xmoves", ArgFalse, (LPVOID) &appData.getMoveList, FALSE },
858   { "-moves", ArgFalse, (LPVOID) &appData.getMoveList, FALSE },
859   { "testLegality", ArgBoolean, (LPVOID) &appData.testLegality, TRUE },
860   { "legal", ArgTrue, (LPVOID) &appData.testLegality, FALSE },
861   { "xlegal", ArgFalse, (LPVOID) &appData.testLegality, FALSE },
862   { "-legal", ArgFalse, (LPVOID) &appData.testLegality, FALSE },
863   { "premove", ArgBoolean, (LPVOID) &appData.premove, TRUE },
864   { "pre", ArgTrue, (LPVOID) &appData.premove, FALSE },
865   { "xpre", ArgFalse, (LPVOID) &appData.premove, FALSE },
866   { "-pre", ArgFalse, (LPVOID) &appData.premove, FALSE },
867   { "premoveWhite", ArgBoolean, (LPVOID) &appData.premoveWhite, TRUE },
868   { "prewhite", ArgTrue, (LPVOID) &appData.premoveWhite, FALSE },
869   { "xprewhite", ArgFalse, (LPVOID) &appData.premoveWhite, FALSE },
870   { "-prewhite", ArgFalse, (LPVOID) &appData.premoveWhite, FALSE },
871   { "premoveWhiteText", ArgString, (LPVOID) &appData.premoveWhiteText, TRUE },
872   { "premoveBlack", ArgBoolean, (LPVOID) &appData.premoveBlack, TRUE },
873   { "preblack", ArgTrue, (LPVOID) &appData.premoveBlack, FALSE },
874   { "xpreblack", ArgFalse, (LPVOID) &appData.premoveBlack, FALSE },
875   { "-preblack", ArgFalse, (LPVOID) &appData.premoveBlack, FALSE },
876   { "premoveBlackText", ArgString, (LPVOID) &appData.premoveBlackText, TRUE },
877   { "icsAlarm", ArgBoolean, (LPVOID) &appData.icsAlarm, TRUE},
878   { "alarm", ArgTrue, (LPVOID) &appData.icsAlarm, FALSE},
879   { "xalarm", ArgFalse, (LPVOID) &appData.icsAlarm, FALSE},
880   { "-alarm", ArgFalse, (LPVOID) &appData.icsAlarm, FALSE},
881   { "icsAlarmTime", ArgInt, (LPVOID) &appData.icsAlarmTime, TRUE},
882   { "localLineEditing", ArgBoolean, (LPVOID) &appData.localLineEditing, FALSE},
883   { "localLineEditing", ArgBoolean, (LPVOID) &appData.localLineEditing, FALSE},
884   { "edit", ArgTrue, (LPVOID) &appData.localLineEditing, FALSE },
885   { "xedit", ArgFalse, (LPVOID) &appData.localLineEditing, FALSE },
886   { "-edit", ArgFalse, (LPVOID) &appData.localLineEditing, FALSE },
887   { "animateMoving", ArgBoolean, (LPVOID) &appData.animate, TRUE },
888   { "animate", ArgTrue, (LPVOID) &appData.animate, FALSE },
889   { "xanimate", ArgFalse, (LPVOID) &appData.animate, FALSE },
890   { "-animate", ArgFalse, (LPVOID) &appData.animate, FALSE },
891   { "animateSpeed", ArgInt, (LPVOID) &appData.animSpeed, TRUE },
892   { "animateDragging", ArgBoolean, (LPVOID) &appData.animateDragging, TRUE },
893   { "drag", ArgTrue, (LPVOID) &appData.animateDragging, FALSE },
894   { "xdrag", ArgFalse, (LPVOID) &appData.animateDragging, FALSE },
895   { "-drag", ArgFalse, (LPVOID) &appData.animateDragging, FALSE },
896   { "blindfold", ArgBoolean, (LPVOID) &appData.blindfold, TRUE },
897   { "blind", ArgTrue, (LPVOID) &appData.blindfold, FALSE },
898   { "xblind", ArgFalse, (LPVOID) &appData.blindfold, FALSE },
899   { "-blind", ArgFalse, (LPVOID) &appData.blindfold, FALSE },
900   { "highlightLastMove", ArgBoolean,
901     (LPVOID) &appData.highlightLastMove, TRUE },
902   { "highlight", ArgTrue, (LPVOID) &appData.highlightLastMove, FALSE },
903   { "xhighlight", ArgFalse, (LPVOID) &appData.highlightLastMove, FALSE },
904   { "-highlight", ArgFalse, (LPVOID) &appData.highlightLastMove, FALSE },
905   { "highlightDragging", ArgBoolean,
906     (LPVOID) &appData.highlightDragging, TRUE },
907   { "highdrag", ArgTrue, (LPVOID) &appData.highlightDragging, FALSE },
908   { "xhighdrag", ArgFalse, (LPVOID) &appData.highlightDragging, FALSE },
909   { "-highdrag", ArgFalse, (LPVOID) &appData.highlightDragging, FALSE },
910   { "colorizeMessages", ArgBoolean, (LPVOID) &appData.colorize, TRUE },
911   { "colorize", ArgTrue, (LPVOID) &appData.colorize, FALSE },
912   { "xcolorize", ArgFalse, (LPVOID) &appData.colorize, FALSE },
913   { "-colorize", ArgFalse, (LPVOID) &appData.colorize, FALSE },
914   { "colorShout", ArgAttribs, (LPVOID) ColorShout, TRUE },
915   { "colorSShout", ArgAttribs, (LPVOID) ColorSShout, TRUE },
916   { "colorChannel1", ArgAttribs, (LPVOID) ColorChannel1, TRUE },
917   { "colorChannel", ArgAttribs, (LPVOID) ColorChannel, TRUE },
918   { "colorKibitz", ArgAttribs, (LPVOID) ColorKibitz, TRUE },
919   { "colorTell", ArgAttribs, (LPVOID) ColorTell, TRUE },
920   { "colorChallenge", ArgAttribs, (LPVOID) ColorChallenge, TRUE },
921   { "colorRequest", ArgAttribs, (LPVOID) ColorRequest, TRUE },
922   { "colorSeek", ArgAttribs, (LPVOID) ColorSeek, TRUE },
923   { "colorNormal", ArgAttribs, (LPVOID) ColorNormal, TRUE },
924   { "colorBackground", ArgColor, (LPVOID) &consoleBackgroundColor, TRUE },
925   { "soundShout", ArgFilename,
926     (LPVOID) &textAttribs[ColorShout].sound.name, TRUE },
927   { "soundSShout", ArgFilename,
928     (LPVOID) &textAttribs[ColorSShout].sound.name, TRUE },
929   { "soundChannel1", ArgFilename,
930     (LPVOID) &textAttribs[ColorChannel1].sound.name, TRUE },
931   { "soundChannel", ArgFilename,
932     (LPVOID) &textAttribs[ColorChannel].sound.name, TRUE },
933   { "soundKibitz", ArgFilename,
934     (LPVOID) &textAttribs[ColorKibitz].sound.name, TRUE },
935   { "soundTell", ArgFilename,
936     (LPVOID) &textAttribs[ColorTell].sound.name, TRUE },
937   { "soundChallenge", ArgFilename,
938     (LPVOID) &textAttribs[ColorChallenge].sound.name, TRUE },
939   { "soundRequest", ArgFilename,
940     (LPVOID) &textAttribs[ColorRequest].sound.name, TRUE },
941   { "soundSeek", ArgFilename,
942     (LPVOID) &textAttribs[ColorSeek].sound.name, TRUE },
943   { "soundMove", ArgFilename, (LPVOID) &sounds[(int)SoundMove].name, TRUE },
944   { "soundBell", ArgFilename, (LPVOID) &sounds[(int)SoundBell].name, TRUE },
945   { "soundIcsWin", ArgFilename, (LPVOID) &sounds[(int)SoundIcsWin].name,TRUE },
946   { "soundIcsLoss", ArgFilename, 
947     (LPVOID) &sounds[(int)SoundIcsLoss].name, TRUE },
948   { "soundIcsDraw", ArgFilename, 
949     (LPVOID) &sounds[(int)SoundIcsDraw].name, TRUE },
950   { "soundIcsUnfinished", ArgFilename, 
951     (LPVOID) &sounds[(int)SoundIcsUnfinished].name, TRUE},
952   { "soundIcsAlarm", ArgFilename, 
953     (LPVOID) &sounds[(int)SoundAlarm].name, TRUE },
954   { "reuseFirst", ArgBoolean, (LPVOID) &appData.reuseFirst, FALSE },
955   { "reuse", ArgTrue, (LPVOID) &appData.reuseFirst, FALSE },
956   { "xreuse", ArgFalse, (LPVOID) &appData.reuseFirst, FALSE },
957   { "-reuse", ArgFalse, (LPVOID) &appData.reuseFirst, FALSE },
958   { "reuseChessPrograms", ArgBoolean,
959     (LPVOID) &appData.reuseFirst, FALSE }, /* backward compat only */
960   { "reuseSecond", ArgBoolean, (LPVOID) &appData.reuseSecond, FALSE },
961   { "reuse2", ArgTrue, (LPVOID) &appData.reuseSecond, FALSE },
962   { "xreuse2", ArgFalse, (LPVOID) &appData.reuseSecond, FALSE },
963   { "-reuse2", ArgFalse, (LPVOID) &appData.reuseSecond, FALSE },
964   { "comPortSettings", ArgCommSettings, (LPVOID) &dcb, TRUE },
965   { "x", ArgInt, (LPVOID) &boardX, TRUE },
966   { "y", ArgInt, (LPVOID) &boardY, TRUE },
967   { "icsX", ArgInt, (LPVOID) &consoleX, TRUE },
968   { "icsY", ArgInt, (LPVOID) &consoleY, TRUE },
969   { "icsW", ArgInt, (LPVOID) &consoleW, TRUE },
970   { "icsH", ArgInt, (LPVOID) &consoleH, TRUE },
971   { "analysisX", ArgInt, (LPVOID) &analysisX, TRUE },
972   { "analysisY", ArgInt, (LPVOID) &analysisY, TRUE },
973   { "analysisW", ArgInt, (LPVOID) &analysisW, TRUE },
974   { "analysisH", ArgInt, (LPVOID) &analysisH, TRUE },
975   { "commentX", ArgInt, (LPVOID) &commentX, TRUE },
976   { "commentY", ArgInt, (LPVOID) &commentY, TRUE },
977   { "commentW", ArgInt, (LPVOID) &commentW, TRUE },
978   { "commentH", ArgInt, (LPVOID) &commentH, TRUE },
979   { "tagsX", ArgInt, (LPVOID) &editTagsX, TRUE },
980   { "tagsY", ArgInt, (LPVOID) &editTagsY, TRUE },
981   { "tagsW", ArgInt, (LPVOID) &editTagsW, TRUE },
982   { "tagsH", ArgInt, (LPVOID) &editTagsH, TRUE },
983   { "gameListX", ArgInt, (LPVOID) &gameListX, TRUE },
984   { "gameListY", ArgInt, (LPVOID) &gameListY, TRUE },
985   { "gameListW", ArgInt, (LPVOID) &gameListW, TRUE },
986   { "gameListH", ArgInt, (LPVOID) &gameListH, TRUE },
987   { "settingsFile", ArgSettingsFilename, (LPVOID) &settingsFileName, FALSE },
988   { "ini", ArgSettingsFilename, (LPVOID) &settingsFileName, FALSE },
989   { "saveSettingsOnExit", ArgBoolean, (LPVOID) &saveSettingsOnExit, TRUE },
990   { "chessProgram", ArgBoolean, (LPVOID) &chessProgram, FALSE },
991   { "cp", ArgTrue, (LPVOID) &chessProgram, FALSE },
992   { "xcp", ArgFalse, (LPVOID) &chessProgram, FALSE },
993   { "-cp", ArgFalse, (LPVOID) &chessProgram, FALSE },
994   { "icsMenu", ArgString, (LPVOID) &icsTextMenuString, TRUE },
995   { "icsNames", ArgString, (LPVOID) &icsNames, TRUE },
996   { "firstChessProgramNames", ArgString, (LPVOID) &firstChessProgramNames,
997     TRUE },
998   { "secondChessProgramNames", ArgString, (LPVOID) &secondChessProgramNames,
999     TRUE },
1000   { "initialMode", ArgString, (LPVOID) &appData.initialMode, FALSE },
1001   { "mode", ArgString, (LPVOID) &appData.initialMode, FALSE },
1002   { "variant", ArgString, (LPVOID) &appData.variant, FALSE },
1003   { "firstProtocolVersion", ArgInt, (LPVOID) &appData.firstProtocolVersion,
1004     FALSE },
1005   { "secondProtocolVersion", ArgInt, (LPVOID) &appData.secondProtocolVersion,
1006     FALSE },
1007   { "showButtonBar", ArgBoolean, (LPVOID) &appData.showButtonBar, TRUE },
1008   { "buttons", ArgTrue, (LPVOID) &appData.showButtonBar, FALSE },
1009   { "xbuttons", ArgFalse, (LPVOID) &appData.showButtonBar, FALSE },
1010   { "-buttons", ArgFalse, (LPVOID) &appData.showButtonBar, FALSE },
1011 #ifdef ZIPPY
1012   { "zippyTalk", ArgBoolean, (LPVOID) &appData.zippyTalk, FALSE },
1013   { "zt", ArgTrue, (LPVOID) &appData.zippyTalk, FALSE },
1014   { "xzt", ArgFalse, (LPVOID) &appData.zippyTalk, FALSE },
1015   { "-zt", ArgFalse, (LPVOID) &appData.zippyTalk, FALSE },
1016   { "zippyPlay", ArgBoolean, (LPVOID) &appData.zippyPlay, FALSE },
1017   { "zp", ArgTrue, (LPVOID) &appData.zippyPlay, FALSE },
1018   { "xzp", ArgFalse, (LPVOID) &appData.zippyPlay, FALSE },
1019   { "-zp", ArgFalse, (LPVOID) &appData.zippyPlay, FALSE },
1020   { "zippyLines", ArgFilename, (LPVOID) &appData.zippyLines, FALSE },
1021   { "zippyPinhead", ArgString, (LPVOID) &appData.zippyPinhead, FALSE },
1022   { "zippyPassword", ArgString, (LPVOID) &appData.zippyPassword, FALSE },
1023   { "zippyPassword2", ArgString, (LPVOID) &appData.zippyPassword2, FALSE },
1024   { "zippyWrongPassword", ArgString, (LPVOID) &appData.zippyWrongPassword,
1025     FALSE },
1026   { "zippyAcceptOnly", ArgString, (LPVOID) &appData.zippyAcceptOnly, FALSE },
1027   { "zippyUseI", ArgBoolean, (LPVOID) &appData.zippyUseI, FALSE },
1028   { "zui", ArgTrue, (LPVOID) &appData.zippyUseI, FALSE },
1029   { "xzui", ArgFalse, (LPVOID) &appData.zippyUseI, FALSE },
1030   { "-zui", ArgFalse, (LPVOID) &appData.zippyUseI, FALSE },
1031   { "zippyBughouse", ArgInt, (LPVOID) &appData.zippyBughouse, FALSE },
1032   { "zippyNoplayCrafty", ArgBoolean, (LPVOID) &appData.zippyNoplayCrafty,
1033     FALSE },
1034   { "znc", ArgTrue, (LPVOID) &appData.zippyNoplayCrafty, FALSE },
1035   { "xznc", ArgFalse, (LPVOID) &appData.zippyNoplayCrafty, FALSE },
1036   { "-znc", ArgFalse, (LPVOID) &appData.zippyNoplayCrafty, FALSE },
1037   { "zippyGameEnd", ArgString, (LPVOID) &appData.zippyGameEnd, FALSE },
1038   { "zippyGameStart", ArgString, (LPVOID) &appData.zippyGameStart, FALSE },
1039   { "zippyAdjourn", ArgBoolean, (LPVOID) &appData.zippyAdjourn, FALSE },
1040   { "zadj", ArgTrue, (LPVOID) &appData.zippyAdjourn, FALSE },
1041   { "xzadj", ArgFalse, (LPVOID) &appData.zippyAdjourn, FALSE },
1042   { "-zadj", ArgFalse, (LPVOID) &appData.zippyAdjourn, FALSE },
1043   { "zippyAbort", ArgBoolean, (LPVOID) &appData.zippyAbort, FALSE },
1044   { "zab", ArgTrue, (LPVOID) &appData.zippyAbort, FALSE },
1045   { "xzab", ArgFalse, (LPVOID) &appData.zippyAbort, FALSE },
1046   { "-zab", ArgFalse, (LPVOID) &appData.zippyAbort, FALSE },
1047   { "zippyVariants", ArgString, (LPVOID) &appData.zippyVariants, FALSE },
1048   { "zippyMaxGames", ArgInt, (LPVOID)&appData.zippyMaxGames, FALSE },
1049   { "zippyReplayTimeout", ArgInt, (LPVOID)&appData.zippyReplayTimeout, FALSE },
1050   /* Kludge to allow winboard.ini files from buggy 4.0.4 to be read: */
1051   { "zippyReplyTimeout", ArgInt, (LPVOID)&junk, FALSE },
1052 #endif
1053   { NULL, ArgNone, NULL, FALSE }
1054 };
1055
1056
1057 /* Kludge for indirection files on command line */
1058 char* lastIndirectionFilename;
1059 ArgDescriptor argDescriptorIndirection =
1060 { "", ArgSettingsFilename, (LPVOID) NULL, FALSE };
1061
1062
1063 VOID
1064 ExitArgError(char *msg, char *badArg)
1065 {
1066   char buf[MSG_SIZ];
1067
1068   sprintf(buf, "%s %s", msg, badArg);
1069   DisplayFatalError(buf, 0, 2);
1070   exit(2);
1071 }
1072
1073 /* Command line font name parser.  NULL name means do nothing.
1074    Syntax like "Courier New:10.0 bi" or "Arial:10" or "Arial:10b"
1075    For backward compatibility, syntax without the colon is also
1076    accepted, but font names with digits in them won't work in that case.
1077 */
1078 VOID
1079 ParseFontName(char *name, MyFontParams *mfp)
1080 {
1081   char *p, *q;
1082   if (name == NULL) return;
1083   p = name;
1084   q = strchr(p, ':');
1085   if (q) {
1086     if (q - p >= sizeof(mfp->faceName))
1087       ExitArgError("Font name too long:", name);
1088     memcpy(mfp->faceName, p, q - p);
1089     mfp->faceName[q - p] = NULLCHAR;
1090     p = q + 1;
1091   } else {
1092     q = mfp->faceName;
1093     while (*p && !isdigit(*p)) {
1094       *q++ = *p++;
1095       if (q - mfp->faceName >= sizeof(mfp->faceName))
1096         ExitArgError("Font name too long:", name);
1097     }
1098     while (q > mfp->faceName && q[-1] == ' ') q--;
1099     *q = NULLCHAR;
1100   }
1101   if (!*p) ExitArgError("Font point size missing:", name);
1102   mfp->pointSize = (float) atof(p);
1103   mfp->bold = (strchr(p, 'b') != NULL);
1104   mfp->italic = (strchr(p, 'i') != NULL);
1105   mfp->underline = (strchr(p, 'u') != NULL);
1106   mfp->strikeout = (strchr(p, 's') != NULL);
1107 }
1108
1109 /* Color name parser.
1110    X version accepts X color names, but this one
1111    handles only the #rrggbb form (hex) or rrr,ggg,bbb (decimal) */
1112 COLORREF
1113 ParseColorName(char *name)
1114 {
1115   int red, green, blue, count;
1116   char buf[MSG_SIZ];
1117
1118   count = sscanf(name, "#%2x%2x%2x", &red, &green, &blue);
1119   if (count != 3) {
1120     count = sscanf(name, "%3d%*[^0-9]%3d%*[^0-9]%3d", 
1121       &red, &green, &blue);
1122   }
1123   if (count != 3) {
1124     sprintf(buf, "Can't parse color name %s", name);
1125     DisplayError(buf, 0);
1126     return RGB(0, 0, 0);
1127   }
1128   return PALETTERGB(red, green, blue);
1129 }
1130
1131
1132 void ParseAttribs(COLORREF *color, int *effects, char* argValue)
1133 {
1134   char *e = argValue;
1135   int eff = 0;
1136
1137   while (*e) {
1138     if (*e == 'b')      eff |= CFE_BOLD;
1139     else if (*e == 'i') eff |= CFE_ITALIC;
1140     else if (*e == 'u') eff |= CFE_UNDERLINE;
1141     else if (*e == 's') eff |= CFE_STRIKEOUT;
1142     else if (*e == '#' || isdigit(*e)) break;
1143     e++;
1144   }
1145   *effects = eff;
1146   *color   = ParseColorName(e);
1147 }
1148
1149
1150 BoardSize
1151 ParseBoardSize(char *name)
1152 {
1153   BoardSize bs = SizeTiny;
1154   while (sizeInfo[bs].name != NULL) {
1155     if (StrCaseCmp(name, sizeInfo[bs].name) == 0) return bs;
1156     bs++;
1157   }
1158   ExitArgError("Unrecognized board size value", name);
1159   return bs; /* not reached */
1160 }
1161
1162
1163 char
1164 StringGet(void *getClosure)
1165 {
1166   char **p = (char **) getClosure;
1167   return *((*p)++);
1168 }
1169
1170 char
1171 FileGet(void *getClosure)
1172 {
1173   int c;
1174   FILE* f = (FILE*) getClosure;
1175
1176   c = getc(f);
1177   if (c == EOF)
1178     return NULLCHAR;
1179   else
1180     return (char) c;
1181 }
1182
1183 /* Parse settings file named "name". If file found, return the
1184    full name in fullname and return TRUE; else return FALSE */
1185 BOOLEAN
1186 ParseSettingsFile(char *name, char fullname[MSG_SIZ])
1187 {
1188   char *dummy;
1189   FILE *f;
1190
1191   if (SearchPath(installDir, name, NULL, MSG_SIZ, fullname, &dummy)) {
1192     f = fopen(fullname, "r");
1193     if (f != NULL) {
1194       ParseArgs(FileGet, f);
1195       fclose(f);
1196       return TRUE;
1197     }
1198   }
1199   return FALSE;
1200 }
1201
1202 VOID
1203 ParseArgs(GetFunc get, void *cl)
1204 {
1205   char argName[ARG_MAX];
1206   char argValue[ARG_MAX];
1207   ArgDescriptor *ad;
1208   char start;
1209   char *q;
1210   int i, octval;
1211   char ch;
1212   int posarg = 0;
1213
1214   ch = get(cl);
1215   for (;;) {
1216     while (ch == ' ' || ch == '\n' || ch == '\t') ch = get(cl);
1217     if (ch == NULLCHAR) break;
1218     if (ch == ';') {
1219       /* Comment to end of line */
1220       ch = get(cl);
1221       while (ch != '\n' && ch != NULLCHAR) ch = get(cl);
1222       continue;
1223     } else if (ch == '/' || ch == '-') {
1224       /* Switch */
1225       q = argName;
1226       while (ch != ' ' && ch != '=' && ch != ':' && ch != NULLCHAR &&
1227              ch != '\n' && ch != '\t') {
1228         *q++ = ch;
1229         ch = get(cl);
1230       }
1231       *q = NULLCHAR;
1232
1233       for (ad = argDescriptors; ad->argName != NULL; ad++)
1234         if (strcmp(ad->argName, argName + 1) == 0) break;
1235
1236       if (ad->argName == NULL)
1237         ExitArgError("Unrecognized argument", argName);
1238
1239     } else if (ch == '@') {
1240       /* Indirection file */
1241       ad = &argDescriptorIndirection;
1242       ch = get(cl);
1243     } else {
1244       /* Positional argument */
1245       ad = &argDescriptors[posarg++];
1246       strcpy(argName, ad->argName);
1247     }
1248
1249     if (ad->argType == ArgTrue) {
1250       *(Boolean *) ad->argLoc = TRUE;
1251       continue;
1252     }
1253     if (ad->argType == ArgFalse) {
1254       *(Boolean *) ad->argLoc = FALSE;
1255       continue;
1256     }
1257
1258     while (ch == ' ' || ch == '=' || ch == ':' || ch == '\t') ch = get(cl);
1259     if (ch == NULLCHAR || ch == '\n') {
1260       ExitArgError("No value provided for argument", argName);
1261     }
1262     q = argValue;
1263     if (ch == '{') {
1264       // Quoting with { }.  No characters have to (or can) be escaped.
1265       // Thus the string cannot contain a '}' character.
1266       start = ch;
1267       ch = get(cl);
1268       while (start) {
1269         switch (ch) {
1270         case NULLCHAR:
1271           start = NULLCHAR;
1272           break;
1273           
1274         case '}':
1275           ch = get(cl);
1276           start = NULLCHAR;
1277           break;
1278
1279         default:
1280           *q++ = ch;
1281           ch = get(cl);
1282           break;
1283         }
1284       }   
1285     } else if (ch == '\'' || ch == '"') {
1286       // Quoting with ' ' or " ", with \ as escape character.
1287       // Inconvenient for long strings that may contain Windows filenames.
1288       start = ch;
1289       ch = get(cl);
1290       while (start) {
1291         switch (ch) {
1292         case NULLCHAR:
1293           start = NULLCHAR;
1294           break;
1295
1296         default:
1297         not_special:
1298           *q++ = ch;
1299           ch = get(cl);
1300           break;
1301
1302         case '\'':
1303         case '\"':
1304           if (ch == start) {
1305             ch = get(cl);
1306             start = NULLCHAR;
1307             break;
1308           } else {
1309             goto not_special;
1310           }
1311
1312         case '\\':
1313           if (ad->argType == ArgFilename
1314               || ad->argType == ArgSettingsFilename) {
1315               goto not_special;
1316           }
1317           ch = get(cl);
1318           switch (ch) {
1319           case NULLCHAR:
1320             ExitArgError("Incomplete \\ escape in value for", argName);
1321             break;
1322           case 'n':
1323             *q++ = '\n';
1324             ch = get(cl);
1325             break;
1326           case 'r':
1327             *q++ = '\r';
1328             ch = get(cl);
1329             break;
1330           case 't':
1331             *q++ = '\t';
1332             ch = get(cl);
1333             break;
1334           case 'b':
1335             *q++ = '\b';
1336             ch = get(cl);
1337             break;
1338           case 'f':
1339             *q++ = '\f';
1340             ch = get(cl);
1341             break;
1342           default:
1343             octval = 0;
1344             for (i = 0; i < 3; i++) {
1345               if (ch >= '0' && ch <= '7') {
1346                 octval = octval*8 + (ch - '0');
1347                 ch = get(cl);
1348               } else {
1349                 break;
1350               }
1351             }
1352             if (i > 0) {
1353               *q++ = (char) octval;
1354             } else {
1355               *q++ = ch;
1356               ch = get(cl);
1357             }
1358             break;
1359           }
1360           break;
1361         }
1362       }
1363     } else {
1364       while (ch != ' ' && ch != NULLCHAR && ch != '\t' && ch != '\n') {
1365         *q++ = ch;
1366         ch = get(cl);
1367       }
1368     }
1369     *q = NULLCHAR;
1370
1371     switch (ad->argType) {
1372     case ArgInt:
1373       *(int *) ad->argLoc = atoi(argValue);
1374       break;
1375
1376     case ArgFloat:
1377       *(float *) ad->argLoc = (float) atof(argValue);
1378       break;
1379
1380     case ArgString:
1381     case ArgFilename:
1382       *(char **) ad->argLoc = strdup(argValue);
1383       break;
1384
1385     case ArgSettingsFilename:
1386       {
1387         char fullname[MSG_SIZ];
1388         if (ParseSettingsFile(argValue, fullname)) {
1389           if (ad->argLoc != NULL) {
1390             *(char **) ad->argLoc = strdup(fullname);
1391           }
1392         } else {
1393           if (ad->argLoc != NULL) {
1394           } else {
1395             ExitArgError("Failed to open indirection file", argValue);
1396           }
1397         }
1398       }
1399       break;
1400
1401     case ArgBoolean:
1402       switch (argValue[0]) {
1403       case 't':
1404       case 'T':
1405         *(Boolean *) ad->argLoc = TRUE;
1406         break;
1407       case 'f':
1408       case 'F':
1409         *(Boolean *) ad->argLoc = FALSE;
1410         break;
1411       default:
1412         ExitArgError("Unrecognized boolean argument value", argValue);
1413         break;
1414       }
1415       break;
1416
1417     case ArgColor:
1418       *(COLORREF *)ad->argLoc = ParseColorName(argValue);
1419       break;
1420
1421     case ArgAttribs: {
1422       ColorClass cc = (ColorClass)ad->argLoc;
1423       ParseAttribs(&textAttribs[cc].color, &textAttribs[cc].effects, argValue);
1424       }
1425       break;
1426       
1427     case ArgBoardSize:
1428       *(BoardSize *)ad->argLoc = ParseBoardSize(argValue);
1429       break;
1430
1431     case ArgFont:
1432       ParseFontName(argValue, &font[boardSize][(int)ad->argLoc]->mfp);
1433       break;
1434
1435     case ArgCommSettings:
1436       ParseCommSettings(argValue, &dcb);
1437       break;
1438
1439     case ArgNone:
1440       ExitArgError("Unrecognized argument", argValue);
1441       break;
1442     }
1443   }
1444 }
1445
1446 VOID
1447 LFfromMFP(LOGFONT* lf, MyFontParams *mfp)
1448 {
1449   HDC hdc = CreateDC("DISPLAY", NULL, NULL, NULL);
1450   lf->lfHeight = -(int)(mfp->pointSize * GetDeviceCaps(hdc, LOGPIXELSY) / 72.0 + 0.5);
1451   DeleteDC(hdc);
1452   lf->lfWidth = 0;
1453   lf->lfEscapement = 0;
1454   lf->lfOrientation = 0;
1455   lf->lfWeight = mfp->bold ? FW_BOLD : FW_NORMAL;
1456   lf->lfItalic = mfp->italic;
1457   lf->lfUnderline = mfp->underline;
1458   lf->lfStrikeOut = mfp->strikeout;
1459   lf->lfCharSet = DEFAULT_CHARSET;
1460   lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
1461   lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
1462   lf->lfQuality = DEFAULT_QUALITY;
1463   lf->lfPitchAndFamily = DEFAULT_PITCH|FF_DONTCARE;
1464   strcpy(lf->lfFaceName, mfp->faceName);
1465 }
1466
1467 VOID
1468 CreateFontInMF(MyFont *mf)
1469 {
1470   LFfromMFP(&mf->lf, &mf->mfp);
1471   if (mf->hf) DeleteObject(mf->hf);
1472   mf->hf = CreateFontIndirect(&mf->lf);
1473 }
1474
1475 VOID
1476 SetDefaultTextAttribs()
1477 {
1478   ColorClass cc;
1479   for (cc = (ColorClass)0; cc < NColorClasses; cc++) {
1480     ParseAttribs(&textAttribs[cc].color, 
1481                  &textAttribs[cc].effects, 
1482                  defaultTextAttribs[cc]);
1483   }
1484 }
1485
1486 VOID
1487 SetDefaultSounds()
1488 {
1489   ColorClass cc;
1490   SoundClass sc;
1491   for (cc = (ColorClass)0; cc < NColorClasses; cc++) {
1492     textAttribs[cc].sound.name = strdup("");
1493     textAttribs[cc].sound.data = NULL;
1494   }
1495   for (sc = (SoundClass)0; sc < NSoundClasses; sc++) {
1496     sounds[sc].name = strdup("");
1497     sounds[sc].data = NULL;
1498   }
1499   sounds[(int)SoundBell].name = strdup(SOUND_BELL);
1500 }
1501
1502 VOID
1503 LoadAllSounds()
1504 {
1505   ColorClass cc;
1506   SoundClass sc;
1507   for (cc = (ColorClass)0; cc < NColorClasses; cc++) {
1508     MyLoadSound(&textAttribs[cc].sound);
1509   }
1510   for (sc = (SoundClass)0; sc < NSoundClasses; sc++) {
1511     MyLoadSound(&sounds[sc]);
1512   }
1513 }
1514
1515 VOID
1516 InitAppData(LPSTR lpCmdLine)
1517 {
1518   int i, j;
1519   char buf[ARG_MAX], currDir[MSG_SIZ];
1520   char *dummy, *p;
1521
1522   programName = szAppName;
1523
1524   /* Initialize to defaults */
1525   lightSquareColor = ParseColorName(LIGHT_SQUARE_COLOR);
1526   darkSquareColor = ParseColorName(DARK_SQUARE_COLOR);
1527   whitePieceColor = ParseColorName(WHITE_PIECE_COLOR);
1528   blackPieceColor = ParseColorName(BLACK_PIECE_COLOR);
1529   highlightSquareColor = ParseColorName(HIGHLIGHT_SQUARE_COLOR);
1530   premoveHighlightColor = ParseColorName(PREMOVE_HIGHLIGHT_COLOR);
1531   consoleBackgroundColor = ParseColorName(COLOR_BKGD);
1532   SetDefaultTextAttribs();
1533   SetDefaultSounds();
1534   appData.movesPerSession = MOVES_PER_SESSION;
1535   appData.initString = INIT_STRING;
1536   appData.secondInitString = INIT_STRING;
1537   appData.firstComputerString = COMPUTER_STRING;
1538   appData.secondComputerString = COMPUTER_STRING;
1539   appData.firstChessProgram = FIRST_CHESS_PROGRAM;
1540   appData.secondChessProgram = SECOND_CHESS_PROGRAM;
1541   appData.firstPlaysBlack = FALSE;
1542   appData.noChessProgram = FALSE;
1543   chessProgram = FALSE;
1544   appData.firstHost = FIRST_HOST;
1545   appData.secondHost = SECOND_HOST;
1546   appData.firstDirectory = FIRST_DIRECTORY;
1547   appData.secondDirectory = SECOND_DIRECTORY;
1548   appData.bitmapDirectory = "";
1549   appData.remoteShell = REMOTE_SHELL;
1550   appData.remoteUser = "";
1551   appData.timeDelay = TIME_DELAY;
1552   appData.timeControl = TIME_CONTROL;
1553   appData.timeIncrement = TIME_INCREMENT;
1554   appData.icsActive = FALSE;
1555   appData.icsHost = "";
1556   appData.icsPort = ICS_PORT;
1557   appData.icsCommPort = ICS_COMM_PORT;
1558   appData.icsLogon = ICS_LOGON;
1559   appData.icsHelper = "";
1560   appData.useTelnet = FALSE;
1561   appData.telnetProgram = TELNET_PROGRAM;
1562   appData.gateway = "";
1563   appData.loadGameFile = "";
1564   appData.loadGameIndex = 0;
1565   appData.saveGameFile = "";
1566   appData.autoSaveGames = FALSE;
1567   appData.loadPositionFile = "";
1568   appData.loadPositionIndex = 1;
1569   appData.savePositionFile = "";
1570   appData.matchMode = FALSE;
1571   appData.matchGames = 0;
1572   appData.monoMode = FALSE;
1573   appData.debugMode = FALSE;
1574   appData.clockMode = TRUE;
1575   boardSize = (BoardSize) -1; /* determine by screen size */
1576   appData.Iconic = FALSE; /*unused*/
1577   appData.searchTime = "";
1578   appData.searchDepth = 0;
1579   appData.showCoords = FALSE;
1580   appData.ringBellAfterMoves = TRUE; /*obsolete in WinBoard*/
1581   appData.autoCallFlag = FALSE;
1582   appData.flipView = FALSE;
1583   appData.autoFlipView = TRUE;
1584   appData.cmailGameName = "";
1585   appData.alwaysPromoteToQueen = FALSE;
1586   appData.oldSaveStyle = FALSE;
1587   appData.quietPlay = FALSE;
1588   appData.showThinking = FALSE;
1589   appData.ponderNextMove = TRUE;
1590   appData.periodicUpdates = TRUE;
1591   appData.popupExitMessage = TRUE;
1592   appData.popupMoveErrors = FALSE;
1593   appData.autoObserve = FALSE;
1594   appData.autoComment = FALSE;
1595   appData.animate = TRUE;
1596   appData.animSpeed = 10;
1597   appData.animateDragging = TRUE;
1598   appData.highlightLastMove = TRUE;
1599   appData.getMoveList = TRUE;
1600   appData.testLegality = TRUE;
1601   appData.premove = TRUE;
1602   appData.premoveWhite = FALSE;
1603   appData.premoveWhiteText = "";
1604   appData.premoveBlack = FALSE;
1605   appData.premoveBlackText = "";
1606   appData.icsAlarm = TRUE;
1607   appData.icsAlarmTime = 5000;
1608   appData.autoRaiseBoard = TRUE;
1609   appData.localLineEditing = TRUE;
1610   appData.colorize = TRUE;
1611   appData.reuseFirst = TRUE;
1612   appData.reuseSecond = TRUE;
1613   appData.blindfold = FALSE;
1614   dcb.DCBlength = sizeof(DCB);
1615   dcb.BaudRate = 9600;
1616   dcb.fBinary = TRUE;
1617   dcb.fParity = FALSE;
1618   dcb.fOutxCtsFlow = FALSE;
1619   dcb.fOutxDsrFlow = FALSE;
1620   dcb.fDtrControl = DTR_CONTROL_ENABLE;
1621   dcb.fDsrSensitivity = FALSE;
1622   dcb.fTXContinueOnXoff = TRUE;
1623   dcb.fOutX = FALSE;
1624   dcb.fInX = FALSE;
1625   dcb.fNull = FALSE;
1626   dcb.fRtsControl = RTS_CONTROL_ENABLE;
1627   dcb.fAbortOnError = FALSE;
1628   /* Microsoft SDK >= Feb. 2003 (MS VS >= 2002) */
1629   #if (defined(_MSC_VER) && _MSC_VER <= 1200) 
1630         dcb.wReserved = 0;
1631   #else
1632     dcb.wReserved = 0;
1633   #endif
1634   dcb.ByteSize = 7;
1635   dcb.Parity = SPACEPARITY;
1636   dcb.StopBits = ONESTOPBIT;
1637   settingsFileName = SETTINGS_FILE;
1638   saveSettingsOnExit = TRUE;
1639   boardX = CW_USEDEFAULT;
1640   boardY = CW_USEDEFAULT;
1641   consoleX = CW_USEDEFAULT; 
1642   consoleY = CW_USEDEFAULT; 
1643   consoleW = CW_USEDEFAULT;
1644   consoleH = CW_USEDEFAULT;
1645   analysisX = CW_USEDEFAULT; 
1646   analysisY = CW_USEDEFAULT; 
1647   analysisW = CW_USEDEFAULT;
1648   analysisH = CW_USEDEFAULT;
1649   commentX = CW_USEDEFAULT; 
1650   commentY = CW_USEDEFAULT; 
1651   commentW = CW_USEDEFAULT;
1652   commentH = CW_USEDEFAULT;
1653   editTagsX = CW_USEDEFAULT; 
1654   editTagsY = CW_USEDEFAULT; 
1655   editTagsW = CW_USEDEFAULT;
1656   editTagsH = CW_USEDEFAULT;
1657   gameListX = CW_USEDEFAULT; 
1658   gameListY = CW_USEDEFAULT; 
1659   gameListW = CW_USEDEFAULT;
1660   gameListH = CW_USEDEFAULT;
1661   icsTextMenuString = ICS_TEXT_MENU_DEFAULT;
1662   icsNames = ICS_NAMES;
1663   firstChessProgramNames = FCP_NAMES;
1664   secondChessProgramNames = SCP_NAMES;
1665   appData.initialMode = "";
1666   appData.variant = "normal";
1667   appData.firstProtocolVersion = PROTOVER;
1668   appData.secondProtocolVersion = PROTOVER;
1669   appData.showButtonBar = TRUE;
1670 #ifdef ZIPPY
1671   appData.zippyTalk = ZIPPY_TALK;
1672   appData.zippyPlay = ZIPPY_PLAY;
1673   appData.zippyLines = ZIPPY_LINES;
1674   appData.zippyPinhead = ZIPPY_PINHEAD;
1675   appData.zippyPassword = ZIPPY_PASSWORD;
1676   appData.zippyPassword2 = ZIPPY_PASSWORD2;
1677   appData.zippyWrongPassword = ZIPPY_WRONG_PASSWORD;
1678   appData.zippyAcceptOnly = ZIPPY_ACCEPT_ONLY;
1679   appData.zippyUseI = ZIPPY_USE_I;
1680   appData.zippyBughouse = ZIPPY_BUGHOUSE;
1681   appData.zippyNoplayCrafty = ZIPPY_NOPLAY_CRAFTY;
1682   appData.zippyGameEnd = ZIPPY_GAME_END;
1683   appData.zippyGameStart = ZIPPY_GAME_START;
1684   appData.zippyAdjourn = ZIPPY_ADJOURN;
1685   appData.zippyAbort = ZIPPY_ABORT;
1686   appData.zippyVariants = ZIPPY_VARIANTS;
1687   appData.zippyMaxGames = ZIPPY_MAX_GAMES;
1688   appData.zippyReplayTimeout = ZIPPY_REPLAY_TIMEOUT;
1689 #endif
1690
1691   /* Point font array elements to structures and
1692      parse default font names */
1693   for (i=0; i<NUM_FONTS; i++) {
1694     for (j=0; j<NUM_SIZES; j++) {
1695       font[j][i] = &fontRec[j][i];
1696       ParseFontName(font[j][i]->def, &font[j][i]->mfp);
1697     }
1698   }
1699   
1700   /* Parse default settings file if any */
1701   if (ParseSettingsFile(settingsFileName, buf)) {
1702     settingsFileName = strdup(buf);
1703   }
1704
1705   /* Parse command line */
1706   ParseArgs(StringGet, &lpCmdLine);
1707
1708   /* Propagate options that affect others */
1709   if (appData.matchMode || appData.matchGames) chessProgram = TRUE;
1710   if (appData.icsActive || appData.noChessProgram) {
1711      chessProgram = FALSE;  /* not local chess program mode */
1712   }
1713
1714   /* Open startup dialog if needed */
1715   if ((!appData.noChessProgram && !chessProgram && !appData.icsActive) ||
1716       (appData.icsActive && *appData.icsHost == NULLCHAR) ||
1717       (chessProgram && (*appData.firstChessProgram == NULLCHAR ||
1718                         *appData.secondChessProgram == NULLCHAR))) {
1719     FARPROC lpProc;
1720     
1721     lpProc = MakeProcInstance((FARPROC)StartupDialog, hInst);
1722     DialogBox(hInst, MAKEINTRESOURCE(DLG_Startup), NULL, (DLGPROC)lpProc);
1723     FreeProcInstance(lpProc);
1724   }
1725
1726   /* Make sure save files land in the right (?) directory */
1727   if (GetFullPathName(appData.saveGameFile, MSG_SIZ, buf, &dummy)) {
1728     appData.saveGameFile = strdup(buf);
1729   }
1730   if (GetFullPathName(appData.savePositionFile, MSG_SIZ, buf, &dummy)) {
1731     appData.savePositionFile = strdup(buf);
1732   }
1733
1734   /* Finish initialization for fonts and sounds */
1735   for (i=0; i<NUM_FONTS; i++) {
1736     for (j=0; j<NUM_SIZES; j++) {
1737       CreateFontInMF(font[j][i]);
1738     }
1739   }
1740   /* xboard, and older WinBoards, controlled the move sound with the
1741      appData.ringBellAfterMoves option.  In the current WinBoard, we
1742      always turn the option on (so that the backend will call us),
1743      then let the user turn the sound off by setting it to silence if
1744      desired.  To accommodate old winboard.ini files saved by old
1745      versions of WinBoard, we also turn off the sound if the option
1746      was initially set to false. */
1747   if (!appData.ringBellAfterMoves) {
1748     sounds[(int)SoundMove].name = strdup("");
1749     appData.ringBellAfterMoves = TRUE;
1750   }
1751   GetCurrentDirectory(MSG_SIZ, currDir);
1752   SetCurrentDirectory(installDir);
1753   LoadAllSounds();
1754   SetCurrentDirectory(currDir);
1755
1756   p = icsTextMenuString;
1757   if (p[0] == '@') {
1758     FILE* f = fopen(p + 1, "r");
1759     if (f == NULL) {
1760       DisplayFatalError(p + 1, errno, 2);
1761       return;
1762     }
1763     i = fread(buf, 1, sizeof(buf)-1, f);
1764     fclose(f);
1765     buf[i] = NULLCHAR;
1766     p = buf;
1767   }
1768   ParseIcsTextMenu(strdup(p));
1769 }
1770
1771
1772 VOID
1773 InitMenuChecks()
1774 {
1775   HMENU hmenu = GetMenu(hwndMain);
1776
1777   (void) EnableMenuItem(hmenu, IDM_CommPort,
1778                         MF_BYCOMMAND|((appData.icsActive &&
1779                                        *appData.icsCommPort != NULLCHAR) ?
1780                                       MF_ENABLED : MF_GRAYED));
1781   (void) CheckMenuItem(hmenu, IDM_SaveSettingsOnExit,
1782                        MF_BYCOMMAND|(saveSettingsOnExit ?
1783                                      MF_CHECKED : MF_UNCHECKED));
1784 }
1785
1786
1787 VOID
1788 SaveSettings(char* name)
1789 {
1790   FILE *f;
1791   ArgDescriptor *ad;
1792   WINDOWPLACEMENT wp;
1793   char dir[MSG_SIZ];
1794
1795   if (!hwndMain) return;
1796
1797   GetCurrentDirectory(MSG_SIZ, dir);
1798   SetCurrentDirectory(installDir);
1799   f = fopen(name, "w");
1800   SetCurrentDirectory(dir);
1801   if (f == NULL) {
1802     DisplayError(name, errno);
1803     return;
1804   }
1805   fprintf(f, ";\n");
1806   fprintf(f, "; %s %s.%s Save Settings file\n", PRODUCT, VERSION, PATCHLEVEL);
1807   fprintf(f, ";\n");
1808   fprintf(f, "; You can edit the values of options that are already set in this file,\n");
1809   fprintf(f, "; but if you add other options, the next Save Settings will not save them.\n");
1810   fprintf(f, "; Use a shortcut, an @indirection file, or a .bat file instead.\n");
1811   fprintf(f, ";\n");
1812
1813   wp.length = sizeof(WINDOWPLACEMENT);
1814   GetWindowPlacement(hwndMain, &wp);
1815   boardX = wp.rcNormalPosition.left;
1816   boardY = wp.rcNormalPosition.top;
1817
1818   if (hwndConsole) {
1819     GetWindowPlacement(hwndConsole, &wp);
1820     consoleX = wp.rcNormalPosition.left;
1821     consoleY = wp.rcNormalPosition.top;
1822     consoleW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
1823     consoleH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
1824   }
1825
1826   if (analysisDialog) {
1827     GetWindowPlacement(analysisDialog, &wp);
1828     analysisX = wp.rcNormalPosition.left;
1829     analysisY = wp.rcNormalPosition.top;
1830     analysisW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
1831     analysisH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
1832   }
1833
1834   if (commentDialog) {
1835     GetWindowPlacement(commentDialog, &wp);
1836     commentX = wp.rcNormalPosition.left;
1837     commentY = wp.rcNormalPosition.top;
1838     commentW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
1839     commentH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
1840   }
1841
1842   if (editTagsDialog) {
1843     GetWindowPlacement(editTagsDialog, &wp);
1844     editTagsX = wp.rcNormalPosition.left;
1845     editTagsY = wp.rcNormalPosition.top;
1846     editTagsW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
1847     editTagsH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
1848   }
1849
1850   if (gameListDialog) {
1851     GetWindowPlacement(gameListDialog, &wp);
1852     gameListX = wp.rcNormalPosition.left;
1853     gameListY = wp.rcNormalPosition.top;
1854     gameListW = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
1855     gameListH = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
1856   }
1857
1858   for (ad = argDescriptors; ad->argName != NULL; ad++) {
1859     if (!ad->save) continue;
1860     switch (ad->argType) {
1861     case ArgString:
1862       {
1863         char *p = *(char **)ad->argLoc;
1864         if ((strchr(p, '\\') || strchr(p, '\n')) && !strchr(p, '}')) {
1865           /* Quote multiline values or \-containing values
1866              with { } if possible */
1867           fprintf(f, "/%s={%s}\n", ad->argName, p);
1868         } else {
1869           /* Else quote with " " */
1870           fprintf(f, "/%s=\"", ad->argName);
1871           while (*p) {
1872             if (*p == '\n') fprintf(f, "\n");
1873             else if (*p == '\r') fprintf(f, "\\r");
1874             else if (*p == '\t') fprintf(f, "\\t");
1875             else if (*p == '\b') fprintf(f, "\\b");
1876             else if (*p == '\f') fprintf(f, "\\f");
1877             else if (*p < ' ') fprintf(f, "\\%03o", *p);
1878             else if (*p == '\"') fprintf(f, "\\\"");
1879             else if (*p == '\\') fprintf(f, "\\\\");
1880             else putc(*p, f);
1881             p++;
1882           }
1883           fprintf(f, "\"\n");
1884         }
1885       }
1886       break;
1887     case ArgInt:
1888       fprintf(f, "/%s=%d\n", ad->argName, *(int *)ad->argLoc);
1889       break;
1890     case ArgFloat:
1891       fprintf(f, "/%s=%g\n", ad->argName, *(float *)ad->argLoc);
1892       break;
1893     case ArgBoolean:
1894       fprintf(f, "/%s=%s\n", ad->argName, 
1895         (*(Boolean *)ad->argLoc) ? "true" : "false");
1896       break;
1897     case ArgTrue:
1898       if (*(Boolean *)ad->argLoc) fprintf(f, "/%s\n", ad->argName);
1899       break;
1900     case ArgFalse:
1901       if (!*(Boolean *)ad->argLoc) fprintf(f, "/%s\n", ad->argName);
1902       break;
1903     case ArgColor:
1904       {
1905         COLORREF color = *(COLORREF *)ad->argLoc;
1906         fprintf(f, "/%s=#%02x%02x%02x\n", ad->argName, 
1907           color&0xff, (color>>8)&0xff, (color>>16)&0xff);
1908       }
1909       break;
1910     case ArgAttribs:
1911       {
1912         MyTextAttribs* ta = &textAttribs[(ColorClass)ad->argLoc];
1913         fprintf(f, "/%s=\"%s%s%s%s%s#%02x%02x%02x\"\n", ad->argName,
1914           (ta->effects & CFE_BOLD) ? "b" : "",
1915           (ta->effects & CFE_ITALIC) ? "i" : "",
1916           (ta->effects & CFE_UNDERLINE) ? "u" : "",
1917           (ta->effects & CFE_STRIKEOUT) ? "s" : "",
1918           (ta->effects) ? " " : "",
1919           ta->color&0xff, (ta->color >> 8)&0xff, (ta->color >> 16)&0xff);
1920       }
1921       break;
1922     case ArgFilename:
1923       if (strchr(*(char **)ad->argLoc, '\"')) {
1924         fprintf(f, "/%s='%s'\n", ad->argName, *(char **)ad->argLoc);
1925       } else {
1926         fprintf(f, "/%s=\"%s\"\n", ad->argName, *(char **)ad->argLoc);
1927       }
1928       break;
1929     case ArgBoardSize:
1930       fprintf(f, "/%s=%s\n", ad->argName,
1931               sizeInfo[*(BoardSize *)ad->argLoc].name);
1932       break;
1933     case ArgFont:
1934       {
1935         int bs;
1936         for (bs=0; bs<NUM_SIZES; bs++) {
1937           MyFontParams *mfp = &font[bs][(int) ad->argLoc]->mfp;
1938           fprintf(f, "/size=%s ", sizeInfo[bs].name);
1939           fprintf(f, "/%s=\"%s:%g%s%s%s%s%s\"\n",
1940             ad->argName, mfp->faceName, mfp->pointSize,
1941             mfp->bold || mfp->italic || mfp->underline || mfp->strikeout ? " " : "",
1942             mfp->bold ? "b" : "",
1943             mfp->italic ? "i" : "",
1944             mfp->underline ? "u" : "",
1945             mfp->strikeout ? "s" : "");
1946         }
1947       }
1948       break;
1949     case ArgCommSettings:
1950       PrintCommSettings(f, ad->argName, (DCB *)ad->argLoc);
1951     }
1952   }
1953   fclose(f);
1954 }
1955
1956
1957
1958 /*---------------------------------------------------------------------------*\
1959  *
1960  * GDI board drawing routines
1961  *
1962 \*---------------------------------------------------------------------------*/
1963
1964 HBITMAP
1965 DoLoadBitmap(HINSTANCE hinst, char *piece, int squareSize, char *suffix)
1966 {
1967   char name[128];
1968
1969   sprintf(name, "%s%d%s", piece, squareSize, suffix);
1970   if (gameInfo.event &&
1971       strcmp(gameInfo.event, "Easter Egg Hunt") == 0 &&
1972       strcmp(name, "k80s") == 0) {
1973     strcpy(name, "tim");
1974   }
1975   return LoadBitmap(hinst, name);
1976 }
1977
1978
1979 /* Insert a color into the program's logical palette
1980    structure.  This code assumes the given color is
1981    the result of the RGB or PALETTERGB macro, and it
1982    knows how those macros work (which is documented).
1983 */
1984 VOID
1985 InsertInPalette(COLORREF color)
1986 {
1987   LPPALETTEENTRY pe = &(pLogPal->palPalEntry[pLogPal->palNumEntries]);
1988
1989   if (pLogPal->palNumEntries++ >= PALETTESIZE) {
1990     DisplayFatalError("Too many colors", 0, 1);
1991     pLogPal->palNumEntries--;
1992     return;
1993   }
1994
1995   pe->peFlags = (char) 0;
1996   pe->peRed = (char) (0xFF & color);
1997   pe->peGreen = (char) (0xFF & (color >> 8));
1998   pe->peBlue = (char) (0xFF & (color >> 16));
1999   return;
2000 }
2001
2002
2003 VOID
2004 InitDrawingColors()
2005 {
2006   if (pLogPal == NULL) {
2007     /* Allocate enough memory for a logical palette with
2008      * PALETTESIZE entries and set the size and version fields
2009      * of the logical palette structure.
2010      */
2011     pLogPal = (NPLOGPALETTE)
2012       LocalAlloc(LMEM_FIXED, (sizeof(LOGPALETTE) +
2013                               (sizeof(PALETTEENTRY) * (PALETTESIZE))));
2014     pLogPal->palVersion    = 0x300;
2015   }
2016   pLogPal->palNumEntries = 0;
2017
2018   InsertInPalette(lightSquareColor);
2019   InsertInPalette(darkSquareColor);
2020   InsertInPalette(whitePieceColor);
2021   InsertInPalette(blackPieceColor);
2022   InsertInPalette(highlightSquareColor);
2023   InsertInPalette(premoveHighlightColor);
2024
2025   /*  create a logical color palette according the information
2026    *  in the LOGPALETTE structure.
2027    */
2028   hPal = CreatePalette((LPLOGPALETTE) pLogPal);
2029
2030   lightSquareBrush = CreateSolidBrush(lightSquareColor);
2031   darkSquareBrush = CreateSolidBrush(darkSquareColor);
2032   whitePieceBrush = CreateSolidBrush(whitePieceColor);
2033   blackPieceBrush = CreateSolidBrush(blackPieceColor);
2034   iconBkgndBrush = CreateSolidBrush(GetSysColor(COLOR_BACKGROUND));
2035 }
2036
2037
2038 int
2039 BoardWidth(int boardSize)
2040 {
2041   return (BOARD_SIZE + 1) * sizeInfo[boardSize].lineGap +
2042           BOARD_SIZE * sizeInfo[boardSize].squareSize;
2043 }
2044
2045 /* Respond to board resize by dragging edge */
2046 VOID
2047 ResizeBoard(int newSizeX, int newSizeY, int flags)
2048 {
2049   BoardSize newSize = NUM_SIZES - 1;
2050   static int recurse = 0;
2051   if (IsIconic(hwndMain)) return;
2052   if (recurse > 0) return;
2053   recurse++;
2054   while (newSize > 0 &&
2055          (newSizeX < sizeInfo[newSize].cliWidth ||
2056           newSizeY < sizeInfo[newSize].cliHeight)) {
2057     newSize--;
2058   } 
2059   boardSize = newSize;
2060   InitDrawingSizes(boardSize, flags);
2061   recurse--;
2062 }
2063
2064
2065
2066 VOID
2067 InitDrawingSizes(BoardSize boardSize, int flags)
2068 {
2069   int i, boardWidth;
2070   ChessSquare piece;
2071   static int oldBoardSize = -1, oldTinyLayout = 0;
2072   HDC hdc;
2073   SIZE clockSize, messageSize;
2074   HFONT oldFont;
2075   char buf[MSG_SIZ];
2076   char *str;
2077   HMENU hmenu = GetMenu(hwndMain);
2078   RECT crect, wrect;
2079   int offby;
2080   LOGBRUSH logbrush;
2081
2082   tinyLayout = sizeInfo[boardSize].tinyLayout;
2083   smallLayout = sizeInfo[boardSize].smallLayout;
2084   squareSize = sizeInfo[boardSize].squareSize;
2085   lineGap = sizeInfo[boardSize].lineGap;
2086
2087   if (tinyLayout != oldTinyLayout) {
2088     long style = GetWindowLong(hwndMain, GWL_STYLE);
2089     if (tinyLayout) {
2090       style &= ~WS_SYSMENU;
2091       InsertMenu(hmenu, IDM_Exit, MF_BYCOMMAND, IDM_Minimize,
2092                  "&Minimize\tCtrl+F4");
2093     } else {
2094       style |= WS_SYSMENU;
2095       RemoveMenu(hmenu, IDM_Minimize, MF_BYCOMMAND);
2096     }
2097     SetWindowLong(hwndMain, GWL_STYLE, style);
2098
2099     for (i=0; menuBarText[tinyLayout][i]; i++) {
2100       ModifyMenu(hmenu, i, MF_STRING|MF_BYPOSITION|MF_POPUP, 
2101         (UINT)GetSubMenu(hmenu, i), menuBarText[tinyLayout][i]);
2102     }
2103     DrawMenuBar(hwndMain);
2104   }
2105
2106   boardWidth = BoardWidth(boardSize);
2107
2108   /* Get text area sizes */
2109   hdc = GetDC(hwndMain);
2110   if (appData.clockMode) {
2111     sprintf(buf, "White: %s", TimeString(23*60*60*1000L));
2112   } else {
2113     sprintf(buf, "White");
2114   }
2115   oldFont = SelectObject(hdc, font[boardSize][CLOCK_FONT]->hf);
2116   GetTextExtentPoint(hdc, buf, strlen(buf), &clockSize);
2117   SelectObject(hdc, font[boardSize][MESSAGE_FONT]->hf);
2118   str = "We only care about the height here";
2119   GetTextExtentPoint(hdc, str, strlen(str), &messageSize);
2120   SelectObject(hdc, oldFont);
2121   ReleaseDC(hwndMain, hdc);
2122
2123   /* Compute where everything goes */
2124   whiteRect.left = OUTER_MARGIN;
2125   whiteRect.right = whiteRect.left + boardWidth/2 - INNER_MARGIN/2;
2126   whiteRect.top = OUTER_MARGIN;
2127   whiteRect.bottom = whiteRect.top + clockSize.cy;
2128
2129   blackRect.left = whiteRect.right + INNER_MARGIN;
2130   blackRect.right = blackRect.left + boardWidth/2 - 1;
2131   blackRect.top = whiteRect.top;
2132   blackRect.bottom = whiteRect.bottom;
2133
2134   messageRect.left = whiteRect.left + MESSAGE_LINE_LEFTMARGIN;
2135   if (appData.showButtonBar) {
2136     messageRect.right = blackRect.right
2137       - N_BUTTONS*BUTTON_WIDTH - MESSAGE_LINE_LEFTMARGIN;
2138   } else {
2139     messageRect.right = blackRect.right;
2140   }
2141   messageRect.top = whiteRect.bottom + INNER_MARGIN;
2142   messageRect.bottom = messageRect.top + messageSize.cy;
2143
2144   boardRect.left = whiteRect.left;
2145   boardRect.right = boardRect.left + boardWidth;
2146   boardRect.top = messageRect.bottom + INNER_MARGIN;
2147   boardRect.bottom = boardRect.top + boardWidth;
2148
2149   sizeInfo[boardSize].cliWidth = boardRect.right + OUTER_MARGIN;
2150   sizeInfo[boardSize].cliHeight = boardRect.bottom + OUTER_MARGIN;
2151   winWidth = 2 * GetSystemMetrics(SM_CXFRAME) + boardRect.right + OUTER_MARGIN;
2152   winHeight = 2 * GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYMENU) +
2153     GetSystemMetrics(SM_CYCAPTION) + boardRect.bottom + OUTER_MARGIN;
2154   GetWindowRect(hwndMain, &wrect);
2155   SetWindowPos(hwndMain, NULL, 0, 0, winWidth, winHeight,
2156                SWP_NOCOPYBITS|SWP_NOZORDER|SWP_NOMOVE);
2157   /* compensate if menu bar wrapped */
2158   GetClientRect(hwndMain, &crect);
2159   offby = boardRect.bottom + OUTER_MARGIN - crect.bottom;
2160   winHeight += offby;
2161   switch (flags) {
2162   case WMSZ_TOPLEFT:
2163     SetWindowPos(hwndMain, NULL, 
2164                  wrect.right - winWidth, wrect.bottom - winHeight, 
2165                  winWidth, winHeight, SWP_NOCOPYBITS|SWP_NOZORDER);
2166     break;
2167
2168   case WMSZ_TOPRIGHT:
2169   case WMSZ_TOP:
2170     SetWindowPos(hwndMain, NULL, 
2171                  wrect.left, wrect.bottom - winHeight, 
2172                  winWidth, winHeight, SWP_NOCOPYBITS|SWP_NOZORDER);
2173     break;
2174
2175   case WMSZ_BOTTOMLEFT:
2176   case WMSZ_LEFT:
2177     SetWindowPos(hwndMain, NULL, 
2178                  wrect.right - winWidth, wrect.top, 
2179                  winWidth, winHeight, SWP_NOCOPYBITS|SWP_NOZORDER);
2180     break;
2181
2182   case WMSZ_BOTTOMRIGHT:
2183   case WMSZ_BOTTOM:
2184   case WMSZ_RIGHT:
2185   default:
2186     SetWindowPos(hwndMain, NULL, 0, 0, winWidth, winHeight,
2187                SWP_NOCOPYBITS|SWP_NOZORDER|SWP_NOMOVE);
2188     break;
2189   }
2190
2191   hwndPause = NULL;
2192   for (i = 0; i < N_BUTTONS; i++) {
2193     if (buttonDesc[i].hwnd != NULL) {
2194       DestroyWindow(buttonDesc[i].hwnd);
2195       buttonDesc[i].hwnd = NULL;
2196     }
2197     if (appData.showButtonBar) {
2198       buttonDesc[i].hwnd =
2199         CreateWindow("BUTTON", buttonDesc[i].label,
2200                      WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
2201                      boardRect.right - BUTTON_WIDTH*(N_BUTTONS-i),
2202                      messageRect.top, BUTTON_WIDTH, messageSize.cy, hwndMain,
2203                      (HMENU) buttonDesc[i].id,
2204                      (HINSTANCE) GetWindowLong(hwndMain, GWL_HINSTANCE), NULL);
2205       if (tinyLayout) {
2206         SendMessage(buttonDesc[i].hwnd, WM_SETFONT, 
2207                     (WPARAM)font[boardSize][MESSAGE_FONT]->hf,
2208                     MAKELPARAM(FALSE, 0));
2209       }
2210       if (buttonDesc[i].id == IDM_Pause)
2211         hwndPause = buttonDesc[i].hwnd;
2212       buttonDesc[i].wndproc = (WNDPROC)
2213         SetWindowLong(buttonDesc[i].hwnd, GWL_WNDPROC, (LONG) ButtonProc);
2214     }
2215   }
2216   if (gridPen != NULL) DeleteObject(gridPen);
2217   if (highlightPen != NULL) DeleteObject(highlightPen);
2218   if (premovePen != NULL) DeleteObject(premovePen);
2219   if (lineGap != 0) {
2220     logbrush.lbStyle = BS_SOLID;
2221     logbrush.lbColor = RGB(0, 0, 0); /* grid pen color = black */
2222     gridPen =
2223       ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
2224                    lineGap, &logbrush, 0, NULL);
2225     logbrush.lbColor = highlightSquareColor;
2226     highlightPen =
2227       ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
2228                    lineGap, &logbrush, 0, NULL);
2229
2230     logbrush.lbColor = premoveHighlightColor; 
2231     premovePen =
2232       ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
2233                    lineGap, &logbrush, 0, NULL);
2234
2235     for (i = 0; i < BOARD_SIZE + 1; i++) {
2236       gridEndpoints[i*2].x = boardRect.left + lineGap / 2;
2237       gridEndpoints[i*2 + BOARD_SIZE*2 + 2].y = boardRect.top + lineGap / 2;
2238       gridEndpoints[i*2].y = gridEndpoints[i*2 + 1].y =
2239         boardRect.top + lineGap / 2 + (i * (squareSize + lineGap));
2240       gridEndpoints[i*2 + 1].x = boardRect.left + lineGap / 2 +
2241         BOARD_SIZE * (squareSize + lineGap);
2242       gridEndpoints[i*2 + BOARD_SIZE*2 + 2].x =
2243         gridEndpoints[i*2 + 1 + BOARD_SIZE*2 + 2].x = boardRect.left +
2244         lineGap / 2 + (i * (squareSize + lineGap));
2245       gridEndpoints[i*2 + 1 + BOARD_SIZE*2 + 2].y =
2246         boardRect.top + BOARD_SIZE * (squareSize + lineGap);
2247       gridVertexCounts[i*2] = gridVertexCounts[i*2 + 1] = 2;
2248     }
2249   }
2250
2251   if (boardSize == oldBoardSize) return;
2252   oldBoardSize = boardSize;
2253   oldTinyLayout = tinyLayout;
2254
2255   /* Load piece bitmaps for this board size */
2256   for (i=0; i<=2; i++) {
2257     for (piece = WhitePawn;
2258          (int) piece <= (int) WhiteKing;
2259          piece = (ChessSquare) ((int) piece + 1)) {
2260       if (pieceBitmap[i][piece] != NULL)
2261         DeleteObject(pieceBitmap[i][piece]);
2262     }
2263   }
2264
2265   pieceBitmap[0][WhitePawn] = DoLoadBitmap(hInst, "p", squareSize, "s");
2266   pieceBitmap[0][WhiteKnight] = DoLoadBitmap(hInst, "n", squareSize, "s");
2267   pieceBitmap[0][WhiteBishop] = DoLoadBitmap(hInst, "b", squareSize, "s");
2268   pieceBitmap[0][WhiteRook] = DoLoadBitmap(hInst, "r", squareSize, "s");
2269   pieceBitmap[0][WhiteQueen] = DoLoadBitmap(hInst, "q", squareSize, "s");
2270   pieceBitmap[0][WhiteKing] = DoLoadBitmap(hInst, "k", squareSize, "s");
2271   pieceBitmap[1][WhitePawn] = DoLoadBitmap(hInst, "p", squareSize, "o");
2272   pieceBitmap[1][WhiteKnight] = DoLoadBitmap(hInst, "n", squareSize, "o");
2273   pieceBitmap[1][WhiteBishop] = DoLoadBitmap(hInst, "b", squareSize, "o");
2274   pieceBitmap[1][WhiteRook] = DoLoadBitmap(hInst, "r", squareSize, "o");
2275   pieceBitmap[1][WhiteQueen] = DoLoadBitmap(hInst, "q", squareSize, "o");
2276   pieceBitmap[1][WhiteKing] = DoLoadBitmap(hInst, "k", squareSize, "o");
2277   pieceBitmap[2][WhitePawn] = DoLoadBitmap(hInst, "p", squareSize, "w");
2278   pieceBitmap[2][WhiteKnight] = DoLoadBitmap(hInst, "n", squareSize, "w");
2279   pieceBitmap[2][WhiteBishop] = DoLoadBitmap(hInst, "b", squareSize, "w");
2280   pieceBitmap[2][WhiteRook] = DoLoadBitmap(hInst, "r", squareSize, "w");
2281   pieceBitmap[2][WhiteQueen] = DoLoadBitmap(hInst, "q", squareSize, "w");
2282   pieceBitmap[2][WhiteKing] = DoLoadBitmap(hInst, "k", squareSize, "w");
2283
2284 }
2285
2286 HBITMAP
2287 PieceBitmap(ChessSquare p, int kind)
2288 {
2289   if ((int) p >= (int) BlackPawn)
2290     p = (ChessSquare) ((int) p - (int) BlackPawn + (int) WhitePawn);
2291
2292   return pieceBitmap[kind][(int) p];
2293 }
2294
2295 /***************************************************************/
2296
2297 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
2298 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
2299 /*
2300 #define MIN3(a,b,c) (((a) < (b) && (a) < (c)) ? (a) : (((b) < (a) && (b) < (c)) ? (b) : (c)))
2301 #define MAX3(a,b,c) (((a) > (b) && (a) > (c)) ? (a) : (((b) > (a) && (b) > (c)) ? (b) : (c)))
2302 */
2303
2304 VOID
2305 SquareToPos(int row, int column, int * x, int * y)
2306 {
2307   if (flipView) {
2308     *x = boardRect.left + lineGap + ((BOARD_SIZE-1)-column) * (squareSize + lineGap);
2309     *y = boardRect.top + lineGap + row * (squareSize + lineGap);
2310   } else {
2311     *x = boardRect.left + lineGap + column * (squareSize + lineGap);
2312     *y = boardRect.top + lineGap + ((BOARD_SIZE-1)-row) * (squareSize + lineGap);
2313   }
2314 }
2315
2316 VOID
2317 DrawCoordsOnDC(HDC hdc)
2318 {
2319   static char files[16] = {'1','2','3','4','5','6','7','8','8','7','6','5','4','3','2','1'};
2320   static char ranks[16] = {'h','g','f','e','d','c','b','a','a','b','c','d','e','f','g','h'};
2321   char str[2] = { NULLCHAR, NULLCHAR };
2322   int oldMode, oldAlign, x, y, start, i;
2323   HFONT oldFont;
2324   HBRUSH oldBrush;
2325
2326   if (!appData.showCoords)
2327     return;
2328
2329   start = flipView ? 0 : 8;
2330
2331   oldBrush = SelectObject(hdc, GetStockObject(BLACK_BRUSH));
2332   oldMode = SetBkMode(hdc, (appData.monoMode ? OPAQUE : TRANSPARENT));
2333   oldAlign = GetTextAlign(hdc);
2334   oldFont = SelectObject(hdc, font[boardSize][COORD_FONT]->hf);
2335
2336   y = boardRect.top + lineGap;
2337   x = boardRect.left + lineGap;
2338
2339   SetTextAlign(hdc, TA_LEFT|TA_TOP);
2340   for (i = 0; i < 8; i++) {
2341     str[0] = files[start + i];
2342     ExtTextOut(hdc, x + 2, y + 1, 0, NULL, str, 1, NULL);
2343     y += squareSize + lineGap;
2344   }
2345
2346   SetTextAlign(hdc, TA_RIGHT|TA_BOTTOM);
2347   for (i = 0; i < 8; i++) {
2348     str[0] = ranks[start + i];
2349     ExtTextOut(hdc, x + squareSize - 2, y - 1, 0, NULL, str, 1, NULL);
2350     x += squareSize + lineGap;
2351   }    
2352
2353   SelectObject(hdc, oldBrush);
2354   SetBkMode(hdc, oldMode);
2355   SetTextAlign(hdc, oldAlign);
2356   SelectObject(hdc, oldFont);
2357 }
2358
2359 VOID
2360 DrawGridOnDC(HDC hdc)
2361 {
2362   HPEN oldPen;
2363  
2364   if (lineGap != 0) {
2365     oldPen = SelectObject(hdc, gridPen);
2366     PolyPolyline(hdc, gridEndpoints, gridVertexCounts, BOARD_SIZE*2 + 2);
2367     SelectObject(hdc, oldPen);
2368   }
2369 }
2370
2371 #define HIGHLIGHT_PEN 0
2372 #define PREMOVE_PEN   1
2373
2374 VOID
2375 DrawHighlightOnDC(HDC hdc, BOOLEAN on, int x, int y, int pen)
2376 {
2377   int x1, y1;
2378   HPEN oldPen, hPen;
2379   if (lineGap == 0) return;
2380   if (flipView) {
2381     x1 = boardRect.left +
2382       lineGap/2 + ((BOARD_SIZE-1)-x) * (squareSize + lineGap);
2383     y1 = boardRect.top +
2384       lineGap/2 + y * (squareSize + lineGap);
2385   } else {
2386     x1 = boardRect.left +
2387       lineGap/2 + x * (squareSize + lineGap);
2388     y1 = boardRect.top +
2389       lineGap/2 + ((BOARD_SIZE-1)-y) * (squareSize + lineGap);
2390   }
2391   hPen = pen ? premovePen : highlightPen;
2392   oldPen = SelectObject(hdc, on ? hPen : gridPen);
2393   MoveToEx(hdc, x1, y1, NULL);
2394   LineTo(hdc, x1 + squareSize + lineGap, y1);
2395   LineTo(hdc, x1 + squareSize + lineGap, y1 + squareSize + lineGap);
2396   LineTo(hdc, x1, y1 + squareSize + lineGap);
2397   LineTo(hdc, x1, y1);
2398   SelectObject(hdc, oldPen);
2399 }
2400
2401 VOID
2402 DrawHighlightsOnDC(HDC hdc)
2403 {
2404   int i;
2405   for (i=0; i<2; i++) {
2406     if (highlightInfo.sq[i].x >= 0 && highlightInfo.sq[i].y >= 0) 
2407       DrawHighlightOnDC(hdc, TRUE,
2408                         highlightInfo.sq[i].x, highlightInfo.sq[i].y,
2409                         HIGHLIGHT_PEN);
2410   }
2411   for (i=0; i<2; i++) {
2412     if (premoveHighlightInfo.sq[i].x >= 0 && 
2413         premoveHighlightInfo.sq[i].y >= 0) {
2414         DrawHighlightOnDC(hdc, TRUE,
2415                           premoveHighlightInfo.sq[i].x, 
2416                           premoveHighlightInfo.sq[i].y,
2417                           PREMOVE_PEN);
2418     }
2419   }
2420 }
2421
2422 /* Note: sqcolor is used only in monoMode */
2423 /* Note that this code is largely duplicated in woptions.c,
2424    function DrawSampleSquare, so that needs to be updated too */
2425 VOID
2426 DrawPieceOnDC(HDC hdc, ChessSquare piece, int color, int sqcolor, int x, int y, HDC tmphdc)
2427 {
2428   HBITMAP oldBitmap;
2429   HBRUSH oldBrush;
2430
2431   if (appData.blindfold) return;
2432
2433   if (appData.monoMode) {
2434     SelectObject(tmphdc, PieceBitmap(piece, 
2435       color == sqcolor ? OUTLINE_PIECE : SOLID_PIECE));
2436     BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0,
2437            sqcolor ? SRCCOPY : NOTSRCCOPY);
2438   } else {
2439     if (color) {
2440       oldBitmap = SelectObject(tmphdc, PieceBitmap(piece, WHITE_PIECE));
2441       oldBrush = SelectObject(hdc, whitePieceBrush);
2442       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, 0x00B8074A);
2443 #if 0
2444       /* Use black piece color for outline of white pieces */
2445       /* Not sure this looks really good (though xboard does it).
2446          Maybe better to have another selectable color, default black */
2447       SelectObject(hdc, blackPieceBrush); /* could have own brush */
2448       SelectObject(tmphdc, PieceBitmap(piece, OUTLINE_PIECE));
2449       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, 0x00B8074A);
2450 #else
2451       /* Use black for outline of white pieces */
2452       SelectObject(tmphdc, PieceBitmap(piece, OUTLINE_PIECE));
2453       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, SRCAND);
2454 #endif
2455     } else {
2456 #if 0
2457       /* Use white piece color for details of black pieces */
2458       /* Requires filled-in solid bitmaps (BLACK_PIECE class); the
2459          WHITE_PIECE ones aren't always the right shape. */
2460       /* Not sure this looks really good (though xboard does it).
2461          Maybe better to have another selectable color, default medium gray? */
2462       oldBitmap = SelectObject(tmphdc, PieceBitmap(piece, BLACK_PIECE));
2463       oldBrush = SelectObject(hdc, whitePieceBrush); /* could have own brush */
2464       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, 0x00B8074A);
2465       SelectObject(tmphdc, PieceBitmap(piece, SOLID_PIECE));
2466       SelectObject(hdc, blackPieceBrush);
2467       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, 0x00B8074A);
2468 #else
2469       /* Use square color for details of black pieces */
2470       oldBitmap = SelectObject(tmphdc, PieceBitmap(piece, SOLID_PIECE));
2471       oldBrush = SelectObject(hdc, blackPieceBrush);
2472       BitBlt(hdc, x, y, squareSize, squareSize, tmphdc, 0, 0, 0x00B8074A);
2473 #endif
2474     }
2475     SelectObject(hdc, oldBrush);
2476     SelectObject(tmphdc, oldBitmap);
2477   }
2478 }
2479
2480 VOID
2481 DrawBoardOnDC(HDC hdc, Board board, HDC tmphdc)
2482 {
2483   int row, column, x, y, square_color, piece_color;
2484   ChessSquare piece;
2485   HBRUSH oldBrush;
2486
2487   for (row = 0; row < BOARD_SIZE; row++) {
2488     for (column = 0; column < BOARD_SIZE; column++) {
2489   
2490       SquareToPos(row, column, &x, &y);
2491
2492       piece = board[row][column];
2493
2494       square_color = ((column + row) % 2) == 1;
2495       piece_color = (int) piece < (int) BlackPawn;
2496
2497       if (appData.monoMode) {
2498         if (piece == EmptySquare) {
2499           BitBlt(hdc, x, y, squareSize, squareSize, 0, 0, 0,
2500                  square_color ? WHITENESS : BLACKNESS);
2501         } else {
2502           DrawPieceOnDC(hdc, piece, piece_color, square_color, x, y, tmphdc);
2503         }
2504       } else {
2505         oldBrush = SelectObject(hdc, square_color ?
2506                                 lightSquareBrush : darkSquareBrush);
2507         BitBlt(hdc, x, y, squareSize, squareSize, 0, 0, 0, PATCOPY);
2508         SelectObject(hdc, oldBrush);
2509         if (piece != EmptySquare)
2510           DrawPieceOnDC(hdc, piece, piece_color, -1, x, y, tmphdc);
2511       }
2512     }
2513   }
2514 }
2515
2516 #define MAX_CLIPS 200   /* more than enough */
2517
2518 VOID
2519 HDCDrawPosition(HDC hdc, BOOLEAN repaint, Board board)
2520 {
2521   static Board lastReq, lastDrawn;
2522   static HighlightInfo lastDrawnHighlight, lastDrawnPremove;
2523   static int lastDrawnFlipView = 0;
2524   static int lastReqValid = 0, lastDrawnValid = 0;
2525   int releaseDC, x, y, x2, y2, row, column, num_clips = 0, i;
2526   HDC tmphdc;
2527   HDC hdcmem;
2528   HBITMAP bufferBitmap;
2529   HBITMAP oldBitmap;
2530   RECT Rect;
2531   HRGN clips[MAX_CLIPS];
2532   ChessSquare dragged_piece = EmptySquare;
2533
2534   /* I'm undecided on this - this function figures out whether a full
2535    * repaint is necessary on its own, so there's no real reason to have the
2536    * caller tell it that.  I think this can safely be set to FALSE - but
2537    * if we trust the callers not to request full repaints unnessesarily, then
2538    * we could skip some clipping work.  In other words, only request a full
2539    * redraw when the majority of pieces have changed positions (ie. flip, 
2540    * gamestart and similar)  --Hawk
2541    */
2542   Boolean fullrepaint = repaint;
2543
2544   if (board == NULL) {
2545     if (!lastReqValid) {
2546       return;
2547     }
2548     board = lastReq;
2549   } else {
2550     CopyBoard(lastReq, board);
2551     lastReqValid = 1;
2552   }
2553
2554   if (doingSizing) {
2555     return;
2556   }
2557
2558   if (IsIconic(hwndMain)) {
2559     return;
2560   }
2561
2562   if (hdc == NULL) {
2563     hdc = GetDC(hwndMain);
2564     if (!appData.monoMode) {
2565       SelectPalette(hdc, hPal, FALSE);
2566       RealizePalette(hdc);
2567     }
2568     releaseDC = TRUE;
2569   } else {
2570     releaseDC = FALSE;
2571   }
2572
2573 #if 0
2574   fprintf(debugFP, "*******************************\n"
2575                    "repaint = %s\n"
2576                    "dragInfo.from (%d,%d)\n"
2577                    "dragInfo.start (%d,%d)\n"
2578                    "dragInfo.pos (%d,%d)\n"
2579                    "dragInfo.lastpos (%d,%d)\n", 
2580                     repaint ? "TRUE" : "FALSE",
2581                     dragInfo.from.x, dragInfo.from.y, 
2582                     dragInfo.start.x, dragInfo.start.y,
2583                     dragInfo.pos.x, dragInfo.pos.y,
2584                     dragInfo.lastpos.x, dragInfo.lastpos.y);
2585   fprintf(debugFP, "prev:  ");
2586   for (row = 0; row < 8; row++) {
2587     for (column = 0; column < 8; column++) {
2588       fprintf(debugFP, "%d ", lastDrawn[row][column]);
2589     }
2590   }
2591   fprintf(debugFP, "\n");
2592   fprintf(debugFP, "board: ");
2593   for (row = 0; row < 8; row++) {
2594     for (column = 0; column < 8; column++) {
2595       fprintf(debugFP, "%d ", board[row][column]);
2596     }
2597   }
2598   fprintf(debugFP, "\n");
2599   fflush(debugFP);
2600 #endif
2601
2602   /* Create some work-DCs */
2603   hdcmem = CreateCompatibleDC(hdc);
2604   tmphdc = CreateCompatibleDC(hdc);
2605
2606   /* Figure out which squares need updating by comparing the 
2607    * newest board with the last drawn board and checking if
2608    * flipping has changed.
2609    */
2610   if (!fullrepaint && lastDrawnValid && lastDrawnFlipView == flipView) {
2611     for (row = 0; row < 8; row++) {
2612       for (column = 0; column < 8; column++) {
2613         if (lastDrawn[row][column] != board[row][column]) {
2614           SquareToPos(row, column, &x, &y);
2615           clips[num_clips++] =
2616             CreateRectRgn(x, y, x + squareSize, y + squareSize);
2617         }
2618       }
2619     }
2620     for (i=0; i<2; i++) {
2621       if (lastDrawnHighlight.sq[i].x != highlightInfo.sq[i].x ||
2622           lastDrawnHighlight.sq[i].y != highlightInfo.sq[i].y) {
2623         if (lastDrawnHighlight.sq[i].x >= 0 &&
2624             lastDrawnHighlight.sq[i].y >= 0) {
2625           SquareToPos(lastDrawnHighlight.sq[i].y,
2626                       lastDrawnHighlight.sq[i].x, &x, &y);
2627           clips[num_clips++] =
2628             CreateRectRgn(x - lineGap, y - lineGap, 
2629                           x + squareSize + lineGap, y + squareSize + lineGap);
2630         }
2631         if (highlightInfo.sq[i].x >= 0 && highlightInfo.sq[i].y >= 0) {
2632           SquareToPos(highlightInfo.sq[i].y, highlightInfo.sq[i].x, &x, &y);
2633           clips[num_clips++] =
2634             CreateRectRgn(x - lineGap, y - lineGap, 
2635                           x + squareSize + lineGap, y + squareSize + lineGap);
2636         }
2637       }
2638     }
2639     for (i=0; i<2; i++) {
2640       if (lastDrawnPremove.sq[i].x != premoveHighlightInfo.sq[i].x ||
2641           lastDrawnPremove.sq[i].y != premoveHighlightInfo.sq[i].y) {
2642         if (lastDrawnPremove.sq[i].x >= 0 &&
2643             lastDrawnPremove.sq[i].y >= 0) {
2644           SquareToPos(lastDrawnPremove.sq[i].y,
2645                       lastDrawnPremove.sq[i].x, &x, &y);
2646           clips[num_clips++] =
2647             CreateRectRgn(x - lineGap, y - lineGap, 
2648                           x + squareSize + lineGap, y + squareSize + lineGap);
2649         }
2650         if (premoveHighlightInfo.sq[i].x >= 0 && 
2651             premoveHighlightInfo.sq[i].y >= 0) {
2652           SquareToPos(premoveHighlightInfo.sq[i].y, 
2653                       premoveHighlightInfo.sq[i].x, &x, &y);
2654           clips[num_clips++] =
2655             CreateRectRgn(x - lineGap, y - lineGap, 
2656                           x + squareSize + lineGap, y + squareSize + lineGap);
2657         }
2658       }
2659     }
2660   } else {
2661     fullrepaint = TRUE;
2662   }
2663
2664   /* Create a buffer bitmap - this is the actual bitmap
2665    * being written to.  When all the work is done, we can
2666    * copy it to the real DC (the screen).  This avoids
2667    * the problems with flickering.
2668    */
2669   GetClientRect(hwndMain, &Rect);
2670   bufferBitmap = CreateCompatibleBitmap(hdc, Rect.right-Rect.left+1,
2671                                         Rect.bottom-Rect.top+1);
2672   oldBitmap = SelectObject(hdcmem, bufferBitmap);
2673   if (!appData.monoMode) {
2674     SelectPalette(hdcmem, hPal, FALSE);
2675   }
2676
2677   /* Create clips for dragging */
2678   if (!fullrepaint) {
2679     if (dragInfo.from.x >= 0) {
2680       SquareToPos(dragInfo.from.y, dragInfo.from.x, &x, &y);
2681       clips[num_clips++] = CreateRectRgn(x, y, x+squareSize, y+squareSize);
2682     }
2683     if (dragInfo.start.x >= 0) {
2684       SquareToPos(dragInfo.start.y, dragInfo.start.x, &x, &y);
2685       clips[num_clips++] = CreateRectRgn(x, y, x+squareSize, y+squareSize);
2686     }
2687     if (dragInfo.pos.x >= 0) {
2688       x = dragInfo.pos.x - squareSize / 2;
2689       y = dragInfo.pos.y - squareSize / 2;
2690       clips[num_clips++] = CreateRectRgn(x, y, x+squareSize, y+squareSize);
2691     }
2692     if (dragInfo.lastpos.x >= 0) {
2693       x = dragInfo.lastpos.x - squareSize / 2;
2694       y = dragInfo.lastpos.y - squareSize / 2;
2695       clips[num_clips++] = CreateRectRgn(x, y, x+squareSize, y+squareSize);
2696     }
2697   }
2698
2699   /* If dragging is in progress, we temporarely remove the piece */
2700   if (dragInfo.from.x >= 0 && dragInfo.pos.x >= 0) {
2701     dragged_piece = board[dragInfo.from.y][dragInfo.from.x];
2702     board[dragInfo.from.y][dragInfo.from.x] = EmptySquare;
2703   }
2704
2705   /* Are we animating a move?  
2706    * If so, 
2707    *   - remove the piece from the board (temporarely)
2708    *   - calculate the clipping region
2709    */
2710   if (!fullrepaint) {
2711     if (animInfo.piece != EmptySquare) {
2712       board[animInfo.from.y][animInfo.from.x] = EmptySquare;
2713       x = boardRect.left + animInfo.lastpos.x;
2714       y = boardRect.top + animInfo.lastpos.y;
2715       x2 = boardRect.left + animInfo.pos.x;
2716       y2 = boardRect.top + animInfo.pos.y;
2717       clips[num_clips++] = CreateRectRgn(MIN(x,x2), MIN(y,y2), MAX(x,x2)+squareSize, MAX(y,y2)+squareSize);
2718       /* Slight kludge.  The real problem is that after AnimateMove is
2719          done, the position on the screen does not match lastDrawn.
2720          This currently causes trouble only on e.p. captures in
2721          atomic, where the piece moves to an empty square and then
2722          explodes.  The old and new positions both had an empty square
2723          at the destination, but animation has drawn a piece there and
2724          we have to remember to erase it. */
2725       lastDrawn[animInfo.to.y][animInfo.to.x] = animInfo.piece;
2726     }
2727   }
2728
2729   /* No clips?  Make sure we have fullrepaint set to TRUE */
2730   if (num_clips == 0)
2731     fullrepaint = TRUE;
2732
2733   /* Set clipping on the memory DC */
2734   if (!fullrepaint) {
2735     SelectClipRgn(hdcmem, clips[0]);
2736     for (x = 1; x < num_clips; x++) {
2737       if (ExtSelectClipRgn(hdcmem, clips[x], RGN_OR) == ERROR)
2738         abort();  // this should never ever happen!
2739     }
2740   }
2741
2742   /* Do all the drawing to the memory DC */
2743   DrawGridOnDC(hdcmem);
2744   DrawHighlightsOnDC(hdcmem);
2745   DrawBoardOnDC(hdcmem, board, tmphdc);
2746   DrawCoordsOnDC(hdcmem);
2747
2748   /* Put the dragged piece back into place and draw it */
2749   if (dragged_piece != EmptySquare) {
2750     board[dragInfo.from.y][dragInfo.from.x] = dragged_piece;
2751     x = dragInfo.pos.x - squareSize / 2;
2752     y = dragInfo.pos.y - squareSize / 2;
2753     DrawPieceOnDC(hdcmem, dragged_piece,
2754                   ((int) dragged_piece < (int) BlackPawn), 
2755                   (dragInfo.from.y + dragInfo.from.x) % 2, x, y, tmphdc);
2756   }   
2757   
2758   /* Put the animated piece back into place and draw it */
2759   if (animInfo.piece != EmptySquare) {
2760     board[animInfo.from.y][animInfo.from.x]  = animInfo.piece;
2761     x = boardRect.left + animInfo.pos.x;
2762     y = boardRect.top + animInfo.pos.y;
2763     DrawPieceOnDC(hdcmem, animInfo.piece,
2764                   ((int) animInfo.piece < (int) BlackPawn),
2765                   (animInfo.from.y + animInfo.from.x) % 2, x, y, tmphdc);
2766   }
2767
2768   /* Release the bufferBitmap by selecting in the old bitmap 
2769    * and delete the memory DC
2770    */
2771   SelectObject(hdcmem, oldBitmap);
2772   DeleteDC(hdcmem);
2773
2774   /* Set clipping on the target DC */
2775   if (!fullrepaint) {
2776     SelectClipRgn(hdc, clips[0]);
2777     for (x = 1; x < num_clips; x++) {
2778       if (ExtSelectClipRgn(hdc, clips[x], RGN_OR) == ERROR)
2779         abort();   // this should never ever happen!
2780     } 
2781   }
2782
2783   /* Copy the new bitmap onto the screen in one go.
2784    * This way we avoid any flickering
2785    */
2786   oldBitmap = SelectObject(tmphdc, bufferBitmap);
2787   BitBlt(hdc, boardRect.left, boardRect.top,
2788          boardRect.right - boardRect.left,
2789          boardRect.bottom - boardRect.top,
2790          tmphdc, boardRect.left, boardRect.top, SRCCOPY);
2791   SelectObject(tmphdc, oldBitmap);
2792
2793   /* Massive cleanup */
2794   for (x = 0; x < num_clips; x++)
2795     DeleteObject(clips[x]);
2796
2797   DeleteDC(tmphdc);
2798   DeleteObject(bufferBitmap);
2799
2800   if (releaseDC) 
2801     ReleaseDC(hwndMain, hdc);
2802   
2803   if (lastDrawnFlipView != flipView) {
2804     if (flipView)
2805       CheckMenuItem(GetMenu(hwndMain),IDM_FlipView, MF_BYCOMMAND|MF_CHECKED);
2806     else
2807       CheckMenuItem(GetMenu(hwndMain),IDM_FlipView, MF_BYCOMMAND|MF_UNCHECKED);
2808   }
2809
2810   CopyBoard(lastDrawn, board);
2811   lastDrawnHighlight = highlightInfo;
2812   lastDrawnPremove   = premoveHighlightInfo;
2813   lastDrawnFlipView = flipView;
2814   lastDrawnValid = 1;
2815 }
2816
2817
2818 /*---------------------------------------------------------------------------*\
2819 | CLIENT PAINT PROCEDURE
2820 |   This is the main event-handler for the WM_PAINT message.
2821 |
2822 \*---------------------------------------------------------------------------*/
2823 VOID
2824 PaintProc(HWND hwnd)
2825 {
2826   HDC         hdc;
2827   PAINTSTRUCT ps;
2828   HFONT       oldFont;
2829
2830   if(hdc = BeginPaint(hwnd, &ps)) {
2831     if (IsIconic(hwnd)) {
2832       DrawIcon(hdc, 2, 2, iconCurrent);
2833     } else {
2834       if (!appData.monoMode) {
2835         SelectPalette(hdc, hPal, FALSE);
2836         RealizePalette(hdc);
2837       }
2838       HDCDrawPosition(hdc, 1, NULL);
2839       oldFont =
2840         SelectObject(hdc, font[boardSize][MESSAGE_FONT]->hf);
2841       ExtTextOut(hdc, messageRect.left, messageRect.top,
2842                  ETO_CLIPPED|ETO_OPAQUE,
2843                  &messageRect, messageText, strlen(messageText), NULL);
2844       SelectObject(hdc, oldFont);
2845       DisplayBothClocks();
2846     }
2847     EndPaint(hwnd,&ps);
2848   }
2849
2850   return;
2851 }
2852
2853
2854 /*
2855  * If the user selects on a border boundary, return -1; if off the board,
2856  *   return -2.  Otherwise map the event coordinate to the square.
2857  * The offset boardRect.left or boardRect.top must already have been
2858  *   subtracted from x.
2859  */
2860 int
2861 EventToSquare(int x)
2862 {
2863   if (x <= 0)
2864     return -2;
2865   if (x < lineGap)
2866     return -1;
2867   x -= lineGap;
2868   if ((x % (squareSize + lineGap)) >= squareSize)
2869     return -1;
2870   x /= (squareSize + lineGap);
2871   if (x >= BOARD_SIZE)
2872     return -2;
2873   return x;
2874 }
2875
2876 typedef struct {
2877   char piece;
2878   int command;
2879   char* name;
2880 } DropEnable;
2881
2882 DropEnable dropEnables[] = {
2883   { 'P', DP_Pawn, "Pawn" },
2884   { 'N', DP_Knight, "Knight" },
2885   { 'B', DP_Bishop, "Bishop" },
2886   { 'R', DP_Rook, "Rook" },
2887   { 'Q', DP_Queen, "Queen" },
2888 };
2889
2890 VOID
2891 SetupDropMenu(HMENU hmenu)
2892 {
2893   int i, count, enable;
2894   char *p;
2895   extern char white_holding[], black_holding[];
2896   char item[MSG_SIZ];
2897
2898   for (i=0; i<sizeof(dropEnables)/sizeof(DropEnable); i++) {
2899     p = strchr(gameMode == IcsPlayingWhite ? white_holding : black_holding,
2900                dropEnables[i].piece);
2901     count = 0;
2902     while (p && *p++ == dropEnables[i].piece) count++;
2903     sprintf(item, "%s  %d", dropEnables[i].name, count);
2904     enable = count > 0 || !appData.testLegality
2905       /*!!temp:*/ || (gameInfo.variant == VariantCrazyhouse
2906                       && !appData.icsActive);
2907     ModifyMenu(hmenu, dropEnables[i].command,
2908                MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED) | MF_STRING,
2909                dropEnables[i].command, item);
2910   }
2911 }
2912
2913 static int fromX = -1, fromY = -1, toX, toY;
2914
2915 /* Event handler for mouse messages */
2916 VOID
2917 MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
2918 {
2919   int x, y;
2920   POINT pt;
2921   static int recursive = 0;
2922   HMENU hmenu;
2923   BOOLEAN saveAnimate;
2924   static BOOLEAN sameAgain = FALSE;
2925
2926   if (recursive) {
2927     if (message == WM_MBUTTONUP) {
2928       /* Hideous kludge to fool TrackPopupMenu into paying attention
2929          to the middle button: we simulate pressing the left button too!
2930          */
2931       PostMessage(hwnd, WM_LBUTTONDOWN, wParam, lParam);
2932       PostMessage(hwnd, WM_LBUTTONUP, wParam, lParam);
2933     }
2934     return;
2935   }
2936   recursive++;
2937   
2938   pt.x = LOWORD(lParam);
2939   pt.y = HIWORD(lParam);
2940   x = EventToSquare(pt.x - boardRect.left);
2941   y = EventToSquare(pt.y - boardRect.top);
2942   if (!flipView && y >= 0) {
2943     y = BOARD_SIZE - 1 - y;
2944   }
2945   if (flipView && x >= 0) {
2946     x = BOARD_SIZE - 1 - x;
2947   }
2948
2949   switch (message) {
2950   case WM_LBUTTONDOWN:
2951     ErrorPopDown();
2952     sameAgain = FALSE;
2953     if (y == -2) {
2954       /* Downclick vertically off board; check if on clock */
2955       if (PtInRect((LPRECT) &whiteRect, pt)) {
2956         if (gameMode == EditPosition) {
2957           SetWhiteToPlayEvent();
2958         } else if (gameMode == IcsPlayingBlack ||
2959                    gameMode == MachinePlaysWhite) {
2960           CallFlagEvent();
2961         }
2962       } else if (PtInRect((LPRECT) &blackRect, pt)) {
2963         if (gameMode == EditPosition) {
2964           SetBlackToPlayEvent();
2965         } else if (gameMode == IcsPlayingWhite ||
2966                    gameMode == MachinePlaysBlack) {
2967           CallFlagEvent();
2968         }
2969       }
2970       if (!appData.highlightLastMove) {
2971         ClearHighlights();
2972         DrawPosition(FALSE, NULL);
2973       }
2974       fromX = fromY = -1;
2975       dragInfo.start.x = dragInfo.start.y = -1;
2976       dragInfo.from = dragInfo.start;
2977       break;
2978     } else if (x < 0 || y < 0) {
2979       break;
2980     } else if (fromX == x && fromY == y) {
2981       /* Downclick on same square again */
2982       ClearHighlights();
2983       DrawPosition(FALSE, NULL);
2984       sameAgain = TRUE;  
2985     } else if (fromX != -1) {
2986       /* Downclick on different square */
2987       ChessSquare pdown, pup;
2988       pdown = boards[currentMove][fromY][fromX];
2989       pup = boards[currentMove][y][x];
2990       if (gameMode == EditPosition ||
2991           !((WhitePawn <= pdown && pdown <= WhiteKing &&
2992              WhitePawn <= pup && pup <= WhiteKing) ||
2993             (BlackPawn <= pdown && pdown <= BlackKing &&
2994              BlackPawn <= pup && pup <= BlackKing))) {
2995         /* EditPosition, empty square, or different color piece;
2996            click-click move is possible */
2997         toX = x;
2998         toY = y;
2999         if (IsPromotion(fromX, fromY, toX, toY)) {
3000           if (appData.alwaysPromoteToQueen) {
3001             UserMoveEvent(fromX, fromY, toX, toY, 'q');
3002             if (!appData.highlightLastMove) {
3003               ClearHighlights();
3004               DrawPosition(FALSE, NULL);
3005             }
3006           } else {
3007             SetHighlights(fromX, fromY, toX, toY);
3008             DrawPosition(FALSE, NULL);
3009             PromotionPopup(hwnd);
3010           }
3011         } else {        /* not a promotion */
3012           if (appData.animate || appData.highlightLastMove) {
3013             SetHighlights(fromX, fromY, toX, toY);
3014           } else {
3015             ClearHighlights();
3016           }
3017           UserMoveEvent(fromX, fromY, toX, toY, NULLCHAR);
3018           if (appData.animate && !appData.highlightLastMove) {
3019             ClearHighlights();
3020             DrawPosition(FALSE, NULL);
3021           }
3022         }
3023         if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
3024         fromX = fromY = -1;
3025         break;
3026       }
3027       ClearHighlights();
3028       DrawPosition(FALSE, NULL);
3029     }
3030     /* First downclick, or restart on a square with same color piece */
3031     if (!frozen && OKToStartUserMove(x, y)) {
3032       fromX = x;
3033       fromY = y;
3034       dragInfo.lastpos = pt;
3035       dragInfo.from.x = fromX;
3036       dragInfo.from.y = fromY;
3037       dragInfo.start = dragInfo.from;
3038       SetCapture(hwndMain);
3039     } else {
3040       fromX = fromY = -1;
3041       dragInfo.start.x = dragInfo.start.y = -1;
3042       dragInfo.from = dragInfo.start;
3043     }
3044     break;
3045
3046   case WM_LBUTTONUP:
3047     ReleaseCapture();
3048     if (fromX == -1) break;
3049     if (x == fromX && y == fromY) {
3050       dragInfo.from.x = dragInfo.from.y = -1;
3051       /* Upclick on same square */
3052       if (sameAgain) {
3053         /* Clicked same square twice: abort click-click move */
3054         fromX = fromY = -1;
3055         gotPremove = 0;
3056         ClearPremoveHighlights();
3057       } else {
3058         /* First square clicked: start click-click move */
3059         SetHighlights(fromX, fromY, -1, -1);
3060       }
3061       DrawPosition(FALSE, NULL);
3062     } else if (dragInfo.from.x < 0 || dragInfo.from.y < 0) {
3063       /* Errant click; ignore */
3064       break;
3065     } else {
3066       /* Finish drag move */
3067       dragInfo.from.x = dragInfo.from.y = -1;
3068       toX = x;
3069       toY = y;
3070       saveAnimate = appData.animate; /* sorry, Hawk :) */
3071       appData.animate = appData.animate && !appData.animateDragging;
3072       if (IsPromotion(fromX, fromY, toX, toY)) {
3073         if (appData.alwaysPromoteToQueen) {
3074           UserMoveEvent(fromX, fromY, toX, toY, 'q');
3075         } else {
3076           DrawPosition(FALSE, NULL);
3077           PromotionPopup(hwnd);
3078         }
3079       } else {
3080         UserMoveEvent(fromX, fromY, toX, toY, NULLCHAR);
3081       }
3082       if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
3083       appData.animate = saveAnimate;
3084       fromX = fromY = -1;
3085       if (appData.highlightDragging && !appData.highlightLastMove) {
3086         ClearHighlights();
3087       }
3088       if (appData.animate || appData.animateDragging ||
3089           appData.highlightDragging || gotPremove) {
3090         DrawPosition(FALSE, NULL);
3091       }
3092     }
3093     dragInfo.start.x = dragInfo.start.y = -1; 
3094     dragInfo.pos = dragInfo.lastpos = dragInfo.start;
3095     break;
3096
3097   case WM_MOUSEMOVE:
3098     if ((appData.animateDragging || appData.highlightDragging)
3099         && (wParam & MK_LBUTTON)
3100         && dragInfo.from.x >= 0) {
3101       if (appData.animateDragging) {
3102         dragInfo.pos = pt;
3103       }
3104       if (appData.highlightDragging) {
3105         SetHighlights(fromX, fromY, x, y);
3106       }
3107       DrawPosition(FALSE, NULL);
3108       dragInfo.lastpos = dragInfo.pos;
3109     }
3110     break;
3111   case WM_MOUSEWHEEL:
3112         /* Mouse Wheel is being rolled forward 
3113          * Play moves forward
3114          */
3115         if ((short)HIWORD(wParam) > 0) 
3116            if (forwardMostMove > 0 && currentMove != forwardMostMove)
3117                    ForwardEvent();
3118            /* Mouse Wheel is being rolled backward 
3119             * Play moves backward
3120             */
3121         if ((short)HIWORD(wParam) < 0) 
3122            if (currentMove > 0) BackwardEvent();
3123         break;
3124   case WM_MBUTTONDOWN:
3125   case WM_RBUTTONDOWN:
3126     ErrorPopDown();
3127     ReleaseCapture();
3128     fromX = fromY = -1;
3129     dragInfo.pos.x = dragInfo.pos.y = -1;
3130     dragInfo.start.x = dragInfo.start.y = -1;
3131     dragInfo.from = dragInfo.start;
3132     dragInfo.lastpos = dragInfo.pos;
3133     if (appData.highlightDragging) {
3134       ClearHighlights();
3135     }
3136     DrawPosition(TRUE, NULL);
3137
3138     switch (gameMode) {
3139     case EditPosition:
3140     case IcsExamining:
3141       if (x < 0 || y < 0) break;
3142       fromX = x;
3143       fromY = y;
3144       if (message == WM_MBUTTONDOWN) {
3145         buttonCount = 3;  /* even if system didn't think so */
3146         if (wParam & MK_SHIFT) 
3147           MenuPopup(hwnd, pt, LoadMenu(hInst, "BlackPieceMenu"), -1);
3148         else
3149           MenuPopup(hwnd, pt, LoadMenu(hInst, "WhitePieceMenu"), -1);
3150       } else { /* message == WM_RBUTTONDOWN */
3151 #if 0
3152         if (buttonCount == 3) {
3153           if (wParam & MK_SHIFT) 
3154             MenuPopup(hwnd, pt, LoadMenu(hInst, "WhitePieceMenu"), -1);
3155           else
3156             MenuPopup(hwnd, pt, LoadMenu(hInst, "BlackPieceMenu"), -1);
3157         } else {
3158           MenuPopup(hwnd, pt, LoadMenu(hInst, "PieceMenu"), -1);
3159         }
3160 #else
3161         /* Just have one menu, on the right button.  Windows users don't
3162            think to try the middle one, and sometimes other software steals
3163            it, or it doesn't really exist. */
3164         MenuPopup(hwnd, pt, LoadMenu(hInst, "PieceMenu"), -1);
3165 #endif
3166       }
3167       break;
3168     case IcsPlayingWhite:
3169     case IcsPlayingBlack:
3170     case EditGame:
3171     case MachinePlaysWhite:
3172     case MachinePlaysBlack:
3173       if (appData.testLegality &&
3174           gameInfo.variant != VariantBughouse &&
3175           gameInfo.variant != VariantCrazyhouse) break;
3176       if (x < 0 || y < 0) break;
3177       fromX = x;
3178       fromY = y;
3179       hmenu = LoadMenu(hInst, "DropPieceMenu");
3180       SetupDropMenu(hmenu);
3181       MenuPopup(hwnd, pt, hmenu, -1);
3182       break;
3183     default:
3184       break;
3185     }
3186     break;
3187   }
3188
3189   recursive--;
3190 }
3191
3192 /* Preprocess messages for buttons in main window */
3193 LRESULT CALLBACK
3194 ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3195 {
3196   int id = GetWindowLong(hwnd, GWL_ID);
3197   int i, dir;
3198
3199   for (i=0; i<N_BUTTONS; i++) {
3200     if (buttonDesc[i].id == id) break;
3201   }
3202   if (i == N_BUTTONS) return 0;
3203   switch (message) {
3204   case WM_KEYDOWN:
3205     switch (wParam) {
3206     case VK_LEFT:
3207     case VK_RIGHT:
3208       dir = (wParam == VK_LEFT) ? -1 : 1;
3209       SetFocus(buttonDesc[(i + dir + N_BUTTONS) % N_BUTTONS].hwnd);
3210       return TRUE;
3211     }
3212     break;
3213   case WM_CHAR:
3214     switch (wParam) {
3215     case '\r':
3216       SendMessage(hwndMain, WM_COMMAND, MAKEWPARAM(buttonDesc[i].id, 0), 0);
3217       return TRUE;
3218     case '\t':
3219       if (appData.icsActive) {
3220         if (GetKeyState(VK_SHIFT) < 0) {
3221           /* shifted */
3222           HWND h = GetDlgItem(hwndConsole, OPT_ConsoleInput);
3223           if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3224           SetFocus(h);
3225         } else {
3226           /* unshifted */
3227           HWND h = GetDlgItem(hwndConsole, OPT_ConsoleText);
3228           if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3229           SetFocus(h);
3230         }
3231         return TRUE;
3232       }
3233       break;
3234     default:
3235       if (appData.icsActive) {
3236         HWND h = GetDlgItem(hwndConsole, OPT_ConsoleInput);
3237         if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3238         SetFocus(h);
3239         SendMessage(h, WM_CHAR, wParam, lParam);
3240         return TRUE;
3241       } else if (isalpha((char)wParam) || isdigit((char)wParam)){
3242         PopUpMoveDialog((char)wParam);
3243       }
3244       break;
3245     }
3246     break;
3247   }
3248   return CallWindowProc(buttonDesc[i].wndproc, hwnd, message, wParam, lParam);
3249 }
3250
3251 /* Process messages for Promotion dialog box */
3252 LRESULT CALLBACK
3253 Promotion(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
3254 {
3255   char promoChar;
3256
3257   switch (message) {
3258   case WM_INITDIALOG: /* message: initialize dialog box */
3259     /* Center the dialog over the application window */
3260     CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
3261     ShowWindow(GetDlgItem(hDlg, PB_King), 
3262       (!appData.testLegality || gameInfo.variant == VariantSuicide ||
3263        gameInfo.variant == VariantGiveaway) ?
3264                SW_SHOW : SW_HIDE);
3265     return TRUE;
3266
3267   case WM_COMMAND: /* message: received a command */
3268     switch (LOWORD(wParam)) {
3269     case IDCANCEL:
3270       EndDialog(hDlg, TRUE); /* Exit the dialog */
3271       ClearHighlights();
3272       DrawPosition(FALSE, NULL);
3273       return TRUE;
3274     case PB_King:
3275       promoChar = 'k';
3276       break;
3277     case PB_Queen:
3278       promoChar = 'q';
3279       break;
3280     case PB_Rook:
3281       promoChar = 'r';
3282       break;
3283     case PB_Bishop:
3284       promoChar = 'b';
3285       break;
3286     case PB_Knight:
3287       promoChar = 'n';
3288       break;
3289     default:
3290       return FALSE;
3291     }
3292     EndDialog(hDlg, TRUE); /* Exit the dialog */
3293     UserMoveEvent(fromX, fromY, toX, toY, promoChar);
3294     if (!appData.highlightLastMove) {
3295       ClearHighlights();
3296       DrawPosition(FALSE, NULL);
3297     }
3298     return TRUE;
3299   }
3300   return FALSE;
3301 }
3302
3303 /* Pop up promotion dialog */
3304 VOID
3305 PromotionPopup(HWND hwnd)
3306 {
3307   FARPROC lpProc;
3308
3309   lpProc = MakeProcInstance((FARPROC)Promotion, hInst);
3310   DialogBox(hInst, MAKEINTRESOURCE(DLG_PromotionKing),
3311     hwnd, (DLGPROC)lpProc);
3312   FreeProcInstance(lpProc);
3313 }
3314
3315 /* Toggle ShowThinking */
3316 VOID
3317 ToggleShowThinking()
3318 {
3319   ShowThinkingEvent(!appData.showThinking);
3320 }
3321
3322 VOID
3323 LoadGameDialog(HWND hwnd, char* title)
3324 {
3325   UINT number = 0;
3326   FILE *f;
3327   char fileTitle[MSG_SIZ];
3328   f = OpenFileDialog(hwnd, FALSE, "",
3329                      appData.oldSaveStyle ? "gam" : "pgn",
3330                      GAME_FILT,
3331                      title, &number, fileTitle, NULL);
3332   if (f != NULL) {
3333     cmailMsgLoaded = FALSE;
3334     if (number == 0) {
3335       int error = GameListBuild(f);
3336       if (error) {
3337         DisplayError("Cannot build game list", error);
3338       } else if (!ListEmpty(&gameList) &&
3339                  ((ListGame *) gameList.tailPred)->number > 1) {
3340         GameListPopUp(f, fileTitle);
3341         return;
3342       }
3343       GameListDestroy();
3344       number = 1;
3345     }
3346     LoadGame(f, number, fileTitle, FALSE);
3347   }
3348 }
3349
3350 VOID
3351 ChangedConsoleFont()
3352 {
3353   CHARFORMAT cfmt;
3354   CHARRANGE tmpsel, sel;
3355   MyFont *f = font[boardSize][CONSOLE_FONT];
3356   HWND hText = GetDlgItem(hwndConsole, OPT_ConsoleText);
3357   HWND hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
3358   PARAFORMAT paraf;
3359
3360   cfmt.cbSize = sizeof(CHARFORMAT);
3361   cfmt.dwMask = CFM_FACE|CFM_SIZE|CFM_CHARSET;
3362   strcpy(cfmt.szFaceName, font[boardSize][CONSOLE_FONT]->mfp.faceName);
3363   /* yHeight is expressed in twips.  A twip is 1/20 of a font's point
3364    * size.  This was undocumented in the version of MSVC++ that I had
3365    * when I wrote the code, but is apparently documented now.
3366    */
3367   cfmt.yHeight = (int)(f->mfp.pointSize * 20.0 + 0.5);
3368   cfmt.bCharSet = f->lf.lfCharSet;
3369   cfmt.bPitchAndFamily = f->lf.lfPitchAndFamily;
3370   SendMessage(hText, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &cfmt); 
3371   SendMessage(hInput, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &cfmt); 
3372   /* Why are the following seemingly needed too? */
3373   SendMessage(hText, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM) &cfmt); 
3374   SendMessage(hInput, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM) &cfmt); 
3375   SendMessage(hText, EM_EXGETSEL, 0, (LPARAM)&sel);
3376   tmpsel.cpMin = 0;
3377   tmpsel.cpMax = -1; /*999999?*/
3378   SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&tmpsel);
3379   SendMessage(hText, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &cfmt); 
3380   /* Trying putting this here too.  It still seems to tickle a RichEdit
3381    *  bug: sometimes RichEdit indents the first line of a paragraph too.
3382    */
3383   paraf.cbSize = sizeof(paraf);
3384   paraf.dwMask = PFM_OFFSET | PFM_STARTINDENT;
3385   paraf.dxStartIndent = 0;
3386   paraf.dxOffset = WRAP_INDENT;
3387   SendMessage(hText, EM_SETPARAFORMAT, 0, (LPARAM) &paraf);
3388   SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&sel);
3389 }
3390
3391 /*---------------------------------------------------------------------------*\
3392  *
3393  * Window Proc for main window
3394  *
3395 \*---------------------------------------------------------------------------*/
3396
3397 /* Process messages for main window, etc. */
3398 LRESULT CALLBACK
3399 WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
3400 {
3401   FARPROC lpProc;
3402   int wmId, wmEvent;
3403   char *defName;
3404   FILE *f;
3405   UINT number;
3406   char fileTitle[MSG_SIZ];
3407
3408   switch (message) {
3409
3410   case WM_PAINT: /* message: repaint portion of window */
3411     PaintProc(hwnd);
3412     break;
3413
3414   case WM_ERASEBKGND:
3415     if (IsIconic(hwnd)) {
3416       /* Cheat; change the message */
3417       return (DefWindowProc(hwnd, WM_ICONERASEBKGND, wParam, lParam));
3418     } else {
3419       return (DefWindowProc(hwnd, message, wParam, lParam));
3420     }
3421     break;
3422
3423   case WM_LBUTTONDOWN:
3424   case WM_MBUTTONDOWN:
3425   case WM_RBUTTONDOWN:
3426   case WM_LBUTTONUP:
3427   case WM_MBUTTONUP:
3428   case WM_RBUTTONUP:
3429   case WM_MOUSEMOVE:
3430   case WM_MOUSEWHEEL:
3431     MouseEvent(hwnd, message, wParam, lParam);
3432     break;
3433
3434   case WM_CHAR:
3435     
3436     if (appData.icsActive) {
3437       if (wParam == '\t') {
3438         if (GetKeyState(VK_SHIFT) < 0) {
3439           /* shifted */
3440           HWND h = GetDlgItem(hwndConsole, OPT_ConsoleInput);
3441           if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3442           SetFocus(h);
3443         } else {
3444           /* unshifted */
3445           HWND h = GetDlgItem(hwndConsole, OPT_ConsoleText);
3446           if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3447           SetFocus(h);
3448         }
3449       } else {
3450         HWND h = GetDlgItem(hwndConsole, OPT_ConsoleInput);
3451         if (IsIconic(hwndConsole)) ShowWindow(hwndConsole, SW_RESTORE);
3452         SetFocus(h);
3453         SendMessage(h, message, wParam, lParam);
3454       }
3455     } else if (isalpha((char)wParam) || isdigit((char)wParam)) {
3456       PopUpMoveDialog((char)wParam);
3457     }
3458     break;
3459
3460   case WM_PALETTECHANGED:
3461     if (hwnd != (HWND)wParam && !appData.monoMode) {
3462       int nnew;
3463       HDC hdc = GetDC(hwndMain);
3464       SelectPalette(hdc, hPal, TRUE);
3465       nnew = RealizePalette(hdc);
3466       if (nnew > 0) {
3467         paletteChanged = TRUE;
3468 #if 0
3469         UpdateColors(hdc);
3470 #else
3471         InvalidateRect(hwnd, &boardRect, FALSE);/*faster!*/
3472 #endif
3473       }
3474       ReleaseDC(hwnd, hdc);
3475     }
3476     break;
3477
3478   case WM_QUERYNEWPALETTE:
3479     if (!appData.monoMode /*&& paletteChanged*/) {
3480       int nnew;
3481       HDC hdc = GetDC(hwndMain);
3482       paletteChanged = FALSE;
3483       SelectPalette(hdc, hPal, FALSE);
3484       nnew = RealizePalette(hdc);
3485       if (nnew > 0) {
3486         InvalidateRect(hwnd, &boardRect, FALSE);
3487       }
3488       ReleaseDC(hwnd, hdc);
3489       return TRUE;
3490     }
3491     return FALSE;
3492
3493   case WM_COMMAND: /* message: command from application menu */
3494     wmId    = LOWORD(wParam);
3495     wmEvent = HIWORD(wParam);
3496
3497     switch (wmId) {
3498     case IDM_NewGame:
3499       ResetGameEvent();
3500       AnalysisPopDown();
3501       break;
3502
3503     case IDM_LoadGame:
3504       LoadGameDialog(hwnd, "Load Game from File");
3505       break;
3506
3507     case IDM_LoadNextGame:
3508       ReloadGame(1);
3509       break;
3510
3511     case IDM_LoadPrevGame:
3512       ReloadGame(-1);
3513       break;
3514
3515     case IDM_ReloadGame:
3516       ReloadGame(0);
3517       break;
3518
3519     case IDM_LoadPosition:
3520       if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
3521         Reset(FALSE, TRUE);
3522       }
3523       number = 1;
3524       f = OpenFileDialog(hwnd, FALSE, "",
3525                          appData.oldSaveStyle ? "pos" : "fen",
3526                          POSITION_FILT,
3527                          "Load Position from File", &number, fileTitle, NULL);
3528       if (f != NULL) {
3529         LoadPosition(f, number, fileTitle);
3530       }
3531       break;
3532
3533     case IDM_LoadNextPosition:
3534       ReloadPosition(1);
3535       break;
3536
3537     case IDM_LoadPrevPosition:
3538       ReloadPosition(-1);
3539       break;
3540
3541     case IDM_ReloadPosition:
3542       ReloadPosition(0);
3543       break;
3544
3545     case IDM_SaveGame:
3546       defName = DefaultFileName(appData.oldSaveStyle ? "gam" : "pgn");
3547       f = OpenFileDialog(hwnd, TRUE, defName,
3548                          appData.oldSaveStyle ? "gam" : "pgn",
3549                          GAME_FILT,
3550                          "Save Game to File", NULL, fileTitle, NULL);
3551       if (f != NULL) {
3552         SaveGame(f, 0, "");
3553       }
3554       break;
3555
3556     case IDM_SavePosition:
3557       defName = DefaultFileName(appData.oldSaveStyle ? "pos" : "fen");
3558       f = OpenFileDialog(hwnd, TRUE, defName,
3559                          appData.oldSaveStyle ? "pos" : "fen",
3560                          POSITION_FILT,
3561                          "Save Position to File", NULL, fileTitle, NULL);
3562       if (f != NULL) {
3563         SavePosition(f, 0, "");
3564       }
3565       break;
3566
3567     case IDM_CopyGame:
3568       CopyGameToClipboard();
3569       break;
3570
3571     case IDM_PasteGame:
3572       PasteGameFromClipboard();
3573       break;
3574
3575     case IDM_CopyPosition:
3576       CopyFENToClipboard();
3577       break;
3578
3579     case IDM_PastePosition:
3580       PasteFENFromClipboard();
3581       break;
3582
3583     case IDM_MailMove:
3584       MailMoveEvent();
3585       break;
3586
3587     case IDM_ReloadCMailMsg:
3588       Reset(TRUE, TRUE);
3589       ReloadCmailMsgEvent(FALSE);
3590       break;
3591
3592     case IDM_Minimize:
3593       ShowWindow(hwnd, SW_MINIMIZE);
3594       break;
3595
3596     case IDM_Exit:
3597       ExitEvent(0);
3598       break;
3599
3600     case IDM_MachineWhite:
3601       MachineWhiteEvent();
3602       /*
3603        * refresh the tags dialog only if it's visible
3604        */
3605       if (gameMode == MachinePlaysWhite && IsWindowVisible(editTagsDialog)) {
3606           char *tags;
3607           tags = PGNTags(&gameInfo);
3608           TagsPopUp(tags, CmailMsg());
3609           free(tags);
3610       }
3611       break;
3612
3613     case IDM_MachineBlack:
3614       MachineBlackEvent();
3615       /*
3616        * refresh the tags dialog only if it's visible
3617        */
3618       if (gameMode == MachinePlaysBlack && IsWindowVisible(editTagsDialog)) {
3619           char *tags;
3620           tags = PGNTags(&gameInfo);
3621           TagsPopUp(tags, CmailMsg());
3622           free(tags);
3623       }
3624       break;
3625
3626     case IDM_TwoMachines:
3627       TwoMachinesEvent();
3628       /*
3629        * refresh the tags dialog only if it's visible
3630        */
3631       if (gameMode == TwoMachinesPlay && IsWindowVisible(editTagsDialog)) {
3632           char *tags;
3633           tags = PGNTags(&gameInfo);
3634           TagsPopUp(tags, CmailMsg());
3635           free(tags);
3636       }
3637       break;
3638
3639     case IDM_AnalysisMode:
3640       if (!first.analysisSupport) {
3641         char buf[MSG_SIZ];
3642         sprintf(buf, "%s does not support analysis", first.tidy);
3643         DisplayError(buf, 0);
3644       } else {
3645         if (!appData.showThinking) ToggleShowThinking();
3646         AnalyzeModeEvent();
3647       }
3648       break;
3649
3650     case IDM_AnalyzeFile:
3651       if (!first.analysisSupport) {
3652         char buf[MSG_SIZ];
3653         sprintf(buf, "%s does not support analysis", first.tidy);
3654         DisplayError(buf, 0);
3655       } else {
3656         if (!appData.showThinking) ToggleShowThinking();
3657         AnalyzeFileEvent();
3658         LoadGameDialog(hwnd, "Analyze Game from File");
3659         AnalysisPeriodicEvent(1);
3660       }
3661       break;
3662
3663     case IDM_IcsClient:
3664       IcsClientEvent();
3665       break;
3666
3667     case IDM_EditGame:
3668       EditGameEvent();
3669       break;
3670
3671     case IDM_EditPosition:
3672       EditPositionEvent();
3673       break;
3674
3675     case IDM_Training:
3676       TrainingEvent();
3677       break;
3678
3679     case IDM_ShowGameList:
3680       ShowGameListProc();
3681       break;
3682
3683     case IDM_EditTags:
3684       EditTagsProc();
3685       break;
3686
3687     case IDM_EditComment:
3688       if (commentDialogUp && editComment) {
3689         CommentPopDown();
3690       } else {
3691         EditCommentEvent();
3692       }
3693       break;
3694
3695     case IDM_Pause:
3696       PauseEvent();
3697       break;
3698
3699     case IDM_Accept:
3700       AcceptEvent();
3701       break;
3702
3703     case IDM_Decline:
3704       DeclineEvent();
3705       break;
3706
3707     case IDM_Rematch:
3708       RematchEvent();
3709       break;
3710
3711     case IDM_CallFlag:
3712       CallFlagEvent();
3713       break;
3714
3715     case IDM_Draw:
3716       DrawEvent();
3717       break;
3718
3719     case IDM_Adjourn:
3720       AdjournEvent();
3721       break;
3722
3723     case IDM_Abort:
3724       AbortEvent();
3725       break;
3726
3727     case IDM_Resign:
3728       ResignEvent();
3729       break;
3730
3731     case IDM_StopObserving:
3732       StopObservingEvent();
3733       break;
3734
3735     case IDM_StopExamining:
3736       StopExaminingEvent();
3737       break;
3738
3739     case IDM_TypeInMove:
3740       PopUpMoveDialog('\000');
3741       break;
3742
3743     case IDM_Backward:
3744       BackwardEvent();
3745       SetFocus(hwndMain);
3746       break;
3747
3748     case IDM_Forward:
3749       ForwardEvent();
3750       SetFocus(hwndMain);
3751       break;
3752
3753     case IDM_ToStart:
3754       ToStartEvent();
3755       SetFocus(hwndMain);
3756       break;
3757
3758     case IDM_ToEnd:
3759       ToEndEvent();
3760       SetFocus(hwndMain);
3761       break;
3762
3763     case IDM_Revert:
3764       RevertEvent();
3765       break;
3766
3767     case IDM_TruncateGame:
3768       TruncateGameEvent();
3769       break;
3770
3771     case IDM_MoveNow:
3772       MoveNowEvent();
3773       break;
3774
3775     case IDM_RetractMove:
3776       RetractMoveEvent();
3777       break;
3778
3779     case IDM_FlipView:
3780       flipView = !flipView;
3781       DrawPosition(FALSE, NULL);
3782       break;
3783
3784     case IDM_GeneralOptions:
3785       GeneralOptionsPopup(hwnd);
3786       break;
3787
3788     case IDM_BoardOptions:
3789       BoardOptionsPopup(hwnd);
3790       break;
3791
3792     case IDM_IcsOptions:
3793       IcsOptionsPopup(hwnd);
3794       break;
3795
3796     case IDM_Fonts:
3797       FontsOptionsPopup(hwnd);
3798       break;
3799
3800     case IDM_Sounds:
3801       SoundOptionsPopup(hwnd);
3802       break;
3803
3804     case IDM_CommPort:
3805       CommPortOptionsPopup(hwnd);
3806       break;
3807
3808     case IDM_LoadOptions:
3809       LoadOptionsPopup(hwnd);
3810       break;
3811
3812     case IDM_SaveOptions:
3813       SaveOptionsPopup(hwnd);
3814       break;
3815
3816     case IDM_TimeControl:
3817       TimeControlOptionsPopup(hwnd);
3818       break;
3819
3820     case IDM_SaveSettings:
3821       SaveSettings(settingsFileName);
3822       break;
3823
3824     case IDM_SaveSettingsOnExit:
3825       saveSettingsOnExit = !saveSettingsOnExit;
3826       (void) CheckMenuItem(GetMenu(hwndMain), IDM_SaveSettingsOnExit,
3827                            MF_BYCOMMAND|(saveSettingsOnExit ?
3828                                          MF_CHECKED : MF_UNCHECKED));
3829       break;
3830
3831     case IDM_Hint:
3832       HintEvent();
3833       break;
3834
3835     case IDM_Book:
3836       BookEvent();
3837       break;
3838
3839     case IDM_AboutGame:
3840       AboutGameEvent();
3841       break;
3842
3843     case IDM_Debug:
3844       appData.debugMode = !appData.debugMode;
3845       if (appData.debugMode) {
3846         char dir[MSG_SIZ];
3847         GetCurrentDirectory(MSG_SIZ, dir);
3848         SetCurrentDirectory(installDir);
3849         debugFP = fopen("WinBoard.debug", "w");
3850         SetCurrentDirectory(dir);
3851         setbuf(debugFP, NULL);
3852       } else {
3853         fclose(debugFP);
3854         debugFP = NULL;
3855       }
3856       break;
3857
3858     case IDM_HELPCONTENTS:
3859       if (!WinHelp (hwnd, "winboard.hlp", HELP_KEY,(DWORD)(LPSTR)"CONTENTS")) {
3860         MessageBox (GetFocus(),
3861                     "Unable to activate help",
3862                     szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
3863       }
3864       break;
3865
3866     case IDM_HELPSEARCH:
3867       if (!WinHelp(hwnd, "winboard.hlp", HELP_PARTIALKEY, (DWORD)(LPSTR)"")) {
3868         MessageBox (GetFocus(),
3869                     "Unable to activate help",
3870                     szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
3871       }
3872       break;
3873
3874     case IDM_HELPHELP:
3875       if(!WinHelp(hwnd, (LPSTR)NULL, HELP_HELPONHELP, 0)) {
3876         MessageBox (GetFocus(),
3877                     "Unable to activate help",
3878                     szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
3879       }
3880       break;
3881
3882     case IDM_ABOUT:
3883       lpProc = MakeProcInstance((FARPROC)About, hInst);
3884       DialogBox(hInst, 
3885         (gameInfo.event && strcmp(gameInfo.event, "Easter Egg Hunt") == 0) ?
3886         "AboutBox2" : "AboutBox", hwnd, (DLGPROC)lpProc);
3887       FreeProcInstance(lpProc);
3888       break;
3889
3890     case IDM_DirectCommand1:
3891       AskQuestionEvent("Direct Command",
3892                        "Send to chess program:", "", "1");
3893       break;
3894     case IDM_DirectCommand2:
3895       AskQuestionEvent("Direct Command",
3896                        "Send to second chess program:", "", "2");
3897       break;
3898
3899     case EP_WhitePawn:
3900       EditPositionMenuEvent(WhitePawn, fromX, fromY);
3901       fromX = fromY = -1;
3902       break;
3903
3904     case EP_WhiteKnight:
3905       EditPositionMenuEvent(WhiteKnight, fromX, fromY);
3906       fromX = fromY = -1;
3907       break;
3908
3909     case EP_WhiteBishop:
3910       EditPositionMenuEvent(WhiteBishop, fromX, fromY);
3911       fromX = fromY = -1;
3912       break;
3913
3914     case EP_WhiteRook:
3915       EditPositionMenuEvent(WhiteRook, fromX, fromY);
3916       fromX = fromY = -1;
3917       break;
3918
3919     case EP_WhiteQueen:
3920       EditPositionMenuEvent(WhiteQueen, fromX, fromY);
3921       fromX = fromY = -1;
3922       break;
3923
3924     case EP_WhiteKing:
3925       EditPositionMenuEvent(WhiteKing, fromX, fromY);
3926       fromX = fromY = -1;
3927       break;
3928
3929     case EP_BlackPawn:
3930       EditPositionMenuEvent(BlackPawn, fromX, fromY);
3931       fromX = fromY = -1;
3932       break;
3933
3934     case EP_BlackKnight:
3935       EditPositionMenuEvent(BlackKnight, fromX, fromY);
3936       fromX = fromY = -1;
3937       break;
3938
3939     case EP_BlackBishop:
3940       EditPositionMenuEvent(BlackBishop, fromX, fromY);
3941       fromX = fromY = -1;
3942       break;
3943
3944     case EP_BlackRook:
3945       EditPositionMenuEvent(BlackRook, fromX, fromY);
3946       fromX = fromY = -1;
3947       break;
3948
3949     case EP_BlackQueen:
3950       EditPositionMenuEvent(BlackQueen, fromX, fromY);
3951       fromX = fromY = -1;
3952       break;
3953
3954     case EP_BlackKing:
3955       EditPositionMenuEvent(BlackKing, fromX, fromY);
3956       fromX = fromY = -1;
3957       break;
3958
3959     case EP_EmptySquare:
3960       EditPositionMenuEvent(EmptySquare, fromX, fromY);
3961       fromX = fromY = -1;
3962       break;
3963
3964     case EP_ClearBoard:
3965       EditPositionMenuEvent(ClearBoard, fromX, fromY);
3966       fromX = fromY = -1;
3967       break;
3968
3969     case EP_White:
3970       EditPositionMenuEvent(WhitePlay, fromX, fromY);
3971       fromX = fromY = -1;
3972       break;
3973
3974     case EP_Black:
3975       EditPositionMenuEvent(BlackPlay, fromX, fromY);
3976       fromX = fromY = -1;
3977       break;
3978
3979     case DP_Pawn:
3980       DropMenuEvent(WhitePawn, fromX, fromY);
3981       fromX = fromY = -1;
3982       break;
3983
3984     case DP_Knight:
3985       DropMenuEvent(WhiteKnight, fromX, fromY);
3986       fromX = fromY = -1;
3987       break;
3988
3989     case DP_Bishop:
3990       DropMenuEvent(WhiteBishop, fromX, fromY);
3991       fromX = fromY = -1;
3992       break;
3993
3994     case DP_Rook:
3995       DropMenuEvent(WhiteRook, fromX, fromY);
3996       fromX = fromY = -1;
3997       break;
3998
3999     case DP_Queen:
4000       DropMenuEvent(WhiteQueen, fromX, fromY);
4001       fromX = fromY = -1;
4002       break;
4003
4004     default:
4005       return (DefWindowProc(hwnd, message, wParam, lParam));
4006     }
4007     break;
4008
4009   case WM_TIMER:
4010     switch (wParam) {
4011     case CLOCK_TIMER_ID:
4012       KillTimer(hwnd, clockTimerEvent);  /* Simulate one-shot timer as in X */
4013       clockTimerEvent = 0;
4014       DecrementClocks(); /* call into back end */
4015       break;
4016     case LOAD_GAME_TIMER_ID:
4017       KillTimer(hwnd, loadGameTimerEvent); /* Simulate one-shot timer as in X*/
4018       loadGameTimerEvent = 0;
4019       AutoPlayGameLoop(); /* call into back end */
4020       break;
4021     case ANALYSIS_TIMER_ID:
4022       if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) && 
4023           appData.periodicUpdates) {
4024         AnalysisPeriodicEvent(0);
4025       } else {
4026         KillTimer(hwnd, analysisTimerEvent);
4027         analysisTimerEvent = 0;
4028       }
4029       break;
4030     case DELAYED_TIMER_ID:
4031       KillTimer(hwnd, delayedTimerEvent);
4032       delayedTimerEvent = 0;
4033       delayedTimerCallback();
4034       break;
4035     }
4036     break;
4037
4038   case WM_USER_Input:
4039     InputEvent(hwnd, message, wParam, lParam);
4040     break;
4041
4042   case WM_ENTERSIZEMOVE:
4043     if (hwnd == hwndMain) {
4044       doingSizing = TRUE;
4045       lastSizing = 0;
4046     }
4047     break;
4048
4049   case WM_SIZING:
4050     if (hwnd == hwndMain) {
4051       lastSizing = wParam;
4052     }
4053     break;
4054
4055   case WM_EXITSIZEMOVE:
4056     if (hwnd == hwndMain) {
4057       RECT client;
4058       doingSizing = FALSE;
4059       InvalidateRect(hwnd, &boardRect, FALSE);
4060       GetClientRect(hwnd, &client);
4061       ResizeBoard(client.right, client.bottom, lastSizing);
4062       lastSizing = 0;
4063     }
4064     break;
4065
4066   case WM_DESTROY: /* message: window being destroyed */
4067     PostQuitMessage(0);
4068     break;
4069
4070   case WM_CLOSE:
4071     if (hwnd == hwndMain) {
4072       ExitEvent(0);
4073     }
4074     break;
4075
4076   default:      /* Passes it on if unprocessed */
4077     return (DefWindowProc(hwnd, message, wParam, lParam));
4078   }
4079   return 0;
4080 }
4081
4082 /*---------------------------------------------------------------------------*\
4083  *
4084  * Misc utility routines
4085  *
4086 \*---------------------------------------------------------------------------*/
4087
4088 /*
4089  * Decent random number generator, at least not as bad as Windows
4090  * standard rand, which returns a value in the range 0 to 0x7fff.
4091  */
4092 unsigned int randstate;
4093
4094 int
4095 myrandom(void)
4096 {
4097   randstate = randstate * 1664525 + 1013904223;
4098   return (int) randstate & 0x7fffffff;
4099 }
4100
4101 void
4102 mysrandom(unsigned int seed)
4103 {
4104   randstate = seed;
4105 }
4106
4107
4108 /* 
4109  * returns TRUE if user selects a different color, FALSE otherwise 
4110  */
4111
4112 BOOL
4113 ChangeColor(HWND hwnd, COLORREF *which)
4114 {
4115   static BOOL firstTime = TRUE;
4116   static DWORD customColors[16];
4117   CHOOSECOLOR cc;
4118   COLORREF newcolor;
4119   int i;
4120   ColorClass ccl;
4121
4122   if (firstTime) {
4123     /* Make initial colors in use available as custom colors */
4124     /* Should we put the compiled-in defaults here instead? */
4125     i = 0;
4126     customColors[i++] = lightSquareColor & 0xffffff;
4127     customColors[i++] = darkSquareColor & 0xffffff;
4128     customColors[i++] = whitePieceColor & 0xffffff;
4129     customColors[i++] = blackPieceColor & 0xffffff;
4130     customColors[i++] = highlightSquareColor & 0xffffff;
4131     customColors[i++] = premoveHighlightColor & 0xffffff;
4132
4133     for (ccl = (ColorClass) 0; ccl < NColorClasses && i < 16; ccl++) {
4134       customColors[i++] = textAttribs[ccl].color;
4135     }
4136     while (i < 16) customColors[i++] = RGB(255, 255, 255);
4137     firstTime = FALSE;
4138   }
4139
4140   cc.lStructSize = sizeof(cc);
4141   cc.hwndOwner = hwnd;
4142   cc.hInstance = NULL;
4143   cc.rgbResult = (DWORD) (*which & 0xffffff);
4144   cc.lpCustColors = (LPDWORD) customColors;
4145   cc.Flags = CC_RGBINIT|CC_FULLOPEN;
4146
4147   if (!ChooseColor(&cc)) return FALSE;
4148
4149   newcolor = (COLORREF) (0x2000000 | cc.rgbResult);
4150   if (newcolor == *which) return FALSE;
4151   *which = newcolor;
4152   return TRUE;
4153
4154   /*
4155   InitDrawingColors();
4156   InvalidateRect(hwnd, &boardRect, FALSE);
4157   */
4158 }
4159
4160 BOOLEAN
4161 MyLoadSound(MySound *ms)
4162 {
4163   BOOL ok = FALSE;
4164   struct stat st;
4165   FILE *f;
4166
4167   if (ms->data) free(ms->data);
4168   ms->data = NULL;
4169
4170   switch (ms->name[0]) {
4171   case NULLCHAR:
4172     /* Silence */
4173     ok = TRUE;
4174     break;
4175   case '$':
4176     /* System sound from Control Panel.  Don't preload here. */
4177     ok = TRUE;
4178     break;
4179   case '!':
4180     if (ms->name[1] == NULLCHAR) {
4181       /* "!" alone = silence */
4182       ok = TRUE;
4183     } else {
4184       /* Builtin wave resource.  Error if not found. */
4185       HANDLE h = FindResource(hInst, ms->name + 1, "WAVE");
4186       if (h == NULL) break;
4187       ms->data = (void *)LoadResource(hInst, h);
4188       if (h == NULL) break;
4189       ok = TRUE;
4190     }
4191     break;
4192   default:
4193     /* .wav file.  Error if not found. */
4194     f = fopen(ms->name, "rb");
4195     if (f == NULL) break;
4196     if (fstat(fileno(f), &st) < 0) break;
4197     ms->data = malloc(st.st_size);
4198     if (fread(ms->data, st.st_size, 1, f) < 1) break;
4199     fclose(f);
4200     ok = TRUE;
4201     break;
4202   }
4203   if (!ok) {
4204     char buf[MSG_SIZ];
4205     sprintf(buf, "Error loading sound %s", ms->name);
4206     DisplayError(buf, GetLastError());
4207   }
4208   return ok;
4209 }
4210
4211 BOOLEAN
4212 MyPlaySound(MySound *ms)
4213 {
4214   BOOLEAN ok = FALSE;
4215   switch (ms->name[0]) {
4216   case NULLCHAR:
4217     /* Silence */
4218     ok = TRUE;
4219     break;
4220   case '$':
4221     /* System sound from Control Panel (deprecated feature).
4222        "$" alone or an unset sound name gets default beep (still in use). */
4223     if (ms->name[1]) {
4224       ok = PlaySound(ms->name + 1, NULL, SND_ALIAS|SND_ASYNC);
4225     }
4226     if (!ok) ok = MessageBeep(MB_OK);
4227     break; 
4228   case '!':
4229     /* Builtin wave resource, or "!" alone for silence */
4230     if (ms->name[1]) {
4231       if (ms->data == NULL) return FALSE;
4232       ok = PlaySound(ms->data, NULL, SND_MEMORY|SND_ASYNC);
4233     } else {
4234       ok = TRUE;
4235     }
4236     break;
4237   default:
4238     /* .wav file.  Error if not found. */
4239     if (ms->data == NULL) return FALSE;
4240     ok = PlaySound(ms->data, NULL, SND_MEMORY|SND_ASYNC);
4241     break;
4242   }
4243   /* Don't print an error: this can happen innocently if the sound driver
4244      is busy; for instance, if another instance of WinBoard is playing
4245      a sound at about the same time. */
4246 #if 0
4247   if (!ok) {
4248     char buf[MSG_SIZ];
4249     sprintf(buf, "Error playing sound %s", ms->name);
4250     DisplayError(buf, GetLastError());
4251   }
4252 #endif
4253   return ok;
4254 }
4255
4256
4257 LRESULT CALLBACK
4258 OldOpenFileHook(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
4259 {
4260   BOOL ok;
4261   OPENFILENAME *ofn;
4262   static UINT *number; /* gross that this is static */
4263
4264   switch (message) {
4265   case WM_INITDIALOG: /* message: initialize dialog box */
4266     /* Center the dialog over the application window */
4267     ofn = (OPENFILENAME *) lParam;
4268     if (ofn->Flags & OFN_ENABLETEMPLATE) {
4269       number = (UINT *) ofn->lCustData;
4270       SendMessage(GetDlgItem(hDlg, edt2), WM_SETTEXT, 0, (LPARAM) "");
4271     } else {
4272       number = NULL;
4273     }
4274     CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));
4275     return FALSE;  /* Allow for further processing */
4276
4277   case WM_COMMAND:
4278     if ((LOWORD(wParam) == IDOK) && (number != NULL)) {
4279       *number = GetDlgItemInt(hDlg, OPT_IndexNumberOld, &ok, FALSE);
4280     }
4281     return FALSE;  /* Allow for further processing */
4282   }
4283   return FALSE;
4284 }
4285
4286 UINT APIENTRY
4287 OpenFileHook(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
4288 {
4289   static UINT *number;
4290   OPENFILENAME *ofname;
4291   OFNOTIFY *ofnot;
4292   switch (uiMsg) {
4293   case WM_INITDIALOG:
4294     ofname = (OPENFILENAME *)lParam;
4295     number = (UINT *)(ofname->lCustData);
4296     break;
4297   case WM_NOTIFY:
4298     ofnot = (OFNOTIFY *)lParam;
4299     if (ofnot->hdr.code == CDN_FILEOK) {
4300       *number = GetDlgItemInt(hdlg, OPT_IndexNumber, NULL, FALSE);
4301     }
4302     break;
4303   }
4304   return 0;
4305 }
4306
4307
4308 FILE *
4309 OpenFileDialog(HWND hwnd, BOOL write, char *defName, char *defExt,
4310                char *nameFilt, char *dlgTitle, UINT *number,
4311                char fileTitle[MSG_SIZ], char fileName[MSG_SIZ])
4312 {
4313   OPENFILENAME openFileName;
4314   char buf1[MSG_SIZ];
4315   FILE *f;
4316
4317   if (fileName == NULL) fileName = buf1;
4318   if (defName == NULL) {
4319     strcpy(fileName, "*.");
4320     strcat(fileName, defExt);
4321   } else {
4322     strcpy(fileName, defName);
4323   }
4324   if (fileTitle) strcpy(fileTitle, "");
4325   if (number) *number = 0;
4326
4327   openFileName.lStructSize       = sizeof(OPENFILENAME);
4328   openFileName.hwndOwner         = hwnd;
4329   openFileName.hInstance         = (HANDLE) hInst;
4330   openFileName.lpstrFilter       = nameFilt;
4331   openFileName.lpstrCustomFilter = (LPSTR) NULL;
4332   openFileName.nMaxCustFilter    = 0L;
4333   openFileName.nFilterIndex      = 1L;
4334   openFileName.lpstrFile         = fileName;
4335   openFileName.nMaxFile          = MSG_SIZ;
4336   openFileName.lpstrFileTitle    = fileTitle;
4337   openFileName.nMaxFileTitle     = fileTitle ? MSG_SIZ : 0;
4338   openFileName.lpstrInitialDir   = NULL;
4339   openFileName.lpstrTitle        = dlgTitle;
4340   openFileName.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY 
4341     | (write ? 0 : OFN_FILEMUSTEXIST) 
4342     | (number ? OFN_ENABLETEMPLATE | OFN_ENABLEHOOK: 0)
4343     | (oldDialog ? 0 : OFN_EXPLORER);
4344   openFileName.nFileOffset       = 0;
4345   openFileName.nFileExtension    = 0;
4346   openFileName.lpstrDefExt       = defExt;
4347   openFileName.lCustData         = (LONG) number;
4348   openFileName.lpfnHook          = oldDialog ?
4349     (LPOFNHOOKPROC) OldOpenFileHook : (LPOFNHOOKPROC) OpenFileHook;
4350   openFileName.lpTemplateName    = (LPSTR)(oldDialog ? 1536 : DLG_IndexNumber);
4351
4352   if (write ? GetSaveFileName(&openFileName) : 
4353               GetOpenFileName(&openFileName)) {
4354     /* open the file */
4355     f = fopen(openFileName.lpstrFile, write ? "a" : "rb");
4356     if (f == NULL) {
4357       MessageBox(hwnd, "File open failed", NULL,
4358                  MB_OK|MB_ICONEXCLAMATION);
4359       return NULL;
4360     }
4361   } else {
4362     int err = CommDlgExtendedError();
4363     if (err != 0) DisplayError("Internal error in file dialog box", err);
4364     return FALSE;
4365   }
4366   return f;
4367 }
4368
4369
4370
4371 VOID APIENTRY
4372 MenuPopup(HWND hwnd, POINT pt, HMENU hmenu, UINT def)
4373 {
4374   HMENU hmenuTrackPopup;        /* floating pop-up menu  */
4375
4376   /*
4377    * Get the first pop-up menu in the menu template. This is the
4378    * menu that TrackPopupMenu displays.
4379    */
4380   hmenuTrackPopup = GetSubMenu(hmenu, 0);
4381
4382   SetMenuDefaultItem(hmenuTrackPopup, def, FALSE);
4383
4384   /*
4385    * TrackPopup uses screen coordinates, so convert the
4386    * coordinates of the mouse click to screen coordinates.
4387    */
4388   ClientToScreen(hwnd, (LPPOINT) &pt);
4389
4390   /* Draw and track the floating pop-up menu. */
4391   TrackPopupMenu(hmenuTrackPopup, TPM_CENTERALIGN | TPM_RIGHTBUTTON,
4392                  pt.x, pt.y, 0, hwnd, NULL);
4393
4394   /* Destroy the menu.*/
4395   DestroyMenu(hmenu);
4396 }
4397    
4398 typedef struct {
4399   HWND hDlg, hText;
4400   int sizeX, sizeY, newSizeX, newSizeY;
4401   HDWP hdwp;
4402 } ResizeEditPlusButtonsClosure;
4403
4404 BOOL CALLBACK
4405 ResizeEditPlusButtonsCallback(HWND hChild, LPARAM lparam)
4406 {
4407   ResizeEditPlusButtonsClosure *cl = (ResizeEditPlusButtonsClosure *)lparam;
4408   RECT rect;
4409   POINT pt;
4410
4411   if (hChild == cl->hText) return TRUE;
4412   GetWindowRect(hChild, &rect); /* gives screen coords */
4413   pt.x = rect.left + (cl->newSizeX - cl->sizeX)/2;
4414   pt.y = rect.top + cl->newSizeY - cl->sizeY;
4415   ScreenToClient(cl->hDlg, &pt);
4416   cl->hdwp = DeferWindowPos(cl->hdwp, hChild, NULL, 
4417     pt.x, pt.y, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
4418   return TRUE;
4419 }
4420
4421 /* Resize a dialog that has a (rich) edit field filling most of
4422    the top, with a row of buttons below */
4423 VOID
4424 ResizeEditPlusButtons(HWND hDlg, HWND hText, int sizeX, int sizeY, int newSizeX, int newSizeY)
4425 {
4426   RECT rectText;
4427   int newTextHeight, newTextWidth;
4428   ResizeEditPlusButtonsClosure cl;
4429   
4430   /*if (IsIconic(hDlg)) return;*/
4431   if (newSizeX == sizeX && newSizeY == sizeY) return;
4432   
4433   cl.hdwp = BeginDeferWindowPos(8);
4434
4435   GetWindowRect(hText, &rectText); /* gives screen coords */
4436   newTextWidth = rectText.right - rectText.left + newSizeX - sizeX;
4437   newTextHeight = rectText.bottom - rectText.top + newSizeY - sizeY;
4438   if (newTextHeight < 0) {
4439     newSizeY += -newTextHeight;
4440     newTextHeight = 0;
4441   }
4442   cl.hdwp = DeferWindowPos(cl.hdwp, hText, NULL, 0, 0, 
4443     newTextWidth, newTextHeight, SWP_NOZORDER|SWP_NOMOVE);
4444
4445   cl.hDlg = hDlg;
4446   cl.hText = hText;
4447   cl.sizeX = sizeX;
4448   cl.sizeY = sizeY;
4449   cl.newSizeX = newSizeX;
4450   cl.newSizeY = newSizeY;
4451   EnumChildWindows(hDlg, ResizeEditPlusButtonsCallback, (LPARAM)&cl);
4452
4453   EndDeferWindowPos(cl.hdwp);
4454 }
4455
4456 /* Center one window over another */
4457 BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
4458 {
4459     RECT    rChild, rParent;
4460     int     wChild, hChild, wParent, hParent;
4461     int     wScreen, hScreen, xNew, yNew;
4462     HDC     hdc;
4463
4464     /* Get the Height and Width of the child window */
4465     GetWindowRect (hwndChild, &rChild);
4466     wChild = rChild.right - rChild.left;
4467     hChild = rChild.bottom - rChild.top;
4468
4469     /* Get the Height and Width of the parent window */
4470     GetWindowRect (hwndParent, &rParent);
4471     wParent = rParent.right - rParent.left;
4472     hParent = rParent.bottom - rParent.top;
4473
4474     /* Get the display limits */
4475     hdc = GetDC (hwndChild);
4476     wScreen = GetDeviceCaps (hdc, HORZRES);
4477     hScreen = GetDeviceCaps (hdc, VERTRES);
4478     ReleaseDC(hwndChild, hdc);
4479
4480     /* Calculate new X position, then adjust for screen */
4481     xNew = rParent.left + ((wParent - wChild) /2);
4482     if (xNew < 0) {
4483         xNew = 0;
4484     } else if ((xNew+wChild) > wScreen) {
4485         xNew = wScreen - wChild;
4486     }
4487
4488     /* Calculate new Y position, then adjust for screen */
4489     yNew = rParent.top  + ((hParent - hChild) /2);
4490     if (yNew < 0) {
4491         yNew = 0;
4492     } else if ((yNew+hChild) > hScreen) {
4493         yNew = hScreen - hChild;
4494     }
4495
4496     /* Set it, and return */
4497     return SetWindowPos (hwndChild, NULL,
4498                          xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
4499 }
4500
4501 /*---------------------------------------------------------------------------*\
4502  *
4503  * Startup Dialog functions
4504  *
4505 \*---------------------------------------------------------------------------*/
4506 void
4507 InitComboStrings(HANDLE hwndCombo, char **cd)
4508 {
4509   SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
4510
4511   while (*cd != NULL) {
4512     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) *cd);
4513     cd++;
4514   }
4515 }
4516
4517 void
4518 InitComboStringsFromOption(HANDLE hwndCombo, char *str)
4519 {
4520   char buf1[ARG_MAX];
4521   int len;
4522
4523   if (str[0] == '@') {
4524     FILE* f = fopen(str + 1, "r");
4525     if (f == NULL) {
4526       DisplayFatalError(str + 1, errno, 2);
4527       return;
4528     }
4529     len = fread(buf1, 1, sizeof(buf1)-1, f);
4530     fclose(f);
4531     buf1[len] = NULLCHAR;
4532     str = buf1;
4533   }
4534
4535   SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
4536
4537   for (;;) {
4538     char buf[MSG_SIZ];
4539     char *end = strchr(str, '\n');
4540     if (end == NULL) return;
4541     memcpy(buf, str, end - str);
4542     buf[end - str] = NULLCHAR;
4543     SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) buf);
4544     str = end + 1;
4545   }
4546 }
4547
4548 void
4549 SetStartupDialogEnables(HWND hDlg)
4550 {
4551   EnableWindow(GetDlgItem(hDlg, OPT_ChessEngineName),
4552     IsDlgButtonChecked(hDlg, OPT_ChessEngine) ||
4553     appData.zippyPlay && IsDlgButtonChecked(hDlg, OPT_ChessServer));
4554   EnableWindow(GetDlgItem(hDlg, OPT_SecondChessEngineName),
4555     IsDlgButtonChecked(hDlg, OPT_ChessEngine));
4556   EnableWindow(GetDlgItem(hDlg, OPT_ChessServerName),
4557     IsDlgButtonChecked(hDlg, OPT_ChessServer));
4558   EnableWindow(GetDlgItem(hDlg, OPT_AdditionalOptions),
4559     IsDlgButtonChecked(hDlg, OPT_AnyAdditional));
4560   EnableWindow(GetDlgItem(hDlg, IDOK),
4561     IsDlgButtonChecked(hDlg, OPT_ChessEngine) ||
4562     IsDlgButtonChecked(hDlg, OPT_ChessServer) ||
4563     IsDlgButtonChecked(hDlg, OPT_View));
4564 }
4565
4566 char *
4567 QuoteForFilename(char *filename)
4568 {
4569   int dquote, space;
4570   dquote = strchr(filename, '"') != NULL;
4571   space = strchr(filename, ' ') != NULL;
4572   if (dquote || space) {
4573     if (dquote) {
4574       return "'";
4575     } else {
4576       return "\"";
4577     }
4578   } else {
4579     return "";
4580   }
4581 }
4582
4583 VOID
4584 InitEngineBox(HWND hDlg, HWND hwndCombo, char* nthcp, char* nthd, char* nthdir, char *nthnames)
4585 {
4586   char buf[MSG_SIZ];
4587   char *q;
4588
4589   InitComboStringsFromOption(hwndCombo, nthnames);
4590   q = QuoteForFilename(nthcp);
4591   sprintf(buf, "%s%s%s", q, nthcp, q);
4592   if (*nthdir != NULLCHAR) {
4593     q = QuoteForFilename(nthdir);
4594     sprintf(buf + strlen(buf), " /%s=%s%s%s", nthd, q, nthdir, q);
4595   }
4596   if (*nthcp == NULLCHAR) {
4597     SendMessage(hwndCombo, CB_SETCURSEL, (WPARAM) 0, (LPARAM) 0);
4598   } else if (SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) buf) == CB_ERR) {
4599     SendMessage(hwndCombo, CB_SETCURSEL, (WPARAM) -1, (LPARAM) 0);
4600     SendMessage(hwndCombo, WM_SETTEXT, (WPARAM) 0, (LPARAM) buf);
4601   }
4602 }
4603
4604 LRESULT CALLBACK
4605 StartupDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
4606 {
4607   char buf[MSG_SIZ];
4608   HANDLE hwndCombo;
4609   char *p;
4610
4611   switch (message) {
4612   case WM_INITDIALOG:
4613     /* Center the dialog */
4614     CenterWindow (hDlg, GetDesktopWindow());
4615     /* Initialize the dialog items */
4616     InitEngineBox(hDlg, GetDlgItem(hDlg, OPT_ChessEngineName),
4617                   appData.firstChessProgram, "fd", appData.firstDirectory,
4618                   firstChessProgramNames);
4619     InitEngineBox(hDlg, GetDlgItem(hDlg, OPT_SecondChessEngineName),
4620                   appData.secondChessProgram, "sd", appData.secondDirectory,
4621                   secondChessProgramNames);
4622     hwndCombo = GetDlgItem(hDlg, OPT_ChessServerName);
4623     InitComboStringsFromOption(hwndCombo, icsNames);    
4624     sprintf(buf, "%s /icsport=%s", appData.icsHost, appData.icsPort);
4625     if (*appData.icsHelper != NULLCHAR) {
4626       char *q = QuoteForFilename(appData.icsHelper);
4627       sprintf(buf + strlen(buf), " /icshelper=%s%s%s", q, appData.icsHelper, q);
4628     }
4629     if (*appData.icsHost == NULLCHAR) {
4630       SendMessage(hwndCombo, CB_SETCURSEL, (WPARAM) 0, (LPARAM) 0);
4631       /*SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, (LPARAM) 0); !!too soon */
4632     } else if (SendMessage(hwndCombo, CB_SELECTSTRING, (WPARAM) -1, (LPARAM) buf) == CB_ERR) {
4633       SendMessage(hwndCombo, CB_SETCURSEL, (WPARAM) -1, (LPARAM) 0);
4634       SendMessage(hwndCombo, WM_SETTEXT, (WPARAM) 0, (LPARAM) buf);
4635     }
4636     if (chessProgram) {
4637       CheckDlgButton(hDlg, OPT_ChessEngine, BST_CHECKED);
4638     } else if (appData.icsActive) {
4639       CheckDlgButton(hDlg, OPT_ChessServer, BST_CHECKED);
4640     } else if (appData.noChessProgram) {
4641       CheckDlgButton(hDlg, OPT_View, BST_CHECKED);
4642     }
4643     SetStartupDialogEnables(hDlg);
4644     return TRUE;
4645
4646   case WM_COMMAND:
4647     switch (LOWORD(wParam)) {
4648     case IDOK:
4649       if (IsDlgButtonChecked(hDlg, OPT_ChessEngine)) {
4650         strcpy(buf, "/fcp=");
4651         GetDlgItemText(hDlg, OPT_ChessEngineName, buf + strlen(buf), sizeof(buf) - strlen(buf));
4652         p = buf;
4653         ParseArgs(StringGet, &p);
4654         strcpy(buf, "/scp=");
4655         GetDlgItemText(hDlg, OPT_SecondChessEngineName, buf + strlen(buf), sizeof(buf) - strlen(buf));
4656         p = buf;
4657         ParseArgs(StringGet, &p);
4658         appData.noChessProgram = FALSE;
4659         appData.icsActive = FALSE;
4660       } else if (IsDlgButtonChecked(hDlg, OPT_ChessServer)) {
4661         strcpy(buf, "/ics /icshost=");
4662         GetDlgItemText(hDlg, OPT_ChessServerName, buf + strlen(buf), sizeof(buf) - strlen(buf));
4663         p = buf;
4664         ParseArgs(StringGet, &p);
4665         if (appData.zippyPlay) {
4666           strcpy(buf, "/fcp=");
4667           GetDlgItemText(hDlg, OPT_ChessEngineName, buf + strlen(buf), sizeof(buf) - strlen(buf));
4668           p = buf;
4669           ParseArgs(StringGet, &p);
4670         }
4671       } else if (IsDlgButtonChecked(hDlg, OPT_View)) {
4672         appData.noChessProgram = TRUE;
4673         appData.icsActive = FALSE;
4674       } else {
4675         MessageBox(hDlg, "Choose an option, or cancel to exit",
4676                    "Option Error", MB_OK|MB_ICONEXCLAMATION);
4677         return TRUE;
4678       }
4679       if (IsDlgButtonChecked(hDlg, OPT_AnyAdditional)) {
4680         GetDlgItemText(hDlg, OPT_AdditionalOptions, buf, sizeof(buf));
4681         p = buf;
4682         ParseArgs(StringGet, &p);
4683       }
4684       EndDialog(hDlg, TRUE);
4685       return TRUE;
4686
4687     case IDCANCEL:
4688       ExitEvent(0);
4689       return TRUE;
4690
4691     case IDM_HELPCONTENTS:
4692       if (!WinHelp (hDlg, "winboard.hlp", HELP_KEY,(DWORD)(LPSTR)"CONTENTS")) {
4693         MessageBox (GetFocus(),
4694                     "Unable to activate help",
4695                     szAppName, MB_SYSTEMMODAL|MB_OK|MB_ICONHAND);
4696       }
4697       break;
4698
4699     default:
4700       SetStartupDialogEnables(hDlg);
4701       break;
4702     }
4703     break;
4704   }
4705   return FALSE;
4706 }
4707
4708 /*---------------------------------------------------------------------------*\
4709  *
4710  * About box dialog functions
4711  *
4712 \*---------------------------------------------------------------------------*/
4713
4714 /* Process messages for "About" dialog box */
4715 LRESULT CALLBACK
4716 About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
4717 {
4718   switch (message) {
4719   case WM_INITDIALOG: /* message: initialize dialog box */
4720     /* Center the dialog over the application window */
4721     CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));
4722     SetDlgItemText(hDlg, ABOUTBOX_Version, programVersion);
4723     return (TRUE);
4724
4725   case WM_COMMAND: /* message: received a command */
4726     if (LOWORD(wParam) == IDOK /* "OK" box selected? */
4727         || LOWORD(wParam) == IDCANCEL) { /* System menu close command? */
4728       EndDialog(hDlg, TRUE); /* Exit the dialog */
4729       return (TRUE);
4730     }
4731     break;
4732   }
4733   return (FALSE);
4734 }
4735
4736 /*---------------------------------------------------------------------------*\
4737  *
4738  * Comment Dialog functions
4739  *
4740 \*---------------------------------------------------------------------------*/
4741
4742 LRESULT CALLBACK
4743 CommentDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
4744 {
4745   static HANDLE hwndText = NULL;
4746   int len, newSizeX, newSizeY, flags;
4747   static int sizeX, sizeY;
4748   char *str;
4749   RECT rect;
4750   MINMAXINFO *mmi;
4751
4752   switch (message) {
4753   case WM_INITDIALOG: /* message: initialize dialog box */
4754     /* Initialize the dialog items */
4755     hwndText = GetDlgItem(hDlg, OPT_CommentText);
4756     SetDlgItemText(hDlg, OPT_CommentText, commentText);
4757     EnableWindow(GetDlgItem(hDlg, OPT_CancelComment), editComment);
4758     EnableWindow(GetDlgItem(hDlg, OPT_ClearComment), editComment);
4759     EnableWindow(GetDlgItem(hDlg, OPT_EditComment), !editComment);
4760     SendMessage(hwndText, EM_SETREADONLY, !editComment, 0);
4761     SetWindowText(hDlg, commentTitle);
4762     if (editComment) {
4763       SetFocus(hwndText);
4764     } else {
4765       SetFocus(GetDlgItem(hDlg, IDOK));
4766     }
4767     SendMessage(GetDlgItem(hDlg, OPT_CommentText),
4768                 WM_SETFONT, (WPARAM)font[boardSize][COMMENT_FONT]->hf,
4769                 MAKELPARAM(FALSE, 0));
4770     /* Size and position the dialog */
4771     if (!commentDialog) {
4772       commentDialog = hDlg;
4773       flags = SWP_NOZORDER;
4774       GetClientRect(hDlg, &rect);
4775       sizeX = rect.right;
4776       sizeY = rect.bottom;
4777       if (commentX != CW_USEDEFAULT && commentY != CW_USEDEFAULT &&
4778           commentW != CW_USEDEFAULT && commentH != CW_USEDEFAULT) {
4779         WINDOWPLACEMENT wp;
4780         EnsureOnScreen(&commentX, &commentY);
4781         wp.length = sizeof(WINDOWPLACEMENT);
4782         wp.flags = 0;
4783         wp.showCmd = SW_SHOW;
4784         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
4785         wp.rcNormalPosition.left = commentX;
4786         wp.rcNormalPosition.right = commentX + commentW;
4787         wp.rcNormalPosition.top = commentY;
4788         wp.rcNormalPosition.bottom = commentY + commentH;
4789         SetWindowPlacement(hDlg, &wp);
4790
4791         GetClientRect(hDlg, &rect);
4792         newSizeX = rect.right;
4793         newSizeY = rect.bottom;
4794         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
4795                               newSizeX, newSizeY);
4796         sizeX = newSizeX;
4797         sizeY = newSizeY;
4798       }
4799     }
4800     return FALSE;
4801
4802   case WM_COMMAND: /* message: received a command */
4803     switch (LOWORD(wParam)) {
4804     case IDOK:
4805       if (editComment) {
4806         char *p, *q;
4807         /* Read changed options from the dialog box */
4808         hwndText = GetDlgItem(hDlg, OPT_CommentText);
4809         len = GetWindowTextLength(hwndText);
4810         str = (char *) malloc(len + 1);
4811         GetWindowText(hwndText, str, len + 1);
4812         p = q = str;
4813         while (*q) {
4814           if (*q == '\r')
4815             q++;
4816           else
4817             *p++ = *q++;
4818         }
4819         *p = NULLCHAR;
4820         ReplaceComment(commentIndex, str);
4821         free(str);
4822       }
4823       CommentPopDown();
4824       return TRUE;
4825
4826     case IDCANCEL:
4827     case OPT_CancelComment:
4828       CommentPopDown();
4829       return TRUE;
4830
4831     case OPT_ClearComment:
4832       SetDlgItemText(hDlg, OPT_CommentText, "");
4833       break;
4834
4835     case OPT_EditComment:
4836       EditCommentEvent();
4837       return TRUE;
4838
4839     default:
4840       break;
4841     }
4842     break;
4843
4844   case WM_SIZE:
4845     newSizeX = LOWORD(lParam);
4846     newSizeY = HIWORD(lParam);
4847     ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY, newSizeX, newSizeY);
4848     sizeX = newSizeX;
4849     sizeY = newSizeY;
4850     break;
4851
4852   case WM_GETMINMAXINFO:
4853     /* Prevent resizing window too small */
4854     mmi = (MINMAXINFO *) lParam;
4855     mmi->ptMinTrackSize.x = 100;
4856     mmi->ptMinTrackSize.y = 100;
4857     break;
4858   }
4859   return FALSE;
4860 }
4861
4862 VOID
4863 EitherCommentPopUp(int index, char *title, char *str, BOOLEAN edit)
4864 {
4865   FARPROC lpProc;
4866   char *p, *q;
4867
4868   CheckMenuItem(GetMenu(hwndMain), IDM_EditComment, edit ? MF_CHECKED : MF_UNCHECKED);
4869
4870   if (str == NULL) str = "";
4871   p = (char *) malloc(2 * strlen(str) + 2);
4872   q = p;
4873   while (*str) {
4874     if (*str == '\n') *q++ = '\r';
4875     *q++ = *str++;
4876   }
4877   *q = NULLCHAR;
4878   if (commentText != NULL) free(commentText);
4879
4880   commentIndex = index;
4881   commentTitle = title;
4882   commentText = p;
4883   editComment = edit;
4884
4885   if (commentDialog) {
4886     SendMessage(commentDialog, WM_INITDIALOG, 0, 0);
4887     if (!commentDialogUp) ShowWindow(commentDialog, SW_SHOW);
4888   } else {
4889     lpProc = MakeProcInstance((FARPROC)CommentDialog, hInst);
4890     CreateDialog(hInst, MAKEINTRESOURCE(DLG_EditComment),
4891                  hwndMain, (DLGPROC)lpProc);
4892     FreeProcInstance(lpProc);
4893   }
4894   commentDialogUp = TRUE;
4895 }
4896
4897
4898 /*---------------------------------------------------------------------------*\
4899  *
4900  * Type-in move dialog functions
4901  * 
4902 \*---------------------------------------------------------------------------*/
4903
4904 LRESULT CALLBACK
4905 TypeInMoveDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
4906 {
4907   char move[MSG_SIZ];
4908   HWND hInput;
4909   ChessMove moveType;
4910   int fromX, fromY, toX, toY;
4911   char promoChar;
4912
4913   switch (message) {
4914   case WM_INITDIALOG:
4915     move[0] = (char) lParam;
4916     move[1] = NULLCHAR;
4917     CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
4918     hInput = GetDlgItem(hDlg, OPT_Move);
4919     SetWindowText(hInput, move);
4920     SetFocus(hInput);
4921     SendMessage(hInput, EM_SETSEL, (WPARAM)9999, (LPARAM)9999);
4922     return FALSE;
4923
4924   case WM_COMMAND:
4925     switch (LOWORD(wParam)) {
4926     case IDOK:
4927       if (gameMode != EditGame && currentMove != forwardMostMove && 
4928         gameMode != Training) {
4929         DisplayMoveError("Displayed move is not current");
4930       } else {
4931         GetDlgItemText(hDlg, OPT_Move, move, sizeof(move));
4932         if (ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, 
4933           &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) {
4934           if (gameMode != Training)
4935               forwardMostMove = currentMove;
4936           UserMoveEvent(fromX, fromY, toX, toY, promoChar);     
4937         } else {
4938           DisplayMoveError("Could not parse move");
4939         }
4940       }
4941       EndDialog(hDlg, TRUE);
4942       return TRUE;
4943     case IDCANCEL:
4944       EndDialog(hDlg, FALSE);
4945       return TRUE;
4946     default:
4947       break;
4948     }
4949     break;
4950   }
4951   return FALSE;
4952 }
4953
4954 VOID
4955 PopUpMoveDialog(char firstchar)
4956 {
4957     FARPROC lpProc;
4958     
4959     if ((gameMode == BeginningOfGame && !appData.icsActive) || 
4960         gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
4961         gameMode == AnalyzeMode || gameMode == EditGame || 
4962         gameMode == EditPosition || gameMode == IcsExamining ||
4963         gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
4964         gameMode == Training) {
4965       lpProc = MakeProcInstance((FARPROC)TypeInMoveDialog, hInst);
4966       DialogBoxParam(hInst, MAKEINTRESOURCE(DLG_TypeInMove),
4967         hwndMain, (DLGPROC)lpProc, (LPARAM)firstchar);
4968       FreeProcInstance(lpProc);
4969     }
4970 }
4971
4972 /*---------------------------------------------------------------------------*\
4973  *
4974  *  Error dialogs
4975  * 
4976 \*---------------------------------------------------------------------------*/
4977
4978 /* Nonmodal error box */
4979 LRESULT CALLBACK ErrorDialog(HWND hDlg, UINT message,
4980                              WPARAM wParam, LPARAM lParam);
4981
4982 VOID
4983 ErrorPopUp(char *title, char *content)
4984 {
4985   FARPROC lpProc;
4986   char *p, *q;
4987   BOOLEAN modal = hwndMain == NULL;
4988
4989   p = content;
4990   q = errorMessage;
4991   while (*p) {
4992     if (*p == '\n') {
4993       if (modal) {
4994         *q++ = ' ';
4995         p++;
4996       } else {
4997         *q++ = '\r';
4998         *q++ = *p++;
4999       }
5000     } else {
5001       *q++ = *p++;
5002     }
5003   }
5004   *q = NULLCHAR;
5005   strncpy(errorTitle, title, sizeof(errorTitle));
5006   errorTitle[sizeof(errorTitle) - 1] = '\0';
5007   
5008   if (modal) {
5009     MessageBox(NULL, errorMessage, errorTitle, MB_OK|MB_ICONEXCLAMATION);
5010   } else {
5011     lpProc = MakeProcInstance((FARPROC)ErrorDialog, hInst);
5012     CreateDialog(hInst, MAKEINTRESOURCE(DLG_Error),
5013                  hwndMain, (DLGPROC)lpProc);
5014     FreeProcInstance(lpProc);
5015   }
5016 }
5017
5018 VOID
5019 ErrorPopDown()
5020 {
5021   if (!appData.popupMoveErrors && moveErrorMessageUp) DisplayMessage("", "");
5022   if (errorDialog == NULL) return;
5023   DestroyWindow(errorDialog);
5024   errorDialog = NULL;
5025 }
5026
5027 LRESULT CALLBACK
5028 ErrorDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
5029 {
5030   HANDLE hwndText;
5031   RECT rChild;
5032
5033   switch (message) {
5034   case WM_INITDIALOG:
5035     GetWindowRect(hDlg, &rChild);
5036     SetWindowPos(hDlg, NULL, rChild.left,
5037       rChild.top + boardRect.top - (rChild.bottom - rChild.top), 
5038       0, 0, SWP_NOZORDER|SWP_NOSIZE);
5039     errorDialog = hDlg;
5040     SetWindowText(hDlg, errorTitle);
5041     hwndText = GetDlgItem(hDlg, OPT_ErrorText);
5042     SetDlgItemText(hDlg, OPT_ErrorText, errorMessage);
5043     return FALSE;
5044
5045   case WM_COMMAND:
5046     switch (LOWORD(wParam)) {
5047     case IDOK:
5048     case IDCANCEL:
5049       if (errorDialog == hDlg) errorDialog = NULL;
5050       DestroyWindow(hDlg);
5051       return TRUE;
5052
5053     default:
5054       break;
5055     }
5056     break;
5057   }
5058   return FALSE;
5059 }
5060
5061 /*---------------------------------------------------------------------------*\
5062  *
5063  *  Ics Interaction console functions
5064  *
5065 \*---------------------------------------------------------------------------*/
5066
5067 #define HISTORY_SIZE 64
5068 static char *history[HISTORY_SIZE];
5069 int histIn = 0, histP = 0;
5070
5071 VOID
5072 SaveInHistory(char *cmd)
5073 {
5074   if (history[histIn] != NULL) {
5075     free(history[histIn]);
5076     history[histIn] = NULL;
5077   }
5078   if (*cmd == NULLCHAR) return;
5079   history[histIn] = StrSave(cmd);
5080   histIn = (histIn + 1) % HISTORY_SIZE;
5081   if (history[histIn] != NULL) {
5082     free(history[histIn]);
5083     history[histIn] = NULL;
5084   }
5085   histP = histIn;
5086 }
5087
5088 char *
5089 PrevInHistory(char *cmd)
5090 {
5091   int newhp;
5092   if (histP == histIn) {
5093     if (history[histIn] != NULL) free(history[histIn]);
5094     history[histIn] = StrSave(cmd);
5095   }
5096   newhp = (histP - 1 + HISTORY_SIZE) % HISTORY_SIZE;
5097   if (newhp == histIn || history[newhp] == NULL) return NULL;
5098   histP = newhp;
5099   return history[histP];
5100 }
5101
5102 char *
5103 NextInHistory()
5104 {
5105   if (histP == histIn) return NULL;
5106   histP = (histP + 1) % HISTORY_SIZE;
5107   return history[histP];
5108 }
5109
5110 typedef struct {
5111   char *item;
5112   char *command;
5113   BOOLEAN getname;
5114   BOOLEAN immediate;
5115 } IcsTextMenuEntry;
5116 #define ICS_TEXT_MENU_SIZE (IDM_CommandXLast - IDM_CommandX + 1)
5117 IcsTextMenuEntry icsTextMenuEntry[ICS_TEXT_MENU_SIZE];
5118
5119 void
5120 ParseIcsTextMenu(char *icsTextMenuString)
5121 {
5122   int flags = 0;
5123   IcsTextMenuEntry *e = icsTextMenuEntry;
5124   char *p = icsTextMenuString;
5125   while (e->item != NULL && e < icsTextMenuEntry + ICS_TEXT_MENU_SIZE) {
5126     free(e->item);
5127     e->item = NULL;
5128     if (e->command != NULL) {
5129       free(e->command);
5130       e->command = NULL;
5131     }
5132     e++;
5133   }
5134   e = icsTextMenuEntry;
5135   while (*p && e < icsTextMenuEntry + ICS_TEXT_MENU_SIZE) {
5136     if (*p == ';' || *p == '\n') {
5137       e->item = strdup("-");
5138       e->command = NULL;
5139       p++;
5140     } else if (*p == '-') {
5141       e->item = strdup("-");
5142       e->command = NULL;
5143       p++;
5144       if (*p) p++;
5145     } else {
5146       char *q, *r, *s, *t;
5147       char c;
5148       q = strchr(p, ',');
5149       if (q == NULL) break;
5150       *q = NULLCHAR;
5151       r = strchr(q + 1, ',');
5152       if (r == NULL) break;
5153       *r = NULLCHAR;
5154       s = strchr(r + 1, ',');
5155       if (s == NULL) break;
5156       *s = NULLCHAR;
5157       c = ';';
5158       t = strchr(s + 1, c);
5159       if (t == NULL) {
5160         c = '\n';
5161         t = strchr(s + 1, c);
5162       }
5163       if (t != NULL) *t = NULLCHAR;
5164       e->item = strdup(p);
5165       e->command = strdup(q + 1);
5166       e->getname = *(r + 1) != '0';
5167       e->immediate = *(s + 1) != '0';
5168       *q = ',';
5169       *r = ',';
5170       *s = ',';
5171       if (t == NULL) break;
5172       *t = c;
5173       p = t + 1;
5174     }
5175     e++;
5176   } 
5177 }
5178
5179 HMENU
5180 LoadIcsTextMenu(IcsTextMenuEntry *e)
5181 {
5182   HMENU hmenu, h;
5183   int i = 0;
5184   hmenu = LoadMenu(hInst, "TextMenu");
5185   h = GetSubMenu(hmenu, 0);
5186   while (e->item) {
5187     if (strcmp(e->item, "-") == 0) {
5188       AppendMenu(h, MF_SEPARATOR, 0, 0);
5189     } else {
5190       if (e->item[0] == '|') {
5191         AppendMenu(h, MF_STRING|MF_MENUBARBREAK,
5192                    IDM_CommandX + i, &e->item[1]);
5193       } else {
5194         AppendMenu(h, MF_STRING, IDM_CommandX + i, e->item);
5195       }
5196     }
5197     e++;
5198     i++;
5199   } 
5200   return hmenu;
5201 }
5202
5203 WNDPROC consoleTextWindowProc;
5204
5205 void
5206 CommandX(HWND hwnd, char *command, BOOLEAN getname, BOOLEAN immediate)
5207 {
5208   char buf[MSG_SIZ], name[MSG_SIZ];
5209   HWND hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
5210   CHARRANGE sel;
5211
5212   if (!getname) {
5213     SetWindowText(hInput, command);
5214     if (immediate) {
5215       SendMessage(hInput, WM_CHAR, '\r', 0);
5216     } else {
5217       sel.cpMin = 999999;
5218       sel.cpMax = 999999;
5219       SendMessage(hInput, EM_EXSETSEL, 0, (LPARAM)&sel);
5220       SetFocus(hInput);
5221     }
5222     return;
5223   }    
5224   SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
5225   if (sel.cpMin == sel.cpMax) {
5226     /* Expand to surrounding word */
5227     TEXTRANGE tr;
5228     do {
5229       tr.chrg.cpMax = sel.cpMin;
5230       tr.chrg.cpMin = --sel.cpMin;
5231       if (sel.cpMin < 0) break;
5232       tr.lpstrText = name;
5233       SendMessage(hwnd, EM_GETTEXTRANGE, 0, (LPARAM) &tr);
5234     } while (isalpha(name[0]) || isdigit(name[0]) || name[0] == '-');
5235     sel.cpMin++;
5236
5237     do {
5238       tr.chrg.cpMin = sel.cpMax;
5239       tr.chrg.cpMax = ++sel.cpMax;
5240       tr.lpstrText = name;
5241       if (SendMessage(hwnd, EM_GETTEXTRANGE, 0, (LPARAM) &tr) < 1) break;
5242     } while (isalpha(name[0]) || isdigit(name[0]) || name[0] == '-');
5243     sel.cpMax--;
5244
5245     if (sel.cpMax == sel.cpMin || sel.cpMax - sel.cpMin > MSG_SIZ/2) {
5246       MessageBeep(MB_ICONEXCLAMATION);
5247       return;
5248     }
5249     tr.chrg = sel;
5250     tr.lpstrText = name;
5251     SendMessage(hwnd, EM_GETTEXTRANGE, 0, (LPARAM) &tr);
5252   } else {
5253     if (sel.cpMax - sel.cpMin > MSG_SIZ/2) {
5254       MessageBeep(MB_ICONEXCLAMATION);
5255       return;
5256     }
5257     SendMessage(hwnd, EM_GETSELTEXT, 0, (LPARAM) name);
5258   }
5259   if (immediate) {
5260     sprintf(buf, "%s %s", command, name);
5261     SetWindowText(hInput, buf);
5262     SendMessage(hInput, WM_CHAR, '\r', 0);
5263   } else {
5264     sprintf(buf, "%s %s ", command, name); /* trailing space */
5265     SetWindowText(hInput, buf);
5266     sel.cpMin = 999999;
5267     sel.cpMax = 999999;
5268     SendMessage(hInput, EM_EXSETSEL, 0, (LPARAM)&sel);
5269     SetFocus(hInput);
5270   }
5271 }
5272
5273 LRESULT CALLBACK 
5274 ConsoleTextSubclass(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5275 {
5276   HWND hInput;
5277   CHARRANGE sel;
5278
5279   switch (message) {
5280   case WM_KEYDOWN:
5281     if (!(GetKeyState(VK_CONTROL) & ~1)) break;
5282     switch (wParam) {
5283     case VK_PRIOR:
5284       SendMessage(hwnd, EM_LINESCROLL, 0, -999999);
5285       return 0;
5286     case VK_NEXT:
5287       sel.cpMin = 999999;
5288       sel.cpMax = 999999;
5289       SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
5290       SendMessage(hwnd, EM_SCROLLCARET, 0, 0);
5291       return 0;
5292     }
5293     break;
5294   case WM_CHAR:
5295     if (wParam == '\t') {
5296       if (GetKeyState(VK_SHIFT) < 0) {
5297         /* shifted */
5298         if (IsIconic(hwndMain)) ShowWindow(hwndMain, SW_RESTORE);
5299         if (buttonDesc[0].hwnd) {
5300           SetFocus(buttonDesc[0].hwnd);
5301         } else {
5302           SetFocus(hwndMain);
5303         }
5304       } else {
5305         /* unshifted */
5306         SetFocus(GetDlgItem(hwndConsole, OPT_ConsoleInput));
5307       }
5308     } else {
5309       hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
5310       SetFocus(hInput);
5311       SendMessage(hInput, message, wParam, lParam);
5312     }
5313     return 0;
5314   case WM_PASTE:
5315     hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
5316     SetFocus(hInput);
5317     return SendMessage(hInput, message, wParam, lParam);
5318   case WM_MBUTTONDOWN:
5319     return SendMessage(hwnd, WM_COMMAND, MAKEWPARAM(IDM_QuickPaste, 0), 0);
5320   case WM_RBUTTONDOWN:
5321     if (!(GetKeyState(VK_SHIFT) & ~1)) {
5322       /* Move selection here if it was empty */
5323       POINT pt;
5324       pt.x = LOWORD(lParam);
5325       pt.y = HIWORD(lParam);
5326       SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
5327       if (sel.cpMin == sel.cpMax) {
5328         sel.cpMin = SendMessage(hwnd, EM_CHARFROMPOS, 0, (LPARAM)&pt); /*doc is wrong*/
5329         sel.cpMax = sel.cpMin;
5330         SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
5331       }
5332       SendMessage(hwnd, EM_HIDESELECTION, FALSE, FALSE);
5333     }
5334     return 0;
5335   case WM_RBUTTONUP:
5336     if (GetKeyState(VK_SHIFT) & ~1) {
5337       SendDlgItemMessage(hwndConsole, OPT_ConsoleText, 
5338         WM_COMMAND, MAKEWPARAM(IDM_QuickPaste, 0), 0);
5339     } else {
5340       POINT pt;
5341       HMENU hmenu = LoadIcsTextMenu(icsTextMenuEntry);
5342       SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
5343       if (sel.cpMin == sel.cpMax) {
5344         EnableMenuItem(hmenu, IDM_Copy, MF_BYCOMMAND|MF_GRAYED);
5345         EnableMenuItem(hmenu, IDM_QuickPaste, MF_BYCOMMAND|MF_GRAYED);
5346       }
5347       if (!IsClipboardFormatAvailable(CF_TEXT)) {
5348         EnableMenuItem(hmenu, IDM_Paste, MF_BYCOMMAND|MF_GRAYED);
5349       }
5350       pt.x = LOWORD(lParam);
5351       pt.y = HIWORD(lParam);
5352       MenuPopup(hwnd, pt, hmenu, -1);
5353     }
5354     return 0;
5355   case WM_COMMAND:
5356     switch (LOWORD(wParam)) {
5357     case IDM_QuickPaste:
5358       {
5359         SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
5360         if (sel.cpMin == sel.cpMax) {
5361           MessageBeep(MB_ICONEXCLAMATION);
5362           return 0;
5363         }
5364         SendMessage(hwnd, WM_COPY, 0, 0);
5365         hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
5366         SendMessage(hInput, WM_PASTE, 0, 0);
5367         SetFocus(hInput);
5368         return 0;
5369       }
5370     case IDM_Cut:
5371       SendMessage(hwnd, WM_CUT, 0, 0);
5372       return 0;
5373     case IDM_Paste:
5374       SendMessage(hwnd, WM_PASTE, 0, 0);
5375       return 0;
5376     case IDM_Copy:
5377       SendMessage(hwnd, WM_COPY, 0, 0);
5378       return 0;
5379     default:
5380       {
5381         int i = LOWORD(wParam) - IDM_CommandX;
5382         if (i >= 0 && i < ICS_TEXT_MENU_SIZE &&
5383             icsTextMenuEntry[i].command != NULL) {
5384           CommandX(hwnd, icsTextMenuEntry[i].command,
5385                    icsTextMenuEntry[i].getname,
5386                    icsTextMenuEntry[i].immediate);
5387           return 0;
5388         }
5389       }
5390       break;
5391     }
5392     break;
5393   }
5394   return (*consoleTextWindowProc)(hwnd, message, wParam, lParam);
5395 }
5396
5397 WNDPROC consoleInputWindowProc;
5398
5399 LRESULT CALLBACK
5400 ConsoleInputSubclass(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5401 {
5402   char buf[MSG_SIZ];
5403   char *p;
5404   static BOOL sendNextChar = FALSE;
5405   static BOOL quoteNextChar = FALSE;
5406   InputSource *is = consoleInputSource;
5407   CHARFORMAT cf;
5408   CHARRANGE sel;
5409
5410   switch (message) {
5411   case WM_CHAR:
5412     if (!appData.localLineEditing || sendNextChar) {
5413       is->buf[0] = (CHAR) wParam;
5414       is->count = 1;
5415       SendMessage(hwndMain, WM_USER_Input, 0, (LPARAM) is);
5416       sendNextChar = FALSE;
5417       return 0;
5418     }
5419     if (quoteNextChar) {
5420       buf[0] = (char) wParam;
5421       buf[1] = NULLCHAR;
5422       SendMessage(hwnd, EM_REPLACESEL, TRUE, (LPARAM) buf);
5423       quoteNextChar = FALSE;
5424       return 0;
5425     }
5426     switch (wParam) {
5427     case '\r':   /* Enter key */
5428       is->count = GetWindowText(hwnd, is->buf, INPUT_SOURCE_BUF_SIZE-1);     
5429       if (consoleEcho) SaveInHistory(is->buf);
5430       is->buf[is->count++] = '\n';
5431       is->buf[is->count] = NULLCHAR;
5432       SendMessage(hwndMain, WM_USER_Input, 0, (LPARAM) is);
5433       if (consoleEcho) {
5434         ConsoleOutput(is->buf, is->count, TRUE);
5435       } else if (appData.localLineEditing) {
5436         ConsoleOutput("\n", 1, TRUE);
5437       }
5438       /* fall thru */
5439     case '\033': /* Escape key */
5440       SetWindowText(hwnd, "");
5441       cf.cbSize = sizeof(CHARFORMAT);
5442       cf.dwMask = CFM_COLOR|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT;
5443       if (consoleEcho) {
5444         cf.crTextColor = textAttribs[ColorNormal].color;
5445       } else {
5446         cf.crTextColor = COLOR_ECHOOFF;
5447       }
5448       cf.dwEffects = textAttribs[ColorNormal].effects;
5449       SendMessage(hwnd, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
5450       return 0;
5451     case '\t':   /* Tab key */
5452       if (GetKeyState(VK_SHIFT) < 0) {
5453         /* shifted */
5454         SetFocus(GetDlgItem(hwndConsole, OPT_ConsoleText));
5455       } else {
5456         /* unshifted */
5457         if (IsIconic(hwndMain)) ShowWindow(hwndMain, SW_RESTORE);
5458         if (buttonDesc[0].hwnd) {
5459           SetFocus(buttonDesc[0].hwnd);
5460         } else {
5461           SetFocus(hwndMain);
5462         }
5463       }
5464       return 0;
5465     case '\023': /* Ctrl+S */
5466       sendNextChar = TRUE;
5467       return 0;
5468     case '\021': /* Ctrl+Q */
5469       quoteNextChar = TRUE;
5470       return 0;
5471     default:
5472       break;
5473     }
5474     break;
5475   case WM_KEYDOWN:
5476     switch (wParam) {
5477     case VK_UP:
5478       GetWindowText(hwnd, buf, MSG_SIZ);
5479       p = PrevInHistory(buf);
5480       if (p != NULL) {
5481         SetWindowText(hwnd, p);
5482         sel.cpMin = 999999;
5483         sel.cpMax = 999999;
5484         SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
5485         return 0;
5486       }
5487       break;
5488     case VK_DOWN:
5489       p = NextInHistory();
5490       if (p != NULL) {
5491         SetWindowText(hwnd, p);
5492         sel.cpMin = 999999;
5493         sel.cpMax = 999999;
5494         SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
5495         return 0;
5496       }
5497       break;
5498     case VK_HOME:
5499     case VK_END:
5500       if (!(GetKeyState(VK_CONTROL) & ~1)) break;
5501       /* fall thru */
5502     case VK_PRIOR:
5503     case VK_NEXT:
5504       SendDlgItemMessage(hwndConsole, OPT_ConsoleText, message, wParam, lParam);
5505       return 0;
5506     }
5507     break;
5508   case WM_MBUTTONDOWN:
5509     SendDlgItemMessage(hwndConsole, OPT_ConsoleText, 
5510       WM_COMMAND, MAKEWPARAM(IDM_QuickPaste, 0), 0);
5511     break;
5512   case WM_RBUTTONUP:
5513     if (GetKeyState(VK_SHIFT) & ~1) {
5514       SendDlgItemMessage(hwndConsole, OPT_ConsoleText, 
5515         WM_COMMAND, MAKEWPARAM(IDM_QuickPaste, 0), 0);
5516     } else {
5517       POINT pt;
5518       HMENU hmenu;
5519       hmenu = LoadMenu(hInst, "InputMenu");
5520       SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
5521       if (sel.cpMin == sel.cpMax) {
5522         EnableMenuItem(hmenu, IDM_Copy, MF_BYCOMMAND|MF_GRAYED);
5523         EnableMenuItem(hmenu, IDM_Cut, MF_BYCOMMAND|MF_GRAYED);
5524       }
5525       if (!IsClipboardFormatAvailable(CF_TEXT)) {
5526         EnableMenuItem(hmenu, IDM_Paste, MF_BYCOMMAND|MF_GRAYED);
5527       }
5528       pt.x = LOWORD(lParam);
5529       pt.y = HIWORD(lParam);
5530       MenuPopup(hwnd, pt, hmenu, -1);
5531     }
5532     return 0;
5533   case WM_COMMAND:
5534     switch (LOWORD(wParam)) { 
5535     case IDM_Undo:
5536       SendMessage(hwnd, EM_UNDO, 0, 0);
5537       return 0;
5538     case IDM_SelectAll:
5539       sel.cpMin = 0;
5540       sel.cpMax = -1; /*999999?*/
5541       SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&sel);
5542       return 0;
5543     case IDM_Cut:
5544       SendMessage(hwnd, WM_CUT, 0, 0);
5545       return 0;
5546     case IDM_Paste:
5547       SendMessage(hwnd, WM_PASTE, 0, 0);
5548       return 0;
5549     case IDM_Copy:
5550       SendMessage(hwnd, WM_COPY, 0, 0);
5551       return 0;
5552     }
5553     break;
5554   }
5555   return (*consoleInputWindowProc)(hwnd, message, wParam, lParam);
5556 }
5557
5558 #define CO_MAX  100000
5559 #define CO_TRIM   1000
5560
5561 LRESULT CALLBACK
5562 ConsoleWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
5563 {
5564   static HWND hText, hInput, hFocus;
5565   InputSource *is = consoleInputSource;
5566   RECT rect;
5567   static int sizeX, sizeY;
5568   int newSizeX, newSizeY;
5569   MINMAXINFO *mmi;
5570
5571   switch (message) {
5572   case WM_INITDIALOG: /* message: initialize dialog box */
5573     hwndConsole = hDlg;
5574     hText = GetDlgItem(hDlg, OPT_ConsoleText);
5575     hInput = GetDlgItem(hDlg, OPT_ConsoleInput);
5576     SetFocus(hInput);
5577     consoleTextWindowProc = (WNDPROC)
5578       SetWindowLong(hText, GWL_WNDPROC, (LONG) ConsoleTextSubclass);
5579     SendMessage(hText, EM_SETBKGNDCOLOR, FALSE, consoleBackgroundColor);
5580     consoleInputWindowProc = (WNDPROC)
5581       SetWindowLong(hInput, GWL_WNDPROC, (LONG) ConsoleInputSubclass);
5582     SendMessage(hInput, EM_SETBKGNDCOLOR, FALSE, consoleBackgroundColor);
5583     Colorize(ColorNormal, TRUE);
5584     SendMessage(hInput, EM_SETCHARFORMAT, SCF_ALL, (LPARAM) &consoleCF);
5585     ChangedConsoleFont();
5586     GetClientRect(hDlg, &rect);
5587     sizeX = rect.right;
5588     sizeY = rect.bottom;
5589     if (consoleX != CW_USEDEFAULT && consoleY != CW_USEDEFAULT &&
5590         consoleW != CW_USEDEFAULT && consoleH != CW_USEDEFAULT) {
5591       WINDOWPLACEMENT wp;
5592       EnsureOnScreen(&consoleX, &consoleY);
5593       wp.length = sizeof(WINDOWPLACEMENT);
5594       wp.flags = 0;
5595       wp.showCmd = SW_SHOW;
5596       wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
5597       wp.rcNormalPosition.left = consoleX;
5598       wp.rcNormalPosition.right = consoleX + consoleW;
5599       wp.rcNormalPosition.top = consoleY;
5600       wp.rcNormalPosition.bottom = consoleY + consoleH;
5601       SetWindowPlacement(hDlg, &wp);
5602     }
5603     return FALSE;
5604
5605   case WM_SETFOCUS:
5606     SetFocus(hInput);
5607     return 0;
5608
5609   case WM_CLOSE:
5610     ExitEvent(0);
5611     /* not reached */
5612     break;
5613
5614   case WM_SIZE:
5615     if (IsIconic(hDlg)) break;
5616     newSizeX = LOWORD(lParam);
5617     newSizeY = HIWORD(lParam);
5618     if (sizeX != newSizeX || sizeY != newSizeY) {
5619       RECT rectText, rectInput;
5620       POINT pt;
5621       int newTextHeight, newTextWidth;
5622       GetWindowRect(hText, &rectText);
5623       newTextWidth = rectText.right - rectText.left + newSizeX - sizeX;
5624       newTextHeight = rectText.bottom - rectText.top + newSizeY - sizeY;
5625       if (newTextHeight < 0) {
5626         newSizeY += -newTextHeight;
5627         newTextHeight = 0;
5628       }
5629       SetWindowPos(hText, NULL, 0, 0,
5630         newTextWidth, newTextHeight, SWP_NOZORDER|SWP_NOMOVE);
5631       GetWindowRect(hInput, &rectInput); /* gives screen coords */
5632       pt.x = rectInput.left;
5633       pt.y = rectInput.top + newSizeY - sizeY;
5634       ScreenToClient(hDlg, &pt);
5635       SetWindowPos(hInput, NULL, 
5636         pt.x, pt.y, /* needs client coords */   
5637         rectInput.right - rectInput.left + newSizeX - sizeX,
5638         rectInput.bottom - rectInput.top, SWP_NOZORDER);
5639     }
5640     sizeX = newSizeX;
5641     sizeY = newSizeY;
5642     break;
5643
5644   case WM_GETMINMAXINFO:
5645     /* Prevent resizing window too small */
5646     mmi = (MINMAXINFO *) lParam;
5647     mmi->ptMinTrackSize.x = 100;
5648     mmi->ptMinTrackSize.y = 100;
5649     break;
5650   }
5651   return DefWindowProc(hDlg, message, wParam, lParam);
5652 }
5653
5654
5655 VOID
5656 ConsoleCreate()
5657 {
5658   HWND hCons;
5659   if (hwndConsole) return;
5660   hCons = CreateDialog(hInst, szConsoleName, 0, NULL);
5661   SendMessage(hCons, WM_INITDIALOG, 0, 0);
5662 }
5663
5664
5665 VOID
5666 ConsoleOutput(char* data, int length, int forceVisible)
5667 {
5668   HWND hText;
5669   int trim, exlen;
5670   char *p, *q;
5671   char buf[CO_MAX+1];
5672   POINT pEnd;
5673   RECT rect;
5674   static int delayLF = 0;
5675   CHARRANGE savesel, sel;
5676
5677   if (hwndConsole == NULL || length > CO_MAX-100 || length == 0) return;
5678   p = data;
5679   q = buf;
5680   if (delayLF) {
5681     *q++ = '\r';
5682     *q++ = '\n';
5683     delayLF = 0;
5684   }
5685   while (length--) {
5686     if (*p == '\n') {
5687       if (*++p) {
5688         *q++ = '\r';
5689         *q++ = '\n';
5690       } else {
5691         delayLF = 1;
5692       }
5693     } else if (*p == '\007') {
5694        MyPlaySound(&sounds[(int)SoundBell]);
5695        p++;
5696     } else {
5697       *q++ = *p++;
5698     }
5699   }
5700   *q = NULLCHAR;
5701   hText = GetDlgItem(hwndConsole, OPT_ConsoleText);
5702   SendMessage(hText, EM_HIDESELECTION, TRUE, FALSE);
5703   /* Save current selection */
5704   SendMessage(hText, EM_EXGETSEL, 0, (LPARAM)&savesel);
5705   exlen = GetWindowTextLength(hText);
5706   /* Find out whether current end of text is visible */
5707   SendMessage(hText, EM_GETRECT, 0, (LPARAM) &rect);
5708   SendMessage(hText, EM_POSFROMCHAR, (WPARAM) &pEnd, exlen);
5709   /* Trim existing text if it's too long */
5710   if (exlen + (q - buf) > CO_MAX) {
5711     trim = (CO_TRIM > (q - buf)) ? CO_TRIM : (q - buf);
5712     sel.cpMin = 0;
5713     sel.cpMax = trim;
5714     SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&sel);
5715     SendMessage(hText, EM_REPLACESEL, 0, (LPARAM)"");
5716     exlen -= trim;
5717     savesel.cpMin -= trim;
5718     savesel.cpMax -= trim;
5719     if (exlen < 0) exlen = 0;
5720     if (savesel.cpMin < 0) savesel.cpMin = 0;
5721     if (savesel.cpMax < savesel.cpMin) savesel.cpMax = savesel.cpMin;
5722   }
5723   /* Append the new text */
5724   sel.cpMin = exlen;
5725   sel.cpMax = exlen;
5726   SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&sel);
5727   SendMessage(hText, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&consoleCF);
5728   SendMessage(hText, EM_REPLACESEL, 0, (LPARAM) buf);
5729   if (forceVisible || exlen == 0 ||
5730       (rect.left <= pEnd.x && pEnd.x < rect.right &&
5731        rect.top <= pEnd.y && pEnd.y < rect.bottom)) {
5732     /* Scroll to make new end of text visible if old end of text
5733        was visible or new text is an echo of user typein */
5734     sel.cpMin = 9999999;
5735     sel.cpMax = 9999999;
5736     SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&sel);
5737     SendMessage(hText, EM_HIDESELECTION, FALSE, FALSE);
5738     SendMessage(hText, EM_SCROLLCARET, 0, 0);
5739     SendMessage(hText, EM_HIDESELECTION, TRUE, FALSE);
5740   }
5741   if (savesel.cpMax == exlen || forceVisible) {
5742     /* Move insert point to new end of text if it was at the old
5743        end of text or if the new text is an echo of user typein */
5744     sel.cpMin = 9999999;
5745     sel.cpMax = 9999999;
5746     SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&sel);
5747   } else {
5748     /* Restore previous selection */
5749     SendMessage(hText, EM_EXSETSEL, 0, (LPARAM)&savesel);
5750   }
5751   SendMessage(hText, EM_HIDESELECTION, FALSE, FALSE);
5752 }
5753
5754 /*---------*/
5755
5756
5757 void
5758 DisplayAClock(HDC hdc, int timeRemaining, int highlight,
5759               RECT *rect, char *color)
5760 {
5761   char buf[100];
5762   char *str;
5763   COLORREF oldFg, oldBg;
5764   HFONT oldFont;
5765
5766   if (appData.clockMode) {
5767     if (tinyLayout)
5768       sprintf(buf, "%c %s", color[0], TimeString(timeRemaining));
5769     else
5770       sprintf(buf, "%s: %s", color, TimeString(timeRemaining));
5771     str = buf;
5772   } else {
5773     str = color;
5774   }
5775
5776   if (highlight) {
5777     oldFg = SetTextColor(hdc, RGB(255, 255, 255)); /* white */
5778     oldBg = SetBkColor(hdc, RGB(0, 0, 0)); /* black */
5779   } else {
5780     oldFg = SetTextColor(hdc, RGB(0, 0, 0)); /* black */
5781     oldBg = SetBkColor(hdc, RGB(255, 255, 255)); /* white */
5782   }
5783   oldFont = SelectObject(hdc, font[boardSize][CLOCK_FONT]->hf);
5784
5785   ExtTextOut(hdc, rect->left + MESSAGE_LINE_LEFTMARGIN,
5786              rect->top, ETO_CLIPPED|ETO_OPAQUE,
5787              rect, str, strlen(str), NULL);
5788
5789   (void) SetTextColor(hdc, oldFg);
5790   (void) SetBkColor(hdc, oldBg);
5791   (void) SelectObject(hdc, oldFont);
5792 }
5793
5794
5795 int
5796 DoReadFile(HANDLE hFile, char *buf, int count, DWORD *outCount,
5797            OVERLAPPED *ovl)
5798 {
5799   int ok, err;
5800
5801   ResetEvent(ovl->hEvent);
5802   ovl->Offset = ovl->OffsetHigh = 0;
5803   ok = ReadFile(hFile, buf, count, outCount, ovl);
5804   if (ok) {
5805     err = NO_ERROR;
5806   } else {
5807     err = GetLastError();
5808     if (err == ERROR_IO_PENDING) {
5809       ok = GetOverlappedResult(hFile, ovl, outCount, TRUE);
5810       if (ok)
5811         err = NO_ERROR;
5812       else
5813         err = GetLastError();
5814     }
5815   }
5816   return err;
5817 }
5818
5819 int
5820 DoWriteFile(HANDLE hFile, char *buf, int count, DWORD *outCount,
5821             OVERLAPPED *ovl)
5822 {
5823   int ok, err;
5824
5825   ResetEvent(ovl->hEvent);
5826   ovl->Offset = ovl->OffsetHigh = 0;
5827   ok = WriteFile(hFile, buf, count, outCount, ovl);
5828   if (ok) {
5829     err = NO_ERROR;
5830   } else {
5831     err = GetLastError();
5832     if (err == ERROR_IO_PENDING) {
5833       ok = GetOverlappedResult(hFile, ovl, outCount, TRUE);
5834       if (ok)
5835         err = NO_ERROR;
5836       else
5837         err = GetLastError();
5838     }
5839   }
5840   return err;
5841 }
5842
5843
5844 DWORD
5845 InputThread(LPVOID arg)
5846 {
5847   InputSource *is;
5848   OVERLAPPED ovl;
5849
5850   is = (InputSource *) arg;
5851   ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
5852   ovl.Internal = ovl.InternalHigh = ovl.Offset = ovl.OffsetHigh = 0;
5853   while (is->hThread != NULL) {
5854     is->error = DoReadFile(is->hFile, is->next,
5855                            INPUT_SOURCE_BUF_SIZE - (is->next - is->buf),
5856                            &is->count, &ovl);
5857     if (is->error == NO_ERROR) {
5858       is->next += is->count;
5859     } else {
5860       if (is->error == ERROR_BROKEN_PIPE) {
5861         /* Correct for MS brain damage.  EOF reading a pipe is not an error. */
5862         is->count = 0;
5863       } else {
5864         is->count = (DWORD) -1;
5865       }
5866     }
5867     SendMessage(hwndMain, WM_USER_Input, 0, (LPARAM) is);
5868     if (is->count <= 0) break;  /* Quit on EOF or error */
5869   }
5870   CloseHandle(ovl.hEvent);
5871   CloseHandle(is->hFile);
5872   return 0;
5873 }
5874
5875
5876 /* Windows 95 beta 2 won't let you do overlapped i/o on a console or pipe */
5877 DWORD
5878 NonOvlInputThread(LPVOID arg)
5879 {
5880   InputSource *is;
5881   char *p, *q;
5882   int i;
5883   char prev;
5884
5885   is = (InputSource *) arg;
5886   while (is->hThread != NULL) {
5887     is->error = ReadFile(is->hFile, is->next,
5888                          INPUT_SOURCE_BUF_SIZE - (is->next - is->buf),
5889                          &is->count, NULL) ? NO_ERROR : GetLastError();
5890     if (is->error == NO_ERROR) {
5891       /* Change CRLF to LF */
5892       if (is->next > is->buf) {
5893         p = is->next - 1;
5894         i = is->count + 1;
5895       } else {
5896         p = is->next;
5897         i = is->count;
5898       }
5899       q = p;
5900       prev = NULLCHAR;
5901       while (i > 0) {
5902         if (prev == '\r' && *p == '\n') {
5903           *(q-1) = '\n';
5904           is->count--;
5905         } else { 
5906           *q++ = *p;
5907         }
5908         prev = *p++;
5909         i--;
5910       }
5911       *q = NULLCHAR;
5912       is->next = q;
5913     } else {
5914       if (is->error == ERROR_BROKEN_PIPE) {
5915         /* Correct for MS brain damage.  EOF reading a pipe is not an error. */
5916         is->count = 0; 
5917       } else {
5918         is->count = (DWORD) -1;
5919       }
5920     }
5921     SendMessage(hwndMain, WM_USER_Input, 0, (LPARAM) is);
5922     if (is->count < 0) break;  /* Quit on error */
5923   }
5924   CloseHandle(is->hFile);
5925   return 0;
5926 }
5927
5928 DWORD
5929 SocketInputThread(LPVOID arg)
5930 {
5931   InputSource *is;
5932
5933   is = (InputSource *) arg;
5934   while (is->hThread != NULL) {
5935     is->count = recv(is->sock, is->buf, INPUT_SOURCE_BUF_SIZE, 0);
5936     if ((int)is->count == SOCKET_ERROR) {
5937       is->count = (DWORD) -1;
5938       is->error = WSAGetLastError();
5939     } else {
5940       is->error = NO_ERROR;
5941       is->next += is->count;
5942       if (is->count == 0 && is->second == is) {
5943         /* End of file on stderr; quit with no message */
5944         break;
5945       }
5946     }
5947     SendMessage(hwndMain, WM_USER_Input, 0, (LPARAM) is);
5948     if (is->count <= 0) break;  /* Quit on EOF or error */
5949   }
5950   return 0;
5951 }
5952
5953 VOID
5954 InputEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
5955 {
5956   InputSource *is;
5957
5958   is = (InputSource *) lParam;
5959   if (is->lineByLine) {
5960     /* Feed in lines one by one */
5961     char *p = is->buf;
5962     char *q = p;
5963     while (q < is->next) {
5964       if (*q++ == '\n') {
5965         (is->func)(is, is->closure, p, q - p, NO_ERROR);
5966         p = q;
5967       }
5968     }
5969     /* Move any partial line to the start of the buffer */
5970     q = is->buf;
5971     while (p < is->next) {
5972       *q++ = *p++;
5973     }
5974     is->next = q;
5975     if (is->error != NO_ERROR || is->count == 0) {
5976       /* Notify backend of the error.  Note: If there was a partial
5977          line at the end, it is not flushed through. */
5978       (is->func)(is, is->closure, is->buf, is->count, is->error);   
5979     }
5980   } else {
5981     /* Feed in the whole chunk of input at once */
5982     (is->func)(is, is->closure, is->buf, is->count, is->error);
5983     is->next = is->buf;
5984   }
5985 }
5986
5987 /*---------------------------------------------------------------------------*\
5988  *
5989  *  Menu enables. Used when setting various modes.
5990  *
5991 \*---------------------------------------------------------------------------*/
5992
5993 typedef struct {
5994   int item;
5995   int flags;
5996 } Enables;
5997
5998 VOID
5999 SetMenuEnables(HMENU hmenu, Enables *enab)
6000 {
6001   while (enab->item > 0) {
6002     (void) EnableMenuItem(hmenu, enab->item, enab->flags);
6003     enab++;
6004   }
6005 }
6006
6007 Enables gnuEnables[] = {
6008   { IDM_MailMove, MF_BYCOMMAND|MF_GRAYED },
6009   { IDM_ReloadCMailMsg, MF_BYCOMMAND|MF_GRAYED },
6010   { IDM_IcsClient, MF_BYCOMMAND|MF_GRAYED },
6011   { IDM_Accept, MF_BYCOMMAND|MF_GRAYED },
6012   { IDM_Decline, MF_BYCOMMAND|MF_GRAYED },
6013   { IDM_Rematch, MF_BYCOMMAND|MF_GRAYED },
6014   { IDM_Adjourn, MF_BYCOMMAND|MF_GRAYED },
6015   { IDM_StopExamining, MF_BYCOMMAND|MF_GRAYED },
6016   { IDM_StopObserving, MF_BYCOMMAND|MF_GRAYED },
6017   { IDM_Revert, MF_BYCOMMAND|MF_GRAYED },
6018   { -1, -1 }
6019 };
6020
6021 Enables icsEnables[] = {
6022   { IDM_MailMove, MF_BYCOMMAND|MF_GRAYED },
6023   { IDM_ReloadCMailMsg, MF_BYCOMMAND|MF_GRAYED },
6024   { IDM_MachineWhite, MF_BYCOMMAND|MF_GRAYED },
6025   { IDM_MachineBlack, MF_BYCOMMAND|MF_GRAYED },
6026   { IDM_TwoMachines, MF_BYCOMMAND|MF_GRAYED },
6027   { IDM_AnalysisMode, MF_BYCOMMAND|MF_GRAYED },
6028   { IDM_AnalyzeFile, MF_BYCOMMAND|MF_GRAYED },
6029   { IDM_TimeControl, MF_BYCOMMAND|MF_GRAYED },
6030   { IDM_MoveNow, MF_BYCOMMAND|MF_GRAYED },
6031   { IDM_Hint, MF_BYCOMMAND|MF_GRAYED },
6032   { IDM_Book, MF_BYCOMMAND|MF_GRAYED },
6033   { IDM_IcsOptions, MF_BYCOMMAND|MF_ENABLED },
6034   { -1, -1 }
6035 };
6036
6037 #ifdef ZIPPY
6038 Enables zippyEnables[] = {
6039   { IDM_MoveNow, MF_BYCOMMAND|MF_ENABLED },
6040   { IDM_Hint, MF_BYCOMMAND|MF_ENABLED },
6041   { IDM_Book, MF_BYCOMMAND|MF_ENABLED },
6042   { -1, -1 }
6043 };
6044 #endif
6045
6046 Enables ncpEnables[] = {
6047   { IDM_MailMove, MF_BYCOMMAND|MF_GRAYED },
6048   { IDM_ReloadCMailMsg, MF_BYCOMMAND|MF_GRAYED },
6049   { IDM_MachineWhite, MF_BYCOMMAND|MF_GRAYED },
6050   { IDM_MachineBlack, MF_BYCOMMAND|MF_GRAYED },
6051   { IDM_TwoMachines, MF_BYCOMMAND|MF_GRAYED },
6052   { IDM_AnalysisMode, MF_BYCOMMAND|MF_GRAYED },
6053   { IDM_AnalyzeFile, MF_BYCOMMAND|MF_GRAYED },
6054   { IDM_IcsClient, MF_BYCOMMAND|MF_GRAYED },
6055   { ACTION_POS, MF_BYPOSITION|MF_GRAYED },
6056   { IDM_Revert, MF_BYCOMMAND|MF_GRAYED },
6057   { IDM_MoveNow, MF_BYCOMMAND|MF_GRAYED },
6058   { IDM_RetractMove, MF_BYCOMMAND|MF_GRAYED },
6059   { IDM_TimeControl, MF_BYCOMMAND|MF_GRAYED },
6060   { IDM_Hint, MF_BYCOMMAND|MF_GRAYED },
6061   { IDM_Book, MF_BYCOMMAND|MF_GRAYED },
6062   { -1, -1 }
6063 };
6064
6065 Enables trainingOnEnables[] = {
6066   { IDM_EditComment, MF_BYCOMMAND|MF_GRAYED },
6067   { IDM_Pause, MF_BYCOMMAND|MF_GRAYED },
6068   { IDM_Forward, MF_BYCOMMAND|MF_GRAYED },
6069   { IDM_Backward, MF_BYCOMMAND|MF_GRAYED },
6070   { IDM_ToEnd, MF_BYCOMMAND|MF_GRAYED },
6071   { IDM_ToStart, MF_BYCOMMAND|MF_GRAYED },
6072   { IDM_MoveNow, MF_BYCOMMAND|MF_GRAYED },
6073   { IDM_TruncateGame, MF_BYCOMMAND|MF_GRAYED },
6074   { -1, -1 }
6075 };
6076
6077 Enables trainingOffEnables[] = {
6078   { IDM_EditComment, MF_BYCOMMAND|MF_ENABLED },
6079   { IDM_Pause, MF_BYCOMMAND|MF_ENABLED },
6080   { IDM_Forward, MF_BYCOMMAND|MF_ENABLED },
6081   { IDM_Backward, MF_BYCOMMAND|MF_ENABLED },
6082   { IDM_ToEnd, MF_BYCOMMAND|MF_ENABLED },
6083   { IDM_ToStart, MF_BYCOMMAND|MF_ENABLED },
6084   { IDM_MoveNow, MF_BYCOMMAND|MF_ENABLED },
6085   { IDM_TruncateGame, MF_BYCOMMAND|MF_ENABLED },
6086   { -1, -1 }
6087 };
6088
6089 /* These modify either ncpEnables or gnuEnables */
6090 Enables cmailEnables[] = {
6091   { IDM_MailMove, MF_BYCOMMAND|MF_ENABLED },
6092   { IDM_ReloadCMailMsg, MF_BYCOMMAND|MF_ENABLED },
6093   { ACTION_POS, MF_BYPOSITION|MF_ENABLED },
6094   { IDM_CallFlag, MF_BYCOMMAND|MF_GRAYED },
6095   { IDM_Draw, MF_BYCOMMAND|MF_ENABLED },
6096   { IDM_Adjourn, MF_BYCOMMAND|MF_GRAYED },
6097   { IDM_Abort, MF_BYCOMMAND|MF_GRAYED },
6098   { -1, -1 }
6099 };
6100
6101 Enables machineThinkingEnables[] = {
6102   { IDM_LoadGame, MF_BYCOMMAND|MF_GRAYED },
6103   { IDM_LoadNextGame, MF_BYCOMMAND|MF_GRAYED },
6104   { IDM_LoadPrevGame, MF_BYCOMMAND|MF_GRAYED },
6105   { IDM_ReloadGame, MF_BYCOMMAND|MF_GRAYED },
6106   { IDM_PasteGame, MF_BYCOMMAND|MF_GRAYED },
6107   { IDM_LoadPosition, MF_BYCOMMAND|MF_GRAYED },
6108   { IDM_LoadNextPosition, MF_BYCOMMAND|MF_GRAYED },
6109   { IDM_LoadPrevPosition, MF_BYCOMMAND|MF_GRAYED },
6110   { IDM_ReloadPosition, MF_BYCOMMAND|MF_GRAYED },
6111   { IDM_PastePosition, MF_BYCOMMAND|MF_GRAYED },
6112   { IDM_MachineWhite, MF_BYCOMMAND|MF_GRAYED },
6113   { IDM_MachineBlack, MF_BYCOMMAND|MF_GRAYED },
6114   { IDM_TwoMachines, MF_BYCOMMAND|MF_GRAYED },
6115   { IDM_TypeInMove, MF_BYCOMMAND|MF_GRAYED },
6116   { IDM_RetractMove, MF_BYCOMMAND|MF_GRAYED },
6117   { -1, -1 }
6118 };
6119
6120 Enables userThinkingEnables[] = {
6121   { IDM_LoadGame, MF_BYCOMMAND|MF_ENABLED },
6122   { IDM_LoadNextGame, MF_BYCOMMAND|MF_ENABLED },
6123   { IDM_LoadPrevGame, MF_BYCOMMAND|MF_ENABLED },
6124   { IDM_ReloadGame, MF_BYCOMMAND|MF_ENABLED },
6125   { IDM_PasteGame, MF_BYCOMMAND|MF_ENABLED },
6126   { IDM_LoadPosition, MF_BYCOMMAND|MF_ENABLED },
6127   { IDM_LoadNextPosition, MF_BYCOMMAND|MF_ENABLED },
6128   { IDM_LoadPrevPosition, MF_BYCOMMAND|MF_ENABLED },
6129   { IDM_ReloadPosition, MF_BYCOMMAND|MF_ENABLED },
6130   { IDM_PastePosition, MF_BYCOMMAND|MF_ENABLED },
6131   { IDM_MachineWhite, MF_BYCOMMAND|MF_ENABLED },
6132   { IDM_MachineBlack, MF_BYCOMMAND|MF_ENABLED },
6133   { IDM_TwoMachines, MF_BYCOMMAND|MF_ENABLED },
6134   { IDM_TypeInMove, MF_BYCOMMAND|MF_ENABLED },
6135   { IDM_RetractMove, MF_BYCOMMAND|MF_ENABLED },
6136   { -1, -1 }
6137 };
6138
6139 /*---------------------------------------------------------------------------*\
6140  *
6141  *  Front-end interface functions exported by XBoard.
6142  *  Functions appear in same order as prototypes in frontend.h.
6143  * 
6144 \*---------------------------------------------------------------------------*/
6145 VOID
6146 ModeHighlight()
6147 {
6148   static UINT prevChecked = 0;
6149   static int prevPausing = 0;
6150   UINT nowChecked;
6151
6152   if (pausing != prevPausing) {
6153     prevPausing = pausing;
6154     (void) CheckMenuItem(GetMenu(hwndMain), IDM_Pause,
6155                          MF_BYCOMMAND|(pausing ? MF_CHECKED : MF_UNCHECKED));
6156     if (hwndPause) SetWindowText(hwndPause, pausing ? "C" : "P");
6157   }
6158
6159   switch (gameMode) {
6160   case BeginningOfGame:
6161     if (appData.icsActive)
6162       nowChecked = IDM_IcsClient;
6163     else if (appData.noChessProgram)
6164       nowChecked = IDM_EditGame;
6165     else
6166       nowChecked = IDM_MachineBlack;
6167     break;
6168   case MachinePlaysBlack:
6169     nowChecked = IDM_MachineBlack;
6170     break;
6171   case MachinePlaysWhite:
6172     nowChecked = IDM_MachineWhite;
6173     break;
6174   case TwoMachinesPlay:
6175     nowChecked = IDM_TwoMachines;
6176     break;
6177   case AnalyzeMode:
6178     nowChecked = IDM_AnalysisMode;
6179     break;
6180   case AnalyzeFile:
6181     nowChecked = IDM_AnalyzeFile;
6182     break;
6183   case EditGame:
6184     nowChecked = IDM_EditGame;
6185     break;
6186   case PlayFromGameFile:
6187     nowChecked = IDM_LoadGame;
6188     break;
6189   case EditPosition:
6190     nowChecked = IDM_EditPosition;
6191     break;
6192   case Training:
6193     nowChecked = IDM_Training;
6194     break;
6195   case IcsPlayingWhite:
6196   case IcsPlayingBlack:
6197   case IcsObserving:
6198   case IcsIdle:
6199     nowChecked = IDM_IcsClient;
6200     break;
6201   default:
6202   case EndOfGame:
6203     nowChecked = 0;
6204     break;
6205   }
6206   if (prevChecked != 0)
6207     (void) CheckMenuItem(GetMenu(hwndMain),
6208                          prevChecked, MF_BYCOMMAND|MF_UNCHECKED);
6209   if (nowChecked != 0)
6210     (void) CheckMenuItem(GetMenu(hwndMain),
6211                          nowChecked, MF_BYCOMMAND|MF_CHECKED);
6212
6213   if (nowChecked == IDM_LoadGame || nowChecked == IDM_Training) {
6214     (void) EnableMenuItem(GetMenu(hwndMain), IDM_Training, 
6215                           MF_BYCOMMAND|MF_ENABLED);
6216   } else {
6217     (void) EnableMenuItem(GetMenu(hwndMain), 
6218                           IDM_Training, MF_BYCOMMAND|MF_GRAYED);
6219   }
6220
6221   prevChecked = nowChecked;
6222 }
6223
6224 VOID
6225 SetICSMode()
6226 {
6227   HMENU hmenu = GetMenu(hwndMain);
6228   SetMenuEnables(hmenu, icsEnables);
6229   EnableMenuItem(GetSubMenu(hmenu, OPTIONS_POS), ICS_POS,
6230     MF_BYPOSITION|MF_ENABLED);
6231 #ifdef ZIPPY
6232   if (appData.zippyPlay) {
6233     SetMenuEnables(hmenu, zippyEnables);
6234   }
6235 #endif
6236 }
6237
6238 VOID
6239 SetGNUMode()
6240 {
6241   SetMenuEnables(GetMenu(hwndMain), gnuEnables);
6242 }
6243
6244 VOID
6245 SetNCPMode()
6246 {
6247   HMENU hmenu = GetMenu(hwndMain);
6248   SetMenuEnables(hmenu, ncpEnables);
6249   EnableMenuItem(GetSubMenu(hmenu, OPTIONS_POS), SOUNDS_POS,
6250     MF_BYPOSITION|MF_GRAYED);
6251     DrawMenuBar(hwndMain);
6252 }
6253
6254 VOID
6255 SetCmailMode()
6256 {
6257   SetMenuEnables(GetMenu(hwndMain), cmailEnables);
6258 }
6259
6260 VOID 
6261 SetTrainingModeOn()
6262 {
6263   int i;
6264   SetMenuEnables(GetMenu(hwndMain), trainingOnEnables);
6265   for (i = 0; i < N_BUTTONS; i++) {
6266     if (buttonDesc[i].hwnd != NULL)
6267       EnableWindow(buttonDesc[i].hwnd, FALSE);
6268   }
6269   CommentPopDown();
6270 }
6271
6272 VOID SetTrainingModeOff()
6273 {
6274   int i;
6275   SetMenuEnables(GetMenu(hwndMain), trainingOffEnables);
6276   for (i = 0; i < N_BUTTONS; i++) {
6277     if (buttonDesc[i].hwnd != NULL)
6278       EnableWindow(buttonDesc[i].hwnd, TRUE);
6279   }
6280 }
6281
6282
6283 VOID
6284 SetUserThinkingEnables()
6285 {
6286   SetMenuEnables(GetMenu(hwndMain), userThinkingEnables);
6287 }
6288
6289 VOID
6290 SetMachineThinkingEnables()
6291 {
6292   HMENU hMenu = GetMenu(hwndMain);
6293   int flags = MF_BYCOMMAND|MF_ENABLED;
6294
6295   SetMenuEnables(hMenu, machineThinkingEnables);
6296
6297   if (gameMode == MachinePlaysBlack) {
6298     (void)EnableMenuItem(hMenu, IDM_MachineBlack, flags);
6299   } else if (gameMode == MachinePlaysWhite) {
6300     (void)EnableMenuItem(hMenu, IDM_MachineWhite, flags);
6301   } else if (gameMode == TwoMachinesPlay) {
6302     (void)EnableMenuItem(hMenu, IDM_TwoMachines, flags);
6303   }
6304 }
6305
6306
6307 VOID
6308 DisplayTitle(char *str)
6309 {
6310   char title[MSG_SIZ], *host;
6311   if (str[0] != NULLCHAR) {
6312     strcpy(title, str);
6313   } else if (appData.icsActive) {
6314     if (appData.icsCommPort[0] != NULLCHAR)
6315       host = "ICS";
6316     else 
6317       host = appData.icsHost;
6318     sprintf(title, "%s: %s", szTitle, host);
6319   } else if (appData.noChessProgram) {
6320     strcpy(title, szTitle);
6321   } else {
6322     strcpy(title, szTitle);
6323     strcat(title, ": ");
6324     strcat(title, first.tidy);
6325   }
6326   SetWindowText(hwndMain, title);
6327 }
6328
6329
6330 VOID
6331 DisplayMessage(char *str1, char *str2)
6332 {
6333   HDC hdc;
6334   HFONT oldFont;
6335   int remain = MESSAGE_TEXT_MAX - 1;
6336   int len;
6337
6338   moveErrorMessageUp = FALSE; /* turned on later by caller if needed */
6339   messageText[0] = NULLCHAR;
6340   if (*str1) {
6341     len = strlen(str1);
6342     if (len > remain) len = remain;
6343     strncpy(messageText, str1, len);
6344     messageText[len] = NULLCHAR;
6345     remain -= len;
6346   }
6347   if (*str2 && remain >= 2) {
6348     if (*str1) {
6349       strcat(messageText, "  ");
6350       remain -= 2;
6351     }
6352     len = strlen(str2);
6353     if (len > remain) len = remain;
6354     strncat(messageText, str2, len);
6355   }
6356   messageText[MESSAGE_TEXT_MAX - 1] = NULLCHAR;
6357
6358   if (IsIconic(hwndMain)) return;
6359   hdc = GetDC(hwndMain);
6360   oldFont = SelectObject(hdc, font[boardSize][MESSAGE_FONT]->hf);
6361   ExtTextOut(hdc, messageRect.left, messageRect.top, ETO_CLIPPED|ETO_OPAQUE,
6362              &messageRect, messageText, strlen(messageText), NULL);
6363   (void) SelectObject(hdc, oldFont);
6364   (void) ReleaseDC(hwndMain, hdc);
6365 }
6366
6367 VOID
6368 DisplayError(char *str, int error)
6369 {
6370   char buf[MSG_SIZ*2], buf2[MSG_SIZ];
6371   int len;
6372  
6373   if (error == 0) {
6374     strcpy(buf, str);
6375   } else {
6376     len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
6377                         NULL, error, LANG_NEUTRAL,
6378                         (LPSTR) buf2, MSG_SIZ, NULL);
6379     if (len > 0) {
6380       sprintf(buf, "%s:\n%s", str, buf2);
6381     } else {
6382       ErrorMap *em = errmap;
6383       while (em->err != 0 && em->err != error) em++;
6384       if (em->err != 0) {
6385         sprintf(buf, "%s:\n%s", str, em->msg);
6386       } else {
6387         sprintf(buf, "%s:\nError code %d", str, error);
6388       }
6389     }
6390   }
6391   
6392   ErrorPopUp("Error", buf);
6393 }
6394
6395
6396 VOID
6397 DisplayMoveError(char *str)
6398 {
6399   fromX = fromY = -1;
6400   ClearHighlights();
6401   DrawPosition(FALSE, NULL);
6402   if (appData.popupMoveErrors) {
6403     ErrorPopUp("Error", str);
6404   } else {
6405     DisplayMessage(str, "");
6406     moveErrorMessageUp = TRUE;
6407   }
6408 }
6409
6410 VOID
6411 DisplayFatalError(char *str, int error, int exitStatus)
6412 {
6413   char buf[2*MSG_SIZ], buf2[MSG_SIZ];
6414   int len;
6415   char *label = exitStatus ? "Fatal Error" : "Exiting";
6416
6417   if (error != 0) {
6418     len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
6419                         NULL, error, LANG_NEUTRAL,
6420                         (LPSTR) buf2, MSG_SIZ, NULL);
6421     if (len > 0) {
6422       sprintf(buf, "%s:\n%s", str, buf2);
6423     } else {
6424       ErrorMap *em = errmap;
6425       while (em->err != 0 && em->err != error) em++;
6426       if (em->err != 0) {
6427         sprintf(buf, "%s:\n%s", str, em->msg);
6428       } else {
6429         sprintf(buf, "%s:\nError code %d", str, error);
6430       }
6431     }
6432     str = buf;
6433   }
6434   if (appData.debugMode) {
6435     fprintf(debugFP, "%s: %s\n", label, str);
6436   }
6437   if (appData.popupExitMessage) {
6438     (void) MessageBox(hwndMain, str, label, MB_OK|
6439                       (exitStatus ? MB_ICONSTOP : MB_ICONINFORMATION));
6440   }
6441   ExitEvent(exitStatus);
6442 }
6443
6444
6445 VOID
6446 DisplayInformation(char *str)
6447 {
6448   (void) MessageBox(hwndMain, str, "Information", MB_OK|MB_ICONINFORMATION);
6449 }
6450
6451
6452 VOID
6453 DisplayNote(char *str)
6454 {
6455   ErrorPopUp("Note", str);
6456 }
6457
6458
6459 typedef struct {
6460   char *title, *question, *replyPrefix;
6461   ProcRef pr;
6462 } QuestionParams;
6463
6464 LRESULT CALLBACK
6465 QuestionDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
6466 {
6467   static QuestionParams *qp;
6468   char reply[MSG_SIZ];
6469   int len, err;
6470
6471   switch (message) {
6472   case WM_INITDIALOG:
6473     qp = (QuestionParams *) lParam;
6474     CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));
6475     SetWindowText(hDlg, qp->title);
6476     SetDlgItemText(hDlg, OPT_QuestionText, qp->question);
6477     SetFocus(GetDlgItem(hDlg, OPT_QuestionInput));
6478     return FALSE;
6479
6480   case WM_COMMAND:
6481     switch (LOWORD(wParam)) {
6482     case IDOK:
6483       strcpy(reply, qp->replyPrefix);
6484       if (*reply) strcat(reply, " ");
6485       len = strlen(reply);
6486       GetDlgItemText(hDlg, OPT_QuestionInput, reply + len, sizeof(reply) - len);
6487       strcat(reply, "\n");
6488       OutputToProcess(qp->pr, reply, strlen(reply), &err);
6489       EndDialog(hDlg, TRUE);
6490       if (err) DisplayFatalError("Error writing to chess program", err, 1);
6491       return TRUE;
6492     case IDCANCEL:
6493       EndDialog(hDlg, FALSE);
6494       return TRUE;
6495     default:
6496       break;
6497     }
6498     break;
6499   }
6500   return FALSE;
6501 }
6502
6503 VOID
6504 AskQuestion(char* title, char *question, char *replyPrefix, ProcRef pr)
6505 {
6506     QuestionParams qp;
6507     FARPROC lpProc;
6508     
6509     qp.title = title;
6510     qp.question = question;
6511     qp.replyPrefix = replyPrefix;
6512     qp.pr = pr;
6513     lpProc = MakeProcInstance((FARPROC)QuestionDialog, hInst);
6514     DialogBoxParam(hInst, MAKEINTRESOURCE(DLG_Question),
6515       hwndMain, (DLGPROC)lpProc, (LPARAM)&qp);
6516     FreeProcInstance(lpProc);
6517 }
6518
6519
6520 VOID
6521 DisplayIcsInteractionTitle(char *str)
6522 {
6523   char consoleTitle[MSG_SIZ];
6524
6525   sprintf(consoleTitle, "%s: %s", szConsoleTitle, str);
6526   SetWindowText(hwndConsole, consoleTitle);
6527 }
6528
6529 void
6530 DrawPosition(int fullRedraw, Board board)
6531 {
6532   HDCDrawPosition(NULL, (BOOLEAN) fullRedraw, board); 
6533 }
6534
6535
6536 VOID
6537 ResetFrontEnd()
6538 {
6539   fromX = fromY = -1;
6540   if (dragInfo.pos.x != -1 || dragInfo.pos.y != -1) {
6541     dragInfo.pos.x = dragInfo.pos.y = -1;
6542     dragInfo.pos.x = dragInfo.pos.y = -1;
6543     dragInfo.lastpos = dragInfo.pos;
6544     dragInfo.start.x = dragInfo.start.y = -1;
6545     dragInfo.from = dragInfo.start;
6546     ReleaseCapture();
6547     DrawPosition(TRUE, NULL);
6548   }
6549 }
6550
6551
6552 VOID
6553 CommentPopUp(char *title, char *str)
6554 {
6555   HWND hwnd = GetActiveWindow();
6556   EitherCommentPopUp(0, title, str, FALSE);
6557   SetActiveWindow(hwnd);
6558 }
6559
6560 VOID
6561 CommentPopDown(void)
6562 {
6563   CheckMenuItem(GetMenu(hwndMain), IDM_EditComment, MF_UNCHECKED);
6564   if (commentDialog) {
6565     ShowWindow(commentDialog, SW_HIDE);
6566   }
6567   commentDialogUp = FALSE;
6568 }
6569
6570 VOID
6571 EditCommentPopUp(int index, char *title, char *str)
6572 {
6573   EitherCommentPopUp(index, title, str, TRUE);
6574 }
6575
6576
6577 VOID
6578 RingBell()
6579 {
6580   MyPlaySound(&sounds[(int)SoundMove]);
6581 }
6582
6583 VOID PlayIcsWinSound()
6584 {
6585   MyPlaySound(&sounds[(int)SoundIcsWin]);
6586 }
6587
6588 VOID PlayIcsLossSound()
6589 {
6590   MyPlaySound(&sounds[(int)SoundIcsLoss]);
6591 }
6592
6593 VOID PlayIcsDrawSound()
6594 {
6595   MyPlaySound(&sounds[(int)SoundIcsDraw]);
6596 }
6597
6598 VOID PlayIcsUnfinishedSound()
6599 {
6600   MyPlaySound(&sounds[(int)SoundIcsUnfinished]);
6601 }
6602
6603 VOID
6604 PlayAlarmSound()
6605 {
6606   MyPlaySound(&sounds[(int)SoundAlarm]);
6607 }
6608
6609
6610 VOID
6611 EchoOn()
6612 {
6613   HWND hInput;
6614   consoleEcho = TRUE;
6615   hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
6616   SendMessage(hInput, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&consoleCF);
6617   SendMessage(hInput, EM_SETBKGNDCOLOR, FALSE, consoleBackgroundColor);
6618 }
6619
6620
6621 VOID
6622 EchoOff()
6623 {
6624   CHARFORMAT cf;
6625   HWND hInput;
6626   consoleEcho = FALSE;
6627   hInput = GetDlgItem(hwndConsole, OPT_ConsoleInput);
6628   /* This works OK: set text and background both to the same color */
6629   cf = consoleCF;
6630   cf.crTextColor = COLOR_ECHOOFF;
6631   SendMessage(hInput, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
6632   SendMessage(hInput, EM_SETBKGNDCOLOR, FALSE, cf.crTextColor);
6633 }
6634
6635 /* No Raw()...? */
6636
6637 void Colorize(ColorClass cc, int continuation)
6638 {
6639   currentColorClass = cc;
6640   consoleCF.dwMask = CFM_COLOR|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT;
6641   consoleCF.crTextColor = textAttribs[cc].color;
6642   consoleCF.dwEffects = textAttribs[cc].effects;
6643   if (!continuation) MyPlaySound(&textAttribs[cc].sound);
6644 }
6645
6646 char *
6647 UserName()
6648 {
6649   static char buf[MSG_SIZ];
6650   DWORD bufsiz = MSG_SIZ;
6651
6652   if (!GetUserName(buf, &bufsiz)) {
6653     /*DisplayError("Error getting user name", GetLastError());*/
6654     strcpy(buf, "User");
6655   }
6656   return buf;
6657 }
6658
6659 char *
6660 HostName()
6661 {
6662   static char buf[MSG_SIZ];
6663   DWORD bufsiz = MSG_SIZ;
6664
6665   if (!GetComputerName(buf, &bufsiz)) {
6666     /*DisplayError("Error getting host name", GetLastError());*/
6667     strcpy(buf, "Unknown");
6668   }
6669   return buf;
6670 }
6671
6672
6673 int
6674 ClockTimerRunning()
6675 {
6676   return clockTimerEvent != 0;
6677 }
6678
6679 int
6680 StopClockTimer()
6681 {
6682   if (clockTimerEvent == 0) return FALSE;
6683   KillTimer(hwndMain, clockTimerEvent);
6684   clockTimerEvent = 0;
6685   return TRUE;
6686 }
6687
6688 void
6689 StartClockTimer(long millisec)
6690 {
6691   clockTimerEvent = SetTimer(hwndMain, (UINT) CLOCK_TIMER_ID,
6692                              (UINT) millisec, NULL);
6693 }
6694
6695 void
6696 DisplayWhiteClock(long timeRemaining, int highlight)
6697 {
6698   HDC hdc;
6699   hdc = GetDC(hwndMain);
6700   if (!IsIconic(hwndMain)) {
6701     DisplayAClock(hdc, timeRemaining, highlight, &whiteRect, "White");
6702   }
6703   if (highlight && iconCurrent == iconBlack) {
6704     iconCurrent = iconWhite;
6705     PostMessage(hwndMain, WM_SETICON, (WPARAM) TRUE, (LPARAM) iconCurrent);
6706     if (IsIconic(hwndMain)) {
6707       DrawIcon(hdc, 2, 2, iconCurrent);
6708     }
6709   }
6710   (void) ReleaseDC(hwndMain, hdc);
6711   if (hwndConsole)
6712     PostMessage(hwndConsole, WM_SETICON, (WPARAM) TRUE, (LPARAM) iconCurrent);
6713 }
6714
6715 void
6716 DisplayBlackClock(long timeRemaining, int highlight)
6717 {
6718   HDC hdc;
6719   hdc = GetDC(hwndMain);
6720   if (!IsIconic(hwndMain)) {
6721     DisplayAClock(hdc, timeRemaining, highlight, &blackRect, "Black");
6722   }
6723   if (highlight && iconCurrent == iconWhite) {
6724     iconCurrent = iconBlack;
6725     PostMessage(hwndMain, WM_SETICON, (WPARAM) TRUE, (LPARAM) iconCurrent);
6726     if (IsIconic(hwndMain)) {
6727       DrawIcon(hdc, 2, 2, iconCurrent);
6728     }
6729   }
6730   (void) ReleaseDC(hwndMain, hdc);
6731   if (hwndConsole)
6732     PostMessage(hwndConsole, WM_SETICON, (WPARAM) TRUE, (LPARAM) iconCurrent);
6733 }
6734
6735
6736 int
6737 LoadGameTimerRunning()
6738 {
6739   return loadGameTimerEvent != 0;
6740 }
6741
6742 int
6743 StopLoadGameTimer()
6744 {
6745   if (loadGameTimerEvent == 0) return FALSE;
6746   KillTimer(hwndMain, loadGameTimerEvent);
6747   loadGameTimerEvent = 0;
6748   return TRUE;
6749 }
6750
6751 void
6752 StartLoadGameTimer(long millisec)
6753 {
6754   loadGameTimerEvent = SetTimer(hwndMain, (UINT) LOAD_GAME_TIMER_ID,
6755                                 (UINT) millisec, NULL);
6756 }
6757
6758 void
6759 AutoSaveGame()
6760 {
6761   char *defName;
6762   FILE *f;
6763   char fileTitle[MSG_SIZ];
6764
6765   defName = DefaultFileName(appData.oldSaveStyle ? "gam" : "pgn");
6766   f = OpenFileDialog(hwndMain, TRUE, defName,
6767                      appData.oldSaveStyle ? "gam" : "pgn",
6768                      GAME_FILT, 
6769                      "Save Game to File", NULL, fileTitle, NULL);
6770   if (f != NULL) {
6771     SaveGame(f, 0, "");
6772     fclose(f);
6773   }
6774 }
6775
6776
6777 void
6778 ScheduleDelayedEvent(DelayedEventCallback cb, long millisec)
6779 {
6780   if (delayedTimerEvent != 0) {
6781     if (appData.debugMode) {
6782       fprintf(debugFP, "ScheduleDelayedEvent: event already scheduled\n");
6783     }
6784     KillTimer(hwndMain, delayedTimerEvent);
6785     delayedTimerEvent = 0;
6786     delayedTimerCallback();
6787   }
6788   delayedTimerCallback = cb;
6789   delayedTimerEvent = SetTimer(hwndMain, (UINT) DELAYED_TIMER_ID,
6790                                 (UINT) millisec, NULL);
6791 }
6792
6793 DelayedEventCallback
6794 GetDelayedEvent()
6795 {
6796   if (delayedTimerEvent) {
6797     return delayedTimerCallback;
6798   } else {
6799     return NULL;
6800   }
6801 }
6802
6803 void
6804 CancelDelayedEvent()
6805 {
6806   if (delayedTimerEvent) {
6807     KillTimer(hwndMain, delayedTimerEvent);
6808     delayedTimerEvent = 0;
6809   }
6810 }
6811
6812 /* Start a child process running the given program.
6813    The process's standard output can be read from "from", and its
6814    standard input can be written to "to".
6815    Exit with fatal error if anything goes wrong.
6816    Returns an opaque pointer that can be used to destroy the process
6817    later.
6818 */
6819 int
6820 StartChildProcess(char *cmdLine, char *dir, ProcRef *pr)
6821 {
6822 #define BUFSIZE 4096
6823
6824   HANDLE hChildStdinRd, hChildStdinWr,
6825     hChildStdoutRd, hChildStdoutWr;
6826   HANDLE hChildStdinWrDup, hChildStdoutRdDup;
6827   SECURITY_ATTRIBUTES saAttr;
6828   BOOL fSuccess;
6829   PROCESS_INFORMATION piProcInfo;
6830   STARTUPINFO siStartInfo;
6831   ChildProc *cp;
6832   char buf[MSG_SIZ];
6833   DWORD err;
6834
6835   if (appData.debugMode) {
6836     fprintf(debugFP, "StartChildProcess (dir=\"%s\") %s\n", dir, cmdLine);
6837   }
6838
6839   *pr = NoProc;
6840
6841   /* Set the bInheritHandle flag so pipe handles are inherited. */
6842   saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
6843   saAttr.bInheritHandle = TRUE;
6844   saAttr.lpSecurityDescriptor = NULL;
6845
6846   /*
6847    * The steps for redirecting child's STDOUT:
6848    *     1. Create anonymous pipe to be STDOUT for child.
6849    *     2. Create a noninheritable duplicate of read handle,
6850    *         and close the inheritable read handle.
6851    */
6852
6853   /* Create a pipe for the child's STDOUT. */
6854   if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) {
6855     return GetLastError();
6856   }
6857
6858   /* Duplicate the read handle to the pipe, so it is not inherited. */
6859   fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
6860                              GetCurrentProcess(), &hChildStdoutRdDup, 0,
6861                              FALSE,     /* not inherited */
6862                              DUPLICATE_SAME_ACCESS);
6863   if (! fSuccess) {
6864     return GetLastError();
6865   }
6866   CloseHandle(hChildStdoutRd);
6867
6868   /*
6869    * The steps for redirecting child's STDIN:
6870    *     1. Create anonymous pipe to be STDIN for child.
6871    *     2. Create a noninheritable duplicate of write handle,
6872    *         and close the inheritable write handle.
6873    */
6874
6875   /* Create a pipe for the child's STDIN. */
6876   if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
6877     return GetLastError();
6878   }
6879
6880   /* Duplicate the write handle to the pipe, so it is not inherited. */
6881   fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
6882                              GetCurrentProcess(), &hChildStdinWrDup, 0,
6883                              FALSE,     /* not inherited */
6884                              DUPLICATE_SAME_ACCESS);
6885   if (! fSuccess) {
6886     return GetLastError();
6887   }
6888   CloseHandle(hChildStdinWr);
6889
6890   /* Arrange to (1) look in dir for the child .exe file, and
6891    * (2) have dir be the child's working directory.  Interpret
6892    * dir relative to the directory WinBoard loaded from. */
6893   GetCurrentDirectory(MSG_SIZ, buf);
6894   SetCurrentDirectory(installDir);
6895   SetCurrentDirectory(dir);
6896
6897   /* Now create the child process. */
6898
6899   siStartInfo.cb = sizeof(STARTUPINFO);
6900   siStartInfo.lpReserved = NULL;
6901   siStartInfo.lpDesktop = NULL;
6902   siStartInfo.lpTitle = NULL;
6903   siStartInfo.dwFlags = STARTF_USESTDHANDLES;
6904   siStartInfo.cbReserved2 = 0;
6905   siStartInfo.lpReserved2 = NULL;
6906   siStartInfo.hStdInput = hChildStdinRd;
6907   siStartInfo.hStdOutput = hChildStdoutWr;
6908   siStartInfo.hStdError = hChildStdoutWr;
6909
6910   fSuccess = CreateProcess(NULL,
6911                            cmdLine,        /* command line */
6912                            NULL,           /* process security attributes */
6913                            NULL,           /* primary thread security attrs */
6914                            TRUE,           /* handles are inherited */
6915                            DETACHED_PROCESS|CREATE_NEW_PROCESS_GROUP,
6916                            NULL,           /* use parent's environment */
6917                            NULL,
6918                            &siStartInfo, /* STARTUPINFO pointer */
6919                            &piProcInfo); /* receives PROCESS_INFORMATION */
6920
6921   err = GetLastError();
6922   SetCurrentDirectory(buf); /* return to prev directory */
6923   if (! fSuccess) {
6924     return err;
6925   }
6926
6927   /* Close the handles we don't need in the parent */
6928   CloseHandle(piProcInfo.hThread);
6929   CloseHandle(hChildStdinRd);
6930   CloseHandle(hChildStdoutWr);
6931
6932   /* Prepare return value */
6933   cp = (ChildProc *) calloc(1, sizeof(ChildProc));
6934   cp->kind = CPReal;
6935   cp->hProcess = piProcInfo.hProcess;
6936   cp->pid = piProcInfo.dwProcessId;
6937   cp->hFrom = hChildStdoutRdDup;
6938   cp->hTo = hChildStdinWrDup;
6939
6940   *pr = (void *) cp;
6941
6942   /* Klaus Friedel says that this Sleep solves a problem under Windows
6943      2000 where engines sometimes don't see the initial command(s)
6944      from WinBoard and hang.  I don't understand how that can happen,
6945      but the Sleep is harmless, so I've put it in.  Others have also
6946      reported what may be the same problem, so hopefully this will fix
6947      it for them too.  */
6948   Sleep(500);
6949
6950   return NO_ERROR;
6951 }
6952
6953
6954 void
6955 DestroyChildProcess(ProcRef pr, int/*boolean*/ signal)
6956 {
6957   ChildProc *cp;
6958
6959   cp = (ChildProc *) pr;
6960   if (cp == NULL) return;
6961
6962   switch (cp->kind) {
6963   case CPReal:
6964     /* TerminateProcess is considered harmful, so... */
6965     CloseHandle(cp->hTo); /* Closing this will give the child an EOF and hopefully kill it */
6966     if (cp->hFrom) CloseHandle(cp->hFrom);  /* if NULL, InputThread will close it */
6967     /* The following doesn't work because the chess program
6968        doesn't "have the same console" as WinBoard.  Maybe
6969        we could arrange for this even though neither WinBoard
6970        nor the chess program uses a console for stdio? */
6971     /*!!if (signal) GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, cp->pid);*/
6972     CloseHandle(cp->hProcess);
6973     break;
6974
6975   case CPComm:
6976     if (cp->hFrom) CloseHandle(cp->hFrom);
6977     break;
6978
6979   case CPSock:
6980     closesocket(cp->sock);
6981     WSACleanup();
6982     break;
6983
6984   case CPRcmd:
6985     if (signal) send(cp->sock2, "\017", 1, 0);  /* 017 = 15 = SIGTERM */
6986     closesocket(cp->sock);
6987     closesocket(cp->sock2);
6988     WSACleanup();
6989     break;
6990   }
6991   free(cp);
6992 }
6993
6994 void
6995 InterruptChildProcess(ProcRef pr)
6996 {
6997   ChildProc *cp;
6998
6999   cp = (ChildProc *) pr;
7000   if (cp == NULL) return;
7001   switch (cp->kind) {
7002   case CPReal:
7003     /* The following doesn't work because the chess program
7004        doesn't "have the same console" as WinBoard.  Maybe
7005        we could arrange for this even though neither WinBoard
7006        nor the chess program uses a console for stdio */
7007     /*!!GenerateConsoleCtrlEvent(CTRL_C_EVENT, cp->pid);*/
7008     break;
7009
7010   case CPComm:
7011   case CPSock:
7012     /* Can't interrupt */
7013     break;
7014
7015   case CPRcmd:
7016     send(cp->sock2, "\002", 1, 0);  /* 2 = SIGINT */
7017     break;
7018   }
7019 }
7020
7021
7022 int
7023 OpenTelnet(char *host, char *port, ProcRef *pr)
7024 {
7025   char cmdLine[MSG_SIZ];
7026
7027   if (port[0] == NULLCHAR) {
7028     sprintf(cmdLine, "%s %s", appData.telnetProgram, host);
7029   } else {
7030     sprintf(cmdLine, "%s %s %s", appData.telnetProgram, host, port);
7031   }
7032   return StartChildProcess(cmdLine, "", pr);
7033 }
7034
7035
7036 /* Code to open TCP sockets */
7037
7038 int
7039 OpenTCP(char *host, char *port, ProcRef *pr)
7040 {
7041   ChildProc *cp;
7042   int err;
7043   SOCKET s;
7044   struct sockaddr_in sa, mysa;
7045   struct hostent FAR *hp;
7046   unsigned short uport;
7047   WORD wVersionRequested;
7048   WSADATA wsaData;
7049
7050   /* Initialize socket DLL */
7051   wVersionRequested = MAKEWORD(1, 1);
7052   err = WSAStartup(wVersionRequested, &wsaData);
7053   if (err != 0) return err;
7054
7055   /* Make socket */
7056   if ((s = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
7057     err = WSAGetLastError();
7058     WSACleanup();
7059     return err;
7060   }
7061
7062   /* Bind local address using (mostly) don't-care values.
7063    */
7064   memset((char *) &mysa, 0, sizeof(struct sockaddr_in));
7065   mysa.sin_family = AF_INET;
7066   mysa.sin_addr.s_addr = INADDR_ANY;
7067   uport = (unsigned short) 0;
7068   mysa.sin_port = htons(uport);
7069   if (bind(s, (struct sockaddr *) &mysa, sizeof(struct sockaddr_in))
7070       == SOCKET_ERROR) {
7071     err = WSAGetLastError();
7072     WSACleanup();
7073     return err;
7074   }
7075
7076   /* Resolve remote host name */
7077   memset((char *) &sa, 0, sizeof(struct sockaddr_in));
7078   if (!(hp = gethostbyname(host))) {
7079     unsigned int b0, b1, b2, b3;
7080
7081     err = WSAGetLastError();
7082
7083     if (sscanf(host, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) == 4) {
7084       hp = (struct hostent *) calloc(1, sizeof(struct hostent));
7085       hp->h_addrtype = AF_INET;
7086       hp->h_length = 4;
7087       hp->h_addr_list = (char **) calloc(2, sizeof(char *));
7088       hp->h_addr_list[0] = (char *) malloc(4);
7089       hp->h_addr_list[0][0] = (char) b0;
7090       hp->h_addr_list[0][1] = (char) b1;
7091       hp->h_addr_list[0][2] = (char) b2;
7092       hp->h_addr_list[0][3] = (char) b3;
7093     } else {
7094       WSACleanup();
7095       return err;
7096     }
7097   }
7098   sa.sin_family = hp->h_addrtype;
7099   uport = (unsigned short) atoi(port);
7100   sa.sin_port = htons(uport);
7101   memcpy((char *) &sa.sin_addr, hp->h_addr, hp->h_length);
7102
7103   /* Make connection */
7104   if (connect(s, (struct sockaddr *) &sa,
7105               sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
7106     err = WSAGetLastError();
7107     WSACleanup();
7108     return err;
7109   }
7110
7111   /* Prepare return value */
7112   cp = (ChildProc *) calloc(1, sizeof(ChildProc));
7113   cp->kind = CPSock;
7114   cp->sock = s;
7115   *pr = (ProcRef *) cp;
7116
7117   return NO_ERROR;
7118 }
7119
7120 int
7121 OpenCommPort(char *name, ProcRef *pr)
7122 {
7123   HANDLE h;
7124   COMMTIMEOUTS ct;
7125   ChildProc *cp;
7126   char fullname[MSG_SIZ];
7127
7128   if (*name != '\\')
7129     sprintf(fullname, "\\\\.\\%s", name);
7130   else
7131     strcpy(fullname, name);
7132
7133   h = CreateFile(name, GENERIC_READ | GENERIC_WRITE,
7134                  0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
7135   if (h == (HANDLE) -1) {
7136     return GetLastError();
7137   }
7138   hCommPort = h;
7139
7140   if (!SetCommState(h, (LPDCB) &dcb)) return GetLastError();
7141
7142   /* Accumulate characters until a 100ms pause, then parse */
7143   ct.ReadIntervalTimeout = 100;
7144   ct.ReadTotalTimeoutMultiplier = 0;
7145   ct.ReadTotalTimeoutConstant = 0;
7146   ct.WriteTotalTimeoutMultiplier = 0;
7147   ct.WriteTotalTimeoutConstant = 0;
7148   if (!SetCommTimeouts(h, (LPCOMMTIMEOUTS) &ct)) return GetLastError();
7149
7150   /* Prepare return value */
7151   cp = (ChildProc *) calloc(1, sizeof(ChildProc));
7152   cp->kind = CPComm;
7153   cp->hFrom = h;
7154   cp->hTo = h;
7155   *pr = (ProcRef *) cp;
7156
7157   return NO_ERROR;
7158 }
7159
7160 int
7161 OpenLoopback(ProcRef *pr)
7162 {
7163   DisplayFatalError("Not implemented", 0, 1);
7164   return NO_ERROR;
7165 }
7166
7167
7168 int
7169 OpenRcmd(char* host, char* user, char* cmd, ProcRef* pr)
7170 {
7171   ChildProc *cp;
7172   int err;
7173   SOCKET s, s2, s3;
7174   struct sockaddr_in sa, mysa;
7175   struct hostent FAR *hp;
7176   unsigned short uport;
7177   WORD wVersionRequested;
7178   WSADATA wsaData;
7179   int fromPort;
7180   char stderrPortStr[MSG_SIZ];
7181
7182   /* Initialize socket DLL */
7183   wVersionRequested = MAKEWORD(1, 1);
7184   err = WSAStartup(wVersionRequested, &wsaData);
7185   if (err != 0) return err;
7186
7187   /* Resolve remote host name */
7188   memset((char *) &sa, 0, sizeof(struct sockaddr_in));
7189   if (!(hp = gethostbyname(host))) {
7190     unsigned int b0, b1, b2, b3;
7191
7192     err = WSAGetLastError();
7193
7194     if (sscanf(host, "%u.%u.%u.%u", &b0, &b1, &b2, &b3) == 4) {
7195       hp = (struct hostent *) calloc(1, sizeof(struct hostent));
7196       hp->h_addrtype = AF_INET;
7197       hp->h_length = 4;
7198       hp->h_addr_list = (char **) calloc(2, sizeof(char *));
7199       hp->h_addr_list[0] = (char *) malloc(4);
7200       hp->h_addr_list[0][0] = (char) b0;
7201       hp->h_addr_list[0][1] = (char) b1;
7202       hp->h_addr_list[0][2] = (char) b2;
7203       hp->h_addr_list[0][3] = (char) b3;
7204     } else {
7205       WSACleanup();
7206       return err;
7207     }
7208   }
7209   sa.sin_family = hp->h_addrtype;
7210   uport = (unsigned short) 514;
7211   sa.sin_port = htons(uport);
7212   memcpy((char *) &sa.sin_addr, hp->h_addr, hp->h_length);
7213
7214   /* Bind local socket to unused "privileged" port address
7215    */
7216   s = INVALID_SOCKET;
7217   memset((char *) &mysa, 0, sizeof(struct sockaddr_in));
7218   mysa.sin_family = AF_INET;
7219   mysa.sin_addr.s_addr = INADDR_ANY;
7220   for (fromPort = 1023;; fromPort--) {
7221     if (fromPort < 0) {
7222       WSACleanup();
7223       return WSAEADDRINUSE;
7224     }
7225     if (s == INVALID_SOCKET) {
7226       if ((s = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
7227         err = WSAGetLastError();
7228         WSACleanup();
7229         return err;
7230       }
7231     }
7232     uport = (unsigned short) fromPort;
7233     mysa.sin_port = htons(uport);
7234     if (bind(s, (struct sockaddr *) &mysa, sizeof(struct sockaddr_in))
7235         == SOCKET_ERROR) {
7236       err = WSAGetLastError();
7237       if (err == WSAEADDRINUSE) continue;
7238       WSACleanup();
7239       return err;
7240     }
7241     if (connect(s, (struct sockaddr *) &sa,
7242       sizeof(struct sockaddr_in)) == SOCKET_ERROR) {
7243       err = WSAGetLastError();
7244       if (err == WSAEADDRINUSE) {
7245         closesocket(s);
7246         s = -1;
7247         continue;
7248       }
7249       WSACleanup();
7250       return err;
7251     }
7252     break;
7253   }
7254
7255   /* Bind stderr local socket to unused "privileged" port address
7256    */
7257   s2 = INVALID_SOCKET;
7258   memset((char *) &mysa, 0, sizeof(struct sockaddr_in));
7259   mysa.sin_family = AF_INET;
7260   mysa.sin_addr.s_addr = INADDR_ANY;
7261   for (fromPort = 1023;; fromPort--) {
7262     if (fromPort == prevStderrPort) continue; // don't reuse port
7263     if (fromPort < 0) {
7264       (void) closesocket(s);
7265       WSACleanup();
7266       return WSAEADDRINUSE;
7267     }
7268     if (s2 == INVALID_SOCKET) {
7269       if ((s2 = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
7270         err = WSAGetLastError();
7271         closesocket(s);
7272         WSACleanup();
7273         return err;
7274       }
7275     }
7276     uport = (unsigned short) fromPort;
7277     mysa.sin_port = htons(uport);
7278     if (bind(s2, (struct sockaddr *) &mysa, sizeof(struct sockaddr_in))
7279         == SOCKET_ERROR) {
7280       err = WSAGetLastError();
7281       if (err == WSAEADDRINUSE) continue;
7282       (void) closesocket(s);
7283       WSACleanup();
7284       return err;
7285     }
7286     if (listen(s2, 1) == SOCKET_ERROR) {
7287       err = WSAGetLastError();
7288       if (err == WSAEADDRINUSE) {
7289         closesocket(s2);
7290         s2 = INVALID_SOCKET;
7291         continue;
7292       }
7293       (void) closesocket(s);
7294       (void) closesocket(s2);
7295       WSACleanup();
7296       return err;
7297     }
7298     break;
7299   }
7300   prevStderrPort = fromPort; // remember port used
7301   sprintf(stderrPortStr, "%d", fromPort);
7302
7303   if (send(s, stderrPortStr, strlen(stderrPortStr) + 1, 0) == SOCKET_ERROR) {
7304     err = WSAGetLastError();
7305     (void) closesocket(s);
7306     (void) closesocket(s2);
7307     WSACleanup();
7308     return err;
7309   }
7310
7311   if (send(s, UserName(), strlen(UserName()) + 1, 0) == SOCKET_ERROR) {
7312     err = WSAGetLastError();
7313     (void) closesocket(s);
7314     (void) closesocket(s2);
7315     WSACleanup();
7316     return err;
7317   }
7318   if (*user == NULLCHAR) user = UserName();
7319   if (send(s, user, strlen(user) + 1, 0) == SOCKET_ERROR) {
7320     err = WSAGetLastError();
7321     (void) closesocket(s);
7322     (void) closesocket(s2);
7323     WSACleanup();
7324     return err;
7325   }
7326   if (send(s, cmd, strlen(cmd) + 1, 0) == SOCKET_ERROR) {
7327     err = WSAGetLastError();
7328     (void) closesocket(s);
7329     (void) closesocket(s2);
7330     WSACleanup();
7331     return err;
7332   }
7333
7334   if ((s3 = accept(s2, NULL, NULL)) == INVALID_SOCKET) {
7335     err = WSAGetLastError();
7336     (void) closesocket(s);
7337     (void) closesocket(s2);
7338     WSACleanup();
7339     return err;
7340   }
7341   (void) closesocket(s2);  /* Stop listening */
7342
7343   /* Prepare return value */
7344   cp = (ChildProc *) calloc(1, sizeof(ChildProc));
7345   cp->kind = CPRcmd;
7346   cp->sock = s;
7347   cp->sock2 = s3;
7348   *pr = (ProcRef *) cp;
7349
7350   return NO_ERROR;
7351 }
7352
7353
7354 InputSourceRef
7355 AddInputSource(ProcRef pr, int lineByLine,
7356                InputCallback func, VOIDSTAR closure)
7357 {
7358   InputSource *is, *is2;
7359   ChildProc *cp = (ChildProc *) pr;
7360
7361   is = (InputSource *) calloc(1, sizeof(InputSource));
7362   is->lineByLine = lineByLine;
7363   is->func = func;
7364   is->closure = closure;
7365   is->second = NULL;
7366   is->next = is->buf;
7367   if (pr == NoProc) {
7368     is->kind = CPReal;
7369     consoleInputSource = is;
7370   } else {
7371     is->kind = cp->kind;
7372     switch (cp->kind) {
7373     case CPReal:
7374       is->hFile = cp->hFrom;
7375       cp->hFrom = NULL; /* now owned by InputThread */
7376       is->hThread =
7377         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) NonOvlInputThread,
7378                      (LPVOID) is, 0, &is->id);
7379       break;
7380
7381     case CPComm:
7382       is->hFile = cp->hFrom;
7383       cp->hFrom = NULL; /* now owned by InputThread */
7384       is->hThread =
7385         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) InputThread,
7386                      (LPVOID) is, 0, &is->id);
7387       break;
7388
7389     case CPSock:
7390       is->sock = cp->sock;
7391       is->hThread =
7392         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SocketInputThread,
7393                      (LPVOID) is, 0, &is->id);
7394       break;
7395
7396     case CPRcmd:
7397       is2 = (InputSource *) calloc(1, sizeof(InputSource));
7398       *is2 = *is;
7399       is->sock = cp->sock;
7400       is->second = is2;
7401       is2->sock = cp->sock2;
7402       is2->second = is2;
7403       is->hThread =
7404         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SocketInputThread,
7405                      (LPVOID) is, 0, &is->id);
7406       is2->hThread =
7407         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SocketInputThread,
7408                      (LPVOID) is2, 0, &is2->id);
7409       break;
7410     }
7411   }
7412   return (InputSourceRef) is;
7413 }
7414
7415 void
7416 RemoveInputSource(InputSourceRef isr)
7417 {
7418   InputSource *is;
7419
7420   is = (InputSource *) isr;
7421   is->hThread = NULL;  /* tell thread to stop */
7422   CloseHandle(is->hThread);
7423   if (is->second != NULL) {
7424     is->second->hThread = NULL;
7425     CloseHandle(is->second->hThread);
7426   }
7427 }
7428
7429
7430 int
7431 OutputToProcess(ProcRef pr, char *message, int count, int *outError)
7432 {
7433   DWORD dOutCount;
7434   int outCount = SOCKET_ERROR;
7435   ChildProc *cp = (ChildProc *) pr;
7436   static OVERLAPPED ovl;
7437
7438   if (pr == NoProc) {
7439     ConsoleOutput(message, count, FALSE);
7440     return count;
7441   } 
7442
7443   if (ovl.hEvent == NULL) {
7444     ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
7445   }
7446   ovl.Internal = ovl.InternalHigh = ovl.Offset = ovl.OffsetHigh = 0;
7447
7448   switch (cp->kind) {
7449   case CPSock:
7450   case CPRcmd:
7451     outCount = send(cp->sock, message, count, 0);
7452     if (outCount == SOCKET_ERROR) {
7453       *outError = WSAGetLastError();
7454     } else {
7455       *outError = NO_ERROR;
7456     }
7457     break;
7458
7459   case CPReal:
7460     if (WriteFile(((ChildProc *)pr)->hTo, message, count,
7461                   &dOutCount, NULL)) {
7462       *outError = NO_ERROR;
7463       outCount = (int) dOutCount;
7464     } else {
7465       *outError = GetLastError();
7466     }
7467     break;
7468
7469   case CPComm:
7470     *outError = DoWriteFile(((ChildProc *)pr)->hTo, message, count,
7471                             &dOutCount, &ovl);
7472     if (*outError == NO_ERROR) {
7473       outCount = (int) dOutCount;
7474     }
7475     break;
7476   }
7477   return outCount;
7478 }
7479
7480 int
7481 OutputToProcessDelayed(ProcRef pr, char *message, int count, int *outError,
7482                        long msdelay)
7483 {
7484   /* Ignore delay, not implemented for WinBoard */
7485   return OutputToProcess(pr, message, count, outError);
7486 }
7487
7488
7489 void
7490 CmailSigHandlerCallBack(InputSourceRef isr, VOIDSTAR closure,
7491                         char *buf, int count, int error)
7492 {
7493   DisplayFatalError("Not implemented", 0, 1);
7494 }
7495
7496 /* see wgamelist.c for Game List functions */
7497 /* see wedittags.c for Edit Tags functions */
7498
7499
7500 VOID
7501 ICSInitScript()
7502 {
7503   FILE *f;
7504   char buf[MSG_SIZ];
7505   char *dummy;
7506
7507   if (SearchPath(installDir, appData.icsLogon, NULL, MSG_SIZ, buf, &dummy)) {
7508     f = fopen(buf, "r");
7509     if (f != NULL) {
7510       ProcessICSInitScript(f);
7511       fclose(f);
7512     }
7513   }
7514 }
7515
7516
7517 VOID
7518 StartAnalysisClock()
7519 {
7520   if (analysisTimerEvent) return;
7521   analysisTimerEvent = SetTimer(hwndMain, (UINT) ANALYSIS_TIMER_ID,
7522                                         (UINT) 2000, NULL);
7523 }
7524
7525 LRESULT CALLBACK
7526 AnalysisDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
7527 {
7528   static HANDLE hwndText;
7529   RECT rect;
7530   static int sizeX, sizeY;
7531   int newSizeX, newSizeY, flags;
7532   MINMAXINFO *mmi;
7533
7534   switch (message) {
7535   case WM_INITDIALOG: /* message: initialize dialog box */
7536     /* Initialize the dialog items */
7537     hwndText = GetDlgItem(hDlg, OPT_AnalysisText);
7538     SetWindowText(hDlg, analysisTitle);
7539     SetDlgItemText(hDlg, OPT_AnalysisText, analysisText);
7540     /* Size and position the dialog */
7541     if (!analysisDialog) {
7542       analysisDialog = hDlg;
7543       flags = SWP_NOZORDER;
7544       GetClientRect(hDlg, &rect);
7545       sizeX = rect.right;
7546       sizeY = rect.bottom;
7547       if (analysisX != CW_USEDEFAULT && analysisY != CW_USEDEFAULT &&
7548           analysisW != CW_USEDEFAULT && analysisH != CW_USEDEFAULT) {
7549         WINDOWPLACEMENT wp;
7550         EnsureOnScreen(&analysisX, &analysisY);
7551         wp.length = sizeof(WINDOWPLACEMENT);
7552         wp.flags = 0;
7553         wp.showCmd = SW_SHOW;
7554         wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0;
7555         wp.rcNormalPosition.left = analysisX;
7556         wp.rcNormalPosition.right = analysisX + analysisW;
7557         wp.rcNormalPosition.top = analysisY;
7558         wp.rcNormalPosition.bottom = analysisY + analysisH;
7559         SetWindowPlacement(hDlg, &wp);
7560
7561         GetClientRect(hDlg, &rect);
7562         newSizeX = rect.right;
7563         newSizeY = rect.bottom;
7564         ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY,
7565                               newSizeX, newSizeY);
7566         sizeX = newSizeX;
7567         sizeY = newSizeY;
7568       }
7569     }
7570     return FALSE;
7571
7572   case WM_COMMAND: /* message: received a command */
7573     switch (LOWORD(wParam)) {
7574     case IDCANCEL:
7575       EditGameEvent();
7576       return TRUE;
7577     default:
7578       break;
7579     }
7580     break;
7581
7582   case WM_SIZE:
7583     newSizeX = LOWORD(lParam);
7584     newSizeY = HIWORD(lParam);
7585     ResizeEditPlusButtons(hDlg, hwndText, sizeX, sizeY, newSizeX, newSizeY);
7586     sizeX = newSizeX;
7587     sizeY = newSizeY;
7588     break;
7589
7590   case WM_GETMINMAXINFO:
7591     /* Prevent resizing window too small */
7592     mmi = (MINMAXINFO *) lParam;
7593     mmi->ptMinTrackSize.x = 100;
7594     mmi->ptMinTrackSize.y = 100;
7595     break;
7596   }
7597   return FALSE;
7598 }
7599
7600 VOID
7601 AnalysisPopUp(char* title, char* str)
7602 {
7603   FARPROC lpProc;
7604   char *p, *q;
7605
7606   if (str == NULL) str = "";
7607   p = (char *) malloc(2 * strlen(str) + 2);
7608   q = p;
7609   while (*str) {
7610     if (*str == '\n') *q++ = '\r';
7611     *q++ = *str++;
7612   }
7613   *q = NULLCHAR;
7614   if (analysisText != NULL) free(analysisText);
7615   analysisText = p;
7616
7617   if (analysisDialog) {
7618     SetWindowText(analysisDialog, title);
7619     SetDlgItemText(analysisDialog, OPT_AnalysisText, analysisText);
7620     ShowWindow(analysisDialog, SW_SHOW);
7621   } else {
7622     analysisTitle = title;
7623     lpProc = MakeProcInstance((FARPROC)AnalysisDialog, hInst);
7624     CreateDialog(hInst, MAKEINTRESOURCE(DLG_Analysis),
7625                  hwndMain, (DLGPROC)lpProc);
7626     FreeProcInstance(lpProc);
7627   }
7628   analysisDialogUp = TRUE;  
7629 }
7630
7631 VOID
7632 AnalysisPopDown()
7633 {
7634   if (analysisDialog) {
7635     ShowWindow(analysisDialog, SW_HIDE);
7636   }
7637   analysisDialogUp = FALSE;  
7638 }
7639
7640
7641 VOID
7642 SetHighlights(int fromX, int fromY, int toX, int toY)
7643 {
7644   highlightInfo.sq[0].x = fromX;
7645   highlightInfo.sq[0].y = fromY;
7646   highlightInfo.sq[1].x = toX;
7647   highlightInfo.sq[1].y = toY;
7648 }
7649
7650 VOID
7651 ClearHighlights()
7652 {
7653   highlightInfo.sq[0].x = highlightInfo.sq[0].y = 
7654     highlightInfo.sq[1].x = highlightInfo.sq[1].y = -1;
7655 }
7656
7657 VOID
7658 SetPremoveHighlights(int fromX, int fromY, int toX, int toY)
7659 {
7660   premoveHighlightInfo.sq[0].x = fromX;
7661   premoveHighlightInfo.sq[0].y = fromY;
7662   premoveHighlightInfo.sq[1].x = toX;
7663   premoveHighlightInfo.sq[1].y = toY;
7664 }
7665
7666 VOID
7667 ClearPremoveHighlights()
7668 {
7669   premoveHighlightInfo.sq[0].x = premoveHighlightInfo.sq[0].y = 
7670     premoveHighlightInfo.sq[1].x = premoveHighlightInfo.sq[1].y = -1;
7671 }
7672
7673 VOID
7674 ShutDownFrontEnd()
7675 {
7676   if (saveSettingsOnExit) SaveSettings(settingsFileName);
7677   DeleteClipboardTempFiles();
7678 }
7679
7680 void
7681 BoardToTop()
7682 {
7683     if (IsIconic(hwndMain))
7684       ShowWindow(hwndMain, SW_RESTORE);
7685
7686     SetActiveWindow(hwndMain);
7687 }
7688
7689 /*
7690  * Prototypes for animation support routines
7691  */
7692 static void ScreenSquare(int column, int row, POINT * pt);
7693 static void Tween( POINT * start, POINT * mid, POINT * finish, int factor,
7694      POINT frames[], int * nFrames);
7695
7696
7697 #define kFactor 4
7698
7699 void
7700 AnimateMove(board, fromX, fromY, toX, toY)
7701      Board board;
7702      int fromX;
7703      int fromY;
7704      int toX;
7705      int toY;
7706 {
7707   ChessSquare piece;
7708   POINT start, finish, mid;
7709   POINT frames[kFactor * 2 + 1];
7710   int nFrames, n;
7711
7712   if (!appData.animate) return;
7713   if (doingSizing) return;
7714   if (fromY < 0 || fromX < 0) return;
7715   piece = board[fromY][fromX];
7716   if (piece >= EmptySquare) return;
7717
7718   ScreenSquare(fromX, fromY, &start);
7719   ScreenSquare(toX, toY, &finish);
7720
7721   /* All pieces except knights move in straight line */
7722   if (piece != WhiteKnight && piece != BlackKnight) {
7723     mid.x = start.x + (finish.x - start.x) / 2;
7724     mid.y = start.y + (finish.y - start.y) / 2;
7725   } else {
7726     /* Knight: make diagonal movement then straight */
7727     if (abs(toY - fromY) < abs(toX - fromX)) {
7728        mid.x = start.x + (finish.x - start.x) / 2;
7729        mid.y = finish.y;
7730      } else {
7731        mid.x = finish.x;
7732        mid.y = start.y + (finish.y - start.y) / 2;
7733      }
7734   }
7735   
7736   /* Don't use as many frames for very short moves */
7737   if (abs(toY - fromY) + abs(toX - fromX) <= 2)
7738     Tween(&start, &mid, &finish, kFactor - 1, frames, &nFrames);
7739   else
7740     Tween(&start, &mid, &finish, kFactor, frames, &nFrames);
7741
7742   animInfo.from.x = fromX;
7743   animInfo.from.y = fromY;
7744   animInfo.to.x = toX;
7745   animInfo.to.y = toY;
7746   animInfo.lastpos = start;
7747   animInfo.piece = piece;
7748   for (n = 0; n < nFrames; n++) {
7749     animInfo.pos = frames[n];
7750     DrawPosition(FALSE, NULL);
7751     animInfo.lastpos = animInfo.pos;
7752     Sleep(appData.animSpeed);
7753   }
7754   animInfo.pos = finish;
7755   DrawPosition(FALSE, NULL);
7756   animInfo.piece = EmptySquare;
7757 }
7758
7759 /*      Convert board position to corner of screen rect and color       */
7760
7761 static void
7762 ScreenSquare(column, row, pt)
7763      int column; int row; POINT * pt;
7764 {
7765   if (flipView) {
7766     pt->x = lineGap + ((BOARD_SIZE-1)-column) * (squareSize + lineGap);
7767     pt->y = lineGap + row * (squareSize + lineGap);
7768   } else {
7769     pt->x = lineGap + column * (squareSize + lineGap);
7770     pt->y = lineGap + ((BOARD_SIZE-1)-row) * (squareSize + lineGap);
7771   }
7772 }
7773
7774 /*      Generate a series of frame coords from start->mid->finish.
7775         The movement rate doubles until the half way point is
7776         reached, then halves back down to the final destination,
7777         which gives a nice slow in/out effect. The algorithmn
7778         may seem to generate too many intermediates for short
7779         moves, but remember that the purpose is to attract the
7780         viewers attention to the piece about to be moved and
7781         then to where it ends up. Too few frames would be less
7782         noticeable.                                             */
7783
7784 static void
7785 Tween(start, mid, finish, factor, frames, nFrames)
7786      POINT * start; POINT * mid;
7787      POINT * finish; int factor;
7788      POINT frames[]; int * nFrames;
7789 {
7790   int n, fraction = 1, count = 0;
7791
7792   /* Slow in, stepping 1/16th, then 1/8th, ... */
7793   for (n = 0; n < factor; n++)
7794     fraction *= 2;
7795   for (n = 0; n < factor; n++) {
7796     frames[count].x = start->x + (mid->x - start->x) / fraction;
7797     frames[count].y = start->y + (mid->y - start->y) / fraction;
7798     count ++;
7799     fraction = fraction / 2;
7800   }
7801   
7802   /* Midpoint */
7803   frames[count] = *mid;
7804   count ++;
7805   
7806   /* Slow out, stepping 1/2, then 1/4, ... */
7807   fraction = 2;
7808   for (n = 0; n < factor; n++) {
7809     frames[count].x = finish->x - (finish->x - mid->x) / fraction;
7810     frames[count].y = finish->y - (finish->y - mid->y) / fraction;
7811     count ++;
7812     fraction = fraction * 2;
7813   }
7814   *nFrames = count;
7815 }
7816
7817 void
7818 HistorySet(char movelist[][2*MOVE_LEN], int first, int last, int current)
7819 {
7820   /* Currently not implemented in WinBoard */
7821 }
7822
7823