<commit_before>/*
* Copyright (c) 2003-2010 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

// GeneralMsgBox.cpp
/*
* GeneralMsgBox.h
*
* Implements the extended Message Box class.
*
* This is a cut down version of TcxMsgBox by Thales P. Carvalho but then
* significantly enhanced to support text with HTML formatting and links
* instead of a RTF string by using a CRichEditCtrlExtn control.
* See www.codeproject.com for the orgininal code
*/

#include "stdafx.h"

#include "ThisMfcApp.h"
#include "GeneralMsgBox.h"
#include "RichEditCtrlExtn.h"
#include <RichEdit.h>

#include "resource3.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Local constants

/*
The following _dlgData corresponds to:

DLGTEMPLATEEX dtex;

  dtex.wDlgVer = 1;
  dtex.wSignature = 0xFFFF;
  dtex.dwHelpID = 0;
  dtex.dwStyle = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | DS_SETFONT | DS_MODALFRAME;
  dtex.dwExStyle = WS_EX_APPWINDOW;
  dtex.cDlgItems = 0;
  dtex.x = 0;
  dtex.y = 0;
  dtex.cx = 185;
  dtex.cy = 92;
  no menu ( (WORD)0 )
  no windowClass ( (WORD)0 )
  caption L"."
  point size (only if DS_SETFONT specified)
  weight                    "
  italic                    "
  charset                   "
  typeface                  "
*/

static const BYTE _dlgData[] =
{
  0x01, 0x00,                                      // wDlgVer (must be on DWORD boundary)
  0xff, 0xff,                                      // wSignature
  0x00, 0x00, 0x00, 0x00,                          // dwhelpID
  0x00, 0x00, 0x04, 0x00,                          // dsStyle
  0xc0, 0x00, 0xc8, 0x90,                          // dwExStyle
  0x00, 0x00,                                      // cDlgItems
  0x00, 0x00,                                      // x
  0x00, 0x00,                                      // y
  0xb9, 0x00,                                      // cx
  0x5c, 0x00,                                      // cy
  0x00, 0x00,                                      // no menu (must be on WORD boundary)
  0x00, 0x00,                                      // no windowClass
  0x2e, 0x00, 0x00, 0x00,                          // Caption = '.\0'
  0x08, 0x00,                                      // point size
  0x00, 0x00,                                      // weight
  0x00,                                            // italic
  0x01,                                            // charset
  // typeface (must be on WORD boundary)
  0x4d, 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00,  // 'MS S'
  0x61, 0x00, 0x6e, 0x00, 0x73, 0x00, 0x20, 0x00,  // 'ans '
  0x53, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00,  // 'Seri'
  0x66, 0x00, 0x00, 0x00                           // 'f\0'
};

/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox

// Constructor
CGeneralMsgBox::CGeneralMsgBox(CWnd *pParentWnd)
  : m_uiDefCmdId((UINT)IDC_STATIC), m_uiEscCmdId((UINT)IDC_STATIC),
  m_hIcon(NULL), m_strTitle(AfxGetApp()->m_pszAppName)
{
  m_pParentWnd = pParentWnd;

  // default metric values
  const int _aDefMetrics[NUM_OF_METRICS] =
  {
    10,   // CX_LEFT_BORDER,
    10,   // CX_RIGHT_BORDER,
    10,   // CY_TOP_BORDER,
    7,    // CY_BOTTOM_BORDER,

    10,   // CX_ICON_MSG_SPACE,
    7,    // CY_BTNS_MSG_SPACE,

    7,    // CX_BTN_BORDER,
    4,    // CY_BTN_BORDER,

    7,    // CX_BTNS_SPACE,
    40,   // CX_MIN_BTN,
  };

  for (int i = 0; i < NUM_OF_METRICS; ++i) {
    m_aMetrics[i] = _aDefMetrics[i];
  }
}

// Destructor
CGeneralMsgBox::~CGeneralMsgBox()
{
  ::DestroyIcon(m_hIcon);
}

INT_PTR CGeneralMsgBox::MessageBoxTimeOut(LPCWSTR lpText, LPCWSTR lpCaption,
                                          UINT uiFlags, DWORD dwMilliseconds)
{
  if (dwMilliseconds == 0)
    return MessageBox(lpText, lpCaption, uiFlags);

  m_dwTimeOut = dwMilliseconds;
  m_bTimedOut = false;
  m_nResult = 0;

  DWORD dwThreadId;
  HANDLE hThread = CreateThread(NULL, 0, ThreadFunction, (LPVOID)this, 0, &dwThreadId);
  if (hThread == NULL)
    return -1;

  m_nResult = MessageBox(lpText, lpCaption, uiFlags);

  WaitForSingleObject(hThread, INFINITE);

  CloseHandle(hThread);

  if (m_bTimedOut)
    return IDTIMEOUT;

  return m_nResult;
}

