Subversion Repositories Games.Chess Giants

Rev

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