0ae04cca10cea96e05c7d4c261aa5d3b208a0b47
[xboard.git] / callback.c
1 #include <gtk/gtk.h>
2 #include "common.h"
3 #include <errno.h>
4 #include "backend.h"
5
6 #ifdef ENABLE_NLS
7 # define  _(s) gettext (s)
8 # define N_(s) gettext_noop (s)
9 #else
10 # define  _(s) (s)
11 # define N_(s)  s
12 #endif
13
14
15 extern GtkWidget  *about;
16 extern GtkWidget  *GUI_Window;
17
18 extern char *programVersion;
19 extern int errorExitStatus;
20 extern int promotionUp;
21 extern int fromX;
22 extern int fromY;
23 extern int toX;
24 extern int toY;
25
26 void
27 QuitProc (object, user_data)
28      GtkObject *object;
29      gpointer user_data;
30 {
31   gtk_main_quit();
32   ExitEvent(0);
33 }
34
35 void AboutProc (object, user_data)
36      GtkObject *object;
37      gpointer user_data;
38 {
39   GtkWidget               *about;
40
41   const gchar *authors[] = {"Tim Mann <tim@tim-mann.org>",
42                             "John Chanak",
43                             "Evan Welsh <Evan.Welsh@msdw.com>",
44                             "Elmar Bartel <bartel@informatik.tu-muenchen.de>",
45                             "Jochen Wiedmann",
46                             "Frank McIngvale",
47                             "Hugh Fisher <Hugh.Fisher@cs.anu.edu.au>",
48                             "Allessandro Scotti",
49                             "H.G. Muller <h.g.muller AT hccnet DOT nl>",
50                             "Eric Mullins <emwine AT earthlink DOT net>",
51                             "Arun Persaud <arun@nubati.net>"};
52
53   /* set up about window */
54   about =  GTK_WIDGET(gtk_about_dialog_new());
55
56   /* fill in some information */
57   char buf[MSG_SIZ];
58 #if ZIPPY
59   char *zippy = " (with Zippy code)";
60 #else
61   char *zippy = "";
62 #endif
63   sprintf(buf, "%s%s",  programVersion, zippy);
64
65   gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(about),buf);
66
67   gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(about),
68                                  "Copyright 1991 Digital Equipment Corporation\n"
69                                  "Enhancements Copyright 1992-2009 Free Software Foundation\n"
70                                  "Enhancements Copyright 2005 Alessandro Scotti");
71   gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(about),"http://www.gnu.org/software/xboard/");
72   gtk_about_dialog_set_authors(GTK_ABOUT_DIALOG(about),authors);
73   gtk_about_dialog_set_translator_credits(GTK_ABOUT_DIALOG(about),
74                                           " A. Alper (turkish)\n"
75                                           " A. Persaud (german)\n");
76
77   /* end set up about window */
78   gtk_dialog_run(GTK_DIALOG (about));
79   gtk_widget_destroy(about);
80 }
81
82 void LoadNextGameProc(object, user_data)
83      GtkObject *object;
84      gpointer user_data;
85 {
86     ReloadGame(1);
87     return;
88 }
89
90 void LoadPrevGameProc(object, user_data)
91      GtkObject *object;
92      gpointer user_data;
93 {
94     ReloadGame(-1);
95     return;
96 }
97
98 void ReloadGameProc(object, user_data)
99      GtkObject *object;
100      gpointer user_data;
101 {
102     ReloadGame(0);
103     return;
104 }
105
106
107 gboolean CloseWindowProc(GtkWidget *button)
108 {
109     gtk_widget_destroy(gtk_widget_get_toplevel(button));
110     return TRUE;
111 }
112
113 void
114 ResetProc (object, user_data)
115      GtkObject *object;
116      gpointer user_data;
117 {
118   ResetGameEvent();
119   AnalysisPopDown();
120 }
121
122 void WhiteClockProc(object, user_data)
123      GtkObject *object;
124      gpointer user_data;
125 {
126     if (gameMode == EditPosition || gameMode == IcsExamining) {
127         SetWhiteToPlayEvent();
128     } else if (gameMode == IcsPlayingBlack || gameMode == MachinePlaysWhite) {
129         CallFlagEvent();
130     }
131 }
132
133 void BlackClockProc(object, user_data)
134      GtkObject *object;
135      gpointer user_data;
136 {
137     if (gameMode == EditPosition || gameMode == IcsExamining) {
138         SetBlackToPlayEvent();
139     } else if (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack) {
140         CallFlagEvent();
141     }
142 }
143
144
145 void ShowCoordsProc(object, user_data)
146      GtkObject *object;
147      gpointer user_data;
148 {
149     appData.showCoords = !appData.showCoords;
150
151     DrawPosition(True, NULL);
152 }
153
154 void ErrorPopDownProc(object, user_data)
155      GtkObject *object;
156      gpointer user_data;
157 {
158   gtk_widget_destroy(GTK_WIDGET(object));
159   ErrorPopDown();
160 }
161
162 void PauseProc(object, user_data)
163      GtkObject *object;
164      gpointer user_data;
165 {
166     // todo this toggling of the pause button doesn't seem to work?
167     // e.g. select pause from buttonbar doesn't activate menumode.pause
168   PauseEvent();
169 }
170
171
172 void LoadGameProc(object, user_data)
173      GtkObject *object;
174      gpointer user_data;
175 {
176   GtkWidget *dialog;
177   dialog = gtk_file_chooser_dialog_new (_("Load game file name?"),
178                                         GTK_WINDOW(GUI_Window),
179                                         GTK_FILE_CHOOSER_ACTION_OPEN,
180                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
181                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
182                                         NULL);
183   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
184     {
185       char *filename;
186       FILE *f;
187
188       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
189
190       //see loadgamepopup
191       f = fopen(filename, "rb");
192       if (f == NULL) {
193         DisplayError(_("Failed to open file"), errno);
194       } else {
195         /* TODO add indec */
196         (void) LoadGamePopUp(f, 0, filename);
197       }
198       g_free (filename);
199     }
200   gtk_widget_destroy (dialog);
201   ModeHighlight();
202 }
203
204
205 /*************
206  * EVENTS
207  *************/
208
209 void EventProc(window, event, data)
210      GtkWindow *window;
211      GdkEvent *event;
212      gpointer data;
213 {
214   /* todo do we still need this?
215     if (!XtIsRealized(widget))
216       return;
217   */
218
219     switch (event->type) {
220       case GDK_EXPOSE:
221         if (event->expose.count > 0) return;  /* no clipping is done */
222         DrawPosition(True, NULL);
223         break;
224       default:
225         return;
226     }
227 }
228
229
230 /*
231  * event handler for parsing user moves
232  */
233 void UserMoveProc(window, event, data)
234      GtkWindow *window;
235      GdkEvent *event;
236      gpointer data;
237 {
238     int x, y;
239     Boolean saveAnimate;
240     static int second = 0;
241
242     if (errorExitStatus != -1) return;
243
244     if (event->type == GDK_BUTTON_PRESS) ErrorPopDown();
245
246     if (promotionUp)
247       {
248         if (event->type == GDK_BUTTON_PRESS)
249           {
250             /* todo add promotionshellwidget
251                XtPopdown(promotionShell);
252                XtDestroyWidget(promotionShell); */
253             promotionUp = False;
254             ClearHighlights();
255             fromX = fromY = -1;
256           }
257         else
258           {
259             return;
260           }
261       }
262
263     x = EventToSquare( (int)event->button.x, BOARD_WIDTH  );
264     y = EventToSquare( (int)event->button.y, BOARD_HEIGHT );
265     if (!flipView && y >= 0)
266       {
267         y = BOARD_HEIGHT - 1 - y;
268       }
269     if (flipView && x >= 0)
270       {
271         x = BOARD_WIDTH - 1 - x;
272       }
273
274     if (fromX == -1)
275       {
276         if (event->type == ButtonPress)
277           {
278             /* First square */
279             if (OKToStartUserMove(x, y))
280               {
281                 fromX = x;
282                 fromY = y;
283                 second = 0;
284                 DragPieceBegin(event->button.x, event->button.y);
285                 if (appData.highlightDragging)
286                   {
287                     SetHighlights(x, y, -1, -1);
288                   }
289               }
290           }
291         return;
292       }
293
294     /* fromX != -1 */
295     if (event->type == GDK_BUTTON_PRESS && gameMode != EditPosition &&
296         x >= 0 && y >= 0) {
297         ChessSquare fromP;
298         ChessSquare toP;
299         /* Check if clicking again on the same color piece */
300         fromP = boards[currentMove][fromY][fromX];
301         toP = boards[currentMove][y][x];
302         if ((WhitePawn <= fromP && fromP <= WhiteKing &&
303              WhitePawn <= toP && toP <= WhiteKing) ||
304             (BlackPawn <= fromP && fromP <= BlackKing &&
305              BlackPawn <= toP && toP <= BlackKing)) {
306             /* Clicked again on same color piece -- changed his mind */
307             second = (x == fromX && y == fromY);
308             if (appData.highlightDragging) {
309                 SetHighlights(x, y, -1, -1);
310             } else {
311                 ClearHighlights();
312             }
313             if (OKToStartUserMove(x, y)) {
314                 fromX = x;
315                 fromY = y;
316                 DragPieceBegin(event->button.x, event->button.y);
317             }
318             return;
319         }
320     }
321
322     if (event->type == GDK_BUTTON_RELEASE &&    x == fromX && y == fromY)
323       {
324         DragPieceEnd(event->button.x, event->button.y);
325         if (appData.animateDragging)
326           {
327             /* Undo animation damage if any */
328             DrawPosition(FALSE, NULL);
329           }
330         if (second)
331           {
332             /* Second up/down in same square; just abort move */
333             second = 0;
334             fromX = fromY = -1;
335             ClearHighlights();
336             gotPremove = 0;
337             ClearPremoveHighlights();
338           }
339         else
340           {
341             /* First upclick in same square; start click-click mode */
342             SetHighlights(x, y, -1, -1);
343           }
344         return;
345       }
346
347     /* Completed move */
348     toX = x;
349     toY = y;
350     saveAnimate = appData.animate;
351
352     if (event->type == GDK_BUTTON_PRESS)
353       {
354         /* Finish clickclick move */
355         if (appData.animate || appData.highlightLastMove)
356           {
357             SetHighlights(fromX, fromY, toX, toY);
358           }
359         else
360           {
361             ClearHighlights();
362           }
363       }
364     else
365       {
366         /* Finish drag move */
367         if (appData.highlightLastMove)
368           {
369             SetHighlights(fromX, fromY, toX, toY);
370           }
371         else
372           {
373             ClearHighlights();
374           }
375         DragPieceEnd(event->button.x, event->button.y);
376         /* Don't animate move and drag both */
377         appData.animate = FALSE;
378       }
379
380     if (IsPromotion(fromX, fromY, toX, toY))
381       {
382         if (appData.alwaysPromoteToQueen)
383           {
384             UserMoveEvent(fromX, fromY, toX, toY, 'q');
385             if (!appData.highlightLastMove || gotPremove) ClearHighlights();
386             if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
387             fromX = fromY = -1;
388           }
389         else
390           {
391             SetHighlights(fromX, fromY, toX, toY);
392             PromotionPopUp();
393           }
394       }
395     else
396       {
397         UserMoveEvent(fromX, fromY, toX, toY, NULLCHAR);
398
399         if (!appData.highlightLastMove || gotPremove) ClearHighlights();
400         if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
401         fromX = fromY = -1;
402       }
403
404     appData.animate = saveAnimate;
405     if (appData.animate || appData.animateDragging) {
406         /* Undo animation damage if needed */
407         DrawPosition(FALSE, NULL);
408     }
409
410     return;
411 }
412