Move ICS-engine analyze and AnalyzeGame code to shared back-end
[xboard.git] / menus.c
1 /*
2  * menus.c -- platform-indendent menu handling code for XBoard
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
9  *
10  * The following terms apply to Digital Equipment Corporation's copyright
11  * interest in XBoard:
12  * ------------------------------------------------------------------------
13  * All Rights Reserved
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose and without fee is hereby granted,
17  * provided that the above copyright notice appear in all copies and that
18  * both that copyright notice and this permission notice appear in
19  * supporting documentation, and that the name of Digital not be
20  * used in advertising or publicity pertaining to distribution of the
21  * software without specific, written prior permission.
22  *
23  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29  * SOFTWARE.
30  * ------------------------------------------------------------------------
31  *
32  * The following terms apply to the enhanced version of XBoard
33  * distributed by the Free Software Foundation:
34  * ------------------------------------------------------------------------
35  *
36  * GNU XBoard 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 3 of the License, or (at
39  * your option) any later version.
40  *
41  * GNU XBoard is distributed in the hope that it will be useful, but
42  * WITHOUT ANY WARRANTY; without even the implied warranty of
43  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44  * 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, see http://www.gnu.org/licenses/.  *
48  *
49  *------------------------------------------------------------------------
50  ** See the file ChangeLog for a revision history.  */
51
52 #define HIGHDRAG 1
53
54 #include "config.h"
55
56 #include <stdio.h>
57 #include <ctype.h>
58 #include <signal.h>
59 #include <errno.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <pwd.h>
63 #include <math.h>
64
65 #if STDC_HEADERS
66 # include <stdlib.h>
67 # include <string.h>
68 #else /* not STDC_HEADERS */
69 extern char *getenv();
70 # if HAVE_STRING_H
71 #  include <string.h>
72 # else /* not HAVE_STRING_H */
73 #  include <strings.h>
74 # endif /* not HAVE_STRING_H */
75 #endif /* not STDC_HEADERS */
76
77 #if HAVE_UNISTD_H
78 # include <unistd.h>
79 #endif
80
81 #if ENABLE_NLS
82 #include <locale.h>
83 #endif
84
85 // [HGM] bitmaps: put before incuding the bitmaps / pixmaps, to know how many piece types there are.
86 #include "common.h"
87
88 #include "frontend.h"
89 #include "backend.h"
90 #include "backendz.h"
91 #include "moves.h"
92 #include "xhistory.h"
93 #include "xedittags.h"
94 #include "menus.h"
95 #include "gettext.h"
96
97 #ifdef ENABLE_NLS
98 # define  _(s) gettext (s)
99 # define N_(s) gettext_noop (s)
100 #else
101 # define  _(s) (s)
102 # define N_(s)  s
103 #endif
104
105 /*
106  * Button/menu procedures
107  */
108
109 char  *gameCopyFilename, *gamePasteFilename;
110 Boolean saveSettingsOnExit;
111 char *settingsFileName;
112
113 static int
114 LoadGamePopUp (FILE *f, int gameNumber, char *title)
115 {
116     cmailMsgLoaded = FALSE;
117     if (gameNumber == 0) {
118         int error = GameListBuild(f);
119         if (error) {
120             DisplayError(_("Cannot build game list"), error);
121         } else if (!ListEmpty(&gameList) &&
122                    ((ListGame *) gameList.tailPred)->number > 1) {
123             GameListPopUp(f, title);
124             return TRUE;
125         }
126         GameListDestroy();
127         gameNumber = 1;
128     }
129     return LoadGame(f, gameNumber, title, FALSE);
130 }
131
132 void
133 LoadGameProc ()
134 {
135     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
136         Reset(FALSE, TRUE);
137     }
138     FileNamePopUp(_("Load game file name?"), "", ".pgn .game", LoadGamePopUp, "rb");
139 }
140
141 void
142 LoadNextGameProc ()
143 {
144     ReloadGame(1);
145 }
146
147 void
148 LoadPrevGameProc ()
149 {
150     ReloadGame(-1);
151 }
152
153 void
154 ReloadGameProc ()
155 {
156     ReloadGame(0);
157 }
158
159 void
160 LoadNextPositionProc ()
161 {
162     ReloadPosition(1);
163 }
164
165 void
166 LoadPrevPositionProc ()
167 {
168     ReloadPosition(-1);
169 }
170
171 void
172 ReloadPositionProc ()
173 {
174     ReloadPosition(0);
175 }
176
177 void
178 LoadPositionProc() 
179 {
180     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
181         Reset(FALSE, TRUE);
182     }
183     FileNamePopUp(_("Load position file name?"), "", ".fen .epd .pos", LoadPosition, "rb");
184 }
185
186 void
187 SaveGameProc ()
188 {
189     FileNamePopUp(_("Save game file name?"),
190                   DefaultFileName(appData.oldSaveStyle ? "game" : "pgn"),
191                   appData.oldSaveStyle ? ".game" : ".pgn",
192                   SaveGame, "a");
193 }
194
195 void
196 SavePositionProc ()
197 {
198     FileNamePopUp(_("Save position file name?"),
199                   DefaultFileName(appData.oldSaveStyle ? "pos" : "fen"),
200                   appData.oldSaveStyle ? ".pos" : ".fen",
201                   SavePosition, "a");
202 }
203
204 void
205 ReloadCmailMsgProc ()
206 {
207     ReloadCmailMsgEvent(FALSE);
208 }
209
210 void
211 CopyFENToClipboard ()
212 { // wrapper to make call from back-end possible
213   CopyPositionProc();
214 }
215
216 void
217 CopyPositionProc ()
218 {
219     static char *selected_fen_position=NULL;
220     if(gameMode == EditPosition) EditPositionDone(TRUE);
221     if (selected_fen_position) free(selected_fen_position);
222     selected_fen_position = (char *)PositionToFEN(currentMove, NULL);
223     if (!selected_fen_position) return;
224     CopySomething(selected_fen_position);
225 }
226
227 void
228 CopyGameProc ()
229 {
230   int ret;
231
232   ret = SaveGameToFile(gameCopyFilename, FALSE);
233   if (!ret) return;
234
235   CopySomething(NULL);
236 }
237
238 void
239 CopyGameListProc ()
240 {
241   if(!SaveGameListAsText(fopen(gameCopyFilename, "w"))) return;
242   CopySomething(NULL);
243 }
244
245 void
246 AutoSaveGame ()
247 {
248     SaveGameProc();
249 }
250
251
252 void
253 QuitProc ()
254 {
255     ExitEvent(0);
256 }
257
258 void
259 MatchProc ()
260 {
261     MatchEvent(2);
262 }
263
264 void
265 AdjuWhiteProc ()
266 {
267     UserAdjudicationEvent(+1);
268 }
269
270 void
271 AdjuBlackProc ()
272 {
273     UserAdjudicationEvent(-1);
274 }
275
276 void
277 AdjuDrawProc ()
278 {
279     UserAdjudicationEvent(0);
280 }
281
282 void
283 RevertProc ()
284 {
285     RevertEvent(False);
286 }
287
288 void
289 AnnotateProc ()
290 {
291     RevertEvent(True);
292 }
293
294 void
295 FlipViewProc ()
296 {
297     if(twoBoards) { partnerUp = 1; DrawPosition(True, NULL); partnerUp = 0; }
298     flipView = !flipView;
299     DrawPosition(True, NULL);
300 }
301
302 void
303 SaveOnExitProc ()
304 {
305   saveSettingsOnExit = !saveSettingsOnExit;
306
307   MarkMenuItem("Options.SaveSettingsonExit", saveSettingsOnExit);
308 }
309
310 void
311 SaveSettingsProc ()
312 {
313   SaveSettings(settingsFileName);
314 }
315
316 void
317 InfoProc ()
318 {
319     char buf[MSG_SIZ];
320     snprintf(buf, sizeof(buf), "xterm -e info --directory %s --directory . -f %s &",
321             INFODIR, INFOFILE);
322     system(buf);
323 }
324
325 void
326 BugReportProc ()
327 {
328     char buf[MSG_SIZ];
329     snprintf(buf, MSG_SIZ, "%s mailto:bug-xboard@gnu.org", appData.sysOpen);
330     system(buf);
331 }
332
333 void
334 GuideProc ()
335 {
336     char buf[MSG_SIZ];
337     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/user_guide/UserGuide.html", appData.sysOpen);
338     system(buf);
339 }
340
341 void
342 HomePageProc ()
343 {
344     char buf[MSG_SIZ];
345     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/", appData.sysOpen);
346     system(buf);
347 }
348
349 void
350 NewsPageProc ()
351 {
352     char buf[MSG_SIZ];
353     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/whats_new/portal.html", appData.sysOpen);
354     system(buf);
355 }
356
357 void
358 AboutProc ()
359 {
360     char buf[2 * MSG_SIZ];
361 #if ZIPPY
362     char *zippy = _(" (with Zippy code)");
363 #else
364     char *zippy = "";
365 #endif
366     snprintf(buf, sizeof(buf), 
367 _("%s%s\n\n"
368 "Copyright 1991 Digital Equipment Corporation\n"
369 "Enhancements Copyright 1992-2012 Free Software Foundation\n"
370 "Enhancements Copyright 2005 Alessandro Scotti\n\n"
371 "%s is free software and carries NO WARRANTY;"
372 "see the file COPYING for more information.\n\n"
373 "Visit XBoard on the web at: http://www.gnu.org/software/xboard/\n"
374 "Check out the newest features at: http://www.gnu.org/software/xboard/whats_new.html\n\n"
375 "Report bugs via email at: <bug-xboard@gnu.org>\n\n"
376   ),
377             programVersion, zippy, PACKAGE);
378     ErrorPopUp(_("About XBoard"), buf, FALSE);
379 }
380
381 void
382 DebugProc ()
383 {
384     appData.debugMode = !appData.debugMode;
385     if(!strcmp(appData.nameOfDebugFile, "stderr")) return; // stderr is already open, and should never be closed
386     if(!appData.debugMode) fclose(debugFP);
387     else {
388         debugFP = fopen(appData.nameOfDebugFile, "w");
389         if(debugFP == NULL) debugFP = stderr;
390         else setbuf(debugFP, NULL);
391     }
392 }
393
394 void
395 NothingProc ()
396 {
397     return;
398 }
399
400 #ifdef OPTIONSDIALOG
401 #   define MARK_MENU_ITEM(X,Y)
402 #else
403 #   define MARK_MENU_ITEM(X,Y) MarkMenuItem(X, Y)
404 #endif
405
406 void
407 PonderNextMoveProc ()
408 {
409   PonderNextMoveEvent(!appData.ponderNextMove);
410   MARK_MENU_ITEM("Options.PonderNextMove", appData.ponderNextMove);
411 }
412
413 void
414 AlwaysQueenProc ()
415 {
416     appData.alwaysPromoteToQueen = !appData.alwaysPromoteToQueen;
417     MARK_MENU_ITEM("Options.AlwaysQueen", appData.alwaysPromoteToQueen);
418 }
419
420 void
421 AnimateDraggingProc ()
422 {
423     appData.animateDragging = !appData.animateDragging;
424
425     if (appData.animateDragging) CreateAnimVars();
426     MARK_MENU_ITEM("Options.AnimateDragging", appData.animateDragging);
427 }
428
429 void
430 AnimateMovingProc ()
431 {
432     appData.animate = !appData.animate;
433     if (appData.animate) CreateAnimVars();
434     MARK_MENU_ITEM("Options.AnimateMoving", appData.animate);
435 }
436
437 void
438 AutoflagProc ()
439 {
440     appData.autoCallFlag = !appData.autoCallFlag;
441     MARK_MENU_ITEM("Options.AutoFlag", appData.autoCallFlag);
442 }
443
444 void
445 AutoflipProc ()
446 {
447     appData.autoFlipView = !appData.autoFlipView;
448     MARK_MENU_ITEM("Options.AutoFlipView", appData.autoFlipView);
449 }
450
451 void
452 BlindfoldProc ()
453 {
454     appData.blindfold = !appData.blindfold;
455     MARK_MENU_ITEM("Options.Blindfold", appData.blindfold);
456     DrawPosition(True, NULL);
457 }
458
459 void
460 TestLegalityProc ()
461 {
462     appData.testLegality = !appData.testLegality;
463     MARK_MENU_ITEM("Options.TestLegality", appData.testLegality);
464 }
465
466
467 void
468 FlashMovesProc ()
469 {
470     if (appData.flashCount == 0) {
471         appData.flashCount = 3;
472     } else {
473         appData.flashCount = -appData.flashCount;
474     }
475     MARK_MENU_ITEM("Options.FlashMoves", appData.flashCount > 0);
476 }
477
478 #if HIGHDRAG
479 void
480 HighlightDraggingProc ()
481 {
482     appData.highlightDragging = !appData.highlightDragging;
483     MARK_MENU_ITEM("Options.HighlightDragging", appData.highlightDragging);
484 }
485 #endif
486
487 void
488 HighlightLastMoveProc ()
489 {
490     appData.highlightLastMove = !appData.highlightLastMove;
491     MARK_MENU_ITEM("Options.HighlightLastMove", appData.highlightLastMove);
492 }
493
494 void
495 HighlightArrowProc ()
496 {
497     appData.highlightMoveWithArrow = !appData.highlightMoveWithArrow;
498     MARK_MENU_ITEM("Options.HighlightWithArrow", appData.highlightMoveWithArrow);
499 }
500
501 void
502 IcsAlarmProc ()
503 {
504     appData.icsAlarm = !appData.icsAlarm;
505 //    MARK_MENU_ITEM("Options.ICSAlarm", appData.icsAlarm);
506 }
507
508 void
509 MoveSoundProc ()
510 {
511     appData.ringBellAfterMoves = !appData.ringBellAfterMoves;
512     MARK_MENU_ITEM("Options.MoveSound", appData.ringBellAfterMoves);
513 }
514
515 void
516 OneClickProc ()
517 {
518     appData.oneClick = !appData.oneClick;
519     MARK_MENU_ITEM("Options.OneClickMoving", appData.oneClick);
520 }
521
522 void
523 PeriodicUpdatesProc ()
524 {
525     PeriodicUpdatesEvent(!appData.periodicUpdates);
526     MARK_MENU_ITEM("Options.PeriodicUpdates", appData.periodicUpdates);
527 }
528
529 void
530 PopupExitMessageProc ()
531 {
532     appData.popupExitMessage = !appData.popupExitMessage;
533     MARK_MENU_ITEM("Options.PopupExitMessage", appData.popupExitMessage);
534 }
535
536 void
537 PopupMoveErrorsProc ()
538 {
539     appData.popupMoveErrors = !appData.popupMoveErrors;
540     MARK_MENU_ITEM("Options.PopupMoveErrors", appData.popupMoveErrors);
541 }
542
543 void
544 PremoveProc ()
545 {
546     appData.premove = !appData.premove;
547 //    MARK_MENU_ITEM("Options.Premove", appData.premove);
548 }
549
550 void
551 ShowCoordsProc ()
552 {
553     appData.showCoords = !appData.showCoords;
554     MARK_MENU_ITEM("Options.ShowCoords", appData.showCoords);
555     DrawPosition(True, NULL);
556 }
557
558 void
559 ShowThinkingProc ()
560 {
561     appData.showThinking = !appData.showThinking; // [HGM] thinking: taken out of ShowThinkingEvent
562     ShowThinkingEvent();
563 }
564
565 void
566 HideThinkingProc ()
567 {
568   appData.hideThinkingFromHuman = !appData.hideThinkingFromHuman; // [HGM] thinking: taken out of ShowThinkingEvent
569   ShowThinkingEvent();
570
571   MARK_MENU_ITEM("Options.HideThinking", appData.hideThinkingFromHuman);
572 }
573
574 /*
575  *  Menu definition tables
576  */
577
578 MenuItem fileMenu[] = {
579     {N_("New Game        Ctrl+N"),        "NewGame", ResetGameEvent},
580     {N_("New Shuffle Game ..."),          "NewShuffleGame", ShuffleMenuProc},
581     {N_("New Variant ...   Alt+Shift+V"), "NewVariant", NewVariantProc},      // [HGM] variant: not functional yet
582     {"----", NULL, NothingProc},
583     {N_("Load Game       Ctrl+O"),        "LoadGame", LoadGameProc},
584     {N_("Load Position    Ctrl+Shift+O"), "LoadPosition", LoadPositionProc},
585 //    {N_("Load Next Game"), "LoadNextGame", LoadNextGameProc},
586 //    {N_("Load Previous Game"), "LoadPreviousGame", LoadPrevGameProc},
587 //    {N_("Reload Same Game"), "ReloadSameGame", ReloadGameProc},
588     {N_("Next Position     Shift+PgDn"), "LoadNextPosition", LoadNextPositionProc},
589     {N_("Prev Position     Shift+PgUp"), "LoadPreviousPosition", LoadPrevPositionProc},
590     {"----", NULL, NothingProc},
591 //    {N_("Reload Same Position"), "Reload Same Position", ReloadPositionProc},
592     {N_("Save Game       Ctrl+S"),        "SaveGame", SaveGameProc},
593     {N_("Save Position    Ctrl+Shift+S"), "SavePosition", SavePositionProc},
594     {"----", NULL, NothingProc},
595     {N_("Mail Move"),            "MailMove", MailMoveEvent},
596     {N_("Reload CMail Message"), "ReloadCMailMessage", ReloadCmailMsgProc},
597     {"----", NULL, NothingProc},
598     {N_("Quit                 Ctr+Q"), "Quit", QuitProc},
599     {NULL, NULL, NULL}
600 };
601
602 MenuItem editMenu[] = {
603     {N_("Copy Game    Ctrl+C"),        "CopyGame", CopyGameProc},
604     {N_("Copy Position Ctrl+Shift+C"), "CopyPosition", CopyPositionProc},
605     {N_("Copy Game List"),        "CopyGameList", CopyGameListProc},
606     {"----", NULL, NothingProc},
607     {N_("Paste Game    Ctrl+V"),        "PasteGame", PasteGameProc},
608     {N_("Paste Position Ctrl+Shift+V"), "PastePosition", PastePositionProc},
609     {"----", NULL, NothingProc},
610     {N_("Edit Game      Ctrl+E"),        "EditGame", EditGameEvent},
611     {N_("Edit Position   Ctrl+Shift+E"), "EditPosition", EditPositionEvent},
612     {N_("Edit Tags"),                    "EditTags", EditTagsProc},
613     {N_("Edit Comment"),                 "EditComment", EditCommentProc},
614     {N_("Edit Book"),                    "EditBook", EditBookEvent},
615     {"----", NULL, NothingProc},
616     {N_("Revert              Home"), "Revert", RevertProc},
617     {N_("Annotate"),                 "Annotate", AnnotateProc},
618     {N_("Truncate Game  End"),       "TruncateGame", TruncateGameEvent},
619     {"----", NULL, NothingProc},
620     {N_("Backward         Alt+Left"),   "Backward", BackwardEvent},
621     {N_("Forward           Alt+Right"), "Forward", ForwardEvent},
622     {N_("Back to Start     Alt+Home"),  "BacktoStart", ToStartEvent},
623     {N_("Forward to End Alt+End"),      "ForwardtoEnd", ToEndEvent},
624     {NULL, NULL, NULL}
625 };
626
627 MenuItem viewMenu[] = {
628     {N_("Flip View             F2"),         "FlipView", FlipViewProc},
629     {"----", NULL, NothingProc},
630     {N_("Engine Output      Alt+Shift+O"),   "EngineOutput", EngineOutputProc},
631     {N_("Move History       Alt+Shift+H"),   "MoveHistory", HistoryShowProc}, // [HGM] hist: activate 4.2.7 code
632     {N_("Evaluation Graph  Alt+Shift+E"),    "EvaluationGraph", EvalGraphProc},
633     {N_("Game List            Alt+Shift+G"), "GameList", ShowGameListProc},
634     {N_("ICS text menu"), "ICStextmenu", IcsTextProc},
635     {"----", NULL, NothingProc},
636     {N_("Tags"),             "Tags", EditTagsProc},
637     {N_("Comments"),         "Comments", EditCommentProc},
638     {N_("ICS Input Box"),    "ICSInputBox", IcsInputBoxProc},
639     {N_("Open Chat Window"), "OpenChatWindow", ChatProc},
640     {"----", NULL, NothingProc},
641     {N_("Board..."),          "Board", BoardOptionsProc},
642     {N_("Game List Tags..."), "GameListTags", GameListOptionsProc},
643     {NULL, NULL, NULL}
644 };
645
646 MenuItem modeMenu[] = {
647     {N_("Machine White  Ctrl+W"), "MachineWhite", MachineWhiteEvent},
648     {N_("Machine Black  Ctrl+B"), "MachineBlack", MachineBlackEvent},
649     {N_("Two Machines   Ctrl+T"), "TwoMachines", TwoMachinesEvent},
650     {N_("Analysis Mode  Ctrl+A"), "AnalysisMode", (MenuProc*) AnalyzeModeEvent},
651     {N_("Analyze Game   Ctrl+G"), "AnalyzeFile", AnalyzeFileEvent },
652     {N_("Edit Game         Ctrl+E"), "EditGame", EditGameEvent},
653     {N_("Edit Position      Ctrl+Shift+E"), "EditPosition", EditPositionEvent},
654     {N_("Training"),      "Training", TrainingEvent},
655     {N_("ICS Client"),    "ICSClient", IcsClientEvent},
656     {"----", NULL, NothingProc},
657     {N_("Machine Match"),         "MachineMatch", MatchProc},
658     {N_("Pause               Pause"),         "Pause", PauseEvent},
659     {NULL, NULL, NULL}
660 };
661
662 MenuItem actionMenu[] = {
663     {N_("Accept             F3"), "Accept", AcceptEvent},
664     {N_("Decline            F4"), "Decline", DeclineEvent},
665     {N_("Rematch           F12"), "Rematch", RematchEvent},
666     {"----", NULL, NothingProc},
667     {N_("Call Flag          F5"), "CallFlag", CallFlagEvent},
668     {N_("Draw                F6"), "Draw", DrawEvent},
669     {N_("Adjourn            F7"),  "Adjourn", AdjournEvent},
670     {N_("Abort                F8"),"Abort", AbortEvent},
671     {N_("Resign              F9"), "Resign", ResignEvent},
672     {"----", NULL, NothingProc},
673     {N_("Stop Observing  F10"), "StopObserving", StopObservingEvent},
674     {N_("Stop Examining  F11"), "StopExamining", StopExaminingEvent},
675     {N_("Upload to Examine"),   "UploadtoExamine", UploadGameEvent},
676     {"----", NULL, NothingProc},
677     {N_("Adjudicate to White"), "AdjudicatetoWhite", AdjuWhiteProc},
678     {N_("Adjudicate to Black"), "AdjudicatetoBlack", AdjuBlackProc},
679     {N_("Adjudicate Draw"),     "AdjudicateDraw", AdjuDrawProc},
680     {NULL, NULL, NULL}
681 };
682
683 MenuItem engineMenu[] = {
684     {N_("Load New 1st Engine ..."), "LoadNew1stEngine", LoadEngine1Proc},
685     {N_("Load New 2nd Engine ..."), "LoadNew2ndEngine", LoadEngine2Proc},
686     {"----", NULL, NothingProc},
687     {N_("Engine #1 Settings ..."), "Engine#1Settings", FirstSettingsProc},
688     {N_("Engine #2 Settings ..."), "Engine#2Settings", SecondSettingsProc},
689     {"----", NULL, NothingProc},
690     {N_("Hint"), "Hint", HintEvent},
691     {N_("Book"), "Book", BookEvent},
692     {"----", NULL, NothingProc},
693     {N_("Move Now     Ctrl+M"),     "MoveNow", MoveNowEvent},
694     {N_("Retract Move  Ctrl+X"), "RetractMove", RetractMoveEvent},
695     {NULL, NULL, NULL}
696 };
697
698 MenuItem optionsMenu[] = {
699 #ifdef OPTIONSDIALOG
700     {N_("General ..."), "General", OptionsProc},
701 #endif
702     {N_("Time Control ...       Alt+Shift+T"), "TimeControl", TimeControlProc},
703     {N_("Common Engine ...  Alt+Shift+U"),     "CommonEngine", UciMenuProc},
704     {N_("Adjudications ...      Alt+Shift+J"), "Adjudications", EngineMenuProc},
705     {N_("ICS ..."),    "ICS", IcsOptionsProc},
706     {N_("Match ..."), "Match", MatchOptionsProc},
707     {N_("Load Game ..."),    "LoadGame", LoadOptionsProc},
708     {N_("Save Game ..."),    "SaveGame", SaveOptionsProc},
709 //    {N_(" ..."),    "", OptionsProc},
710     {N_("Game List ..."),    "GameList", GameListOptionsProc},
711     {N_("Sounds ..."),    "Sounds", SoundOptionsProc},
712     {"----", NULL, NothingProc},
713 #ifndef OPTIONSDIALOG
714     {N_("Always Queen        Ctrl+Shift+Q"),   "AlwaysQueen", AlwaysQueenProc},
715     {N_("Animate Dragging"), "AnimateDragging", AnimateDraggingProc},
716     {N_("Animate Moving      Ctrl+Shift+A"),   "AnimateMoving", AnimateMovingProc},
717     {N_("Auto Flag               Ctrl+Shift+F"), "AutoFlag", AutoflagProc},
718     {N_("Auto Flip View"),   "AutoFlipView", AutoflipProc},
719     {N_("Blindfold"),        "Blindfold", BlindfoldProc},
720     {N_("Flash Moves"),      "FlashMoves", FlashMovesProc},
721 #if HIGHDRAG
722     {N_("Highlight Dragging"),    "HighlightDragging", HighlightDraggingProc},
723 #endif
724     {N_("Highlight Last Move"),   "HighlightLastMove", HighlightLastMoveProc},
725     {N_("Highlight With Arrow"),  "HighlightWithArrow", HighlightArrowProc},
726     {N_("Move Sound"),            "MoveSound", MoveSoundProc},
727 //    {N_("ICS Alarm"),             "ICSAlarm", IcsAlarmProc},
728     {N_("One-Click Moving"),      "OneClickMoving", OneClickProc},
729     {N_("Periodic Updates"),      "PeriodicUpdates", PeriodicUpdatesProc},
730     {N_("Ponder Next Move  Ctrl+Shift+P"), "PonderNextMove", PonderNextMoveProc},
731     {N_("Popup Exit Message"),    "PopupExitMessage", PopupExitMessageProc},
732     {N_("Popup Move Errors"),     "PopupMoveErrors", PopupMoveErrorsProc},
733 //    {N_("Premove"),               "Premove", PremoveProc},
734     {N_("Show Coords"),           "ShowCoords", ShowCoordsProc},
735     {N_("Hide Thinking        Ctrl+Shift+H"),   "HideThinking", HideThinkingProc},
736     {N_("Test Legality          Ctrl+Shift+L"), "TestLegality", TestLegalityProc},
737     {"----", NULL, NothingProc},
738 #endif
739     {N_("Save Settings Now"),     "SaveSettingsNow", SaveSettingsProc},
740     {N_("Save Settings on Exit"), "SaveSettingsonExit", SaveOnExitProc},
741     {NULL, NULL, NULL}
742 };
743
744 MenuItem helpMenu[] = {
745     {N_("Info XBoard"),     "InfoXBoard", InfoProc},
746     {N_("Man XBoard   F1"), "ManXBoard", ManProc},
747     {"----", NULL, NothingProc},
748     {N_("XBoard Home Page"), "XBoardHomePage", HomePageProc},
749     {N_("On-line User Guide"), "On-lineUserGuide", GuideProc},
750     {N_("Development News"), "DevelopmentNews", NewsPageProc},
751     {N_("e-Mail Bug Report"), "e-MailBugReport", BugReportProc},
752     {"----", NULL, NothingProc},
753     {N_("About XBoard"), "AboutXBoard", AboutProc},
754     {NULL, NULL, NULL}
755 };
756
757 MenuItem noMenu[] = {
758     { "", "LoadNextGame", LoadNextGameProc },
759     { "", "LoadPrevGame", LoadPrevGameProc },
760     { "", "ReloadGame", ReloadGameProc },
761     { "", "ReloadPosition", ReloadPositionProc },
762 #ifndef OPTIONSDIALOG
763     { "", "AlwaysQueen", AlwaysQueenProc },
764     { "", "AnimateDragging", AnimateDraggingProc },
765     { "", "AnimateMoving", AnimateMovingProc },
766     { "", "Autoflag", AutoflagProc },
767     { "", "Autoflip", AutoflipProc },
768     { "", "Blindfold", BlindfoldProc },
769     { "", "FlashMoves", FlashMovesProc },
770 #if HIGHDRAG
771     { "", "HighlightDragging", HighlightDraggingProc },
772 #endif
773     { "", "HighlightLastMove", HighlightLastMoveProc },
774 //    { "", "IcsAlarm", IcsAlarmProc },
775     { "", "MoveSound", MoveSoundProc },
776     { "", "PeriodicUpdates", PeriodicUpdatesProc },
777     { "", "PopupExitMessage", PopupExitMessageProc },
778     { "", "PopupMoveErrors", PopupMoveErrorsProc },
779 //    { "", "Premove", PremoveProc },
780     { "", "ShowCoords", ShowCoordsProc },
781     { "", "ShowThinking", ShowThinkingProc },
782     { "", "HideThinking", HideThinkingProc },
783     { "", "TestLegality", TestLegalityProc },
784 #endif
785     { "", "AboutGame", AboutGameEvent },
786     { "", "DebugProc", DebugProc },
787     { "", "Nothing", NothingProc },
788     {NULL, NULL, NULL}
789 };
790
791 Menu menuBar[] = {
792     {N_("File"),    "File", fileMenu},
793     {N_("Edit"),    "Edit", editMenu},
794     {N_("View"),    "View", viewMenu},
795     {N_("Mode"),    "Mode", modeMenu},
796     {N_("Action"),  "Action", actionMenu},
797     {N_("Engine"),  "Engine", engineMenu},
798     {N_("Options"), "Options", optionsMenu},
799     {N_("Help"),    "Help", helpMenu},
800     {NULL, NULL, NULL}
801 };
802
803 MenuItem *
804 MenuNameToItem (char *menuName)
805 {
806     int i=0;
807     char buf[MSG_SIZ], *p;
808     MenuItem *menuTab;
809     static MenuItem a = { NULL, NULL, NothingProc };
810     extern Option mainOptions[];
811     safeStrCpy(buf, menuName, MSG_SIZ);
812     p = strchr(buf, '.');
813     if(!p) menuTab = noMenu, p = menuName; else {
814         *p++ = NULLCHAR;
815         for(i=0; menuBar[i].name; i++)
816             if(!strcmp(buf, menuBar[i].name)) break;
817         if(!menuBar[i].name) return NULL; // main menu not found
818         menuTab = menuBar[i].mi;
819     }
820     if(*p == NULLCHAR) { a.handle = mainOptions[i+1].handle; return &a; } // main menu bar
821     for(i=0; menuTab[i].string; i++)
822         if(menuTab[i].ref && !strcmp(p, menuTab[i].ref)) return menuTab + i;
823     return NULL; // item not found
824 }
825
826 void
827 AppendEnginesToMenu (char *list)
828 {
829     int i=0;
830     char *p;
831     if(appData.icsActive || appData.recentEngines <= 0) return;
832     recentEngines = strdup(list);
833     while (*list) {
834         p = strchr(list, '\n'); if(p == NULL) break;
835         if(i == 0) AppendMenuItem("----", 0); // at least one valid item to add
836         *p = 0;
837         AppendMenuItem(list, i);
838         i++; *p = '\n'; list = p + 1;
839     }
840 }
841
842 Enables icsEnables[] = {
843     { "File.MailMove", False },
844     { "File.ReloadCMailMessage", False },
845     { "Mode.MachineBlack", False },
846     { "Mode.MachineWhite", False },
847     { "Mode.AnalysisMode", False },
848     { "Mode.AnalyzeFile", False },
849     { "Mode.TwoMachines", False },
850     { "Mode.MachineMatch", False },
851 #ifndef ZIPPY
852     { "Engine.Hint", False },
853     { "Engine.Book", False },
854     { "Engine.MoveNow", False },
855 #ifndef OPTIONSDIALOG
856     { "PeriodicUpdates", False },
857     { "HideThinking", False },
858     { "PonderNextMove", False },
859 #endif
860 #endif
861     { "Engine.Engine#1Settings", False },
862     { "Engine.Engine#2Settings", False },
863     { "Engine.Load1stEngine", False },
864     { "Engine.Load2ndEngine", False },
865     { "Edit.Annotate", False },
866     { "Options.Match", False },
867     { NULL, False }
868 };
869
870 Enables ncpEnables[] = {
871     { "File.MailMove", False },
872     { "File.ReloadCMailMessage", False },
873     { "Mode.MachineWhite", False },
874     { "Mode.MachineBlack", False },
875     { "Mode.AnalysisMode", False },
876     { "Mode.AnalyzeFile", False },
877     { "Mode.TwoMachines", False },
878     { "Mode.MachineMatch", False },
879     { "Mode.ICSClient", False },
880     { "View.ICStextmenu", False },
881     { "View.ICSInputBox", False },
882     { "View.OpenChatWindow", False },
883     { "Action.", False },
884     { "Edit.Revert", False },
885     { "Edit.Annotate", False },
886     { "Engine.Engine#1Settings", False },
887     { "Engine.Engine#2Settings", False },
888     { "Engine.MoveNow", False },
889     { "Engine.RetractMove", False },
890     { "Options.ICS", False },
891 #ifndef OPTIONSDIALOG
892     { "Options.AutoFlag", False },
893     { "Options.AutoFlip View", False },
894 //    { "Options.ICSAlarm", False },
895     { "Options.MoveSound", False },
896     { "Options.HideThinking", False },
897     { "Options.PeriodicUpdates", False },
898     { "Options.PonderNextMove", False },
899 #endif
900     { "Engine.Hint", False },
901     { "Engine.Book", False },
902     { NULL, False }
903 };
904
905 Enables gnuEnables[] = {
906     { "Mode.ICSClient", False },
907     { "View.ICStextmenu", False },
908     { "View.ICSInputBox", False },
909     { "View.OpenChatWindow", False },
910     { "Action.Accept", False },
911     { "Action.Decline", False },
912     { "Action.Rematch", False },
913     { "Action.Adjourn", False },
914     { "Action.StopExamining", False },
915     { "Action.StopObserving", False },
916     { "Action.UploadtoExamine", False },
917     { "Edit.Revert", False },
918     { "Edit.Annotate", False },
919     { "Options.ICS", False },
920
921     /* The next two options rely on SetCmailMode being called *after*    */
922     /* SetGNUMode so that when GNU is being used to give hints these     */
923     /* menu options are still available                                  */
924
925     { "File.MailMove", False },
926     { "File.ReloadCMailMessage", False },
927     // [HGM] The following have been added to make a switch from ncp to GNU mode possible
928     { "Mode.MachineWhite", True },
929     { "Mode.MachineBlack", True },
930     { "Mode.AnalysisMode", True },
931     { "Mode.AnalyzeFile", True },
932     { "Mode.TwoMachines", True },
933     { "Mode.MachineMatch", True },
934     { "Engine.Engine#1Settings", True },
935     { "Engine.Engine#2Settings", True },
936     { "Engine.Hint", True },
937     { "Engine.Book", True },
938     { "Engine.MoveNow", True },
939     { "Engine.RetractMove", True },
940     { "Action.", True },
941     { NULL, False }
942 };
943
944 Enables cmailEnables[] = {
945     { "Action.", True },
946     { "Action.CallFlag", False },
947     { "Action.Draw", True },
948     { "Action.Adjourn", False },
949     { "Action.Abort", False },
950     { "Action.StopObserving", False },
951     { "Action.StopExamining", False },
952     { "File.MailMove", True },
953     { "File.ReloadCMailMessage", True },
954     { NULL, False }
955 };
956
957 Enables trainingOnEnables[] = {
958   { "Edit.EditComment", False },
959   { "Mode.Pause", False },
960   { "Edit.Forward", False },
961   { "Edit.Backward", False },
962   { "Edit.ForwardtoEnd", False },
963   { "Edit.BacktoStart", False },
964   { "Engine.MoveNow", False },
965   { "Edit.TruncateGame", False },
966   { NULL, False }
967 };
968
969 Enables trainingOffEnables[] = {
970   { "Edit.EditComment", True },
971   { "Mode.Pause", True },
972   { "Edit.Forward", True },
973   { "Edit.Backward", True },
974   { "Edit.ForwardtoEnd", True },
975   { "Edit.BacktoStart", True },
976   { "Engine.MoveNow", True },
977   { "Engine.TruncateGame", True },
978   { NULL, False }
979 };
980
981 Enables machineThinkingEnables[] = {
982   { "File.LoadGame", False },
983 //  { "LoadNextGame", False },
984 //  { "LoadPreviousGame", False },
985 //  { "ReloadSameGame", False },
986   { "Edit.PasteGame", False },
987   { "File.LoadPosition", False },
988 //  { "LoadNextPosition", False },
989 //  { "LoadPreviousPosition", False },
990 //  { "ReloadSamePosition", False },
991   { "Edit.PastePosition", False },
992   { "Mode.MachineWhite", False },
993   { "Mode.MachineBlack", False },
994   { "Mode.TwoMachines", False },
995 //  { "MachineMatch", False },
996   { "Engine.RetractMove", False },
997   { NULL, False }
998 };
999
1000 Enables userThinkingEnables[] = {
1001   { "File.LoadGame", True },
1002 //  { "LoadNextGame", True },
1003 //  { "LoadPreviousGame", True },
1004 //  { "ReloadSameGame", True },
1005   { "Edit.PasteGame", True },
1006   { "File.LoadPosition", True },
1007 //  { "LoadNextPosition", True },
1008 //  { "LoadPreviousPosition", True },
1009 //  { "ReloadSamePosition", True },
1010   { "Edit.PastePosition", True },
1011   { "Mode.MachineWhite", True },
1012   { "Mode.MachineBlack", True },
1013   { "Mode.TwoMachines", True },
1014 //  { "MachineMatch", True },
1015   { "Engine.RetractMove", True },
1016   { NULL, False }
1017 };
1018
1019 void
1020 SetICSMode ()
1021 {
1022   SetMenuEnables(icsEnables);
1023
1024 #if ZIPPY
1025   if (appData.zippyPlay && !appData.noChessProgram) { /* [DM] icsEngineAnalyze */
1026      EnableNamedMenuItem("Mode.AnalysisMode", True);
1027      EnableNamedMenuItem("Engine.Engine#1Settings", True);
1028   }
1029 #endif
1030 }
1031
1032 void
1033 SetNCPMode ()
1034 {
1035   SetMenuEnables(ncpEnables);
1036 }
1037
1038 void
1039 SetGNUMode ()
1040 {
1041   SetMenuEnables(gnuEnables);
1042 }
1043
1044 void
1045 SetCmailMode ()
1046 {
1047   SetMenuEnables(cmailEnables);
1048 }
1049
1050 void
1051 SetTrainingModeOn ()
1052 {
1053   SetMenuEnables(trainingOnEnables);
1054   if (appData.showButtonBar) {
1055     EnableButtonBar(False);
1056   }
1057   CommentPopDown();
1058 }
1059
1060 void
1061 SetTrainingModeOff ()
1062 {
1063   SetMenuEnables(trainingOffEnables);
1064   if (appData.showButtonBar) {
1065     EnableButtonBar(True);
1066   }
1067 }
1068
1069 void
1070 SetUserThinkingEnables ()
1071 {
1072   if (appData.noChessProgram) return;
1073   SetMenuEnables(userThinkingEnables);
1074 }
1075
1076 void
1077 SetMachineThinkingEnables ()
1078 {
1079   if (appData.noChessProgram) return;
1080   SetMenuEnables(machineThinkingEnables);
1081   switch (gameMode) {
1082   case MachinePlaysBlack:
1083   case MachinePlaysWhite:
1084   case TwoMachinesPlay:
1085     EnableNamedMenuItem(ModeToWidgetName(gameMode), True);
1086     break;
1087   default:
1088     break;
1089   }
1090 }
1091
1092 void
1093 GreyRevert (Boolean grey)
1094 {
1095     MarkMenuItem("Edit.Revert", !grey);
1096     MarkMenuItem("Edit.Annotate", !grey);
1097 }
1098
1099 char *
1100 ModeToWidgetName (GameMode mode)
1101 {
1102     switch (mode) {
1103       case BeginningOfGame:
1104         if (appData.icsActive)
1105           return "Mode.ICSClient";
1106         else if (appData.noChessProgram ||
1107                  *appData.cmailGameName != NULLCHAR)
1108           return "Mode.EditGame";
1109         else
1110           return "Mode.MachineBlack";
1111       case MachinePlaysBlack:
1112         return "Mode.MachineBlack";
1113       case MachinePlaysWhite:
1114         return "Mode.MachineWhite";
1115       case AnalyzeMode:
1116         return "Mode.AnalysisMode";
1117       case AnalyzeFile:
1118         return "Mode.AnalyzeFile";
1119       case TwoMachinesPlay:
1120         return "Mode.TwoMachines";
1121       case EditGame:
1122         return "Mode.EditGame";
1123       case PlayFromGameFile:
1124         return "File.LoadGame";
1125       case EditPosition:
1126         return "Mode.EditPosition";
1127       case Training:
1128         return "Mode.Training";
1129       case IcsPlayingWhite:
1130       case IcsPlayingBlack:
1131       case IcsObserving:
1132       case IcsIdle:
1133       case IcsExamining:
1134         return "Mode.ICSClient";
1135       default:
1136       case EndOfGame:
1137         return NULL;
1138     }
1139 }
1140
1141 void
1142 InitMenuMarkers()
1143 {
1144 #ifndef OPTIONSDIALOG
1145     if (appData.alwaysPromoteToQueen) {
1146         MarkMenuItem("Options.Always Queen", True);
1147     }
1148     if (appData.animateDragging) {
1149         MarkMenuItem("Options.Animate Dragging", True);
1150     }
1151     if (appData.animate) {
1152         MarkMenuItem("Options.Animate Moving", True);
1153     }
1154     if (appData.autoCallFlag) {
1155         MarkMenuItem("Options.Auto Flag", True);
1156     }
1157     if (appData.autoFlipView) {
1158         XtSetValues(XtNameToWidget(menuBarWidget,"Options.Auto Flip View", True);
1159     }
1160     if (appData.blindfold) {
1161         MarkMenuItem("Options.Blindfold", True);
1162     }
1163     if (appData.flashCount > 0) {
1164         MarkMenuItem("Options.Flash Moves", True);
1165     }
1166 #if HIGHDRAG
1167     if (appData.highlightDragging) {
1168         MarkMenuItem("Options.Highlight Dragging", True);
1169     }
1170 #endif
1171     if (appData.highlightLastMove) {
1172         MarkMenuItem("Options.Highlight Last Move", True);
1173     }
1174     if (appData.highlightMoveWithArrow) {
1175         MarkMenuItem("Options.Arrow", True);
1176     }
1177 //    if (appData.icsAlarm) {
1178 //      MarkMenuItem("Options.ICS Alarm", True);
1179 //    }
1180     if (appData.ringBellAfterMoves) {
1181         MarkMenuItem("Options.Move Sound", True);
1182     }
1183     if (appData.oneClick) {
1184         MarkMenuItem("Options.OneClick", True);
1185     }
1186     if (appData.periodicUpdates) {
1187         MarkMenuItem("Options.Periodic Updates", True);
1188     }
1189     if (appData.ponderNextMove) {
1190         MarkMenuItem("Options.Ponder Next Move", True);
1191     }
1192     if (appData.popupExitMessage) {
1193         MarkMenuItem("Options.Popup Exit Message", True);
1194     }
1195     if (appData.popupMoveErrors) {
1196         MarkMenuItem("Options.Popup Move Errors", True);
1197     }
1198 //    if (appData.premove) {
1199 //      MarkMenuItem("Options.Premove", True);
1200 //    }
1201     if (appData.showCoords) {
1202         MarkMenuItem("Options.Show Coords", True);
1203     }
1204     if (appData.hideThinkingFromHuman) {
1205         MarkMenuItem("Options.Hide Thinking", True);
1206     }
1207     if (appData.testLegality) {
1208         MarkMenuItem("Options.Test Legality", True);
1209     }
1210 #endif
1211     if (saveSettingsOnExit) {
1212         MarkMenuItem("Options.SaveSettingsonExit", True);
1213     }
1214 }
1215
1216