From f4e61963dab20b1d576d7235b5d9b8e681c008a9 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 5 May 2017 02:12:28 -0700 Subject: [PATCH 1/9] make win32 editbox inherit from UIEditBoxImpl-common --- native/cocos/2d/libcocos2d.vcxproj | 2 + native/cocos/2d/libcocos2d.vcxproj.filters | 6 + .../ui/UIEditBox/UIEditBoxImpl-android.cpp | 2 +- .../ui/UIEditBox/UIEditBoxImpl-common.cpp | 2 +- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 343 +++--------------- .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 82 +---- 6 files changed, 80 insertions(+), 357 deletions(-) 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..54ef791d4b 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 diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 6f699fd33e..98cb8e13d8 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 @@ -625,16 +626,7 @@ EditBoxImpl* __createSystemEditBox(EditBox* 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) +: EditBoxImplCommon(pEditText) { } @@ -643,312 +635,77 @@ 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; + return false; } -void EditBoxImplWin::setText(const char* pText) -{ - if (pText != nullptr) - { - if (strcmp(_text.c_str(), pText) == 0) // do nothing if the text is not changed - { - return; - } - - _text = pText; - if (_isEditing && s_win32InputBox != nullptr) // set text for the running Win32InputBox - { - s_win32InputBox->SetText(pText); - } - - if (_text.length() > 0) - { - _labelPlaceHolder->setVisible(false); - - 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); - } - - float maxWidth = _editSize.width - 2 * CC_EDIT_BOX_PADDING; - auto labelSize = _label->getContentSize(); - if (labelSize.width > maxWidth) - { - _label->setDimensions(maxWidth, labelSize.height); - } - } - else - { - _labelPlaceHolder->setVisible(true); - _label->setString(""); - } - - } -} - -const char* EditBoxImplWin::getText() -{ - return _text.c_str(); -} - -void EditBoxImplWin::setPlaceHolder(const char* pText) -{ - if (pText != nullptr) - { - _placeHolder = pText; - if (_placeHolder.length() > 0 && _text.length() == 0) - { - _labelPlaceHolder->setVisible(true); - } - - _labelPlaceHolder->setString(_placeHolder); - } -} - -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::createNativeControl(const Rect & frame) { } -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::setNativeFont(const char * pFontName, int fontSize) { } -void EditBoxImplWin::openKeyboard() + + +void EditBoxImplWin::setNativeFontColor(const Color4B & color) { - if (s_win32InputBox != nullptr) - { - return; - } - - if (_delegate != nullptr) - { - _delegate->editBoxEditingDidBegin(_editBox); - } - - _isEditing = true; -#if CC_ENABLE_SCRIPT_BINDING - auto editBox = this->getEditBox(); - if (nullptr != editBox && 0 != editBox->getScriptEditBoxHandler()) - { - CommonScriptData data(editBox->getScriptEditBoxHandler(), "began",editBox); - ScriptEvent event(kCommonEvent,(void*)&data); - ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); - } -#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) +void EditBoxImplWin::setNativePlaceholderFont(const char * pFontName, int fontSize) { - bool didChange = buttonId == IDOK; - CC_SAFE_DELETE(s_win32InputBox); - _isEditing = false; - - setText(didChange ? _editingText.c_str() : _originalText.c_str()); - - if (_delegate != nullptr) - { - _delegate->editBoxTextChanged(_editBox, getText()); - _delegate->editBoxEditingDidEnd(_editBox); - _delegate->editBoxEditingReturn(_editBox); - } - -#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); - 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); - } -#endif // #if CC_ENABLE_SCRIPT_BINDING } -void EditBoxImplWin::onWin32InputBoxTextChange(const char *pText) +void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color) { - _isEditing = false; // Prevent recursive calls - setText(pText); - _isEditing = true; - if (_delegate != nullptr) - { - _delegate->editBoxTextChanged(_editBox, _text); - } - -#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() +void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) { - // close the running Win32InputBox - if (s_win32InputBox != nullptr) - { - s_win32InputBox->Close(IDCANCEL); - CC_SAFE_DELETE(s_win32InputBox); - _isEditing = false; - } -} -void EditBoxImplWin::onEnter() +} +void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { + +} +void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) +{ + +} +void EditBoxImplWin::setNativeText(const char* pText) +{ + +} +void EditBoxImplWin::setNativePlaceHolder(const char* pText) +{ + +} +void EditBoxImplWin::setNativeVisible(bool visible) +{ + +} +void EditBoxImplWin::updateNativeFrame(const Rect& rect) +{ + +} +const char* EditBoxImplWin::getNativeDefaultFontName() +{ + return "Arial"; +} +void EditBoxImplWin::nativeOpenKeyboard() +{ + +} +void EditBoxImplWin::nativeCloseKeyboard() +{ + +} +void EditBoxImplWin::setNativeMaxLength(int maxLength) +{ + } } diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 0a3ca1f679..25dd4ecc81 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -30,7 +30,7 @@ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) -#include "UIEditBoxImpl.h" +#include "UIEditBoxImpl-common.h" NS_CC_BEGIN @@ -38,74 +38,32 @@ 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 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(); + 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); private: - Label* _label; - Label* _labelPlaceHolder; - EditBox::InputMode _editBoxInputMode; - EditBox::InputFlag _editBoxInputFlag; - EditBox::KeyboardReturnType _keyboardReturnType; - - 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); }; } From 479475b06ccaa10e5e6ca13613490e5ce611674f Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Sun, 7 May 2017 23:44:55 -0700 Subject: [PATCH 2/9] create editbox on top of cocos view --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 651 ++---------------- .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 11 +- 2 files changed, 78 insertions(+), 584 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 98cb8e13d8..260e0fdcfd 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -29,597 +29,29 @@ #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" +#include +#include -#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); - } -} NS_CC_BEGIN namespace ui { +bool EditBoxImplWin::s_isInitialized = false; +int EditBoxImplWin::s_editboxChildID = 100; +void EditBoxImplWin::lazyInit() +{ + HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); + LONG style = GetWindowLong(hwnd, GWL_STYLE); + SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN); + s_isInitialized = true; +} + EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) { return new (std::nothrow) EditBoxImplWin(pEditBox); @@ -628,6 +60,14 @@ EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) : EditBoxImplCommon(pEditText) { + if (!s_isInitialized) + { + lazyInit(); + } + + hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); + hInstance = GetModuleHandle(NULL); + s_editboxChildID++; } @@ -635,6 +75,7 @@ EditBoxImplWin::~EditBoxImplWin() { } + bool EditBoxImplWin::isEditing() { return false; @@ -642,6 +83,16 @@ bool EditBoxImplWin::isEditing() void EditBoxImplWin::createNativeControl(const Rect & frame) { + hwndEdit = CreateWindowEx( + WS_EX_CLIENTEDGE, L"EDIT", // predefined class + NULL, // no window title + WS_CHILD | + ES_LEFT | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_EX_TRANSPARENT, + 2, 14, 200, 50, // set size in WM_SIZE message + hwndCocos, // parent window + (HMENU)s_editboxChildID, // edit control ID + hInstance, + NULL); // pointer not needed } void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) @@ -665,11 +116,19 @@ void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color) void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) { - + // 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); } void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { + // // 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); } void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) { @@ -685,10 +144,26 @@ void EditBoxImplWin::setNativePlaceHolder(const char* pText) } void EditBoxImplWin::setNativeVisible(bool visible) { - + if (visible) + { + ::ShowWindow(hwndEdit, SW_SHOW); + } + else + { + ::ShowWindow(hwndEdit, SW_HIDE); + } } void EditBoxImplWin::updateNativeFrame(const Rect& rect) + { + // ::SetWindowPos( + // hwndEdit2, + // HWND_NOTOPMOST, + // rectEdit1.left - rectDlg.left, + // (rectEdit1.top - rectDlg.top) - (rectEdit1.bottom - rectEdit1.top), + // 0, + // 0, + // SWP_NOSIZE | SWP_NOZORDER); } const char* EditBoxImplWin::getNativeDefaultFontName() @@ -705,7 +180,19 @@ void EditBoxImplWin::nativeCloseKeyboard() } void EditBoxImplWin::setNativeMaxLength(int maxLength) { + // // 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); + // } +} +LRESULT EditBoxImplWin::WndProc(HWND, UINT, WPARAM, LPARAM) +{ + return LRESULT(); } } diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 25dd4ecc81..82a9a76c87 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -29,7 +29,6 @@ #include "platform/CCPlatformConfig.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - #include "UIEditBoxImpl-common.h" NS_CC_BEGIN @@ -62,8 +61,16 @@ public: virtual void nativeOpenKeyboard() override; virtual void nativeCloseKeyboard() override; virtual void setNativeMaxLength(int maxLength); -private: +private: + static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); + HWND hwndEdit; + HWND hwndCocos; + HINSTANCE hInstance; + + static bool s_isInitialized; + static int s_editboxChildID; + static void lazyInit(); }; } From 27ccd45770e8ac20bcf1c14038ed7196d5a79265 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 8 May 2017 00:53:15 -0700 Subject: [PATCH 3/9] try to use CEdit --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 260e0fdcfd..9cd03c8032 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -86,13 +86,21 @@ void EditBoxImplWin::createNativeControl(const Rect & frame) hwndEdit = CreateWindowEx( WS_EX_CLIENTEDGE, L"EDIT", // predefined class NULL, // no window title - WS_CHILD | - ES_LEFT | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_EX_TRANSPARENT, - 2, 14, 200, 50, // set size in WM_SIZE message + WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT, + frame.origin.x, + frame.origin.y, + frame.size.width, + frame.size.height, // set size in WM_SIZE message hwndCocos, // parent window (HMENU)s_editboxChildID, // edit control ID hInstance, NULL); // pointer not needed + + // Clear the password style + SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); + + + } void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) @@ -124,6 +132,15 @@ void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) } void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { + if (inputFlag == EditBox::InputFlag::PASSWORD) + { + SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)'*', (LPARAM)0); + + } + else if (inputFlag == 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 @@ -156,14 +173,14 @@ void EditBoxImplWin::setNativeVisible(bool visible) void EditBoxImplWin::updateNativeFrame(const Rect& rect) { - // ::SetWindowPos( - // hwndEdit2, - // HWND_NOTOPMOST, - // rectEdit1.left - rectDlg.left, - // (rectEdit1.top - rectDlg.top) - (rectEdit1.bottom - rectEdit1.top), - // 0, - // 0, - // SWP_NOSIZE | SWP_NOZORDER); + ::SetWindowPos( + hwndEdit, + HWND_NOTOPMOST, + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + SWP_NOZORDER); } const char* EditBoxImplWin::getNativeDefaultFontName() From 143a82c7c0f4931ef4f5d449e12b02cd9fb540b0 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 8 May 2017 03:04:47 -0700 Subject: [PATCH 4/9] fix the skeleton of editbox --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 347 ++++++++++-------- .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 7 +- 2 files changed, 203 insertions(+), 151 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 9cd03c8032..1f8dd8322a 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -42,176 +42,223 @@ NS_CC_BEGIN namespace ui { -bool EditBoxImplWin::s_isInitialized = false; -int EditBoxImplWin::s_editboxChildID = 100; -void EditBoxImplWin::lazyInit() -{ - HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); - LONG style = GetWindowLong(hwnd, GWL_STYLE); - SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN); - s_isInitialized = true; -} - -EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) -{ - return new (std::nothrow) EditBoxImplWin(pEditBox); -} - -EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) -: EditBoxImplCommon(pEditText) -{ - if (!s_isInitialized) + bool EditBoxImplWin::s_isInitialized = false; + int EditBoxImplWin::s_editboxChildID = 100; + void EditBoxImplWin::lazyInit() { - lazyInit(); + HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); + LONG style = GetWindowLong(hwnd, GWL_STYLE); + SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN); + s_isInitialized = true; } - hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); - hInstance = GetModuleHandle(NULL); - s_editboxChildID++; - -} - -EditBoxImplWin::~EditBoxImplWin() -{ -} - - -bool EditBoxImplWin::isEditing() -{ - return false; -} - -void EditBoxImplWin::createNativeControl(const Rect & frame) -{ - hwndEdit = CreateWindowEx( - WS_EX_CLIENTEDGE, L"EDIT", // predefined class - NULL, // no window title - WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT, - frame.origin.x, - frame.origin.y, - frame.size.width, - frame.size.height, // set size in WM_SIZE message - hwndCocos, // parent window - (HMENU)s_editboxChildID, // edit control ID - hInstance, - NULL); // pointer not needed - - // Clear the password style - SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); - - - -} - -void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) -{ -} - - - -void EditBoxImplWin::setNativeFontColor(const Color4B & color) -{ -} - -void EditBoxImplWin::setNativePlaceholderFont(const char * pFontName, int fontSize) -{ -} - -void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color) -{ - -} - -void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) -{ - // 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); -} -void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) -{ - if (inputFlag == EditBox::InputFlag::PASSWORD) + EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) { - SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)'*', (LPARAM)0); + return new (std::nothrow) EditBoxImplWin(pEditBox); + } + + EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) + : EditBoxImplCommon(pEditText) + { + if (!s_isInitialized) + { + lazyInit(); + } + + hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); + hInstance = GetModuleHandle(NULL); + s_editboxChildID++; } - else if (inputFlag == EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS) + + EditBoxImplWin::~EditBoxImplWin() + { + if (hwndEdit) + { + SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)_prevWndProc); + DestroyWindow(hwndEdit); + } + } + + + bool EditBoxImplWin::isEditing() + { + return false; + } + + void EditBoxImplWin::createNativeControl(const Rect & frame) + { + hwndEdit = CreateWindowEx( + WS_EX_CLIENTEDGE, L"EDIT", // predefined class + NULL, // no window title + WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT, + frame.origin.x, + frame.origin.y, + frame.size.width, + frame.size.height, // set size in WM_SIZE message + hwndCocos, // parent window + (HMENU)s_editboxChildID, // edit control ID + hInstance, + this); // pointer not needed + + // Clear the password style + SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); + + //register new window proc func + SetWindowLongPtr(hwndEdit, GWL_USERDATA, (LONG_PTR)this); + _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); + + + } + + void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) + { + } + + + + void EditBoxImplWin::setNativeFontColor(const Color4B & color) + { + } + + void EditBoxImplWin::setNativePlaceholderFont(const char * pFontName, int fontSize) + { + } + + void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color) { } - // // 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); -} -void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) -{ - -} -void EditBoxImplWin::setNativeText(const char* pText) -{ - -} -void EditBoxImplWin::setNativePlaceHolder(const char* pText) -{ - -} -void EditBoxImplWin::setNativeVisible(bool visible) -{ - if (visible) + void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) { - ::ShowWindow(hwndEdit, SW_SHOW); + // 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); } - else + void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { - ::ShowWindow(hwndEdit, SW_HIDE); + if (inputFlag == EditBox::InputFlag::PASSWORD) + { + //SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)'*', (LPARAM)0); + + } + else if (inputFlag == 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); } -} -void EditBoxImplWin::updateNativeFrame(const Rect& rect) + void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) + { -{ - ::SetWindowPos( - hwndEdit, - HWND_NOTOPMOST, - rect.origin.x, - rect.origin.y, - rect.size.width, - rect.size.height, - SWP_NOZORDER); + } + void EditBoxImplWin::setNativeText(const char* pText) + { -} -const char* EditBoxImplWin::getNativeDefaultFontName() -{ - return "Arial"; -} -void EditBoxImplWin::nativeOpenKeyboard() -{ + } + void EditBoxImplWin::setNativePlaceHolder(const char* pText) + { -} -void EditBoxImplWin::nativeCloseKeyboard() -{ + } + void EditBoxImplWin::setNativeVisible(bool visible) + { + if (visible) + { + ::ShowWindow(hwndEdit, SW_SHOW); + } + else + { + ::ShowWindow(hwndEdit, SW_HIDE); + } + } + void EditBoxImplWin::updateNativeFrame(const Rect& rect) -} -void EditBoxImplWin::setNativeMaxLength(int maxLength) -{ - // // 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); - // } -} + { + ::SetWindowPos( + hwndEdit, + HWND_NOTOPMOST, + rect.origin.x, + rect.origin.y, + rect.size.width, + rect.size.height, + SWP_NOZORDER); -LRESULT EditBoxImplWin::WndProc(HWND, UINT, WPARAM, LPARAM) -{ - return LRESULT(); -} + } + const char* EditBoxImplWin::getNativeDefaultFontName() + { + return "Arial"; + } + void EditBoxImplWin::nativeOpenKeyboard() + { + } + void EditBoxImplWin::nativeCloseKeyboard() + { + + } + void EditBoxImplWin::setNativeMaxLength(int maxLength) + { + // // 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); + // } + } + + + void EditBoxImplWin::_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) + { + switch (uMsg) + { + case WM_CHAR: + if (wParam == VK_RETURN) + { + CCLOG("return key pressed"); + } + break; + case WM_KEYUP: + { + 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); + /* this->_param->pstrResult->clear(); + if (conversionResult) + { + *(_this->_param->pstrResult) = std::move(utf8Result); + } +*/ + // Invoke editBoxTextChanged indirectly + //this->_onTextChange(_this->_param->pstrResult->c_str()); + CCLOG("%s", utf8Result.c_str()); + } + break; + + } + } + + LRESULT EditBoxImplWin::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) + { + EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA); + pThis->_WindowProc(hwnd, uMsg, wParam, lParam); + + return CallWindowProc(pThis->_prevWndProc, hwnd, uMsg, wParam, lParam); + + } } NS_CC_END diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 82a9a76c87..94a36cf866 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -63,7 +63,12 @@ public: virtual void setNativeMaxLength(int maxLength); private: - static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); + void _WindowProc(HWND, UINT, WPARAM, LPARAM); + + WNDPROC _prevWndProc; + static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + + HWND hwndEdit; HWND hwndCocos; HINSTANCE hInstance; From 475a176cd3e030cf2d26ef7107625d7fbafa4017 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 9 May 2017 03:09:26 -0700 Subject: [PATCH 5/9] fix cursor issue --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 21 +++++++++++++++++-- .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 1f8dd8322a..eba14358f7 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -44,12 +44,14 @@ namespace ui { bool EditBoxImplWin::s_isInitialized = false; int EditBoxImplWin::s_editboxChildID = 100; + HWND EditBoxImplWin::s_previousFocusWnd = 0; void EditBoxImplWin::lazyInit() { HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); LONG style = GetWindowLong(hwnd, GWL_STYLE); SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN); s_isInitialized = true; + s_previousFocusWnd = hwnd; } EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) @@ -91,7 +93,7 @@ namespace ui { hwndEdit = CreateWindowEx( WS_EX_CLIENTEDGE, L"EDIT", // predefined class NULL, // no window title - WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT, + WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP, frame.origin.x, frame.origin.y, frame.size.width, @@ -197,11 +199,17 @@ namespace ui { } void EditBoxImplWin::nativeOpenKeyboard() { + if (s_previousFocusWnd != hwndCocos) { + ::ShowWindow(s_previousFocusWnd, SW_HIDE); + } + ::PostMessage(hwndEdit, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = hwndEdit; + } void EditBoxImplWin::nativeCloseKeyboard() { - + // ::PostMessage(hwndEdit, WM_KILLFOCUS, 0, 0); } void EditBoxImplWin::setNativeMaxLength(int maxLength) { @@ -226,10 +234,19 @@ namespace ui { CCLOG("return key pressed"); } break; + case WM_SETFOCUS: + ::PostMessage(hwnd, WM_ACTIVATE, (WPARAM)s_previousFocusWnd, 0); + ::PostMessage(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); + s_previousFocusWnd = hwndEdit; + break; + case WM_KILLFOCUS: + break; case WM_KEYUP: { + std::u16string wstrResult; std::string utf8Result; + int inputLength = ::GetWindowTextLengthW(this->hwndEdit); wstrResult.resize(inputLength); diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 94a36cf866..5d01c1d83b 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -73,6 +73,7 @@ private: HWND hwndCocos; HINSTANCE hInstance; + static HWND s_previousFocusWnd; static bool s_isInitialized; static int s_editboxChildID; static void lazyInit(); From 0e614765ac0030e715ccef4a4ffe7476683d4ba0 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 10 May 2017 18:29:28 -0700 Subject: [PATCH 6/9] fix win32 password --- .../ui/UIEditBox/UIEditBoxImpl-common.cpp | 8 +- .../cocos/ui/UIEditBox/UIEditBoxImpl-common.h | 3 +- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 99 +++++++++++++------ .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 9 +- 4 files changed, 85 insertions(+), 34 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp index 54ef791d4b..b86bfcd444 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp @@ -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..63b4726b36 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h @@ -113,6 +113,8 @@ public: virtual void nativeCloseKeyboard() = 0; virtual void setNativeMaxLength(int maxLength) {}; +protected: + bool _editingMode; private: void initInactiveLabels(const Size& size); @@ -133,7 +135,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 eba14358f7..8af2e7e0db 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -37,7 +37,6 @@ #include #include - NS_CC_BEGIN namespace ui { @@ -45,13 +44,21 @@ namespace ui { 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() { - HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); - LONG style = GetWindowLong(hwnd, GWL_STYLE); - SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN); + 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 = hwnd; + s_previousFocusWnd = s_hwndCocos; + + s_hInstance = GetModuleHandle(NULL); + + s_prevCocosWndProc = (WNDPROC)SetWindowLongPtr(s_hwndCocos, GWL_WNDPROC, (LONG_PTR)hookGLFWWindowProc); + } EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) @@ -67,8 +74,6 @@ namespace ui { lazyInit(); } - hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); - hInstance = GetModuleHandle(NULL); s_editboxChildID++; } @@ -98,9 +103,9 @@ namespace ui { frame.origin.y, frame.size.width, frame.size.height, // set size in WM_SIZE message - hwndCocos, // parent window + s_hwndCocos, // parent window (HMENU)s_editboxChildID, // edit control ID - hInstance, + s_hInstance, this); // pointer not needed // Clear the password style @@ -199,7 +204,7 @@ namespace ui { } void EditBoxImplWin::nativeOpenKeyboard() { - if (s_previousFocusWnd != hwndCocos) { + if (s_previousFocusWnd != s_hwndCocos) { ::ShowWindow(s_previousFocusWnd, SW_HIDE); } @@ -232,6 +237,8 @@ namespace ui { if (wParam == VK_RETURN) { CCLOG("return key pressed"); + this->editBoxEditingReturn(); + this->editBoxEditingDidEnd(this->getText()); } break; case WM_SETFOCUS: @@ -239,28 +246,22 @@ namespace ui { ::PostMessage(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); s_previousFocusWnd = hwndEdit; break; - case WM_KILLFOCUS: + case WM_PARENTNOTIFY: + if (wParam == WM_LBUTTONDOWN) + { + //do you staff + CCLOG("WM_PARENTNOTIFY"); + } + break; + case WM_KILLFOCUS: + //when app enter background, this message also be called. + if (!this->_editingMode) { + s_previousFocusWnd = s_hwndCocos; + } break; case WM_KEYUP: { - - 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); - /* this->_param->pstrResult->clear(); - if (conversionResult) - { - *(_this->_param->pstrResult) = std::move(utf8Result); - } -*/ - // Invoke editBoxTextChanged indirectly - //this->_onTextChange(_this->_param->pstrResult->c_str()); + std::string utf8Result = this->getText(); CCLOG("%s", utf8Result.c_str()); } break; @@ -268,6 +269,46 @@ namespace ui { } } + std::string EditBoxImplWin::getText()const + { + 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); + } + + LRESULT EditBoxImplWin::hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) + { + switch (uMsg) + { + + case WM_PARENTNOTIFY: + if (wParam == WM_LBUTTONDOWN) + { + //do you staff + CCLOG("WM_PARENTNOTIFY"); + } + break; + case WM_LBUTTONDOWN: + CCLOG("WM_LBUTTONDOWN"); + break; + default: + break; + } + + return CallWindowProc(s_prevCocosWndProc, hwnd, uMsg, wParam, lParam); + } + LRESULT EditBoxImplWin::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA); diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 5d01c1d83b..a9319e1bd2 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -63,16 +63,19 @@ public: virtual void setNativeMaxLength(int maxLength); private: + 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); HWND hwndEdit; - HWND hwndCocos; - HINSTANCE hInstance; + static WNDPROC s_prevCocosWndProc; + + static HINSTANCE s_hInstance; + static HWND s_hwndCocos; static HWND s_previousFocusWnd; static bool s_isInitialized; static int s_editboxChildID; From 4137fabd2ec7906b7329961c88898d8151ce5473 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 11 May 2017 14:30:16 +0800 Subject: [PATCH 7/9] add editbox event handling --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 8af2e7e0db..ed2e048218 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -57,6 +57,7 @@ namespace ui { s_hInstance = GetModuleHandle(NULL); + s_prevCocosWndProc = (WNDPROC)SetWindowLongPtr(s_hwndCocos, GWL_WNDPROC, (LONG_PTR)hookGLFWWindowProc); } @@ -73,7 +74,7 @@ namespace ui { { lazyInit(); } - + s_editboxChildID++; } @@ -210,6 +211,7 @@ namespace ui { ::PostMessage(hwndEdit, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0); s_previousFocusWnd = hwndEdit; + this->editBoxEditingDidBegin(); } void EditBoxImplWin::nativeCloseKeyboard() @@ -236,7 +238,6 @@ namespace ui { case WM_CHAR: if (wParam == VK_RETURN) { - CCLOG("return key pressed"); this->editBoxEditingReturn(); this->editBoxEditingDidEnd(this->getText()); } @@ -246,24 +247,15 @@ namespace ui { ::PostMessage(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); s_previousFocusWnd = hwndEdit; break; - case WM_PARENTNOTIFY: - if (wParam == WM_LBUTTONDOWN) - { - //do you staff - CCLOG("WM_PARENTNOTIFY"); - } - break; case WM_KILLFOCUS: //when app enter background, this message also be called. - if (!this->_editingMode) { - s_previousFocusWnd = s_hwndCocos; + if (this->_editingMode && !IsWindowVisible(hwnd)) + { + this->editBoxEditingReturn(); + this->editBoxEditingDidEnd(this->getText()); } break; - case WM_KEYUP: - { - std::string utf8Result = this->getText(); - CCLOG("%s", utf8Result.c_str()); - } + default: break; } @@ -291,16 +283,25 @@ namespace ui { { switch (uMsg) { + case WM_COMMAND: + if (HIWORD(wParam) == EN_CHANGE) { + EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr((HWND)lParam, GWLP_USERDATA); + if (pThis) + { + pThis->editBoxEditingChanged(pThis->getText()); + } - case WM_PARENTNOTIFY: - if (wParam == WM_LBUTTONDOWN) - { - //do you staff - CCLOG("WM_PARENTNOTIFY"); } break; case WM_LBUTTONDOWN: - CCLOG("WM_LBUTTONDOWN"); + 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; + } + break; default: break; @@ -312,7 +313,10 @@ namespace ui { LRESULT EditBoxImplWin::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA); - pThis->_WindowProc(hwnd, uMsg, wParam, lParam); + if (pThis) + { + pThis->_WindowProc(hwnd, uMsg, wParam, lParam); + } return CallWindowProc(pThis->_prevWndProc, hwnd, uMsg, wParam, lParam); From a100de44eb2f400903a2650a22f2332b27f4f3a9 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 11 May 2017 16:14:08 +0800 Subject: [PATCH 8/9] almost finish win32 editbox refactoring --- .../cocos/ui/UIEditBox/UIEditBoxImpl-common.h | 1 - .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 154 ++++++++++++------ .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 3 + 3 files changed, 107 insertions(+), 51 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h index 63b4726b36..691f7d7f21 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-common.h @@ -116,7 +116,6 @@ public: protected: bool _editingMode; -private: void initInactiveLabels(const Size& size); void setInactiveText(const char* pText); void placeInactiveLabels(const Size& size); diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index ed2e048218..25b3c5b5f9 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -81,11 +81,7 @@ namespace ui { EditBoxImplWin::~EditBoxImplWin() { - if (hwndEdit) - { - SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)_prevWndProc); - DestroyWindow(hwndEdit); - } + this->cleanupEditCtrl(); } @@ -94,29 +90,73 @@ namespace ui { return false; } + void EditBoxImplWin::cleanupEditCtrl() + { + if (hwndEdit) + { + SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)_prevWndProc); + DestroyWindow(hwndEdit); + hwndEdit = NULL; + } + } + + void EditBoxImplWin::createSingleLineEditCtrl() + { + 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_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 + + SetWindowLongPtr(hwndEdit, GWL_USERDATA, (LONG_PTR)this); + _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); + + ::SendMessageW(hwndEdit, EM_LIMITTEXT, this->_maxLength, 0); + } + } + + 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); + + + } + } + void EditBoxImplWin::createNativeControl(const Rect & frame) { - hwndEdit = CreateWindowEx( - WS_EX_CLIENTEDGE, L"EDIT", // predefined class - NULL, // no window title - WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP, - frame.origin.x, - frame.origin.y, - frame.size.width, - frame.size.height, // set size in WM_SIZE message - s_hwndCocos, // parent window - (HMENU)s_editboxChildID, // edit control ID - s_hInstance, - this); // pointer not needed - - // Clear the password style - SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); - - //register new window proc func - SetWindowLongPtr(hwndEdit, GWL_USERDATA, (LONG_PTR)this); - _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); - - + + this->createMultilineEditCtrl(); } void EditBoxImplWin::setNativeFont(const char * pFontName, int fontSize) @@ -140,28 +180,39 @@ namespace ui { void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode) { - // 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); + 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 + { + this->createSingleLineEditCtrl(); + } } void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag) { + if (_editBoxInputMode != cocos2d::ui::EditBox::InputMode::ANY) + { + this->createSingleLineEditCtrl(); + PostMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0); + } + if (inputFlag == EditBox::InputFlag::PASSWORD) { - //SendMessage(hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)'*', (LPARAM)0); - + // 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); } - - // // 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); } void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType) { @@ -220,14 +271,9 @@ namespace ui { } void EditBoxImplWin::setNativeMaxLength(int maxLength) { - // // 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); - // } + + ::SendMessageW(hwndEdit, EM_LIMITTEXT, maxLength, 0); + } @@ -238,8 +284,16 @@ namespace ui { 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(); - this->editBoxEditingDidEnd(this->getText()); } break; case WM_SETFOCUS: @@ -297,8 +351,8 @@ namespace ui { 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); + //::PostMessage(s_hwndCocos, WM_ACTIVATE, (WPARAM)s_previousFocusWnd, 0); + // ::PostMessage(s_hwndCocos, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0); s_previousFocusWnd = s_hwndCocos; } diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index a9319e1bd2..5b68db9c60 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -63,6 +63,9 @@ public: virtual void setNativeMaxLength(int maxLength); private: + void createSingleLineEditCtrl(); + void createMultilineEditCtrl(); + void cleanupEditCtrl(); std::string getText() const; void _WindowProc(HWND, UINT, WPARAM, LPARAM); From 6e2db24d6696e38581977ecbce9e2892114c1321 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Thu, 11 May 2017 17:03:37 +0800 Subject: [PATCH 9/9] finish editbox font change --- .../ui/UIEditBox/UIEditBoxImpl-win32.cpp | 42 +++++++++++++------ .../cocos/ui/UIEditBox/UIEditBoxImpl-win32.h | 4 +- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp index 25b3c5b5f9..d66b6a4da1 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.cpp @@ -68,7 +68,9 @@ namespace ui { } EditBoxImplWin::EditBoxImplWin(EditBox* pEditText) - : EditBoxImplCommon(pEditText) + : EditBoxImplCommon(pEditText), + _fontSize(40), + _changedTextManually(false) { if (!s_isInitialized) { @@ -122,6 +124,8 @@ namespace ui { _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); ::SendMessageW(hwndEdit, EM_LIMITTEXT, this->_maxLength, 0); + + this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize); } } @@ -148,7 +152,7 @@ namespace ui { _prevWndProc = (WNDPROC)SetWindowLongPtr(hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc); ::SendMessageW(hwndEdit, EM_LIMITTEXT, this->_maxLength, 0); - + this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize); } } @@ -161,21 +165,31 @@ namespace ui { 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) @@ -216,15 +230,19 @@ namespace ui { } 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) { @@ -267,7 +285,7 @@ namespace ui { } void EditBoxImplWin::nativeCloseKeyboard() { - // ::PostMessage(hwndEdit, WM_KILLFOCUS, 0, 0); + //don't need to implement } void EditBoxImplWin::setNativeMaxLength(int maxLength) { @@ -300,6 +318,7 @@ namespace ui { ::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. @@ -340,9 +359,10 @@ namespace ui { case WM_COMMAND: if (HIWORD(wParam) == EN_CHANGE) { EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtr((HWND)lParam, GWLP_USERDATA); - if (pThis) + if (pThis && !pThis->_changedTextManually) { pThis->editBoxEditingChanged(pThis->getText()); + pThis->_changedTextManually = false; } } @@ -351,8 +371,6 @@ namespace ui { 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; } diff --git a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h index 5b68db9c60..6e0a6197e3 100644 --- a/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h +++ b/native/cocos/ui/UIEditBox/UIEditBoxImpl-win32.h @@ -74,7 +74,9 @@ private: static LRESULT CALLBACK hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); HWND hwndEdit; - + //FIXME: fontSize should be in parent class + int _fontSize; + bool _changedTextManually; static WNDPROC s_prevCocosWndProc; static HINSTANCE s_hInstance;