diff --git a/native/cocos/2d/libcocos2d.vcxproj b/native/cocos/2d/libcocos2d.vcxproj index 6917eee510..9a7edab79a 100644 --- a/native/cocos/2d/libcocos2d.vcxproj +++ b/native/cocos/2d/libcocos2d.vcxproj @@ -377,6 +377,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32\libs\*.*" "$(OutDir)" + @@ -752,6 +753,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\win32\libs\*.*" "$(OutDir)" + diff --git a/native/cocos/2d/libcocos2d.vcxproj.filters b/native/cocos/2d/libcocos2d.vcxproj.filters index cc6b030c8d..8c39671269 100644 --- a/native/cocos/2d/libcocos2d.vcxproj.filters +++ b/native/cocos/2d/libcocos2d.vcxproj.filters @@ -1140,6 +1140,9 @@ audioengine + + ui\UIWidgets\EditBox + @@ -2205,6 +2208,9 @@ audioengine + + ui\UIWidgets\EditBox + diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp index 4d03153065..9dc089aef4 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-android.cpp @@ -1,7 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen - Copyright (c) 2013-2015 zilongshanren + Copyright (c) 2013-2017 zilongshanren http://www.cocos2d-x.org diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp index e9784595c6..b86bfcd444 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp @@ -1,7 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2012 James Chen - Copyright (c) 2013-2015 zilongshanren + Copyright (c) 2013-2017 zilongshanren http://www.cocos2d-x.org @@ -27,6 +27,12 @@ #define kLabelZOrder 9999 +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) +#define PASSWORD_CHAR "*" +#else +#define PASSWORD_CHAR "\u25CF" +#endif + #include "UIEditBox.h" #include "base/CCDirector.h" #include "2d/CCLabel.h" @@ -127,7 +133,7 @@ void EditBoxImplCommon::setInactiveText(const char* pText) { std::string passwordString; for(int i = 0; i < strlen(pText); ++i) - passwordString.append("\u25CF"); + passwordString.append(PASSWORD_CHAR); _label->setString(passwordString); } else diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h index 6cb9163e06..691f7d7f21 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h @@ -113,8 +113,9 @@ public: virtual void nativeCloseKeyboard() = 0; virtual void setNativeMaxLength(int maxLength) {}; +protected: + bool _editingMode; -private: void initInactiveLabels(const Size& size); void setInactiveText(const char* pText); void placeInactiveLabels(const Size& size); @@ -133,7 +134,6 @@ private: int _maxLength; Size _contentSize; - bool _editingMode; }; diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 6f699fd33e..d66b6a4da1 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2010-2012 cocos2d-x.org Copyright (c) 2013 Jozef Pridavok + Copyright (c) 2017 zilongshanren http://www.cocos2d-x.org @@ -28,929 +29,370 @@ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #include "UIEditBox.h" -#include #include #include #include "2d/CCLabel.h" #include "base/CCDirector.h" #include "base/ccUTF8.h" - -#pragma warning (disable: 4312) - -// Structure used to orient the inputbox behavior -struct WIN32INPUTBOX_PARAM -{ - friend class CWin32InputBox; - - // - IN OPTIONAL bool bMultiline; - - // Pass this as none zero so to use this memory dlg template - IN OPTIONAL LPVOID DlgTemplateData; - - // Pass this as none ZERO so to load DLGTEMPLATE from resources - IN OPTIONAL LPCSTR DlgTemplateName; - - // passing both "DlgTemplateName" and "DlgTemplateData" ZERO will cause - // the dialog to use his default embedded resource - - // Center on monitor or owner window? - IN OPTIONAL bool bCenter; - - // Want to add more styles to the dialog? - IN OPTIONAL DWORD dwStylesPlus, dwStylesMinus; - IN OPTIONAL DWORD dwExStylesPlus, dwExStylesMinus; - - IN LPCSTR szTitle, szPrompt; - - // Return buffer - std::string* pstrResult; - - IN DWORD nMaxLength; - - // Owner window - HWND hwndOwner; - HINSTANCE hInstance; - - short xPos, yPos; - - WIN32INPUTBOX_PARAM(); -private: - HWND hDlg; -}; - -class CWin32InputBox -{ -private: - WIN32INPUTBOX_PARAM *_param; - static LRESULT CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); - HWND _hwndEditCtrl; - int _recursionDepth; - HHOOK _getMsgHook; - - cocos2d::ui::EditBox::KeyboardReturnType _returnType; - cocos2d::ui::EditBox::InputMode _inputMode; - cocos2d::ui::EditBox::InputFlag _inputFlag; - std::function _onTextChange; - std::function _onClose; - - void InitDialog(); - static LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam); - -public: - - CWin32InputBox(WIN32INPUTBOX_PARAM *); - ~CWin32InputBox(); - - static INT_PTR InputBoxEx(WIN32INPUTBOX_PARAM *, - cocos2d::ui::EditBox::KeyboardReturnType eReturnType, - cocos2d::ui::EditBox::InputMode eInputMode, - cocos2d::ui::EditBox::InputFlag eInputFlag, - const std::function &onTextChange, - const std::function &onClose); - - static INT_PTR InputBox( - LPCSTR szTitle, - LPCSTR szPrompt, - std::string* pstrResult, - DWORD nMaxLength, - bool bMultiLine, - HWND hwndParent, - cocos2d::ui::EditBox::KeyboardReturnType eReturnType, - cocos2d::ui::EditBox::InputMode eInputMode, - cocos2d::ui::EditBox::InputFlag eInputFlag, - const std::function &onTextChange, - const std::function &onClose); - - void SetText(const char *pText); - void Close(INT_PTR buttonId); -}; - -typedef struct _MSDN_DLGTEMPLATEEX -{ - WORD dlgVer; - WORD signature; - DWORD helpID; - DWORD exStyle; - DWORD style; - WORD cDlgItems; - short x; - short y; - short cx; - short cy; - BYTE _rest[1]; // rest of structure -} MSDN_DLGTEMPLATEEX; - -static bool IsDlgTemplateExtended(DLGTEMPLATE *dlgTemplate) -{ - MSDN_DLGTEMPLATEEX *dgExTemplate = (MSDN_DLGTEMPLATEEX *)dlgTemplate; - - // MSDN excerpt: - //* dlgVer - // Specifies the version number of the extended dialog box template. This member must be 1. - //* signature - // Indicates whether a template is an extended dialog box template. - // If signature is 0xFFFF, this is an extended dialog box template. - // In this case, the dlgVer member specifies the template version number. - // If signature is any value other than 0xFFFF, this is a standard dialog box template that uses the DLGTEMPLATE and DLGITEMTEMPLATE structures. - - return (dgExTemplate->dlgVer == 1) && (dgExTemplate->signature == 0xFFFF); -} - -// Use alignment if supported by the compiler -#ifdef _MSC_VER -#if _MSC_VER > 1200 -__declspec(align(4)) -#endif -#endif - -// per the MSDN, the DLGTEMPLATE must be DWORD aligned -// this was generated by the DlgResToDlgTemplate tool -static unsigned char definputbox_dlg[] = -{ - 0x01, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0xc8, 0x00, 0x06, - 0x00, 0x16, 0x00, 0x11, 0x00, 0xe7, 0x00, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x69, - 0x00, 0x6e, 0x00, 0x33, 0x00, 0x32, 0x00, 0x49, 0x00, 0x6e, 0x00, 0x70, 0x00, 0x75, 0x00, 0x74, - 0x00, 0x42, 0x00, 0x6f, 0x00, 0x78, 0x00, 0x00, 0x00, 0x08, 0x00, 0xbc, 0x02, 0x00, 0x00, 0x4d, - 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00, 0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x20, - 0x00, 0x44, 0x00, 0x6c, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x50, 0x06, 0x00, 0x04, 0x00, 0x9d, 0x00, 0x21, 0x00, 0xe8, - 0x03, 0x00, 0x00, 0xff, 0xff, 0x82, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x70, - 0x00, 0x74, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x80, 0x00, 0x81, 0x50, 0x06, 0x00, 0x25, 0x00, 0xd8, 0x00, 0x0e, 0x00, 0xe9, - 0x03, 0x00, 0x00, 0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x84, 0x10, 0xa1, 0x50, 0x06, 0x00, 0x37, 0x00, 0xd8, 0x00, 0x31, 0x00, 0xea, - 0x03, 0x00, 0x00, 0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x50, 0xab, 0x00, 0x04, 0x00, 0x33, 0x00, 0x0e, 0x00, 0x01, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00, 0x4f, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x50, 0xab, 0x00, 0x15, 0x00, 0x33, - 0x00, 0x0e, 0x00, 0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00, 0x43, 0x00, 0x41, 0x00, 0x4e, - 0x00, 0x43, 0x00, 0x45, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x27, 0x00, 0x08, 0x00, 0x08, 0x00, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00 -}; - -static const INT -definputbox_id_prompt = 1000, -definputbox_id_edit1 = 1001, -definputbox_id_edit2 = 1002; - -static CWin32InputBox *s_win32InputBox = nullptr; - -WIN32INPUTBOX_PARAM::WIN32INPUTBOX_PARAM() -{ - bMultiline = false; - hwndOwner = 0; - DlgTemplateName = 0; - hInstance = (HINSTANCE) ::GetModuleHandle(0); - DlgTemplateData = definputbox_dlg; - - bCenter = true; - - dwStylesPlus = 0; - dwExStylesPlus = 0; - dwStylesMinus = 0xFFFFFFFF; - dwExStylesMinus = 0xFFFFFFFF; - - xPos = yPos = -1; - - pstrResult = nullptr; - nMaxLength = (DWORD) -1; -} - -CWin32InputBox::CWin32InputBox(WIN32INPUTBOX_PARAM *param) -{ - _param = new (std::nothrow) WIN32INPUTBOX_PARAM(*param); - _hwndEditCtrl = NULL; - _recursionDepth = 0; - _getMsgHook = NULL; -} - -CWin32InputBox::~CWin32InputBox() -{ - CC_SAFE_DELETE(_param); -} - -INT_PTR CWin32InputBox::InputBoxEx(WIN32INPUTBOX_PARAM *param, - cocos2d::ui::EditBox::KeyboardReturnType eReturnType, - cocos2d::ui::EditBox::InputMode eInputMode, - cocos2d::ui::EditBox::InputFlag eInputFlag, - const std::function &onTextChange, - const std::function &onClose) -{ - // Check mandatory parameters - if (param->pstrResult == nullptr) - { - ::SetLastError(ERROR_INVALID_PARAMETER); - return 0; - } - - LPDLGTEMPLATE dlgTemplate; - - if (param->DlgTemplateName != 0) - { - HMODULE hModule = (HMODULE)param->hInstance; -#ifdef __MINGW32__ - HRSRC rcDlg = ::FindResource(hModule, (LPWSTR)(ULONG_PTR)(size_t)(param->DlgTemplateName), RT_DIALOG); -#else - HRSRC rcDlg = ::FindResource(hModule, MAKEINTRESOURCE(param->DlgTemplateName), RT_DIALOG); -#endif - if (rcDlg == nullptr) - return 0; - - HGLOBAL hglobalDlg = ::LoadResource(hModule, rcDlg); - if (hglobalDlg == nullptr) - return 0; - - dlgTemplate = (LPDLGTEMPLATE)hglobalDlg; - } - else if (param->DlgTemplateData != 0) - { - dlgTemplate = (LPDLGTEMPLATE)param->DlgTemplateData; - } - - MSDN_DLGTEMPLATEEX *dlgTemplateEx = - IsDlgTemplateExtended((LPDLGTEMPLATE)dlgTemplate) ? (MSDN_DLGTEMPLATEEX *)dlgTemplate : 0; - - if (dlgTemplateEx != 0) - { - dlgTemplateEx->exStyle |= param->dwExStylesPlus; - dlgTemplateEx->style |= param->dwStylesPlus; - dlgTemplateEx->exStyle &= param->dwExStylesMinus; - dlgTemplateEx->style &= param->dwStylesMinus; - - if (param->bCenter) - dlgTemplateEx->style |= DS_CENTER; - - if (param->xPos != -1) - dlgTemplateEx->x = param->xPos; - if (param->yPos != -1) - dlgTemplateEx->y = param->yPos; - } - else - { - dlgTemplate->dwExtendedStyle |= param->dwExStylesPlus; - dlgTemplate->style |= param->dwStylesPlus; - dlgTemplate->dwExtendedStyle &= param->dwExStylesMinus; - dlgTemplate->style &= param->dwStylesMinus; - - if (param->bCenter) - dlgTemplate->style |= DS_CENTER; - - if (param->xPos != -1) - dlgTemplate->x = param->xPos; - - if (param->yPos != -1) - dlgTemplate->y = param->yPos; - } - - CCASSERT(s_win32InputBox == nullptr, "Only one instance of Win32InputBox allowed"); - - s_win32InputBox = new (std::nothrow) CWin32InputBox(param); - s_win32InputBox->_returnType = eReturnType; - s_win32InputBox->_inputMode = eInputMode; - s_win32InputBox->_inputFlag = eInputFlag; - s_win32InputBox->_onTextChange = onTextChange; - s_win32InputBox->_onClose = onClose; - - HWND hDlg = ::CreateDialogIndirectParam(param->hInstance, dlgTemplate, param->hwndOwner, (DLGPROC)DlgProc, (LPARAM)s_win32InputBox); - ::ShowWindow(hDlg, SW_SHOW); - ::EnableWindow(param->hwndOwner, FALSE); - - return TRUE; -} - -INT_PTR CWin32InputBox::InputBox( - LPCSTR szTitle, - LPCSTR szPrompt, - std::string* pstrResult, - DWORD nResultSize, - bool bMultiLine, - HWND hwndParent, - cocos2d::ui::EditBox::KeyboardReturnType eReturnType, - cocos2d::ui::EditBox::InputMode eInputMode, - cocos2d::ui::EditBox::InputFlag eInputFlag, - const std::function &onTextChange, - const std::function &onClose) -{ - WIN32INPUTBOX_PARAM param; - - param.szTitle = szTitle; - param.szPrompt = szPrompt; - param.pstrResult = pstrResult; - param.nMaxLength = nResultSize; - param.bMultiline = bMultiLine; - param.hwndOwner = hwndParent; - - return InputBoxEx(¶m, eReturnType, eInputMode, eInputFlag, onTextChange, onClose); -} - -void CWin32InputBox::InitDialog() -{ - // Set the button captions, associated with ReturnType - switch (_returnType) - { - case cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT: - ::SetDlgItemTextW(_param->hDlg, IDOK, L"OK"); - break; - case cocos2d::ui::EditBox::KeyboardReturnType::DONE: - ::SetDlgItemTextW(_param->hDlg, IDOK, L"DONE"); - break; - case cocos2d::ui::EditBox::KeyboardReturnType::SEND: - ::SetDlgItemTextW(_param->hDlg, IDOK, L"SEND"); - break; - case cocos2d::ui::EditBox::KeyboardReturnType::SEARCH: - ::SetDlgItemTextW(_param->hDlg, IDOK, L"SEARCH"); - break; - case cocos2d::ui::EditBox::KeyboardReturnType::GO: - ::SetDlgItemTextW(_param->hDlg, IDOK, L"GO"); - break; - } - ::SetDlgItemTextW(_param->hDlg, IDCANCEL, L"CANCEL"); - - // Set other controls - std::u16string utf16Title; - cocos2d::StringUtils::UTF8ToUTF16(_param->szTitle, utf16Title); - ::SetWindowTextW(_param->hDlg, (LPCWSTR) utf16Title.c_str()); - - std::u16string utf16Prompt; - cocos2d::StringUtils::UTF8ToUTF16(_param->szPrompt, utf16Prompt); - ::SetDlgItemTextW(_param->hDlg, definputbox_id_prompt, (LPCWSTR) utf16Prompt.c_str()); - - HWND hwndEdit1 = ::GetDlgItem(_param->hDlg, definputbox_id_edit1); - HWND hwndEdit2 = ::GetDlgItem(_param->hDlg, definputbox_id_edit2); - - if (_param->bMultiline) - _hwndEditCtrl = hwndEdit2; - else - _hwndEditCtrl = hwndEdit1; - - std::u16string utf16Result; - cocos2d::StringUtils::UTF8ToUTF16(*_param->pstrResult, utf16Result); - ::SetWindowTextW(_hwndEditCtrl, (LPCWSTR) utf16Result.c_str()); - - RECT rectDlg, rectEdit1, rectEdit2; - - ::GetWindowRect(_param->hDlg, &rectDlg); - ::GetWindowRect(hwndEdit1, &rectEdit1); - ::GetWindowRect(hwndEdit2, &rectEdit2); - - if (_param->bMultiline) - { - ::ShowWindow(hwndEdit1, SW_HIDE); - ::SetWindowPos( - hwndEdit2, - HWND_NOTOPMOST, - rectEdit1.left - rectDlg.left, - (rectEdit1.top - rectDlg.top) - (rectEdit1.bottom - rectEdit1.top), - 0, - 0, - SWP_NOSIZE | SWP_NOZORDER); - - ::SetWindowPos( - _param->hDlg, - HWND_NOTOPMOST, - 0, - 0, - rectDlg.right - rectDlg.left, - rectDlg.bottom - rectDlg.top - (rectEdit1.bottom - rectEdit1.top), - SWP_NOMOVE); - } - else - { - ::SetWindowPos( - _param->hDlg, - HWND_NOTOPMOST, - 0, - 0, - rectDlg.right - rectDlg.left, - rectEdit1.bottom - rectDlg.top + 5, - SWP_NOMOVE); - - ::ShowWindow(hwndEdit2, SW_HIDE); - } - - // Sets the text limit of the edit control. - // wParam is the maximum number of WCHARs the user can enter. For Unicode text, wParam is the number of characters. - // Please refer to https://msdn.microsoft.com/en-us/library/bb761607%28v=vs.85%29.aspx - if ((int)_param->nMaxLength > 0) - { - ::SendMessageW(hwndEdit1, EM_LIMITTEXT, (WPARAM)_param->nMaxLength, 0); - ::SendMessageW(hwndEdit2, EM_LIMITTEXT, (WPARAM)_param->nMaxLength, 0); - } - - switch (_inputFlag) - { - case cocos2d::ui::EditBox::InputFlag::PASSWORD: - // Sets a password character displayed in a edit control when the user types text. - // Please refer to https://msdn.microsoft.com/en-us/library/bb761653%28v=vs.85%29.aspx - ::SendMessageW(hwndEdit1, EM_SETPASSWORDCHAR, (WPARAM)L'*', 0); - ::SendMessageW(hwndEdit2, EM_SETPASSWORDCHAR, (WPARAM)L'*', 0); - break; - case cocos2d::ui::EditBox::InputFlag::SENSITIVE: - break; - case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD: - break; - case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE: - break; - case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS: - // Converts all characters to lowercase as they are typed into the edit control. - // Please refer to https://msdn.microsoft.com/en-us/library/bb775464%28v=vs.85%29.aspx - ::SetWindowLongW(hwndEdit1, GWL_STYLE, ::GetWindowLongW(hwndEdit1, GWL_STYLE) | ES_UPPERCASE); - ::SetWindowLongW(hwndEdit2, GWL_STYLE, ::GetWindowLongW(hwndEdit2, GWL_STYLE) | ES_UPPERCASE); - break; - default: - break; - } - - switch (_inputMode) - { - case cocos2d::ui::EditBox::InputMode::ANY: - break; - case cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS: - break; - case cocos2d::ui::EditBox::InputMode::NUMERIC: - // Allows only digits to be entered into the edit control. - // Note that, even with this set, it is still possible to paste non-digits into the edit control. - // Please refer to https://msdn.microsoft.com/en-us/library/bb775464%28v=vs.85%29.aspx - ::SetWindowLongW(hwndEdit1, GWL_STYLE, ::GetWindowLongW(hwndEdit1, GWL_STYLE) | ES_NUMBER); - ::SetWindowLongW(hwndEdit2, GWL_STYLE, ::GetWindowLongW(hwndEdit2, GWL_STYLE) | ES_NUMBER); - break; - case cocos2d::ui::EditBox::InputMode::PHONE_NUMBER: - break; - case cocos2d::ui::EditBox::InputMode::URL: - break; - case cocos2d::ui::EditBox::InputMode::DECIMAL: - break; - case cocos2d::ui::EditBox::InputMode::SINGLE_LINE: - break; - default: - break; - } - CCASSERT(_getMsgHook == NULL, "Windows Message hook already set"); - - // To make the enter key work, here need a Windows Message hook. - // Please refer to https://support.microsoft.com/en-us/kb/187988 - _getMsgHook = ::SetWindowsHookEx(WH_GETMESSAGE, &CWin32InputBox::GetMsgProc, ::GetModuleHandle(nullptr), ::GetCurrentThreadId()); -} - -LRESULT CALLBACK CWin32InputBox::GetMsgProc(int code, WPARAM wParam, LPARAM lParam) -{ - if (code == HC_ACTION) - { - LPMSG lpMsg = (LPMSG)lParam; - if (lpMsg->message == WM_KEYDOWN) - { - if (lpMsg->wParam == VK_RETURN) // User has hit the ENTER key. - { - // Translate the message manually. - lpMsg->hwnd = s_win32InputBox->_param->hDlg; - lpMsg->message = WM_COMMAND; - lpMsg->wParam = IDOK; - } - } - } - return ::CallNextHookEx(s_win32InputBox->_getMsgHook, code, wParam, lParam); -} - -// Message handler for about box. -LRESULT CALLBACK CWin32InputBox::DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) -{ - CWin32InputBox *_this = (CWin32InputBox *) ::GetWindowLongPtr(hDlg, GWLP_USERDATA); - - switch (message) - { - case WM_INITDIALOG: - { - SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR)lParam); - - _this = (CWin32InputBox *)lParam; - _this->_param->hDlg = hDlg; - _this->InitDialog(); - return TRUE; - } - - case WM_COMMAND: - { -#ifdef _MY_DEBUG - CHAR buf[1024]; - static int i = 0; - sprintf(buf, "WM_COMMAND: %09d wParam=%08X lParam=%08X\n", i++, wParam, lParam); - OutputDebugStringA(buf); -#endif - INT_PTR buttonId = LOWORD(wParam); - if (buttonId == IDOK || buttonId == IDCANCEL) - { - // Yes! Just close the dialog box simply, - // because the text of EditBox has been already synced at the moment of text altered in the edit control - _this->Close(buttonId); - return TRUE; - } - - // text altered in the edit control - // Please refer to https://msdn.microsoft.com/en-us/library/bb761676%28v=vs.85%29.aspx - if (HIWORD(wParam) == EN_CHANGE && _this->_recursionDepth == 0) - { - std::u16string wstrResult; - std::string utf8Result; - - int inputLength = ::GetWindowTextLengthW(_this->_hwndEditCtrl); - wstrResult.resize(inputLength); - - ::GetWindowTextW(_this->_hwndEditCtrl, (LPWSTR) const_cast(wstrResult.c_str()), inputLength+1); - bool conversionResult = cocos2d::StringUtils::UTF16ToUTF8(wstrResult, utf8Result); - _this->_param->pstrResult->clear(); - ++_this->_recursionDepth; // Prevent recursive calls - if (conversionResult) - { - *(_this->_param->pstrResult) = std::move(utf8Result); - } - - // Invoke editBoxTextChanged indirectly - _this->_onTextChange(_this->_param->pstrResult->c_str()); - --_this->_recursionDepth; - return TRUE; - } - break; - } - } - return FALSE; -} - -void CWin32InputBox::SetText(const char* pText) -{ - if (_hwndEditCtrl != NULL) - { - std::u16string utf16Text; - cocos2d::StringUtils::UTF8ToUTF16(pText, utf16Text); - - // Keep the cursor position - // Please refer to : - // https://msdn.microsoft.com/en-us/library/bb761598%28v=vs.85%29.aspx - // https://msdn.microsoft.com/en-us/library/bb761661%28v=vs.85%29.aspx - size_t len = utf16Text.length(); - DWORD beginPos, endPos; - ::SendMessageW(_hwndEditCtrl, EM_GETSEL, (WPARAM)&beginPos, (LPARAM)&endPos); - ::SendMessageW(_hwndEditCtrl, WM_SETTEXT, 0, (LPARAM)utf16Text.c_str()); - ::SendMessageW(_hwndEditCtrl, EM_SETSEL, (WPARAM)std::min(beginPos, len), (LPARAM)std::min(endPos, len)); - } -} - -void CWin32InputBox::Close(INT_PTR buttonId) -{ - if (_getMsgHook != NULL) - { - ::UnhookWindowsHookEx(_getMsgHook); - _getMsgHook = NULL; - } - - if (_param->hDlg != NULL) - { - ::EnableWindow(_param->hwndOwner, TRUE); - ::SetFocus(_param->hwndOwner); - ::DestroyWindow(_param->hDlg); - _param->hDlg = NULL; - - _onClose(buttonId); - } -} +#include +#include NS_CC_BEGIN namespace ui { -EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) -{ - return new (std::nothrow) EditBoxImplWin(pEditBox); -} - -EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) -: EditBoxImpl(pEditText) -, _label(nullptr) -, _labelPlaceHolder(nullptr) -, _editBoxInputMode(EditBox::InputMode::SINGLE_LINE) -, _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS) -, _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT) -, _colText(Color3B::WHITE) -, _colPlaceHolder(Color3B::GRAY) -, _maxLength(-1) -, _isEditing(false) -{ - -} - -EditBoxImplWin::~EditBoxImplWin() -{ -} - -void EditBoxImplWin::doAnimationWhenKeyboardMove(float duration, float distance) -{ -} - -static const int CC_EDIT_BOX_PADDING = 5; - -bool EditBoxImplWin::initWithSize(const Size& size) -{ - //! int fontSize = getFontSizeAccordingHeightJni(size.height-12); - _label = Label::create(); - _label->setSystemFontSize(size.height-12); - // align the text vertically center - _label->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT); - _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height / 2.0f)); - _label->setTextColor(_colText); - _editBox->addChild(_label); - - _labelPlaceHolder = Label::create(); - _labelPlaceHolder->setSystemFontSize(size.height-12); - // align the text vertically center - _labelPlaceHolder->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT); - _labelPlaceHolder->setPosition(CC_EDIT_BOX_PADDING, size.height / 2.0f); - _labelPlaceHolder->setVisible(false); - _labelPlaceHolder->setTextColor(_colPlaceHolder); - _editBox->addChild(_labelPlaceHolder); - - _editSize = size; - return true; -} - -void EditBoxImplWin::setFont(const char* pFontName, int fontSize) -{ - if (_label != nullptr) - { - if (pFontName[0] != '\0') // To determine whether a string is empty quickly - { - _label->setSystemFontName(pFontName); - } - if(fontSize > 0) - { - _label->setSystemFontSize(fontSize); - } - } - - if (_labelPlaceHolder != nullptr) - { - if (pFontName[0] != '\0') // To determine whether a string is empty quickly - { - _labelPlaceHolder->setSystemFontName(pFontName); - } - if(fontSize > 0) - { - _labelPlaceHolder->setSystemFontSize(fontSize); - } - } -} - -void EditBoxImplWin::setFontColor(const Color4B& color) -{ - _colText = color; - _label->setTextColor(color); -} - -void EditBoxImplWin::setPlaceholderFont(const char* pFontName, int fontSize) -{ - if (_labelPlaceHolder != nullptr) - { - if (pFontName[0] != '\0') // To determine whether a string is empty quickly - { - _labelPlaceHolder->setSystemFontName(pFontName); - } - if(fontSize > 0) - { - _labelPlaceHolder->setSystemFontSize(fontSize); - } - } -} - -void EditBoxImplWin::setPlaceholderFontColor(const Color4B& color) -{ - _colPlaceHolder = color; - _labelPlaceHolder->setTextColor(color); -} - -void EditBoxImplWin::setInputMode(EditBox::InputMode inputMode) -{ - _editBoxInputMode = inputMode; -} - -void EditBoxImplWin::setMaxLength(int maxLength) -{ - _maxLength = maxLength; -} - -int EditBoxImplWin::getMaxLength() -{ - return _maxLength; -} - -void EditBoxImplWin::setInputFlag(EditBox::InputFlag inputFlag) -{ - _editBoxInputFlag = inputFlag; -} - -void EditBoxImplWin::setReturnType(EditBox::KeyboardReturnType returnType) -{ - _keyboardReturnType = returnType; -} - -bool EditBoxImplWin::isEditing() -{ - return _isEditing; -} - -void EditBoxImplWin::setText(const char* pText) -{ - if (pText != nullptr) + bool EditBoxImplWin::s_isInitialized = false; + int EditBoxImplWin::s_editboxChildID = 100; + HWND EditBoxImplWin::s_previousFocusWnd = 0; + WNDPROC EditBoxImplWin::s_prevCocosWndProc = 0; + HINSTANCE EditBoxImplWin::s_hInstance = 0; + HWND EditBoxImplWin::s_hwndCocos = 0; + void EditBoxImplWin::lazyInit() { - if (strcmp(_text.c_str(), pText) == 0) // do nothing if the text is not changed + s_hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); + LONG style = GetWindowLong(s_hwndCocos, GWL_STYLE); + SetWindowLong(s_hwndCocos, GWL_STYLE, style | WS_CLIPCHILDREN); + s_isInitialized = true; + s_previousFocusWnd = s_hwndCocos; + + s_hInstance = GetModuleHandle(NULL); + + + s_prevCocosWndProc = (WNDPROC)SetWindowLongPtr(s_hwndCocos, GWL_WNDPROC, (LONG_PTR)hookGLFWWindowProc); + + } + + EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) + { + return new (std::nothrow) EditBoxImplWin(pEditBox); + } + + EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) + : EditBoxImplCommon(pEditText), + _fontSize(40), + _changedTextManually(false) + { + if (!s_isInitialized) { - return; + lazyInit(); } + + s_editboxChildID++; - _text = pText; - if (_isEditing && s_win32InputBox != nullptr) // set text for the running Win32InputBox + } + + EditBoxImplWin::~EditBoxImplWin() + { + this->cleanupEditCtrl(); + } + + + bool EditBoxImplWin::isEditing() + { + return false; + } + + void EditBoxImplWin::cleanupEditCtrl() + { + if (hwndEdit) { - s_win32InputBox->SetText(pText); + SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)_prevWndProc); + DestroyWindow(hwndEdit); + hwndEdit = NULL; } + } - if (_text.length() > 0) + void EditBoxImplWin::createSingleLineEditCtrl() + { + this->cleanupEditCtrl(); + if (!hwndEdit) { - _labelPlaceHolder->setVisible(false); + hwndEdit = CreateWindowEx( + WS_EX_CLIENTEDGE, L"EDIT", // predefined class + NULL, // no window title + WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP | ES_AUTOHSCROLL, + 0, + 0, + 0, + 0, // set size in WM_SIZE message + s_hwndCocos, // parent window + (HMENU)s_editboxChildID, // edit control ID + s_hInstance, + this); // pointer not needed - if (EditBox::InputFlag::PASSWORD == _editBoxInputFlag) - { - long length = StringUtils::getCharacterCountInUTF8String(_text); - std::string strToShow(length, '*'); // Fills strToShow with length consecutive copies of character '*' - _label->setString(strToShow); - } - else - { - _label->setString(_text); - } + SetWindowLongPtr(hwndEdit, GWL_USERDATA, (LONG_PTR)this); + _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); - float maxWidth = _editSize.width - 2 * CC_EDIT_BOX_PADDING; - auto labelSize = _label->getContentSize(); - if (labelSize.width > maxWidth) - { - _label->setDimensions(maxWidth, labelSize.height); - } + ::SendMessageW(hwndEdit, EM_LIMITTEXT, this->_maxLength, 0); + + this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize); + } + } + + void EditBoxImplWin::createMultilineEditCtrl() + { + this->cleanupEditCtrl(); + if (!hwndEdit) + { + hwndEdit = CreateWindowEx( + WS_EX_CLIENTEDGE, L"EDIT", // predefined class + NULL, // no window title + WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP | ES_MULTILINE | ES_AUTOVSCROLL, + 0, + 0, + 0, + 0, // set size in WM_SIZE message + s_hwndCocos, // parent window + (HMENU)s_editboxChildID, // edit control ID + s_hInstance, + this); // pointer not needed + + //register new window proc func + SetWindowLongPtr(hwndEdit, GWL_USERDATA, (LONG_PTR)this); + _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); + + ::SendMessageW(hwndEdit, EM_LIMITTEXT, this->_maxLength, 0); + this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize); + + } + } + + void EditBoxImplWin::createNativeControl(const Rect & frame) + { + + this->createMultilineEditCtrl(); + } + + void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) + { + //not implemented yet + this->_fontSize = fontSize; + HFONT hFont = CreateFontW(fontSize, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, + CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial")); + + SendMessage(hwndEdit, // Handle of edit control + WM_SETFONT, // Message to change the font + (WPARAM)hFont, // handle of the font + MAKELPARAM(TRUE, 0) // Redraw text + ); + } + + void EditBoxImplWin::setNativeFontColor(const Color4B & color) + { + //not implemented yet + } + + void EditBoxImplWin::setNativePlaceholderFont(const char * pFontName, int fontSize) + { + //not implemented yet + } + + void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color) + { + //not implemented yet + } + + void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) + { + if (_editBoxInputMode == cocos2d::ui::EditBox::InputMode::ANY) + { + this->createMultilineEditCtrl(); + } + else if (_editBoxInputMode == cocos2d::ui::EditBox::InputMode::NUMERIC + || _editBoxInputMode == cocos2d::ui::EditBox::InputMode::DECIMAL + || _editBoxInputMode == cocos2d::ui::EditBox::InputMode::PHONE_NUMBER) + { + this->createSingleLineEditCtrl(); + ::SetWindowLongW(hwndEdit, GWL_STYLE, ::GetWindowLongW(hwndEdit, GWL_STYLE) | ES_NUMBER); } else { - _labelPlaceHolder->setVisible(true); - _label->setString(""); + this->createSingleLineEditCtrl(); } - } -} - -const char* EditBoxImplWin::getText() -{ - return _text.c_str(); -} - -void EditBoxImplWin::setPlaceHolder(const char* pText) -{ - if (pText != nullptr) + void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { - _placeHolder = pText; - if (_placeHolder.length() > 0 && _text.length() == 0) + if (_editBoxInputMode != cocos2d::ui::EditBox::InputMode::ANY) { - _labelPlaceHolder->setVisible(true); + this->createSingleLineEditCtrl(); + PostMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); + } + + if (inputFlag == EditBox::InputFlag::PASSWORD) + { + // Clear the password style + this->createSingleLineEditCtrl(); + } + else if (inputFlag == EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS) + { + ::SetWindowLongW(hwndEdit, GWL_STYLE, ::GetWindowLongW(hwndEdit, GWL_STYLE) | ES_UPPERCASE); + } + } + void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) + { + //not implemented yet + } + void EditBoxImplWin::setNativeText(const char* pText) + { + std::u16string utf16Result; + std::string text(pText); + cocos2d::StringUtils::UTF8ToUTF16(text, utf16Result); + ::SetWindowTextW(hwndEdit, (LPCWSTR)utf16Result.c_str()); + this->_changedTextManually = true; + } + void EditBoxImplWin::setNativePlaceHolder(const char* pText) + { + //not implemented yet + } + void EditBoxImplWin::setNativeVisible(bool visible) + { + if (visible) + { + ::ShowWindow(hwndEdit, SW_SHOW); + } + else + { + ::ShowWindow(hwndEdit, SW_HIDE); + } + } + void EditBoxImplWin::updateNativeFrame(const Rect& rect) + + { + ::SetWindowPos( + hwndEdit, + HWND_NOTOPMOST, + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + SWP_NOZORDER); + + } + const char* EditBoxImplWin::getNativeDefaultFontName() + { + return "Arial"; + } + void EditBoxImplWin::nativeOpenKeyboard() + { + if (s_previousFocusWnd != s_hwndCocos) { + ::ShowWindow(s_previousFocusWnd, SW_HIDE); } - _labelPlaceHolder->setString(_placeHolder); + ::PostMessage(hwndEdit, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = hwndEdit; + this->editBoxEditingDidBegin(); + } -} - -void EditBoxImplWin::setPosition(const Vec2& pos) -{ - //_label->setPosition(pos); - //_labelPlaceHolder->setPosition(pos); -} - -void EditBoxImplWin::setVisible(bool visible) -{ // don't need to be implemented on win32 platform. -} - -void EditBoxImplWin::setContentSize(const Size& size) -{ -} - -void EditBoxImplWin::setAnchorPoint(const Vec2& anchorPoint) -{ // don't need to be implemented on win32 platform. - -} - -void EditBoxImplWin::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) -{ -} - -void EditBoxImplWin::openKeyboard() -{ - if (s_win32InputBox != nullptr) + void EditBoxImplWin::nativeCloseKeyboard() { - return; + //don't need to implement } - - if (_delegate != nullptr) + void EditBoxImplWin::setNativeMaxLength(int maxLength) { - _delegate->editBoxEditingDidBegin(_editBox); + + ::SendMessageW(hwndEdit, EM_LIMITTEXT, maxLength, 0); + } - _isEditing = true; -#if CC_ENABLE_SCRIPT_BINDING - auto editBox = this->getEditBox(); - if (nullptr != editBox && 0 != editBox->getScriptEditBoxHandler()) + + void EditBoxImplWin::_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - CommonScriptData data(editBox->getScriptEditBoxHandler(), "began",editBox); - ScriptEvent event(kCommonEvent,(void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + switch (uMsg) + { + case WM_CHAR: + if (wParam == VK_RETURN) + { + if (_editBoxInputMode != cocos2d::ui::EditBox::InputMode::ANY) { + if (s_previousFocusWnd != s_hwndCocos) { + ::ShowWindow(s_previousFocusWnd, SW_HIDE); + ::PostMessage(s_hwndCocos, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0); + ::PostMessage(s_hwndCocos, WM_ACTIVATE, (WPARAM)s_previousFocusWnd, 0); + ::PostMessage(s_hwndCocos, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = s_hwndCocos; + } + } + this->editBoxEditingReturn(); + } + break; + case WM_SETFOCUS: + ::PostMessage(hwnd, WM_ACTIVATE, (WPARAM)s_previousFocusWnd, 0); + ::PostMessage(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = hwndEdit; + this->_changedTextManually = false; + break; + case WM_KILLFOCUS: + //when app enter background, this message also be called. + if (this->_editingMode && !IsWindowVisible(hwnd)) + { + this->editBoxEditingReturn(); + this->editBoxEditingDidEnd(this->getText()); + } + break; + default: + break; + + } } -#endif - std::string placeHolder = _labelPlaceHolder->getString(); - if (placeHolder.length() == 0) - placeHolder = "Enter value"; - - _editingText = getText(); - _originalText = _editingText; - auto glView = Director::getInstance()->getOpenGLView(); - HWND hwnd = glView->getWin32Window(); - CWin32InputBox::InputBox("Input", placeHolder.c_str(), &_editingText, _maxLength, false, hwnd, - _keyboardReturnType, _editBoxInputMode, _editBoxInputFlag, - std::bind(&EditBoxImplWin::onWin32InputBoxTextChange, this, std::placeholders::_1), - std::bind(&EditBoxImplWin::onWin32InputBoxClose, this, std::placeholders::_1)); -} - -void EditBoxImplWin::onWin32InputBoxClose(INT_PTR buttonId) -{ - bool didChange = buttonId == IDOK; - CC_SAFE_DELETE(s_win32InputBox); - _isEditing = false; - - setText(didChange ? _editingText.c_str() : _originalText.c_str()); - - if (_delegate != nullptr) + std::string EditBoxImplWin::getText()const { - _delegate->editBoxTextChanged(_editBox, getText()); - _delegate->editBoxEditingDidEnd(_editBox); - _delegate->editBoxEditingReturn(_editBox); + std::u16string wstrResult; + std::string utf8Result; + + + int inputLength = ::GetWindowTextLengthW(this->hwndEdit); + wstrResult.resize(inputLength); + + ::GetWindowTextW(this->hwndEdit, (LPWSTR) const_cast(wstrResult.c_str()), inputLength + 1); + bool conversionResult = cocos2d::StringUtils::UTF16ToUTF8(wstrResult, utf8Result); + if (!conversionResult) + { + CCLOG("warning, editbox input text convertion error."); + } + return std::move(utf8Result); } -#if CC_ENABLE_SCRIPT_BINDING - if (nullptr != _editBox && 0 != _editBox->getScriptEditBoxHandler()) + LRESULT EditBoxImplWin::hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed",_editBox); - ScriptEvent event(kCommonEvent,(void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - memset(data.eventName,0,sizeof(data.eventName)); - strncpy(data.eventName,"ended",sizeof(data.eventName)); - event.data = (void*)&data; - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - memset(data.eventName,0,sizeof(data.eventName)); - strncpy(data.eventName,"return",sizeof(data.eventName)); - event.data = (void*)&data; - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + switch (uMsg) + { + case WM_COMMAND: + if (HIWORD(wParam) == EN_CHANGE) { + EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr((HWND)lParam, GWLP_USERDATA); + if (pThis && !pThis->_changedTextManually) + { + pThis->editBoxEditingChanged(pThis->getText()); + pThis->_changedTextManually = false; + } + + } + break; + case WM_LBUTTONDOWN: + if (s_previousFocusWnd != s_hwndCocos) { + ::ShowWindow(s_previousFocusWnd, SW_HIDE); + ::PostMessage(s_hwndCocos, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = s_hwndCocos; + } + + break; + default: + break; + } + + return CallWindowProc(s_prevCocosWndProc, hwnd, uMsg, wParam, lParam); } -#endif // #if CC_ENABLE_SCRIPT_BINDING -} -void EditBoxImplWin::onWin32InputBoxTextChange(const char *pText) -{ - _isEditing = false; // Prevent recursive calls - setText(pText); - _isEditing = true; - - if (_delegate != nullptr) + LRESULT EditBoxImplWin::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - _delegate->editBoxTextChanged(_editBox, _text); + EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA); + if (pThis) + { + pThis->_WindowProc(hwnd, uMsg, wParam, lParam); + } + + return CallWindowProc(pThis->_prevWndProc, hwnd, uMsg, wParam, lParam); + } - -#if CC_ENABLE_SCRIPT_BINDING - if (nullptr != _editBox && 0 != _editBox->getScriptEditBoxHandler()) - { - CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox); - ScriptEvent event(kCommonEvent, (void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } -#endif // #if CC_ENABLE_SCRIPT_BINDING -} - -void EditBoxImplWin::closeKeyboard() -{ - // close the running Win32InputBox - if (s_win32InputBox != nullptr) - { - s_win32InputBox->Close(IDCANCEL); - CC_SAFE_DELETE(s_win32InputBox); - _isEditing = false; - } -} - -void EditBoxImplWin::onEnter() -{ -} - } NS_CC_END diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 0a3ca1f679..6e0a6197e3 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -29,8 +29,7 @@ #include "platform/CCPlatformConfig.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - -#include "UIEditBoxImpl.h" +#include "UIEditBoxImpl-common.h" NS_CC_BEGIN @@ -38,74 +37,54 @@ namespace ui { class EditBox; -class CC_GUI_DLL EditBoxImplWin : public EditBoxImpl +class CC_GUI_DLL EditBoxImplWin : public EditBoxImplCommon { public: - /** - * @js NA - */ + EditBoxImplWin(EditBox* pEditText); - /** - * @js NA - * @lua NA - */ virtual ~EditBoxImplWin(); - virtual bool initWithSize(const Size& size); - virtual void setFont(const char* pFontName, int fontSize); - virtual void setFontColor(const Color4B& color); - virtual void setPlaceholderFont(const char* pFontName, int fontSize); - virtual void setPlaceholderFontColor(const Color4B& color); - virtual void setInputMode(EditBox::InputMode inputMode); - virtual void setInputFlag(EditBox::InputFlag inputFlag); - virtual void setMaxLength(int maxLength); - virtual int getMaxLength(); - virtual void setReturnType(EditBox::KeyboardReturnType returnType); - virtual bool isEditing(); + virtual bool isEditing() override; + virtual void createNativeControl(const Rect& frame) override; + virtual void setNativeFont(const char* pFontName, int fontSize) override; + virtual void setNativeFontColor(const Color4B& color) override; + virtual void setNativePlaceholderFont(const char* pFontName, int fontSize) override; + virtual void setNativePlaceholderFontColor(const Color4B& color) override; + virtual void setNativeInputMode(EditBox::InputMode inputMode) override; + virtual void setNativeInputFlag(EditBox::InputFlag inputFlag) override; + virtual void setNativeReturnType(EditBox::KeyboardReturnType returnType)override; + virtual void setNativeText(const char* pText) override; + virtual void setNativePlaceHolder(const char* pText) override; + virtual void setNativeVisible(bool visible) override; + virtual void updateNativeFrame(const Rect& rect) override; + virtual const char* getNativeDefaultFontName() override; + virtual void nativeOpenKeyboard() override; + virtual void nativeCloseKeyboard() override; + virtual void setNativeMaxLength(int maxLength); - virtual void setText(const char* pText); - virtual const char* getText(); - virtual void setPlaceHolder(const char* pText); - virtual void setPosition(const Vec2& pos); - virtual void setVisible(bool visible); - virtual void setContentSize(const Size& size); - virtual void setAnchorPoint(const Vec2& anchorPoint); - /** - * @js NA - * @lua NA - */ - virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)override; - virtual void doAnimationWhenKeyboardMove(float duration, float distance); - virtual void openKeyboard(); - virtual void closeKeyboard(); - /** - * @js NA - * @lua NA - */ - virtual void onEnter(); private: + void createSingleLineEditCtrl(); + void createMultilineEditCtrl(); + void cleanupEditCtrl(); + std::string getText() const; + void _WindowProc(HWND, UINT, WPARAM, LPARAM); + + WNDPROC _prevWndProc; + static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); - Label* _label; - Label* _labelPlaceHolder; - EditBox::InputMode _editBoxInputMode; - EditBox::InputFlag _editBoxInputFlag; - EditBox::KeyboardReturnType _keyboardReturnType; + HWND hwndEdit; + //FIXME: fontSize should be in parent class + int _fontSize; + bool _changedTextManually; + static WNDPROC s_prevCocosWndProc; - std::string _text; - std::string _placeHolder; - - Color4B _colText; - Color4B _colPlaceHolder; - - int _maxLength; - Size _editSize; - - std::string _editingText; - std::string _originalText; - bool _isEditing; - - void onWin32InputBoxTextChange(const char *pText); - void onWin32InputBoxClose(INT_PTR buttonId); + static HINSTANCE s_hInstance; + static HWND s_hwndCocos; + static HWND s_previousFocusWnd; + static bool s_isInitialized; + static int s_editboxChildID; + static void lazyInit(); }; }