// dialog_comment.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// dialog template
 
#define THIS_DIALOG DIALOG_COMMENT
 
 
 
 
 
// prototypes of local functions
 
static void StartThread_ThisDialog (void *thread_parms);
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
 
 
 
 
void DialogBox_Comment (void)
 
{
 
   // helper function to fire up the modeless dialog box
 
 
 
   is_dialogbox_displayed = true;
 
   _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new one
 
 
 
   return; // return as soon as the thread is fired up
 
}
 
 
 
 
 
void DialogBox_Comment_Validated (void)
 
{
 
   // callback function called by the main game thread when the dialog box is validated
 
 
 
   // remember this callback is no longer to be called
 
   is_dialogbox_comment_validated = false;
 
 
 
   return; // finished
 
}
 
 
 
 
 
static void StartThread_ThisDialog (void *thread_parms)
 
{
 
   // this function runs in a separate thread, for that's the only way (seemingly)
 
   // to implement a non-modal message box using the Common Controls library.
 
 
 
   // display the dialog box
 
   if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1)
 
      is_dialogbox_comment_validated = true;
 
   is_dialogbox_displayed = false;
 
 
 
   the_board.reevaluate = true; // refresh the GUI buttons if needed
 
   return; // _endthread() implied
 
}
 
 
 
 
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
 
{
 
   // message handler for the dialog box
 
 
 
   unsigned short wParam_hiword;
 
   unsigned short wParam_loword;
 
   int new_size;
 
 
 
   // filter out the commonly used message values
 
   wParam_hiword = HIWORD (wParam);
 
   wParam_loword = LOWORD (wParam);
 
 
 
   // have we just fired up this window ?
 
   if (message == WM_INITDIALOG)
 
   {
 
      // center the window
 
      CenterWindow (hWnd, hMainWnd);
 
 
 
      // set dialog icons (small one for title bar & big one for task manager)
 
      SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
      SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
 
 
      // set window title and control texts
 
      SetWindowText (hWnd, LOCALIZE (L"Comment_Title"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_QUESTION), LOCALIZE (L"Comment_Question"));
 
      SetWindowText (GetDlgItem (hWnd, BUTTON_OK), LOCALIZE (L"Button_OK"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR), LOCALIZE (L"Comment_StatusBar"));
 
 
 
      // set the edit box comment, select all text and send focus to it
 
      if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count)
 
          && (the_board.moves[the_board.viewed_move].comment != NULL) && (the_board.moves[the_board.viewed_move].comment[0] != 0))
 
      {
 
         SetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment);
 
         SendMessage (GetDlgItem (hWnd, EDITBOX_COMMENT), EM_SETSEL, 0, -1);
 
      }
 
      SetFocus (GetDlgItem (hWnd, EDITBOX_COMMENT));
 
 
 
      // convert the status bar message to a hyperlink
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_COMMENT_STATUSBAR));
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      EndDialog (hWnd, 0); // close the dialog box
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
 
      if (wParam_loword == IDCANCEL)
 
         EndDialog (hWnd, 0); // close the dialog box
 
 
 
      // else was it the OK button ?
 
      else if (wParam_loword == BUTTON_OK)
 
      {
 
         // grab the edit box comment
 
         if ((the_board.move_count > 1) && (the_board.viewed_move >= 1) && (the_board.viewed_move < the_board.move_count))
 
         {
 
            // get comment length
 
            new_size = GetWindowTextLength (GetDlgItem (hWnd, EDITBOX_COMMENT));
 
 
 
            // was there text at all ?
 
            if (new_size > 0)
 
            {
 
               // if so, reallocate (add one for the terminator character) and copy comment
 
               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);
 
               GetWindowText (GetDlgItem (hWnd, EDITBOX_COMMENT), the_board.moves[the_board.viewed_move].comment, new_size + 1);
 
               the_board.moves[the_board.viewed_move].comment_size = new_size + 1; // remember comment size
 
            }
 
            else
 
            {
 
               SAFE_free ((void **) &the_board.moves[the_board.viewed_move].comment); // comment was erased, free the memory space used for it
 
               the_board.moves[the_board.viewed_move].comment_size = 0; // and reset its size to zero
 
            }
 
         }
 
 
 
         EndDialog (hWnd, 1); // close the dialog box and return OK
 
      }
 
 
 
      // else was it the status bar hyperlink ?
 
      else if (wParam_loword == STATICTEXT_COMMENT_STATUSBAR)
 
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
 
   }
 
 
 
   // call the default dialog message processing function to keep things going
 
   return (false);
 
}