Skip to content

Commit

Permalink
SetText/GetText优化 (#12)
Browse files Browse the repository at this point in the history
* SetTextA/GetTextA, in-progress

* Unnececcary + 1

* DUIRichEdit::GetLineText, 确保当字符小于sizeof(int) -1 时,分配足够空间,避免踩无效内存

* 优化,避免不必要的buffer深拷贝

* Use external to manage dependeicies

* 利用函数重载

* fix vs2013 compile issue

* 避免不必要的强转

* Add c++ standard choose support, _CXX_STD=17

* C++17 compiler support

* Implement DM::CStringW to std::string for ntcvt

* Explicit match c style string

* Rename name

* Define WIN32_LEAN_AND_MEAN
  • Loading branch information
halx99 committed Jul 5, 2020
1 parent 58395c6 commit a73dc10
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 111 deletions.
33 changes: 32 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ OPTION(USE_DMSKIA_ "DM user skia render draw" OFF)
LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Tools/CMake)
INCLUDE(PrecompiledHeader)

# --- The compiler flags
message("-- Building REDM with cpp${_CXX_STD} support")
if ( IOS )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fembed-bitcode" CACHE INTERNAL "CMAKE_CXX_FLAGS")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fembed-bitcode" CACHE INTERNAL "CMAKE_C_FLAGS")
endif ()
if (NOT WIN32 OR CYGWIN)
if (_CXX_STD EQUAL 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z" CACHE INTERNAL "CMAKE_CXX_FLAGS")
if ( IOS )
# Aligned deallocation function of type 'void (void *, std::align_val_t) noexcept' is only available on iOS 11 or newer
# most of time, low level malloc will alloc a aligned address for new operator,
# so it's ok to add -faligned-allocation, certainly, still need find a ios9.0 device
# to test does it works well?
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-allocation" CACHE INTERNAL "CMAKE_CXX_FLAGS")
endif()
elseif(_CXX_STD EQUAL 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y" CACHE INTERNAL "CMAKE_CXX_FLAGS")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" CACHE INTERNAL "CMAKE_CXX_FLAGS")
endif()
else()
if(_CXX_STD EQUAL 17)
# target_compile_features(REDM PUBLIC cxx_std_17) works
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17" CACHE INTERNAL "CMAKE_CXX_FLAGS")
elseif(_CXX_STD EQUAL 14)
# target_compile_features(REDM PUBLIC cxx_std_14) not works
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++14" CACHE INTERNAL "CMAKE_CXX_FLAGS")
endif()
endif()

# 增加子文件夹
ADD_SUBDIRECTORY(${PROJDIR}/DmMain)
ADD_SUBDIRECTORY(${PROJDIR}/Samples/DMDemo)
Expand All @@ -77,4 +108,4 @@ ADD_SUBDIRECTORY(${PROJDIR}/3rdParty/lua)
ADD_SUBDIRECTORY(${PROJDIR}/3rdParty/scintilla)
if(USE_DMSKIA_)
ADD_SUBDIRECTORY(${PROJDIR}/3rdParty/skia)
endif()
endif()
1 change: 1 addition & 0 deletions DmMain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ target_include_directories(DmMain
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/Modules/Skin
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/Modules/Task
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/Widgets
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../External
)
SET_TARGET_PROPERTIES(DmMain PROPERTIES OUTPUT_NAME "DmMain")

Expand Down
135 changes: 67 additions & 68 deletions DmMain/inc/Common/Template/DMStringT.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#define TSTRING_PADDING 0
#endif

#include "ntcvt/ntcvt.hpp"

namespace DM
{
__pragma(warning(push))
Expand Down Expand Up @@ -1676,74 +1678,6 @@ namespace DM
typedef CStringA CStringT;
#endif

static CStringW DMA2W(const CStringA &str, UINT CodePage=CP_ACP)
{
int nSize = ::MultiByteToWideChar(CodePage, 0, str, str.GetLength(), NULL, 0);
if (nSize>0)
{
wchar_t *pBuf=new wchar_t[nSize];
::MultiByteToWideChar(CodePage, 0, str, str.GetLength(), pBuf, nSize);
CStringW strw(pBuf, nSize);
delete []pBuf;
pBuf = NULL;
return strw;
}
return L"";
}

/// <summary>
/// 用于脚本中char*直接转CStringW
/// </summary>
static CStringW DMCA2W(LPCSTR lpsz, UINT CodePage=CP_ACP)
{
CStringA str = lpsz;
CStringW strw = DMA2W(str, CodePage);
return strw;
}

static CStringA DMW2A(const CStringW &str, UINT CodePage=CP_ACP)
{
int nSize = ::WideCharToMultiByte(CodePage,0,str, str.GetLength(), NULL, 0, NULL, NULL);
if (nSize>0)
{
char *pBuf = new char[nSize];
::WideCharToMultiByte(CodePage,0,str, str.GetLength(), pBuf, nSize, NULL, NULL);
CStringA stra(pBuf,nSize);
delete []pBuf;
pBuf = NULL;
return stra;
}
return "";
}

static CStringW DMW2W(const CStringW &str)
{
return str;
}

static CStringA DMA2A(const CStringA &str, UINT CodePageFrom=CP_UTF8, UINT CodePageTo=CP_ACP)
{
if (CodePageFrom == CodePageTo)
{
return str;
}
CStringW strw = DMA2W(str,CodePageFrom);
return DMW2A(strw, CodePageTo);
}


#ifdef UNICODE
#define DMA2T DMA2W
#define DMW2T DMW2W
#define DMT2A DMW2A
#define DMT2W DMW2W
#else
#define DMA2T DMA2A
#define DMW2T DMW2A
#define DMT2A DMA2A
#define DMT2W DMA2W
#endif

template< typename T >
class SStringElementTraits
{
Expand Down Expand Up @@ -1807,3 +1741,68 @@ namespace DM

}//end of namespace

namespace ntcvt {
namespace buffer_traits {
inline char* inplaced(DM::CStringA& str, int size) {
return str.GetBufferSetLength(size);
}
inline wchar_t* inplaced(DM::CStringW& str, int size) {
return str.GetBufferSetLength(size);
}
}
inline std::string from_chars(const DM::CStringW& wcb, UINT cp = CP_ACP)
{
return wcbs2a<std::string>((LPCWSTR)wcb, wcb.GetLength(), cp);
}
}

namespace DM {
/// <summary>
/// 用于脚本中char*直接转CStringW
/// </summary>
static CStringW DMCA2W(LPCSTR lpsz, int len /*=-1*/, UINT CodePage/* = CP_ACP*/)
{
return ntcvt::mcbs2w<CStringW>(lpsz, len, CodePage);
}

static CStringW DMA2W(const CStringA& str, UINT CodePage = CP_ACP)
{
return DMCA2W((LPCSTR)str, str.GetLength(), CodePage);
}

static CStringA DMWC2A(LPCWSTR lpsz, int len /*=-1*/, UINT CodePage /*=CP_ACP*/)
{
return ntcvt::wcbs2a<CStringA>(lpsz, len, CodePage);
}

static CStringA DMW2A(const CStringW& str, UINT CodePage = CP_ACP)
{
return DMWC2A((LPCWSTR)str, str.GetLength(), CodePage);
}

static CStringW DMW2W(const CStringW& str)
{
return str;
}

static CStringA DMA2A(const CStringA& str, UINT CodePageFrom = CP_UTF8, UINT CodePageTo = CP_ACP)
{
if (CodePageFrom == CodePageTo)
return str;
CStringW strw = DMA2W(str, CodePageFrom);
return DMW2A(strw, CodePageTo);
}


#ifdef UNICODE
#define DMA2T DMA2W
#define DMW2T DMW2W
#define DMT2A DMW2A
#define DMT2W DMW2W
#else
#define DMA2T DMA2A
#define DMW2T DMW2A
#define DMT2A DMA2A
#define DMT2W DMA2W
#endif
}
10 changes: 7 additions & 3 deletions DmMain/inc/Widgets/DUIEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ namespace DM

public:
//---------------------------------------------------
// Function Des: 对外接口,参看afxcmn.inl,和CRichEditCtrl保持一致
// Function Des: 对外接口
//---------------------------------------------------
void SetText(const CStringW& text) override;
CStringW GetText() const override;

// [deprecated] 参看afxcmn.inl,和CRichEditCtrl保持一致
void SetWindowText(LPCWSTR lpszText) { SetText(lpszText); }
CStringW GetWindowText();
int GetWindowText(LPWSTR lpString,int nMaxCount);
int GetWindowText(LPWSTR lpString, int nMaxCount);
int GetWindowTextLength();
void SetWindowText(LPCWSTR lpszText);

DWORD GetEventMask();
DWORD SetEventMask(DWORD dwEventMask); ///< 设置需要接收的事件类型 SetEventMask(ENM_OBJECTPOSITIONS | ENM_PROTECTED | ENM_DROPFILES | ENM_CHANGE | ENM_LINK | ENM_SELCHANGE | ENM_DRAGDROPDONE);
Expand Down
17 changes: 15 additions & 2 deletions DmMain/inc/Widgets/DUIWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,21 @@ namespace DM
virtual DMCode DV_OnStateChanged(DWORD dwOldState,DWORD dwNewState); ///< 状态改变时绘制

/// 文字相关
virtual DMCode DV_SetWindowText(LPCWSTR lpszText); ///< 设置文本
virtual const CStringW& DV_GetWindowText() const;
#if _HAS_CXX17
void SetText(std::string_view text, UINT cp = CP_UTF8) { SetText(DMCA2W(text.data(), text.length(), cp)); }
#else
void SetText(const char* text, UINT cp = CP_UTF8) { SetText(DMCA2W(text, -1, cp)); }
#endif
virtual void SetText(const CStringW& text);

CStringA GetTextA(UINT cp = CP_UTF8) { return DM::DMW2A(GetText(), cp); }
virtual CStringW GetText() const;

// [deprecated]
DMCode DV_SetWindowText(LPCWSTR lpszText); ///< 设置文本
const CStringW& DV_GetWindowText() const;

// Draw文字
virtual DMCode DV_DrawText(IDMCanvas* pCanvas, LPCWSTR pszBuf,int cchText,LPRECT lpRect,UINT uFormat); ///< 绘制文字,在WM_PAINT中触发
virtual DMCode DV_DrawMultText(IDMCanvas* pCanvas, LPCWSTR pszBuf,int cchText,LPRECT lpRect,UINT uFormat,int nLineInter); ///< 示例代码,用于xml中的字符串\R\N解析

Expand Down
4 changes: 2 additions & 2 deletions DmMain/src/Common/Plugins/DMPluginsTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ namespace DM
DMXmlNode directory = body.FirstChild(L"directory");
while (directory.IsValid())
{
wchar_t *pDir = (wchar_t*)directory.Attribute(L"name");
CStringW pDir = directory.Attribute(L"name");
wchar_t szPluginDir[MAX_PATH] = {0};
if (NULL == PathCombineW(szPluginDir, szExeDir, pDir))
{
Expand All @@ -269,7 +269,7 @@ namespace DM
strDir += L'\\';
}

wchar_t *pPluginName = (wchar_t*)item.Attribute(L"name");
CStringW pPluginName = item.Attribute(L"name");
LoadPlugin(strDir+pPluginName);
item = item.NextSibling(L"item");
}
Expand Down
4 changes: 2 additions & 2 deletions DmMain/src/Core/Dui/DUISkinPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace DM
{
if (!pItem->IsKeyExist(strId))
{// key不存在时才加入
LPCWSTR lpszClassName = XmlSkin.GetName();
CStringW lpszClassName = XmlSkin.GetName();
IDMSkinPtr pSkinPtr = NULL;
if (DMSUCCEEDED(g_pDMApp->CreateRegObj((void**)&pSkinPtr,lpszClassName,DMREG_Skin)))
{
Expand Down Expand Up @@ -231,7 +231,7 @@ namespace DM
}

//4.创建skin对象
LPCWSTR lpszClassName = XmlNode.GetName();
CStringW lpszClassName = XmlNode.GetName();
IDMSkinPtr pSkinPtr = NULL;
if (!DMSUCCEEDED(g_pDMApp->CreateRegObj((void**)&pSkinPtr,lpszClassName,DMREG_Skin)))
{
Expand Down
12 changes: 6 additions & 6 deletions DmMain/src/Modules/DMResFolderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ namespace DM
DMXmlNode XmlFileNode = XmlNode.FirstChild(L"file");
while (XmlFileNode.IsValid())
{
LPCWSTR lpszName = XmlFileNode.Attribute(L"name");
LPCWSTR lpszFilePath = XmlFileNode.Attribute(L"path");
CStringW lpszName = XmlFileNode.Attribute(L"name");
CStringW lpszFilePath = XmlFileNode.Attribute(L"path");
wchar_t szPath[MAX_PATH] = {0};
if (0 != PathCombineW(szPath, m_strDir, lpszFilePath))
{
Expand Down Expand Up @@ -415,13 +415,13 @@ namespace DM
XmlNode = XmlNode.FirstChild();
while (XmlNode.IsValid())
{
LPCWSTR lpszType = XmlNode.GetName();
CStringW lpszType = XmlNode.GetName();
DMXmlNode XmlFileNode = XmlNode.FirstChild(L"file");
while (XmlFileNode.IsValid())
{
LPCWSTR lpszName = XmlFileNode.Attribute(L"name");
LPCWSTR lpszFilePath = XmlFileNode.Attribute(L"path");
if (NULL!=lpszFilePath&&0!=wcslen(lpszFilePath))
CStringW lpszName = XmlFileNode.Attribute(L"name");
CStringW lpszFilePath = XmlFileNode.Attribute(L"path");
if (!lpszFilePath.IsEmpty())
{
wchar_t szPath[MAX_PATH] = {0};
if (0 != PathCombineW(szPath, m_strDir, lpszFilePath))
Expand Down
43 changes: 26 additions & 17 deletions DmMain/src/Widgets/DUIEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,28 @@ namespace DM

//---------------------------------------------------
// Function Des: 对外接口
CStringW DUIRichEdit::GetWindowText()
void DUIRichEdit::SetText(const CStringW& text)
{
DM_SendMessage(WM_SETTEXT, 0, (LPARAM)(LPCWSTR)text);
}

CStringW DUIRichEdit::GetText() const
{
DUIRichEdit* thiz = const_cast<DUIRichEdit*>(this);
CStringW strRet;
int nLen = (int)DM_SendMessage(WM_GETTEXTLENGTH);
wchar_t *pBuf = strRet.GetBufferSetLength(nLen+1);
DM_SendMessage(WM_GETTEXT,(WPARAM)nLen+1,(LPARAM)pBuf);
strRet.ReleaseBuffer();
int nLen = (int)thiz->DM_SendMessage(WM_GETTEXTLENGTH);
if (nLen > 0) {
wchar_t* pBuf = strRet.GetBufferSetLength(nLen); // 内部会预留'\0'的空间
thiz->DM_SendMessage(WM_GETTEXT, (WPARAM)nLen + 1, (LPARAM)pBuf);
}
return strRet;
}

CStringW DUIRichEdit::GetWindowText()
{
return GetText();
}

int DUIRichEdit::GetWindowText(LPWSTR lpString,int nMaxCount)
{
int iNum = -1;
Expand All @@ -95,11 +107,6 @@ namespace DM
return (int)DM_SendMessage(WM_GETTEXTLENGTH);
}

void DUIRichEdit::SetWindowText(LPCWSTR lpszText)
{
DM_SendMessage(WM_SETTEXT,0,(LPARAM)lpszText);
}

DWORD DUIRichEdit::GetEventMask()
{
return (DWORD)DM_SendMessage(EM_GETEVENTMASK, 0, 0L);
Expand Down Expand Up @@ -151,16 +158,18 @@ namespace DM

CStringW DUIRichEdit::GetLineText(int nLine /*= -1*/)
{
CStringW strRet;
int nLen = LineLength(nLine)+1;
wchar_t *pBuf = strRet.GetBufferSetLength(nLen);
*(LPINT)pBuf = nLen;
if (-1 == nLine)
{
nLine = LineFromChar(-1);

CStringW strRet;
int nLen = LineLength(nLine);
if (nLen > 0) {
wchar_t* pBuf = strRet.GetBuffer(max(nLen, sizeof(INT) - 1));
*(LPINT)pBuf = nLen; // windowsx Edit_GetLine, mfc CRichEdit::GetLine

DM_SendMessage(EM_GETLINE, nLine, (LPARAM)pBuf);
strRet.SetLength(nLen);
}
DM_SendMessage(EM_GETLINE,nLine,(LPARAM)pBuf);
strRet.ReleaseBuffer();
return strRet;
}

Expand Down
2 changes: 1 addition & 1 deletion DmMain/src/Widgets/DUIHeaderCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace DM
{
pNewItem->pSkin = m_pItemSkin;
}
strValue = (LPWSTR)XmlNode.Attribute(DMAttr::DUIHeaderCtrlAttr::ITEM_text);
strValue = XmlNode.Attribute(DMAttr::DUIHeaderCtrlAttr::ITEM_text);
pNewItem->lpszText = _wcsdup(strValue);
pNewItem->cchTextMax = strValue.GetLength();
strValue = XmlNode.Attribute(DMAttr::DUIHeaderCtrlAttr::ITEM_data);
Expand Down
Loading

0 comments on commit a73dc10

Please sign in to comment.