void CPokeMiniEmulatorDlg::OnCancel()
{
    OnRomClose();
    SafeFreePointer(m_screenDev.pData);
    SafeFreePointer(m_dev[1].pData);
    SafeFreePointer(m_dev[0].pData);
    GsReleasePokeMiniEmuHandle(m_pSystem);

    if (GS_NULL != m_pDirectSoundBuffer) { m_pDirectSoundBuffer->Release(); }
    if (GS_NULL != m_pDirectSound) { m_pDirectSound->Release(); }

    CDialogEx::OnCancel();
}
GS_VOID CPokeMiniEmulatorDlg::OnRomClose()
{
    if (EMU_STOP != m_state)
    {
        m_state = EMU_STOP;
        WaitForSingleObject(m_emuThread, INFINITE);
        m_pDirectSoundBuffer->Stop();
    }

    if (GS_NULL != m_pSystem)
    {
        m_pSystem->Destroy();
    }

    SafeFreePointer(m_pSaveData);
    SafeFreePointer(m_pRomData);
}
void UIAbstractText::SetText(const wstring& wstrTxt)
{
    char* strTmpText = EncodingUtil::WCharToChar(wstrTxt.c_str());
    if(NULL == strTmpText)
    {
        return;
    }
    m_TextDrawer.SetText(strTmpText);
    SafeFreePointer(strTmpText);
}
void UIAbstractText::SetText(SPtr<DkBufferBase> spText)
{
    CString m_strText;
    if(spText)
    {
        DebugPrintf(DLC_UITEXT,"spText");
        m_strText = CString((const CHAR *)spText->pbBuffer, spText->nLength);
        DebugPrintf(DLC_UITEXT,"spText over");
    }
    else
    {
        DebugPrintf(DLC_UITEXT,"else");
        BYTE8 *p = new BYTE8[4];
        if(p)
        {
            memset(p, 0, 4);
            DkBuffer *pBuf = new DkBuffer(p, 4);
            if(NULL == pBuf)
            {
                delete[] p;
                return;
            }

            SPtr<DkBufferBase>  ptr(pBuf);
            if(ptr)
            {
                m_strText = CString((const CHAR *)ptr->pbBuffer, ptr->nLength);
            }

        }
        DebugPrintf(DLC_UITEXT,"else over");
    }
    DebugPrintf(DLC_UITEXT,"char to wchar");
    DK_WCHAR* pTmpText = EncodingUtil::CharToWChar(m_strText);

    if(NULL == pTmpText)
    {
        return;
    }

    SafeFreePointer(pTmpText);
    m_TextDrawer.SetText(m_strText.GetBuffer());

    DebugPrintf(DLC_UITEXT,"setText over");
}
void UIAbstractText::SetFontPath()
{
    DKFont* pChineseFont (NULL);
    wchar_t* wcChineseFontName = EncodingUtil::CharToWChar(SystemSettingInfo::GetInstance()->GetDefaultFontByCharset(DK_CHARSET_GB));
    wstring pChineseFontName = wcChineseFontName;
    SafeFreePointer(wcChineseFontName);

    pChineseFont = g_pFontManager->GetFontForFaceName (pChineseFontName);
    m_TextDrawer.SetGbFontFile(pChineseFont->GetFontPath());
    wstring wstrDefaultEnglishFont = L"Lingoes Unicode";
    DKFont* pEnglishFont (NULL);
    pEnglishFont = g_pFontManager->GetFontForFaceName (wstrDefaultEnglishFont);
    if(NULL == pEnglishFont)
    {
        pEnglishFont = pChineseFont;
    }
    m_TextDrawer.SetAnsiFontFile(pEnglishFont->GetFontPath());
}
bool MobiBookReader::CloseDocument()
{
    SafeDeletePointer(m_pBookTextController);
    SafeFreePointer(m_tCurPageImage.pbData);
	for(size_t i = 0; i < m_vTOCList.size(); i++)
	{
		SafeDeletePointer(m_vTOCList[i]);
	}
    m_vTOCList.clear();
    m_dGraphicsGamma = 1.0;
    m_dTextGamma = 1.0;
    m_lLogicPageCount = 0;
    m_uFileLength = 0;
    DestroyCurPage();
    if (m_pBook)
    {
        DKM_CloseDocument(m_pBook);
        m_pBook = NULL;
    }

    return true;
};
bool MobiBookReader::RenderCurPage()
{
    if (NULL == m_pBook || NULL == m_pPage)
    {
        DebugPrintf(DLC_ZHAIGH, "MobiBookReader::%s() m_pPage is NULL", __FUNCTION__);
        return false;
    }

    DebugPrintf(DLC_ZHAIGH, "MobiBookReader::%s() Start", __FUNCTION__);
    DKE_SetGBToBig5(m_familiarToTraditional);
    // 当前页面的图像信息设置,初始为32色,需要经过BMPProcessor的处理
    SafeFreePointer(m_tCurPageImage.pbData);
    m_tCurPageImage.iColorChannels = 4;
    m_tCurPageImage.iWidth         = (int)m_parseOption.pageBox.Width();
    m_tCurPageImage.iHeight        = (int)m_parseOption.pageBox.Height();
    m_tCurPageImage.iStrideWidth   = m_tCurPageImage.iWidth * m_tCurPageImage.iColorChannels;

    long iDataLen = m_tCurPageImage.iStrideWidth * m_tCurPageImage.iHeight;
    m_tCurPageImage.pbData = DK_MALLOCZ_OBJ_N(BYTE8, iDataLen);
    if (NULL == m_tCurPageImage.pbData)
    {
        DebugPrintf(DLC_ZHAIGH, "MobiBookReader::%s() m_tCurPageImage.pbData is NULL", __FUNCTION__);
        return false;
    }

    // DEV信息根据DK_IMAGE来生成
    DK_BITMAPBUFFER_DEV dev;
    dev.lWidth       = m_tCurPageImage.iWidth;
    dev.lHeight      = m_tCurPageImage.iHeight;
    dev.nPixelFormat = DK_PIXELFORMAT_RGB32;
    dev.lStride      = m_tCurPageImage.iStrideWidth;
    dev.nDPI         = RenderConst::SCREEN_DPI;
    dev.pbyData      = m_tCurPageImage.pbData;

    DK_RENDERGAMMA  gma;
    gma.dTextGamma = m_dTextGamma;

    DK_FLOWRENDEROPTION renderOption;
    renderOption.nDeviceType = DK_OUTPUTDEV_BITMAPBUFFER;
    renderOption.pDevice     = &dev;
    renderOption.gamma       = gma;
	renderOption.fontSmoothType = m_eFontSmoothType;
    if (m_bIsInstant)
    {
        renderOption.fontSmoothType = DK_FONT_SMOOTH_BINARY;
    }
    
    memset(dev.pbyData, 0xFF, iDataLen); //画布颜色变为白色
    DK_FLOWRENDERRESULT clsDKERenderResult;

    DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage Render!!!");
    if (DK_FAILED(m_pPage->Render(renderOption, &clsDKERenderResult)))
    {
        DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage Render ERROR!!!");
        return false;
    }

    DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage Render SUCCESS!!!");

    // 抖动当前图像
    DitherImage();

    if (m_pBookTextController)
    {
        DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage m_pBookTextController Initialize!!!");
        m_pBookTextController->Initialize<IDKMPageTraits>(m_pBook, m_pPage);
        m_pBookTextController->SetModeController(GetPdfModeController());
    }

    std::vector<IBMPProcessor *>::iterator vi;
    for (vi = m_lstBMPProcessor.begin(); vi != m_lstBMPProcessor.end(); ++vi)
    {
        DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage Process Image!!!");
        if (NULL == (*vi) || !(*vi)->Process(&m_tCurPageImage))
        {
            DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage Process ERROR!!!");
            return false;
        }
    }

    DebugPrintf(DLC_ZHAIGH, "MobiBookReader::RenderPage FINISHED!!!");
    return true;
}