Subversion Repositories Games.Chess Giants

Rev

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