converted more callbacks from mode menu
[xboard.git] / callback.c
index d2e42f3..cd83153 100644 (file)
@@ -14,6 +14,7 @@
 
 extern GtkWidget  *about;
 extern GtkWidget  *GUI_Window;
+extern GtkWidget  *GUI_Aspect;
 extern GtkWidget  *GUI_Menubar;
 extern GtkWidget  *GUI_Timer;
 extern GtkWidget  *GUI_Buttonbar;
@@ -28,6 +29,11 @@ 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;
@@ -38,20 +44,20 @@ ExposeProc(object, user_data)
   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;
@@ -59,16 +65,17 @@ ExposeProc(object, user_data)
       totalh += w.height;
       gtk_widget_size_request(GTK_WIDGET(GUI_Buttonbar),   &w);
       totalh += w.height;
-      
-      ratio  = (totalh+boardHeight)/(boardWidth) ;
-            
+
+      ratio  = ((float)totalh+boardHeight)/((float)boardWidth) ;
+
       gtk_widget_set_size_request(GTK_WIDGET(GUI_Board),
                                  boardWidth,boardHeight);
-      
-      GUI_SetAspectRatio(ratio);
+
+      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 */
 }
 
@@ -168,6 +175,32 @@ void AboutProc (object, user_data)
 
 /* End Help Menu */
 
+/* Mode Menu */
+
+void MachineWhiteProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    MachineWhiteEvent();
+    return;
+}
+
+void MachineBlackProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    MachineBlackEvent();
+    return;
+}
+
+void TwoMachinesProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    TwoMachinesEvent();
+    return;
+}
+
 void IcsClientProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
@@ -176,7 +209,114 @@ void IcsClientProc(object, user_data)
     return;
 }
 
-void LoadNextGameProc(object, user_data)
+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;
+}
+
+void 
+AnalyzeModeProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+    char buf[MSG_SIZ];
+
+    if (!first.analysisSupport) 
+      {
+       snprintf(buf, sizeof(buf), _("%s does not support analysis"), first.tidy);
+       DisplayError(buf, 0);
+       return;
+      }
+    /* [DM] icsEngineAnalyze [HGM] This is horrible code; reverse the gameMode and isEngineAnalyze tests! */
+    if (appData.icsActive) 
+      {
+        if (gameMode != IcsObserving) 
+         {
+           sprintf(buf,_("You are not observing a game"));
+           DisplayError(buf, 0);
+            /* secure check */
+            if (appData.icsEngineAnalyze) 
+             {
+               if (appData.debugMode)
+                 fprintf(debugFP, _("Found unexpected active ICS engine analyze \n"));
+                ExitAnalyzeMode();
+                ModeHighlight();
+             }
+            return;
+         }
+        /* if enable, use want disable icsEngineAnalyze */
+        if (appData.icsEngineAnalyze) 
+         {
+           ExitAnalyzeMode();
+           ModeHighlight();
+           return;
+         }
+        appData.icsEngineAnalyze = TRUE;
+        if (appData.debugMode)
+         fprintf(debugFP, _("ICS engine analyze starting... \n"));
+      }
+    if (!appData.showThinking)
+      ShowThinkingProc(NULL,NULL);
+    
+    AnalyzeModeEvent();
+    return;
+}
+
+void 
+EditGameProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  EditGameEvent();
+  return;
+}
+
+void 
+EditPositionProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  EditPositionEvent();
+  return;
+}
+
+void 
+TrainingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  TrainingEvent();
+  return;
+}
+
+
+
+/* End Mode Menu */
+
+/*
+ * File menu
+ */
+
+void
+LoadNextGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -184,7 +324,8 @@ void LoadNextGameProc(object, user_data)
     return;
 }
 
-void LoadPrevGameProc(object, user_data)
+void
+LoadPrevGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -192,7 +333,8 @@ void LoadPrevGameProc(object, user_data)
     return;
 }
 
-void ReloadGameProc(object, user_data)
+void
+ReloadGameProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -200,30 +342,72 @@ void ReloadGameProc(object, user_data)
     return;
 }
 
-void MachineWhiteProc(object, user_data)
+void
+LoadNextPositionProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
-    MachineWhiteEvent();
+    ReloadPosition(1);
     return;
 }
 
-void MachineBlackProc(object, user_data)
+void
+LoadPrevPositionProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
-    MachineBlackEvent();
+    ReloadPosition(-1);
     return;
 }
 
-void TwoMachinesProc(object, user_data)
+void
+ReloadPositionProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
-    TwoMachinesEvent();
+    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;
+}
+
+
+/* End File Menu */
+
 void AcceptProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
@@ -393,17 +577,46 @@ void RetractMoveProc(object, user_data)
 }
 
 /* Option Menu */
