UIEditBoxImpl-win32.cpp 14 KB

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