Fix Eval Graph resolution problems
[xboard.git] / xevalgraph.c
1 /*
2  * Evaluation graph
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  * Translated to X by H.G.Muller (Nov 2009)
6  *
7  * Copyright 2005 Alessandro Scotti
8  *
9  * Enhancements Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
10  *
11  * ------------------------------------------------------------------------
12  *
13  * GNU XBoard is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or (at
16  * your option) any later version.
17  *
18  * GNU XBoard is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see http://www.gnu.org/licenses/.
25  *
26  * ------------------------------------------------------------------------
27  ** See the file ChangeLog for a revision history.  */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if STDC_HEADERS
37 # include <stdlib.h>
38 # include <string.h>
39 #else /* not STDC_HEADERS */
40 extern char *getenv();
41 # if HAVE_STRING_H
42 #  include <string.h>
43 # else /* not HAVE_STRING_H */
44 #  include <strings.h>
45 # endif /* not HAVE_STRING_H */
46 #endif /* not STDC_HEADERS */
47
48 #if HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 #include <X11/Intrinsic.h>
53 #include <X11/StringDefs.h>
54 #include <X11/Shell.h>
55 #include <X11/Xaw/Dialog.h>
56 #include <X11/Xaw/Form.h>
57 #include <X11/Xaw/List.h>
58 #include <X11/Xaw/Label.h>
59 #include <X11/Xaw/SimpleMenu.h>
60 #include <X11/Xaw/SmeBSB.h>
61 #include <X11/Xaw/SmeLine.h>
62 #include <X11/Xaw/Box.h>
63 #include <X11/Xaw/Paned.h>
64 #include <X11/Xaw/MenuButton.h>
65 #include <X11/cursorfont.h>
66 #include <X11/Xaw/Text.h>
67 #include <X11/Xaw/AsciiText.h>
68 #include <X11/Xaw/Viewport.h>
69
70 #include <cairo/cairo.h>
71 #include <cairo/cairo-xlib.h>
72
73 #include "common.h"
74 #include "frontend.h"
75 #include "backend.h"
76 #include "dialogs.h"
77 #include "menus.h"
78 #include "xboard.h"
79 #include "evalgraph.h"
80 #include "gettext.h"
81
82 #ifdef ENABLE_NLS
83 # define  _(s) gettext (s)
84 # define N_(s) gettext_noop (s)
85 #else
86 # define  _(s) (s)
87 # define N_(s)  s
88 #endif
89
90 #include <X11/xpm.h>
91
92 #ifdef SNAP
93 #include "wsnap.h"
94 #endif
95
96 #define _LL_ 100
97
98 Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
99 Widget outputField[2][7]; // [HGM] front-end array to translate output field to window handle
100 static char *title = N_("Evaluation graph");
101
102 //extern WindowPlacement wpEvalGraph;
103
104 Position evalGraphX = -1, evalGraphY = -1;
105 Dimension evalGraphW, evalGraphH;
106
107 /* Module variables */
108
109 char *crWhite = "#FFFFB0";
110 char *crBlack = "#AD5D3D";
111 static Window eGraphWindow;
112 static cairo_surface_t *cs;
113
114 static Option *EvalCallback P((int button, int x, int y));
115
116 static float
117 Color (char *col, int n)
118 {
119   int c;
120   sscanf(col, "#%x", &c);
121   c = c >> 4*n & 255;
122   return c/255.;
123 }
124
125 static void
126 SetPen(cairo_t *cr, float w, char *col, int dash) {
127   static const double dotted[] = {4.0, 4.0};
128   static int len  = sizeof(dotted) / sizeof(dotted[0]);
129   cairo_set_line_width (cr, w);
130   cairo_set_source_rgba (cr, Color(col, 4), Color(col, 2), Color(col, 0), 1.0);
131   if(dash) cairo_set_dash (cr, dotted, len, 0.0);
132 }
133
134 static void
135 ChoosePen(cairo_t *cr, int i)
136 {
137   switch(i) {
138     case PEN_BLACK:
139       SetPen(cr, 1.0, "#000000", 0);
140       break;
141     case PEN_DOTTED:
142       SetPen(cr, 1.0, "#A0A0A0", 1);
143       break;
144     case PEN_BLUEDOTTED:
145       SetPen(cr, 1.0, "#0000FF", 1);
146       break;
147     case PEN_BOLDWHITE:
148       SetPen(cr, 3.0, crWhite, 0);
149       break;
150     case PEN_BOLDBLACK:
151       SetPen(cr, 3.0, crBlack, 0);
152       break;
153     case PEN_BACKGD:
154       SetPen(cr, 3.0, "#E0E0F0", 0);
155       break;
156   }
157 }
158
159 // [HGM] front-end, added as wrapper to avoid use of LineTo and MoveToEx in other routines (so they can be back-end)
160 void
161 DrawSegment (int x, int y, int *lastX, int *lastY, enum PEN penType)
162 {
163   static int curX, curY;
164
165   if(penType != PEN_NONE) {
166     cairo_t *cr = cairo_create(cs);
167     cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
168     cairo_move_to (cr, curX, curY);
169     cairo_line_to (cr, x,y);
170     ChoosePen(cr, penType);
171     cairo_stroke (cr);
172     cairo_destroy (cr);
173   }
174
175   if(lastX != NULL) { *lastX = curX; *lastY = curY; }
176   curX = x; curY = y;
177 }
178
179 // front-end wrapper for drawing functions to do rectangles
180 void
181 DrawRectangle (int left, int top, int right, int bottom, int side, int style)
182 {
183   cairo_t *cr;
184
185   cr = cairo_create (cs);
186   cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
187   cairo_rectangle (cr, left, top, right-left, bottom-top);
188   switch(side)
189     {
190     case 0: ChoosePen(cr, PEN_BOLDWHITE); break;
191     case 1: ChoosePen(cr, PEN_BOLDBLACK); break;
192     case 2: ChoosePen(cr, PEN_BACKGD); break;
193     }
194   cairo_fill (cr);
195
196   if(style != FILLED)
197     {
198       cairo_rectangle (cr, left, top, right-left-1, bottom-top-1);
199       ChoosePen(cr, PEN_BLACK);
200       cairo_stroke (cr);
201     }
202
203   cairo_destroy(cr);
204 }
205
206 // front-end wrapper for putting text in graph
207 void
208 DrawEvalText (char *buf, int cbBuf, int y)
209 {
210     // the magic constants 8 and 5 should really be derived from the font size somehow
211   cairo_text_extents_t extents;
212   cairo_t *cr = cairo_create(cs);
213
214   /* GTK-TODO this has to go into the font-selection */
215   cairo_select_font_face (cr, "Sans",
216                           CAIRO_FONT_SLANT_NORMAL,
217                           CAIRO_FONT_WEIGHT_NORMAL);
218   cairo_set_font_size (cr, 12.0);
219
220
221   cairo_text_extents (cr, buf, &extents);
222
223   cairo_move_to (cr, MarginX - 2 - 8*cbBuf, y+5);
224   cairo_text_path (cr, buf);
225   cairo_set_source_rgb (cr, 0.0, 0.0, 0);
226   cairo_fill_preserve (cr);
227   cairo_set_source_rgb (cr, 0, 1.0, 0);
228   cairo_set_line_width (cr, 0.1);
229   cairo_stroke (cr);
230
231   /* free memory */
232   cairo_destroy (cr);
233 }
234
235 static int initDone = FALSE;
236
237 static void
238 InitializeEvalGraph (Option *opt, int w, int h)
239 {
240   eGraphWindow = XtWindow(opt->handle);
241
242   if(w == 0) {
243     Arg args[10];
244     XtSetArg(args[0], XtNwidth, &evalGraphW);
245     XtSetArg(args[1], XtNheight, &evalGraphH);
246     XtGetValues(opt->handle, args, 2);
247     nWidthPB = evalGraphW; nHeightPB = evalGraphH;
248   } else nWidthPB = w, nHeightPB = h;
249
250   if(cs) cairo_surface_destroy(cs);
251   cs=cairo_xlib_surface_create(xDisplay, eGraphWindow, DefaultVisual(xDisplay, 0), nWidthPB, nHeightPB);
252
253   initDone = TRUE;
254 }
255
256 // The following stuff is really back-end (but too little to bother with a separate file)
257
258 static void
259 DisplayEvalGraph ()
260 {   // back-end painting; calls back front-end primitives for lines, rectangles and text
261     char *t = MakeEvalTitle(_(title));
262     if(t != title && nWidthPB < 340) t = MakeEvalTitle(nWidthPB < 240 ? "" : _("Eval"));
263     PaintEvalGraph();
264     SetDialogTitle(EvalGraphDlg, t);
265 }
266
267 static void
268 EvalClick (int x, int y)
269 {
270     int index = GetMoveIndexFromPoint( x, y );
271
272     if( index >= 0 && index < currLast ) ToNrEvent( index + 1 );
273 }
274
275 static Option graphOptions[] = {
276 { 150, 0x9C, 300, NULL, (void*) &EvalCallback, NULL, NULL, Graph , "" },
277 { 0, 2, 0, NULL, NULL, "", NULL, EndMark , "" }
278 };
279
280 static Option *
281 EvalCallback (int button, int x, int y)
282 {
283     if(!initDone) return NULL;
284
285     switch(button) {
286         case 10: // expose event
287             /* Create or recreate paint box if needed */
288             if(x != nWidthPB || y != nHeightPB) {
289                 InitializeEvalGraph(&graphOptions[0], x, y);
290             }
291             nWidthPB = x;
292             nHeightPB = y;
293             DisplayEvalGraph();
294             break;
295         case 1: EvalClick(x, y); // left button
296         default: break; // other buttons ignored
297     }
298     return NULL; // no context menu!
299 }
300
301 void
302 EvalGraphPopUp ()
303 {
304     if (GenericPopUp(graphOptions, _(title), EvalGraphDlg, BoardWindow, NONMODAL, 1)) {
305         InitializeEvalGraph(&graphOptions[0], 0, 0); // first time: add callbacks and initialize pens
306     } else {
307         SetDialogTitle(EvalGraphDlg, _(title));
308         SetIconName(EvalGraphDlg, _(title));
309     }
310
311     MarkMenu("View.EvaluationGraph", EvalGraphDlg);
312
313 //    ShowThinkingEvent(); // [HGM] thinking: might need to prompt engine for thinking output
314 }
315
316 void
317 EvalGraphPopDown ()
318 {
319     PopDown(EvalGraphDlg);
320
321 //    ShowThinkingEvent(); // [HGM] thinking: might need to shut off thinking output
322 }
323
324 Boolean
325 EvalGraphIsUp ()
326 {
327     return shellUp[EvalGraphDlg];
328 }
329
330 int
331 EvalGraphDialogExists ()
332 {
333     return DialogExists(EvalGraphDlg);
334 }
335
336 void
337 EvalGraphProc ()
338 {
339   if (!PopDown(EvalGraphDlg)) EvalGraphPopUp();
340 }
341
342 // This function is the interface to the back-end.
343
344 void
345 EvalGraphSet (int first, int last, int current, ChessProgramStats_Move * pvInfo)
346 {
347     /* [AS] Danger! For now we rely on the pvInfo parameter being a static variable! */
348
349     currFirst = first;
350     currLast = last;
351     currCurrent = current;
352     currPvInfo = pvInfo;
353
354     if( DialogExists(EvalGraphDlg) ) {
355         DisplayEvalGraph();
356     }
357 }
358