X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=winboard%2Fwevalgraph.c;h=3f1f07644849c018370fed2316b85a2567b857ef;hb=HEAD;hp=fca6a679654cadbddec863320f164193e0f50abd;hpb=91d8e5853ca580769cc130aa6ea004869118d171;p=xboard.git diff --git a/winboard/wevalgraph.c b/winboard/wevalgraph.c index fca6a67..3f1f076 100644 --- a/winboard/wevalgraph.c +++ b/winboard/wevalgraph.c @@ -1,8 +1,13 @@ /* - * Evaluation graph + * wevalgraph.c - Evaluation graph front-end part * * Author: Alessandro Scotti (Dec 2005) * + * Copyright 2005 Alessandro Scotti + * + * Enhancements Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, + * 2016 Free Software Foundation, Inc. + * * ------------------------------------------------------------------------ * * GNU XBoard is free software: you can redistribute it and/or modify @@ -21,374 +26,92 @@ *------------------------------------------------------------------------ ** See the file ChangeLog for a revision history. */ +// code refactored by HGM to obtain front-end / back-end separation + #include "config.h" -#include /* required for all Windows applications */ -#include -#include -#include -#include +#include #include #include +#include #include "common.h" -#include "winboard.h" #include "frontend.h" #include "backend.h" - +#include "winboard.h" +#include "evalgraph.h" #include "wsnap.h" -VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pvInfo ); -VOID EvalGraphPopUp(); -VOID EvalGraphPopDown(); -BOOL EvalGraphIsUp(); - #define WM_REFRESH_GRAPH (WM_USER + 1) -/* Imports from backend.c */ -char * SavePart(char *str); - -/* Imports from winboard.c */ -extern HWND evalGraphDialog; -extern BOOLEAN evalGraphDialogUp; - -extern HINSTANCE hInst; -extern HWND hwndMain; - -extern WindowPlacement wpEvalGraph; - /* Module globals */ -static ChessProgramStats_Move * currPvInfo; -static int currFirst = 0; -static int currLast = 0; -static int currCurrent = -1; +static BOOLEAN evalGraphDialogUp; static COLORREF crWhite = RGB( 0xFF, 0xFF, 0xB0 ); static COLORREF crBlack = RGB( 0xAD, 0x5D, 0x3D ); static HDC hdcPB = NULL; static HBITMAP hbmPB = NULL; -static int nWidthPB = 0; -static int nHeightPB = 0; -static HPEN hpenDotted = NULL; -static HPEN hpenBlueDotted = NULL; -static HPEN hpenBold[2] = { NULL, NULL }; -static HBRUSH hbrHist[2] = { NULL, NULL }; - -static int MarginX = 18; -static int MarginW = 4; -static int MarginH = 4; - -#define MIN_HIST_WIDTH 4 -#define MAX_HIST_WIDTH 10 - -static int GetPvScore( int index ) -{ - int score = currPvInfo[ index ].score; - - if( index & 1 ) score = -score; /* Flip score for black */ - - return score; -} - -static VOID DrawLine( int x1, int y1, int x2, int y2 ) -{ - MoveToEx( hdcPB, x1, y1, NULL ); - - LineTo( hdcPB, x2, y2 ); -} - -static VOID DrawLineEx( int x1, int y1, int x2, int y2 ) -{ - POINT stPT; - - MoveToEx( hdcPB, x1, y1, &stPT ); - - LineTo( hdcPB, x2, y2 ); - - MoveToEx( hdcPB, stPT.x, stPT.y, NULL ); -} - -static HBRUSH CreateBrush( UINT style, COLORREF color ) -{ - LOGBRUSH stLB; - - stLB.lbStyle = style; - stLB.lbColor = color; - stLB.lbHatch = 0; - - return CreateBrushIndirect( &stLB ); -} +static HPEN pens[PEN_ANY+1]; // [HGM] put all pens in one array +static HBRUSH hbrHist[3] = { NULL, NULL, NULL }; -/* - For a centipawn value, this function returns the height of the corresponding - histogram, centered on the reference axis. - - Note: height can be negative! -*/ -static int GetValueY( int value ) +Boolean EvalGraphIsUp() { - if( value < -700 ) value = -700; - if( value > +700 ) value = +700; - - return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / 1400.0); + return evalGraphDialogUp; } -static VOID DrawAxisSegmentHoriz( int value, BOOL drawValue ) +// [HGM] front-end, added as wrapper to avoid use of LineTo and MoveToEx in other routines (so they can be back-end) +void DrawSegment( int x, int y, int *lastX, int *lastY, int penType ) { - int y = GetValueY( value*100 ); - - SelectObject( hdcPB, GetStockObject(BLACK_PEN) ); - DrawLine( MarginX, y, MarginX + MarginW, y ); - SelectObject( hdcPB, hpenDotted ); - DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y ); - - if( drawValue ) { - SIZE stSize; - char buf[8]; - int cbBuf; - - if( value > 0 ) { - buf[0] = '+'; - itoa( value, buf+1, 10 ); - } - else { - itoa( value, buf, 10 ); - } - - cbBuf = strlen( buf ); - - GetTextExtentPoint32( hdcPB, buf, cbBuf, &stSize ); - - TextOut( hdcPB, MarginX - stSize.cx - 2, y - stSize.cy / 2, buf, cbBuf ); + POINT stPt; + if(penType == PEN_NONE) MoveToEx( hdcPB, x, y, &stPt ); else { + HPEN hp = SelectObject( hdcPB, pens[penType] ); + LineTo( hdcPB, x, y ); + SelectObject( hdcPB, hp ); } + if(lastX != NULL) { *lastX = stPt.x; *lastY = stPt.y; } } -static VOID DrawAxis() -{ - int cy = nHeightPB / 2; - - SelectObject( hdcPB, GetStockObject(NULL_BRUSH) ); - - SetBkMode( hdcPB, TRANSPARENT ); - - DrawAxisSegmentHoriz( +5, TRUE ); - DrawAxisSegmentHoriz( +3, FALSE ); - DrawAxisSegmentHoriz( +1, FALSE ); - DrawAxisSegmentHoriz( 0, TRUE ); - DrawAxisSegmentHoriz( -1, FALSE ); - DrawAxisSegmentHoriz( -3, FALSE ); - DrawAxisSegmentHoriz( -5, TRUE ); - - SelectObject( hdcPB, GetStockObject(BLACK_PEN) ); - - DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy ); - DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH ); -} - -static VOID DrawHistogram( int x, int y, int width, int value, int side ) +// front-end wrapper for drawing functions to do rectangles +void DrawRectangle( int left, int top, int right, int bottom, int side, int style ) { + HPEN hp = SelectObject( hdcPB, pens[PEN_BLACK] ); RECT rc; - if( value > -25 && value < +25 ) return; - - rc.left = x; - rc.right = rc.left + width + 1; - - if( value > 0 ) { - rc.top = GetValueY( value ); - rc.bottom = y+1; - } - else { - rc.top = y; - rc.bottom = GetValueY( value ) + 1; - } - - - if( width == MIN_HIST_WIDTH ) { - rc.right--; + rc.top = top; rc.left = left; rc.bottom = bottom; rc.right = right; + if(style == FILLED) FillRect( hdcPB, &rc, hbrHist[side] ); - } else { SelectObject( hdcPB, hbrHist[side] ); - Rectangle( hdcPB, rc.left, rc.top, rc.right, rc.bottom ); - } -} - -static VOID DrawSeparator( int index, int x ) -{ - if( index > 0 ) { - if( index == currCurrent ) { - HPEN hp = SelectObject( hdcPB, hpenBlueDotted ); - DrawLineEx( x, MarginH, x, nHeightPB - MarginH ); - SelectObject( hdcPB, hp ); - } - else if( (index % 20) == 0 ) { - HPEN hp = SelectObject( hdcPB, hpenDotted ); - DrawLineEx( x, MarginH, x, nHeightPB - MarginH ); - SelectObject( hdcPB, hp ); - } - } -} - -/* Actually draw histogram as a diagram, cause there's too much data */ -static VOID DrawHistogramAsDiagram( int cy, int paint_width, int hist_count ) -{ - double step; - int i; - - /* Rescale the graph every few moves (as opposed to every move) */ - hist_count -= hist_count % 8; - hist_count += 8; - hist_count /= 2; - - step = (double) paint_width / (hist_count + 1); - - for( i=0; i<2; i++ ) { - int index = currFirst; - int side = (currCurrent + i + 1) & 1; /* Draw current side last */ - double x = MarginX + MarginW; - - if( (index & 1) != side ) { - x += step / 2; - index++; - } - - SelectObject( hdcPB, hpenBold[side] ); - - MoveToEx( hdcPB, (int) x, cy, NULL ); - - index += 2; - - while( index < currLast ) { - x += step; - - DrawSeparator( index, (int) x ); - - /* Extend line up to current point */ - if( currPvInfo[index].depth > 0 ) { - LineTo( hdcPB, (int) x, GetValueY( GetPvScore(index) ) ); - } - - index += 2; - } + Rectangle( hdcPB, left, top, right, bottom ); } + SelectObject( hdcPB, hp ); } -static VOID DrawHistogramFull( int cy, int hist_width, int hist_count ) +// front-end wrapper for putting text in graph +void DrawEvalText(char *buf, int cbBuf, int y) { - int i; - - SelectObject( hdcPB, GetStockObject(BLACK_PEN) ); - - for( i=0; i 0 ) { - DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 ); - } - } -} - -typedef struct { - int cy; - int hist_width; - int hist_count; - int paint_width; -} VisualizationData; - -static BOOL InitVisualization( VisualizationData * vd ) -{ - BOOL result = FALSE; - - vd->cy = nHeightPB / 2; - vd->hist_width = MIN_HIST_WIDTH; - vd->hist_count = currLast - currFirst; - vd->paint_width = nWidthPB - MarginX - 2*MarginW; - - if( vd->hist_count > 0 ) { - result = TRUE; - - /* Compute width */ - vd->hist_width = vd->paint_width / vd->hist_count; - - if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH; - - vd->hist_width -= vd->hist_width % 2; - } - - return result; -} - -static VOID DrawHistograms() -{ - VisualizationData vd; - - if( InitVisualization( &vd ) ) { - if( vd.hist_width < MIN_HIST_WIDTH ) { - DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count ); - } - else { - DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count ); - } - } -} - -static int GetMoveIndexFromPoint( int x, int y ) -{ - int result = -1; - int start_x = MarginX + MarginW; - VisualizationData vd; - - if( x >= start_x && InitVisualization( &vd ) ) { - /* Almost an hack here... we duplicate some of the paint logic */ - if( vd.hist_width < MIN_HIST_WIDTH ) { - double step; - - vd.hist_count -= vd.hist_count % 8; - vd.hist_count += 8; - vd.hist_count /= 2; - - step = (double) vd.paint_width / (vd.hist_count + 1); - step /= 2; - - result = (int) (0.5 + (double) (x - start_x) / step); - } - else { - result = (x - start_x) / vd.hist_width; - } - } - - if( result >= currLast ) { - result = -1; - } - - return result; + SIZE stSize; + SetBkMode( hdcPB, TRANSPARENT ); + GetTextExtentPoint32( hdcPB, buf, cbBuf, &stSize ); + TextOut( hdcPB, MarginX - stSize.cx - 2, y - stSize.cy / 2, buf, cbBuf ); } -static VOID DrawBackground() +// front-end +static HBRUSH CreateBrush( UINT style, COLORREF color ) { - HBRUSH hbr; - RECT rc; - - hbr = CreateBrush( BS_SOLID, GetSysColor( COLOR_3DFACE ) ); - - rc.left = 0; - rc.top = 0; - rc.right = nWidthPB; - rc.bottom = nHeightPB; + LOGBRUSH stLB; - FillRect( hdcPB, &rc, hbr ); + stLB.lbStyle = style; + stLB.lbColor = color; + stLB.lbHatch = 0; - DeleteObject( hbr ); + return CreateBrushIndirect( &stLB ); } -static VOID PaintEvalGraph( HWND hWnd, HDC hDC ) +// front-end. Create pens, device context and buffer bitmap for global use, copy result to display +// The back-end part n the middle has been taken out and moed to PainEvalGraph() +static VOID DisplayEvalGraph( HWND hWnd, HDC hDC ) { RECT rcClient; int width; @@ -402,13 +125,15 @@ static VOID PaintEvalGraph( HWND hWnd, HDC hDC ) /* Create or recreate paint box if needed */ if( hbmPB == NULL || width != nWidthPB || height != nHeightPB ) { - if( hpenDotted == NULL ) { - hpenDotted = CreatePen( PS_DOT, 0, RGB(0xA0,0xA0,0xA0) ); - hpenBlueDotted = CreatePen( PS_DOT, 0, RGB(0x00,0x00,0xFF) ); - hpenBold[0] = CreatePen( PS_SOLID, 2, crWhite ); - hpenBold[1] = CreatePen( PS_SOLID, 2, crBlack ); + if( pens[PEN_DOTTED] == NULL ) { + pens[PEN_BLACK] = GetStockObject(BLACK_PEN); + pens[PEN_DOTTED] = CreatePen( PS_DOT, 0, RGB(0xA0,0xA0,0xA0) ); + pens[PEN_BLUEDOTTED] = CreatePen( PS_DOT, 0, RGB(0x00,0x00,0xFF) ); + pens[PEN_BOLDWHITE] = CreatePen( PS_SOLID, 2, crWhite ); + pens[PEN_BOLDBLACK] = CreatePen( PS_SOLID, 2, crBlack ); hbrHist[0] = CreateBrush( BS_SOLID, crWhite ); hbrHist[1] = CreateBrush( BS_SOLID, crBlack ); + hbrHist[2] = CreateBrush( BS_SOLID, GetSysColor( COLOR_3DFACE ) ); // background } if( hdcPB != NULL ) { @@ -430,15 +155,17 @@ static VOID PaintEvalGraph( HWND hWnd, HDC hDC ) SelectObject( hdcPB, hbmPB ); } - /* Draw */ - DrawBackground(); - DrawAxis(); - DrawHistograms(); + // back-end painting; calls back front-end primitives for lines, rectangles and text + PaintEvalGraph(); + SetWindowText(hWnd, MakeEvalTitle(differentialView ? T_("Blunder Graph") : T_("Evaluation Graph"))); /* Copy bitmap into destination DC */ - BitBlt( hDC, 0, 0, width, height, hdcPB, 0, 0, SRCCOPY ); + BitBlt( hDC, 0, 0, nWidthPB, nHeightPB, hdcPB, 0, 0, SRCCOPY ); } +// Note: Once the eval graph is opened, this window-proc lives forever; een closing the +// eval-graph window merely hides it. On opening we re-initialize it, though, so it could +// as well hae been destroyed. While it is open it processes the REFRESH_GRAPH commands. LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam ) { static SnapData sd; @@ -448,6 +175,7 @@ LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM l switch (message) { case WM_INITDIALOG: + Translate(hDlg, DLG_EvalGraph); if( evalGraphDialog == NULL ) { evalGraphDialog = hDlg; @@ -477,17 +205,24 @@ LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM l case WM_PAINT: hDC = BeginPaint( hDlg, &stPS ); - PaintEvalGraph( hDlg, hDC ); + DisplayEvalGraph( hDlg, hDC ); EndPaint( hDlg, &stPS ); break; + case WM_MOUSEWHEEL: + if((short)HIWORD(wParam) < 0) appData.zoom++; + if((short)HIWORD(wParam) > 0 && appData.zoom > 1) appData.zoom--; + goto paint; + case WM_RBUTTONDOWN: + differentialView = !differentialView; case WM_REFRESH_GRAPH: + paint: hDC = GetDC( hDlg ); - PaintEvalGraph( hDlg, hDC ); + DisplayEvalGraph( hDlg, hDC ); ReleaseDC( hDlg, hDC ); break; - case WM_LBUTTONDBLCLK: + case WM_LBUTTONDOWN: if( wParam == 0 || wParam == MK_LBUTTON ) { int index = GetMoveIndexFromPoint( LOWORD(lParam), HIWORD(lParam) ); @@ -511,38 +246,6 @@ LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM l break; /* Support for captionless window */ -#if 0 - case WM_NCLBUTTONDBLCLK: - if( wParam == HTCAPTION ) { - int index; - POINT mouse_xy; - POINTS pts = MAKEPOINTS(lParam); - - mouse_xy.x = pts.x; - mouse_xy.y = pts.y; - ScreenToClient( hDlg, &mouse_xy ); - - index = GetMoveIndexFromPoint( mouse_xy.x, mouse_xy.y ); - - if( index >= 0 && index < currLast ) { - ToNrEvent( index + 1 ); - } - } - break; - - case WM_NCHITTEST: - { - LRESULT res = DefWindowProc( hDlg, message, wParam, lParam ); - - if( res == HTCLIENT ) res = HTCAPTION; - - SetWindowLong( hDlg, DWL_MSGRESULT, res ); - - return TRUE; - } - break; -#endif - case WM_CLOSE: EvalGraphPopDown(); break; @@ -563,6 +266,7 @@ LRESULT CALLBACK EvalGraphProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM l return FALSE; } +// creates the eval graph, or unhides it. VOID EvalGraphPopUp() { FARPROC lpProc; @@ -591,6 +295,7 @@ VOID EvalGraphPopUp() evalGraphDialogUp = TRUE; } +// Note that this hides the window. It could as well have destroyed it. VOID EvalGraphPopDown() { CheckMenuItem(GetMenu(hwndMain), IDM_ShowEvalGraph, MF_UNCHECKED); @@ -602,6 +307,9 @@ VOID EvalGraphPopDown() evalGraphDialogUp = FALSE; } +// This function is the interface to the back-end. It is currently called through the front-end, +// though, where it shares the HistorySet() wrapper with MoveHistorySet(). Once all front-ends +// support the eval graph, it would be more logical to call it directly from the back-end. VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pvInfo ) { /* [AS] Danger! For now we rely on the pvInfo parameter being a static variable! */ @@ -615,8 +323,3 @@ VOID EvalGraphSet( int first, int last, int current, ChessProgramStats_Move * pv SendMessage( evalGraphDialog, WM_REFRESH_GRAPH, 0, 0 ); } } - -BOOL EvalGraphIsUp() -{ - return evalGraphDialogUp; -}