Fix key binding of DebugProc
[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 AnalyzeModeProc ()
260 {
261     char buf[MSG_SIZ];
262
263     if (!first.analysisSupport) {
264       snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
265       DisplayError(buf, 0);
266       return;
267     }
268     /* [DM] icsEngineAnalyze [HGM] This is horrible code; reverse the gameMode and isEngineAnalyze tests! */
269     if (appData.icsActive) {
270         if (gameMode != IcsObserving) {
271           snprintf(buf, MSG_SIZ, _("You are not observing a game"));
272             DisplayError(buf, 0);
273             /* secure check */
274             if (appData.icsEngineAnalyze) {
275                 if (appData.debugMode)
276                     fprintf(debugFP, _("Found unexpected active ICS engine analyze \n"));
277                 ExitAnalyzeMode();
278                 ModeHighlight();
279             }
280             return;
281         }
282         /* if enable, use want disable icsEngineAnalyze */
283         if (appData.icsEngineAnalyze) {
284                 ExitAnalyzeMode();
285                 ModeHighlight();
286                 return;
287         }
288         appData.icsEngineAnalyze = TRUE;
289         if (appData.debugMode)
290             fprintf(debugFP, _("ICS engine analyze starting... \n"));
291     }
292 #ifndef OPTIONSDIALOG
293     if (!appData.showThinking)
294       ShowThinkingProc();
295 #endif
296
297     AnalyzeModeEvent();
298 }
299
300 void
301 AnalyzeFileProc ()
302 {
303     if (!first.analysisSupport) {
304       char buf[MSG_SIZ];
305       snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
306       DisplayError(buf, 0);
307       return;
308     }
309 //    Reset(FALSE, TRUE);
310 #ifndef OPTIONSDIALOG
311     if (!appData.showThinking)
312       ShowThinkingProc();
313 #endif
314     AnalyzeFileEvent();
315 //    FileNamePopUp(_("File to analyze"), "", ".pgn .game", LoadGamePopUp, "rb");
316     AnalysisPeriodicEvent(1);
317 }
318
319 void
320 MatchProc ()
321 {
322     MatchEvent(2);
323 }
324
325 void
326 AdjuWhiteProc ()
327 {
328     UserAdjudicationEvent(+1);
329 }
330
331 void
332 AdjuBlackProc ()
333 {
334     UserAdjudicationEvent(-1);
335 }
336
337 void
338 AdjuDrawProc ()
339 {
340     UserAdjudicationEvent(0);
341 }
342
343 void
344 RevertProc ()
345 {
346     RevertEvent(False);
347 }
348
349 void
350 AnnotateProc ()
351 {
352     RevertEvent(True);
353 }
354
355 void
356 FlipViewProc ()
357 {
358     if(twoBoards) { partnerUp = 1; DrawPosition(True, NULL); partnerUp = 0; }
359     flipView = !flipView;
360     DrawPosition(True, NULL);
361 }
362
363 void
364 SaveOnExitProc ()
365 {
366   saveSettingsOnExit = !saveSettingsOnExit;
367
368   MarkMenuItem("Options.SaveSettingsonExit", saveSettingsOnExit);
369 }
370
371 void
372 SaveSettingsProc ()
373 {
374   SaveSettings(settingsFileName);
375 }
376
377 void
378 InfoProc ()
379 {
380     char buf[MSG_SIZ];
381     snprintf(buf, sizeof(buf), "xterm -e info --directory %s --directory . -f %s &",
382             INFODIR, INFOFILE);
383     system(buf);
384 }
385
386 void
387 BugReportProc ()
388 {
389     char buf[MSG_SIZ];
390     snprintf(buf, MSG_SIZ, "%s mailto:bug-xboard@gnu.org", appData.sysOpen);
391     system(buf);
392 }
393
394 void
395 GuideProc ()
396 {
397     char buf[MSG_SIZ];
398     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/user_guide/UserGuide.html", appData.sysOpen);
399     system(buf);
400 }
401
402 void
403 HomePageProc ()
404 {
405     char buf[MSG_SIZ];
406     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/", appData.sysOpen);
407     system(buf);
408 }
409
410 void
411 NewsPageProc ()
412 {
413     char buf[MSG_SIZ];
414     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/whats_new/portal.html", appData.sysOpen);
415     system(buf);
416 }
417
418 void
419 AboutProc ()
420 {
421     char buf[2 * MSG_SIZ];
422 #if ZIPPY
423     char *zippy = _(" (with Zippy code)");
424 #else
425     char *zippy = "";
426 #endif
427     snprintf(buf, sizeof(buf), 
428 _("%s%s\n\n"
429 "Copyright 1991 Digital Equipment Corporation\n"
430 "Enhancements Copyright 1992-2012 Free Software Foundation\n"
431 "Enhancements Copyright 2005 Alessandro Scotti\n\n"
432 "%s is free software and carries NO WARRANTY;"
433 "see the file COPYING for more information.\n\n"
434 "Visit XBoard on the web at: http://www.gnu.org/software/xboard/\n"
435 "Check out the newest features at: http://www.gnu.org/software/xboard/whats_new.html\n\n"
436 "Report bugs via email at: <bug-xboard@gnu.org>\n\n"
437   ),
438             programVersion, zippy, PACKAGE);
439     ErrorPopUp(_("About XBoard"), buf, FALSE);
440 }
441
442 void
443 DebugProc ()
444 {
445     appData.debugMode = !appData.debugMode;
446     if(!strcmp(appData.nameOfDebugFile, "stderr")) return; // stderr is already open, and should never be closed
447     if(!appData.debugMode) fclose(debugFP);
448     else {
449         debugFP = fopen(appData.nameOfDebugFile, "w");
450         if(debugFP == NULL) debugFP = stderr;
451         else setbuf(debugFP, NULL);
452     }
453 }
454
455 void
456 NothingProc ()
457 {
458     return;
459 }
460
461 #ifdef OPTIONSDIALOG
462 #   define MARK_MENU_ITEM(X,Y)
463 #else
464 #   define MARK_MENU_ITEM(X,Y) MarkMenuItem(X, Y)
465 #endif
466
467 void
468 PonderNextMoveProc ()
469 {
470   PonderNextMoveEvent(!appData.ponderNextMove);
471   MARK_MENU_ITEM("Options.PonderNextMove", appData.ponderNextMove);
472 }
473
474 void
475 AlwaysQueenProc ()
476 {
477     appData.alwaysPromoteToQueen = !appData.alwaysPromoteToQueen;
478     MARK_MENU_ITEM("Options.AlwaysQueen", appData.alwaysPromoteToQueen);
479 }
480
481 void
482 AnimateDraggingProc ()
483 {
484     appData.animateDragging = !appData.animateDragging;
485
486     if (appData.animateDragging) CreateAnimVars();
487     MARK_MENU_ITEM("Options.AnimateDragging", appData.animateDragging);
488 }
489
490 void
491 AnimateMovingProc ()
492 {
493     appData.animate = !appData.animate;
494     if (appData.animate) CreateAnimVars();
495     MARK_MENU_ITEM("Options.AnimateMoving", appData.animate);
496 }
497
498 void
499 AutoflagProc ()
500 {
501     appData.autoCallFlag = !appData.autoCallFlag;
502     MARK_MENU_ITEM("Options.AutoFlag", appData.autoCallFlag);
503 }
504
505 void
506 AutoflipProc ()
507 {
508     appData.autoFlipView = !appData.autoFlipView;
509     MARK_MENU_ITEM("Options.AutoFlipView", appData.autoFlipView);
510 }
511
512 void
513 BlindfoldProc ()
514 {
515     appData.blindfold = !appData.blindfold;
516     MARK_MENU_ITEM("Options.Blindfold", appData.blindfold);
517     DrawPosition(True, NULL);
518 }
519
520 void
521 TestLegalityProc ()
522 {
523     appData.testLegality = !appData.testLegality;
524     MARK_MENU_ITEM("Options.TestLegality", appData.testLegality);
525 }
526
527
528 void
529 FlashMovesProc ()
530 {
531     if (appData.flashCount == 0) {
532         appData.flashCount = 3;
533     } else {
534         appData.flashCount = -appData.flashCount;
535     }
536     MARK_MENU_ITEM("Options.FlashMoves", appData.flashCount > 0);
537 }
538
539 #if HIGHDRAG
540 void
541 HighlightDraggingProc ()
542 {
543     appData.highlightDragging = !appData.highlightDragging;
544     MARK_MENU_ITEM("Options.HighlightDragging", appData.highlightDragging);
545 }
546 #endif
547
548 void
549 HighlightLastMoveProc ()
550 {
551     appData.highlightLastMove = !appData.highlightLastMove;
552     MARK_MENU_ITEM("Options.HighlightLastMove", appData.highlightLastMove);
553 }
554
555 void
556 HighlightArrowProc ()
557 {
558     appData.highlightMoveWithArrow = !appData.highlightMoveWithArrow;
559     MARK_MENU_ITEM("Options.HighlightWithArrow", appData.highlightMoveWithArrow);
560 }
561
562 void
563 IcsAlarmProc ()
564 {
565     appData.icsAlarm = !appData.icsAlarm;
566 //    MARK_MENU_ITEM("Options.ICSAlarm", appData.icsAlarm);
567 }
568
569 void
570 MoveSoundProc ()
571 {
572     appData.ringBellAfterMoves = !appData.ringBellAfterMoves;
573     MARK_MENU_ITEM("Options.MoveSound", appData.ringBellAfterMoves);
574 }
575
576 void
577 OneClickProc ()
578 {
579     appData.oneClick = !appData.oneClick;
580     MARK_MENU_ITEM("Options.OneClickMoving", appData.oneClick);
581 }
582
583 void
584 PeriodicUpdatesProc ()
585 {
586     PeriodicUpdatesEvent(!appData.periodicUpdates);
587     MARK_MENU_ITEM("Options.PeriodicUpdates", appData.periodicUpdates);
588 }
589
590 void
591 PopupExitMessageProc ()
592 {
593     appData.popupExitMessage = !appData.popupExitMessage;
594     MARK_MENU_ITEM("Options.PopupExitMessage", appData.popupExitMessage);
595 }
596
597 void
598 PopupMoveErrorsProc ()
599 {
600     appData.popupMoveErrors = !appData.popupMoveErrors;
601     MARK_MENU_ITEM("Options.PopupMoveErrors", appData.popupMoveErrors);
602 }
603
604 void
605 PremoveProc ()
606 {
607     appData.premove = !appData.premove;
608 //    MARK_MENU_ITEM("Options.Premove", appData.premove);
609 }
610
611 void
612 ShowCoordsProc ()
613 {
614     appData.showCoords = !appData.showCoords;
615     MARK_MENU_ITEM("Options.ShowCoords", appData.showCoords);
616     DrawPosition(True, NULL);
617 }
618
619 void
620 ShowThinkingProc ()
621 {
622     appData.showThinking = !appData.showThinking; // [HGM] thinking: taken out of ShowThinkingEvent
623     ShowThinkingEvent();
624 }
625
626 void
627 HideThinkingProc ()
628 {
629   appData.hideThinkingFromHuman = !appData.hideThinkingFromHuman; // [HGM] thinking: taken out of ShowThinkingEvent
630   ShowThinkingEvent();
631
632   MARK_MENU_ITEM("Options.HideThinking", appData.hideThinkingFromHuman);
633 }
634
635 /*
636  *  Menu definition tables
637  */
638
639 MenuItem fileMenu[] = {
640     {N_("New Game        Ctrl+N"),        "NewGame", ResetGameEvent},
641     {N_("New Shuffle Game ..."),          "NewShuffleGame", ShuffleMenuProc},
642     {N_("New Variant ...   Alt+Shift+V"), "NewVariant", NewVariantProc},      // [HGM] variant: not functional yet
643     {"----", NULL, NothingProc},
644     {N_("Load Game       Ctrl+O"),        "LoadGame", LoadGameProc},
645     {N_("Load Position    Ctrl+Shift+O"), "LoadPosition", LoadPositionProc},
646 //    {N_("Load Next Game"), "LoadNextGame", LoadNextGameProc},
647 //    {N_("Load Previous Game"), "LoadPreviousGame", LoadPrevGameProc},
648 //    {N_("Reload Same Game"), "ReloadSameGame", ReloadGameProc},
649     {N_("Next Position     Shift+PgDn"), "LoadNextPosition", LoadNextPositionProc},
650     {N_("Prev Position     Shift+PgUp"), "LoadPreviousPosition", LoadPrevPositionProc},
651     {"----", NULL, NothingProc},
652 //    {N_("Reload Same Position"), "Reload Same Position", ReloadPositionProc},
653     {N_("Save Game       Ctrl+S"),        "SaveGame", SaveGameProc},
654     {N_("Save Position    Ctrl+Shift+S"), "SavePosition", SavePositionProc},
655     {"----", NULL, NothingProc},
656     {N_("Mail Move"),            "MailMove", MailMoveEvent},
657     {N_("Reload CMail Message"), "ReloadCMailMessage", ReloadCmailMsgProc},
658     {"----", NULL, NothingProc},
659     {N_("Quit                 Ctr+Q"), "Quit", QuitProc},
660     {NULL, NULL, NULL}
661 };
662
663 MenuItem editMenu[] = {
664     {N_("Copy Game    Ctrl+C"),        "CopyGame", CopyGameProc},
665     {N_("Copy Position Ctrl+Shift+C"), "CopyPosition", CopyPositionProc},
666     {N_("Copy Game List"),        "CopyGameList", CopyGameListProc},
667     {"----", NULL, NothingProc},
668     {N_("Paste Game    Ctrl+V"),        "PasteGame", PasteGameProc},
669     {N_("Paste Position Ctrl+Shift+V"), "PastePosition", PastePositionProc},
670     {"----", NULL, NothingProc},
671     {N_("Edit Game      Ctrl+E"),        "EditGame", EditGameEvent},
672     {N_("Edit Position   Ctrl+Shift+E"), "EditPosition", EditPositionEvent},
673     {N_("Edit Tags"),                    "EditTags", EditTagsProc},
674     {N_("Edit Comment"),                 "EditComment", EditCommentProc},
675     {N_("Edit Book"),                    "EditBook", EditBookEvent},
676     {"----", NULL, NothingProc},
677     {N_("Revert              Home"), "Revert", RevertProc},
678     {N_("Annotate"),                 "Annotate", AnnotateProc},
679     {N_("Truncate Game  End"),       "TruncateGame", TruncateGameEvent},
680     {"----", NULL, NothingProc},
681     {N_("Backward         Alt+Left"),   "Backward", BackwardEvent},
682     {N_("Forward           Alt+Right"), "Forward", ForwardEvent},
683     {N_("Back to Start     Alt+Home"),  "BacktoStart", ToStartEvent},
684     {N_("Forward to End Alt+End"),      "ForwardtoEnd", ToEndEvent},
685     {NULL, NULL, NULL}
686 };
687
688 MenuItem viewMenu[] = {
689     {N_("Flip View             F2"),         "FlipView", FlipViewProc},
690     {"----", NULL, NothingProc},
691     {N_("Engine Output      Alt+Shift+O"),   "EngineOutput", EngineOutputProc},
692     {N_("Move History       Alt+Shift+H"),   "MoveHistory", HistoryShowProc}, // [HGM] hist: activate 4.2.7 code
693     {N_("Evaluation Graph  Alt+Shift+E"),    "EvaluationGraph", EvalGraphProc},
694     {N_("Game List            Alt+Shift+G"), "GameList", ShowGameListProc},
695     {N_("ICS text menu"), "ICStextmenu", IcsTextProc},
696     {"----", NULL, NothingProc},
697     {N_("Tags"),             "Tags", EditTagsProc},
698     {N_("Comments"),         "Comments", EditCommentProc},
699     {N_("ICS Input Box"),    "ICSInputBox", IcsInputBoxProc},
700     {N_("Open Chat Window"), "OpenChatWindow", ChatProc},
701     {"----", NULL, NothingProc},
702     {N_("Board..."),          "Board", BoardOptionsProc},
703     {N_("Game List Tags..."), "GameListTags", GameListOptionsProc},
704     {NULL, NULL, NULL}
705 };
706
707 MenuItem modeMenu[] = {
708     {N_("Machine White  Ctrl+W"), "MachineWhite", MachineWhiteEvent},
709     {N_("Machine Black  Ctrl+B"), "MachineBlack", MachineBlackEvent},
710     {N_("Two Machines   Ctrl+T"), "TwoMachines", TwoMachinesEvent},
711     {N_("Analysis Mode  Ctrl+A"), "AnalysisMode", AnalyzeModeProc},
712     {N_("Analyze Game   Ctrl+G"), "AnalyzeFile", AnalyzeFileProc },
713     {N_("Edit Game         Ctrl+E"), "EditGame", EditGameEvent},
714     {N_("Edit Position      Ctrl+Shift+E"), "EditPosition", EditPositionEvent},
715     {N_("Training"),      "Training", TrainingEvent},
716     {N_("ICS Client"),    "ICSClient", IcsClientEvent},
717     {"----", NULL, NothingProc},
718     {N_("Machine Match"),         "MachineMatch", MatchProc},
719     {N_("Pause               Pause"),         "Pause", PauseEvent},
720     {NULL, NULL, NULL}
721 };
722
723 MenuItem actionMenu[] = {
724     {N_("Accept             F3"), "Accept", AcceptEvent},
725     {N_("Decline            F4"), "Decline", DeclineEvent},
726     {N_("Rematch           F12"), "Rematch", RematchEvent},
727     {"----", NULL, NothingProc},
728     {N_("Call Flag          F5"), "CallFlag", CallFlagEvent},
729     {N_("Draw                F6"), "Draw", DrawEvent},
730     {N_("Adjourn            F7"),  "Adjourn", AdjournEvent},
731     {N_("Abort                F8"),"Abort", AbortEvent},
732     {N_("Resign              F9"), "Resign", ResignEvent},
733     {"----", NULL, NothingProc},
734     {N_("Stop Observing  F10"), "StopObserving", StopObservingEvent},
735     {N_("Stop Examining  F11"), "StopExamining", StopExaminingEvent},
736     {N_("Upload to Examine"),   "UploadtoExamine", UploadGameEvent},
737     {"----", NULL, NothingProc},
738     {N_("Adjudicate to White"), "AdjudicatetoWhite", AdjuWhiteProc},
739     {N_("Adjudicate to Black"), "AdjudicatetoBlack", AdjuBlackProc},
740     {N_("Adjudicate Draw"),     "AdjudicateDraw", AdjuDrawProc},
741     {NULL, NULL, NULL}
742 };
743
744 MenuItem engineMenu[] = {
745     {N_("Load New 1st Engine ..."), "LoadNew1stEngine", LoadEngine1Proc},
746     {N_("Load New 2nd Engine ..."), "LoadNew2ndEngine", LoadEngine2Proc},
747     {"----", NULL, NothingProc},
748     {N_("Engine #1 Settings ..."), "Engine#1Settings", FirstSettingsProc},
749     {N_("Engine #2 Settings ..."), "Engine#2Settings", SecondSettingsProc},
750     {"----", NULL, NothingProc},
751     {N_("Hint"), "Hint", HintEvent},
752     {N_("Book"), "Book", BookEvent},
753     {"----", NULL, NothingProc},
754     {N_("Move Now     Ctrl+M"),     "MoveNow", MoveNowEvent},
755     {N_("Retract Move  Ctrl+X"), "RetractMove", RetractMoveEvent},
756     {NULL, NULL, NULL}
757 };
758
759 MenuItem optionsMenu[] = {
760 #ifdef OPTIONSDIALOG
761     {N_("General ..."), "General", OptionsProc},
762 #endif
763     {N_("Time Control ...       Alt+Shift+T"), "TimeControl", TimeControlProc},
764     {N_("Common Engine ...  Alt+Shift+U"),     "CommonEngine", UciMenuProc},
765     {N_("Adjudications ...      Alt+Shift+J"), "Adjudications", EngineMenuProc},
766     {N_("ICS ..."),    "ICS", IcsOptionsProc},
767     {N_("Match ..."), "Match", MatchOptionsProc},
768     {N_("Load Game ..."),    "LoadGame", LoadOptionsProc},
769     {N_("Save Game ..."),    "SaveGame", SaveOptionsProc},
770 //    {N_(" ..."),    "", OptionsProc},
771     {N_("Game List ..."),    "GameList", GameListOptionsProc},
772     {N_("Sounds ..."),    "Sounds", SoundOptionsProc},
773     {"----", NULL, NothingProc},
774 #ifndef OPTIONSDIALOG
775     {N_("Always Queen        Ctrl+Shift+Q"),   "AlwaysQueen", AlwaysQueenProc},
776     {N_("Animate Dragging"), "AnimateDragging", AnimateDraggingProc},
777     {N_("Animate Moving      Ctrl+Shift+A"),   "AnimateMoving", AnimateMovingProc},
778     {N_("Auto Flag               Ctrl+Shift+F"), "AutoFlag", AutoflagProc},
779     {N_("Auto Flip View"),   "AutoFlipView", AutoflipProc},
780     {N_("Blindfold"),        "Blindfold", BlindfoldProc},
781     {N_("Flash Moves"),      "FlashMoves", FlashMovesProc},
782 #if HIGHDRAG
783     {N_("Highlight Dragging"),    "HighlightDragging", HighlightDraggingProc},
784 #endif
785     {N_("Highlight Last Move"),   "HighlightLastMove", HighlightLastMoveProc},
786     {N_("Highlight With Arrow"),  "HighlightWithArrow", HighlightArrowProc},
787     {N_("Move Sound"),            "MoveSound", MoveSoundProc},
788 //    {N_("ICS Alarm"),             "ICSAlarm", IcsAlarmProc},
789     {N_("One-Click Moving"),      "OneClickMoving", OneClickProc},
790     {N_("Periodic Updates"),      "PeriodicUpdates", PeriodicUpdatesProc},
791     {N_("Ponder Next Move  Ctrl+Shift+P"), "PonderNextMove", PonderNextMoveProc},
792     {N_("Popup Exit Message"),    "PopupExitMessage", PopupExitMessageProc},
793     {N_("Popup Move Errors"),     "PopupMoveErrors", PopupMoveErrorsProc},
794 //    {N_("Premove"),               "Premove", PremoveProc},
795     {N_("Show Coords"),           "ShowCoords", ShowCoordsProc},
796     {N_("Hide Thinking        Ctrl+Shift+H"),   "HideThinking", HideThinkingProc},
797     {N_("Test Legality          Ctrl+Shift+L"), "TestLegality", TestLegalityProc},
798     {"----", NULL, NothingProc},
799 #endif
800     {N_("Save Settings Now"),     "SaveSettingsNow", SaveSettingsProc},
801     {N_("Save Settings on Exit"), "SaveSettingsonExit", SaveOnExitProc},
802     {NULL, NULL, NULL}
803 };
804
805 MenuItem helpMenu[] = {
806     {N_("Info XBoard"),     "InfoXBoard", InfoProc},
807     {N_("Man XBoard   F1"), "ManXBoard", ManProc},
808     {"----", NULL, NothingProc},
809     {N_("XBoard Home Page"), "XBoardHomePage", HomePageProc},
810     {N_("On-line User Guide"), "On-lineUserGuide", GuideProc},
811     {N_("Development News"), "DevelopmentNews", NewsPageProc},
812     {N_("e-Mail Bug Report"), "e-MailBugReport", BugReportProc},
813     {"----", NULL, NothingProc},
814     {N_("About XBoard"), "AboutXBoard", AboutProc},
815     {NULL, NULL, NULL}
816 };
817
818 MenuItem noMenu[] = {
819     { "", "LoadNextGame", LoadNextGameProc },
820     { "", "LoadPrevGame", LoadPrevGameProc },
821     { "", "ReloadGame", ReloadGameProc },
822     { "", "ReloadPosition", ReloadPositionProc },
823 #ifndef OPTIONSDIALOG
824     { "", "AlwaysQueen", AlwaysQueenProc },
825     { "", "AnimateDragging", AnimateDraggingProc },
826     { "", "AnimateMoving", AnimateMovingProc },
827     { "", "Autoflag", AutoflagProc },
828     { "", "Autoflip", AutoflipProc },
829     { "", "Blindfold", BlindfoldProc },
830     { "", "FlashMoves", FlashMovesProc },
831 #if HIGHDRAG
832     { "", "HighlightDragging", HighlightDraggingProc },
833 #endif
834     { "", "HighlightLastMove", HighlightLastMoveProc },
835 //    { "", "IcsAlarm", IcsAlarmProc },
836     { "", "MoveSound", MoveSoundProc },
837     { "", "PeriodicUpdates", PeriodicUpdatesProc },
838     { "", "PopupExitMessage", PopupExitMessageProc },
839     { "", "PopupMoveErrors", PopupMoveErrorsProc },
840 //    { "", "Premove", PremoveProc },
841     { "", "ShowCoords", ShowCoordsProc },
842     { "", "ShowThinking", ShowThinkingProc },
843     { "", "HideThinking", HideThinkingProc },
844     { "", "TestLegality", TestLegalityProc },
845 #endif
846     { "", "AboutGame", AboutGameEvent },
847     { "", "DebugProc", DebugProc },
848     { "", "Nothing", NothingProc },
849     {NULL, NULL, NULL}
850 };
851
852 Menu menuBar[] = {
853     {N_("File"),    "File", fileMenu},
854     {N_("Edit"),    "Edit", editMenu},
855     {N_("View"),    "View", viewMenu},
856     {N_("Mode"),    "Mode", modeMenu},
857     {N_("Action"),  "Action", actionMenu},
858     {N_("Engine"),  "Engine", engineMenu},
859     {N_("Options"), "Options", optionsMenu},
860     {N_("Help"),    "Help", helpMenu},
861     {NULL, NULL, NULL}
862 };
863
864 MenuItem *
865 MenuNameToItem (char *menuName)
866 {
867     int i=0;
868     char buf[MSG_SIZ], *p;
869     MenuItem *menuTab;
870     static MenuItem a = { NULL, NULL, NothingProc };
871     extern Option mainOptions[];
872     safeStrCpy(buf, menuName, MSG_SIZ);
873     p = strchr(buf, '.');
874     if(!p) menuTab = noMenu, p = menuName; else {
875         *p++ = NULLCHAR;
876         for(i=0; menuBar[i].name; i++)
877             if(!strcmp(buf, menuBar[i].name)) break;
878         if(!menuBar[i].name) return NULL; // main menu not found
879         menuTab = menuBar[i].mi;
880     }
881     if(*p == NULLCHAR) { a.handle = mainOptions[i+1].handle; return &a; } // main menu bar
882     for(i=0; menuTab[i].string; i++)
883         if(menuTab[i].ref && !strcmp(p, menuTab[i].ref)) return menuTab + i;
884     return NULL; // item not found
885 }
886
887 void
888 AppendEnginesToMenu (char *list)
889 {
890     int i=0;
891     char *p;
892     if(appData.icsActive || appData.recentEngines <= 0) return;
893     recentEngines = strdup(list);
894     while (*list) {
895         p = strchr(list, '\n'); if(p == NULL) break;
896         if(i == 0) AppendMenuItem("----", 0); // at least one valid item to add
897         *p = 0;
898         AppendMenuItem(list, i);
899         i++; *p = '\n'; list = p + 1;
900     }
901 }
902
903 Enables icsEnables[] = {
904     { "File.MailMove", False },
905     { "File.ReloadCMailMessage", False },
906     { "Mode.MachineBlack", False },
907     { "Mode.MachineWhite", False },
908     { "Mode.AnalysisMode", False },
909     { "Mode.AnalyzeFile", False },
910     { "Mode.TwoMachines", False },
911     { "Mode.MachineMatch", False },
912 #ifndef ZIPPY
913     { "Engine.Hint", False },
914     { "Engine.Book", False },
915     { "Engine.MoveNow", False },
916 #ifndef OPTIONSDIALOG
917     { "PeriodicUpdates", False },
918     { "HideThinking", False },
919     { "PonderNextMove", False },
920 #endif
921 #endif
922     { "Engine.Engine#1Settings", False },
923     { "Engine.Engine#2Settings", False },
924     { "Engine.Load1stEngine", False },
925     { "Engine.Load2ndEngine", False },
926     { "Edit.Annotate", False },
927     { "Options.Match", False },
928     { NULL, False }
929 };
930
931 Enables ncpEnables[] = {
932     { "File.MailMove", False },
933     { "File.ReloadCMailMessage", False },
934     { "Mode.MachineWhite", False },
935     { "Mode.MachineBlack", False },
936     { "Mode.AnalysisMode", False },
937     { "Mode.AnalyzeFile", False },
938     { "Mode.TwoMachines", False },
939     { "Mode.MachineMatch", False },
940     { "Mode.ICSClient", False },
941     { "View.ICStextmenu", False },
942     { "View.ICSInputBox", False },
943     { "View.OpenChatWindow", False },
944     { "Action.", False },
945     { "Edit.Revert", False },
946     { "Edit.Annotate", False },
947     { "Engine.Engine#1Settings", False },
948     { "Engine.Engine#2Settings", False },
949     { "Engine.MoveNow", False },
950     { "Engine.RetractMove", False },
951     { "Options.ICS", False },
952 #ifndef OPTIONSDIALOG
953     { "Options.AutoFlag", False },
954     { "Options.AutoFlip View", False },
955 //    { "Options.ICSAlarm", False },
956     { "Options.MoveSound", False },
957     { "Options.HideThinking", False },
958     { "Options.PeriodicUpdates", False },
959     { "Options.PonderNextMove", False },
960 #endif
961     { "Engine.Hint", False },
962     { "Engine.Book", False },
963     { NULL, False }
964 };
965
966 Enables gnuEnables[] = {
967     { "Mode.ICSClient", False },
968     { "View.ICStextmenu", False },
969     { "View.ICSInputBox", False },
970     { "View.OpenChatWindow", False },
971     { "Action.Accept", False },
972     { "Action.Decline", False },
973     { "Action.Rematch", False },
974     { "Action.Adjourn", False },
975     { "Action.StopExamining", False },
976     { "Action.StopObserving", False },
977     { "Action.UploadtoExamine", False },
978     { "Edit.Revert", False },
979     { "Edit.Annotate", False },
980     { "Options.ICS", False },
981
982     /* The next two options rely on SetCmailMode being called *after*    */
983     /* SetGNUMode so that when GNU is being used to give hints these     */
984     /* menu options are still available                                  */
985
986     { "File.MailMove", False },
987     { "File.ReloadCMailMessage", False },
988     // [HGM] The following have been added to make a switch from ncp to GNU mode possible
989     { "Mode.MachineWhite", True },
990     { "Mode.MachineBlack", True },
991     { "Mode.AnalysisMode", True },
992     { "Mode.AnalyzeFile", True },
993     { "Mode.TwoMachines", True },
994     { "Mode.MachineMatch", True },
995     { "Engine.Engine#1Settings", True },
996     { "Engine.Engine#2Settings", True },
997     { "Engine.Hint", True },
998     { "Engine.Book", True },
999     { "Engine.MoveNow", True },
1000     { "Engine.RetractMove", True },
1001     { "Action.", True },
1002     { NULL, False }
1003 };
1004
1005 Enables cmailEnables[] = {
1006     { "Action.", True },
1007     { "Action.CallFlag", False },
1008     { "Action.Draw", True },
1009     { "Action.Adjourn", False },
1010     { "Action.Abort", False },
1011     { "Action.StopObserving", False },
1012     { "Action.StopExamining", False },
1013     { "File.MailMove", True },
1014     { "File.ReloadCMailMessage", True },
1015     { NULL, False }
1016 };
1017
1018 Enables trainingOnEnables[] = {
1019   { "Edit.EditComment", False },
1020   { "Mode.Pause", False },
1021   { "Edit.Forward", False },
1022   { "Edit.Backward", False },
1023   { "Edit.ForwardtoEnd", False },
1024   { "Edit.BacktoStart", False },
1025   { "Engine.MoveNow", False },
1026   { "Edit.TruncateGame", False },
1027   { NULL, False }
1028 };
1029
1030 Enables trainingOffEnables[] = {
1031   { "Edit.EditComment", True },
1032   { "Mode.Pause", True },
1033   { "Edit.Forward", True },
1034   { "Edit.Backward", True },
1035   { "Edit.ForwardtoEnd", True },
1036   { "Edit.BacktoStart", True },
1037   { "Engine.MoveNow", True },
1038   { "Engine.TruncateGame", True },
1039   { NULL, False }
1040 };
1041
1042 Enables machineThinkingEnables[] = {
1043   { "File.LoadGame", False },
1044 //  { "LoadNextGame", False },
1045 //  { "LoadPreviousGame", False },
1046 //  { "ReloadSameGame", False },
1047   { "Edit.PasteGame", False },
1048   { "File.LoadPosition", False },
1049 //  { "LoadNextPosition", False },
1050 //  { "LoadPreviousPosition", False },
1051 //  { "ReloadSamePosition", False },
1052   { "Edit.PastePosition", False },
1053   { "Mode.MachineWhite", False },
1054   { "Mode.MachineBlack", False },
1055   { "Mode.TwoMachines", False },
1056 //  { "MachineMatch", False },
1057   { "Engine.RetractMove", False },
1058   { NULL, False }
1059 };
1060
1061 Enables userThinkingEnables[] = {
1062   { "File.LoadGame", True },
1063 //  { "LoadNextGame", True },
1064 //  { "LoadPreviousGame", True },
1065 //  { "ReloadSameGame", True },
1066   { "Edit.PasteGame", True },
1067   { "File.LoadPosition", True },
1068 //  { "LoadNextPosition", True },
1069 //  { "LoadPreviousPosition", True },
1070 //  { "ReloadSamePosition", True },
1071   { "Edit.PastePosition", True },
1072   { "Mode.MachineWhite", True },
1073   { "Mode.MachineBlack", True },
1074   { "Mode.TwoMachines", True },
1075 //  { "MachineMatch", True },
1076   { "Engine.RetractMove", True },
1077   { NULL, False }
1078 };
1079
1080 void
1081 SetICSMode ()
1082 {
1083   SetMenuEnables(icsEnables);
1084
1085 #if ZIPPY
1086   if (appData.zippyPlay && !appData.noChessProgram) { /* [DM] icsEngineAnalyze */
1087      EnableNamedMenuItem("Mode.AnalysisMode", True);
1088      EnableNamedMenuItem("Engine.Engine#1Settings", True);
1089   }
1090 #endif
1091 }
1092
1093 void
1094 SetNCPMode ()
1095 {
1096   SetMenuEnables(ncpEnables);
1097 }
1098
1099 void
1100 SetGNUMode ()
1101 {
1102   SetMenuEnables(gnuEnables);
1103 }
1104
1105 void
1106 SetCmailMode ()
1107 {
1108   SetMenuEnables(cmailEnables);
1109 }
1110
1111 void
1112 SetTrainingModeOn ()
1113 {
1114   SetMenuEnables(trainingOnEnables);
1115   if (appData.showButtonBar) {
1116     EnableButtonBar(False);
1117   }
1118   CommentPopDown();
1119 }
1120
1121 void
1122 SetTrainingModeOff ()
1123 {
1124   SetMenuEnables(trainingOffEnables);
1125   if (appData.showButtonBar) {
1126     EnableButtonBar(True);
1127   }
1128 }
1129
1130 void
1131 SetUserThinkingEnables ()
1132 {
1133   if (appData.noChessProgram) return;
1134   SetMenuEnables(userThinkingEnables);
1135 }
1136
1137 void
1138 SetMachineThinkingEnables ()
1139 {
1140   if (appData.noChessProgram) return;
1141   SetMenuEnables(machineThinkingEnables);
1142   switch (gameMode) {
1143   case MachinePlaysBlack:
1144   case MachinePlaysWhite:
1145   case TwoMachinesPlay:
1146     EnableNamedMenuItem(ModeToWidgetName(gameMode), True);
1147     break;
1148   default:
1149     break;
1150   }
1151 }
1152
1153 void
1154 GreyRevert (Boolean grey)
1155 {
1156     MarkMenuItem("Edit.Revert", !grey);
1157     MarkMenuItem("Edit.Annotate", !grey);
1158 }
1159
1160 char *
1161 ModeToWidgetName (GameMode mode)
1162 {
1163     switch (mode) {
1164       case BeginningOfGame:
1165         if (appData.icsActive)
1166           return "Mode.ICSClient";
1167         else if (appData.noChessProgram ||
1168                  *appData.cmailGameName != NULLCHAR)
1169           return "Mode.EditGame";
1170         else
1171           return "Mode.MachineBlack";
1172       case MachinePlaysBlack:
1173         return "Mode.MachineBlack";
1174       case MachinePlaysWhite:
1175         return "Mode.MachineWhite";
1176       case AnalyzeMode:
1177         return "Mode.AnalysisMode";
1178       case AnalyzeFile:
1179         return "Mode.AnalyzeFile";
1180       case TwoMachinesPlay:
1181         return "Mode.TwoMachines";
1182       case EditGame:
1183         return "Mode.EditGame";
1184       case PlayFromGameFile:
1185         return "File.LoadGame";
1186       case EditPosition:
1187         return "Mode.EditPosition";
1188       case Training:
1189         return "Mode.Training";
1190       case IcsPlayingWhite:
1191       case IcsPlayingBlack:
1192       case IcsObserving:
1193       case IcsIdle:
1194       case IcsExamining:
1195         return "Mode.ICSClient";
1196       default:
1197       case EndOfGame:
1198         return NULL;
1199     }
1200 }
1201
1202 void
1203 InitMenuMarkers()
1204 {
1205 #ifndef OPTIONSDIALOG
1206     if (appData.alwaysPromoteToQueen) {
1207         MarkMenuItem("Options.Always Queen", True);
1208     }
1209     if (appData.animateDragging) {
1210         MarkMenuItem("Options.Animate Dragging", True);
1211     }
1212     if (appData.animate) {
1213         MarkMenuItem("Options.Animate Moving", True);
1214     }
1215     if (appData.autoCallFlag) {
1216         MarkMenuItem("Options.Auto Flag", True);
1217     }
1218     if (appData.autoFlipView) {
1219         XtSetValues(XtNameToWidget(menuBarWidget,"Options.Auto Flip View", True);
1220     }
1221     if (appData.blindfold) {
1222         MarkMenuItem("Options.Blindfold", True);
1223     }
1224     if (appData.flashCount > 0) {
1225         MarkMenuItem("Options.Flash Moves", True);
1226     }
1227 #if HIGHDRAG
1228     if (appData.highlightDragging) {
1229         MarkMenuItem("Options.Highlight Dragging", True);
1230     }
1231 #endif
1232     if (appData.highlightLastMove) {
1233         MarkMenuItem("Options.Highlight Last Move", True);
1234     }
1235     if (appData.highlightMoveWithArrow) {
1236         MarkMenuItem("Options.Arrow", True);
1237     }
1238 //    if (appData.icsAlarm) {
1239 //      MarkMenuItem("Options.ICS Alarm", True);
1240 //    }
1241     if (appData.ringBellAfterMoves) {
1242         MarkMenuItem("Options.Move Sound", True);
1243     }
1244     if (appData.oneClick) {
1245         MarkMenuItem("Options.OneClick", True);
1246     }
1247     if (appData.periodicUpdates) {
1248         MarkMenuItem("Options.Periodic Updates", True);
1249     }
1250     if (appData.ponderNextMove) {
1251         MarkMenuItem("Options.Ponder Next Move", True);
1252     }
1253     if (appData.popupExitMessage) {
1254         MarkMenuItem("Options.Popup Exit Message", True);
1255     }
1256     if (appData.popupMoveErrors) {
1257         MarkMenuItem("Options.Popup Move Errors", True);
1258     }
1259 //    if (appData.premove) {
1260 //      MarkMenuItem("Options.Premove", True);
1261 //    }
1262     if (appData.showCoords) {
1263         MarkMenuItem("Options.Show Coords", True);
1264     }
1265     if (appData.hideThinkingFromHuman) {
1266         MarkMenuItem("Options.Hide Thinking", True);
1267     }
1268     if (appData.testLegality) {
1269         MarkMenuItem("Options.Test Legality", True);
1270     }
1271 #endif
1272     if (saveSettingsOnExit) {
1273         MarkMenuItem("Options.SaveSettingsonExit", True);
1274     }
1275 }
1276
1277