Subversion Repositories Games.Chess Giants

Rev

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