Subversion Repositories Games.Chess Giants

Rev

Rev 1 | Rev 140 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// dialog_sendseek.cpp
2
 
3
#include "../common.h"
4
 
5
 
6
// dialog template
7
#define THIS_DIALOG DIALOG_SENDSEEK
8
 
9
 
10
// local type definitions
11
typedef struct sendseek_s
12
{
13
   int color; // color we want to play
14
   int initial_time; // initial time of the game for each color
15
   int increment; // eventual time increment (Fischer pace)
16
   int rating_from; // opponent rating must be at least
17
   int rating_to; // opponent rating must be at most
18
   bool is_rated; // whether this game is rated or not
19
   bool is_autostart; // whether this game is rated or not
20
} sendseek_t;
21
 
22
 
23
// global variables used in this module only
24
static sendseek_t sendseek = { COLOR_UNSPECIFIED, 2, 12, 0, 0, false, false };
25
static HWND hThisWnd = NULL;
26
 
27
 
28
// prototypes of local functions
29
static void StartThread_ThisDialog (void *thread_parms);
30
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
31
 
32
 
33
void DialogBox_SendSeek (void)
34
{
35
   // helper function to fire up the modeless dialog box
36
 
37
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up the thread
38
   return; // return as soon as the thread is fired up
39
}
40
 
41
 
42
void DialogBox_SendSeek_Validated (void)
43
{
44
   // callback function called by the main game thread when the dialog box is validated
45
 
46
   player_t *network_player;
47
 
48
   // remember this callback is no longer to be called
49
   is_dialogbox_sendseek_validated = false;
50
 
51
   network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
52
   if (network_player == NULL)
53
      return; // consistency check
54
 
55
   // send the challenge
56
   Player_SendBuffer_Add (network_player, 1000, L"seek %d %d %s %s %s %d-%d\n",
57
                                                sendseek.initial_time,
58
                                                sendseek.increment,
59
                                                (sendseek.is_rated ? L"rated" : L"unrated"),
60
                                                (sendseek.color == COLOR_BLACK ? L"black" : (sendseek.color == COLOR_WHITE ? L"white" : L"")),
61
                                                (sendseek.is_autostart ? L"auto" : L"manual"),
62
                                                sendseek.rating_from,
63
                                                sendseek.rating_to);
64
 
65
   // display a message box to the user
66
   messagebox.hWndParent = hMainWnd;
67
   wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"SendSeek_Title"));
68
   wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"SendSeek_SeekSent"));
69
   messagebox.flags = MB_ICONINFORMATION | MB_OK;
70
   DialogBox_Message (&messagebox);
71
 
72
   return; // finished, game request is sent
73
}
74
 
75
 
76
static void StartThread_ThisDialog (void *thread_parms)
77
{
78
   // this function runs in a separate thread, for that's the only way (seemingly)
79
   // to implement a non-modal message box using the Common Controls library.
80
 
81
   if (IsWindow (hThisWnd))
82
      SendMessage (hThisWnd, WM_CLOSE, 0, 0); // if such a dialog already exists, close it
83
 
84
   // display the dialog box
85
   if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
86
      is_dialogbox_sendseek_validated = true; // notify main game thread to fire up our callback
87
 
124 pmbaty 88
   the_board.reevaluate = true; // refresh the GUI buttons if needed
1 pmbaty 89
   return; // _endthread() implied
90
}
91
 
92
 
93
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
94
{
95
   // message handler for the dialog box
96
 
97
   unsigned short wParam_hiword;
98
   unsigned short wParam_loword;
99
   BOOL was_translated; // needs to be of type BOOL (int)
100
 
101
   // filter out the commonly used message values
102
   wParam_hiword = HIWORD (wParam);
103
   wParam_loword = LOWORD (wParam);
104
 
105
   // have we just fired up this window ?
106
   if (message == WM_INITDIALOG)
107
   {
108
      hThisWnd = hWnd; // save the dialog window handle
109
 
110
      // center the window
111
      CenterWindow (hWnd, hMainWnd);
112
 
113
      // set window title and control texts
114
      SetWindowText (hWnd, LOCALIZE (L"SendSeek_Title"));
115
 
116
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_QUESTION, LOCALIZE (L"SendSeek_Question"));
117
 
118
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_COLOR, LOCALIZE (L"SendSeek_ColorIWishToPlay"));
119
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), L"");
120
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_White"));
121
      ComboBox_AddString (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), LOCALIZE (L"Games_Black"));
