49637b28057c2004dab140ac1603d7cb7a507573
[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 typedef void (*DrawFunc)();
103 DrawFunc ChooseDrawFunc();
104
105 // imports from xboard.c
106 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
107 extern Display *xDisplay;
108 extern Window xBoardWindow;
109 extern int squareSize;
110 extern Pixmap xMarkPixmap, wIconPixmap, bIconPixmap;
111 extern char *layoutName;
112 extern int lineGap;
113
114 Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
115 Widget outputField[2][7]; // [HGM] front-end array to translate output field to window handle
116
117 /* Imports from backend.c */
118
119 /* Imports from xboard.c */
120 extern Arg layoutArgs[2], formArgs[2], messageArgs[4];
121 extern GC coordGC;
122
123 //extern WindowPlacement wpEvalGraph;
124
125 /* Module variables */
126
127 static Display *zDisplay;
128 static Window promoWindow;
129 static int nrow, ncol;
130 extern Board promoBoard;
131
132 static GC blackBrush;
133
134 Widget promoShell;
135
136 void PromoPopDown()
137 {
138     if (!promoShell) return;
139     XtPopdown(promoShell);
140     XtDestroyWidget(promoShell);
141     promoShell = NULL;
142 }
143
144 // front-end
145 static Pixel MakeColor(char *color )
146 {
147     XrmValue vFrom, vTo;
148
149     vFrom.addr = (caddr_t) color;
150     vFrom.size = strlen(color);
151     XtConvert(promoShell, XtRString, &vFrom, XtRPixel, &vTo);
152     // test for NULL?
153
154     return *(Pixel *) vTo.addr;
155 }
156
157 static GC CreateGC(int width, char *fg, char *bg, int style)
158 {
159     XtGCMask value_mask = GCLineWidth | GCLineStyle | GCForeground
160       | GCBackground | GCFunction | GCPlaneMask;
161     XGCValues gc_values;
162
163     gc_values.plane_mask = AllPlanes;
164     gc_values.line_width = width;
165     gc_values.line_style = style;
166     gc_values.function = GXcopy;
167
168     gc_values.foreground = MakeColor(fg);
169     gc_values.background = MakeColor(bg);
170
171     return XtGetGC(promoShell, value_mask, &gc_values);
172 }
173
174 void DrawPromoSquare(row, column, piece)
175      int row, column;
176      ChessSquare piece;
177 {
178     int square_color, x, y;
179     DrawFunc drawfunc;
180
181     x = lineGap + column * (squareSize + lineGap);
182     y = lineGap + row * (squareSize + lineGap);
183
184     square_color = 1;
185
186     if (piece == EmptySquare) {
187         BlankSquare(x, y, square_color, piece, promoWindow);
188     } else {
189         drawfunc = ChooseDrawFunc();
190         drawfunc(piece, square_color, x, y, promoWindow);
191     }
192
193 }
194
195 // front-end. Create pens, device context and buffer bitmap for global use, copy result to display
196 // The back-end part n the middle has been taken out and moed to PainEvalGraph()
197 static void DisplayPromoDialog()
198 {
199     int j, r, f;
200     int width;
201     int height;
202     Dimension w, h;
203     Arg args[6];
204
205     /* Get client area */
206     j = 0;
207     XtSetArg(args[j], XtNwidth, &w); j++;
208     XtSetArg(args[j], XtNheight, &h); j++;
209     XtGetValues(promoShell, args, j);
210     width = w;
211     height = h;
212
213     /* Create or recreate paint box if needed */
214     if( width != nWidthPB || height != nHeightPB ) {
215
216         nWidthPB = width;
217         nHeightPB = height;
218     }
219
220     XFillRectangle(zDisplay, promoWindow, blackBrush, 0, 0, w, h);
221     for(r = 0; r<nrow; r++) for(f=0; f<ncol; f++) 
222         DrawPromoSquare(r, f, promoBoard[r][f+BOARD_LEFT]);
223
224     XSync(zDisplay, False);
225 }
226
227 static void InitializePromoDialog()
228 { int i;    XtGCMask value_mask = GCLineWidth | GCLineStyle | GCForeground
229       | GCBackground | GCFunction | GCPlaneMask;
230     XGCValues gc_values;
231
232     blackBrush = CreateGC(3, "#000000", "#000000", LineSolid);
233 }
234
235 void PromoClick(widget, unused, event)
236      Widget widget;
237      caddr_t unused;
238      XEvent *event;
239 {
240     int boardHeight = lineGap + BOARD_HEIGHT * (squareSize + lineGap);
241     int boardWidth  = lineGap + BOARD_WIDTH  * (squareSize + lineGap);
242     int boardLeft   = BOARD_LEFT  * (squareSize + lineGap);
243     int x = event->xbutton.x;
244     int y = event->xbutton.y;
245
246     // translate click to as if it was on main board near a1
247     if(flipView) x = boardWidth - x; else y = boardHeight - y;
248
249     if( widget) {
250         if(event->type == ButtonPress)   LeftClick(Press,   x + boardLeft, y);
251         if(event->type == ButtonRelease) RightClick(Release, x + boardLeft, y, &y, &y);
252     }
253     PromoPopDown();
254 }
255
256 // This (cloned from EventProc in xboard.c) is needed as event handler, to prevent
257 // the graph being wiped out after covering / uncovering by other windows.
258 void PromoEventProc(widget, unused, event)
259      Widget widget;
260      caddr_t unused;
261      XEvent *event;
262 {
263     if (!XtIsRealized(widget))
264       return;
265
266     switch (event->type) {
267       case Expose:
268         if (event->xexpose.count > 0) return;  /* no clipping is done */
269         DisplayPromoDialog();
270         break;
271       default:
272         return;
273     }
274 }
275
276 void PromoCallback(w, client_data, call_data)
277      Widget w;
278      XtPointer client_data, call_data;
279 {
280     String name;
281     Widget w2;
282     Arg args[16];
283     char buf[80];
284     VariantClass v;
285     
286     XtSetArg(args[0], XtNlabel, &name);
287     XtGetValues(w, args, 1);
288     
289     if (strcmp(name, _("clear board")) == 0) {
290         EditPositionMenuEvent(ClearBoard, 0, 0);
291     }
292     PromoPopDown();
293 }
294
295 Widget PromoCreate(name, x, y, clear)
296      char *name;
297      int x, y;
298      Boolean clear;
299 {
300     Arg args[16];
301     Widget shell, layout, form, panel, b_clear, b_grant, b_revoke;
302     int j, h, w;
303
304     // define form within layout within shell.
305     j = 0;
306     XtSetArg(args[j], XtNresizable, True);  j++;
307     shell =
308       XtCreatePopupShell(name, transientShellWidgetClass,
309                          shellWidget, args, j);
310     layout =
311       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
312                             layoutArgs, XtNumber(layoutArgs));
313     form =
314       XtCreateManagedWidget("form", formWidgetClass, layout,
315                             formArgs, XtNumber(formArgs));
316     panel =
317       XtCreateManagedWidget("panel", formWidgetClass, form,
318                             formArgs, XtNumber(formArgs));
319     // make sure width is known in advance, for better placement of child widgets
320     j = 0;
321     XtSetArg(args[j], XtNwidth,  (XtArgVal) (w = ncol*squareSize + (ncol+1)*lineGap)); j++;
322     XtSetArg(args[j], XtNheight, (XtArgVal) (h = nrow*squareSize + (nrow+1)*lineGap)); j++;
323     XtSetValues(panel, args, j);
324
325     if(clear) { // piece menu: add buttons
326         j=0;
327         XtSetArg(args[j], XtNfromVert, panel);  j++;
328         b_clear = XtCreateManagedWidget(_("clear board"), commandWidgetClass, form, args, j);
329         XtAddCallback(b_clear, XtNcallback, PromoCallback, (XtPointer) 0);
330
331         j=0;
332         XtSetArg(args[j], XtNfromHoriz, b_clear);  j++;
333         XtSetArg(args[j], XtNfromVert, panel);  j++;
334         b_grant = XtCreateManagedWidget(_("grant rights"), commandWidgetClass, form, args, j);
335         XtAddCallback(b_grant, XtNcallback, PromoCallback, (XtPointer) 0);
336
337         j=0;
338         XtSetArg(args[j], XtNfromHoriz, b_grant);  j++;
339         XtSetArg(args[j], XtNfromVert, panel);  j++;
340         b_revoke = XtCreateManagedWidget(_("revoke rights"), commandWidgetClass, form, args, j);
341         XtAddCallback(b_revoke, XtNcallback, PromoCallback, (XtPointer) 0);
342     }
343
344     if(y >= 0) {
345         Dimension bx, by;
346         if(y > h) y -= h - 5; else y += 60;
347         x -= w/2; if(x<10) x = 10;
348         j = 0;
349         XtSetArg(args[j], XtNx, &bx); j++;
350         XtSetArg(args[j], XtNy, &by); j++;
351         XtGetValues(boardWidget, args, j);
352         j = 0;
353         XtSetArg(args[j], XtNx, (XtArgVal) x+bx); j++;
354         XtSetArg(args[j], XtNy, (XtArgVal) y+by); j++;
355         XtSetValues(shell, args, j);
356     }
357
358     XtRealizeWidget(shell);
359
360     zDisplay = XtDisplay(shell);
361     promoWindow = XtWindow(panel);
362     XtAddEventHandler(panel, ExposureMask, False,
363                       (XtEventHandler) PromoEventProc, NULL);
364     XtAddEventHandler(panel, ButtonPressMask|ButtonReleaseMask, False,
365                       (XtEventHandler) PromoClick, NULL);
366
367     return shell;
368 }
369
370 void 
371 PromoDialog(int h, int w, Board b, Boolean clear, char *title, int x, int y)
372 {
373     Arg args[16];
374     int j;
375     Widget edit;
376     static int  needInit = TRUE;
377
378     nrow = h; ncol = w;
379
380     if (promoShell == NULL) {
381
382         promoShell =
383           PromoCreate(title, x, y, clear);
384         XtRealizeWidget(promoShell);
385         CatchDeleteWindow(promoShell, "PromoPopDown");
386         InitializePromoDialog();
387     }
388
389     XtPopup(promoShell, XtGrabExclusive);
390     XSync(zDisplay, False);
391 //    DisplayPromoDialog();
392 }
393