Subversion Repositories Games.Chess Giants

Rev

Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // dialog_endgame.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_GOTOMOVE
  8.  
  9.  
  10. // global variables used in this module only
  11. static int wanted_move = 0;
  12.  
  13.  
  14. // prototypes of local functions
  15. static void StartThread_ThisDialog (void *thread_parms);
  16. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  17.  
  18.  
  19. void DialogBox_GoToMove (void)
  20. {
  21.    // helper function to fire up the modeless dialog box
  22.  
  23.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
  24.  
  25.    return; // return as soon as the thread is fired up
  26. }
  27.  
  28.  
  29. void DialogBox_GoToMove_Validated (void)
  30. {
  31.    // callback function called by the main game thread when the dialog box is validated
  32.  
  33.    // remember this callback is no longer to be called
  34.    is_dialogbox_gotomove_validated = false;
  35.  
  36.    // enter view mode and set the viewed move to the one we want (with sanity checks)
  37.    if (wanted_move < 0)
  38.       the_board.viewed_move = 0; // clamp min to start state
  39.    else if ((the_board.move_count > 1) && (wanted_move > the_board.move_count - 1))
  40.       the_board.viewed_move = the_board.move_count - 1; // clamp max to last move
  41.    else
  42.       the_board.viewed_move = wanted_move; // else any move in between
  43.  
  44.    the_board.reevaluate = true; // evaluate board again
  45.    return; // finished, invitation is sent
  46. }
  47.  
  48.  
  49. static void StartThread_ThisDialog (void *thread_parms)
  50. {
  51.    // this function runs in a separate thread, for that's the only way (seemingly)
  52.    // to implement a non-modal message box using the Common Controls library.
  53.  
  54.    // display the dialog box
  55.    if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
  56.       is_dialogbox_gotomove_validated = true;
  57.  
  58.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  59.    return; // _endthread() implied
  60. }
  61.  
  62.  
  63. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  64. {
  65.    // message handler for the dialog box
  66.  
  67.    unsigned short wParam_hiword;
  68.    unsigned short wParam_loword;
  69.  
  70.    // filter out the commonly used message values
  71.    wParam_hiword = HIWORD (wParam);
  72.    wParam_loword = LOWORD (wParam);
  73.  
  74.    // have we just fired up this window ?
  75.    if (message == WM_INITDIALOG)
  76.    {
  77.       // center the window
  78.       CenterWindow (hWnd, hMainWnd);
  79.  
  80.       // set dialog icons (small one for title bar & big one for task manager)
  81.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  82.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  83.  
  84.       // set window title and control texts
  85.       SetWindowText (hWnd, LOCALIZE (L"GoToMove_Title"));
  86.       SetDlgItemText (hWnd, BUTTON_OK, LOCALIZE (L"Button_OK"));
  87.       SetDlgItemText (hWnd, STATICTEXT_GOTOMOVE, LOCALIZE (L"GoToMove"));
  88.       SetDlgItemInt (hWnd, EDITBOX_GOTOMOVE, MOVEINDEX_TO_PGNMOVENUMBER (the_board.viewed_move), false);
  89.       SetDlgItemText (hWnd, RADIOBUTTON_GOTOMOVE_WHITE, LOCALIZE (L"Games_White"));
  90.       SetDlgItemText (hWnd, RADIOBUTTON_GOTOMOVE_BLACK, LOCALIZE (L"Games_Black"));
  91.       SetDlgItemText (hWnd, STATICTEXT_GOTOMOVE_STATUSBAR, LOCALIZE (L"GoToMove_StatusBar"));
  92.  
  93.       // initialize the radio buttons
  94.       Button_SetCheck (GetDlgItem (hWnd, RADIOBUTTON_GOTOMOVE_WHITE), (1 - the_board.viewed_move % 2 == COLOR_WHITE ? BST_CHECKED : BST_UNCHECKED));
  95.       Button_SetCheck (GetDlgItem (hWnd, RADIOBUTTON_GOTOMOVE_BLACK), (1 - the_board.viewed_move % 2 == COLOR_BLACK ? BST_CHECKED : BST_UNCHECKED));
  96.  
  97.       // convert the status bar message to a hyperlink
  98.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_GOTOMOVE_STATUSBAR));
  99.    }
  100.  
  101.    // else did we click the close button on the title bar ?
  102.    else if (message == WM_CLOSE)
  103.       EndDialog (hWnd, 0); // close the dialog box
  104.  
  105.    // else did we take action on one of the controls ?
  106.    else if (message == WM_COMMAND)
  107.    {
  108.       // was it the OK button ?
  109.       if (wParam_loword == BUTTON_OK)
  110.       {
  111.          // translate the wanted move number into a move array index
  112.          wanted_move = PGNMOVENUMBER_TO_MOVEINDEX (GetDlgItemInt (hWnd, EDITBOX_GOTOMOVE, NULL, false),
  113.                                                    (Button_GetCheck (GetDlgItem (hWnd, RADIOBUTTON_GOTOMOVE_BLACK)) == BST_CHECKED ? COLOR_BLACK : COLOR_WHITE));
  114.          EndDialog (hWnd, 1); // close the dialog box and return a success value
  115.       }
  116.  
  117.       // else did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  118.       else if (wParam_loword == IDCANCEL)
  119.          EndDialog (hWnd, 0); // close the dialog box
  120.  
  121.       // else is it a radio button ?
  122.       else if (wParam_loword == RADIOBUTTON_GOTOMOVE_WHITE)
  123.          Button_SetCheck (GetDlgItem (hWnd, RADIOBUTTON_GOTOMOVE_BLACK), BST_UNCHECKED);
  124.       else if (wParam_loword == RADIOBUTTON_GOTOMOVE_BLACK)
  125.          Button_SetCheck (GetDlgItem (hWnd, RADIOBUTTON_GOTOMOVE_WHITE), BST_UNCHECKED);
  126.  
  127.       // else was it the status bar hyperlink ?
  128.       else if (wParam_loword == STATICTEXT_ENDGAME_STATUSBAR)
  129.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  130.    }
  131.  
  132.    // call the default dialog message processing function to keep things going
  133.    return (false);
  134. }
  135.