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
153 itoa( value, buf+1, 10 );
\r
156 itoa( value, buf, 10 );
\r
159 cbBuf = strlen( buf );
\r
161 GetTextExtentPoint32( hdcPB, buf, cbBuf, &stSize );
\r
163 TextOut( hdcPB, MarginX - stSize.cx - 2, y - stSize.cy / 2, buf, cbBuf );
\r
167 static VOID DrawAxis()
\r
169 int cy = nHeightPB / 2;
\r
171 SelectObject( hdcPB, GetStockObject(NULL_BRUSH) );
\r
173 SetBkMode( hdcPB, TRANSPARENT );
\r
175 DrawAxisSegmentHoriz( +5, TRUE );
\r
176 DrawAxisSegmentHoriz( +3, FALSE );
\r
177 DrawAxisSegmentHoriz( +1, FALSE );
\r
178 DrawAxisSegmentHoriz( 0, TRUE );
\r
179 DrawAxisSegmentHoriz( -1, FALSE );
\r
180 DrawAxisSegmentHoriz( -3, FALSE );
\r
181 DrawAxisSegmentHoriz( -5, TRUE );
\r
183 SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
\r
185 DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy );
\r
186 DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH );
\r
189 static VOID DrawHistogram( int x, int y, int width, int value, int side )
\r
193 if( value > -25 && value < +25 ) return;
\r
196 rc.right = rc.left + width + 1;
\r
199 rc.top = GetValueY( value );
\r
204 rc.bottom = GetValueY( value ) + 1;
\r
208 if( width == MIN_HIST_WIDTH ) {
\r
210 FillRect( hdcPB, &rc, hbrHist[side] );
\r
213 SelectObject( hdcPB, hbrHist[side] );
\r
214 Rectangle( hdcPB, rc.left, rc.top, rc.right, rc.bottom );
\r
218 static VOID DrawSeparator( int index, int x )
\r
221 if( index == currCurrent ) {
\r
222 HPEN hp = SelectObject( hdcPB, hpenBlueDotted );
\r
223 DrawLineEx( x, MarginH, x, nHeightPB - MarginH );
\r
224 SelectObject( hdcPB, hp );
\r
226 else if( (index % 20) == 0 ) {
\r
227 HPEN hp = SelectObject( hdcPB, hpenDotted );
\r
228 DrawLineEx( x, MarginH, x, nHeightPB - MarginH );
\r
229 SelectObject( hdcPB, hp );
\r
234 /* Actually draw histogram as a diagram, cause there's too much data */
\r
235 static VOID DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
\r
240 /* Rescale the graph every few moves (as opposed to every move) */
\r
241 hist_count -= hist_count % 8;
\r
245 step = (double) paint_width / (hist_count + 1);
\r
247 for( i=0; i<2; i++ ) {
\r
248 int index = currFirst;
\r
249 int side = (currCurrent + i + 1) & 1; /* Draw current side last */
\r
250 double x = MarginX + MarginW;
\r
252 if( (index & 1) != side ) {
\r
257 SelectObject( hdcPB, hpenBold[side] );
\r
259 MoveToEx( hdcPB, (int) x, cy, NULL );
\r
263 while( index < currLast ) {
\r
266 DrawSeparator( index, (int) x );
\r
268 /* Extend line up to current point */
\r
269 if( currPvInfo[index].depth > 0 ) {
\r
270 LineTo( hdcPB, (int) x, GetValueY( GetPvScore(index) ) );
\r
278 static VOID DrawHistogramFull( int cy, int hist_width, int hist_count )
\r
282 SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
\r
284 for( i=0; i<hist_count; i++ ) {
\r
285 int index = currFirst + i;
\r
286 int x = MarginX + MarginW + index * hist_width;
\r
288 /* Draw a separator every 10 moves */
\r
289 DrawSeparator( index, x );
\r
291 /* Draw histogram */
\r
292 if( currPvInfo[i].depth > 0 ) {
\r
293 DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
\r
303 } VisualizationData;
\r
305 static BOOL InitVisualization( VisualizationData * vd )
\r
307 BOOL result = FALSE;
\r
309 vd->cy = nHeightPB / 2;
\r
310 vd->hist_width = MIN_HIST_WIDTH;
\r
311 vd->hist_count = currLast - currFirst;
\r
312 vd->paint_width = nWidthPB - MarginX - 2*MarginW;
\r
314 if( vd->hist_count > 0 ) {
\r
317 /* Compute width */
\r
318 vd->hist_width = vd->paint_width / vd->hist_count;
\r
320 if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
\r
322 vd->hist_width -= vd->hist_width % 2;
\r
328 static VOID DrawHistograms()
\r
330 VisualizationData vd;
\r
332 if( InitVisualization( &vd ) ) {
\r
333 if( vd.hist_width < MIN_HIST_WIDTH ) {
\r
334 DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
\r
337 DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
\r
342 static int GetMoveIndexFromPoint( int x, int y )
\r
345 int start_x = MarginX + MarginW;
\r
346 VisualizationData vd;
\r
348 if( x >= start_x && InitVisualization( &vd ) ) {
\r
349 /* Almost an hack here... we duplicate some of the paint logic */
\r
350 if( vd.hist_width < MIN_HIST_WIDTH ) {
\r
353 vd.hist_count -= vd.hist_count % 8;
\r
354 vd.hist_count += 8;
\r
355 vd.hist_count /= 2;
\r
357 step = (double) vd.paint_width / (vd.hist_count + 1);
\r
360 result = (int) (0.5 + (double) (x - start_x) / step);
\r
363 result = (x - start_x) / vd.hist_width;
\r
367 if( result >= currLast ) {
\r
374 static VOID DrawBackground()
\r
379 hbr = CreateBrush( BS_SOLID, GetSysColor( COLOR_3DFACE ) );
\r
383 rc.right = nWidthPB;
\r
384 rc.bottom = nHeightPB;
\r
386 FillRect( hdcPB, &rc, hbr );
\r
388 DeleteObject( hbr );
\r
391 static VOID PaintEvalGraph( HWND hWnd, HDC hDC )
\r
397 /* Get client area */
\r
398 GetClientRect( hWnd, &rcClient );
\r
400 width = rcClient.right - rcClient.left;
\r
401 height = rcClient.bottom - rcClient.top;
\r
403 /* Create or recreate paint box if needed */
\r
404 if( hbmPB == NULL || width != nWidthPB || height != nHeightPB ) {
\r
405 if( hpenDotted == NULL ) {
\r
406 hpenDotted = CreatePen( PS_DOT, 0, RGB(0xA0,0xA0,0xA0) );
\r
407 hpenBlueDotted = CreatePen( PS_DOT, 0, RGB(0x00,0x00,0xFF) );
\r
408 hpenBold[0] = CreatePen( PS_SOLID, 2, crWhite );
\r
409 hpenBold[1] = CreatePen( PS_SOLID, 2, crBlack );
\r
410 hbrHist[0] = CreateBrush( BS_SOLID, crWhite );
\r
411 hbrHist[1] = CreateBrush( BS_SOLID, crBlack );
\r
414 if( hdcPB != NULL ) {
\r
419 if( hbmPB != NULL ) {
\r
420 DeleteObject( hbmPB );
\r
424 hdcPB = CreateCompatibleDC( hDC );
\r
427 nHeightPB = height;
\r
428 hbmPB = CreateCompatibleBitmap( hDC, nWidthPB, nHeightPB );
\r
430 SelectObject( hdcPB, hbmPB );
\r
438 /* Copy bitmap into destination DC */
\r
439 BitBlt( hDC, 0, 0, width, height, hdcPB, 0, 0, SRCCOPY );
\r
442 LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
444 static SnapData sd;
\r
450 case WM_INITDIALOG:
\r
451 if( evalGraphDialog == NULL ) {
\r
452 evalGraphDialog = hDlg;
\r
454 RestoreWindowPlacement( hDlg, &wpEvalGraph ); /* Restore window placement */
\r
460 switch (LOWORD(wParam)) {
\r
462 EndDialog(hDlg, TRUE);
\r
466 EndDialog(hDlg, FALSE);
\r
475 case WM_ERASEBKGND:
\r
479 hDC = BeginPaint( hDlg, &stPS );
\r
480 PaintEvalGraph( hDlg, hDC );
\r
481 EndPaint( hDlg, &stPS );
\r
484 case WM_REFRESH_GRAPH:
\r
485 hDC = GetDC( hDlg );
\r
486 PaintEvalGraph( hDlg, hDC );
\r
487 ReleaseDC( hDlg, hDC );
\r
490 case WM_LBUTTONDBLCLK:
\r
491 if( wParam == 0 || wParam == MK_LBUTTON ) {
\r
492 int index = GetMoveIndexFromPoint( LOWORD(lParam), HIWORD(lParam) );
\r
494 if( index >= 0 && index < currLast ) {
\r
495 ToNrEvent( index + 1 );
\r
501 InvalidateRect( hDlg, NULL, FALSE );
\r
504 case WM_GETMINMAXINFO:
\r
506 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
508 mmi->ptMinTrackSize.x = 100;
\r
509 mmi->ptMinTrackSize.y = 100;
\r
513 /* Support for captionless window */
\r
515 case WM_NCLBUTTONDBLCLK:
\r
516 if( wParam == HTCAPTION ) {
\r
519 POINTS pts = MAKEPOINTS(lParam);
\r
521 mouse_xy.x = pts.x;
\r
522 mouse_xy.y = pts.y;
\r
523 ScreenToClient( hDlg, &mouse_xy );
\r
525 index = GetMoveIndexFromPoint( mouse_xy.x, mouse_xy.y );
\r
527 if( index >= 0 && index < currLast ) {
\r
528 ToNrEvent( index + 1 );
\r
535 LRESULT res = DefWindowProc( hDlg, message, wParam, lParam );
\r
537 if( res == HTCLIENT ) res = HTCAPTION;
\r
539 SetWindowLong( hDlg, DWL_MSGRESULT, res );
\r
547 EvalGraphPopDown();
\r
550 case WM_ENTERSIZEMOVE:
\r
551 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
554 return OnSizing( &sd, hDlg, wParam, lParam );
\r
557 return OnMoving( &sd, hDlg, wParam, lParam );
\r
559 case WM_EXITSIZEMOVE:
\r
560 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
566 VOID EvalGraphPopUp()
\r
570 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEvalGraph, MF_CHECKED);
\r
572 if( evalGraphDialog ) {
\r
573 SendMessage( evalGraphDialog, WM_INITDIALOG, 0, 0 );
\r
575 if( ! evalGraphDialogUp ) {
\r
576 ShowWindow(evalGraphDialog, SW_SHOW);
\r
580 crWhite = appData.evalHistColorWhite;
\r
581 crBlack = appData.evalHistColorBlack;
\r
583 lpProc = MakeProcInstance( (FARPROC) EvalGraphProc, hInst );
\r
585 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
586 CreateDialog( hInst, MAKEINTRESOURCE(DLG_EvalGraph), hwndMain, (DLGPROC)lpProc );
\r
588 FreeProcInstance(lpProc);
\r
591 evalGraphDialogUp = TRUE;
\r
594 VOID EvalGraphPopDown()
\r
596 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEvalGraph, MF_UNCHECKED);
\r
598 if( evalGraphDialog ) {
\r
599 ShowWindow(evalGraphDialog, SW_HIDE);
\r
602 evalGraphDialogUp = FALSE;
\r
605 VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pvInfo )
\r
607 /* [AS] Danger! For now we rely on the pvInfo parameter being a static variable! */
\r
611 currCurrent = current;
\r
612 currPvInfo = pvInfo;
\r
614 if( evalGraphDialog ) {
\r
615 SendMessage( evalGraphDialog, WM_REFRESH_GRAPH, 0, 0 );
\r
619 BOOL EvalGraphIsUp()
\r
621 return evalGraphDialogUp;
\r