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