// DUiEditBox.cpp: implementation of the CDUiEditBox class.
//
//////////////////////////////////////////////////////////////////////

#include "DUiMan.h"
#include "DirectXHelpers.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDUiEditBox::CDUiEditBox()
{
    SetObjectType(DUI_EDITBOX);

    m_dwTextArraySize = 256;
    m_wszText = (WCHAR *)malloc(sizeof(WCHAR) * m_dwTextArraySize);
    m_wszText[0] = 0;
    m_dwTextLength = 0;
    m_dwCursorPos = 0;

//    m_dwFontWidth = NULL;
//    m_dwFontHeight = NULL;

}

CDUiEditBox::~CDUiEditBox()
{
}

DWORD CDUiEditBox::Display(BOOL bRefresh)
{
    DWORD       dwRetVal = 0;
    HRESULT     hr;
    DDBLTFX     ddbltfx;
    RECT        rSource;
    rSource.left = m_xPos;
    rSource.top = m_yPos;
    rSource.right = m_xPos + m_dwWidth;
    rSource.bottom = m_yPos + m_dwHeight;

    memset(&ddbltfx,0,sizeof(DDBLTFX));
    ddbltfx.dwSize = sizeof(DDBLTFX);
    //draw the outline in a color between the color of the background 
    //and either white or black depending on which is furthest
    ddbltfx.dwFillColor = GetFadeColor(m_dwBackgroundColor,5);
    hr = m_pddsMain->Blt(&rSource, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);

    rSource.left += 1;
    rSource.top += 1;
    rSource.right -= 1;
    rSource.bottom -= 1;

    //draw the background
    ddbltfx.dwFillColor = m_dwBackgroundColor;
    hr = m_pddsMain->Blt(&rSource, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);

    return dwRetVal;
}

DWORD CDUiEditBox::DisplayText(BOOL bRefresh)
{
    return CDUiText::DisplayText(bRefresh);
    DWORD dwRetVal = 0;
    HDC         dc;
    WCHAR       wszTemp[512];
    RECT        rSource;
    rSource.left = m_xPos - 1;
    rSource.top = m_yPos - 1;
    rSource.right = m_xPos + m_dwWidth;
    rSource.bottom = m_yPos + m_dwHeight;

    if (g_pDUiManager->GetDC(&dc))
    {
        SelectObject(dc,m_pFont->GetFont());
        SetBkMode(dc,OPAQUE);
        SetBkColor(dc,ColorToColorRef(m_dwBackgroundColor));
        SetTextColor(dc,ColorToColorRef(m_dwFontColor));
        
        //hack see what characters should actually be visible
        wcscpy(wszTemp,m_wszText);
        ExtTextOut(dc,rSource.left+4 ,rSource.top+5,ETO_OPAQUE,NULL,wszTemp,wcslen(wszTemp),NULL);
    }

    return dwRetVal;
}

DWORD CDUiEditBox::OnChar(UINT nChar, UINT nFlags)
{
    HDC dc;
    HRESULT hr;
    BOOL bCanAddChar = TRUE;

    if (IsPrintable(nChar))
    {
        if ((m_dwTextLength + 2) < m_dwTextArraySize)
        {
            m_dwTextArraySize += 40;
            m_wszText = (WCHAR *)realloc(m_wszText,sizeof(WCHAR) * m_dwTextArraySize);
        }
        m_wszText[m_dwTextLength++] = (WCHAR) nChar;
        m_wszText[m_dwTextLength] = 0;
        hr = m_pddsMain->GetDC(&dc);
        if (hr == S_OK)
        {
            SIZE size;
            SelectObject(dc,m_pFont->GetFont());
            if (GetTextExtentPoint32(dc,m_wszText,m_dwTextLength,&size))
            {
                if ((size.cx + 10) > (int)m_dwWidth)
                    bCanAddChar = FALSE;
            }
            m_pddsMain->ReleaseDC(dc);
        }

        //hack add some sort of overflow method
        //just back out the character for now
        if (!bCanAddChar)
        {
            m_dwTextLength--;
            m_wszText[m_dwTextLength] = 0;
        }
    }
    else if (nChar == VK_RETURN)
    {


    } 
    else if (nChar == VK_BACK)
    {
        if (m_dwTextLength)
        {
            m_dwTextLength--;
            m_wszText[m_dwTextLength] = 0;
        }
    }
    
    return 0;
}
/*
void CDUiEditBox::GetText(WCHAR *wszText)
{
    wcscpy(wszText,m_wszText);
}

void CDUiEditBox::SetText(WCHAR *wszText)
{
    if ((wcslen(wszText) + 1) < m_dwTextArraySize)
    {
        wcscpy(m_wszText,wszText);
    }
    else
    {
        m_dwTextArraySize = wcslen(wszText) * 2;
        m_wszText = (WCHAR *) realloc(m_wszText,m_dwTextArraySize);
        m_dwTextLength = m_dwTextArraySize / 2;
        wcscpy(m_wszText,wszText);
    }
}
*/