UIEditBoxImpl-win32.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013 Jozef Pridavok
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIEditBox/UIEditBoxImpl-win32.h"
  22. #include "platform/CCPlatformConfig.h"
  23. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  24. #include "ui/UIEditBox/UIEditBox.h"
  25. #include <windows.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. #pragma warning (disable: 4312)
  32. // Structure used to orient the inputbox behavior
  33. struct WIN32INPUTBOX_PARAM
  34. {
  35. friend class CWin32InputBox;
  36. //
  37. IN OPTIONAL bool bMultiline;
  38. // Pass this as none zero so to use this memory dlg template
  39. IN OPTIONAL LPVOID DlgTemplateData;
  40. // Pass this as none ZERO so to load DLGTEMPLATE from resources
  41. IN OPTIONAL LPCSTR DlgTemplateName;
  42. // passing both "DlgTemplateName" and "DlgTemplateData" ZERO will cause
  43. // the dialog to use his default embedded resource
  44. // Center on monitor or owner window?
  45. IN OPTIONAL bool bCenter;
  46. // Want to add more styles to the dialog?
  47. IN OPTIONAL DWORD dwStylesPlus, dwStylesMinus;
  48. IN OPTIONAL DWORD dwExStylesPlus, dwExStylesMinus;
  49. IN LPCSTR szTitle, szPrompt;
  50. // Return buffer
  51. std::string* pstrResult;
  52. IN DWORD nMaxLength;
  53. // Owner window
  54. HWND hwndOwner;
  55. HINSTANCE hInstance;
  56. short xPos, yPos;
  57. WIN32INPUTBOX_PARAM();
  58. private:
  59. HWND hDlg;
  60. };
  61. class CWin32InputBox
  62. {
  63. private:
  64. WIN32INPUTBOX_PARAM *_param;
  65. static LRESULT CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
  66. HWND _hwndEditCtrl;
  67. int _recursionDepth;
  68. HHOOK _getMsgHook;
  69. cocos2d::ui::EditBox::KeyboardReturnType _returnType;
  70. cocos2d::ui::EditBox::InputMode _inputMode;
  71. cocos2d::ui::EditBox::InputFlag _inputFlag;
  72. std::function<void (const char *)> _onTextChange;
  73. std::function<void (INT_PTR)> _onClose;
  74. void InitDialog();
  75. static LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam);
  76. public:
  77. CWin32InputBox(WIN32INPUTBOX_PARAM *);
  78. ~CWin32InputBox();
  79. static INT_PTR InputBoxEx(WIN32INPUTBOX_PARAM *,
  80. cocos2d::ui::EditBox::KeyboardReturnType eReturnType,
  81. cocos2d::ui::EditBox::InputMode eInputMode,
  82. cocos2d::ui::EditBox::InputFlag eInputFlag,
  83. const std::function<void (const char *)> &onTextChange,
  84. const std::function<void (INT_PTR)> &onClose);
  85. static INT_PTR InputBox(
  86. LPCSTR szTitle,
  87. LPCSTR szPrompt,
  88. std::string* pstrResult,
  89. DWORD nMaxLength,
  90. bool bMultiLine,
  91. HWND hwndParent,
  92. cocos2d::ui::EditBox::KeyboardReturnType eReturnType,
  93. cocos2d::ui::EditBox::InputMode eInputMode,
  94. cocos2d::ui::EditBox::InputFlag eInputFlag,
  95. const std::function<void (const char *)> &onTextChange,
  96. const std::function<void (INT_PTR)> &onClose);
  97. void SetText(const char *pText);
  98. void Close(INT_PTR buttonId);
  99. };
  100. typedef struct _MSDN_DLGTEMPLATEEX
  101. {
  102. WORD dlgVer;
  103. WORD signature;
  104. DWORD helpID;
  105. DWORD exStyle;
  106. DWORD style;
  107. WORD cDlgItems;
  108. short x;
  109. short y;
  110. short cx;
  111. short cy;
  112. BYTE _rest[1]; // rest of structure
  113. } MSDN_DLGTEMPLATEEX;
  114. static bool IsDlgTemplateExtended(DLGTEMPLATE *dlgTemplate)
  115. {
  116. MSDN_DLGTEMPLATEEX *dgExTemplate = (MSDN_DLGTEMPLATEEX *)dlgTemplate;
  117. // MSDN excerpt:
  118. //* dlgVer
  119. // Specifies the version number of the extended dialog box template. This member must be 1.
  120. //* signature
  121. // Indicates whether a template is an extended dialog box template.
  122. // If signature is 0xFFFF, this is an extended dialog box template.
  123. // In this case, the dlgVer member specifies the template version number.
  124. // If signature is any value other than 0xFFFF, this is a standard dialog box template that uses the DLGTEMPLATE and DLGITEMTEMPLATE structures.
  125. return (dgExTemplate->dlgVer == 1) && (dgExTemplate->signature == 0xFFFF);
  126. }
  127. // Use alignment if supported by the compiler
  128. #ifdef _MSC_VER
  129. #if _MSC_VER > 1200
  130. __declspec(align(4))
  131. #endif
  132. #endif
  133. // per the MSDN, the DLGTEMPLATE must be DWORD aligned
  134. // this was generated by the DlgResToDlgTemplate tool
  135. static unsigned char definputbox_dlg[] =
  136. {
  137. 0x01, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0xc8, 0x00, 0x06,
  138. 0x00, 0x16, 0x00, 0x11, 0x00, 0xe7, 0x00, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x00, 0x69,
  139. 0x00, 0x6e, 0x00, 0x33, 0x00, 0x32, 0x00, 0x49, 0x00, 0x6e, 0x00, 0x70, 0x00, 0x75, 0x00, 0x74,
  140. 0x00, 0x42, 0x00, 0x6f, 0x00, 0x78, 0x00, 0x00, 0x00, 0x08, 0x00, 0xbc, 0x02, 0x00, 0x00, 0x4d,
  141. 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00, 0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x20,
  142. 0x00, 0x44, 0x00, 0x6c, 0x00, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x50, 0x06, 0x00, 0x04, 0x00, 0x9d, 0x00, 0x21, 0x00, 0xe8,
  144. 0x03, 0x00, 0x00, 0xff, 0xff, 0x82, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x70,
  145. 0x00, 0x74, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0x00, 0x80, 0x00, 0x81, 0x50, 0x06, 0x00, 0x25, 0x00, 0xd8, 0x00, 0x0e, 0x00, 0xe9,
  147. 0x03, 0x00, 0x00, 0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. 0x00, 0x00, 0x00, 0x84, 0x10, 0xa1, 0x50, 0x06, 0x00, 0x37, 0x00, 0xd8, 0x00, 0x31, 0x00, 0xea,
  149. 0x03, 0x00, 0x00, 0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150. 0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x50, 0xab, 0x00, 0x04, 0x00, 0x33, 0x00, 0x0e, 0x00, 0x01,
  151. 0x00, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00, 0x4f, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x50, 0xab, 0x00, 0x15, 0x00, 0x33,
  153. 0x00, 0x0e, 0x00, 0x02, 0x00, 0x00, 0x00, 0xff, 0xff, 0x80, 0x00, 0x43, 0x00, 0x41, 0x00, 0x4e,
  154. 0x00, 0x43, 0x00, 0x45, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x27, 0x00, 0x08, 0x00, 0x08, 0x00, 0xff,
  156. 0xff, 0xff, 0xff, 0xff, 0xff, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00
  157. };
  158. static const INT
  159. definputbox_id_prompt = 1000,
  160. definputbox_id_edit1 = 1001,
  161. definputbox_id_edit2 = 1002;
  162. static CWin32InputBox *s_win32InputBox = nullptr;
  163. WIN32INPUTBOX_PARAM::WIN32INPUTBOX_PARAM()
  164. {
  165. bMultiline = false;
  166. hwndOwner = 0;
  167. DlgTemplateName = 0;
  168. hInstance = (HINSTANCE) ::GetModuleHandle(0);
  169. DlgTemplateData = definputbox_dlg;
  170. bCenter = true;
  171. dwStylesPlus = 0;
  172. dwExStylesPlus = 0;
  173. dwStylesMinus = 0xFFFFFFFF;
  174. dwExStylesMinus = 0xFFFFFFFF;
  175. xPos = yPos = -1;
  176. pstrResult = nullptr;
  177. nMaxLength = (DWORD) -1;
  178. }
  179. CWin32InputBox::CWin32InputBox(WIN32INPUTBOX_PARAM *param)
  180. {
  181. _param = new (std::nothrow) WIN32INPUTBOX_PARAM(*param);
  182. _hwndEditCtrl = NULL;
  183. _recursionDepth = 0;
  184. _getMsgHook = NULL;
  185. }
  186. CWin32InputBox::~CWin32InputBox()
  187. {
  188. CC_SAFE_DELETE(_param);
  189. }
  190. INT_PTR CWin32InputBox::InputBoxEx(WIN32INPUTBOX_PARAM *param,
  191. cocos2d::ui::EditBox::KeyboardReturnType eReturnType,
  192. cocos2d::ui::EditBox::InputMode eInputMode,
  193. cocos2d::ui::EditBox::InputFlag eInputFlag,
  194. const std::function<void (const char *)> &onTextChange,
  195. const std::function<void (INT_PTR)> &onClose)
  196. {
  197. // Check mandatory parameters
  198. if (param->pstrResult == nullptr)
  199. {
  200. ::SetLastError(ERROR_INVALID_PARAMETER);
  201. return 0;
  202. }
  203. LPDLGTEMPLATE dlgTemplate;
  204. if (param->DlgTemplateName != 0)
  205. {
  206. HMODULE hModule = (HMODULE)param->hInstance;
  207. #ifdef __MINGW32__
  208. HRSRC rcDlg = ::FindResource(hModule, (LPWSTR)(ULONG_PTR)(size_t)(param->DlgTemplateName), RT_DIALOG);
  209. #else
  210. HRSRC rcDlg = ::FindResource(hModule, MAKEINTRESOURCE(param->DlgTemplateName), RT_DIALOG);
  211. #endif
  212. if (rcDlg == nullptr)
  213. return 0;
  214. HGLOBAL hglobalDlg = ::LoadResource(hModule, rcDlg);
  215. if (hglobalDlg == nullptr)
  216. return 0;
  217. dlgTemplate = (LPDLGTEMPLATE)hglobalDlg;
  218. }
  219. else if (param->DlgTemplateData != 0)
  220. {
  221. dlgTemplate = (LPDLGTEMPLATE)param->DlgTemplateData;
  222. }
  223. MSDN_DLGTEMPLATEEX *dlgTemplateEx =
  224. IsDlgTemplateExtended((LPDLGTEMPLATE)dlgTemplate) ? (MSDN_DLGTEMPLATEEX *)dlgTemplate : 0;
  225. if (dlgTemplateEx != 0)
  226. {
  227. dlgTemplateEx->exStyle |= param->dwExStylesPlus;
  228. dlgTemplateEx->style |= param->dwStylesPlus;
  229. dlgTemplateEx->exStyle &= param->dwExStylesMinus;
  230. dlgTemplateEx->style &= param->dwStylesMinus;
  231. if (param->bCenter)
  232. dlgTemplateEx->style |= DS_CENTER;
  233. if (param->xPos != -1)
  234. dlgTemplateEx->x = param->xPos;
  235. if (param->yPos != -1)
  236. dlgTemplateEx->y = param->yPos;
  237. }
  238. else
  239. {
  240. dlgTemplate->dwExtendedStyle |= param->dwExStylesPlus;
  241. dlgTemplate->style |= param->dwStylesPlus;
  242. dlgTemplate->dwExtendedStyle &= param->dwExStylesMinus;
  243. dlgTemplate->style &= param->dwStylesMinus;
  244. if (param->bCenter)
  245. dlgTemplate->style |= DS_CENTER;
  246. if (param->xPos != -1)
  247. dlgTemplate->x = param->xPos;
  248. if (param->yPos != -1)
  249. dlgTemplate->y = param->yPos;
  250. }
  251. CCASSERT(s_win32InputBox == nullptr, "Only one instance of Win32InputBox allowed");
  252. s_win32InputBox = new (std::nothrow) CWin32InputBox(param);
  253. s_win32InputBox->_returnType = eReturnType;
  254. s_win32InputBox->_inputMode = eInputMode;
  255. s_win32InputBox->_inputFlag = eInputFlag;
  256. s_win32InputBox->_onTextChange = onTextChange;
  257. s_win32InputBox->_onClose = onClose;
  258. HWND hDlg = ::CreateDialogIndirectParam(param->hInstance, dlgTemplate, param->hwndOwner, (DLGPROC)DlgProc, (LPARAM)s_win32InputBox);
  259. ::ShowWindow(hDlg, SW_SHOW);
  260. ::EnableWindow(param->hwndOwner, FALSE);
  261. return TRUE;
  262. }
  263. INT_PTR CWin32InputBox::InputBox(
  264. LPCSTR szTitle,
  265. LPCSTR szPrompt,
  266. std::string* pstrResult,
  267. DWORD nResultSize,
  268. bool bMultiLine,
  269. HWND hwndParent,
  270. cocos2d::ui::EditBox::KeyboardReturnType eReturnType,
  271. cocos2d::ui::EditBox::InputMode eInputMode,
  272. cocos2d::ui::EditBox::InputFlag eInputFlag,
  273. const std::function<void (const char *)> &onTextChange,
  274. const std::function<void (INT_PTR)> &onClose)
  275. {
  276. WIN32INPUTBOX_PARAM param;
  277. param.szTitle = szTitle;
  278. param.szPrompt = szPrompt;
  279. param.pstrResult = pstrResult;
  280. param.nMaxLength = nResultSize;
  281. param.bMultiline = bMultiLine;
  282. param.hwndOwner = hwndParent;
  283. return InputBoxEx(&param, eReturnType, eInputMode, eInputFlag, onTextChange, onClose);
  284. }
  285. void CWin32InputBox::InitDialog()
  286. {
  287. // Set the button captions, associated with ReturnType
  288. switch (_returnType)
  289. {
  290. case cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT:
  291. ::SetDlgItemTextW(_param->hDlg, IDOK, L"OK");
  292. break;
  293. case cocos2d::ui::EditBox::KeyboardReturnType::DONE:
  294. ::SetDlgItemTextW(_param->hDlg, IDOK, L"DONE");
  295. break;
  296. case cocos2d::ui::EditBox::KeyboardReturnType::SEND:
  297. ::SetDlgItemTextW(_param->hDlg, IDOK, L"SEND");
  298. break;
  299. case cocos2d::ui::EditBox::KeyboardReturnType::SEARCH:
  300. ::SetDlgItemTextW(_param->hDlg, IDOK, L"SEARCH");
  301. break;
  302. case cocos2d::ui::EditBox::KeyboardReturnType::GO:
  303. ::SetDlgItemTextW(_param->hDlg, IDOK, L"GO");
  304. break;
  305. }
  306. ::SetDlgItemTextW(_param->hDlg, IDCANCEL, L"CANCEL");
  307. // Set other controls
  308. std::u16string utf16Title;
  309. cocos2d::StringUtils::UTF8ToUTF16(_param->szTitle, utf16Title);
  310. ::SetWindowTextW(_param->hDlg, (LPCWSTR) utf16Title.c_str());
  311. std::u16string utf16Prompt;
  312. cocos2d::StringUtils::UTF8ToUTF16(_param->szPrompt, utf16Prompt);
  313. ::SetDlgItemTextW(_param->hDlg, definputbox_id_prompt, (LPCWSTR) utf16Prompt.c_str());
  314. HWND hwndEdit1 = ::GetDlgItem(_param->hDlg, definputbox_id_edit1);
  315. HWND hwndEdit2 = ::GetDlgItem(_param->hDlg, definputbox_id_edit2);
  316. if (_param->bMultiline)
  317. _hwndEditCtrl = hwndEdit2;
  318. else
  319. _hwndEditCtrl = hwndEdit1;
  320. std::u16string utf16Result;
  321. cocos2d::StringUtils::UTF8ToUTF16(*_param->pstrResult, utf16Result);
  322. ::SetWindowTextW(_hwndEditCtrl, (LPCWSTR) utf16Result.c_str());
  323. RECT rectDlg, rectEdit1, rectEdit2;
  324. ::GetWindowRect(_param->hDlg, &rectDlg);
  325. ::GetWindowRect(hwndEdit1, &rectEdit1);
  326. ::GetWindowRect(hwndEdit2, &rectEdit2);
  327. if (_param->bMultiline)
  328. {
  329. ::ShowWindow(hwndEdit1, SW_HIDE);
  330. ::SetWindowPos(
  331. hwndEdit2,
  332. HWND_NOTOPMOST,
  333. rectEdit1.left - rectDlg.left,
  334. (rectEdit1.top - rectDlg.top) - (rectEdit1.bottom - rectEdit1.top),
  335. 0,
  336. 0,
  337. SWP_NOSIZE | SWP_NOZORDER);
  338. ::SetWindowPos(
  339. _param->hDlg,
  340. HWND_NOTOPMOST,
  341. 0,
  342. 0,
  343. rectDlg.right - rectDlg.left,
  344. rectDlg.bottom - rectDlg.top - (rectEdit1.bottom - rectEdit1.top),
  345. SWP_NOMOVE);
  346. }
  347. else
  348. {
  349. ::SetWindowPos(
  350. _param->hDlg,
  351. HWND_NOTOPMOST,
  352. 0,
  353. 0,
  354. rectDlg.right - rectDlg.left,
  355. rectEdit1.bottom - rectDlg.top + 5,
  356. SWP_NOMOVE);
  357. ::ShowWindow(hwndEdit2, SW_HIDE);
  358. }
  359. // Sets the text limit of the edit control.
  360. // wParam is the maximum number of WCHARs the user can enter. For Unicode text, wParam is the number of characters.
  361. // Please refer to https://msdn.microsoft.com/en-us/library/bb761607%28v=vs.85%29.aspx
  362. if ((int)_param->nMaxLength > 0)
  363. {
  364. ::SendMessageW(hwndEdit1, EM_LIMITTEXT, (WPARAM)_param->nMaxLength, 0);
  365. ::SendMessageW(hwndEdit2, EM_LIMITTEXT, (WPARAM)_param->nMaxLength, 0);
  366. }
  367. switch (_inputFlag)
  368. {
  369. case cocos2d::ui::EditBox::InputFlag::PASSWORD:
  370. // Sets a password character displayed in a edit control when the user types text.
  371. // Please refer to https://msdn.microsoft.com/en-us/library/bb761653%28v=vs.85%29.aspx
  372. ::SendMessageW(hwndEdit1, EM_SETPASSWORDCHAR, (WPARAM)L'*', 0);
  373. ::SendMessageW(hwndEdit2, EM_SETPASSWORDCHAR, (WPARAM)L'*', 0);
  374. break;
  375. case cocos2d::ui::EditBox::InputFlag::SENSITIVE:
  376. break;
  377. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD:
  378. break;
  379. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
  380. break;
  381. case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
  382. // Converts all characters to lowercase as they are typed into the edit control.
  383. // Please refer to https://msdn.microsoft.com/en-us/library/bb775464%28v=vs.85%29.aspx
  384. ::SetWindowLongW(hwndEdit1, GWL_STYLE, ::GetWindowLongW(hwndEdit1, GWL_STYLE) | ES_UPPERCASE);
  385. ::SetWindowLongW(hwndEdit2, GWL_STYLE, ::GetWindowLongW(hwndEdit2, GWL_STYLE) | ES_UPPERCASE);
  386. break;
  387. default:
  388. break;
  389. }
  390. switch (_inputMode)
  391. {
  392. case cocos2d::ui::EditBox::InputMode::ANY:
  393. break;
  394. case cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS:
  395. break;
  396. case cocos2d::ui::EditBox::InputMode::NUMERIC:
  397. // Allows only digits to be entered into the edit control.
  398. // Note that, even with this set, it is still possible to paste non-digits into the edit control.
  399. // Please refer to https://msdn.microsoft.com/en-us/library/bb775464%28v=vs.85%29.aspx
  400. ::SetWindowLongW(hwndEdit1, GWL_STYLE, ::GetWindowLongW(hwndEdit1, GWL_STYLE) | ES_NUMBER);
  401. ::SetWindowLongW(hwndEdit2, GWL_STYLE, ::GetWindowLongW(hwndEdit2, GWL_STYLE) | ES_NUMBER);
  402. break;
  403. case cocos2d::ui::EditBox::InputMode::PHONE_NUMBER:
  404. break;
  405. case cocos2d::ui::EditBox::InputMode::URL:
  406. break;
  407. case cocos2d::ui::EditBox::InputMode::DECIMAL:
  408. break;
  409. case cocos2d::ui::EditBox::InputMode::SINGLE_LINE:
  410. break;
  411. default:
  412. break;
  413. }
  414. CCASSERT(_getMsgHook == nullptr, "Windows Message hook already set");
  415. // To make the enter key work, here need a Windows Message hook.
  416. // Please refer to https://support.microsoft.com/en-us/kb/187988
  417. _getMsgHook = ::SetWindowsHookEx(WH_GETMESSAGE, &CWin32InputBox::GetMsgProc, ::GetModuleHandle(nullptr), ::GetCurrentThreadId());
  418. }
  419. LRESULT CALLBACK CWin32InputBox::GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
  420. {
  421. if (code == HC_ACTION)
  422. {
  423. LPMSG lpMsg = (LPMSG)lParam;
  424. if (lpMsg->message == WM_KEYDOWN)
  425. {
  426. if (lpMsg->wParam == VK_RETURN) // User has hit the ENTER key.
  427. {
  428. // Translate the message manually.
  429. lpMsg->hwnd = s_win32InputBox->_param->hDlg;
  430. lpMsg->message = WM_COMMAND;
  431. lpMsg->wParam = IDOK;
  432. }
  433. }
  434. }
  435. return ::CallNextHookEx(s_win32InputBox->_getMsgHook, code, wParam, lParam);
  436. }
  437. // Message handler for about box.
  438. LRESULT CALLBACK CWin32InputBox::DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  439. {
  440. CWin32InputBox *_this = (CWin32InputBox *) ::GetWindowLongPtr(hDlg, GWLP_USERDATA);
  441. switch (message)
  442. {
  443. case WM_INITDIALOG:
  444. {
  445. SetWindowLongPtr(hDlg, GWLP_USERDATA, (LONG_PTR)lParam);
  446. _this = (CWin32InputBox *)lParam;
  447. _this->_param->hDlg = hDlg;
  448. _this->InitDialog();
  449. return TRUE;
  450. }
  451. case WM_COMMAND:
  452. {
  453. #ifdef _MY_DEBUG
  454. CHAR buf[1024];
  455. static int i = 0;
  456. sprintf(buf, "WM_COMMAND: %09d wParam=%08X lParam=%08X\n", i++, wParam, lParam);
  457. OutputDebugStringA(buf);
  458. #endif
  459. INT_PTR buttonId = LOWORD(wParam);
  460. if (buttonId == IDOK || buttonId == IDCANCEL)
  461. {
  462. // Yes! Just close the dialog box simply,
  463. // because the text of EditBox has been already synced at the moment of text altered in the edit control
  464. _this->Close(buttonId);
  465. return TRUE;
  466. }
  467. // text altered in the edit control
  468. // Please refer to https://msdn.microsoft.com/en-us/library/bb761676%28v=vs.85%29.aspx
  469. if (HIWORD(wParam) == EN_CHANGE && _this->_recursionDepth == 0)
  470. {
  471. std::u16string wstrResult;
  472. std::string utf8Result;
  473. int inputLength = ::GetWindowTextLengthW(_this->_hwndEditCtrl);
  474. wstrResult.resize(inputLength);
  475. ::GetWindowTextW(_this->_hwndEditCtrl, (LPWSTR) const_cast<char16_t*>(wstrResult.c_str()), inputLength+1);
  476. bool conversionResult = cocos2d::StringUtils::UTF16ToUTF8(wstrResult, utf8Result);
  477. _this->_param->pstrResult->clear();
  478. ++_this->_recursionDepth; // Prevent recursive calls
  479. if (conversionResult)
  480. {
  481. *(_this->_param->pstrResult) = std::move(utf8Result);
  482. }
  483. // Invoke editBoxTextChanged indirectly
  484. _this->_onTextChange(_this->_param->pstrResult->c_str());
  485. --_this->_recursionDepth;
  486. return TRUE;
  487. }
  488. }
  489. break;
  490. }
  491. return FALSE;
  492. }
  493. void CWin32InputBox::SetText(const char* pText)
  494. {
  495. if (_hwndEditCtrl != NULL)
  496. {
  497. std::u16string utf16Text;
  498. cocos2d::StringUtils::UTF8ToUTF16(pText, utf16Text);
  499. // Keep the cursor position
  500. // Please refer to :
  501. // https://msdn.microsoft.com/en-us/library/bb761598%28v=vs.85%29.aspx
  502. // https://msdn.microsoft.com/en-us/library/bb761661%28v=vs.85%29.aspx
  503. size_t len = utf16Text.length();
  504. DWORD beginPos, endPos;
  505. ::SendMessageW(_hwndEditCtrl, EM_GETSEL, (WPARAM)&beginPos, (LPARAM)&endPos);
  506. ::SendMessageW(_hwndEditCtrl, WM_SETTEXT, 0, (LPARAM)utf16Text.c_str());
  507. ::SendMessageW(_hwndEditCtrl, EM_SETSEL, (WPARAM)std::min<long>(beginPos, len), (LPARAM)std::min<long>(endPos, len));
  508. }
  509. }
  510. void CWin32InputBox::Close(INT_PTR buttonId)
  511. {
  512. if (_getMsgHook != NULL)
  513. {
  514. ::UnhookWindowsHookEx(_getMsgHook);
  515. _getMsgHook = NULL;
  516. }
  517. if (_param->hDlg != NULL)
  518. {
  519. ::EnableWindow(_param->hwndOwner, TRUE);
  520. ::SetFocus(_param->hwndOwner);
  521. ::DestroyWindow(_param->hDlg);
  522. _param->hDlg = NULL;
  523. _onClose(buttonId);
  524. }
  525. }
  526. NS_CC_BEGIN
  527. namespace ui {
  528. EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
  529. {
  530. return new (std::nothrow) EditBoxImplWin(pEditBox);
  531. }
  532. EditBoxImplWin::EditBoxImplWin(EditBox* pEditText)
  533. : EditBoxImpl(pEditText)
  534. , _label(nullptr)
  535. , _labelPlaceHolder(nullptr)
  536. , _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
  537. , _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
  538. , _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
  539. , _alignment(TextHAlignment::LEFT)
  540. , _fontSize(-1)
  541. , _placeholderFontSize(-1)
  542. , _colText(Color3B::WHITE)
  543. , _colPlaceHolder(Color3B::GRAY)
  544. , _maxLength(-1)
  545. , _isEditing(false)
  546. {
  547. }
  548. EditBoxImplWin::~EditBoxImplWin()
  549. {
  550. }
  551. void EditBoxImplWin::doAnimationWhenKeyboardMove(float duration, float distance)
  552. {
  553. }
  554. static const int CC_EDIT_BOX_PADDING = 5;
  555. bool EditBoxImplWin::initWithSize(const Size& size)
  556. {
  557. //! int fontSize = getFontSizeAccordingHeightJni(size.height-12);
  558. _label = Label::create();
  559. _label->setSystemFontSize(size.height-12);
  560. // align the text vertically center
  561. _label->setAnchorPoint(Vec2(0, 0.5f));
  562. _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height / 2.0f));
  563. _label->setTextColor(_colText);
  564. _editBox->addChild(_label);
  565. _labelPlaceHolder = Label::create();
  566. _labelPlaceHolder->setSystemFontSize(size.height-12);
  567. // align the text vertically center
  568. _labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
  569. _labelPlaceHolder->setPosition(CC_EDIT_BOX_PADDING, size.height / 2.0f);
  570. _labelPlaceHolder->setVisible(false);
  571. _labelPlaceHolder->setTextColor(_colPlaceHolder);
  572. _editBox->addChild(_labelPlaceHolder);
  573. _editSize = size;
  574. return true;
  575. }
  576. void EditBoxImplWin::setFont(const char* pFontName, int fontSize)
  577. {
  578. _fontName = pFontName;
  579. _fontSize = fontSize;
  580. if (_label != nullptr)
  581. {
  582. if (pFontName[0] != '\0') // To determine whether a string is empty quickly
  583. {
  584. _label->setSystemFontName(pFontName);
  585. }
  586. if (fontSize > 0)
  587. {
  588. _label->setSystemFontSize(fontSize);
  589. }
  590. }
  591. if (_labelPlaceHolder != nullptr)
  592. {
  593. if (pFontName[0] != '\0') // To determine whether a string is empty quickly
  594. {
  595. _labelPlaceHolder->setSystemFontName(pFontName);
  596. }
  597. if (fontSize > 0)
  598. {
  599. _labelPlaceHolder->setSystemFontSize(fontSize);
  600. }
  601. }
  602. }
  603. void EditBoxImplWin::setFontColor(const Color4B& color)
  604. {
  605. _colText = color;
  606. _label->setTextColor(color);
  607. }
  608. void EditBoxImplWin::setPlaceholderFont(const char* pFontName, int fontSize)
  609. {
  610. _placeholderFontName = pFontName;
  611. _placeholderFontSize = fontSize;
  612. if (_labelPlaceHolder != nullptr)
  613. {
  614. if (pFontName[0] != '\0') // To determine whether a string is empty quickly
  615. {
  616. _labelPlaceHolder->setSystemFontName(pFontName);
  617. }
  618. if (fontSize > 0)
  619. {
  620. _labelPlaceHolder->setSystemFontSize(fontSize);
  621. }
  622. }
  623. }
  624. void EditBoxImplWin::setPlaceholderFontColor(const Color4B& color)
  625. {
  626. _colPlaceHolder = color;
  627. _labelPlaceHolder->setTextColor(color);
  628. }
  629. void EditBoxImplWin::setInputMode(EditBox::InputMode inputMode)
  630. {
  631. _editBoxInputMode = inputMode;
  632. }
  633. void EditBoxImplWin::setMaxLength(int maxLength)
  634. {
  635. _maxLength = maxLength;
  636. }
  637. int EditBoxImplWin::getMaxLength()
  638. {
  639. return _maxLength;
  640. }
  641. void EditBoxImplWin::setInputFlag(EditBox::InputFlag inputFlag)
  642. {
  643. _editBoxInputFlag = inputFlag;
  644. }
  645. void EditBoxImplWin::setReturnType(EditBox::KeyboardReturnType returnType)
  646. {
  647. _keyboardReturnType = returnType;
  648. }
  649. bool EditBoxImplWin::isEditing()
  650. {
  651. return _isEditing;
  652. }
  653. void EditBoxImplWin::setText(const char* pText)
  654. {
  655. if (pText != nullptr)
  656. {
  657. if (strcmp(_text.c_str(), pText) == 0) // do nothing if the text is not changed
  658. {
  659. return;
  660. }
  661. _text = pText;
  662. if (_isEditing && s_win32InputBox != nullptr) // set text for the running Win32InputBox
  663. {
  664. s_win32InputBox->SetText(pText);
  665. }
  666. if (_text.length() > 0)
  667. {
  668. _labelPlaceHolder->setVisible(false);
  669. if (EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
  670. {
  671. long length = StringUtils::getCharacterCountInUTF8String(_text);
  672. std::string strToShow(length, '*'); // Fills strToShow with length consecutive copies of character '*'
  673. _label->setString(strToShow);
  674. }
  675. else
  676. {
  677. _label->setString(_text);
  678. }
  679. float maxWidth = _editSize.width - 2 * CC_EDIT_BOX_PADDING;
  680. auto labelSize = _label->getContentSize();
  681. if (labelSize.width > maxWidth)
  682. {
  683. _label->setDimensions(maxWidth, labelSize.height);
  684. }
  685. }
  686. else
  687. {
  688. _labelPlaceHolder->setVisible(true);
  689. _label->setString("");
  690. }
  691. }
  692. }
  693. const char* EditBoxImplWin::getText(void)
  694. {
  695. return _text.c_str();
  696. }
  697. void EditBoxImplWin::setPlaceHolder(const char* pText)
  698. {
  699. if (pText != nullptr)
  700. {
  701. _placeHolder = pText;
  702. if (_placeHolder.length() > 0 && _text.length() == 0)
  703. {
  704. _labelPlaceHolder->setVisible(true);
  705. }
  706. _labelPlaceHolder->setString(_placeHolder);
  707. }
  708. }
  709. const char* EditBoxImplWin::getPlaceHolder(void)
  710. {
  711. return _placeHolder.c_str();
  712. }
  713. void EditBoxImplWin::setPosition(const Vec2& pos)
  714. {
  715. //_label->setPosition(pos);
  716. //_labelPlaceHolder->setPosition(pos);
  717. }
  718. void EditBoxImplWin::setVisible(bool visible)
  719. { // don't need to be implemented on win32 platform.
  720. }
  721. void EditBoxImplWin::setContentSize(const Size& size)
  722. {
  723. }
  724. void EditBoxImplWin::setAnchorPoint(const Vec2& anchorPoint)
  725. { // don't need to be implemented on win32 platform.
  726. }
  727. void EditBoxImplWin::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  728. {
  729. }
  730. void EditBoxImplWin::openKeyboard()
  731. {
  732. if (s_win32InputBox != nullptr)
  733. {
  734. return;
  735. }
  736. if (_delegate != nullptr)
  737. {
  738. _delegate->editBoxEditingDidBegin(_editBox);
  739. }
  740. _isEditing = true;
  741. #if CC_ENABLE_SCRIPT_BINDING
  742. auto editBox = this->getEditBox();
  743. if (nullptr != editBox && 0 != editBox->getScriptEditBoxHandler())
  744. {
  745. CommonScriptData data(editBox->getScriptEditBoxHandler(), "began", editBox);
  746. ScriptEvent event(kCommonEvent, (void*)&data);
  747. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  748. }
  749. #endif
  750. std::string placeHolder = _labelPlaceHolder->getString();
  751. if (placeHolder.length() == 0)
  752. placeHolder = "Enter value";
  753. _editingText = getText();
  754. _originalText = _editingText;
  755. auto glView = Director::getInstance()->getOpenGLView();
  756. HWND hwnd = glView->getWin32Window();
  757. CWin32InputBox::InputBox("Input", placeHolder.c_str(), &_editingText, _maxLength, false, hwnd,
  758. _keyboardReturnType, _editBoxInputMode, _editBoxInputFlag,
  759. std::bind(&EditBoxImplWin::onWin32InputBoxTextChange, this, std::placeholders::_1),
  760. std::bind(&EditBoxImplWin::onWin32InputBoxClose, this, std::placeholders::_1));
  761. }
  762. void EditBoxImplWin::onWin32InputBoxClose(INT_PTR buttonId)
  763. {
  764. bool didChange = buttonId == IDOK;
  765. CC_SAFE_DELETE(s_win32InputBox);
  766. _isEditing = false;
  767. setText(didChange ? _editingText.c_str() : _originalText.c_str());
  768. if (_delegate != nullptr)
  769. {
  770. _delegate->editBoxTextChanged(_editBox, getText());
  771. _delegate->editBoxEditingDidEnd(_editBox);
  772. _delegate->editBoxReturn(_editBox);
  773. }
  774. #if CC_ENABLE_SCRIPT_BINDING
  775. if (nullptr != _editBox && 0 != _editBox->getScriptEditBoxHandler())
  776. {
  777. CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
  778. ScriptEvent event(kCommonEvent,(void*)&data);
  779. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  780. memset(data.eventName,0,sizeof(data.eventName));
  781. strncpy(data.eventName,"ended",sizeof(data.eventName));
  782. event.data = (void*)&data;
  783. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  784. memset(data.eventName,0,sizeof(data.eventName));
  785. strncpy(data.eventName,"return",sizeof(data.eventName));
  786. event.data = (void*)&data;
  787. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  788. }
  789. #endif // #if CC_ENABLE_SCRIPT_BINDING
  790. }
  791. void EditBoxImplWin::onWin32InputBoxTextChange(const char *pText)
  792. {
  793. _isEditing = false; // Prevent recursive calls
  794. setText(pText);
  795. _isEditing = true;
  796. if (_delegate != nullptr)
  797. {
  798. _delegate->editBoxTextChanged(_editBox, _text);
  799. }
  800. #if CC_ENABLE_SCRIPT_BINDING
  801. if (nullptr != _editBox && 0 != _editBox->getScriptEditBoxHandler())
  802. {
  803. CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
  804. ScriptEvent event(kCommonEvent, (void*)&data);
  805. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  806. }
  807. #endif // #if CC_ENABLE_SCRIPT_BINDING
  808. }
  809. void EditBoxImplWin::closeKeyboard()
  810. {
  811. // close the running Win32InputBox
  812. if (s_win32InputBox != nullptr)
  813. {
  814. s_win32InputBox->Close(IDCANCEL);
  815. CC_SAFE_DELETE(s_win32InputBox);
  816. _isEditing = false;
  817. }
  818. }
  819. void EditBoxImplWin::onEnter(void)
  820. {
  821. }
  822. }
  823. NS_CC_END
  824. #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */