📄 pphtmldrawer.cpp
字号:
#include "stdafx.h"
#include "PPHtmlDrawer.h"
#include "atlconv.h" // for Unicode conversion - requires #include <afxdisp.h> // MFC OLE automation classes
#include <shellapi.h>
#pragma comment(lib, "comctl32.lib")
/*
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
*/
#define PPHTMLDRAWER_NO_HOVERLINK -2 //A hot area is not exist under the cursor
#define PPHTMLDRAWER_BREAK_CHARS _T(" -.,!:;)}]?") //A set of the chars to break line in the text wrap mode
enum {
MODE_DRAW = 0,
MODE_FIRSTPASS,
MODE_SECONDPASS
};
/*
#define m_szOffsetShadow.cx 4 //
#define m_szOffsetShadow.cy 4 //
#define m_szDepthShadow.cx 7 //
#define m_szDepthShadow.cy 7 //
#define PPHTMLDRAWER_SHADOW_COLOR RGB (64, 64, 64) //A gradient shadow's color
*/
/////////////////////////////////////////////////////////////////////////////
// CPPHtmlDrawer
CPPHtmlDrawer::CPPHtmlDrawer()
{
m_nNumPass = MODE_FIRSTPASS;
m_hInstDll = NULL;
m_bFreeInstDll = FALSE;
m_hDC = NULL;
m_hImageList = NULL;
m_csCallbackRepaint.hWnd = NULL;
m_csCallbackRepaint.nMessage = 0;
m_csCallbackRepaint.lParam = 0;
m_csCallbackRepaint.wParam = 0;
m_csCallbackLink.hWnd = NULL;
m_csCallbackLink.nMessage = 0;
m_csCallbackLink.lParam = 0;
m_csCallbackLink.wParam = 0;
// m_clrShadow = PPHTMLDRAWER_SHADOW_COLOR;
m_hLinkCursor = NULL; // No cursor as yet
m_nHoverIndexLink = PPHTMLDRAWER_NO_HOVERLINK;
SetListOfTags();
SetListSpecChars();
SetTableOfColors();
SetDefaultCursor();
EnableEscapeSequences();
SetMaxWidth(0);
// EnableTextWrap(FALSE); //A text warpping was disabled by default
// EnableTextWrap(TRUE); //A text warpping was disabled by default
SetImageShadow(4, 4);
SetTabSize(32);
SetDefaultCssStyles();
EnableOutput();
SetDisabledColor(::GetSysColor(COLOR_BTNSHADOW));
}
CPPHtmlDrawer::~CPPHtmlDrawer()
{
SetResourceDll(NULL);
if (NULL != m_hLinkCursor)
{
::DestroyCursor(m_hLinkCursor);
m_hLinkCursor = NULL;
}
if (NULL != m_hImageList)
::DeleteObject(m_hImageList);
}
void CPPHtmlDrawer::EnableOutput(BOOL bEnable /* = TRUE */)
{
m_bIsEnable = bEnable;
} //End of EnableOutput
void CPPHtmlDrawer::SetDisabledColor(COLORREF color)
{
m_crDisabled = color;
}
HICON CPPHtmlDrawer::GetIconFromResources(DWORD dwID, int nWidth /* = 0 */, int nHeight /* = 0 */) const
{
if (0 == dwID) return NULL;
// Find correct resource handle
#ifdef _MFC_VER
HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(dwID), RT_GROUP_ICON);
#else
HINSTANCE hInstResource = ::GetModuleHandle(NULL);
#endif
// Set icon when the mouse is IN the button
HICON hIcon = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(dwID), IMAGE_ICON, nWidth, nHeight, LR_DEFAULTCOLOR);
return hIcon;
}
HICON CPPHtmlDrawer::GetIconFromFile(LPCTSTR lpszPath, int nWidth /* = 0 */, int nHeight /* = 0 */) const
{
HICON hIcon = (HICON)::LoadImage(NULL, lpszPath, IMAGE_ICON, nWidth, nHeight, LR_LOADFROMFILE | LR_DEFAULTCOLOR);
return hIcon;
}
HICON CPPHtmlDrawer::GetIconFromDll(DWORD dwID, int nWidth /* = 0 */, int nHeight /* = 0 */, LPCTSTR lpszPathDll /* = NULL */) const
{
if (0 == dwID) return NULL;
HICON hIcon = NULL;
HINSTANCE hInstDll = NULL;
BOOL bNewDll = FALSE;
if (NULL == lpszPathDll)
{
if (NULL != m_hInstDll)
hInstDll = m_hInstDll;
}
else
{
//Load New Library
hInstDll = ::LoadLibraryEx(lpszPathDll, NULL, 0);
if (NULL != hInstDll)
bNewDll = TRUE;
}
if (NULL != hInstDll)
{
hIcon = (HICON)::LoadImage(hInstDll, MAKEINTRESOURCE(dwID), IMAGE_ICON, nWidth, nHeight, LR_DEFAULTCOLOR);
if (bNewDll)
::FreeLibrary(hInstDll);
}
return hIcon;
}
HBITMAP CPPHtmlDrawer::GetBitmapFromResources(DWORD dwID) const
{
if (0 == dwID) return NULL;
// Find correct resource handle
#ifdef _MFC_VER
HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(dwID), RT_BITMAP);
#else
HINSTANCE hInstResource = ::GetModuleHandle(NULL);
#endif
// Load bitmap
HBITMAP hBitmap = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(dwID), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
return hBitmap;
}
HBITMAP CPPHtmlDrawer::GetBitmapFromFile(LPCTSTR lpszPath) const
{
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, lpszPath, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
return hBitmap;
}
HBITMAP CPPHtmlDrawer::GetBitmapFromDll(DWORD dwID, LPCTSTR lpszPathDll /* = NULL */) const
{
if (0 == dwID) return NULL;
HBITMAP hBitmap = NULL;
HINSTANCE hInstDll = NULL;
BOOL bNewDll = FALSE;
if (NULL == lpszPathDll)
{
if (NULL != m_hInstDll)
hInstDll = m_hInstDll;
}
else
{
//Load New Library
hInstDll = ::LoadLibraryEx(lpszPathDll, NULL, 0);
if (NULL != hInstDll)
bNewDll = TRUE;
}
if (NULL != hInstDll)
{
hBitmap = (HBITMAP)::LoadImage(hInstDll, MAKEINTRESOURCE(dwID), IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
if (bNewDll)
::FreeLibrary(hInstDll);
}
return hBitmap;
}
CPPString CPPHtmlDrawer::GetStringFromResource(DWORD dwID) const
{
if (0 == dwID) return _T("");
CPPString str;
str.LoadString(dwID);
return str;
}
CPPString CPPHtmlDrawer::GetStringFromDll(DWORD dwID, LPCTSTR lpszPathDll /* = NULL */) const
{
if (0 == dwID) return _T("");
CPPString str = _T("");
HINSTANCE hInstDll = NULL;
BOOL bNewDll = FALSE;
if (NULL == lpszPathDll)
{
if (NULL != m_hInstDll)
hInstDll = m_hInstDll;
}
else
{
//Load New Library
hInstDll = ::LoadLibraryEx(lpszPathDll, NULL, 0);
if (NULL != hInstDll)
bNewDll = TRUE;
}
if (NULL != hInstDll)
{
#ifdef _UNICODE
#define CHAR_FUDGE 1 // one TCHAR unused is good enough
#else
#define CHAR_FUDGE 2 // two BYTES unused for case of DBC last char
#endif
// try fixed buffer first (to avoid wasting space in the heap)
TCHAR szTemp[256];
DWORD dwLen = ::LoadString(hInstDll, dwID, szTemp, (sizeof(szTemp) * sizeof(TCHAR)));
// If resource not found (or ::LoadString failure)
if (0 != dwLen)
{
if ((sizeof(szTemp) * sizeof(TCHAR)) - dwLen > CHAR_FUDGE)
{
str = szTemp;
} // if
else
{
// try buffer size of 512, then larger size until entire string is retrieved
int nSize = 256;
do
{
nSize += 256;
dwLen = ::LoadString(hInstDll, dwID, str.GetBuffer(nSize-1), nSize);
} while (nSize - dwLen <= CHAR_FUDGE);
str.ReleaseBuffer();
}
#undef CHAR_FUDGE
}
if (bNewDll)
::FreeLibrary(hInstDll);
}
return str;
}
///////////////////////////////////////////////////////////
// Get tooltip string for menu and toolbar items from the
// resources of the application.
//
// Parameters:
// nID - Resource ID of the string
// nNumParam - Which parameter will gets:
// 0=long,
// 1=short,
// 2=disable
//
//
// Format prompt string: long prompt \n short prompt \n disable prompt
////////////////////////////////////////////////////////////
CPPString CPPHtmlDrawer::GetResCommandPrompt(UINT nID, UINT nNumParam /* = 0 */)
{
CPPString str = GetStringFromResource(nID);
if (!str.IsEmpty())
{
int nFirst = 0;
int nLast = nFirst;
UINT nCount = 0;
while ((nCount <= nNumParam) && (nFirst < str.GetLength()))
{
nLast = str.Find(_T('\n'), nFirst);
if (nLast < 0)
{
//Char wasn't found
if (nCount == nNumParam)
str = str.Mid(nFirst, str.GetLength() - nFirst);
else
str.Empty();
return str;
}
else
{
//Char was found
if (nCount == nNumParam)
{
str = str.Mid(nFirst, nLast - nFirst);
return str;
}
else
{
nFirst = nLast + 1;
} //if
} //if
nCount ++;
} //while
} //if
return _T("");
} //End of GetResCommandPrompt
/////////////////////////////////////////////////////////////////////////////
//
void CPPHtmlDrawer::SetListSpecChars()
{
AddSpecChar(_T("&"), _T("&")); // ampersand
AddSpecChar(_T("•"), _T("\x95\0")); // bullet NOT IN MS SANS SERIF
AddSpecChar(_T("©"), _T("\xA9\0")); // copyright
// AddSpecChar(_T("€"), _T("\x80\0")); // euro sign IN NOT CYRILLIC FONTS
AddSpecChar(_T("€"), _T("\x88\0")); // euro sign IN CYRILLIC FONTS
AddSpecChar(_T(">"), _T(">")); // greater than
AddSpecChar(_T("¿"), _T("\xBF\0")); // inverted question mark
AddSpecChar(_T("<"), _T("<<")); // less than
AddSpecChar(_T(" "), _T(" ")); // nonbreaking space
AddSpecChar(_T("¶"), _T("\xB6\0")); // paragraph sign
AddSpecChar(_T("£"), _T("\xA3\0")); // pound sign
AddSpecChar(_T("""), _T("\"")); // quotation mark
AddSpecChar(_T("®"), _T("\xAE\0")); // registered trademark
AddSpecChar(_T("™"), _T("\x99\0")); // trademark NOT IN MS SANS SERIF
} //End of SetListSpecChars
void CPPHtmlDrawer::AddSpecChar(LPCTSTR lpszAlias, LPCTSTR lpszValue)
{
iter_mapStyles iter = m_mapSpecChars.find(lpszAlias);
if (iter != m_mapSpecChars.end())
iter->second = lpszValue; //Modifies
else
m_mapSpecChars.insert(std::make_pair(lpszAlias, lpszValue)); //Add new
} //End of AddSpecialChar
void CPPHtmlDrawer::ReplaceSpecChars()
{
CPPString sAlias, sValue;
for (iter_mapStyles iter = m_mapSpecChars.begin(); iter != m_mapSpecChars.end(); ++iter)
{
sAlias = iter->first;
sValue = iter->second;
m_csHtmlText.Replace(sAlias, sValue);
} //for
m_csHtmlText.Remove(_T('\r'));
if (!m_bEnableEscapeSequences)
{
//ENG: Remove escape sequences
//RUS: 愉嚯
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -