Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to allow CTRL+A key combination to work with multiple EditBox instances #2246

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions core/ui/UIEditBox/UIEditBoxImpl-win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ HWND EditBoxImplWin::s_previousFocusWnd = 0;
WNDPROC EditBoxImplWin::s_prevCocosWndProc = 0;
HINSTANCE EditBoxImplWin::s_hInstance = 0;
HWND EditBoxImplWin::s_hwndCocos = 0;
ATOM EditBoxImplWin::s_hotKeyIdCtrlA = 0;
int EditBoxImplWin::s_editBoxCount = 0;
bool EditBoxImplWin::s_editBoxFocused = false;

void EditBoxImplWin::lazyInit()
{
Expand All @@ -73,7 +76,7 @@ EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)

EditBoxImplWin::EditBoxImplWin(EditBox* pEditText)
: EditBoxImplCommon(pEditText)
, _hotKeyIdCtrlA(0)
, _prevWndProc(NULL)
, _hwndEdit(NULL)
, _changedTextManually(false)
, _hasFocus(false)
Expand All @@ -85,6 +88,12 @@ EditBoxImplWin::EditBoxImplWin(EditBox* pEditText)
}

s_editboxChildID++;

if (s_hotKeyIdCtrlA == 0)
{
s_hotKeyIdCtrlA = GlobalAddAtom(L"CTRL+A");
RegisterHotKey(s_hwndCocos, s_hotKeyIdCtrlA, MOD_CONTROL | MOD_NOREPEAT, 'A');
}
}

EditBoxImplWin::~EditBoxImplWin()
Expand All @@ -101,16 +110,20 @@ void EditBoxImplWin::cleanupEditCtrl()
{
if (_hwndEdit)
{
UnregisterHotKey(_hwndEdit, _hotKeyIdCtrlA);
GlobalDeleteAtom(_hotKeyIdCtrlA);
_hotKeyIdCtrlA = 0;

SetWindowLongPtrW(_hwndEdit, GWLP_WNDPROC, (LONG_PTR)_prevWndProc);
::DestroyWindow(_hwndEdit);
_hasFocus = false;
_changedTextManually = false;
_editingMode = false;
_hwndEdit = NULL;
--s_editBoxCount;

if (s_editBoxCount == 0)
{
UnregisterHotKey(_hwndEdit, s_hotKeyIdCtrlA);
GlobalDeleteAtom(s_hotKeyIdCtrlA);
s_hotKeyIdCtrlA = 0;
}
}
}

Expand All @@ -119,6 +132,7 @@ void EditBoxImplWin::createEditCtrl(bool singleLine)
this->cleanupEditCtrl();
if (!_hwndEdit)
{
++s_editBoxCount;
_hwndEdit = ::CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", // predefined class
NULL, // no window title
WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP | ES_AUTOHSCROLL |
Expand All @@ -137,9 +151,6 @@ void EditBoxImplWin::createEditCtrl(bool singleLine)
s_previousFocusWnd = s_hwndCocos;
this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize);
this->setNativeText(this->_text.c_str());

_hotKeyIdCtrlA = GlobalAddAtom(L"CTRL+A");
RegisterHotKey(_hwndEdit, _hotKeyIdCtrlA, MOD_CONTROL | MOD_NOREPEAT, 'A');
}
}

Expand Down Expand Up @@ -358,23 +369,19 @@ void EditBoxImplWin::_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPa
::PostMessageW(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0);
s_previousFocusWnd = _hwndEdit;
_hasFocus = true;
s_editBoxFocused = true;
this->_changedTextManually = false;
}
break;
case WM_KILLFOCUS:
s_editBoxFocused = false;
_hasFocus = false;
// when app enter background, this message also be called.
if (this->_editingMode && !::IsWindowVisible(hwnd))
{
this->editBoxEditingDidEnd(this->getText(), _endAction);
}
break;
case WM_HOTKEY:
if (wParam == (WPARAM)_hotKeyIdCtrlA)
{
::SendMessageW(_hwndEdit, EM_SETSEL, 0, -1);
}
break;
default:
break;
}
Expand Down Expand Up @@ -433,6 +440,20 @@ LRESULT EditBoxImplWin::hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
}

break;
case WM_HOTKEY:
{
if (s_editBoxFocused &&
wParam == (WPARAM)s_hotKeyIdCtrlA)
{
EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtrW((HWND)s_previousFocusWnd, GWLP_USERDATA);
if (pThis != nullptr && pThis->_hasFocus)
{
::SendMessageW((HWND)s_previousFocusWnd, EM_SETSEL, 0, -1);
}
}

break;
}
default:
break;
}
Expand Down
4 changes: 3 additions & 1 deletion core/ui/UIEditBox/UIEditBoxImpl-win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class AX_GUI_DLL EditBoxImplWin : public EditBoxImplCommon
void _WindowProc(HWND, UINT, WPARAM, LPARAM);

WNDPROC _prevWndProc;
ATOM _hotKeyIdCtrlA;

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Expand All @@ -89,6 +88,9 @@ class AX_GUI_DLL EditBoxImplWin : public EditBoxImplCommon
static HWND s_previousFocusWnd;
static bool s_isInitialized;
static HMENU s_editboxChildID;
static ATOM s_hotKeyIdCtrlA;
static int s_editBoxCount;
static bool s_editBoxFocused;
static void lazyInit();
};

Expand Down