moved several game/position opening/saving functions to gtk
[xboard.git] / callback.c
index 6fcd469..652db01 100644 (file)
@@ -1,5 +1,6 @@
 #include <gtk/gtk.h>
 #include "common.h"
+#include "xboard.h"
 #include <errno.h>
 #include "backend.h"
 
 # define N_(s)  s
 #endif
 
-
 extern GtkWidget  *about;
 extern GtkWidget  *GUI_Window;
+extern GtkWidget  *GUI_Aspect;
+extern GtkWidget  *GUI_Menubar;
+extern GtkWidget  *GUI_Timer;
+extern GtkWidget  *GUI_Buttonbar;
+extern GtkWidget  *GUI_Board;
 
 extern char *programVersion;
 extern int errorExitStatus;
@@ -22,6 +27,57 @@ extern int fromX;
 extern int fromY;
 extern int toX;
 extern int toY;
+extern int squareSize,lineGap;
+
+extern int LoadGamePopUp P((FILE *f, int gameNumber, char *title));
+extern int LoadPosition P((FILE *f, int gameNumber, char *title));
+extern int SaveGame P((FILE *f, int gameNumber, char *title));
+extern int SavePosition P((FILE *f, int gameNumber, char *title));
+
+gboolean
+ExposeProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  /* do resizing to a fixed aspect ratio */
+  GtkRequisition w;
+  int totalh=0,nw,nh;
+  float ratio;
+  int boardWidth,boardHeight,old,new;
+
+  nw=GTK_WIDGET(object)->allocation.width;
+  nh=GTK_WIDGET(object)->allocation.height;
+
+  old=squareSize;
+  squareSize  = nw/(BOARD_WIDTH*1.05+0.05);
+
+  if(old!=squareSize)
+    {
+      lineGap = squareSize*0.05;
+
+      boardWidth  = lineGap + BOARD_WIDTH  * (squareSize + lineGap);
+      boardHeight = lineGap + BOARD_HEIGHT * (squareSize + lineGap);
+
+      /* get the height of the menus, etc. and calculate the aspect ratio */
+      gtk_widget_size_request(GTK_WIDGET(GUI_Menubar),   &w);
+      totalh += w.height;
+      gtk_widget_size_request(GTK_WIDGET(GUI_Timer),   &w);
+      totalh += w.height;
+      gtk_widget_size_request(GTK_WIDGET(GUI_Buttonbar),   &w);
+      totalh += w.height;
+
+      ratio  = ((float)totalh+boardHeight)/((float)boardWidth) ;
+
+      gtk_widget_set_size_request(GTK_WIDGET(GUI_Board),
+                                 boardWidth,boardHeight);
+
+      gtk_aspect_frame_set (GTK_ASPECT_FRAME(GUI_Aspect),0,0,ratio,TRUE);
+
+      /* recreate pieces with new size... TODO: keep svg in memory and just recreate pixmap instead of reloading files */
+      CreatePieces();
+    }
+  return FALSE; /* return false, so that other expose events are called too */
+}
 
 void
 QuitProc (object, user_data)
@@ -32,6 +88,44 @@ QuitProc (object, user_data)
   ExitEvent(0);
 }
 
+/* Help Menu */
+void InfoProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    char buf[MSG_SIZ];
+    snprintf(buf, sizeof(buf), "xterm -e info --directory %s --directory . -f %s &",
+           INFODIR, INFOFILE);
+    system(buf);
+    return;
+}
+
+void ManProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    char buf[MSG_SIZ];
+    snprintf(buf, sizeof(buf), "xterm -e man xboard &");
+    system(buf);
+    return;
+}
+
+void HintProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    HintEvent();
+    return;
+}
+
+void BookProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    BookEvent();
+    return;
+}
+
 void AboutProc (object, user_data)
      GtkObject *object;
      gpointer user_data;
@@ -79,7 +173,22 @@ void AboutProc (object, user_data)
   gtk_widget_destroy(about);
 }
 
-void LoadNextGameProc(object, user_data)
+/* End Help Menu */
+
+void IcsClientProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    IcsClientEvent();
+    return;
+}
+
+/*
+ * File menu
+ */
+
+void
+LoadNextGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -87,7 +196,8 @@ void LoadNextGameProc(object, user_data)
     return;
 }
 
-void LoadPrevGameProc(object, user_data)
+void
+LoadPrevGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -95,7 +205,8 @@ void LoadPrevGameProc(object, user_data)
     return;
 }
 
-void ReloadGameProc(object, user_data)
+void
+ReloadGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -103,6 +214,95 @@ void ReloadGameProc(object, user_data)
     return;
 }
 
