Implement searching games in Game List for a position
[xboard.git] / xhistory.c
1 /*
2  * New (WinBoard-style) Move history for XBoard
3  *
4  * ------------------------------------------------------------------------
5  *
6  * GNU XBoard is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or (at
9  * your option) any later version.
10  *
11  * GNU XBoard is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://www.gnu.org/licenses/. 
18  *
19  * ------------------------------------------------------------------------
20  ** See the file ChangeLog for a revision history.  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <malloc.h>
27
28 #include <X11/Intrinsic.h>
29 #include <X11/StringDefs.h>
30 #include <X11/Shell.h>
31 #include <X11/Xaw/Dialog.h>
32 #include <X11/Xaw/Form.h>
33 #include <X11/Xaw/List.h>
34 #include <X11/Xaw/Label.h>
35 #include <X11/Xaw/SimpleMenu.h>
36 #include <X11/Xaw/SmeBSB.h>
37 #include <X11/Xaw/SmeLine.h>
38 #include <X11/Xaw/Box.h>
39 #include <X11/Xaw/Paned.h>
40 #include <X11/Xaw/MenuButton.h>
41 #include <X11/cursorfont.h>
42 #include <X11/Xaw/Text.h>
43 #include <X11/Xaw/AsciiText.h>
44 #include <X11/Xaw/Viewport.h>
45 #include <X11/Xatom.h>
46 #include <X11/Xmu/Atoms.h>
47
48 #include "common.h"
49 #include "frontend.h"
50 #include "backend.h"
51 #include "xhistory.h"
52 #include "xboard.h"
53 #include "gettext.h"
54
55 #ifdef ENABLE_NLS
56 # define  _(s) gettext (s)
57 # define N_(s) gettext_noop (s)
58 #else
59 # define  _(s) (s)
60 # define N_(s)  s
61 #endif
62
63 // templates for calls into back-end (= history.c; should be moved to history.h header shared with it!)
64 void RefreshMemoContent P((void));
65 void MemoContentUpdated P((void));
66 void FindMoveByCharIndex P(( int char_index ));
67 void MoveHistorySet P(( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo ));
68
69 // variables in xoptions.c
70 extern Option historyOptions[];
71
72 // ------------- low-level front-end actions called by MoveHistory back-end -----------------
73
74 void HighlightMove( int from, int to, Boolean highlight )
75 {
76     if(highlight)
77         XawTextSetSelection( historyOptions[0].handle, from, to ); // for lack of a better method, use selection for highighting
78 }
79
80 void ClearHistoryMemo()
81 {
82     ClearTextWidget(&historyOptions[0]);
83 }
84
85 // the bold argument says 0 = normal, 1 = bold typeface
86 // the colorNr argument says 0 = font-default, 1 = gray
87 int AppendToHistoryMemo( char * text, int bold, int colorNr )
88 {
89     return AppendText(&historyOptions[0], text); // for now ignore bold & color stuff, as Xaw cannot handle that
90 }
91
92 void ScrollToCurrent(int caretPos)
93 {
94     Arg args[10];
95     char *s;
96     int len;
97     GetWidgetText(&historyOptions[0], &s);
98     len = strlen(s);
99     if(caretPos < 0 || caretPos > len) caretPos = len;
100     if(caretPos > len-30) { // scroll to end, which causes no flicker
101       static XEvent event;
102       XtCallActionProc(historyOptions[0].handle, "end-of-file", &event, NULL, 0);
103       return;
104     }
105     // the following leads to a very annoying flicker, even when no scrolling is done at all.
106     XtSetArg(args[0], XtNinsertPosition, caretPos); // this triggers scrolling in Xaw
107     XtSetArg(args[1], XtNdisplayCaret, False);
108     XtSetValues(historyOptions[0].handle, args, 2);
109 }
110
111
112 // ------------------------------ callbacks --------------------------
113
114 char *historyText;
115 char historyTranslations[] =
116 "<Btn3Down>: select-start() \n \
117 <Btn3Up>: extend-end() SelectMove() \n";
118
119 void
120 SelectMove (Widget w, XEvent * event, String * params, Cardinal * nParams)
121 {
122         XawTextPosition index, dummy;
123
124         XawTextGetSelectionPos(w, &index, &dummy);
125         FindMoveByCharIndex( index ); // [HGM] also does the actual moving to it, now
126 }
127
128 Option historyOptions[] = {
129 { 0xD, 200, 400, NULL, (void*) &historyText, "", NULL, TextBox, "" },
130 {   0,  2,    0, NULL, (void*) NULL, "", NULL, EndMark , "" }
131 };
132
133 // ------------ standard entry points into MoveHistory code -----------
134
135 Boolean MoveHistoryIsUp()
136 {
137     return shellUp[7];
138 }
139
140 Boolean MoveHistoryDialogExists()
141 {
142     return shells[7] != NULL;
143 }
144
145 void HistoryPopUp()
146 {
147     if(GenericPopUp(historyOptions, _("Move list"), 7))
148         XtOverrideTranslations(historyOptions[0].handle, XtParseTranslationTable(historyTranslations));
149     MarkMenu("menuView.Show Move History", 7);
150 }
151
152 void
153 HistoryShowProc(w, event, prms, nprms)
154      Widget w;
155      XEvent *event;
156      String *prms;
157      Cardinal *nprms;
158 {
159   if (!shellUp[7]) {
160     ASSIGN(historyText, "");
161     HistoryPopUp();
162     RefreshMemoContent();
163     MemoContentUpdated();
164   } else PopDown(7);
165   ToNrEvent(currentMove);
166 }
167
168 // duplicate of code in winboard.c, so an move to back-end!
169 void EvalGraphSet P(( int first, int last, int current, ChessProgramStats_Move * pvInfo ));
170 void MakeEngineOutputTitle P(());
171
172 void
173 HistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current )
174 {
175     MoveHistorySet( movelist, first, last, current, pvInfoList );
176
177     EvalGraphSet( first, last, current, pvInfoList );
178
179     MakeEngineOutputTitle();
180 }
181