Subversion Repositories Games.Chess Giants

Rev

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