4 * Author: Alessandro Scotti (Dec 2005)
\r
6 * ------------------------------------------------------------------------
\r
8 * GNU XBoard is free software: you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation, either version 3 of the License, or (at
\r
11 * your option) any later version.
\r
13 * GNU XBoard is distributed in the hope that it will be useful, but
\r
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
16 * General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program. If not, see http://www.gnu.org/licenses/. *
\r
21 *------------------------------------------------------------------------
\r
22 ** See the file ChangeLog for a revision history. */
\r
26 #include <windows.h> /* required for all Windows applications */
\r
27 #include <richedit.h>
\r
31 #include <commdlg.h>
\r
35 #include "winboard.h"
\r
36 #include "frontend.h"
\r
37 #include "backend.h"
\r
41 VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pvInfo );
\r
42 VOID EvalGraphPopUp();
\r
43 VOID EvalGraphPopDown();
\r
44 BOOL EvalGraphIsUp();
\r
46 #define WM_REFRESH_GRAPH (WM_USER + 1)
\r
48 /* Imports from backend.c */
\r
49 char * SavePart(char *str);
\r
51 /* Imports from winboard.c */
\r
52 extern HWND evalGraphDialog;
\r
53 extern BOOLEAN evalGraphDialogUp;
\r
55 extern HINSTANCE hInst;
\r
56 extern HWND hwndMain;
\r
58 extern WindowPlacement wpEvalGraph;
\r
60 /* Module globals */
\r
61 static ChessProgramStats_Move * currPvInfo;
\r
62 static int currFirst = 0;
\r
63 static int currLast = 0;
\r
64 static int currCurrent = -1;
\r
66 static COLORREF crWhite = RGB( 0xFF, 0xFF, 0xB0 );
\r
67 static COLORREF crBlack = RGB( 0xAD, 0x5D, 0x3D );
\r
69 static HDC hdcPB = NULL;
\r
70 static HBITMAP hbmPB = NULL;
\r
71 static int nWidthPB = 0;
\r
72 static int nHeightPB = 0;
\r
73 static HPEN hpenDotted = NULL;
\r
74 static HPEN hpenBlueDotted = NULL;
\r
75 static HPEN hpenBold[2] = { NULL, NULL };
\r
76 static HBRUSH hbrHist[2] = { NULL, NULL };
\r
78 static int MarginX = 18;
\r
79 static int MarginW = 4;
\r
80 static int MarginH = 4;
\r
82 #define MIN_HIST_WIDTH 4
\r
83 #define MAX_HIST_WIDTH 10
\r
85 static int GetPvScore( int index )
\r
87 int score = currPvInfo[ index ].score;
\r
89 if( index & 1 ) score = -score; /* Flip score for black */
\r
94 static VOID DrawLine( int x1, int y1, int x2, int y2 )
\r
96 MoveToEx( hdcPB, x1, y1, NULL );
\r
98 LineTo( hdcPB, x2, y2 );
\r
101 static VOID DrawLineEx( int x1, int y1, int x2, int y2 )
\r
105 MoveToEx( hdcPB, x1, y1, &stPT );
\r
107 LineTo( hdcPB, x2, y2 );
\r
109 MoveToEx( hdcPB, stPT.x, stPT.y, NULL );
\r
112 static HBRUSH CreateBrush( UINT style, COLORREF color )
\r
116 stLB.lbStyle = style;
\r
117 stLB.lbColor = color;
\r
120 return CreateBrushIndirect( &stLB );
\r
124 For a centipawn value, this function returns the height of the corresponding
\r
125 histogram, centered on the reference axis.
\r
127 Note: height can be negative!
\r
129 static int GetValueY( int value )
\r
131 if( value < -700 ) value = -700;
\r
132 if( value > +700 ) value = +700;
\r
134 return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / 1400.0);
\r
137 static VOID DrawAxisSegmentHoriz( int value, BOOL drawValue )
\r
139 int y = GetValueY( value*100 );
\r
141 SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
\r
142 DrawLine( MarginX, y, MarginX + MarginW, y );
\r
143 SelectObject( hdcPB, hpenDotted );
\r
144 DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y );
\r
148 char buf[MSG_SIZ], *b = buf;
\r
151 if( value > 0 ) *b++ = '+';
\r
152 sprintf(b, "%d", value);
\r
154 cbBuf = strlen( buf );
\r
155 GetTextExtentPoint32( hdcPB, buf, cbBuf, &stSize );
\r
156 TextOut( hdcPB, MarginX - stSize.cx - 2, y - stSize.cy / 2, buf, cbBuf );
\r
160 static VOID DrawAxis()
\r
162 int cy = nHeightPB / 2;
\r
164 SelectObject( hdcPB, GetStockObject(NULL_BRUSH) );
\r
166 SetBkMode( hdcPB, TRANSPARENT );
\r
168 DrawAxisSegmentHoriz( +5, TRUE );
\r
169 DrawAxisSegmentHoriz( +3, FALSE );
\r
170 DrawAxisSegmentHoriz( +1, FALSE );
\r
171 DrawAxisSegmentHoriz( 0, TRUE );
\r
172 DrawAxisSegmentHoriz( -1, FALSE );
\r
173 DrawAxisSegmentHoriz( -3, FALSE );
\r
174 DrawAxisSegmentHoriz( -5, TRUE );
\r
176 SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
\r
178 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy );
\r
179 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH );
\r
182 static VOID DrawHistogram( int x, int y, int width, int value, int side )
\r
186 if( value > -25 && value < +25 ) return;
\r
189 rc.right = rc.left + width + 1;
\r
192 rc.top = GetValueY( value );
\r
197 rc.bottom = GetValueY( value ) + 1;
\r
201 if( width == MIN_HIST_WIDTH ) {
\r
203 FillRect( hdcPB, &rc, hbrHist[side] );
\r
206 SelectObject( hdcPB, hbrHist[side] );
\r
207 Rectangle( hdcPB, rc.left, rc.top, rc.right, rc.bottom );
\r
211 static VOID DrawSeparator( int index, int x )
\r
214 if( index == currCurrent ) {
\r
215 HPEN hp = SelectObject( hdcPB, hpenBlueDotted );
\r
216 DrawLineEx( x, MarginH, x, nHeightPB - MarginH );
\r
217 SelectObject( hdcPB, hp );
\r
219 else if( (index % 20) == 0 ) {
\r
220 HPEN hp = SelectObject( hdcPB, hpenDotted );
\r
221 DrawLineEx( x, MarginH, x, nHeightPB - MarginH );
\r
222 SelectObject( hdcPB, hp );
\r
227 /* Actually draw histogram as a diagram, cause there's too much data */
\r
228 static VOID DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
\r
233 /* Rescale the graph every few moves (as opposed to every move) */
\r
234 hist_count -= hist_count % 8;
\r
238 step = (double) paint_width / (hist_count + 1);
\r
240 for( i=0; i<2; i++ ) {
\r
241 int index = currFirst;
\r
242 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
\r
243 double x = MarginX + MarginW;
\r
245 if( (index & 1) != side ) {
\r
250 SelectObject( hdcPB, hpenBold[side] );
\r
252 MoveToEx( hdcPB, (int) x, cy, NULL );
\r
256 while( index < currLast ) {
\r
259 DrawSeparator( index, (int) x );
\r
261 /* Extend line up to current point */
\r
262 if( currPvInfo[index].depth > 0 ) {
\r
263 LineTo( hdcPB, (int) x, GetValueY( GetPvScore(index) ) );
\r
271 static VOID DrawHistogramFull( int cy, int hist_width, int hist_count )
\r
275 SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
\r
277 for( i=0; i<hist_count; i++ ) {
\r
278 int index = currFirst + i;
\r
279 int x = MarginX + MarginW + index * hist_width;
\r
281 /* Draw a separator every 10 moves */
\r
282 DrawSeparator( index, x );
\r
284 /* Draw histogram */
\r
285 if( currPvInfo[i].depth > 0 ) {
\r
286 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
\r
296 } VisualizationData;
\r
298 static BOOL InitVisualization( VisualizationData * vd )
\r
300 BOOL result = FALSE;
\r
302 vd->cy = nHeightPB / 2;
\r
303 vd->hist_width = MIN_HIST_WIDTH;
\r
304 vd->hist_count = currLast - currFirst;
\r
305 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
\r
307 if( vd->hist_count > 0 ) {
\r
310 /* Compute width */
\r
311 vd->hist_width = vd->paint_width / vd->hist_count;
\r
313 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
\r
315 vd->hist_width -= vd->hist_width % 2;
\r
321 static VOID DrawHistograms()
\r
323 VisualizationData vd;
\r
325 if( InitVisualization( &vd ) ) {
\r
326 if( vd.hist_width < MIN_HIST_WIDTH ) {
\r
327 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
\r
330 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
\r
335 static int GetMoveIndexFromPoint( int x, int y )
\r
338 int start_x = MarginX + MarginW;
\r
339 VisualizationData vd;
\r
341 if( x >= start_x && InitVisualization( &vd ) ) {
\r
342 /* Almost an hack here... we duplicate some of the paint logic */
\r
343 if( vd.hist_width < MIN_HIST_WIDTH ) {
\r
346 vd.hist_count -= vd.hist_count % 8;
\r
347 vd.hist_count += 8;
\r
348 vd.hist_count /= 2;
\r
350 step = (double) vd.paint_width / (vd.hist_count + 1);
\r
353 result = (int) (0.5 + (double) (x - start_x) / step);
\r
356 result = (x - start_x) / vd.hist_width;
\r
360 if( result >= currLast ) {
\r
367 static VOID DrawBackground()
\r
372 hbr = CreateBrush( BS_SOLID, GetSysColor( COLOR_3DFACE ) );
\r
376 rc.right = nWidthPB;
\r
377 rc.bottom = nHeightPB;
\r
379 FillRect( hdcPB, &rc, hbr );
\r
381 DeleteObject( hbr );
\r
384 static VOID PaintEvalGraph( HWND hWnd, HDC hDC )
\r
390 /* Get client area */
\r
391 GetClientRect( hWnd, &rcClient );
\r
393 width = rcClient.right - rcClient.left;
\r
394 height = rcClient.bottom - rcClient.top;
\r
396 /* Create or recreate paint box if needed */
\r
397 if( hbmPB == NULL || width != nWidthPB || height != nHeightPB ) {
\r
398 if( hpenDotted == NULL ) {
\r
399 hpenDotted = CreatePen( PS_DOT, 0, RGB(0xA0,0xA0,0xA0) );
\r
400 hpenBlueDotted = CreatePen( PS_DOT, 0, RGB(0x00,0x00,0xFF) );
\r
401 hpenBold[0] = CreatePen( PS_SOLID, 2, crWhite );
\r
402 hpenBold[1] = CreatePen( PS_SOLID, 2, crBlack );
\r
403 hbrHist[0] = CreateBrush( BS_SOLID, crWhite );
\r
404 hbrHist[1] = CreateBrush( BS_SOLID, crBlack );
\r
407 if( hdcPB != NULL ) {
\r
412 if( hbmPB != NULL ) {
\r
413 DeleteObject( hbmPB );
\r
417 hdcPB = CreateCompatibleDC( hDC );
\r
420 nHeightPB = height;
\r
421 hbmPB = CreateCompatibleBitmap( hDC, nWidthPB, nHeightPB );
\r
423 SelectObject( hdcPB, hbmPB );
\r
431 /* Copy bitmap into destination DC */
\r
432 BitBlt( hDC, 0, 0, width, height, hdcPB, 0, 0, SRCCOPY );
\r
435 LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
437 static SnapData sd;
\r
443 case WM_INITDIALOG:
\r
444 if( evalGraphDialog == NULL ) {
\r
445 evalGraphDialog = hDlg;
\r
447 RestoreWindowPlacement( hDlg, &wpEvalGraph ); /* Restore window placement */
\r
453 switch (LOWORD(wParam)) {
\r
455 EndDialog(hDlg, TRUE);
\r
459 EndDialog(hDlg, FALSE);
\r
468 case WM_ERASEBKGND:
\r
472 hDC = BeginPaint( hDlg, &stPS );
\r
473 PaintEvalGraph( hDlg, hDC );
\r
474 EndPaint( hDlg, &stPS );
\r
477 case WM_REFRESH_GRAPH:
\r
478 hDC = GetDC( hDlg );
\r
479 PaintEvalGraph( hDlg, hDC );
\r
480 ReleaseDC( hDlg, hDC );
\r
483 case WM_LBUTTONDBLCLK:
\r
484 if( wParam == 0 || wParam == MK_LBUTTON ) {
\r
485 int index = GetMoveIndexFromPoint( LOWORD(lParam), HIWORD(lParam) );
\r
487 if( index >= 0 && index < currLast ) {
\r
488 ToNrEvent( index + 1 );
\r
494 InvalidateRect( hDlg, NULL, FALSE );
\r
497 case WM_GETMINMAXINFO:
\r
499 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
501 mmi->ptMinTrackSize.x = 100;
\r
502 mmi->ptMinTrackSize.y = 100;
\r
506 /* Support for captionless window */
\r
508 case WM_NCLBUTTONDBLCLK:
\r
509 if( wParam == HTCAPTION ) {
\r
512 POINTS pts = MAKEPOINTS(lParam);
\r
514 mouse_xy.x = pts.x;
\r
515 mouse_xy.y = pts.y;
\r
516 ScreenToClient( hDlg, &mouse_xy );
\r
518 index = GetMoveIndexFromPoint( mouse_xy.x, mouse_xy.y );
\r
520 if( index >= 0 && index < currLast ) {
\r
521 ToNrEvent( index + 1 );
\r
528 LRESULT res = DefWindowProc( hDlg, message, wParam, lParam );
\r
530 if( res == HTCLIENT ) res = HTCAPTION;
\r
532 SetWindowLong( hDlg, DWL_MSGRESULT, res );
\r
540 EvalGraphPopDown();
\r
543 case WM_ENTERSIZEMOVE:
\r
544 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
547 return OnSizing( &sd, hDlg, wParam, lParam );
\r
550 return OnMoving( &sd, hDlg, wParam, lParam );
\r
552 case WM_EXITSIZEMOVE:
\r
553 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
559 VOID EvalGraphPopUp()
\r
563 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEvalGraph, MF_CHECKED);
\r
565 if( evalGraphDialog ) {
\r
566 SendMessage( evalGraphDialog, WM_INITDIALOG, 0, 0 );
\r
568 if( ! evalGraphDialogUp ) {
\r
569 ShowWindow(evalGraphDialog, SW_SHOW);
\r
573 crWhite = appData.evalHistColorWhite;
\r
574 crBlack = appData.evalHistColorBlack;
\r
576 lpProc = MakeProcInstance( (FARPROC) EvalGraphProc, hInst );
\r
578 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
579 CreateDialog( hInst, MAKEINTRESOURCE(DLG_EvalGraph), hwndMain, (DLGPROC)lpProc );
\r
581 FreeProcInstance(lpProc);
\r
584 evalGraphDialogUp = TRUE;
\r
587 VOID EvalGraphPopDown()
\r
589 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEvalGraph, MF_UNCHECKED);
\r
591 if( evalGraphDialog ) {
\r
592 ShowWindow(evalGraphDialog, SW_HIDE);
\r
595 evalGraphDialogUp = FALSE;
\r
598 VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pvInfo )
\r
600 /* [AS] Danger! For now we rely on the pvInfo parameter being a static variable! */
\r
604 currCurrent = current;
\r
605 currPvInfo = pvInfo;
\r
607 if( evalGraphDialog ) {
\r
608 SendMessage( evalGraphDialog, WM_REFRESH_GRAPH, 0, 0 );
\r
612 BOOL EvalGraphIsUp()
\r
614 return evalGraphDialogUp;
\r