UIEditBoxImpl-win32.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013 Jozef Pridavok
  4. Copyright (c) 2013-2017 zilongshanren
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "ui/UIEditBox/UIEditBoxImpl-win32.h"
  24. #include "platform/CCPlatformConfig.h"
  25. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  26. #include "ui/UIEditBox/UIEditBox.h"
  27. #include <tchar.h>
  28. #include <stdio.h>
  29. #include "2d/CCLabel.h"
  30. #include "base/CCDirector.h"
  31. #include "base/ccUTF8.h"
  32. #include <Commctrl.h>
  33. #include <windows.h>
  34. #include "ui/UIHelper.h"
  35. NS_CC_BEGIN
  36. namespace ui {
  37. bool EditBoxImplWin::s_isInitialized = false;
  38. int EditBoxImplWin::s_editboxChildID = 100;
  39. HWND EditBoxImplWin::s_previousFocusWnd = 0;
  40. WNDPROC EditBoxImplWin::s_prevCocosWndProc = 0;
  41. HINSTANCE EditBoxImplWin::s_hInstance = 0;
  42. HWND EditBoxImplWin::s_hwndCocos = 0;
  43. void EditBoxImplWin::lazyInit()
  44. {
  45. s_hwndCocos = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window();
  46. LONG style = ::GetWindowLongW(s_hwndCocos, GWL_STYLE);
  47. ::SetWindowLongW(s_hwndCocos, GWL_STYLE, style | WS_CLIPCHILDREN);
  48. s_isInitialized = true;
  49. s_previousFocusWnd = s_hwndCocos;
  50. s_hInstance = ::GetModuleHandleW(nullptr);
  51. s_prevCocosWndProc = (WNDPROC)SetWindowLongPtrW(s_hwndCocos, GWL_WNDPROC, (LONG_PTR)hookGLFWWindowProc);
  52. }
  53. EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
  54. {
  55. return new (std::nothrow) EditBoxImplWin(pEditBox);
  56. }
  57. EditBoxImplWin::EditBoxImplWin(EditBox* pEditText)
  58. : EditBoxImplCommon(pEditText),
  59. _changedTextManually(false),
  60. _hasFocus(false),
  61. _endAction(EditBoxDelegate::EditBoxEndAction::UNKNOWN)
  62. {
  63. if (!s_isInitialized)
  64. {
  65. lazyInit();
  66. }
  67. s_editboxChildID++;
  68. }
  69. EditBoxImplWin::~EditBoxImplWin()
  70. {
  71. this->cleanupEditCtrl();
  72. }
  73. bool EditBoxImplWin::isEditing()
  74. {
  75. return false;
  76. }
  77. void EditBoxImplWin::cleanupEditCtrl()
  78. {
  79. if (_hwndEdit)
  80. {
  81. SetWindowLongPtrW(_hwndEdit, GWL_WNDPROC, (LONG_PTR)_prevWndProc);
  82. ::DestroyWindow(_hwndEdit);
  83. _hasFocus = false;
  84. _changedTextManually = false;
  85. _editingMode = false;
  86. _hwndEdit = NULL;
  87. }
  88. }
  89. void EditBoxImplWin::createEditCtrl(bool singleLine)
  90. {
  91. this->cleanupEditCtrl();
  92. if (!_hwndEdit)
  93. {
  94. _hwndEdit = ::CreateWindowExW(
  95. WS_EX_CLIENTEDGE, L"EDIT", // predefined class
  96. NULL, // no window title
  97. WS_CHILD | ES_LEFT | WS_BORDER | WS_EX_TRANSPARENT | WS_TABSTOP | ES_AUTOHSCROLL | (singleLine ? 0 : ES_MULTILINE),
  98. 0,
  99. 0,
  100. 0,
  101. 0, // set size in WM_SIZE message
  102. s_hwndCocos, // parent window
  103. (HMENU)s_editboxChildID, // edit control ID
  104. s_hInstance,
  105. this); // pointer not needed
  106. SetWindowLongPtrW(_hwndEdit, GWL_USERDATA, (LONG_PTR)this);
  107. _prevWndProc = (WNDPROC)SetWindowLongPtrW(_hwndEdit, GWL_WNDPROC, (LONG_PTR)WindowProc);
  108. ::SendMessageW(_hwndEdit, EM_LIMITTEXT, this->_maxLength, 0);
  109. s_previousFocusWnd = s_hwndCocos;
  110. this->setNativeFont(this->getNativeDefaultFontName(), this->_fontSize);
  111. this->setNativeText(this->_text.c_str());
  112. }
  113. }
  114. void EditBoxImplWin::createNativeControl(const Rect& frame)
  115. {
  116. this->createEditCtrl(false);
  117. }
  118. void EditBoxImplWin::setNativeFont(const char* pFontName, int fontSize)
  119. {
  120. auto glView = Director::getInstance()->getOpenGLView();
  121. HFONT hFont = ::CreateFontW(static_cast<int>(fontSize * glView->getScaleX()), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_OUTLINE_PRECIS,
  122. CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, L"Arial");
  123. ::SendMessageW(_hwndEdit, // Handle of edit control
  124. WM_SETFONT, // Message to change the font
  125. (WPARAM)hFont, // handle of the font
  126. MAKELPARAM(TRUE, 0) // Redraw text
  127. );
  128. }
  129. void EditBoxImplWin::setNativeFontColor(const Color4B& color)
  130. {
  131. //not implemented yet
  132. }
  133. void EditBoxImplWin::setNativePlaceholderFont(const char* pFontName, int fontSize)
  134. {
  135. //not implemented yet
  136. }
  137. void EditBoxImplWin::setNativePlaceholderFontColor(const Color4B& color)
  138. {
  139. //not implemented yet
  140. }
  141. void EditBoxImplWin::setNativeInputMode(EditBox::InputMode inputMode)
  142. {
  143. if (_editBoxInputMode == cocos2d::ui::EditBox::InputMode::ANY)
  144. {
  145. this->createEditCtrl(false);
  146. }
  147. else if (_editBoxInputMode == cocos2d::ui::EditBox::InputMode::NUMERIC
  148. || _editBoxInputMode == cocos2d::ui::EditBox::InputMode::DECIMAL
  149. || _editBoxInputMode == cocos2d::ui::EditBox::InputMode::PHONE_NUMBER)
  150. {
  151. this->createEditCtrl(true);
  152. ::SetWindowLongW(_hwndEdit, GWL_STYLE, ::GetWindowLongW(_hwndEdit, GWL_STYLE) | ES_NUMBER);
  153. }
  154. else
  155. {
  156. this->createEditCtrl(true);
  157. }
  158. if (this->_editBoxInputFlag != cocos2d::ui::EditBox::InputFlag::PASSWORD)
  159. {
  160. ::PostMessageW(_hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0);
  161. }
  162. }
  163. void EditBoxImplWin::setNativeInputFlag(EditBox::InputFlag inputFlag)
  164. {
  165. if (inputFlag == EditBox::InputFlag::PASSWORD)
  166. {
  167. this->createEditCtrl(true);
  168. }
  169. else
  170. {
  171. if (_editBoxInputMode != cocos2d::ui::EditBox::InputMode::ANY)
  172. {
  173. this->createEditCtrl(true);
  174. if (inputFlag == EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
  175. {
  176. ::SetWindowLongW(_hwndEdit, GWL_STYLE, ::GetWindowLongW(_hwndEdit, GWL_STYLE) | ES_UPPERCASE);
  177. }
  178. // Clear the password style
  179. ::PostMessageW(_hwndEdit, EM_SETPASSWORDCHAR, (WPARAM)0, (LPARAM)0);
  180. }
  181. }
  182. }
  183. void EditBoxImplWin::setNativeReturnType(EditBox::KeyboardReturnType returnType)
  184. {
  185. //not implemented yet
  186. }
  187. void EditBoxImplWin::setNativeTextHorizontalAlignment(TextHAlignment alignment)
  188. {
  189. LONG style = ::GetWindowLongW(_hwndEdit, GWL_STYLE);
  190. switch (alignment)
  191. {
  192. case TextHAlignment::LEFT:
  193. style = (style & ~ES_CENTER & ~ES_RIGHT) | ES_LEFT;
  194. break;
  195. case TextHAlignment::CENTER:
  196. style = (style & ~ES_LEFT & ~ES_RIGHT) | ES_CENTER;
  197. break;
  198. case TextHAlignment::RIGHT:
  199. style = (style & ~ES_LEFT & ~ES_CENTER) | ES_RIGHT;
  200. break;
  201. }
  202. ::SetWindowLongW(_hwndEdit, GWL_STYLE, style);
  203. }
  204. void EditBoxImplWin::setNativeText(const char* pText)
  205. {
  206. std::u16string utf16Result;
  207. std::string text(pText);
  208. cocos2d::StringUtils::UTF8ToUTF16(text, utf16Result);
  209. this->_changedTextManually = true;
  210. ::SetWindowTextW(_hwndEdit, (LPCWSTR)utf16Result.c_str());
  211. int textLen = text.size();
  212. ::SendMessageW(_hwndEdit, EM_SETSEL, textLen, textLen);
  213. if (_editBoxInputMode == cocos2d::ui::EditBox::InputMode::ANY)
  214. {
  215. ::SendMessageW(_hwndEdit, EM_SCROLLCARET, 0, 0);
  216. }
  217. }
  218. void EditBoxImplWin::setNativePlaceHolder(const char* pText)
  219. {
  220. //not implemented yet
  221. }
  222. void EditBoxImplWin::setNativeVisible(bool visible)
  223. {
  224. if (visible)
  225. {
  226. ::ShowWindow(_hwndEdit, SW_SHOW);
  227. }
  228. else
  229. {
  230. ::ShowWindow(_hwndEdit, SW_HIDE);
  231. }
  232. }
  233. void EditBoxImplWin::updateNativeFrame(const Rect& rect)
  234. {
  235. ::SetWindowPos(
  236. _hwndEdit,
  237. HWND_NOTOPMOST,
  238. rect.origin.x,
  239. rect.origin.y,
  240. rect.size.width,
  241. rect.size.height,
  242. SWP_NOZORDER);
  243. }
  244. const char* EditBoxImplWin::getNativeDefaultFontName()
  245. {
  246. return "Arial";
  247. }
  248. void EditBoxImplWin::nativeOpenKeyboard()
  249. {
  250. ::PostMessageW(_hwndEdit, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0);
  251. // s_previousFocusWnd = hwndEdit;
  252. this->editBoxEditingDidBegin();
  253. auto rect = ui::Helper::convertBoundingBoxToScreen(_editBox);
  254. this->updateNativeFrame(rect);
  255. }
  256. void EditBoxImplWin::nativeCloseKeyboard()
  257. {
  258. //don't need to implement
  259. }
  260. void EditBoxImplWin::setNativeMaxLength(int maxLength)
  261. {
  262. ::SendMessageW(_hwndEdit, EM_LIMITTEXT, maxLength, 0);
  263. }
  264. void EditBoxImplWin::_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  265. {
  266. switch (uMsg)
  267. {
  268. case WM_CHAR:
  269. if (wParam == VK_RETURN)
  270. {
  271. if (_editBoxInputMode != cocos2d::ui::EditBox::InputMode::ANY) {
  272. if (s_previousFocusWnd != s_hwndCocos) {
  273. switch (_keyboardReturnType)
  274. {
  275. case EditBox::KeyboardReturnType::DEFAULT:
  276. _endAction = EditBoxDelegate::EditBoxEndAction::UNKNOWN;
  277. break;
  278. case EditBox::KeyboardReturnType::DONE:
  279. _endAction = EditBoxDelegate::EditBoxEndAction::RETURN;
  280. break;
  281. case EditBox::KeyboardReturnType::SEND:
  282. _endAction = EditBoxDelegate::EditBoxEndAction::RETURN;
  283. break;
  284. case EditBox::KeyboardReturnType::SEARCH:
  285. _endAction = EditBoxDelegate::EditBoxEndAction::RETURN;
  286. break;
  287. case EditBox::KeyboardReturnType::GO:
  288. _endAction = EditBoxDelegate::EditBoxEndAction::RETURN;
  289. break;
  290. case EditBox::KeyboardReturnType::NEXT:
  291. _endAction = EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
  292. break;
  293. default:
  294. _endAction = EditBoxDelegate::EditBoxEndAction::UNKNOWN;
  295. break;
  296. }
  297. ::ShowWindow(s_previousFocusWnd, SW_HIDE);
  298. ::SendMessageW(s_hwndCocos, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0);
  299. s_previousFocusWnd = s_hwndCocos;
  300. }
  301. }
  302. }
  303. break;
  304. case WM_SETFOCUS:
  305. if (hwnd != s_previousFocusWnd)
  306. {
  307. ::PostMessageW(hwnd, WM_ACTIVATE, (WPARAM)s_previousFocusWnd, 0);
  308. ::PostMessageW(hwnd, WM_SETCURSOR, (WPARAM)s_previousFocusWnd, 0);
  309. s_previousFocusWnd = _hwndEdit;
  310. _hasFocus = true;
  311. this->_changedTextManually = false;
  312. }
  313. break;
  314. case WM_KILLFOCUS:
  315. _hasFocus = false;
  316. //when app enter background, this message also be called.
  317. if (this->_editingMode && !::IsWindowVisible(hwnd))
  318. {
  319. this->editBoxEditingDidEnd(this->getText(), _endAction);
  320. }
  321. break;
  322. default:
  323. break;
  324. }
  325. }
  326. std::string EditBoxImplWin::getText() const
  327. {
  328. std::u16string wstrResult;
  329. std::string utf8Result;
  330. int inputLength = ::GetWindowTextLengthW(_hwndEdit);
  331. wstrResult.resize(inputLength);
  332. ::GetWindowTextW(_hwndEdit, (LPWSTR)&wstrResult[0], inputLength + 1);
  333. bool conversionResult = cocos2d::StringUtils::UTF16ToUTF8(wstrResult, utf8Result);
  334. if (!conversionResult)
  335. {
  336. CCLOG("warning, editbox input text conversion error.");
  337. }
  338. return std::move(utf8Result);
  339. }
  340. LRESULT EditBoxImplWin::hookGLFWWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  341. {
  342. switch (uMsg)
  343. {
  344. case WM_COMMAND:
  345. if (HIWORD(wParam) == EN_CHANGE) {
  346. EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtrW((HWND)lParam, GWLP_USERDATA);
  347. if (pThis && !pThis->_changedTextManually)
  348. {
  349. pThis->editBoxEditingChanged(pThis->getText());
  350. pThis->_changedTextManually = false;
  351. }
  352. }
  353. break;
  354. case WM_LBUTTONDOWN:
  355. if (s_previousFocusWnd != s_hwndCocos) {
  356. ::ShowWindow(s_previousFocusWnd, SW_HIDE);
  357. EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtrW(s_previousFocusWnd, GWLP_USERDATA);
  358. if (pThis!=nullptr && !pThis->_hasFocus)
  359. {
  360. if (pThis->_editingMode && !IsWindowVisible(s_previousFocusWnd))
  361. {
  362. pThis->editBoxEditingDidEnd(pThis->getText());
  363. }
  364. }
  365. else
  366. {
  367. ::PostMessageW(s_hwndCocos, WM_SETFOCUS, (WPARAM)s_previousFocusWnd, 0);
  368. }
  369. s_previousFocusWnd = s_hwndCocos;
  370. }
  371. break;
  372. default:
  373. break;
  374. }
  375. return ::CallWindowProcW(s_prevCocosWndProc, hwnd, uMsg, wParam, lParam);
  376. }
  377. LRESULT EditBoxImplWin::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  378. {
  379. EditBoxImplWin* pThis = (EditBoxImplWin*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
  380. if (pThis)
  381. {
  382. pThis->_WindowProc(hwnd, uMsg, wParam, lParam);
  383. }
  384. return ::CallWindowProcW(pThis->_prevWndProc, hwnd, uMsg, wParam, lParam);
  385. }
  386. }
  387. NS_CC_END
  388. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */