4 * Author: Alessandro Scotti
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.
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.
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 * ------------------------------------------------------------------------
24 #include <windows.h> /* required for all Windows applications */
39 VOID EngineOutputPopUp();
40 VOID EngineOutputPopDown();
41 BOOL EngineOutputIsUp();
43 /* Imports from backend.c */
44 char * SavePart(char *str);
46 /* Imports from winboard.c */
47 extern HWND engineOutputDialog;
48 extern BOOLEAN engineOutputDialogUp;
50 extern HINSTANCE hInst;
53 extern WindowPlacement wpEngineOutput;
55 /* Module variables */
58 #define LABEL_V_DISTANCE 1 /* Distance between label and memo */
59 #define SPLITTER_SIZE 4 /* Distance between first memo and second label */
61 static int windowMode = 1;
63 static int lastDepth[2] = { -1, -1 };
65 static VOID SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
67 HWND hControl = GetDlgItem( hDlg, id );
69 SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
72 static VOID HideControl( HWND hDlg, int id )
74 /* TODO: we should also hide/disable it!!! what about tab stops?!?! */
75 SetControlPos( hDlg, id, 20000, 20000, 100, 100 );
78 static int GetControlWidth( HWND hDlg, int id )
82 GetWindowRect( GetDlgItem( hDlg, IDC_EngineLabel1 ), &rc );
84 return rc.right - rc.left;
87 static VOID ResizeWindowControls( HWND hDlg, int mode )
96 /* Initialize variables */
97 GetWindowRect( GetDlgItem( hDlg, IDC_EngineLabel1 ), &rc );
99 labelHeight = rc.bottom - rc.top;
101 GetClientRect( hDlg, &rc );
103 clientWidth = rc.right - rc.left;
104 clientHeight = rc.bottom - rc.top;
106 maxControlWidth = clientWidth - 2*H_MARGIN;
108 npsWidth = GetControlWidth( hDlg, IDC_Engine1_NPS );
110 /* Resize controls */
113 int memo_y = V_MARGIN + labelHeight + LABEL_V_DISTANCE;
114 int memo_h = clientHeight - memo_y - V_MARGIN;
116 SetControlPos( hDlg, IDC_EngineLabel1, H_MARGIN, V_MARGIN, maxControlWidth / 2, labelHeight );
117 SetControlPos( hDlg, IDC_EngineMemo1, H_MARGIN, memo_y, maxControlWidth, memo_h );
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?!?! */
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;
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 );
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 );
142 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
143 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
146 static VOID SetDisplayMode( int mode )
148 if( windowMode != mode ) {
151 ResizeWindowControls( engineOutputDialog, mode );
155 static VOID VerifyDisplayMode()
159 /* Get proper mode for current game */
163 case MachinePlaysWhite:
164 case MachinePlaysBlack:
167 case TwoMachinesPlay:
175 SetDisplayMode( mode );
178 static VOID InsertIntoMemo( HWND hMemo, char * text )
180 SendMessage( hMemo, EM_SETSEL, 0, 0 );
182 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
185 #define MAX_NAME_LENGTH 32
187 static VOID UpdateControls( HWND hLabel, HWND hLabelNPS, HWND hMemo, char * name, int depth, unsigned long nodes, int score, int time, char * pv )
189 char s_label[MAX_NAME_LENGTH + 64];
192 if( name == 0 || *name == '\0' ) {
196 strncpy( s_label, name, MAX_NAME_LENGTH );
197 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
199 SetWindowText( hLabel, s_label );
203 if( time > 0 && nodes > 0 ) {
204 unsigned long nps_100 = nodes / time;
206 if( nps_100 < 100000 ) {
207 sprintf( s_label, "NPS: %lu", nps_100 * 100 );
210 sprintf( s_label, "NPS: %.1fk", nps_100 / 10.0 );
214 SetWindowText( hLabelNPS, s_label );
217 if( pv != 0 && *pv != '\0' ) {
223 int time_secs = time / 100;
224 int time_cent = time % 100;
227 if( nodes < 1000000 ) {
228 sprintf( s_nodes, "%lu", nodes );
231 sprintf( s_nodes, "%.1fM", nodes / 1000000.0 );
236 sprintf( s_score, "+%.2f", score / 100.0 );
239 sprintf( s_score, "%.2f", score / 100.0 );
243 sprintf( s_time, "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
245 /* Put all together... */
246 sprintf( buf, "%3d\t%s\t%s\t%s\t", depth, s_score, s_nodes, s_time );
249 buflen = strlen(buf);
251 strncpy( buf + buflen, pv, sizeof(buf) - buflen );
253 buf[ sizeof(buf) - 3 ] = '\0';
255 strcat( buf + buflen, "\r\n" );
258 InsertIntoMemo( hMemo, buf );
262 LRESULT CALLBACK EngineOutputProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
268 if( engineOutputDialog == NULL ) {
269 engineOutputDialog = hDlg;
271 RestoreWindowPlacement( hDlg, &wpEngineOutput ); /* Restore window placement */
273 ResizeWindowControls( hDlg, windowMode );
279 switch (LOWORD(wParam)) {
281 EndDialog(hDlg, TRUE);
285 EndDialog(hDlg, FALSE);
294 case WM_GETMINMAXINFO:
296 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
298 mmi->ptMinTrackSize.x = 100;
299 mmi->ptMinTrackSize.y = 160;
304 EngineOutputPopDown();
308 ResizeWindowControls( hDlg, windowMode );
311 case WM_ENTERSIZEMOVE:
312 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
315 return OnSizing( &sd, hDlg, wParam, lParam );
318 return OnMoving( &sd, hDlg, wParam, lParam );
320 case WM_EXITSIZEMOVE:
321 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
327 VOID EngineOutputPopUp()
331 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_CHECKED);
333 if( engineOutputDialog ) {
334 SendMessage( engineOutputDialog, WM_INITDIALOG, 0, 0 );
336 if( ! engineOutputDialogUp ) {
337 ShowWindow(engineOutputDialog, SW_SHOW);
341 lpProc = MakeProcInstance( (FARPROC) EngineOutputProc, hInst );
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 );
346 FreeProcInstance(lpProc);
349 engineOutputDialogUp = TRUE;
352 VOID EngineOutputPopDown()
354 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_UNCHECKED);
356 if( engineOutputDialog ) {
357 ShowWindow(engineOutputDialog, SW_HIDE);
360 engineOutputDialogUp = FALSE;
363 BOOL EngineOutputIsUp()
365 return engineOutputDialogUp;
368 VOID EngineOutputUpdate( int which, int depth, unsigned long nodes, int score, int time, char * pv )
375 if( which < 0 || which > 1 || depth < 0 || time < 0 || pv == 0 || *pv == '\0' ) {
379 if( engineOutputDialog == NULL ) {
385 /* Get target control */
387 hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel1 );
388 hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine1_NPS );
389 hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo1 );
393 hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel2 );
394 hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine2_NPS );
395 hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo2 );
399 /* Clear memo if needed */
400 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
401 SendMessage( hMemo, WM_SETTEXT, 0, (LPARAM) "" );
405 lastDepth[which] = depth;
408 if( strncmp( pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
413 UpdateControls( hLabel, hLabelNPS, hMemo, name, depth, nodes, score, time, pv );