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