Switch to use of short menu references
[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 "xgamelist.h"
93 #include "xhistory.h"
94 #include "xedittags.h"
95 #include "menus.h"
96 #include "gettext.h"
97
98 #ifdef ENABLE_NLS
99 # define  _(s) gettext (s)
100 # define N_(s) gettext_noop (s)
101 #else
102 # define  _(s) (s)
103 # define N_(s)  s
104 #endif
105
106 MenuItem fileMenu[] = {
107     {N_("New Game        Ctrl+N"),        "New Game", ResetGameEvent},
108     {N_("New Shuffle Game ..."),          "New Shuffle Game", ShuffleMenuProc},
109     {N_("New Variant ...   Alt+Shift+V"), "New Variant", NewVariantProc},      // [HGM] variant: not functional yet
110     {"----", NULL, NothingProc},
111     {N_("Load Game       Ctrl+O"),        "Load Game", LoadGameProc},
112     {N_("Load Position    Ctrl+Shift+O"), "Load Position", LoadPositionProc},
113 //    {N_("Load Next Game"), "Load Next Game", LoadNextGameProc},
114 //    {N_("Load Previous Game"), "Load Previous Game", LoadPrevGameProc},
115 //    {N_("Reload Same Game"), "Reload Same Game", ReloadGameProc},
116     {N_("Next Position     Shift+PgDn"), "Load Next Position", LoadNextPositionProc},
117     {N_("Prev Position     Shift+PgUp"), "Load Previous Position", LoadPrevPositionProc},
118     {"----", NULL, NothingProc},
119 //    {N_("Reload Same Position"), "Reload Same Position", ReloadPositionProc},
120     {N_("Save Game       Ctrl+S"),        "Save Game", SaveGameProc},
121     {N_("Save Position    Ctrl+Shift+S"), "Save Position", SavePositionProc},
122     {"----", NULL, NothingProc},
123     {N_("Mail Move"),            "Mail Move", MailMoveEvent},
124     {N_("Reload CMail Message"), "Reload CMail Message", ReloadCmailMsgProc},
125     {"----", NULL, NothingProc},
126     {N_("Quit                 Ctr+Q"), "Exit", QuitProc},
127     {NULL, NULL, NULL}
128 };
129
130 MenuItem editMenu[] = {
131     {N_("Copy Game    Ctrl+C"),        "Copy Game", CopyGameProc},
132     {N_("Copy Position Ctrl+Shift+C"), "Copy Position", CopyPositionProc},
133     {N_("Copy Game List"),        "Copy Game List", CopyGameListProc},
134     {"----", NULL, NothingProc},
135     {N_("Paste Game    Ctrl+V"),        "Paste Game", PasteGameProc},
136     {N_("Paste Position Ctrl+Shift+V"), "Paste Position", PastePositionProc},
137     {"----", NULL, NothingProc},
138     {N_("Edit Game      Ctrl+E"),        "Edit Game 2", EditGameEvent},
139     {N_("Edit Position   Ctrl+Shift+E"), "Edit Position 2", EditPositionEvent},
140     {N_("Edit Tags"),                    "Edit Tags", EditTagsProc},
141     {N_("Edit Comment"),                 "Edit Comment", EditCommentProc},
142     {N_("Edit Book"),                    "Edit Book", EditBookEvent},
143     {"----", NULL, NothingProc},
144     {N_("Revert              Home"), "Revert", RevertProc},
145     {N_("Annotate"),                 "Annotate", AnnotateProc},
146     {N_("Truncate Game  End"),       "Truncate Game", TruncateGameEvent},
147     {"----", NULL, NothingProc},
148     {N_("Backward         Alt+Left"),   "Backward", BackwardEvent},
149     {N_("Forward           Alt+Right"), "Forward", ForwardEvent},
150     {N_("Back to Start     Alt+Home"),  "Back to Start", ToStartEvent},
151     {N_("Forward to End Alt+End"),      "Forward to End", ToEndEvent},
152     {NULL, NULL, NULL}
153 };
154
155 MenuItem viewMenu[] = {
156     {N_("Flip View             F2"),         "Flip View", FlipViewProc},
157     {"----", NULL, NothingProc},
158     {N_("Engine Output      Alt+Shift+O"),   "Show Engine Output", EngineOutputProc},
159     {N_("Move History       Alt+Shift+H"),   "Show Move History", HistoryShowProc}, // [HGM] hist: activate 4.2.7 code
160     {N_("Evaluation Graph  Alt+Shift+E"),    "Show Evaluation Graph", EvalGraphProc},
161     {N_("Game List            Alt+Shift+G"), "Show Game List", ShowGameListProc},
162     {N_("ICS text menu"), "ICStex", IcsTextProc},
163     {"----", NULL, NothingProc},
164     {N_("Tags"),             "Show Tags", EditTagsProc},
165     {N_("Comments"),         "Show Comments", EditCommentProc},
166     {N_("ICS Input Box"),    "ICS Input Box", IcsInputBoxProc},
167     {"----", NULL, NothingProc},
168     {N_("Board..."),          "Board Options", BoardOptionsProc},
169     {N_("Game List Tags..."), "Game List", GameListOptionsPopUp},
170     {NULL, NULL, NULL}
171 };
172
173 MenuItem modeMenu[] = {
174     {N_("Machine White  Ctrl+W"), "Machine White", MachineWhiteEvent},
175     {N_("Machine Black  Ctrl+B"), "Machine Black", MachineBlackEvent},
176     {N_("Two Machines   Ctrl+T"), "Two Machines", TwoMachinesEvent},
177     {N_("Analysis Mode  Ctrl+A"), "Analysis Mode", AnalyzeModeProc},
178     {N_("Analyze Game   Ctrl+G"), "Analyze File", AnalyzeFileProc },
179     {N_("Edit Game         Ctrl+E"), "Edit Game", EditGameEvent},
180     {N_("Edit Position      Ctrl+Shift+E"), "Edit Position", EditPositionEvent},
181     {N_("Training"),      "Training", TrainingEvent},
182     {N_("ICS Client"),    "ICS Client", IcsClientEvent},
183     {"----", NULL, NothingProc},
184     {N_("Machine Match"),         "Machine Match", MatchProc},
185     {N_("Pause               Pause"),         "Pause", PauseEvent},
186     {NULL, NULL, NULL}
187 };
188
189 MenuItem actionMenu[] = {
190     {N_("Accept             F3"), "Accept", AcceptEvent},
191     {N_("Decline            F4"), "Decline", DeclineEvent},
192     {N_("Rematch           F12"), "Rematch", RematchEvent},
193     {"----", NULL, NothingProc},
194     {N_("Call Flag          F5"), "Call Flag", CallFlagEvent},
195     {N_("Draw                F6"), "Draw", DrawEvent},
196     {N_("Adjourn            F7"),  "Adjourn", AdjournEvent},
197     {N_("Abort                F8"),"Abort", AbortEvent},
198     {N_("Resign              F9"), "Resign", ResignEvent},
199     {"----", NULL, NothingProc},
200     {N_("Stop Observing  F10"), "Stop Observing", StopObservingEvent},
201     {N_("Stop Examining  F11"), "Stop Examining", StopExaminingEvent},
202     {N_("Upload to Examine"),   "Upload to Examine", UploadGameEvent},
203     {"----", NULL, NothingProc},
204     {N_("Adjudicate to White"), "Adjudicate to White", AdjuWhiteProc},
205     {N_("Adjudicate to Black"), "Adjudicate to Black", AdjuBlackProc},
206     {N_("Adjudicate Draw"),     "Adjudicate Draw", AdjuDrawProc},
207     {NULL, NULL, NULL}
208 };
209
210 MenuItem engineMenu[] = {
211     {N_("Load New Engine ..."), "Load Engine", LoadEngineProc},
212     {"----", NULL, NothingProc},
213     {N_("Engine #1 Settings ..."), "Engine #1 Settings", FirstSettingsProc},
214     {N_("Engine #2 Settings ..."), "Engine #2 Settings", SecondSettingsProc},
215     {"----", NULL, NothingProc},
216     {N_("Hint"), "Hint", HintEvent},
217     {N_("Book"), "Book", BookEvent},
218     {"----", NULL, NothingProc},
219     {N_("Move Now     Ctrl+M"),     "Move Now", MoveNowEvent},
220     {N_("Retract Move  Ctrl+X"), "Retract Move", RetractMoveEvent},
221     {NULL, NULL, NULL}
222 };
223
224 MenuItem optionsMenu[] = {
225 #ifdef OPTIONSDIALOG
226     {N_("General ..."), "General", OptionsProc},
227 #endif
228     {N_("Time Control ...       Alt+Shift+T"), "Time Control", TimeControlProc},
229     {N_("Common Engine ...  Alt+Shift+U"),     "Common Engine", UciMenuProc},
230     {N_("Adjudications ...      Alt+Shift+J"), "Adjudications", EngineMenuProc},
231     {N_("ICS ..."),    "ICS", IcsOptionsProc},
232     {N_("Match ..."), "Match", MatchOptionsProc},
233     {N_("Load Game ..."),    "Load Game", LoadOptionsProc},
234     {N_("Save Game ..."),    "Save Game", SaveOptionsProc},
235 //    {N_(" ..."),    "", OptionsProc},
236     {N_("Game List ..."),    "Game List", GameListOptionsPopUp},
237     {N_("Sounds ..."),    "Sounds", SoundOptionsProc},
238     {"----", NULL, NothingProc},
239 #ifndef OPTIONSDIALOG
240     {N_("Always Queen        Ctrl+Shift+Q"),   "Always Queen", AlwaysQueenProc},
241     {N_("Animate Dragging"), "Animate Dragging", AnimateDraggingProc},
242     {N_("Animate Moving      Ctrl+Shift+A"),   "Animate Moving", AnimateMovingProc},
243     {N_("Auto Flag               Ctrl+Shift+F"), "Auto Flag", AutoflagProc},
244     {N_("Auto Flip View"),   "Auto Flip View", AutoflipProc},
245     {N_("Blindfold"),        "Blindfold", BlindfoldProc},
246     {N_("Flash Moves"),      "Flash Moves", FlashMovesProc},
247 #if HIGHDRAG
248     {N_("Highlight Dragging"),    "Highlight Dragging", HighlightDraggingProc},
249 #endif
250     {N_("Highlight Last Move"),   "Highlight Last Move", HighlightLastMoveProc},
251     {N_("Highlight With Arrow"),  "Arrow", HighlightArrowProc},
252     {N_("Move Sound"),            "Move Sound", MoveSoundProc},
253 //    {N_("ICS Alarm"),             "ICS Alarm", IcsAlarmProc},
254     {N_("One-Click Moving"),      "OneClick", OneClickProc},
255     {N_("Periodic Updates"),      "Periodic Updates", PeriodicUpdatesProc},
256     {N_("Ponder Next Move  Ctrl+Shift+P"), "Ponder Next Move", PonderNextMoveProc},
257     {N_("Popup Exit Message"),    "Popup Exit Message", PopupExitMessageProc},
258     {N_("Popup Move Errors"),     "Popup Move Errors", PopupMoveErrorsProc},
259 //    {N_("Premove"),               "Premove", PremoveProc},
260     {N_("Show Coords"),           "Show Coords", ShowCoordsProc},
261     {N_("Hide Thinking        Ctrl+Shift+H"),   "Hide Thinking", HideThinkingProc},
262     {N_("Test Legality          Ctrl+Shift+L"), "Test Legality", TestLegalityProc},
263     {"----", NULL, NothingProc},
264 #endif
265     {N_("Save Settings Now"),     "Save Settings Now", SaveSettingsProc},
266     {N_("Save Settings on Exit"), "Save Settings on Exit", SaveOnExitProc},
267     {NULL, NULL, NULL}
268 };
269
270 MenuItem helpMenu[] = {
271     {N_("Info XBoard"),     "Info XBoard", InfoProc},
272     {N_("Man XBoard   F1"), "Man XBoard", ManProc},
273     {"----", NULL, NothingProc},
274     {N_("XBoard Home Page"), "Home Page", HomePageProc},
275     {N_("On-line User Guide"), "User Guide", GuideProc},
276     {N_("Development News"), "News Page", NewsPageProc},
277     {N_("e-Mail Bug Report"), "Bug Report", BugReportProc},
278     {"----", NULL, NothingProc},
279     {N_("About XBoard"), "About XBoard", AboutProc},
280     {NULL, NULL, NULL}
281 };
282
283 Menu menuBar[] = {
284     {N_("File"),    "File", fileMenu},
285     {N_("Edit"),    "Edit", editMenu},
286     {N_("View"),    "View", viewMenu},
287     {N_("Mode"),    "Mode", modeMenu},
288     {N_("Action"),  "Action", actionMenu},
289     {N_("Engine"),  "Engine", engineMenu},
290     {N_("Options"), "Options", optionsMenu},
291     {N_("Help"),    "Help", helpMenu},
292     {NULL, NULL, NULL}
293 };
294
295 int
296 MenuToNumber(char *menuName)
297 {
298     int i;
299     for(i=0; i<nrOfMenuItems; i++)
300         if(!strcmp(menuName, menuItemList[i].name)) return i;
301     return -1;
302 }
303
304 void
305 AppendEnginesToMenu (char *list)
306 {
307     int i=0;
308     char *p;
309
310     if(appData.icsActive || appData.recentEngines <= 0) return;
311     recentEngines = strdup(list);
312     while (*list) {
313         p = strchr(list, '\n'); if(p == NULL) break;
314         if(i == 0) AppendMenuItem("----", "----", NULL); // at least one valid item to add
315         *p = 0;
316         AppendMenuItem(list, "recent", (MenuProc *) i);
317         i++; *p = '\n'; list = p + 1;
318     }
319 }
320
321 void
322 AddPullDownMenu (char *name, Menu *mb)
323 {
324     MenuItem *mi;
325
326     CreateMenuButton(name, mb);
327
328     mi = mb->mi;
329     while (mi->string != NULL) {
330         AppendMenuItem(mi->string, mi->ref, mi->proc);
331         menuItemList[nrOfMenuItems].name = mi->ref;
332         menuItemList[nrOfMenuItems].proc = mi->proc;
333         if(strcmp(mi->string, "----")) nrOfMenuItems++;
334         mi++;
335     }
336
337     if(!strcmp(mb->name, "Engine")) AppendEnginesToMenu(appData.recentEngineList);
338 }
339
340 void
341 CreateMainMenus (Menu *mb)
342 {
343     char menuName[MSG_SIZ];
344
345     while(menuItemList[nrOfMenuItems].name) nrOfMenuItems++; // skip any predefined items
346
347     while (mb->name != NULL) {
348         safeStrCpy(menuName, "menu", sizeof(menuName)/sizeof(menuName[0]) );
349         strncat(menuName, mb->ref, MSG_SIZ - strlen(menuName) - 1);
350         AddPullDownMenu(menuName, mb++);
351     }
352 }
353
354