<commit_before>//------------------------------------------------------------------------------
//
//    Copyright (C) Streamlet. All rights reserved.
//
//    File Name:   xlDialog.h
//    Author:      Streamlet
//    Create Time: 2011-03-12
//    Description: 
//
//    Version history:
//
//
//
//------------------------------------------------------------------------------

#ifndef __XLDIALOG_H_FB784537_F2A1_43D5_B337_1882AB81D19F_INCLUDED__
#define __XLDIALOG_H_FB784537_F2A1_43D5_B337_1882AB81D19F_INCLUDED__


#include <xl/Win32/GUI/xlWindow.h>

namespace xl
{
    class Dialog : public Window
    {
    public:
        Dialog() :
            m_bInModal(false),
            m_nModalResult(-1)
        {
            AppendMsgHandler(WM_CREATE,       MsgHandler(this, &Dialog::OnMsgCreate));
            AppendMsgHandler(WM_INITDIALOG,   MsgHandler(this, &Dialog::OnMsgInitDialog));
            AppendMsgHandler(WM_ERASEBKGND,   MsgHandler(this, &Dialog::OnMsgEraseBackground));
            AppendMsgHandler(WM_CLOSE,        MsgHandler(this, &Dialog::OnMsgClose));

            AppendCommandMsgHandler(IDOK,     CommandMsgHandler(this, &Dialog::OnMsgOK));
            AppendCommandMsgHandler(IDCANCEL, CommandMsgHandler(this, &Dialog::OnMsgCancel));
        }

        Dialog(HWND hWnd) :
            Window(hWnd),
            m_bInModal(false),
            m_nModalResult(-1)
        {
            AppendMsgHandler(WM_CREATE,       MsgHandler(this, &Dialog::OnMsgCreate));
            AppendMsgHandler(WM_INITDIALOG,   MsgHandler(this, &Dialog::OnMsgInitDialog));
            AppendMsgHandler(WM_ERASEBKGND,   MsgHandler(this, &Dialog::OnMsgEraseBackground));
            AppendMsgHandler(WM_CLOSE,        MsgHandler(this, &Dialog::OnMsgClose));

            AppendCommandMsgHandler(IDOK,     CommandMsgHandler(this, &Dialog::OnMsgOK));
            AppendCommandMsgHandler(IDCANCEL, CommandMsgHandler(this, &Dialog::OnMsgCancel));
        }

        ~Dialog()
        {

        }

    protected:
        static LPCTSTR GetClassName()
        {
            return _T("xlDialog");
        }
        
    public:
        bool Create(int nWidth = CW_USEDEFAULT,
                    int nHeight = CW_USEDEFAULT,
                    Window *pParent = nullptr,
                    LPCTSTR lpszClassName = GetClassName(),
                    DWORD dwStyle = WS_POPUPWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                    DWORD dwExStyle = WS_EX_CONTROLPARENT,
                    HMENU hMenu = nullptr,
                    HINSTANCE hInstance = nullptr)
        {
            RECT rect = {};

            if (pParent != nullptr)
            {
                pParent->GetWindowRect(&rect);
            }
            else
            {
                rect.right  = GetSystemMetrics(SM_CXSCREEN);
                rect.bottom = GetSystemMetrics(SM_CYSCREEN);
            }

            int x = (rect.left + rect.right - nWidth)  / 2;
            int y = (rect.top + rect.bottom - nHeight) / 2;

            if (!Window::Create(x,
                                y,
                                nWidth,
                                nHeight,
                                pParent,
                                lpszClassName,
                                dwStyle,
                                dwExStyle,
                                hMenu,
                                hInstance))
            {
                return false;
            }

            return true;
        }

    public:

        INT_PTR DoModal()
        {
            HWND hOwner = GetWindow(GW_OWNER);

            if (hOwner != nullptr)
            {
                ::EnableWindow(hOwner, FALSE);
            }

            ShowWindow(SW_SHOW);

            INT_PTR nModalResult = RunModalLoop();

            Destroy();

            if (hOwner != nullptr)
            {
                ::EnableWindow(hOwner, TRUE);
                ::BringWindowToTop(hOwner);
            }

            return nModalResult;
        }

        void EndDialog(INT_PTR nModulResult)
        {
            m_nModalResult = nModulResult;
            m_bInModal = false;
        }

    private:

        INT_PTR RunModalLoop()
        {
            m_nModalResult = -1;
            m_bInModal = true;

            MSG msg = {};

            while (m_bInModal)
            {
                do
                {
                    if (!::GetMessage(&msg, NULL, NULL, NULL))
                    {
                        EndDialog(-1);
                    }

                    if (!IsDialogMessage(m_hWnd, &msg))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }

                } while (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE));
            }

            return m_nModalResult;
        }

    private:
        bool    m_bInModal;<commit_msg>fix a bug in model dialog loop<commit_after>//------------------------------------------------------------------------------