-void ShowThinkingProc(object, user_data)
+void 
+AutocommProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
-    appData.showThinking = !appData.showThinking; 
+    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)
+void 
+HideThinkingProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -413,7 +626,8 @@ void HideThinkingProc(object, user_data)
     return;
 }
 
-void FlipViewProc(object, user_data)
+void 
+FlipViewProc(object, user_data)
      GtkObject *object;
      gpointer user_data;
 {
@@ -422,6 +636,217 @@ void FlipViewProc(object, user_data)
     return;
 }
 
+void 
+AlwaysQueenProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.alwaysPromoteToQueen = !appData.alwaysPromoteToQueen;
+  return;
+}
+
+void 
+AnimateDraggingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.animateDragging = !appData.animateDragging;
+
+  if (appData.animateDragging) 
+    {
+      // TODO convert to gtk
+      //      CreateAnimVars();
+    };
+
+  return;
+}
+
+void 
+AnimateMovingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.animate = !appData.animate;
+  
+  if (appData.animate) 
+    {
+      // TODO convert to gtk
+      //      CreateAnimVars();
+    };
+
+  return;
+}
+
+void 
+AutobsProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.autoObserve = !appData.autoObserve;
+  return;
+}
+
+void 
+AutoraiseProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.autoRaiseBoard = !appData.autoRaiseBoard;
+  return;
+}
+
+void 
+AutosaveProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.autoSaveGames = !appData.autoSaveGames;
+  return;
+}
+
+void 
+BlindfoldProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.blindfold = !appData.blindfold;
+  DrawPosition(True, NULL);
+  return;
+}
+
+void 
+TestLegalityProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.testLegality = !appData.testLegality;
+  return;
+}
+
+void 
+FlashMovesProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  if (appData.flashCount == 0) 
+    {
+      appData.flashCount = 3;
+    }
+  else 
+    {
+      appData.flashCount = -appData.flashCount;
+    };
+  
+    // TODO: check if this is working correct*/
+    return;
+}
+
+#if HIGHDRAG
+void 
+HighlightDraggingProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  /* TODO: connect to option menu */
+  appData.highlightDragging = !appData.highlightDragging;
+  return;
+}
+#endif
+
+void 
+HighlightLastMoveProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.highlightLastMove = !appData.highlightLastMove;
+  return;
+}
+
+void 
+IcsAlarmProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.icsAlarm = !appData.icsAlarm;
+  return;
+}
+
+void 
+MoveSoundProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.ringBellAfterMoves = !appData.ringBellAfterMoves;
+  return;
+}
+
+void 
+OldSaveStyleProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.oldSaveStyle = !appData.oldSaveStyle;
+  return;
+}
+
+void 
+PeriodicUpdatesProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  PeriodicUpdatesEvent(!appData.periodicUpdates);
+  return;
+}
+
+void 
+PremoveProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.premove = !appData.premove;
+  return;
+}
+
+void 
+QuietPlayProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.quietPlay = !appData.quietPlay;
+  return;
+}
+
+
+void 
+PonderNextMoveProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  PonderNextMoveEvent(!appData.ponderNextMove);
+  return;
+}
+
+void 
+PopupExitMessageProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.popupExitMessage = !appData.popupExitMessage;
+  return;
+}
+
+void 
+PopupMoveErrorsProc(object, user_data)
+     GtkObject *object;
+     gpointer user_data;
+{
+  appData.popupMoveErrors = !appData.popupMoveErrors;
+  return;
+}
+
+
+
+/* end option menu */
 
 gboolean CloseWindowProc(GtkWidget *button)
 {
@@ -435,7 +860,7 @@ ResetProc (object, user_data)
      gpointer user_data;
 {
   ResetGameEvent();
-  AnalysisPopDown();
+  return;
 }
 
 void WhiteClockProc(object, user_data)
@@ -492,37 +917,7 @@ 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;
 }
 