+void
+LoadNextPositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ReloadPosition(1);
+    return;
+}
+
+void
+LoadPrevPositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ReloadPosition(-1);
+    return;
+}
+
+void
+ReloadPositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ReloadPosition(0);
+    return;
+}
+
+void
+LoadPositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
+    {
+      Reset(FALSE, TRUE);
+    };
+
+  FileNamePopUp(_("Load position file name?"), "", LoadPosition, "rb");
+  return;
+}
+
+void
+SaveGameProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  FileNamePopUp(_("Save game file name?"),
+               DefaultFileName(appData.oldSaveStyle ? "game" : "pgn"),
+               SaveGame, "a");
+  return;
+}
+
+void
+SavePositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  FileNamePopUp(_("Save position file name?"),
+               DefaultFileName(appData.oldSaveStyle ? "pos" : "fen"),
+               SavePosition, "a");
+  return;
+}
+
+void
+AnalyzeFileProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  if (!first.analysisSupport)
+    {
+      char buf[MSG_SIZ];
+      snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
+      DisplayError(buf, 0);
+      return;
+    };
+  Reset(FALSE, TRUE);
+
+  if (!appData.showThinking)
+    ShowThinkingProc(NULL,NULL);
+
+  AnalyzeFileEvent();
+  FileNamePopUp(_("File to analyze"), "", LoadGamePopUp, "rb");
+  AnalysisPeriodicEvent(1);
+  return;
+}
+
+
+/* End File Menu */
+
 void MachineWhiteProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
@@ -231,6 +431,54 @@ void AdjuDrawProc(object, user_data)
     return;
 }
 
+void BackwardProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    BackwardEvent();
+    return;
+}
+
+void ForwardProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ForwardEvent();
+    return;
+}
+
+void ToStartProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ToStartEvent();
+    return;
+}
+
+void ToEndProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    ToEndEvent();
+    return;
+}
+
+void RevertProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    RevertEvent();
+    return;
+}
+
+void TruncateGameProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    TruncateGameEvent();
+    return;
+}
+
 void MoveNowProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
@@ -239,6 +487,68 @@ void MoveNowProc(object, user_data)
     return;
 }
 
+void RetractMoveProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    RetractMoveEvent();
+    return;
+}
+
+/* Option Menu */
+void AutocommProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    appData.autoComment = !appData.autoComment;
+    return;
+}
+
+void AutoflagProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    appData.autoCallFlag = !appData.autoCallFlag;
+    return;
+}
+
+void AutoflipProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    appData.autoFlipView = !appData.autoFlipView;
+    return;
+}
+
+void ShowThinkingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    appData.showThinking = !appData.showThinking;
+    ShowThinkingEvent();
+
+    return;
+}
+
+void HideThinkingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    appData.hideThinkingFromHuman = !appData.hideThinkingFromHuman;
+    ShowThinkingEvent();
+
+    return;
+}
+
+void FlipViewProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    flipView = !flipView;
+    DrawPosition(True, NULL);
+    return;
+}
+
 
 gboolean CloseWindowProc(GtkWidget *button)
 {
@@ -252,7 +562,7 @@ ResetProc (object, user_data)
      gpointer user_data;
 {
   ResetGameEvent();
-  AnalysisPopDown();
+  EngineOutputPopDown();
 }
 
 void WhiteClockProc(object, user_data)
@@ -309,32 +619,8 @@ void LoadGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
-  GtkWidget *dialog;
-  dialog = gtk_file_chooser_dialog_new (_("Load game file name?"),
-                                       GTK_WINDOW(GUI_Window),
-                                       GTK_FILE_CHOOSER_ACTION_OPEN,
-                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-                                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-                                       NULL);
-  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
-    {
-      char *filename;
-      FILE *f;
-
-      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
-
-      //see loadgamepopup
-      f = fopen(filename, "rb");
-      if (f == NULL) {
-       DisplayError(_("Failed to open file"), errno);
-      } else {
-       /* TODO add indec */
-       (void) LoadGamePopUp(f, 0, filename);
-      }
-      g_free (filename);
-    }
-  gtk_widget_destroy (dialog);
-  ModeHighlight();
+  FileNamePopUp(_("Load game file name?"),"",LoadGamePopUp,"rb");
+  return;
 }
 
 
@@ -546,3 +832,19 @@ void UserMoveProc(window, event, data)
     return;
 }
 
+void GetMoveListProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.getMoveList = !appData.getMoveList;
+
+  if (appData.getMoveList)
+    {
+      GetMoveListEvent();
+    }
+
+  // gets set automatically? if we set it with set_active we end up in an endless loop switching between 0 and 1
+  //  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (object),(gboolean) appData.getMoveList );
+
+  return;
+}