From d39b0224115daf24fa2608e27d0ca16c8cd4aa70 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Tue, 12 Apr 2011 19:20:46 +0200 Subject: [PATCH] Refactor move type-in code Common code between XBoard and WinBoard was moved to the back-end creating TypeInEvent() and TypeInDoneEvent() routines. --- backend.c | 49 ++++++++++++++++++++++++++++++++++++ backend.h | 2 + frontend.h | 1 + winboard/winboard.c | 54 ++++------------------------------------ xoptions.c | 68 +++++++++------------------------------------------ 5 files changed, 70 insertions(+), 104 deletions(-) diff --git a/backend.c b/backend.c index c1ea28a..b1df197 100644 --- a/backend.c +++ b/backend.c @@ -14280,6 +14280,55 @@ AskQuestionEvent(title, question, replyPrefix, which) } void +TypeInEvent(char firstChar) +{ + if ((gameMode == BeginningOfGame && !appData.icsActive) || + gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack || + gameMode == AnalyzeMode || gameMode == EditGame || + gameMode == EditPosition || gameMode == IcsExamining || + gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || + isdigit(firstChar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes + ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile || + gameMode == IcsObserving || gameMode == TwoMachinesPlay ) || + gameMode == Training) PopUpMoveDialog(firstChar); +} + +void +TypeInDoneEvent(char *move) +{ + Board board; + int n, fromX, fromY, toX, toY; + char promoChar; + ChessMove moveType; + + // [HGM] FENedit + if(gameMode == EditPosition && ParseFEN(board, &n, move) ) { + EditPositionPasteFEN(move); + return; + } + // [HGM] movenum: allow move number to be typed in any mode + if(sscanf(move, "%d", &n) == 1 && n != 0 ) { + ToNrEvent(2*n-1); + return; + } + + if (gameMode != EditGame && currentMove != forwardMostMove && + gameMode != Training) { + DisplayMoveError(_("Displayed move is not current")); + } else { + int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, + &moveType, &fromX, &fromY, &toX, &toY, &promoChar); + if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized + if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, + &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) { + UserMoveEvent(fromX, fromY, toX, toY, promoChar); + } else { + DisplayMoveError(_("Could not parse move")); + } + } +} + +void DisplayMove(moveNumber) int moveNumber; { diff --git a/backend.h b/backend.h index 74d2a64..32e15dd 100644 --- a/backend.h +++ b/backend.h @@ -206,6 +206,8 @@ void ExitAnalyzeMode P((void)); void AnalyzeModeEvent P((void)); void AnalyzeFileEvent P((void)); void MatchEvent P((int mode)); +void TypeInEvent P((char first)); +void TypeInDoneEvent P((char *move)); void InitPosition P((int redraw)); void NewSettingEvent P((int option, int *feature, char *command, int value)); int WaitForSecond P((DelayedEventCallback x)); diff --git a/frontend.h b/frontend.h index 50906f0..1eba120 100644 --- a/frontend.h +++ b/frontend.h @@ -95,6 +95,7 @@ void DrawSeekAxis P(( int x, int y, int xTo, int yTo )); void DrawSeekBackground P(( int left, int top, int right, int bottom )); void DrawSeekText P((char *buf, int x, int y)); void DrawSeekDot P((int x, int y, int color)); +void PopUpMoveDialog P((char first)); void RingBell P((void)); void PlayIcsWinSound P((void)); diff --git a/winboard/winboard.c b/winboard/winboard.c index ea7c7a7..07e4bd2 100644 --- a/winboard/winboard.c +++ b/winboard/winboard.c @@ -653,7 +653,6 @@ LRESULT CALLBACK StartupDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); VOID APIENTRY MenuPopup(HWND hwnd, POINT pt, HMENU hmenu, UINT def); void ParseIcsTextMenu(char *icsTextMenuString); -VOID PopUpMoveDialog(char firstchar); VOID PopUpNameDialog(char firstchar); VOID UpdateSampleText(HWND hDlg, int id, MyColorizeAttribs *mca); @@ -4280,7 +4279,7 @@ ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) SendMessage(h, WM_CHAR, wParam, lParam); return TRUE; } else if (isalpha((char)wParam) || isdigit((char)wParam)){ - PopUpMoveDialog((char)wParam); + TypeInEvent((char)wParam); } break; } @@ -4589,7 +4588,7 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) SendMessage(h, message, wParam, lParam); } else if(lParam != KF_REPEAT) { if (isalpha((char)wParam) || isdigit((char)wParam)) { - PopUpMoveDialog((char)wParam); + TypeInEvent((char)wParam); } else if((char)wParam == 003) CopyGameToClipboard(); else if((char)wParam == 026) PasteGameOrFENFromClipboard(); } @@ -5013,7 +5012,7 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) break; case IDM_TypeInMove: - PopUpMoveDialog('\000'); + TypeInEvent('\000'); break; case IDM_TypeInName: @@ -6404,9 +6403,6 @@ TypeInMoveDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { char move[MSG_SIZ]; HWND hInput; - ChessMove moveType; - int fromX, fromY, toX, toY; - char promoChar; switch (message) { case WM_INITDIALOG: @@ -6425,36 +6421,8 @@ TypeInMoveDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) case IDOK: shiftKey = GetKeyState(VK_SHIFT) < 0; // [HGM] remember last shift status - GetDlgItemText(hDlg, OPT_Move, move, sizeof(move)); - { int n; Board board; - // [HGM] FENedit - if(gameMode == EditPosition && ParseFEN(board, &n, move) ) { - EditPositionPasteFEN(move); - EndDialog(hDlg, TRUE); - return TRUE; - } - // [HGM] movenum: allow move number to be typed in any mode - if(sscanf(move, "%d", &n) == 1 && n != 0 ) { - ToNrEvent(2*n-1); - EndDialog(hDlg, TRUE); - return TRUE; - } - } - if (gameMode != EditGame && currentMove != forwardMostMove && - gameMode != Training) { - DisplayMoveError(_("Displayed move is not current")); - } else { -// GetDlgItemText(hDlg, OPT_Move, move, sizeof(move)); // moved upstream - int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, - &moveType, &fromX, &fromY, &toX, &toY, &promoChar); - if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized - if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, - &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) { - UserMoveEvent(fromX, fromY, toX, toY, promoChar); - } else { - DisplayMoveError(_("Could not parse move")); - } - } + GetDlgItemText(hDlg, OPT_Move, move, sizeof(move)); + TypeInDoneEvent(move); EndDialog(hDlg, TRUE); return TRUE; case IDCANCEL: @@ -6472,21 +6440,11 @@ VOID PopUpMoveDialog(char firstchar) { FARPROC lpProc; - - if ((gameMode == BeginningOfGame && !appData.icsActive) || - gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack || - gameMode == AnalyzeMode || gameMode == EditGame || - gameMode == EditPosition || gameMode == IcsExamining || - gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || - isdigit(firstchar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes - ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile || - gameMode == IcsObserving || gameMode == TwoMachinesPlay ) || - gameMode == Training) { + lpProc = MakeProcInstance((FARPROC)TypeInMoveDialog, hInst); DialogBoxParam(hInst, MAKEINTRESOURCE(DLG_TypeInMove), hwndMain, (DLGPROC)lpProc, (LPARAM)firstchar); FreeProcInstance(lpProc); - } } /*---------------------------------------------------------------------------*\ diff --git a/xoptions.c b/xoptions.c index 6fe3822..0689355 100644 --- a/xoptions.c +++ b/xoptions.c @@ -1926,70 +1926,27 @@ void TypeInProc(w, event, prms, nprms) String *prms; Cardinal *nprms; { - Widget edit; - int j, fromX, fromY, toX, toY; - Arg args[16]; + Arg args[2]; String val; - char *move, promoChar; - ChessMove moveType; - if(prms[0][0] == '0') PopDown(0); // escape hit - edit = boxOptions[0].handle; - j = 0; - XtSetArg(args[j], XtNstring, &val); j++; - XtGetValues(edit, args, j); - move = val; - { int n; Board board; - // [HGM] FENedit - if(gameMode == EditPosition && ParseFEN(board, &n, move) ) { - EditPositionPasteFEN(move); - PopDown(0); - return; - } - // [HGM] movenum: allow move number to be typed in any mode - if(sscanf(move, "%d", &n) == 1 && n != 0 ) { - ToNrEvent(2*n-1); - PopDown(0); - return; - } - } - if (gameMode != EditGame && currentMove != forwardMostMove && - gameMode != Training) { - DisplayMoveError(_("Displayed move is not current")); - } else { - int ok = ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, - &moveType, &fromX, &fromY, &toX, &toY, &promoChar); - if(!ok && move[0] >= 'a') { move[0] += 'A' - 'a'; ok = 2; } // [HGM] try also capitalized - if (ok==1 || ok && ParseOneMove(move, gameMode == EditPosition ? blackPlaysFirst : currentMove, - &moveType, &fromX, &fromY, &toX, &toY, &promoChar)) { - UserMoveEvent(fromX, fromY, toX, toY, promoChar); - } else { - DisplayMoveError(_("Could not parse move")); - } - } - PopDown(0); + if(prms[0][0] == '1') { + XtSetArg(args[0], XtNstring, &val); + XtGetValues(boxOptions[0].handle, args, 1); + TypeInDoneEvent((char*)val); + } + PopDown(0); } char moveTypeInTranslations[] = "Return: TypeInProc(1) \n" "Escape: TypeInProc(0) \n"; -void MoveTypeInPopup(char firstchar) +void PopUpMoveDialog(char firstchar) { static char buf[2]; - if ((gameMode == BeginningOfGame && !appData.icsActive) || - gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack || - gameMode == AnalyzeMode || gameMode == EditGame || - gameMode == EditPosition || gameMode == IcsExamining || - gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || - isdigit(firstchar) && // [HGM] movenum: allow typing in of move nr in 'passive' modes - ( gameMode == AnalyzeFile || gameMode == PlayFromGameFile || - gameMode == IcsObserving || gameMode == TwoMachinesPlay ) || - gameMode == Training) { - buf[0]= firstchar; icsText = buf; - if(GenericPopUp(boxOptions, _("Type a move"), 0)) - XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(moveTypeInTranslations)); - } + buf[0] = firstchar; icsText = buf; + if(GenericPopUp(boxOptions, _("Type a move"), 0)) + XtOverrideTranslations(boxOptions[0].handle, XtParseTranslationTable(moveTypeInTranslations)); } void MoveTypeInProc(Widget widget, caddr_t unused, XEvent *event) @@ -2001,9 +1958,8 @@ void MoveTypeInProc(Widget widget, caddr_t unused, XEvent *event) XQueryKeymap(xDisplay,keys); metaL = XKeysymToKeycode(xDisplay, XK_Meta_L); metaR = XKeysymToKeycode(xDisplay, XK_Meta_R); -//{int i; for(i=0;i<32;i++)printf("%02x",keys[i]);printf("\n");} if ( n == 1 && *buf > 32 && !(keys[metaL>>3]&1<<(metaL&7)) && !(keys[metaR>>3]&1<<(metaR&7))) // printable, no alt - MoveTypeInPopup(*buf); + PopUpMoveDialog(*buf); } -- 1.7.0.4