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