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