@@ -560,177 +955,25 @@ void UserMoveProc(window, event, data)
      GdkEvent *event;
      gpointer data;
 {
-    int x, y;
-    Boolean saveAnimate;
-    static int second = 0;
-
     if (errorExitStatus != -1) return;
 
-    if (event->type == GDK_BUTTON_PRESS) ErrorPopDown();
-
-    if (promotionUp)
+    if (promotionUp) 
       {
-       if (event->type == GDK_BUTTON_PRESS)
+       if (event->type == GDK_BUTTON_PRESS) 
          {
-           /* todo add promotionshellwidget
-              XtPopdown(promotionShell);
-              XtDestroyWidget(promotionShell); */
            promotionUp = False;
            ClearHighlights();
            fromX = fromY = -1;
          }
-       else
+       else 
          {
            return;
          }
       }
-
-    x = EventToSquare( (int)event->button.x, BOARD_WIDTH  );
-    y = EventToSquare( (int)event->button.y, BOARD_HEIGHT );
-    if (!flipView && y >= 0)
-      {
-       y = BOARD_HEIGHT - 1 - y;
-      }
-    if (flipView && x >= 0)
-      {
-       x = BOARD_WIDTH - 1 - x;
-      }
-
-    if (fromX == -1)
-      {
-       if (event->type == ButtonPress)
-         {
-           /* First square */
-           if (OKToStartUserMove(x, y))
-             {
-               fromX = x;
-               fromY = y;
-               second = 0;
-               DragPieceBegin(event->button.x, event->button.y);
-               if (appData.highlightDragging)
-                 {
-                   SetHighlights(x, y, -1, -1);
-                 }
-             }
-         }
-       return;
-      }
-
-    /* fromX != -1 */
-    if (event->type == GDK_BUTTON_PRESS && gameMode != EditPosition &&
-       x >= 0 && y >= 0) {
-       ChessSquare fromP;
-       ChessSquare toP;
-       /* Check if clicking again on the same color piece */
-       fromP = boards[currentMove][fromY][fromX];
-       toP = boards[currentMove][y][x];
-       if ((WhitePawn <= fromP && fromP <= WhiteKing &&
-            WhitePawn <= toP && toP <= WhiteKing) ||
-           (BlackPawn <= fromP && fromP <= BlackKing &&
-            BlackPawn <= toP && toP <= BlackKing)) {
-           /* Clicked again on same color piece -- changed his mind */
-           second = (x == fromX && y == fromY);
-           if (appData.highlightDragging) {
-               SetHighlights(x, y, -1, -1);
-           } else {
-               ClearHighlights();
-           }
-           if (OKToStartUserMove(x, y)) {
-               fromX = x;
-               fromY = y;
-               DragPieceBegin(event->button.x, event->button.y);
-           }
-           return;
-       }
-    }
-
-    if (event->type == GDK_BUTTON_RELEASE &&   x == fromX && y == fromY)
-      {
-       DragPieceEnd(event->button.x, event->button.y);
-       if (appData.animateDragging)
-         {
-           /* Undo animation damage if any */
-           DrawPosition(FALSE, NULL);
-         }
-       if (second)
-         {
-           /* Second up/down in same square; just abort move */
-           second = 0;
-           fromX = fromY = -1;
-           ClearHighlights();
-           gotPremove = 0;
-           ClearPremoveHighlights();
-         }
-       else
-         {
-           /* First upclick in same square; start click-click mode */
-           SetHighlights(x, y, -1, -1);
-         }
-       return;
-      }
-
-    /* Completed move */
-    toX = x;
-    toY = y;
-    saveAnimate = appData.animate;
-
-    if (event->type == GDK_BUTTON_PRESS)
-      {
-       /* Finish clickclick move */
-       if (appData.animate || appData.highlightLastMove)
-         {
-           SetHighlights(fromX, fromY, toX, toY);
-         }
-       else
-         {
-           ClearHighlights();
-         }
-      }
-    else
-      {
-       /* Finish drag move */
-       if (appData.highlightLastMove)
-         {
-           SetHighlights(fromX, fromY, toX, toY);
-         }
-       else
-         {
-           ClearHighlights();
-         }
-       DragPieceEnd(event->button.x, event->button.y);
-       /* Don't animate move and drag both */
-       appData.animate = FALSE;
-      }
-
-    if (IsPromotion(fromX, fromY, toX, toY))
-      {
-       if (appData.alwaysPromoteToQueen)
-         {
-           UserMoveEvent(fromX, fromY, toX, toY, 'q');
-           if (!appData.highlightLastMove || gotPremove) ClearHighlights();
-           if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
-           fromX = fromY = -1;
-         }
-       else
-         {
-           SetHighlights(fromX, fromY, toX, toY);
-           PromotionPopUp();
-         }
-      }
-    else
-      {
-       UserMoveEvent(fromX, fromY, toX, toY, NULLCHAR);
-
-       if (!appData.highlightLastMove || gotPremove) ClearHighlights();
-       if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
-       fromX = fromY = -1;
-      }
-
-    appData.animate = saveAnimate;
-    if (appData.animate || appData.animateDragging) {
-       /* Undo animation damage if needed */
-       DrawPosition(FALSE, NULL);
-    }
+    
+    // [HGM] mouse: the rest of the mouse handler is moved to the backend, and called here
+    if(event->type == GDK_BUTTON_PRESS)   LeftClick(Press,   (int)event->button.x, (int)event->button.y);
+    if(event->type == GDK_BUTTON_RELEASE) LeftClick(Release, (int)event->button.x, (int)event->button.y);
 
     return;
 }
@@ -740,14 +983,14 @@ void GetMoveListProc(object, user_data)
      gpointer user_data;
 {
   appData.getMoveList = !appData.getMoveList;
-  
-  if (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;
 }