Subversion Repositories Games.Chess Giants

Rev

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

  1. // dialog_sendchallenge.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_SENDCHALLENGE
  8.  
  9.  
  10. // local type definitions
  11. typedef struct sendchallenge_s
  12. {
  13.    wchar_t challengee[32]; // challengee's nickname
  14.    int color; // color we want to play
  15.    bool is_rated; // whether this game is rated or not
  16.    int initial_time; // initial time of the game for each color
  17.    int increment; // eventual time increment (Fischer pace)
  18. } sendchallenge_t;
  19.  
  20.  
  21. // global variables used in this module only
  22. static sendchallenge_t sendchallenge = { L"", COLOR_UNSPECIFIED, false, 2, 12};
  23. static HWND hThisWnd = NULL;
  24.  
  25.  
  26. // prototypes of local functions
  27. static void StartThread_ThisDialog (void *thread_parms);
  28. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  29.  
  30.  
  31. void DialogBox_SendChallenge (wchar_t *challengee_name)
  32. {
  33.    // helper function to fire up the modeless dialog box
  34.  
  35.    // prepare the sendchallenge structure
  36.    wcscpy_s (sendchallenge.challengee, WCHAR_SIZEOF (sendchallenge.challengee), challengee_name);
  37.  
  38.    is_dialogbox_displayed = true;
  39.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up the thread
  40.    return; // return as soon as the thread is fired up
  41. }
  42.  
  43.  
  44. void DialogBox_SendChallenge_Validated (void)
  45. {
  46.    // callback function called by the main game thread when the dialog box is validated
  47.  
  48.    player_t *network_player;
  49.  
  50.    // remember this callback is no longer to be called
  51.    is_dialogbox_sendchallenge_validated = false;
  52.  
  53.    network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
  54.    if (network_player == NULL)
  55.       return; // consistency check
  56.  
  57.    // send the challenge
  58.    Player_SendBuffer_Add (network_player, 1000, L"match %s %s %d %d %s\n",
  59.                                                 sendchallenge.challengee,
  60.                                                 (sendchallenge.is_rated ? L"rated" : L"unrated"),
  61.                                                 sendchallenge.initial_time,
  62.                                                 sendchallenge.increment,
  63.                                                 (sendchallenge.color == COLOR_BLACK ? L"black" : (sendchallenge.color == COLOR_WHITE ? L"white" : L"")));
  64.  
  65.    // send a notification to this player's chat window
  66.    Interlocutor_Notify (Interlocutor_FindOrCreate (sendchallenge.challengee), LOCALIZE (L"Chat_InvitationSent"), sendchallenge.challengee);
  67.    return; // finished, invitation is sent
  68. }
  69.  
  70.  
  71. static void StartThread_ThisDialog (void *thread_parms)
  72. {
  73.    // this function runs in a separate thread, for that's the only way (seemingly)
  74.    // to implement a non-modal message box using the Common Controls library.
  75.  
  76.    if (IsWindow (hThisWnd))
  77.       SendMessage (hThisWnd, WM_CLOSE, 0, 0); // if such a dialog already exists, close it
  78.  
  79.    // display the dialog box
  80.    if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
  81.       is_dialogbox_sendchallenge_validated = true; // notify main game thread to fire up our callback
  82.    is_dialogbox_displayed = false;
  83.  
  84.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  85.    return; // _endthread() implied
  86. }
  87.  
  88.  
  89. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  90. {
  91.    // message handler for the dialog box
  92.  
  93.    unsigned short wParam_hiword;
  94.    unsigned short wParam_loword;
  95.    wchar_t temp_string[256];
  96.    BOOL was_translated; // needs to be of type BOOL (int)
  97.  
  98.    // filter out the commonly used message values
  99.    wParam_hiword = HIWORD (wParam);
  100.    wParam_loword = LOWORD (wParam);
  101.  
  102.    // have we just fired up this window ?
  103.    if (message == WM_INITDIALOG)
  104.    {
  105.       hThisWnd = hWnd; // save the dialog window handle
  106.  
  107.       // center the window
  108.       CenterWindow (hWnd, hMainWnd);
  109.  
  110.       // set window title and control texts
  111.       SetWindowText (hWnd, LOCALIZE (L"SendChallenge_Title"));
  112.  
  113.       swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"SendChallenge_Question"), sendchallenge.challengee);
  114.       SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_QUESTION, temp_string);
  115.  
  116.       SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_COLOR, LOCALIZE (L"SendChallenge_ColorIWishToPlay"));
  117.       ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), L"");
  118.       ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_White"));
  119.       ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_Black"));
  120.       ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), 1 + sendchallenge.color);
  121.  
  122.       SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INITIALTIME, LOCALIZE (L"SendChallenge_InitialTime"));
  123.       SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, sendchallenge.initial_time, false);
  124.       SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INCREMENT, LOCALIZE (L"SendChallenge_Increment"));
  125.       SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, sendchallenge.increment, false);
  126.  
  127.       SetDlgItemText (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME, LOCALIZE (L"SendChallenge_RatedUnrated"));
  128.       Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME), (sendchallenge.is_rated ? BST_CHECKED : BST_UNCHECKED));
  129.  
  130.       SetDlgItemText (hWnd, BUTTON_INVITE, LOCALIZE (L"Button_Invite"));
  131.       SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
  132.       SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR, LOCALIZE (L"SendChallenge_StatusBar"));
  133.  
  134.       // convert the status bar message to hyperlink
  135.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR));
  136.    }
  137.  
  138.    // else did we click the close button on the title bar ?
  139.    else if (message == WM_CLOSE)
  140.       EndDialog (hWnd, 0); // close the dialog box
  141.  
  142.    // else did we take action on one of the controls ?
  143.    else if (message == WM_COMMAND)
  144.    {
  145.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  146.       // OR was it the "cancel" button ?
  147.       if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL))
  148.          EndDialog (hWnd, 0); // close the dialog box
  149.  
  150.       // else was it the "invite" button ?
  151.       else if (wParam_loword == BUTTON_INVITE)
  152.       {
  153.          // retrieve the control values
  154.          sendchallenge.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR)) - 1;
  155.          sendchallenge.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, &was_translated, false);
  156.          if (!was_translated || (sendchallenge.initial_time == 0))
  157.             sendchallenge.initial_time = 2; // default value
  158.          sendchallenge.increment = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, &was_translated, false);
  159.          if (!was_translated)
  160.             sendchallenge.increment = 12; // default value
  161.          sendchallenge.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME)) != 0);
  162.  
  163.          EndDialog (hWnd, 1); // close the dialog box and return OK
  164.       }
  165.  
  166.       // else was it the status bar hyperlink ?
  167.       else if (wParam_loword == STATICTEXT_SENDCHALLENGE_STATUSBAR)
  168.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  169.    }
  170.  
  171.    // call the default dialog message processing function to keep things going
  172.    return (false);
  173. }
  174.