Subversion Repositories Games.Chess Giants

Rev

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

  1. // dialog_comment.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_COMMENT
  8.  
  9.  
  10. // prototypes of local functions
  11. static void StartThread_ThisDialog (void *thread_parms);
  12. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  13.  
  14.  
  15. void DialogBox_Comment (void)
  16. {
  17.    // helper function to fire up the modeless dialog box
  18.  
  19.    _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
  20.  
  21.    return; // return as soon as the thread is fired up
  22. }
  23.  
  24.  
  25. void DialogBox_Comment_Validated (void)
  26. {
  27.    // callback function called by the main game thread when the dialog box is validated
  28.  
  29.    // remember this callback is no longer to be called
  30.    is_dialogbox_comment_validated = false;
  31.  
  32.    return; // finished
  33. }
  34.  
  35.  
  36. static void StartThread_ThisDialog (void *thread_parms)
  37. {
  38.    // this function runs in a separate thread, for that's the only way (seemingly)
  39.    // to implement a non-modal message box using the Common Controls library.
  40.  
  41.    // display the dialog box
  42.    if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
  43.       is_dialogbox_comment_validated = true;
  44.  
  45.    the_board.reevaluate = true; // refresh the GUI buttons if needed
  46.    return; // _endthread() implied
  47. }
  48.  
  49.  
  50. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  51. {
  52.    // message handler for the dialog box
  53.  
  54.    unsigned short wParam_hiword;
  55.    unsigned short wParam_loword;
  56.    int new_size;
  57.  
  58.    // filter out the commonly used message values
  59.    wParam_hiword = HIWORD (wParam);
  60.    wParam_loword = LOWORD (wParam);
  61.  
  62.    // have we just fired up this window ?
  63.    if (message == WM_INITDIALOG)
  64.    {
  65.       // center the window
  66.       CenterWindow (hWnd, hMainWnd);
  67.  
  68.       // set dialog icons (small one for title bar & big one for task manager)
  69.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  70.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  71.  
  72.       // set window title and control texts
  73.       SetWindowText (hWnd, LOCALIZE (L"Comment_Title"));
  74.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_QUESTION), LOCALIZE (L"Comment_Question"));
  75.       SetWindowText (GetDlgItem (hWnd, BUTTON_OK), LOCALIZE (L"Button_OK"));
  76.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR), LOCALIZE (L"Comment_StatusBar"));
  77.  
  78.       // set the edit box comment, select all text and send focus to it
  79.       if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count)
  80.           && (the_board.moves[the_board.viewed_move].comment != NULL) && (the_board.moves[the_board.viewed_move].comment[0] != 0))
  81.       {
  82.          SetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment);
  83.          SendMessage (GetDlgItem (hWnd, EDITBOX_COMMENT), EM_SETSEL, 0, -1);
  84.       }
  85.       SetFocus (GetDlgItem (hWnd, EDITBOX_COMMENT));
  86.  
  87.       // convert the status bar message to a hyperlink
  88.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR));
  89.    }
  90.  
  91.    // else did we click the close button on the title bar ?
  92.    else if (message == WM_CLOSE)
  93.       EndDialog (hWnd, 0); // close the dialog box
  94.  
  95.    // else did we take action on one of the controls ?
  96.    else if (message == WM_COMMAND)
  97.    {
  98.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  99.       if (wParam_loword == IDCANCEL)
  100.          EndDialog (hWnd, 0); // close the dialog box
  101.  
  102.       // else was it the OK button ?
  103.       else if (wParam_loword == BUTTON_OK)
  104.       {
  105.          // grab the edit box comment
  106.          if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count))
  107.          {
  108.             // get comment length
  109.             new_size = GetWindowTextLength (GetDlgItem (hWnd, EDITBOX_COMMENT));
  110.  
  111.             // was there text at all ?
  112.             if (new_size > 0)
  113.             {
  114.                // if so, reallocate (add one for the terminator character) and copy comment
  115.                the_board.moves[the_board.viewed_move].comment = (wchar_t *) SAFE_realloc (the_board.moves[the_board.viewed_move].comment, the_board.moves[the_board.viewed_move].comment_size, new_size + 1, sizeof (wchar_t), false);
  116.                GetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment, new_size + 1);
  117.                the_board.moves[the_board.viewed_move].comment_size = new_size + 1; // remember comment size
  118.             }
  119.             else
  120.             {
  121.                SAFE_free ((void **) &the_board.moves[the_board.viewed_move].comment); // comment was erased, free the memory space used for it
  122.                the_board.moves[the_board.viewed_move].comment_size = 0; // and reset its size to zero
  123.             }
  124.          }
  125.  
  126.          EndDialog (hWnd, 1); // close the dialog box and return OK
  127.       }
  128.  
  129.       // else was it the status bar hyperlink ?
  130.       else if (wParam_loword == STATICTEXT_COMMENT_STATUSBAR)
  131.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  132.    }
  133.  
  134.    // call the default dialog message processing function to keep things going
  135.    return (false);
  136. }
  137.