122
      ComboBox_SetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR), 1 + sendseek.color);
123
 
124
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INITIALTIME, LOCALIZE (L"SendSeek_InitialTime"));
125
      SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, sendseek.initial_time, false);
126
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_INCREMENT, LOCALIZE (L"SendSeek_Increment"));
127
      SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, sendseek.increment, false);
128
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGFROM, LOCALIZE (L"SendSeek_RatingFrom"));
129
      if ((sendseek.rating_from > 0) && (sendseek.rating_from < 9999))
130
         SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, sendseek.rating_from, false);
131
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_RATINGTO, LOCALIZE (L"SendSeek_RatingTo"));
132
      if ((sendseek.rating_to > 0) && (sendseek.rating_to < 9999))
133
         SetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, sendseek.rating_to, false);
134
      SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME, LOCALIZE (L"SendSeek_RatedUnrated"));
135
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME), (sendseek.is_rated ? BST_CHECKED : BST_UNCHECKED));
136
      SetDlgItemText (hWnd, CHECKBOX_SENDSEEK_AUTOSTART, LOCALIZE (L"SendSeek_AutoStart"));
137
      Button_SetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART), (sendseek.is_autostart ? BST_CHECKED : BST_UNCHECKED));
138
 
139
      SetDlgItemText (hWnd, BUTTON_SENDSEEK, LOCALIZE (L"Button_Send"));
140
      SetDlgItemText (hWnd, BUTTON_CANCEL, LOCALIZE (L"Button_Cancel"));
141
      SetDlgItemText (hWnd, STATICTEXT_SENDSEEK_STATUSBAR, LOCALIZE (L"SendSeek_StatusBar"));
142
 
143
      // convert the status bar message to hyperlink
144
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_SENDSEEK_STATUSBAR));
145
   }
146
 
147
   // else did we click the close button on the title bar ?
148
   else if (message == WM_CLOSE)
149
      EndDialog (hWnd, 0); // close the dialog box
150
 
151
   // else did we take action on one of the controls ?
152
   else if (message == WM_COMMAND)
153
   {
154
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
155
      // OR was it the "cancel" button ?
156
      if ((wParam_loword == IDCANCEL) || (wParam_loword == BUTTON_CANCEL))
157
         EndDialog (hWnd, 0); // close the dialog box
158
 
159
      // else was it the "send" button ?
160
      else if (wParam_loword == BUTTON_SENDSEEK)
161
      {
162
         // retrieve the control values
163
         sendseek.color = ComboBox_GetCurSel (GetDlgItem (hWnd, COMBOBOX_SENDSEEK_COLOR)) - 1;
164
         sendseek.initial_time = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INITIALTIME, &was_translated, false);
165
         if (!was_translated || (sendseek.initial_time == 0))
166
            sendseek.initial_time = 2; // default value
167
         sendseek.increment = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_INCREMENT, &was_translated, false);
168
         if (!was_translated)
169
            sendseek.increment = 12; // default value
170
         sendseek.rating_from = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGFROM, &was_translated, false);
171
         if (!was_translated)
172
            sendseek.rating_from = 0; // default value
173
         sendseek.rating_to = GetDlgItemInt (hWnd, EDITBOX_SENDSEEK_RATINGTO, &was_translated, false);
174
         if (!was_translated || (sendseek.rating_to == 0) || (sendseek.rating_to < sendseek.rating_from))
175
            sendseek.rating_to = 9999; // default value
176
         sendseek.is_rated = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_RATETHISGAME)) != 0);
177
         sendseek.is_autostart = (Button_GetCheck (GetDlgItem (hWnd, CHECKBOX_SENDSEEK_AUTOSTART)) != 0);
178
 
179
         EndDialog (hWnd, 1); // close the dialog box and return OK
180
      }
181
 
182
      // else was it the status bar hyperlink ?
183
      else if (wParam_loword == STATICTEXT_SENDSEEK_STATUSBAR)
184
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
185
   }
186
 
187
   // call the default dialog message processing function to keep things going
188
   return (false);
189
}