f83dcf07916bf5ab4ff9845b8d1bdad785016ba5
[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 extern Option engoutOptions[]; // must go in header, but which?
79
80 /* Module variables */
81 static int currentPV, highTextStart[2], highTextEnd[2];
82 #ifdef TODO_GTK
83 static Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
84 static Widget memoWidget;
85 #endif
86
87 #ifdef TODO_GTK
88 static void
89 ReadIcon (char *pixData[], int iconNr, Widget w)
90 {
91     int r;
92
93         if ((r=XpmCreatePixmapFromData(xDisplay, XtWindow(w),
94                                        pixData,
95                                        &(icons[iconNr]),
96                                        NULL, NULL /*&attr*/)) != 0) {
97           fprintf(stderr, _("Error %d loading icon image\n"), r);
98           exit(1);
99         }
100 }
101 #endif
102
103 void
104 InitEngineOutput (Option *opt, Option *memo2)
105 {       // front-end, because it must have access to the pixmaps
106 #ifdef TODO_GTK
107         Widget w = opt->handle;
108         memoWidget = memo2->handle;
109
110         ReadIcon(WHITE_14,   nColorWhite, w);
111         ReadIcon(BLACK_14,   nColorBlack, w);
112         ReadIcon(UNKNOWN_14, nColorUnknown, w);
113
114         ReadIcon(CLEAR_14,   nClear, w);
115         ReadIcon(PONDER_14,  nPondering, w);
116         ReadIcon(THINK_14,   nThinking, w);
117         ReadIcon(ANALYZE_14, nAnalyzing, w);
118 #endif
119 }
120
121 void
122 DrawWidgetIcon (Option *opt, int nIcon)
123 {   // as we are already in X front-end, so do X-stuff here
124 #ifdef TODO_GTK
125     gchar widgetname[50];
126
127     if( nIcon != 0 ) {
128         gtk_image_set_from_pixbuf(GTK_IMAGE(opt->handle), GDK_PIXBUF(iconsGTK[nIcon]));
129     }
130 #endif
131 }
132
133 void
134 InsertIntoMemo (int which, char * text, int where)
135 {
136     char *p;
137     GtkTextIter start;
138  
139     /* the backend adds \r\n, which is needed for winboard,
140      * for xboard we delete them again over here */
141     if(p = strchr(text, '\r')) *p = ' ';
142
143     GtkTextBuffer *tb = (GtkTextBuffer *) (engoutOptions[which ? 12 : 5].handle);
144 //    gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tb), &start);
145     gtk_text_buffer_get_iter_at_offset(tb, &start, where);
146     gtk_text_buffer_insert(tb, &start, text, -1);
147     if(where < highTextStart[which]) { // [HGM] multiPVdisplay: move highlighting
148         int len = strlen(text);
149         highTextStart[which] += len; highTextEnd[which] += len;
150 #ifdef TODO_GTK
151         XawTextSetSelection( edit, highTextStart[which], highTextEnd[which] );
152 #endif
153     }
154 }
155
156 //--------------------------------- PV walking ---------------------------------------
157
158 char memoTranslations[] =
159 ":Ctrl<Key>c: CopyMemoProc() \n \
160 <Btn3Motion>: HandlePV() \n \
161 Shift<Btn3Down>: select-start() extend-end() SelectPV(1) \n \
162 Any<Btn3Down>: select-start() extend-end() SelectPV(0) \n \
163 <Btn3Up>: StopPV() \n";
164
165 #ifdef TODO_GTK
166 void
167 SelectPV (Widget w, XEvent * event, String * params, Cardinal * nParams)
168 {       // [HGM] pv: translate click to PV line, and load it for display
169         String val;
170         int start, end;
171         XawTextPosition index, dummy;
172         int x, y;
173         Arg arg;
174
175         x = event->xmotion.x; y = event->xmotion.y;
176         currentPV = (w != memoWidget);
177         XawTextGetSelectionPos(w, &index, &dummy);
178         XtSetArg(arg, XtNstring, &val);
179         XtGetValues(w, &arg, 1);
180         shiftKey = strcmp(params[0], "0");
181         if(LoadMultiPV(x, y, val, index, &start, &end, currentPV)) {
182             XawTextSetSelection( w, start, end );
183             highTextStart[currentPV] = start; highTextEnd[currentPV] = end;
184         }
185 }
186 #endif
187
188 #ifdef TODO_GTK
189 void
190 StopPV (Widget w, XEvent * event, String * params, Cardinal * nParams)
191 {       // [HGM] pv: on right-button release, stop displaying PV
192         XawTextUnsetSelection( w );
193         highTextStart[currentPV] = highTextEnd[currentPV] = 0;
194         UnLoadPV();
195         XtCallActionProc(w, "beginning-of-file", event, NULL, 0);
196 }
197 #endif
198
199 //------------------------------- pane switching -----------------------------------
200
201 void
202 ResizeWindowControls (int mode)
203 {   // another hideous kludge: to have only a single pane, we resize the
204     // second to 5 pixels (which makes it too small to display anything)
205 #ifdef TODO_GTK
206     Widget form1, form2;
207     Arg args[16];
208     int j;
209     Dimension ew_height, tmp;
210     Widget shell = shells[EngOutDlg];
211
212     form1 = XtNameToWidget(shell, "*paneA");
213     form2 = XtNameToWidget(shell, "*paneB");
214
215     j = 0;
216     XtSetArg(args[j], XtNheight, (XtArgVal) &ew_height); j++;
217     XtGetValues(form1, args, j);
218     j = 0;
219     XtSetArg(args[j], XtNheight, (XtArgVal) &tmp); j++;
220     XtGetValues(form2, args, j);
221     ew_height += tmp; // total height
222
223     if(mode==0) {
224         j = 0;
225         XtSetArg(args[j], XtNheight, (XtArgVal) 5); j++;
226         XtSetValues(form2, args, j);
227         j = 0;
228         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height-5)); j++;
229         XtSetValues(form1, args, j);
230     } else {
231         j = 0;
232         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
233         XtSetValues(form1, args, j);
234         j = 0;
235         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
236         XtSetValues(form2, args, j);
237     }
238 #endif
239 }
240