changes from Alessandro Scotti from 20051231
[xboard.git] / winboard / wengineoutput.c
1 /*
2  * Engine output (PV)
3  *
4  * Author: Alessandro Scotti
5  *
6  * ------------------------------------------------------------------------
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  * ------------------------------------------------------------------------
21  */
22 #include "config.h"
23
24 #include <windows.h> /* required for all Windows applications */
25 #include <richedit.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29 #include <commdlg.h>
30 #include <dlgs.h>
31
32 #include "common.h"
33 #include "winboard.h"
34 #include "frontend.h"
35 #include "backend.h"
36
37 #include "wsnap.h"
38
39 VOID EngineOutputPopUp();
40 VOID EngineOutputPopDown();
41 BOOL EngineOutputIsUp();
42
43 /* Imports from backend.c */
44 char * SavePart(char *str);
45
46 /* Imports from winboard.c */
47 extern HWND engineOutputDialog;
48 extern BOOLEAN engineOutputDialogUp;
49
50 extern HINSTANCE hInst;
51 extern HWND hwndMain;
52
53 extern WindowPlacement wpEngineOutput;
54
55 /* Module variables */
56 #define H_MARGIN            2
57 #define V_MARGIN            2
58 #define LABEL_V_DISTANCE    1   /* Distance between label and memo */
59 #define SPLITTER_SIZE       4   /* Distance between first memo and second label */
60
61 static int  windowMode = 1;
62
63 static int  lastDepth[2] = { -1, -1 };
64
65 static VOID SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
66 {
67     HWND hControl = GetDlgItem( hDlg, id );
68
69     SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
70 }
71
72 static VOID HideControl( HWND hDlg, int id )
73 {
74     /* TODO: we should also hide/disable it!!! what about tab stops?!?! */
75     SetControlPos( hDlg, id, 20000, 20000, 100, 100 );
76 }
77
78 static int GetControlWidth( HWND hDlg, int id )
79 {
80     RECT rc;
81
82     GetWindowRect( GetDlgItem( hDlg, IDC_EngineLabel1 ), &rc );
83
84     return rc.right - rc.left;
85 }
86
87 static VOID ResizeWindowControls( HWND hDlg, int mode )
88 {
89     RECT rc;
90     int labelHeight;
91     int clientWidth;
92     int clientHeight;
93     int maxControlWidth;
94     int npsWidth;
95
96     /* Initialize variables */
97     GetWindowRect( GetDlgItem( hDlg, IDC_EngineLabel1 ), &rc );
98
99     labelHeight = rc.bottom - rc.top;
100
101     GetClientRect( hDlg, &rc );
102
103     clientWidth = rc.right - rc.left;
104     clientHeight = rc.bottom - rc.top;
105
106     maxControlWidth = clientWidth - 2*H_MARGIN;
107
108     npsWidth = GetControlWidth( hDlg, IDC_Engine1_NPS );
109
110     /* Resize controls */
111     if( mode == 0 ) {
112         /* One engine */
113         int memo_y = V_MARGIN + labelHeight + LABEL_V_DISTANCE;
114         int memo_h = clientHeight - memo_y - V_MARGIN;
115
116         SetControlPos( hDlg, IDC_EngineLabel1, H_MARGIN, V_MARGIN, maxControlWidth / 2, labelHeight );
117         SetControlPos( hDlg, IDC_EngineMemo1, H_MARGIN, memo_y, maxControlWidth, memo_h );
118
119         /* Hide controls for the second engine */
120         HideControl( hDlg, IDC_EngineLabel2 );
121         HideControl( hDlg, IDC_Engine2_NPS );
122         HideControl( hDlg, IDC_EngineMemo2 );
123         /* TODO: we should also hide/disable them!!! what about tab stops?!?! */
124     }
125     else {
126         /* Two engines */
127         int memo1_y = V_MARGIN + labelHeight + LABEL_V_DISTANCE;
128         int memo_h = (clientHeight - memo1_y - V_MARGIN - labelHeight - LABEL_V_DISTANCE - SPLITTER_SIZE) / 2;
129         int label2_y = memo1_y + memo_h + SPLITTER_SIZE;
130         int memo2_y = label2_y + labelHeight + LABEL_V_DISTANCE;
131         int nps_x = clientWidth - H_MARGIN - npsWidth;
132
133         SetControlPos( hDlg, IDC_EngineLabel1, H_MARGIN, V_MARGIN, maxControlWidth / 2, labelHeight );
134         SetControlPos( hDlg, IDC_Engine1_NPS, nps_x, V_MARGIN, npsWidth, labelHeight );
135         SetControlPos( hDlg, IDC_EngineMemo1, H_MARGIN, memo1_y, maxControlWidth, memo_h );
136
137         SetControlPos( hDlg, IDC_EngineLabel2, H_MARGIN, label2_y, maxControlWidth / 2, labelHeight );
138         SetControlPos( hDlg, IDC_Engine2_NPS, nps_x, label2_y, npsWidth, labelHeight );
139         SetControlPos( hDlg, IDC_EngineMemo2, H_MARGIN, memo2_y, maxControlWidth, memo_h );
140     }
141
142     InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
143     InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
144 }
145
146 static VOID SetDisplayMode( int mode )
147 {
148     if( windowMode != mode ) {
149         windowMode = mode;
150
151         ResizeWindowControls( engineOutputDialog, mode );
152     }
153 }
154
155 static VOID VerifyDisplayMode()
156 {
157     int mode;
158
159     /* Get proper mode for current game */
160     switch( gameMode ) {
161     case AnalyzeMode:
162     case AnalyzeFile:
163     case MachinePlaysWhite:
164     case MachinePlaysBlack:
165         mode = 0;
166         break;
167     case TwoMachinesPlay:
168         mode = 1;
169         break;
170     default:
171         /* Do not change */
172         return;
173     }
174
175     SetDisplayMode( mode );
176 }
177
178 static VOID InsertIntoMemo( HWND hMemo, char * text )
179 {
180     SendMessage( hMemo, EM_SETSEL, 0, 0 );
181
182     SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
183 }
184
185 #define MAX_NAME_LENGTH 32
186
187 static VOID UpdateControls( HWND hLabel, HWND hLabelNPS, HWND hMemo, char * name, int depth, unsigned long nodes, int score, int time, char * pv )
188 {
189     char s_label[MAX_NAME_LENGTH + 64];
190
191     /* Label */
192     if( name == 0 || *name == '\0' ) {
193         name = "?";
194     }
195
196     strncpy( s_label, name, MAX_NAME_LENGTH );
197     s_label[ MAX_NAME_LENGTH-1 ] = '\0';
198
199     SetWindowText( hLabel, s_label );
200
201     s_label[0] = '\0';
202
203     if( time > 0 && nodes > 0 ) {
204         unsigned long nps_100 = nodes / time;
205
206         if( nps_100 < 100000 ) {
207             sprintf( s_label, "NPS: %lu", nps_100 * 100 );
208         }
209         else {
210             sprintf( s_label, "NPS: %.1fk", nps_100 / 10.0 );
211         }
212     }
213
214     SetWindowText( hLabelNPS, s_label );
215
216     /* Memo */
217     if( pv != 0 && *pv != '\0' ) {
218         char s_nodes[24];
219         char s_score[16];
220         char s_time[24];
221         char buf[256];
222         int buflen;
223         int time_secs = time / 100;
224         int time_cent = time % 100;
225
226         /* Nodes */
227         if( nodes < 1000000 ) {
228             sprintf( s_nodes, "%lu", nodes );
229         }
230         else {
231             sprintf( s_nodes, "%.1fM", nodes / 1000000.0 );
232         }
233
234         /* Score */
235         if( score > 0 ) {
236             sprintf( s_score, "+%.2f", score / 100.0 );
237         }
238         else {
239             sprintf( s_score, "%.2f", score / 100.0 );
240         }
241
242         /* Time */
243         sprintf( s_time, "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
244
245         /* Put all together... */
246         sprintf( buf, "%3d\t%s\t%s\t%s\t", depth, s_score, s_nodes, s_time );
247
248         /* Add PV */
249         buflen = strlen(buf);
250
251         strncpy( buf + buflen, pv, sizeof(buf) - buflen );
252
253         buf[ sizeof(buf) - 3 ] = '\0';
254
255         strcat( buf + buflen, "\r\n" );
256
257         /* Update memo */
258         InsertIntoMemo( hMemo, buf );
259     }
260 }
261
262 LRESULT CALLBACK EngineOutputProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
263 {
264     static SnapData sd;
265
266     switch (message) {
267     case WM_INITDIALOG:
268         if( engineOutputDialog == NULL ) {
269             engineOutputDialog = hDlg;
270
271             RestoreWindowPlacement( hDlg, &wpEngineOutput ); /* Restore window placement */
272
273             ResizeWindowControls( hDlg, windowMode );
274         }
275
276         return FALSE;
277
278     case WM_COMMAND:
279         switch (LOWORD(wParam)) {
280         case IDOK:
281           EndDialog(hDlg, TRUE);
282           return TRUE;
283
284         case IDCANCEL:
285           EndDialog(hDlg, FALSE);
286           return TRUE;
287
288         default:
289           break;
290         }
291
292         break;
293
294     case WM_GETMINMAXINFO:
295         {
296             MINMAXINFO * mmi = (MINMAXINFO *) lParam;
297
298             mmi->ptMinTrackSize.x = 100;
299             mmi->ptMinTrackSize.y = 160;
300         }
301         break;
302
303     case WM_CLOSE:
304         EngineOutputPopDown();
305         break;
306
307     case WM_SIZE:
308         ResizeWindowControls( hDlg, windowMode );
309         break;
310
311     case WM_ENTERSIZEMOVE:
312         return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
313
314     case WM_SIZING:
315         return OnSizing( &sd, hDlg, wParam, lParam );
316
317     case WM_MOVING:
318         return OnMoving( &sd, hDlg, wParam, lParam );
319
320     case WM_EXITSIZEMOVE:
321         return OnExitSizeMove( &sd, hDlg, wParam, lParam );
322     }
323
324     return FALSE;
325 }
326
327 VOID EngineOutputPopUp()
328 {
329   FARPROC lpProc;
330
331   CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_CHECKED);
332
333   if( engineOutputDialog ) {
334     SendMessage( engineOutputDialog, WM_INITDIALOG, 0, 0 );
335
336     if( ! engineOutputDialogUp ) {
337         ShowWindow(engineOutputDialog, SW_SHOW);
338     }
339   }
340   else {
341     lpProc = MakeProcInstance( (FARPROC) EngineOutputProc, hInst );
342
343     /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
344     CreateDialog( hInst, MAKEINTRESOURCE(DLG_EngineOutput), hwndMain, (DLGPROC)lpProc );
345
346     FreeProcInstance(lpProc);
347   }
348
349   engineOutputDialogUp = TRUE;
350 }
351
352 VOID EngineOutputPopDown()
353 {
354   CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_UNCHECKED);
355
356   if( engineOutputDialog ) {
357       ShowWindow(engineOutputDialog, SW_HIDE);
358   }
359
360   engineOutputDialogUp = FALSE;
361 }
362
363 BOOL EngineOutputIsUp()
364 {
365     return engineOutputDialogUp;
366 }
367
368 VOID EngineOutputUpdate( int which, int depth, unsigned long nodes, int score, int time, char * pv )
369 {
370     HWND hLabel;
371     HWND hLabelNPS;
372     HWND hMemo;
373     char * name;
374
375     if( which < 0 || which > 1 || depth < 0 || time < 0 || pv == 0 || *pv == '\0' ) {
376         return;
377     }
378
379     if( engineOutputDialog == NULL ) {
380         return;
381     }
382
383     VerifyDisplayMode();
384
385     /* Get target control */
386     if( which == 0 ) {
387         hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel1 );
388         hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine1_NPS );
389         hMemo  = GetDlgItem( engineOutputDialog, IDC_EngineMemo1 );
390         name = first.tidy;
391     }
392     else {
393         hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel2 );
394         hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine2_NPS );
395         hMemo  = GetDlgItem( engineOutputDialog, IDC_EngineMemo2 );
396         name = second.tidy;
397     }
398
399     /* Clear memo if needed */
400     if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
401         SendMessage( hMemo, WM_SETTEXT, 0, (LPARAM) "" );
402     }
403
404     /* Update */
405     lastDepth[which] = depth;
406
407     if( pv[0] == ' ' ) {
408         if( strncmp( pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
409             pv = "";
410         }
411     }
412
413     UpdateControls( hLabel, hLabelNPS, hMemo, name, depth, nodes, score, time, pv );
414 }