From 157e229f8c0715e50cffaede5c6500ec81e11059 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 31 Jan 2013 14:04:49 +0800 Subject: [PATCH] fixed #1634: Added the very first implementation of CCEditBox for Win32. --- cocos2dx/label_nodes/CCLabelBMFont.cpp | 2 +- extensions/GUI/CCEditBox/CCEditBoxImplWin.cpp | 148 +----------------- extensions/GUI/CCEditBox/CCEditBoxImplWin.h | 5 + extensions/proj.win32/Win32InputBox.cpp | 68 +++++++- extensions/proj.win32/Win32InputBox.h | 8 + .../proj.win32/TestCpp.vcxproj.filters | 9 ++ 6 files changed, 93 insertions(+), 147 deletions(-) diff --git a/cocos2dx/label_nodes/CCLabelBMFont.cpp b/cocos2dx/label_nodes/CCLabelBMFont.cpp index f97aabdf35..49d312e1c7 100644 --- a/cocos2dx/label_nodes/CCLabelBMFont.cpp +++ b/cocos2dx/label_nodes/CCLabelBMFont.cpp @@ -243,7 +243,7 @@ static void cc_utf8_trim_ws(std::vector* str) * * Return value: the length of the string in characters **/ -long +CC_DLL long cc_utf8_strlen (const char * p, int max) { long len = 0; diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplWin.cpp b/extensions/GUI/CCEditBox/CCEditBoxImplWin.cpp index 56ba8dd774..009e5682e3 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplWin.cpp +++ b/extensions/GUI/CCEditBox/CCEditBoxImplWin.cpp @@ -22,13 +22,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ - #include "CCEditBoxImplWin.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + #include "CCEditBox.h" #include "proj.win32/Win32InputBox.h" NS_CC_BEGIN -extern long cc_utf8_strlen (const char * p, int max); +extern CC_DLL long cc_utf8_strlen (const char * p, int max); NS_CC_END NS_CC_EXT_BEGIN @@ -136,7 +138,7 @@ void CCEditBoxImplWin::setText(const char* pText) if (kEditBoxInputFlagPassword == m_eEditBoxInputFlag) { - long length = strlen(m_strText.c_str()); + long length = cc_utf8_strlen(m_strText.c_str(), -1); for (long i = 0; i < length; i++) { strToShow.append("*"); @@ -241,142 +243,4 @@ void CCEditBoxImplWin::closeKeyboard() NS_CC_EXT_END -/* - -#include "CCEditBoxImplWin.h" -#include "CCEditBox.h" -#include "CCEGLView.h" - -NS_CC_EXT_BEGIN - -CCEditBoxImpl* __createSystemEditBox(CCEditBox* pEditBox) -{ - return new CCEditBoxImplWin(pEditBox); -} - -//#define GET_IMPL ((CCEditBoxImplWin*)m_pSysEdit) - -CCEditBoxImplWin::CCEditBoxImplWin(CCEditBox* pEditText) -: CCEditBoxImpl(pEditText) -, m_pSysEdit(NULL) -, m_nMaxTextLength(-1) -{ -} - -CCEditBoxImplWin::~CCEditBoxImplWin() -{ -} - -void CCEditBoxImplWin::doAnimationWhenKeyboardMove(float duration, float distance) -{ -} - -bool CCEditBoxImplWin::initWithSize(const CCSize& size) -{ - do - { - CCEGLViewProtocol* eglView = CCEGLView::sharedOpenGLView(); - CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView(); - - HWND hParent = pMainWnd->getHWnd(); - m_pSysEdit = ::CreateWindowA("EDIT", "text", WS_CHILD|WS_CLIPCHILDREN|WS_VISIBLE, 0, 0, size.width * eglView->getScaleX(),size.height * eglView->getScaleY(), hParent, NULL, NULL, NULL); - if (!m_pSysEdit) - break; - - return true; - } while (0); - - return false; -} - -void CCEditBoxImplWin::setFontColor(const ccColor3B& color) -{ - //GET_IMPL.textField.textColor = [UIColor colorWithRed:color.r / 255.0f green:color.g / 255.0f blue:color.b / 255.0f alpha:1.0f]; -} - -void CCEditBoxImplWin::setPlaceholderFontColor(const ccColor3B& color) -{ - // TODO need to be implemented. -} - -void CCEditBoxImplWin::setInputMode(EditBoxInputMode inputMode) -{ -} - -void CCEditBoxImplWin::setMaxLength(int maxLength) -{ - m_nMaxTextLength = maxLength; -} - -int CCEditBoxImplWin::getMaxLength() -{ - return m_nMaxTextLength; -} - -void CCEditBoxImplWin::setInputFlag(EditBoxInputFlag inputFlag) -{ - // TODO: ES_PASSWORD -} - -void CCEditBoxImplWin::setReturnType(KeyboardReturnType returnType) -{ -} - -bool CCEditBoxImplWin::isEditing() -{ - return true; //GET_IMPL->isEditState() ? true : false; -} - -void CCEditBoxImplWin::setText(const char* pText) -{ - ::SetWindowTextA(m_pSysEdit, pText); - //GET_IMPL.textField.text = [NSString stringWithUTF8String:pText]; -} - -const char* CCEditBoxImplWin::getText(void) -{ - int nCharacters = ::GetWindowTextLength(m_pSysEdit); - char *szBuff = (char *)malloc(nCharacters + 1); - ::GetWindowText(m_pSysEdit, szBuff, nCharacters); - return szBuff; -} - -void CCEditBoxImplWin::setPlaceHolder(const char* pText) -{ -} - -void CCEditBoxImplWin::setPosition(const CCPoint& pos) -{ - //TODO should consider anchor point, the default value is (0.5, 0,5) - RECT rect; - ::GetWindowRect(m_pSysEdit, &rect); - POINT pt; - pt.x = rect.left; - pt.y = rect.top; - CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView(); - ::ScreenToClient(pMainWnd->getHWnd(), &pt); - ::SetWindowPos(m_pSysEdit, NULL, pt.x + pos.x-m_tContentSize.width/2, pt.y + pos.y+m_tContentSize.height/2, 0, 0, SWP_NOSIZE|SWP_NOOWNERZORDER|SWP_NOZORDER|SWP_NOCOPYBITS); - //GET_IMPL->setPosition(ccp(pos.x-m_tContentSize.width/2, pos.y+m_tContentSize.height/2));; -} - -void CCEditBoxImplWin::setContentSize(const CCSize& size) -{ - m_tContentSize = size; -} - -void CCEditBoxImplWin::visit(void) -{ - -} - -void CCEditBoxImplWin::openKeyboard() -{ -} - -void CCEditBoxImplWin::closeKeyboard() -{ -} - -NS_CC_EXT_END - -*/ \ No newline at end of file +#endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */ diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplWin.h b/extensions/GUI/CCEditBox/CCEditBoxImplWin.h index 6677e59f6c..efc6ed53b3 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplWin.h +++ b/extensions/GUI/CCEditBox/CCEditBoxImplWin.h @@ -27,6 +27,9 @@ #define __CCEditBoxIMPLWIN_H__ #include "cocos2d.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + #include "ExtensionMacros.h" #include "CCEditBoxImpl.h" @@ -87,5 +90,7 @@ private: NS_CC_EXT_END +#endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */ + #endif /* __CCEditBoxIMPLWIN_H__ */ diff --git a/extensions/proj.win32/Win32InputBox.cpp b/extensions/proj.win32/Win32InputBox.cpp index 670e87a8f5..1cc2e81f55 100644 --- a/extensions/proj.win32/Win32InputBox.cpp +++ b/extensions/proj.win32/Win32InputBox.cpp @@ -1,4 +1,7 @@ #include "Win32InputBox.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + #include #pragma warning (disable: 4312) @@ -214,8 +217,8 @@ void CWin32InputBox::InitDialog() ::SetDlgItemText(_param->hDlg, (int) definputbox_buttonids[i], definputbox_buttonnames[i]); // Set other controls - ::SetWindowTextA(_param->hDlg, _param->szTitle); - ::SetDlgItemTextA(_param->hDlg, definputbox_id_prompt, _param->szPrompt); + ::SetWindowTextA(_param->hDlg, Utf8ToAnsi(_param->szTitle).c_str()); + ::SetDlgItemTextA(_param->hDlg, definputbox_id_prompt, Utf8ToAnsi(_param->szPrompt).c_str()); HWND hwndEdit1 = ::GetDlgItem(_param->hDlg, definputbox_id_edit1); HWND hwndEdit2 = ::GetDlgItem(_param->hDlg, definputbox_id_edit2); @@ -225,7 +228,7 @@ void CWin32InputBox::InitDialog() else _hwndEditCtrl = hwndEdit1; - ::SetWindowTextA(_hwndEditCtrl, _param->szResult); + ::SetWindowTextA(_hwndEditCtrl, Utf8ToAnsi(_param->szResult).c_str()); RECT rectDlg, rectEdit1, rectEdit2; @@ -308,6 +311,11 @@ LRESULT CALLBACK CWin32InputBox::DlgProc(HWND hDlg, UINT message, WPARAM wParam, _this->_param->szResult, _this->_param->nResultSize); + std::string strUtf8 = AnsiToUtf8(_this->_param->szResult); + + memset(_this->_param->szResult, 0, _this->_param->nResultSize); + strncpy(_this->_param->szResult, strUtf8.c_str(), _this->_param->nResultSize-1); + ::EndDialog(hDlg, buttonId); return TRUE; } @@ -316,4 +324,56 @@ LRESULT CALLBACK CWin32InputBox::DlgProc(HWND hDlg, UINT message, WPARAM wParam, break; } return FALSE; -} \ No newline at end of file +} + + +std::string CWin32InputBox::AnsiToUtf8(std::string strAnsi) +{ + std::string ret; + if (strAnsi.length() > 0) + { + int nWideStrLength = MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), -1, NULL, 0); + WCHAR* pwszBuf = (WCHAR*)malloc((nWideStrLength+1)*sizeof(WCHAR)); + memset(pwszBuf, 0, (nWideStrLength+1)*sizeof(WCHAR)); + MultiByteToWideChar(CP_ACP, 0, strAnsi.c_str(), -1, pwszBuf, (nWideStrLength+1)*sizeof(WCHAR)); + + int nUtf8Length = WideCharToMultiByte( CP_UTF8,0,pwszBuf,-1,NULL,0,NULL,FALSE ); + char* pszUtf8Buf = (char*)malloc((nUtf8Length+1)*sizeof(char)); + memset(pszUtf8Buf, 0, (nUtf8Length+1)*sizeof(char)); + + WideCharToMultiByte(CP_UTF8, 0, pwszBuf, -1, pszUtf8Buf, (nUtf8Length+1)*sizeof(char), NULL, FALSE); + ret = pszUtf8Buf; + + free(pszUtf8Buf); + free(pwszBuf); + } + return ret; +} + +std::string CWin32InputBox::Utf8ToAnsi(std::string strUTF8) +{ + std::string ret; + if (strUTF8.length() > 0) + { + int nWideStrLength = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); + WCHAR* pwszBuf = (WCHAR*)malloc((nWideStrLength+1)*sizeof(WCHAR)); + memset(pwszBuf, 0, (nWideStrLength+1)*sizeof(WCHAR)); + MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, pwszBuf, (nWideStrLength+1)*sizeof(WCHAR)); + + int nAnsiStrLength = WideCharToMultiByte( CP_ACP,0,pwszBuf,-1,NULL,0,NULL,FALSE ); + char* pszAnsiBuf = (char*)malloc((nAnsiStrLength+1)*sizeof(char)); + memset(pszAnsiBuf, 0, (nAnsiStrLength+1)*sizeof(char)); + + WideCharToMultiByte(CP_ACP, 0, pwszBuf, -1, pszAnsiBuf, (nAnsiStrLength+1)*sizeof(char), NULL, FALSE); + ret = pszAnsiBuf; + + free(pszAnsiBuf); + free(pwszBuf); + } + + return ret; +} + + + +#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */ diff --git a/extensions/proj.win32/Win32InputBox.h b/extensions/proj.win32/Win32InputBox.h index 53c96bded1..405cb7bd4e 100644 --- a/extensions/proj.win32/Win32InputBox.h +++ b/extensions/proj.win32/Win32InputBox.h @@ -1,6 +1,9 @@ #ifndef __03022006__WIN32INPUTBOX__ #define __03022006__WIN32INPUTBOX__ +#include "cocos2d.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) /* This library is (c) Elias Bachaalany aka lallous @@ -93,6 +96,11 @@ public: DWORD nResultSize, bool bMultiLine = false, HWND hwndParent = 0); + + static std::string AnsiToUtf8(std::string strAnsi); + static std::string Utf8ToAnsi(std::string strUTF8); }; +#endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */ + #endif \ No newline at end of file diff --git a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters index a6cdadca29..9a393214d1 100644 --- a/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters +++ b/samples/Cpp/TestCpp/proj.win32/TestCpp.vcxproj.filters @@ -208,6 +208,9 @@ {a4c2111f-cf9f-492c-884d-3de24715adce} + + {18a69e7e-8ca7-475a-bfbb-7296baab16ce} + @@ -474,6 +477,9 @@ Classes\FileUtilsTest + + Classes\ExtensionsTest\EditBoxTest + @@ -917,5 +923,8 @@ Classes\FileUtilsTest + + Classes\ExtensionsTest\EditBoxTest + \ No newline at end of file