Merge branch 'v4.7.x' into master
[xboard.git] / nengineoutput.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, 2013, 2014 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 "menus.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
68 /* Module variables */
69 int  windowMode = 1;
70
71 char *mem1, *mem2; // dummies, as this dialog can never be OK'ed
72 int highTextStart[2], highTextEnd[2];
73
74 int MemoProc P((Option *opt, int n, int x, int y, char *text, int index));
75
76 Option engoutOptions[] = {
77 {  0,  LL|T2T,           18, NULL, NULL, NULL, NULL, Icon, " " },
78 {  0, L2L|T2T|SAME_ROW, 162, NULL, NULL, NULL, NULL, Label, N_("engine name") },
79 {  0,     T2T|SAME_ROW,  30, NULL, NULL, NULL, NULL, Icon, " " },
80 /* TRANSLATORS: noun, as in "the move Nf3"*/
81 {  0, R2R|T2T|SAME_ROW, 188, NULL, NULL, NULL, NULL, Label, N_("move") },
82 {  0,  RR|T2T|SAME_ROW,  80, NULL, NULL, NULL, NULL, Label, N_("NPS") },
83 {200, T_VSCRL | T_TOP,  500, NULL, (void*) &mem1, "", (char**) MemoProc, TextBox, "" },
84 {  0,         0,         0, NULL, NULL, "", NULL, Break , "" },
85 {  0,  LL|T2T,           18, NULL, NULL, NULL, NULL, Icon, " " },
86 {  0, L2L|T2T|SAME_ROW, 162, NULL, NULL, NULL, NULL, Label, N_("engine name") },
87 {  0,     T2T|SAME_ROW,  30, NULL, NULL, NULL, NULL, Icon, " " },
88 /* TRANSLATORS: noun, as in "the move Nf3"*/
89 {  0, R2R|T2T|SAME_ROW, 188, NULL, NULL, NULL, NULL, Label, N_("move") },
90 {  0,  RR|T2T|SAME_ROW,  80, NULL, NULL, NULL, NULL, Label, N_("NPS") },
91 {200, T_VSCRL | T_TOP,  500, NULL, (void*) &mem2, "", (char**) MemoProc, TextBox, "" },
92 {   0,      NO_OK,       0, NULL, NULL, "", NULL, EndMark , "" }
93 };
94
95 int
96 MemoProc (Option *opt, int n, int x, int y, char *text, int index)
97 {   // user callback for mouse events in memo
98     static int pressed; // keep track of button 3 state
99     int start, end, currentPV = (opt != &engoutOptions[5]);
100
101     switch(n) {
102       case 0: // pointer motion
103         if(!pressed) return FALSE; // only motion with button 3 down is of interest
104         MovePV(x, y, 500/*lineGap + BOARD_HEIGHT * (squareSize + lineGap)*/);
105         break;
106       case 3: // press button 3
107         pressed = 1;
108         if(LoadMultiPV(x, y, text, index, &start, &end, currentPV)) {
109             highTextStart[currentPV] = start; highTextEnd[currentPV] = end;
110             HighlightText(&engoutOptions[currentPV ? 12 : 5], start, end, TRUE);
111         }
112         break;
113       case -3: // release button 3
114         pressed = 0;
115         if(highTextStart[currentPV] != highTextEnd[currentPV])
116             HighlightText(&engoutOptions[currentPV ? 12 : 5], highTextStart[currentPV], highTextEnd[currentPV], FALSE);
117         highTextStart[currentPV] = highTextEnd[currentPV] = 0;
118         UnLoadPV();
119         break;
120       default:
121         return FALSE; // not meant for us; do regular event handler
122     }
123     return TRUE;
124 }
125
126 void
127 SetIcon (int which, int field, int nIcon)
128 {   // first call into xengineoutput.c to pick up icon pixmap
129     if( nIcon ) DrawWidgetIcon(&engoutOptions[STRIDE*which + field - 1], nIcon);
130 }
131
132 void
133 DoSetWindowText (int which, int field, char *s_label)
134 {
135         SetWidgetLabel (&engoutOptions[STRIDE*which + field - 1], s_label);
136 }
137
138 void
139 SetEngineOutputTitle (char *title)
140 {
141         SetDialogTitle(EngOutDlg, title);
142 }
143
144
145 void
146 DoClearMemo (int which)
147 {
148       SetWidgetText(&engoutOptions[STRIDE*which + MEMO], "", -1);
149 }
150
151 void
152 EngineOutputPopUp ()
153 {
154     static int  needInit = TRUE;
155     static char *title = N_("Engine output");
156
157     if (GenericPopUp(engoutOptions, _(title), EngOutDlg, BoardWindow, NONMODAL, appData.topLevel)) {
158         if(engoutOptions[STRIDE-1].type != Break)
159             DisplayFatalError(_("Mismatch of STRIDE in nengineoutput.c\nChange and recompile!"), 0, 2);
160         AddHandler(&engoutOptions[MEMO], EngOutDlg, 6);
161         AddHandler(&engoutOptions[MEMO+STRIDE], EngOutDlg, 6);
162         if( needInit ) {
163             InitEngineOutput(&engoutOptions[0], &engoutOptions[MEMO]); // make icon bitmaps
164             needInit = FALSE;
165         }
166         SetEngineColorIcon( 0 );
167         SetEngineColorIcon( 1 );
168         SetEngineState( 0, STATE_IDLE, "" );
169         SetEngineState( 1, STATE_IDLE, "" );
170     } else {
171         SetIconName(EngOutDlg, _(title));
172         SetDialogTitle(EngOutDlg, _(title));
173     }
174
175     MarkMenu("View.EngineOutput", EngOutDlg);
176
177     ShowThinkingEvent(); // [HGM] thinking: might need to prompt engine for thinking output
178 }
179
180 int
181 EngineOutputIsUp ()
182 {
183     return shellUp[EngOutDlg];
184 }
185
186 int
187 EngineOutputDialogExists ()
188 {
189     return DialogExists(EngOutDlg);
190 }
191
192 void
193 EngineOutputProc ()
194 {
195   if (!PopDown(EngOutDlg)) EngineOutputPopUp();
196 }