fixed #1634: Added the very first implementation of CCEditBox for Win32.

This commit is contained in:
James Chen 2013-01-31 14:04:49 +08:00
parent 3bdfb82e9c
commit 157e229f8c
6 changed files with 93 additions and 147 deletions

View File

@ -243,7 +243,7 @@ static void cc_utf8_trim_ws(std::vector<unsigned short>* 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;

View File

@ -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
*/
#endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */

View File

@ -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__ */

View File

@ -1,4 +1,7 @@
#include "Win32InputBox.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include <stdio.h>
#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;
}
}
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) */

View File

@ -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 <lallousx86@yahoo.com>
@ -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

View File

@ -208,6 +208,9 @@
<Filter Include="Classes\FileUtilsTest">
<UniqueIdentifier>{a4c2111f-cf9f-492c-884d-3de24715adce}</UniqueIdentifier>
</Filter>
<Filter Include="Classes\ExtensionsTest\EditBoxTest">
<UniqueIdentifier>{18a69e7e-8ca7-475a-bfbb-7296baab16ce}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
@ -474,6 +477,9 @@
<ClCompile Include="..\Classes\FileUtilsTest\FileUtilsTest.cpp">
<Filter>Classes\FileUtilsTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\ExtensionsTest\EditBoxTest\EditBoxTest.cpp">
<Filter>Classes\ExtensionsTest\EditBoxTest</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -917,5 +923,8 @@
<ClInclude Include="..\Classes\FileUtilsTest\FileUtilsTest.h">
<Filter>Classes\FileUtilsTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\ExtensionsTest\EditBoxTest\EditBoxTest.h">
<Filter>Classes\ExtensionsTest\EditBoxTest</Filter>
</ClInclude>
</ItemGroup>
</Project>