Updated copyright notice to 2014
[xboard.git] / xaw / xengineoutput.c
1 /*
2  * Engine output (PV)
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  *
6  * Copyright 2005 Alessandro Scotti
7  *
8  * Enhancements Copyright 2009, 2010, 2011, 2012, 2013,
9  * 2014 Free Software Foundation, Inc.
10  *
11  * ------------------------------------------------------------------------
12  *
13  * GNU XBoard is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or (at
16  * your option) any later version.
17  *
18  * GNU XBoard is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see http://www.gnu.org/licenses/.
25  *
26  * ------------------------------------------------------------------------
27  ** See the file ChangeLog for a revision history.  */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if STDC_HEADERS
37 # include <stdlib.h>
38 # include <string.h>
39 #else /* not STDC_HEADERS */
40 extern char *getenv();
41 # if HAVE_STRING_H
42 #  include <string.h>
43 # else /* not HAVE_STRING_H */
44 #  include <strings.h>
45 # endif /* not HAVE_STRING_H */
46 #endif /* not STDC_HEADERS */
47
48 #if HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 #include <X11/Intrinsic.h>
53 #include <X11/StringDefs.h>
54 #include <X11/Shell.h>
55 #include <X11/Xaw/Dialog.h>
56 #include <X11/Xaw/Form.h>
57 #include <X11/Xaw/List.h>
58 #include <X11/Xaw/Label.h>
59 #include <X11/Xaw/SimpleMenu.h>
60 #include <X11/Xaw/SmeBSB.h>
61 #include <X11/Xaw/SmeLine.h>
62 #include <X11/Xaw/Box.h>
63 #include <X11/Xaw/Paned.h>
64 #include <X11/Xaw/MenuButton.h>
65 #include <X11/cursorfont.h>
66 #include <X11/Xaw/Text.h>
67 #include <X11/Xaw/AsciiText.h>
68 #include <X11/Xaw/Viewport.h>
69 #include <X11/Xatom.h>
70 #include <X11/Xmu/Atoms.h>
71
72 #include "common.h"
73 #include "frontend.h"
74 #include "backend.h"
75 #include "dialogs.h"
76 #include "xboard.h"
77 #include "engineoutput.h"
78 #include "gettext.h"
79
80 #ifdef ENABLE_NLS
81 # define  _(s) gettext (s)
82 # define N_(s) gettext_noop (s)
83 #else
84 # define  _(s) (s)
85 # define N_(s)  s
86 #endif
87
88 #include <X11/xpm.h>
89
90 // [HGM] pixmaps of some ICONS used in the engine-outut window
91 #include "pixmaps/WHITE_14.xpm"
92 #include "pixmaps/BLACK_14.xpm"
93 #include "pixmaps/CLEAR_14.xpm"
94 #include "pixmaps/UNKNOWN_14.xpm"
95 #include "pixmaps/THINKING_14.xpm"
96 #include "pixmaps/PONDER_14.xpm"
97 #include "pixmaps/ANALYZING_14.xpm"
98
99
100 /* Module variables */
101 static int currentPV;
102 static Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
103 static Widget memoWidget;
104
105
106 static void
107 ReadIcon (char *pixData[], int iconNr, Widget w)
108 {
109     int r;
110
111         if ((r=XpmCreatePixmapFromData(xDisplay, XtWindow(w),
112                                        pixData,
113                                        &(icons[iconNr]),
114                                        NULL, NULL /*&attr*/)) != 0) {
115           fprintf(stderr, _("Error %d loading icon image\n"), r);
116           exit(1);
117         }
118 }
119
120 void
121 InitEngineOutput (Option *opt, Option *memo2)
122 {       // front-end, because it must have access to the pixmaps
123         Widget w = opt->handle;
124         memoWidget = memo2->handle;
125
126         ReadIcon(WHITE_14,   nColorWhite, w);
127         ReadIcon(BLACK_14,   nColorBlack, w);
128         ReadIcon(UNKNOWN_14, nColorUnknown, w);
129
130         ReadIcon(CLEAR_14,   nClear, w);
131         ReadIcon(PONDER_14,  nPondering, w);
132         ReadIcon(THINK_14,   nThinking, w);
133         ReadIcon(ANALYZE_14, nAnalyzing, w);
134 }
135
136 void
137 DrawWidgetIcon (Option *opt, int nIcon)
138 {   // as we are already in X front-end, so do X-stuff here
139     Arg arg;
140     XtSetArg(arg, XtNleftBitmap, (XtArgVal) icons[nIcon]);
141     XtSetValues(opt->handle, &arg, 1);
142 }
143
144 void
145 InsertIntoMemo (int which, char * text, int where)
146 {
147         XawTextBlock t;
148         Widget edit;
149
150         /* the backend adds \r\n, which is needed for winboard,
151          * for xboard we delete them again over here */
152         if(t.ptr = strchr(text, '\r')) *t.ptr = ' ';
153
154         t.ptr = text; t.firstPos = 0; t.length = strlen(text); t.format = XawFmt8Bit;
155         edit = XtNameToWidget(shells[EngOutDlg], which ? "*paneB.text" : "*paneA.text");
156         XawTextReplace(edit, where, where, &t);
157         if(where < highTextStart[which]) { // [HGM] multiPVdisplay: move highlighting
158             int len = strlen(text);
159             highTextStart[which] += len; highTextEnd[which] += len;
160             XawTextSetSelection( edit, highTextStart[which], highTextEnd[which] );
161         }
162 }
163
164 //--------------------------------- PV walking ---------------------------------------
165
166 char memoTranslations[] =
167 ":Ctrl<Key>c: CopyMemoProc() \n \
168 <Btn3Motion>: HandlePV() \n \
169 Shift<Btn3Down>: select-start() extend-end(PRIMARY) SelectPV(1) \n \
170 Any<Btn3Down>: select-start() extend-end(PRIMARY) SelectPV(0) \n \
171 <Btn3Up>: StopPV() \n";
172
173 void
174 SelectPV (Widget w, XEvent * event, String * params, Cardinal * nParams)
175 {       // [HGM] pv: translate click to PV line, and load it for display
176         String val;
177         int start, end;
178         XawTextPosition index, dummy;
179         int x, y;
180         Arg arg;
181
182         x = event->xmotion.x; y = event->xmotion.y;
183         currentPV = (w != memoWidget);
184         XawTextGetSelectionPos(w, &index, &dummy);
185         XtSetArg(arg, XtNstring, &val);
186         XtGetValues(w, &arg, 1);
187         shiftKey = strcmp(params[0], "0");
188         if(LoadMultiPV(x, y, val, index, &start, &end, currentPV)) {
189             XawTextSetSelection( w, start, end );
190             highTextStart[currentPV] = start; highTextEnd[currentPV] = end;
191         }
192 }
193
194 void
195 StopPV (Widget w, XEvent * event, String * params, Cardinal * nParams)
196 {       // [HGM] pv: on right-button release, stop displaying PV
197         XawTextUnsetSelection( w );
198         highTextStart[currentPV] = highTextEnd[currentPV] = 0;
199         UnLoadPV();
200         XtCallActionProc(w, "beginning-of-file", event, NULL, 0);
201 }
202
203 //------------------------- Ctrl-C copying of memo texts ---------------------------
204
205 // Awfull code: first read our own primary selection into selected_fen_position,
206 //              and then transfer ownership of this to the clipboard, so that the
207 //              copy-position callback can fetch it there when somebody pastes it
208 // Worst of all is that I only added it because I did not know how to copy primary:
209 // my laptop has no middle button. Ctrl-C might not be needed at all... [HGM]
210
211 // cloned from CopyPositionProc. Abuse selected_fen_position to hold selection
212
213 Boolean SendPositionSelection(Widget w, Atom *selection, Atom *target,
214                  Atom *type_return, XtPointer *value_return,
215                  unsigned long *length_return, int *format_return); // from xboard.c
216
217 static void
218 MemoCB (Widget w, XtPointer client_data, Atom *selection,
219         Atom *type, XtPointer value, unsigned long *len, int *format)
220 {
221   if (value==NULL || *len==0) return; /* nothing had been selected to copy */
222   selected_fen_position = value;
223   selected_fen_position[*len]='\0'; /* normally this string is terminated, but be safe */
224     XtOwnSelection(menuBarWidget, XA_CLIPBOARD(xDisplay),
225                    CurrentTime,
226                    SendPositionSelection,
227                    NULL/* lose_ownership_proc */ ,
228                    NULL/* transfer_done_proc */);
229 }
230
231 void
232 CopyMemoProc (Widget w, XEvent *event, String *prms, Cardinal *nprms)
233 {
234     if(appData.pasteSelection) return;
235     if (selected_fen_position) free(selected_fen_position);
236     XtGetSelectionValue(menuBarWidget,
237       XA_PRIMARY, XA_STRING,
238       /* (XtSelectionCallbackProc) */ MemoCB,
239       NULL, /* client_data passed to PastePositionCB */
240
241       /* better to use the time field from the event that triggered the
242        * call to this function, but that isn't trivial to get
243        */
244       CurrentTime
245     );
246 }
247
248 //------------------------------- pane switching -----------------------------------
249
250 void
251 ResizeWindowControls (int mode)
252 {   // another hideous kludge: to have only a single pane, we resize the
253     // second to 5 pixels (which makes it too small to display anything)
254     Widget form1, form2;
255     Arg args[16];
256     int j;
257     Dimension ew_height, tmp;
258     Widget shell = shells[EngOutDlg];
259
260     form1 = XtNameToWidget(shell, "*paneA");
261     form2 = XtNameToWidget(shell, "*paneB");
262
263     j = 0;
264     XtSetArg(args[j], XtNheight, (XtArgVal) &ew_height); j++;
265     XtGetValues(form1, args, j);
266     j = 0;
267     XtSetArg(args[j], XtNheight, (XtArgVal) &tmp); j++;
268     XtGetValues(form2, args, j);
269     ew_height += tmp; // total height
270
271     if(mode==0) {
272         j = 0;
273         XtSetArg(args[j], XtNheight, (XtArgVal) 5); j++;
274         XtSetValues(form2, args, j);
275         j = 0;
276         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height-5)); j++;
277         XtSetValues(form1, args, j);
278     } else {
279         j = 0;
280         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
281         XtSetValues(form1, args, j);
282         j = 0;
283         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
284         XtSetValues(form2, args, j);
285     }
286 }