Let engine-output memos use new generic callback
[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 static void *memoWidget;
87
88 #ifdef TODO_GTK
89 static void
90 ReadIcon (char *pixData[], int iconNr, Widget w)
91 {
92     int r;
93
94         if ((r=XpmCreatePixmapFromData(xDisplay, XtWindow(w),
95                                        pixData,
96                                        &(icons[iconNr]),
97                                        NULL, NULL /*&attr*/)) != 0) {
98           fprintf(stderr, _("Error %d loading icon image\n"), r);
99           exit(1);
100         }
101 }
102 #endif
103
104 void
105 InitEngineOutput (Option *opt, Option *memo2)
106 {       // front-end, because it must have access to the pixmaps
107 #ifdef TODO_GTK
108         Widget w = opt->handle;
109         memoWidget = memo2->handle;
110
111         ReadIcon(WHITE_14,   nColorWhite, w);
112         ReadIcon(BLACK_14,   nColorBlack, w);
113         ReadIcon(UNKNOWN_14, nColorUnknown, w);
114
115         ReadIcon(CLEAR_14,   nClear, w);
116         ReadIcon(PONDER_14,  nPondering, w);
117         ReadIcon(THINK_14,   nThinking, w);
118         ReadIcon(ANALYZE_14, nAnalyzing, w);
119 #endif
120 }
121
122 void
123 DrawWidgetIcon (Option *opt, int nIcon)
124 {   // as we are already in X front-end, so do X-stuff here
125 #ifdef TODO_GTK
126     gchar widgetname[50];
127
128     if( nIcon != 0 ) {
129         gtk_image_set_from_pixbuf(GTK_IMAGE(opt->handle), GDK_PIXBUF(iconsGTK[nIcon]));
130     }
131 #endif
132 }
133
134 void
135 InsertIntoMemo (int which, char * text, int where)
136 {
137     char *p;
138     GtkTextIter start;
139  
140     /* the backend adds \r\n, which is needed for winboard,
141      * for xboard we delete them again over here */
142     if(p = strchr(text, '\r')) *p = ' ';
143
144     GtkTextBuffer *tb = (GtkTextBuffer *) (engoutOptions[which ? 12 : 5].handle);
145 //    gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(tb), &start);
146     gtk_text_buffer_get_iter_at_offset(tb, &start, where);
147     gtk_text_buffer_insert(tb, &start, text, -1);
148     if(where < highTextStart[which]) { // [HGM] multiPVdisplay: move highlighting
149         int len = strlen(text);
150         highTextStart[which] += len; highTextEnd[which] += len;
151 #ifdef TODO_GTK
152         XawTextSetSelection( edit, highTextStart[which], highTextEnd[which] );
153 #endif
154     }
155 }
156
157 //--------------------------------- PV walking ---------------------------------------
158
159 char memoTranslations[] =
160 ":Ctrl<Key>c: CopyMemoProc() \n \
161 <Btn3Motion>: HandlePV() \n \
162 Shift<Btn3Down>: select-start() extend-end() SelectPV(1) \n \
163 Any<Btn3Down>: select-start() extend-end() SelectPV(0) \n \
164 <Btn3Up>: StopPV() \n";
165
166 //------------------------------- pane switching -----------------------------------
167
168 void
169 ResizeWindowControls (int mode)
170 {   // another hideous kludge: to have only a single pane, we resize the
171     // second to 5 pixels (which makes it too small to display anything)
172 #ifdef TODO_GTK
173     Widget form1, form2;
174     Arg args[16];
175     int j;
176     Dimension ew_height, tmp;
177     Widget shell = shells[EngOutDlg];
178
179     form1 = XtNameToWidget(shell, "*paneA");
180     form2 = XtNameToWidget(shell, "*paneB");
181
182     j = 0;
183     XtSetArg(args[j], XtNheight, (XtArgVal) &ew_height); j++;
184     XtGetValues(form1, args, j);
185     j = 0;
186     XtSetArg(args[j], XtNheight, (XtArgVal) &tmp); j++;
187     XtGetValues(form2, args, j);
188     ew_height += tmp; // total height
189
190     if(mode==0) {
191         j = 0;
192         XtSetArg(args[j], XtNheight, (XtArgVal) 5); j++;
193         XtSetValues(form2, args, j);
194         j = 0;
195         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height-5)); j++;
196         XtSetValues(form1, args, j);
197     } else {
198         j = 0;
199         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
200         XtSetValues(form1, args, j);
201         j = 0;
202         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
203         XtSetValues(form2, args, j);
204     }
205 #endif
206 }
207