True graphical promotion popup for XBoard
[xboard.git] / xpromo.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 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 "common.h"
71 #include "frontend.h"
72 #include "backend.h"
73 #include "xboard.h"
74 #include "evalgraph.h"
75 #include "gettext.h"
76
77 #ifdef ENABLE_NLS
78 # define  _(s) gettext (s)
79 # define N_(s) gettext_noop (s)
80 #else
81 # define  _(s) (s)
82 # define N_(s)  s
83 #endif
84
85 #include <X11/xpm.h>
86
87 // [HGM] pixmaps of some ICONS used in the engine-outut window
88 #include "pixmaps/WHITE_14.xpm"
89 #include "pixmaps/BLACK_14.xpm"
90 #include "pixmaps/CLEAR_14.xpm"
91 #include "pixmaps/UNKNOWN_14.xpm"
92 #include "pixmaps/THINKING_14.xpm"
93 #include "pixmaps/PONDER_14.xpm"
94 #include "pixmaps/ANALYZING_14.xpm"
95
96 #ifdef SNAP
97 #include "wsnap.h"
98 #endif
99
100 #define _LL_ 100
101
102 #define MARGIN 3
103
104 typedef void (*DrawFunc)();
105 DrawFunc ChooseDrawFunc();
106
107 // imports from xboard.c
108 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
109 extern Display *xDisplay;
110 extern Window xBoardWindow;
111 extern int squareSize;
112 extern Pixmap xMarkPixmap, wIconPixmap, bIconPixmap;
113 extern char *layoutName;
114 extern int lineGap;
115
116 Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
117 Widget outputField[2][7]; // [HGM] front-end array to translate output field to window handle
118
119 /* Imports from backend.c */
120
121 /* Imports from xboard.c */
122 extern Arg layoutArgs[2], formArgs[2], messageArgs[4];
123 extern GC coordGC;
124
125 //extern WindowPlacement wpEvalGraph;
126
127 /* Module variables */
128
129 static Display *zDisplay;
130 static Window promoWindow;
131 static int nrow, ncol;
132 extern Board promoBoard;
133
134 static GC blackBrush;
135
136 Widget promoShell;
137
138 void PromoPopDown()
139 {
140     if (!promoShell) return;
141     XtPopdown(promoShell);
142     XtDestroyWidget(promoShell);
143     promoShell = NULL;
144 }
145
146 // front-end
147 static Pixel MakeColor(char *color )
148 {
149     XrmValue vFrom, vTo;
150
151     vFrom.addr = (caddr_t) color;
152     vFrom.size = strlen(color);
153     XtConvert(promoShell, XtRString, &vFrom, XtRPixel, &vTo);
154     // test for NULL?
155
156     return *(Pixel *) vTo.addr;
157 }
158
159 static GC CreateGC(int width, char *fg, char *bg, int style)
160 {
161     XtGCMask value_mask = GCLineWidth | GCLineStyle | GCForeground
162       | GCBackground | GCFunction | GCPlaneMask;
163     XGCValues gc_values;
164
165     gc_values.plane_mask = AllPlanes;
166     gc_values.line_width = width;
167     gc_values.line_style = style;
168     gc_values.function = GXcopy;
169
170     gc_values.foreground = MakeColor(fg);
171     gc_values.background = MakeColor(bg);
172
173     return XtGetGC(promoShell, value_mask, &gc_values);
174 }
175
176 void DrawPromoSquare(row, column, piece)
177      int row, column;
178      ChessSquare piece;
179 {
180     int square_color, x, y;
181     DrawFunc drawfunc;
182
183     x = lineGap + column * (squareSize + lineGap);
184     y = lineGap + row * (squareSize + lineGap) + MARGIN;
185
186     square_color = 1;
187
188     if (piece == EmptySquare) {
189         BlankSquare(x, y, square_color, piece, promoWindow);
190     } else {
191         drawfunc = ChooseDrawFunc();
192         drawfunc(piece, square_color, x, y, promoWindow);
193     }
194
195 }
196
197 // front-end. Create pens, device context and buffer bitmap for global use, copy result to display
198 // The back-end part n the middle has been taken out and moed to PainEvalGraph()
199 static void DisplayPromoDialog()
200 {
201     int j, r, f;
202     int width;
203     int height;
204     Dimension w, h;
205     Arg args[6];
206
207     /* Get client area */
208     j = 0;
209     XtSetArg(args[j], XtNwidth, &w); j++;
210     XtSetArg(args[j], XtNheight, &h); j++;
211     XtGetValues(promoShell, args, j);
212     width = w;
213     height = h;
214
215     /* Create or recreate paint box if needed */
216     if( width != nWidthPB || height != nHeightPB ) {
217
218         nWidthPB = width;
219         nHeightPB = height;
220     }
221
222     XFillRectangle(zDisplay, promoWindow, blackBrush, 0, MARGIN, w, h);
223     for(r = 0; r<nrow; r++) for(f=0; f<ncol; f++) 
224         DrawPromoSquare(r, f, promoBoard[r][f+BOARD_LEFT]);
225
226     XSync(zDisplay, False);
227 }
228
229 static void InitializePromoDialog()
230 { int i;    XtGCMask value_mask = GCLineWidth | GCLineStyle | GCForeground
231       | GCBackground | GCFunction | GCPlaneMask;
232     XGCValues gc_values;
233
234     blackBrush = CreateGC(3, "#000000", "#000000", LineSolid);
235 }
236
237 void PromoClick(widget, unused, event)
238      Widget widget;
239      caddr_t unused;
240      XEvent *event;
241 {
242     int boardHeight = lineGap + BOARD_HEIGHT * (squareSize + lineGap);
243     int boardWidth  = lineGap + BOARD_WIDTH  * (squareSize + lineGap);
244     int boardLeft   = BOARD_LEFT  * (squareSize + lineGap);
245     int x = event->xbutton.x;
246     int y = event->xbutton.y - MARGIN;
247
248     if(y <= lineGap) y = lineGap + 1;
249
250     // translate click to as if it was on main board near a1
251     if(flipView) x = boardWidth - x; else y = boardHeight - y;
252
253     if( widget) {
254         if(event->type == ButtonPress)   LeftClick(Press,   x + boardLeft, y);
255         if(event->type == ButtonRelease) RightClick(Release, x + boardLeft, y, &y, &y);
256     }
257     PromoPopDown();
258 }
259
260 // This (cloned from EventProc in xboard.c) is needed as event handler, to prevent
261 // the graph being wiped out after covering / uncovering by other windows.
262 void PromoEventProc(widget, unused, event)
263      Widget widget;
264      caddr_t unused;
265      XEvent *event;
266 {
267     if (!XtIsRealized(widget))
268       return;
269
270     switch (event->type) {
271       case Expose:
272         if (event->xexpose.count > 0) return;  /* no clipping is done */
273         DisplayPromoDialog();
274         break;
275       default:
276         return;
277     }
278 }
279 // The following routines are mutated clones of the commentPopUp routines
280
281 Widget PromoCreate(name, x, y)
282      char *name;
283      int x, y;
284 {
285     Arg args[16];
286     Widget shell, layout, form;
287     int j, h, w;
288
289     // define form within layout within shell.
290     j = 0;
291     XtSetArg(args[j], XtNresizable, True);  j++;
292     shell =
293       XtCreatePopupShell(name, transientShellWidgetClass,
294                          shellWidget, args, j);
295     layout =
296       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
297                             layoutArgs, XtNumber(layoutArgs));
298     // divide window vertically into two equal parts, by creating two forms
299     form =
300       XtCreateManagedWidget("form", formWidgetClass, layout,
301                             formArgs, XtNumber(formArgs));
302     // make sure width is known in advance, for better placement of child widgets
303     j = 0;
304     XtSetArg(args[j], XtNwidth,  (XtArgVal) (w = ncol*squareSize + (ncol+1)*lineGap)); j++;
305     XtSetArg(args[j], XtNheight, (XtArgVal) (h = nrow*squareSize + (nrow+1)*lineGap + MARGIN)); j++;
306     XtSetValues(form, args, j);
307
308     if(y >= 0) {
309         Dimension bx, by;
310         if(y > h) y -= h - 5; else y += 60;
311         x -= w/2; if(x<10) x = 10;
312         j = 0;
313         XtSetArg(args[j], XtNx, &bx); j++;
314         XtSetArg(args[j], XtNy, &by); j++;
315         XtGetValues(boardWidget, args, j);
316         j = 0;
317         XtSetArg(args[j], XtNx, (XtArgVal) x+bx); j++;
318         XtSetArg(args[j], XtNy, (XtArgVal) y+by); j++;
319         XtSetValues(shell, args, j);
320     }
321
322     XtRealizeWidget(shell);
323
324     zDisplay = XtDisplay(shell);
325     promoWindow = XtWindow(form);
326     XtAddEventHandler(form, ExposureMask, False,
327                       (XtEventHandler) PromoEventProc, NULL);
328     XtAddEventHandler(form, ButtonPressMask, False,
329                       (XtEventHandler) PromoClick, NULL);
330     XtAddEventHandler(form, ButtonReleaseMask, False,
331                       (XtEventHandler) PromoClick, NULL);
332
333     return shell;
334 }
335
336 void 
337 PromoDialog(int h, int w, Board b, Boolean clear, char *title, int x, int y)
338 {
339     Arg args[16];
340     int j;
341     Widget edit;
342     static int  needInit = TRUE;
343
344     nrow = h; ncol = w;
345
346     if (promoShell == NULL) {
347
348         promoShell =
349           PromoCreate(title, x, y);
350         XtRealizeWidget(promoShell);
351         CatchDeleteWindow(promoShell, "PromoPopDown");
352         InitializePromoDialog();
353     }
354
355     XtPopup(promoShell, XtGrabExclusive);
356     XSync(zDisplay, False);
357 //    DisplayPromoDialog();
358 }
359