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