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