7bd3cb7c903e07d02a422ad345693aaf347704d5
[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
27 #include <X11/Intrinsic.h>
28 #include <X11/StringDefs.h>
29 #include <X11/Shell.h>
30 #include <X11/Xaw/Dialog.h>
31 #include <X11/Xaw/Form.h>
32 #include <X11/Xaw/List.h>
33 #include <X11/Xaw/Label.h>
34 #include <X11/Xaw/SimpleMenu.h>
35 #include <X11/Xaw/SmeBSB.h>
36 #include <X11/Xaw/SmeLine.h>
37 #include <X11/Xaw/Box.h>
38 #include <X11/Xaw/Paned.h>
39 #include <X11/Xaw/MenuButton.h>
40 #include <X11/cursorfont.h>
41 #include <X11/Xaw/Text.h>
42 #include <X11/Xaw/AsciiText.h>
43 #include <X11/Xaw/Viewport.h>
44 #include <X11/Xatom.h>
45 #include <X11/Xmu/Atoms.h>
46
47 #include "common.h"
48 #include "frontend.h"
49 #include "backend.h"
50 #include "xhistory.h"
51 #include "xboard.h"
52 #include "gettext.h"
53
54 #ifdef ENABLE_NLS
55 # define  _(s) gettext (s)
56 # define N_(s) gettext_noop (s)
57 #else
58 # define  _(s) (s)
59 # define N_(s)  s
60 #endif
61
62 // templates for calls into back-end (= history.c; should be moved to history.h header shared with it!)
63 void RefreshMemoContent P((void));
64 void MemoContentUpdated P((void));
65 void FindMoveByCharIndex P(( int char_index ));
66
67 // variables in xoptions.c
68 extern Option historyOptions[];
69
70 // ------------- low-level front-end actions called by MoveHistory back-end -----------------
71
72 void HighlightMove( int from, int to, Boolean highlight )
73 {
74     if(highlight)
75         XawTextSetSelection( historyOptions[0].handle, from, to ); // for lack of a better method, use selection for highighting
76 }
77
78 void ClearHistoryMemo()
79 {
80     ClearTextWidget(&historyOptions[0]);
81 }
82
83 // the bold argument says 0 = normal, 1 = bold typeface
84 // the colorNr argument says 0 = font-default, 1 = gray
85 int AppendToHistoryMemo( char * text, int bold, int colorNr )
86 {
87     return AppendText(&historyOptions[0], text); // for now ignore bold & color stuff, as Xaw cannot handle that
88 }
89
90 void ScrollToCurrent(int caretPos)
91 {
92     Arg args[10];
93     char *s;
94     int len;
95     GetWidgetText(&historyOptions[0], &s);
96     len = strlen(s);
97     if(caretPos < 0 || caretPos > len) caretPos = len;
98     if(caretPos > len-30) { // scroll to end, which causes no flicker
99       static XEvent event;
100       XtCallActionProc(historyOptions[0].handle, "end-of-file", &event, NULL, 0);
101       return;
102     }
103     // the following leads to a very annoying flicker, even when no scrolling is done at all.
104     XtSetArg(args[0], XtNinsertPosition, caretPos); // this triggers scrolling in Xaw
105     XtSetArg(args[1], XtNdisplayCaret, False);
106     XtSetValues(historyOptions[0].handle, args, 2);
107 }
108
109
110 // ------------------------------ callbacks --------------------------
111
112 char *historyText;
113 char historyTranslations[] =
114 "<Btn3Down>: select-start() \n \
115 <Btn3Up>: extend-end() SelectMove() \n";
116
117 void
118 SelectMove (Widget w, XEvent * event, String * params, Cardinal * nParams)
119 {
120         XawTextPosition index, dummy;
121
122         XawTextGetSelectionPos(w, &index, &dummy);
123         FindMoveByCharIndex( index ); // [HGM] also does the actual moving to it, now
124 }
125
126 Option historyOptions[] = {
127 { 0xD, 200, 400, NULL, (void*) &historyText, "", NULL, TextBox, "" },
128 {   0,  2,    0, NULL, (void*) NULL, "", NULL, EndMark , "" }
129 };
130
131 // ------------ standard entry points into MoveHistory code -----------
132
133 Boolean MoveHistoryIsUp()
134 {
135     return shellUp[7];
136 }
137
138 Boolean MoveHistoryDialogExists()
139 {
140     return shells[7] != NULL;
141 }
142
143 void HistoryPopUp()
144 {
145     if(GenericPopUp(historyOptions, _("Move list"), 7))
146         XtOverrideTranslations(historyOptions[0].handle, XtParseTranslationTable(historyTranslations));
147     MarkMenu("menuView.Show Move History", 7);
148 }
149
150 void
151 HistoryShowProc(w, event, prms, nprms)
152      Widget w;
153      XEvent *event;
154      String *prms;
155      Cardinal *nprms;
156 {
157   if (!shellUp[7]) {
158     ASSIGN(historyText, "");
159     HistoryPopUp();
160     RefreshMemoContent();
161     MemoContentUpdated();
162   } else PopDown(7);
163   ToNrEvent(currentMove);
164 }