Subversion Repositories Games.Chess Giants

Rev

Rev 21 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // dialog_registration.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_REGISTER
  8.  
  9.  
  10. // prototypes of local functions
  11. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  12.  
  13.  
  14. void DialogBox_Registration (void)
  15. {
  16.    // helper function to fire up the modal dialog box
  17.  
  18.    // display the dialog box (set the parent to be the desktop)
  19.    DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), NULL, DialogProc_ThisDialog);
  20.  
  21.    return; // finished processing this dialog
  22. }
  23.  
  24.  
  25. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  26. {
  27.    // message handler for the dialog box
  28.  
  29.    static wchar_t error_message[1024];
  30.    static wchar_t candidate_email[256];
  31.  
  32.    unsigned long candidate_code;
  33.    unsigned short wParam_hiword;
  34.    unsigned short wParam_loword;
  35.    BOOL was_translated; // needs to be of type BOOL (int)
  36.    int read_index;
  37.    int write_index;
  38.    int length;
  39.  
  40.    // filter out the commonly used message values
  41.    wParam_hiword = HIWORD (wParam);
  42.    wParam_loword = LOWORD (wParam);
  43.  
  44.    // have we just fired up this window ?
  45.    if (message == WM_INITDIALOG)
  46.    {
  47.       // center the window
  48.       CenterWindow (hWnd, hMainWnd);
  49.  
  50.       // set dialog icons (small one for title bar & big one for task manager)
  51.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  52.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  53.  
  54.       // set window title and control texts
  55.       SetWindowText (hWnd, LOCALIZE (L"Registration_Title"));
  56.  
  57.       // set the static texts
  58.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_QUESTION), LOCALIZE (L"Registration_Question"));
  59.       SetWindowText (GetDlgItem (hWnd, BUTTON_DONATE), LOCALIZE (L"Registration_Donate"));
  60.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL), LOCALIZE (L"Registration_NoPayPal"));
  61.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions"));
  62.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_EMAILADDRESS), LOCALIZE (L"Registration_EmailAddress"));
  63.       SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email);
  64.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_ACTIVATIONCODE), LOCALIZE (L"Registration_ActivationCode"));
  65.       //SetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, options.registration.activation_code, false);
  66.       SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode"));
  67.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR), LOCALIZE (L"Registration_StatusBar"));
  68.  
  69.       // set focus to the first settable item
  70.       SendMessage (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS), EM_SETSEL, 0, -1); // select all text
  71.       SetFocus (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS));
  72.  
  73.       // convert the "no paypal" and the status bar message to hyperlinks
  74.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL));
  75.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR));
  76.    }
  77.  
  78.    // else did we click the close button on the title bar ?
  79.    else if (message == WM_CLOSE)
  80.       EndDialog (hWnd, 0); // close the dialog box
  81.  
  82.    // else did we take action on one of the controls ?
  83.    else if (message == WM_COMMAND)
  84.    {
  85.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  86.       if (wParam_loword == IDCANCEL)
  87.          EndDialog (hWnd, 0); // close the dialog box
  88.  
  89.       // else was it the validation button ?
  90.       else if (wParam_loword == BUTTON_VALIDATECODE)
  91.       {
  92.          // retrieve form data
  93.          GetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, candidate_email, WCHAR_SIZEOF (candidate_email));
  94.          candidate_code = GetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, &was_translated, false);
  95.  
  96.          // strip spaces and unprintable characters from candidate email
  97.          length = wcslen (candidate_email);
  98.          write_index = 0;
  99.          for (read_index = 0; read_index < length; read_index++)
  100.             if (!iswspace (candidate_email[read_index]) && (read_index > write_index))
  101.             {
  102.                candidate_email[write_index] = candidate_email[read_index];
  103.                write_index++;
  104.             }
  105.          candidate_email[write_index] = 0; // ensure string is correctly terminated
  106.  
  107.          // save activation email
  108.          wcscpy_s (options.registration.user_email, WCHAR_SIZEOF (options.registration.user_email), candidate_email);
  109.  
  110.          // is the email or the activation code empty ?
  111.          if ((candidate_email[0] == 0) || !was_translated)
  112.             EndDialog (hWnd, 0); // if so, just exit the dialog (assume user did not want to register)
  113.  
  114.          // else is the supplied data correct ?
  115.          else if (IsRegistrationCorrect (candidate_email, candidate_code))
  116.          {
  117.             // if so, save activation code
  118.             options.registration.activation_code = candidate_code;
  119.             MessageBox (hWnd, LOCALIZE (L"Registration_ThankYou"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK);
  120.             EndDialog (hWnd, 0); // then display a thank you dialog box and exit the program
  121.          }
  122.  
  123.          // else the supplied data is wrong
  124.          else
  125.          {
  126.             swprintf_s (error_message, WCHAR_SIZEOF (error_message), LOCALIZE (L"Registration_Error"), AUTHOR_EMAIL);
  127.             MessageBox (hWnd, error_message, PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); // wrong activation. Display an error and DON'T exit the program
  128.          }
  129.       }
  130.  
  131.       // else was it the PayPal button ?
  132.       else if (wParam_loword == BUTTON_DONATE)
  133.          ShellExecute (NULL, L"open", PAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open PayPal in the default browser, maximized
  134.  
  135.       // else was it the "no PayPal" hyperlink ?
  136.       else if (wParam_loword == STATICTEXT_REGISTRATION_NOPAYPAL)
  137.          ShellExecute (NULL, L"open", NOPAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  138.  
  139.       // else was it the status bar hyperlink ?
  140.       else if (wParam_loword == STATICTEXT_REGISTRATION_STATUSBAR)
  141.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  142.    }
  143.  
  144.    // call the default dialog message processing function to keep things going
  145.    return (false);
  146. }
  147.