Subversion Repositories Games.Chess Giants

Rev

Rev 124 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 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
   return; // _endthread() implied
83
}
84
 
85
 
86
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
87
{
88
   // message handler for the dialog box
89
 
90
   unsigned short wParam_hiword;
91
   unsigned short wParam_loword;
92
   wchar_t temp_string[256];
93
   BOOL was_translated; // needs to be of type BOOL (int)
94
 
95
   // filter out the commonly used message values
96
   wParam_hiword = HIWORD (wParam);
97
   wParam_loword = LOWORD (wParam);
98
 
99
   // have we just fired up this window ?
100
   if (message == WM_INITDIALOG)
101
   {
102
      hThisWnd = hWnd; // save the dialog window handle
103
 
104
      // center the window
105
      CenterWindow (hWnd, hMainWnd);
106
 
107
      // set window title and control texts
108
      SetWindowText (hWnd, LOCALIZE (L"SendChallenge_Title"));
109
 
110
      swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"SendChallenge_Question"), sendchallenge.challengee);
111
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_QUESTION, temp_string);
112
 
113
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_COLOR, LOCALIZE (L"SendChallenge_ColorIWishToPlay"));
114
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), L"");
115
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_White"));
116
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), LOCALIZE (L"Games_Black"));
117
      ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR), 1 + sendchallenge.color);
118
 
119
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INITIALTIME, LOCALIZE (L"SendChallenge_InitialTime"));
120
      SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, sendchallenge.initial_time, false);
121
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_INCREMENT, LOCALIZE (L"SendChallenge_Increment"));
122
      SetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, sendchallenge.increment, false);
123
 
124
      SetDlgItemText (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME, LOCALIZE (L"SendChallenge_RatedUnrated"));
125
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME), (sendchallenge.is_rated ? BST_CHECKED : BST_UNCHECKED));
126
 
127
      SetDlgItemText (hWnd, BUTTON_INVITE, LOCALIZE (L"Button_Invite"));
128
      SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
129
      SetDlgItemText (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR, LOCALIZE (L"SendChallenge_StatusBar"));
130
 
131
      // convert the status bar message to hyperlink
132
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDCHALLENGE_STATUSBAR));
133
   }
134
 
135
   // else did we click the close button on the title bar ?
136
   else if (message == WM_CLOSE)
137
      EndDialog (hWnd, 0); // close the dialog box
138
 
139
   // else did we take action on one of the controls ?
140
   else if (message == WM_COMMAND)
141
   {
142
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
143
      // OR was it the "cancel" button ?
144
      if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL))
145
         EndDialog (hWnd, 0); // close the dialog box
146
 
147
      // else was it the "invite" button ?
148
      else if (wParam_loword == BUTTON_INVITE)
149
      {
150
         // retrieve the control values
151
         sendchallenge.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDCHALLENGE_COLOR)) - 1;
152
         sendchallenge.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INITIALTIME, &was_translated, false);
153
         if (!was_translated || (sendchallenge.initial_time == 0))
154
            sendchallenge.initial_time = 2; // default value
155
         sendchallenge.increment = GetDlgItemInt (hWnd, EDITBOX_SENDCHALLENGE_INCREMENT, &was_translated, false);
156
         if (!was_translated)
157
            sendchallenge.increment = 12; // default value
158
         sendchallenge.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDCHALLENGE_RATETHISGAME)) != 0);
159
 
160
         EndDialog (hWnd, 1); // close the dialog box and return OK
161
      }
162
 
163
      // else was it the status bar hyperlink ?
164
      else if (wParam_loword == STATICTEXT_SENDCHALLENGE_STATUSBAR)
165
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
166
   }
167
 
168
   // call the default dialog message processing function to keep things going
169
   return (false);
170
}