d79e7e862f35968c3f6c9b2790d57fa8ee7fd58d
[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 "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 // templates for calls into back-end
62 void RefreshMemoContent P((void));
63 void MemoContentUpdated P((void));
64 void FindMoveByCharIndex P(( int char_index ));
65
66 int AppendText P((Option *opt, char *s));
67 int GenericPopUp P((Option *option, char *title, int dlgNr));
68 void MarkMenu P((char *item, int dlgNr));
69 void GetWidgetText P((Option *opt, char **buf));
70
71 extern Option historyOptions[];
72 extern Widget shells[10];
73 extern Boolean shellUp[10];
74
75 // ------------- low-level front-end actions called by MoveHistory back-end -----------------
76
77 void HighlightMove( int from, int to, Boolean highlight )
78 {
79     if(highlight)
80         XawTextSetSelection( historyOptions[0].handle, from, to ); // for lack of a better method, use selection for highighting
81 }
82
83 void ClearHistoryMemo()
84 {
85     ClearTextWidget(&historyOptions[0]);
86 }
87
88 // the bold argument says 0 = normal, 1 = bold typeface
89 // the colorNr argument says 0 = font-default, 1 = gray
90 int AppendToHistoryMemo( char * text, int bold, int colorNr )
91 {
92     Arg args[10];
93     return AppendText(&historyOptions[0], text); // for now ignore bold & color stuff, as Xaw cannot handle that
94 }
95
96 void ScrollToCurrent(int caretPos)
97 {
98     Arg args[10];
99     char *s;
100     int len;
101     GetWidgetText(&historyOptions[0], &s);
102     len = strlen(s);
103     if(caretPos < 0 || caretPos > len) caretPos = len;
104     if(caretPos > len-30) { // scroll to end, which causes no flicker
105       static XEvent event;
106       XtCallActionProc(historyOptions[0].handle, "end-of-file", &event, NULL, 0);
107       return;
108     }
109     // the following leads to a very annoying flicker, even when no scrolling is done at all.
110     XtSetArg(args[0], XtNinsertPosition, caretPos); // this triggers scrolling in Xaw
111     XtSetArg(args[1], XtNdisplayCaret, False);
112     XtSetValues(historyOptions[0].handle, args, 2);
113 }
114
115
116 // ------------------------------ callbacks --------------------------
117
118 char *historyText;
119 char historyTranslations[] =
120 "<Btn3Down>: select-start() \n \
121 <Btn3Up>: extend-end() SelectMove() \n";
122
123 void
124 SelectMove (Widget w, XEvent * event, String * params, Cardinal * nParams)
125 {
126         XawTextPosition index, dummy;
127
128         XawTextGetSelectionPos(w, &index, &dummy);
129         FindMoveByCharIndex( index ); // [HGM] also does the actual moving to it, now
130 }
131
132 Option historyOptions[] = {
133 { 0xD, 200, 400, NULL, (void*) &historyText, "", NULL, TextBox, "" },
134 {   0,  2,    0, NULL, (void*) NULL, "", NULL, EndMark , "" }
135 };
136
137 // ------------ standard entry points into MoveHistory code -----------
138
139 Boolean MoveHistoryIsUp()
140 {
141     return shellUp[7];
142 }
143
144 Boolean MoveHistoryDialogExists()
145 {
146     return shells[7] != NULL;
147 }
148
149 void HistoryPopUp()
150 {
151     if(GenericPopUp(historyOptions, _("Move list"), 7))
152         XtOverrideTranslations(historyOptions[0].handle, XtParseTranslationTable(historyTranslations));
153     MarkMenu("menuView.Show Move History", 7);
154 }
155
156 void
157 HistoryShowProc(w, event, prms, nprms)
158      Widget w;
159      XEvent *event;
160      String *prms;
161      Cardinal *nprms;
162 {
163   if (!shellUp[7]) {
164     ASSIGN(historyText, "");
165     HistoryPopUp();
166     RefreshMemoContent();
167     MemoContentUpdated();
168   } else PopDown(7);
169   ToNrEvent(currentMove);
170 }
171
172 // duplicate of code in winboard.c, so an move to back-end!
173 void
174 HistorySet( char movelist[][2*MOVE_LEN], int first, int last, int current )
175 {
176     MoveHistorySet( movelist, first, last, current, pvInfoList );
177
178     EvalGraphSet( first, last, current, pvInfoList );
179 }
180