Fix multi-leg promotions
[xboard.git] / ngamelist.c
1 /*
2  * ngamelist.c -- Game list window, Xt-independent front-end code for XBoard
3  *
4  * Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
5  * ------------------------------------------------------------------------
6  *
7  * GNU XBoard is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * GNU XBoard is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see http://www.gnu.org/licenses/.  *
19  *
20  *------------------------------------------------------------------------
21  ** See the file ChangeLog for a revision history.  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if STDC_HEADERS
31 # include <stdlib.h>
32 # include <string.h>
33 #else /* not STDC_HEADERS */
34 extern char *getenv();
35 # if HAVE_STRING_H
36 #  include <string.h>
37 # else /* not HAVE_STRING_H */
38 #  include <strings.h>
39 # endif /* not HAVE_STRING_H */
40 #endif /* not STDC_HEADERS */
41
42 #if HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45
46 #include "common.h"
47 #include "frontend.h"
48 #include "backend.h"
49 #include "dialogs.h"
50 #include "menus.h"
51 #include "gettext.h"
52
53 #ifdef ENABLE_NLS
54 # define  _(s) gettext (s)
55 # define N_(s) gettext_noop (s)
56 #else
57 # define  _(s) (s)
58 # define N_(s)  s
59 #endif
60
61
62 static char filterString[MSG_SIZ];
63 static int listLength, wins, losses, draws, page;
64 int narrowFlag;
65
66
67 typedef struct {
68     short int x, y;
69     short int w, h;
70     FILE *fp;
71     char *filename;
72     char **strings;
73 } GameListClosure;
74 static GameListClosure *glc = NULL;
75
76 static char *filterPtr;
77 static char *list[1003];
78 static int listEnd;
79
80 static int GameListPrepare P((int byPos, int narrow));
81 static void GameListReplace P((int page));
82 static void GL_Button P((int n));
83
84 Option gamesOptions[] = {
85 { 200,  LR|TB,     400, NULL, (void*) list,       NULL, NULL, ListBox, "", &appData.gameListFont },
86 {   0,  0,         100, NULL, (void*) &filterPtr, "", NULL, TextBox, "" },
87 {   4,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("find position") },
88 {   2,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("narrow") }, // buttons referred to by ID in value (=first) field!
89 {   3,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("thresholds") },
90 {   9,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("tags") },
91 {   5,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("next") },
92 {   6,  SAME_ROW,    0, NULL, (void*) &GL_Button, NULL, NULL, Button, N_("close") },
93 {   0,  SAME_ROW | NO_OK, 0, NULL, NULL, "", NULL, EndMark , "" }
94 };
95
96 static void
97 GL_Button (int n)
98 {
99     int index;
100     n = gamesOptions[n].value; // use marker in option rather than n itself, for more easy adding/deletng of buttons
101     if (n == 6) { // close
102         PopDown(GameListDlg);
103         return;
104     }
105     if (n == 3) { // thresholds
106         LoadOptionsPopUp(GameListDlg);
107         return;
108     }
109     if (n == 9) { // tags
110         GameListOptionsPopUp(GameListDlg);
111         return;
112     }
113     index = SelectedListBoxItem(&gamesOptions[0]);
114     if (n == 7) { // load
115         if (index < 0) {
116             DisplayError(_("No game selected"), 0);
117             return;
118         }
119     } else if (n == 5) { // next
120         index++;
121         if (index >= listLength || !list[index]) {
122             DisplayError(_("Can't go forward any further"), 0);
123             return;
124         }
125         HighlightWithScroll(&gamesOptions[0], index, listEnd);
126     } else if (n == 8) { // prev
127         index--;
128         if (index < 0) {
129             DisplayError(_("Can't back up any further"), 0);
130             return;
131         }
132         HighlightWithScroll(&gamesOptions[0], index, listEnd);
133     } else if (n == 2 || // narrow
134                n == 4) { // find position
135         char *text;
136         GetWidgetText(&gamesOptions[1], &text);
137         safeStrCpy(filterString, text, sizeof(filterString)/sizeof(filterString[0]));
138         GameListPrepare(True, n == 2); GameListReplace(0);
139         return;
140     }
141
142     index = atoi(list[index])-1; // [HGM] filter: read true index from sequence nr of line
143     if (cmailMsgLoaded) {
144         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
145     } else {
146         LoadGame(glc->fp, index + 1, glc->filename, True);
147     }
148 }
149
150 static int
151 GameListCreate (char *name)
152 {
153     int new;
154     if(new = GenericPopUp(gamesOptions, name, GameListDlg, BoardWindow, NONMODAL, appData.topLevel))
155         AddHandler(&gamesOptions[1], GameListDlg, 4),
156         AddHandler(&gamesOptions[0], GameListDlg, 5);
157     FocusOnWidget(&gamesOptions[0], GameListDlg);
158     return new;
159 }
160
161 static int
162 GameListPrepare (int byPos, int narrow)
163 {   // [HGM] filter: put in separate routine, to make callable from call-back
164     int nstrings;
165     ListGame *lg;
166     char **st, *line;
167     TimeMark t, t2;
168
169     GetTimeMark(&t);
170     if(st = glc->strings) while(*st) free(*st++);
171     nstrings = ((ListGame *) gameList.tailPred)->number;
172     glc->strings = (char **) malloc((nstrings + 1) * sizeof(char *));
173     st = glc->strings;
174     lg = (ListGame *) gameList.head;
175     listLength = wins = losses = draws = 0;
176     if(byPos) InitSearch();
177     while (nstrings--) {
178         int pos = -1;
179         if(!narrow || lg->position >= 0) { // only consider already selected positions when narrowing
180           line = GameListLine(lg->number, &lg->gameInfo);
181           if((filterString[0] == NULLCHAR || SearchPattern( line, filterString )) && (!byPos || (pos=GameContainsPosition(glc->fp, lg)) >= 0) ) {
182             *st++ = line; // [HGM] filter: make adding line conditional.
183             listLength++;
184             if( lg->gameInfo.result == WhiteWins ) wins++; else
185             if( lg->gameInfo.result == BlackWins ) losses++; else
186             if( lg->gameInfo.result == GameIsDrawn ) draws++;
187             if(!byPos) pos = 0; // indicate selected
188           }
189         }
190         if(lg->number % 2000 == 0) {
191             char buf[MSG_SIZ];
192             snprintf(buf, MSG_SIZ, _("Scanning through games (%d)"), lg->number);
193             DisplayTitle(buf); DoEvents();
194         }
195         lg->position = pos;
196         lg = (ListGame *) lg->node.succ;
197     }
198     if(appData.debugMode) { GetTimeMark(&t2);printf("GameListPrepare %ld msec\n", SubtractTimeMarks(&t2,&t)); }
199     DisplayTitle("XBoard");
200     *st = NULL;
201     return listLength;
202 }
203
204 static void
205 GameListReplace (int page)
206 {
207   // filter: put in separate routine, to make callable from call-back
208   char buf[MSG_SIZ], **st=list;
209   int i;
210
211   if(page) *st++ = _("previous page"); else if(listLength > 1000) *st++ = "";
212   for(i=0; i<1000; i++) if( !(*st++ = glc->strings[page+i]) ) { st--; break; }
213   listEnd = st - list;
214   if(page + 1000 <= listLength) *st++ = _("next page");
215   *st = NULL;
216
217   LoadListBox(&gamesOptions[0], _("no games matched your request"), -1, -1);
218   HighlightWithScroll(&gamesOptions[0], listEnd > 1000, listEnd);
219   snprintf(buf, MSG_SIZ, _("%s - %d/%d games (%d-%d-%d)"), glc->filename, listLength, ((ListGame *) gameList.tailPred)->number, wins, losses, draws);
220   SetDialogTitle(GameListDlg, buf);
221 }
222
223 void
224 GameListUpdate ()
225 {
226     if(!DialogExists(GameListDlg)) return;
227     GameListPrepare(False, False);
228     GameListReplace(0);
229 }
230
231 void
232 GameListPopUp (FILE *fp, char *filename)
233 {
234     if (glc == NULL) {
235         glc = (GameListClosure *) calloc(1, sizeof(GameListClosure));
236         glc->x = glc->y = -1;
237         glc->filename = NULL;
238     }
239
240     GameListPrepare(False, False); // [HGM] filter: code put in separate routine
241
242     glc->fp = fp;
243
244     if (glc->filename != NULL) free(glc->filename);
245     glc->filename = StrSave(filename);
246
247     if (!GameListCreate(filename))
248         SetIconName(GameListDlg, filename);
249
250     page = 0;
251     GameListReplace(0); // [HGM] filter: code put in separate routine, and also called to set title
252     MarkMenu("View.GameList", GameListDlg);
253     EnableNamedMenuItem("File.SaveSelected", TRUE);
254 }
255
256 FILE *
257 GameFile ()
258 {
259   return glc ? glc->fp : NULL;
260 }
261
262 void
263 GameListDestroy ()
264 {
265     if (glc == NULL) return;
266     EnableNamedMenuItem("File.SaveSelected", FALSE);
267     PopDown(GameListDlg);
268     if (glc->strings != NULL) {
269         char **st;
270         st = glc->strings;
271         while (*st) {
272             free(*st++);
273         }
274         free(glc->strings);
275     }
276     free(glc);
277     glc = NULL;
278 }
279
280 void
281 ShowGameListProc ()
282 {
283     if (glc == NULL) {
284         DisplayError(_("There is no game list"), 0);
285         return;
286     }
287     if (shellUp[GameListDlg]) {
288         PopDown(GameListDlg);
289         return;
290     }
291     GenericPopUp(NULL, NULL, GameListDlg, BoardWindow, NONMODAL, appData.topLevel); // first two args ignored when shell exists!
292     MarkMenu("View.GameList", GameListDlg);
293     GameListHighlight(lastLoadGameNumber);
294 }
295
296 int
297 GameListClicks (int direction)
298 {
299     int index;
300
301     if (glc == NULL || listLength == 0) return 1;
302     if(direction == 100) { FocusOnWidget(&gamesOptions[0], GameListDlg); return 1; }
303     index = SelectedListBoxItem(&gamesOptions[0]);
304
305     if (index < 0) return 1;
306     if(page && (index == 0 && direction < 1 || direction == -4)) {
307         page -= 1000;
308         if(page < 0) page = 0; // safety
309         GameListReplace(page);
310         return 1;
311     }
312     if(index == 1001 && direction >= 0 || listEnd == 1001 && direction == 4) {
313         page += 1000;
314         GameListReplace(page);
315         return 1;
316     }
317
318     if(direction != 0) {
319         int doLoad = abs(direction) == 3;
320         if(doLoad) direction /= 3;
321         index += direction;
322         if(direction < -1) index = 0;
323         if(direction >  1) index = listEnd-1;
324         if(index < 0 || index >= listEnd) return 1;
325         HighlightWithScroll(&gamesOptions[0], index, listEnd);
326         if(!doLoad) return 1;
327     }
328     index = atoi(list[index])-1; // [HGM] filter: read true index from sequence nr of line
329     if (cmailMsgLoaded) {
330         CmailLoadGame(glc->fp, index + 1, glc->filename, True);
331     } else {
332         LoadGame(glc->fp, index + 1, glc->filename, True);
333     }
334     return 0;
335 }
336
337 void
338 SetFilter ()
339 {
340         char *name;
341         GetWidgetText(&gamesOptions[1], &name);
342         safeStrCpy(filterString, name, sizeof(filterString)/sizeof(filterString[0]));
343         GameListPrepare(False, False); GameListReplace(0);
344         UnCaret(); // filter text-edit
345         FocusOnWidget(&gamesOptions[0], GameListDlg); // listbox
346 }
347
348 void
349 GameListHighlight (int index)
350 {
351     int i=0; char **st;
352     if (!shellUp[GameListDlg]) return;
353     st = list;
354     while(*st && atoi(*st)<index) st++,i++;
355     HighlightWithScroll(&gamesOptions[0], i, listEnd);
356 }
357
358 int
359 SaveGameListAsText (FILE *f)
360 {
361     ListGame * lg = (ListGame *) gameList.head;
362     int nItem;
363
364     if( !glc || ((ListGame *) gameList.tailPred)->number <= 0 ) {
365       DisplayError(_("Game list not loaded or empty"), 0);
366         return False;
367     }
368
369     /* Copy the list into the global memory block */
370     if( f != NULL ) {
371
372         lg = (ListGame *) gameList.head;
373
374         for (nItem = 0; nItem < ((ListGame *) gameList.tailPred)->number; nItem++){
375             char * st = GameListLineFull(lg->number, &lg->gameInfo);
376             char *line = GameListLine(lg->number, &lg->gameInfo);
377             if(filterString[0] == NULLCHAR || SearchPattern( line, filterString ) )
378                     fprintf( f, "%s\n", st );
379             free(st); free(line);
380             lg = (ListGame *) lg->node.succ;
381         }
382
383         fclose(f);
384         return True;
385     }
386     return False;
387 }