//
//    Copyright (C) Streamlet. All rights reserved.
//
//    File Name:   xlDialog.h
//    Author:      Streamlet
//    Create Time: 2011-03-12
//    Description: 
//
//    Version history:
//
//
//
//------------------------------------------------------------------------------

#ifndef __XLDIALOG_H_FB784537_F2A1_43D5_B337_1882AB81D19F_INCLUDED__
#define __XLDIALOG_H_FB784537_F2A1_43D5_B337_1882AB81D19F_INCLUDED__


#include <xl/Win32/GUI/xlWindow.h>

namespace xl
{
    class Dialog : public Window
    {
    public:
        Dialog() :
            m_bInModal(false),
            m_nModalResult(-1)
        {
            AppendMsgHandler(WM_CREATE,       MsgHandler(this, &Dialog::OnMsgCreate));
            AppendMsgHandler(WM_INITDIALOG,   MsgHandler(this, &Dialog::OnMsgInitDialog));
            AppendMsgHandler(WM_ERASEBKGND,   MsgHandler(this, &Dialog::OnMsgEraseBackground));
            AppendMsgHandler(WM_CLOSE,        MsgHandler(this, &Dialog::OnMsgClose));

            AppendCommandMsgHandler(IDOK,     CommandMsgHandler(this, &Dialog::OnMsgOK));
            AppendCommandMsgHandler(IDCANCEL, CommandMsgHandler(this, &Dialog::OnMsgCancel));
        }

        Dialog(HWND hWnd) :
            Window(hWnd),
            m_bInModal(false),
            m_nModalResult(-1)
        {
            AppendMsgHandler(WM_CREATE,       MsgHandler(this, &Dialog::OnMsgCreate));
            AppendMsgHandler(WM_INITDIALOG,   MsgHandler(this, &Dialog::OnMsgInitDialog));
            AppendMsgHandler(WM_ERASEBKGND,   MsgHandler(this, &Dialog::OnMsgEraseBackground));
            AppendMsgHandler(WM_CLOSE,        MsgHandler(this, &Dialog::OnMsgClose));

            AppendCommandMsgHandler(IDOK,     CommandMsgHandler(this, &Dialog::OnMsgOK));
            AppendCommandMsgHandler(IDCANCEL, CommandMsgHandler(this, &Dialog::OnMsgCancel));
        }

        ~Dialog()
        {

        }

    protected:
        static LPCTSTR GetClassName()
        {
            return _T("xlDialog");
        }
        
    public:
        bool Create(int nWidth = CW_USEDEFAULT,
                    int nHeight = CW_USEDEFAULT,
                    Window *pParent = nullptr,
                    LPCTSTR lpszClassName = GetClassName(),
                    DWORD dwStyle = WS_POPUPWINDOW | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                    DWORD dwExStyle = WS_EX_CONTROLPARENT,
                    HMENU hMenu = nullptr,
                    HINSTANCE hInstance = nullptr)
        {
            RECT rect = {};

            if (pParent != nullptr)
            {
                pParent->GetWindowRect(&rect);
            }
            else
            {
                rect.right  = GetSystemMetrics(SM_CXSCREEN);
                rect.bottom = GetSystemMetrics(SM_CYSCREEN);
            }

            int x = (rect.left + rect.right - nWidth)  / 2;
            int y = (rect.top + rect.bottom - nHeight) / 2;

            if (!Window::Create(x,
                                y,
                                nWidth,
                                nHeight,
                                pParent,
                                lpszClassName,
                                dwStyle,
                                dwExStyle,
                                hMenu,
                                hInstance))
            {
                return false;
            }

            return true;
        }

    public:

        INT_PTR DoModal()
        {
            HWND hOwner = GetWindow(GW_OWNER);

            if (hOwner != nullptr)
            {
                ::EnableWindow(hOwner, FALSE);
            }

            ShowWindow(SW_SHOW);

            INT_PTR nModalResult = RunModalLoop();

            Destroy();

            if (hOwner != nullptr)
            {
                ::EnableWindow(hOwner, TRUE);
                ::BringWindowToTop(hOwner);
            }

            return nModalResult;
        }

        void EndDialog(INT_PTR nModulResult)
        {
            m_nModalResult = nModulResult;
            m_bInModal = false;
        }

    private:

        INT_PTR RunModalLoop()
        {
            m_nModalResult = -1;
            m_bInModal = true;

            MSG msg = {};

            while (m_bInModal)
            {
                while (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
                {
                    if (msg.message == WM_QUIT)
                    {
                        EndDialog(IDCANCEL);
                        break;
                    }

                    if (!::GetMessage(&msg, NULL, NULL, NULL))
                    {
                        EndDialog(IDCANCEL);
                        break;
                    }

                    if (!IsDialogMessage(m_hWnd, &msg))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }

                }

                Sleep(0);
            }

            return m_nModalResult;
        }

    private:
        bool    m_bInModal;
{'fraction_non_alphanumeric': 0.11800197823936696, 'fraction_numerical': 0.011869436201780416, 'mean_word_length': 1.1725397507520412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7466849', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2658, 'n_words': 439}