DWORD WINAPI CGeneralMsgBox::ThreadFunction(LPVOID lpParameter)
{
  if (lpParameter) {
    CGeneralMsgBox *pObject = static_cast<CGeneralMsgBox *>(lpParameter);
    DWORD dwInitTime = GetTickCount();
    DWORD dwStartTime = dwInitTime;
    while (pObject->m_nResult != 0) {
      DWORD dwCurrentTime = GetTickCount();
      DWORD dwDeltaTime = dwCurrentTime - dwStartTime;
      if (dwDeltaTime >= 1000) {
        if ((dwCurrentTime - dwInitTime) >= pObject->m_dwTimeOut) {
          pObject->m_bTimedOut = true;
          pObject->PostMessage(WM_COMMAND, IDTIMEOUT, 0);
          return WAIT_OBJECT_0;
        }
        dwStartTime = GetTickCount();
      }
      Sleep(50);
    }
  }
  return WAIT_OBJECT_0;
}

INT_PTR CGeneralMsgBox::MessageBox(LPCWSTR lpText, LPCWSTR lpCaption, 
                                   UINT uiFlags)
{
  // Private member
  UINT uiType = uiFlags & MB_TYPEMASK;
  UINT uiIcon = uiFlags & MB_ICONMASK;
  int iDefB = (int)uiFlags & MB_DEFMASK;

  if (lpText != NULL)
    SetMsg(lpText);

  if (lpCaption != NULL)
    SetTitle(lpCaption);
  else
    SetTitle(IDS_ERROR);

  if (uiIcon == 0)
    uiIcon = MB_ICONEXCLAMATION;
  SetStandardIcon(uiIcon);

  int num_buttons(0);
  int ButtonCmdIDs[3];
  int ButtonCmdTexts[3];
  
  switch (uiType) {<commit_msg>Fix long term bug in setting default button in CGeneralMsgBox MessageBox<commit_after>/*
* Copyright (c) 2003-2010 <NAME> <<EMAIL>>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/

// GeneralMsgBox.cpp
/*
* GeneralMsgBox.h
*
* Implements the extended Message Box class.
*
* This is a cut down version of TcxMsgBox by <NAME> but then
* significantly enhanced to support text with HTML formatting and links
* instead of a RTF string by using a CRichEditCtrlExtn control.
* See www.codeproject.com for the orgininal code
*/

#include "stdafx.h"

#include "ThisMfcApp.h"
#include "GeneralMsgBox.h"
#include "RichEditCtrlExtn.h"
#include <RichEdit.h>

#include "resource3.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Local constants

/*
The following _dlgData corresponds to:

DLGTEMPLATEEX dtex;

  dtex.wDlgVer = 1;
  dtex.wSignature = 0xFFFF;
  dtex.dwHelpID = 0;
  dtex.dwStyle = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | DS_SETFONT | DS_MODALFRAME;
  dtex.dwExStyle = WS_EX_APPWINDOW;
  dtex.cDlgItems = 0;
  dtex.x = 0;
  dtex.y = 0;
  dtex.cx = 185;
  dtex.cy = 92;
  no menu ( (WORD)0 )
  no windowClass ( (WORD)0 )
  caption L"."
  point size (only if DS_SETFONT specified)
  weight                    "
  italic                    "
  charset                   "
  typeface                  "
*/

static const BYTE _dlgData[] =
{
  0x01, 0x00,                                      // wDlgVer (must be on DWORD boundary)
  0xff, 0xff,                                      // wSignature
  0x00, 0x00, 0x00, 0x00,                          // dwhelpID
  0x00, 0x00, 0x04, 0x00,                          // dsStyle
  0xc0, 0x00, 0xc8, 0x90,                          // dwExStyle
  0x00, 0x00,                                      // cDlgItems
  0x00, 0x00,                                      // x
  0x00, 0x00,                                      // y
  0xb9, 0x00,                                      // cx
  0x5c, 0x00,                                      // cy
  0x00, 0x00,                                      // no menu (must be on WORD boundary)
  0x00, 0x00,                                      // no windowClass
  0x2e, 0x00, 0x00, 0x00,                          // Caption = '.\0'
  0x08, 0x00,                                      // point size
  0x00, 0x00,                                      // weight
  0x00,                                            // italic
  0x01,                                            // charset
  // typeface (must be on WORD boundary)
  0x4d, 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00,  // 'MS S'
  0x61, 0x00, 0x6e, 0x00, 0x73, 0x00, 0x20, 0x00,  // 'ans '
  0x53, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00,  // 'Seri'
  0x66, 0x00, 0x00, 0x00                           // 'f\0'
};

/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox

// Constructor
CGeneralMsgBox::CGeneralMsgBox(CWnd *pParentWnd)
  : m_uiDefCmdId((UINT)IDC_STATIC), m_uiEscCmdId((UINT)IDC_STATIC),
  m_hIcon(NULL), m_strTitle(AfxGetApp()->m_pszAppName)
{
  m_pParentWnd = pParentWnd;

  // default metric values
  const int _aDefMetrics[NUM_OF_METRICS] =
  {
    10,   // CX_LEFT_BORDER,
    10,   // CX_RIGHT_BORDER,
    10,   // CY_TOP_BORDER,
    7,    // CY_BOTTOM_BORDER,

    10,   // CX_ICON_MSG_SPACE,
    7,    // CY_BTNS_MSG_SPACE,

    7,    // CX_BTN_BORDER,
    4,    // CY_BTN_BORDER,

    7,    // CX_BTNS_SPACE,
    40,   // CX_MIN_BTN,
  };

  for (int i = 0; i < NUM_OF_METRICS; ++i) {
    m_aMetrics[i] = _aDefMetrics[i];
  }
}

// Destructor
CGeneralMsgBox::~CGeneralMsgBox()
{
  ::DestroyIcon(m_hIcon);
}

INT_PTR CGeneralMsgBox::MessageBoxTimeOut(LPCWSTR lpText, LPCWSTR lpCaption,
                                          UINT uiFlags, DWORD dwMilliseconds)
{
  if (dwMilliseconds == 0)
    return MessageBox(lpText, lpCaption, uiFlags);

  m_dwTimeOut = dwMilliseconds;
  m_bTimedOut = false;
  m_nResult = 0;

  DWORD dwThreadId;
  HANDLE hThread = CreateThread(NULL, 0, ThreadFunction, (LPVOID)this, 0, &dwThreadId);
  if (hThread == NULL)
    return -1;

  m_nResult = MessageBox(lpText, lpCaption, uiFlags);

  WaitForSingleObject(hThread, INFINITE);

  CloseHandle(hThread);

  if (m_bTimedOut)
    return IDTIMEOUT;

  return m_nResult;
}

DWORD WINAPI CGeneralMsgBox::ThreadFunction(LPVOID lpParameter)
{
  if (lpParameter) {
    CGeneralMsgBox *pObject = static_cast<CGeneralMsgBox *>(lpParameter);
    DWORD dwInitTime = GetTickCount();
    DWORD dwStartTime = dwInitTime;
    while (pObject->m_nResult != 0) {
      DWORD dwCurrentTime = GetTickCount();
      DWORD dwDeltaTime = dwCurrentTime - dwStartTime;
      if (dwDeltaTime >= 1000) {
        if ((dwCurrentTime - dwInitTime) >= pObject->m_dwTimeOut) {
          pObject->m_bTimedOut = true;
          pObject->PostMessage(WM_COMMAND, IDTIMEOUT, 0);
          return WAIT_OBJECT_0;
        }
        dwStartTime = GetTickCount();
      }
      Sleep(50);
    }
  }
  return WAIT_OBJECT_0;
}

INT_PTR CGeneralMsgBox::MessageBox(LPCWSTR lpText, LPCWSTR lpCaption, 
                                   UINT uiFlags)
{
  // Private member
  UINT uiType = uiFlags & MB_TYPEMASK;
  UINT uiIcon = uiFlags & MB_ICONMASK;
  int iDefB = ((int)uiFlags & MB_DEFMASK) / 256;

  if (lpText != NULL)
    SetMsg(lpText);

  if (lpCaption != NULL)
    SetTitle(lpCaption);
  else
    SetTitle(IDS_ERROR);

  if (uiIcon == 0)
    uiIcon = MB_ICONEXCLAMATION;
  SetStandardIcon(uiIcon);

  int num_buttons(0);
  int ButtonCmdIDs[3];
  int ButtonCmdTexts[3];
  
  switch (uiType) {
{'fraction_non_alphanumeric': 0.12844509315703614, 'fraction_numerical': 0.04421739503734867, 'mean_word_length': 1.8237575757575757, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883844', 'n_tokens_mistral': 4449, 'n_tokens_neox': 4061, 'n_words': 931}