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