more callbacks
[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 void MachineWhiteProc(object, user_data)
107      GtkObject *object;
108      gpointer user_data;
109 {
110     MachineWhiteEvent();
111     return;
112 }
113
114 void MachineBlackProc(object, user_data)
115      GtkObject *object;
116      gpointer user_data;
117 {
118     MachineBlackEvent();
119     return;
120 }
121
122 void TwoMachinesProc(object, user_data)
123      GtkObject *object;
124      gpointer user_data;
125 {
126     TwoMachinesEvent();
127     return;
128 }
129
130 void AcceptProc(object, user_data)
131      GtkObject *object;
132      gpointer user_data;
133 {
134     AcceptEvent();
135     return;
136 }
137
138 void DeclineProc(object, user_data)
139      GtkObject *object;
140      gpointer user_data;
141 {
142     DeclineEvent();
143     return;
144 }
145
146 void RematchProc(object, user_data)
147      GtkObject *object;
148      gpointer user_data;
149 {
150     RematchEvent();
151     return;
152 }
153
154 void CallFlagProc(object, user_data)
155      GtkObject *object;
156      gpointer user_data;
157 {
158     CallFlagEvent();
159     return;
160 }
161
162 void DrawProc(object, user_data)
163      GtkObject *object;
164      gpointer user_data;
165 {
166     DrawEvent();
167     return;
168 }
169
170 void AbortProc(object, user_data)
171      GtkObject *object;
172      gpointer user_data;
173 {
174     AbortEvent();
175     return;
176 }
177
178 void AdjournProc(object, user_data)
179      GtkObject *object;
180      gpointer user_data;
181 {
182     AdjournEvent();
183     return;
184 }
185
186 void ResignProc(object, user_data)
187      GtkObject *object;
188      gpointer user_data;
189 {
190     ResignEvent();
191     return;
192 }
193
194 void StopObservingProc(object, user_data)
195      GtkObject *object;
196      gpointer user_data;
197 {
198     StopObservingEvent();
199     return;
200 }
201
202 void StopExaminingProc(object, user_data)
203      GtkObject *object;
204      gpointer user_data;
205 {
206     StopExaminingEvent();
207     return;
208 }
209
210 void AdjuWhiteProc(object, user_data)
211      GtkObject *object;
212      gpointer user_data;
213 {
214     UserAdjudicationEvent(+1);
215     return;
216 }
217
218 void AdjuBlackProc(object, user_data)
219      GtkObject *object;
220      gpointer user_data;
221 {
222     UserAdjudicationEvent(-1);
223     return;
224 }
225
226 void AdjuDrawProc(object, user_data)
227      GtkObject *object;
228      gpointer user_data;
229 {
230     UserAdjudicationEvent(0);
231     return;
232 }
233
234 void MoveNowProc(object, user_data)
235      GtkObject *object;
236      gpointer user_data;
237 {
238     MoveNowEvent();
239     return;
240 }
241
242
243 gboolean CloseWindowProc(GtkWidget *button)
244 {
245     gtk_widget_destroy(gtk_widget_get_toplevel(button));
246     return TRUE;
247 }
248
249 void
250 ResetProc (object, user_data)
251      GtkObject *object;
252      gpointer user_data;
253 {
254   ResetGameEvent();
255   AnalysisPopDown();
256 }
257
258 void WhiteClockProc(object, user_data)
259      GtkObject *object;
260      gpointer user_data;
261 {
262     if (gameMode == EditPosition || gameMode == IcsExamining) {
263         SetWhiteToPlayEvent();
264     } else if (gameMode == IcsPlayingBlack || gameMode == MachinePlaysWhite) {
265         CallFlagEvent();
266     }
267 }
268
269 void BlackClockProc(object, user_data)
270      GtkObject *object;
271      gpointer user_data;
272 {
273     if (gameMode == EditPosition || gameMode == IcsExamining) {
274         SetBlackToPlayEvent();
275     } else if (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack) {
276         CallFlagEvent();
277     }
278 }
279
280
281 void ShowCoordsProc(object, user_data)
282      GtkObject *object;
283      gpointer user_data;
284 {
285     appData.showCoords = !appData.showCoords;
286
287     DrawPosition(True, NULL);
288 }
289
290 void ErrorPopDownProc(object, user_data)
291      GtkObject *object;
292      gpointer user_data;
293 {
294   gtk_widget_destroy(GTK_WIDGET(object));
295   ErrorPopDown();
296 }
297
298 void PauseProc(object, user_data)
299      GtkObject *object;
300      gpointer user_data;
301 {
302     // todo this toggling of the pause button doesn't seem to work?
303     // e.g. select pause from buttonbar doesn't activate menumode.pause
304   PauseEvent();
305 }
306
307
308 void LoadGameProc(object, user_data)
309      GtkObject *object;
310      gpointer user_data;
311 {
312   GtkWidget *dialog;
313   dialog = gtk_file_chooser_dialog_new (_("Load game file name?"),
314                                         GTK_WINDOW(GUI_Window),
315                                         GTK_FILE_CHOOSER_ACTION_OPEN,
316                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
317                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
318                                         NULL);
319   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
320     {
321       char *filename;
322       FILE *f;
323
324       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
325
326       //see loadgamepopup
327       f = fopen(filename, "rb");
328       if (f == NULL) {
329         DisplayError(_("Failed to open file"), errno);
330       } else {
331         /* TODO add indec */
332         (void) LoadGamePopUp(f, 0, filename);
333       }
334       g_free (filename);
335     }
336   gtk_widget_destroy (dialog);
337   ModeHighlight();
338 }
339
340
341 /*************
342  * EVENTS
343  *************/
344
345 void EventProc(window, event, data)
346      GtkWindow *window;
347      GdkEvent *event;
348      gpointer data;
349 {
350   /* todo do we still need this?
351     if (!XtIsRealized(widget))
352       return;
353   */
354
355     switch (event->type) {
356       case GDK_EXPOSE:
357         if (event->expose.count > 0) return;  /* no clipping is done */
358         DrawPosition(True, NULL);
359         break;
360       default:
361         return;
362     }
363 }
364
365
366 /*
367  * event handler for parsing user moves
368  */
369 void UserMoveProc(window, event, data)
370      GtkWindow *window;
371      GdkEvent *event;
372      gpointer data;
373 {
374     int x, y;
375     Boolean saveAnimate;
376     static int second = 0;
377
378     if (errorExitStatus != -1) return;
379
380     if (event->type == GDK_BUTTON_PRESS) ErrorPopDown();
381
382     if (promotionUp)
383       {
384         if (event->type == GDK_BUTTON_PRESS)
385           {
386             /* todo add promotionshellwidget
387                XtPopdown(promotionShell);
388                XtDestroyWidget(promotionShell); */
389             promotionUp = False;
390             ClearHighlights();
391             fromX = fromY = -1;
392           }
393         else
394           {
395             return;
396           }
397       }
398
399     x = EventToSquare( (int)event->button.x, BOARD_WIDTH  );
400     y = EventToSquare( (int)event->button.y, BOARD_HEIGHT );
401     if (!flipView && y >= 0)
402       {
403         y = BOARD_HEIGHT - 1 - y;
404       }
405     if (flipView && x >= 0)
406       {
407         x = BOARD_WIDTH - 1 - x;
408       }
409
410     if (fromX == -1)
411       {
412         if (event->type == ButtonPress)
413           {
414             /* First square */
415             if (OKToStartUserMove(x, y))
416               {
417                 fromX = x;
418                 fromY = y;
419                 second = 0;
420                 DragPieceBegin(event->button.x, event->button.y);
421                 if (appData.highlightDragging)
422                   {
423                     SetHighlights(x, y, -1, -1);
424                   }
425               }
426           }
427         return;
428       }
429
430     /* fromX != -1 */
431     if (event->type == GDK_BUTTON_PRESS && gameMode != EditPosition &&
432         x >= 0 && y >= 0) {
433         ChessSquare fromP;
434         ChessSquare toP;
435         /* Check if clicking again on the same color piece */
436         fromP = boards[currentMove][fromY][fromX];
437         toP = boards[currentMove][y][x];
438         if ((WhitePawn <= fromP && fromP <= WhiteKing &&
439              WhitePawn <= toP && toP <= WhiteKing) ||
440             (BlackPawn <= fromP && fromP <= BlackKing &&
441              BlackPawn <= toP && toP <= BlackKing)) {
442             /* Clicked again on same color piece -- changed his mind */
443             second = (x == fromX && y == fromY);
444             if (appData.highlightDragging) {
445                 SetHighlights(x, y, -1, -1);
446             } else {
447                 ClearHighlights();
448             }
449             if (OKToStartUserMove(x, y)) {
450                 fromX = x;
451                 fromY = y;
452                 DragPieceBegin(event->button.x, event->button.y);
453             }
454             return;
455         }
456     }
457
458     if (event->type == GDK_BUTTON_RELEASE &&    x == fromX && y == fromY)
459       {
460         DragPieceEnd(event->button.x, event->button.y);
461         if (appData.animateDragging)
462           {
463             /* Undo animation damage if any */
464             DrawPosition(FALSE, NULL);
465           }
466         if (second)
467           {
468             /* Second up/down in same square; just abort move */
469             second = 0;
470             fromX = fromY = -1;
471             ClearHighlights();
472             gotPremove = 0;
473             ClearPremoveHighlights();
474           }
475         else
476           {
477             /* First upclick in same square; start click-click mode */
478             SetHighlights(x, y, -1, -1);
479           }
480         return;
481       }
482
483     /* Completed move */
484     toX = x;
485     toY = y;
486     saveAnimate = appData.animate;
487
488     if (event->type == GDK_BUTTON_PRESS)
489       {
490         /* Finish clickclick move */
491         if (appData.animate || appData.highlightLastMove)
492           {
493             SetHighlights(fromX, fromY, toX, toY);
494           }
495         else
496           {
497             ClearHighlights();
498           }
499       }
500     else
501       {
502         /* Finish drag move */
503         if (appData.highlightLastMove)
504           {
505             SetHighlights(fromX, fromY, toX, toY);
506           }
507         else
508           {
509             ClearHighlights();
510           }
511         DragPieceEnd(event->button.x, event->button.y);
512         /* Don't animate move and drag both */
513         appData.animate = FALSE;
514       }
515
516     if (IsPromotion(fromX, fromY, toX, toY))
517       {
518         if (appData.alwaysPromoteToQueen)
519           {
520             UserMoveEvent(fromX, fromY, toX, toY, 'q');
521             if (!appData.highlightLastMove || gotPremove) ClearHighlights();
522             if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
523             fromX = fromY = -1;
524           }
525         else
526           {
527             SetHighlights(fromX, fromY, toX, toY);
528             PromotionPopUp();
529           }
530       }
531     else
532       {
533         UserMoveEvent(fromX, fromY, toX, toY, NULLCHAR);
534
535         if (!appData.highlightLastMove || gotPremove) ClearHighlights();
536         if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
537         fromX = fromY = -1;
538       }
539
540     appData.animate = saveAnimate;
541     if (appData.animate || appData.animateDragging) {
542         /* Undo animation damage if needed */
543         DrawPosition(FALSE, NULL);
544     }
545
546     return;
547 }
548