CCIMEDispatcher.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /****************************************************************************
  2. Copyright (c) 2010 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 "base/CCIMEDispatcher.h"
  23. #include <list>
  24. NS_CC_BEGIN
  25. //////////////////////////////////////////////////////////////////////////
  26. // add/remove delegate in IMEDelegate Cons/Destructor
  27. //////////////////////////////////////////////////////////////////////////
  28. IMEDelegate::IMEDelegate()
  29. {
  30. IMEDispatcher::sharedDispatcher()->addDelegate(this);
  31. }
  32. IMEDelegate::~IMEDelegate()
  33. {
  34. IMEDispatcher::sharedDispatcher()->removeDelegate(this);
  35. }
  36. bool IMEDelegate::attachWithIME()
  37. {
  38. return IMEDispatcher::sharedDispatcher()->attachDelegateWithIME(this);
  39. }
  40. bool IMEDelegate::detachWithIME()
  41. {
  42. return IMEDispatcher::sharedDispatcher()->detachDelegateWithIME(this);
  43. }
  44. //////////////////////////////////////////////////////////////////////////
  45. typedef std::list< IMEDelegate * > DelegateList;
  46. typedef std::list< IMEDelegate * >::iterator DelegateIter;
  47. //////////////////////////////////////////////////////////////////////////
  48. // Delegate List manage class
  49. //////////////////////////////////////////////////////////////////////////
  50. class IMEDispatcher::Impl
  51. {
  52. public:
  53. Impl()
  54. {
  55. }
  56. ~Impl()
  57. {
  58. }
  59. void init()
  60. {
  61. _delegateWithIme = 0;
  62. }
  63. DelegateIter findDelegate(IMEDelegate* delegate)
  64. {
  65. DelegateIter end = _delegateList.end();
  66. for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter)
  67. {
  68. if (delegate == *iter)
  69. {
  70. return iter;
  71. }
  72. }
  73. return end;
  74. }
  75. DelegateList _delegateList;
  76. IMEDelegate* _delegateWithIme;
  77. };
  78. //////////////////////////////////////////////////////////////////////////
  79. // Cons/Destructor
  80. //////////////////////////////////////////////////////////////////////////
  81. IMEDispatcher::IMEDispatcher()
  82. : _impl(new IMEDispatcher::Impl)
  83. {
  84. _impl->init();
  85. }
  86. IMEDispatcher::~IMEDispatcher()
  87. {
  88. CC_SAFE_DELETE(_impl);
  89. }
  90. //////////////////////////////////////////////////////////////////////////
  91. // Add/Attach/Remove IMEDelegate
  92. //////////////////////////////////////////////////////////////////////////
  93. void IMEDispatcher::addDelegate(IMEDelegate* delegate)
  94. {
  95. if (! delegate || ! _impl)
  96. {
  97. return;
  98. }
  99. if (_impl->_delegateList.end() != _impl->findDelegate(delegate))
  100. {
  101. // pDelegate already in list
  102. return;
  103. }
  104. _impl->_delegateList.push_front(delegate);
  105. }
  106. bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
  107. {
  108. bool ret = false;
  109. do
  110. {
  111. CC_BREAK_IF(! _impl || ! delegate);
  112. DelegateIter end = _impl->_delegateList.end();
  113. DelegateIter iter = _impl->findDelegate(delegate);
  114. // if pDelegate is not in delegate list, return
  115. CC_BREAK_IF(end == iter);
  116. if (_impl->_delegateWithIme)
  117. {
  118. if (_impl->_delegateWithIme != delegate)
  119. {
  120. // if old delegate canDetachWithIME return false
  121. // or pDelegate canAttachWithIME return false,
  122. // do nothing.
  123. CC_BREAK_IF(!_impl->_delegateWithIme->canDetachWithIME()
  124. || !delegate->canAttachWithIME());
  125. // detach first
  126. IMEDelegate * oldDelegate = _impl->_delegateWithIme;
  127. _impl->_delegateWithIme = 0;
  128. oldDelegate->didDetachWithIME();
  129. _impl->_delegateWithIme = *iter;
  130. delegate->didAttachWithIME();
  131. }
  132. ret = true;
  133. break;
  134. }
  135. // delegate hasn't attached to IME yet
  136. CC_BREAK_IF(! delegate->canAttachWithIME());
  137. _impl->_delegateWithIme = *iter;
  138. delegate->didAttachWithIME();
  139. ret = true;
  140. } while (0);
  141. return ret;
  142. }
  143. bool IMEDispatcher::detachDelegateWithIME(IMEDelegate * delegate)
  144. {
  145. bool ret = false;
  146. do
  147. {
  148. CC_BREAK_IF(! _impl || ! delegate);
  149. // if pDelegate is not the current delegate attached to IME, return
  150. CC_BREAK_IF(_impl->_delegateWithIme != delegate);
  151. CC_BREAK_IF(! delegate->canDetachWithIME());
  152. _impl->_delegateWithIme = 0;
  153. delegate->didDetachWithIME();
  154. ret = true;
  155. } while (0);
  156. return ret;
  157. }
  158. void IMEDispatcher::removeDelegate(IMEDelegate* delegate)
  159. {
  160. do
  161. {
  162. CC_BREAK_IF(! delegate || ! _impl);
  163. DelegateIter iter = _impl->findDelegate(delegate);
  164. DelegateIter end = _impl->_delegateList.end();
  165. CC_BREAK_IF(end == iter);
  166. if (_impl->_delegateWithIme)
  167. if (*iter == _impl->_delegateWithIme)
  168. {
  169. _impl->_delegateWithIme = 0;
  170. }
  171. _impl->_delegateList.erase(iter);
  172. } while (0);
  173. }
  174. //////////////////////////////////////////////////////////////////////////
  175. // dispatch text message
  176. //////////////////////////////////////////////////////////////////////////
  177. void IMEDispatcher::dispatchInsertText(const char * text, size_t len)
  178. {
  179. do
  180. {
  181. CC_BREAK_IF(! _impl || ! text || len <= 0);
  182. // there is no delegate attached to IME
  183. CC_BREAK_IF(! _impl->_delegateWithIme);
  184. _impl->_delegateWithIme->insertText(text, len);
  185. } while (0);
  186. }
  187. void IMEDispatcher::dispatchDeleteBackward()
  188. {
  189. do
  190. {
  191. CC_BREAK_IF(! _impl);
  192. // there is no delegate attached to IME
  193. CC_BREAK_IF(! _impl->_delegateWithIme);
  194. _impl->_delegateWithIme->deleteBackward();
  195. } while (0);
  196. }
  197. void IMEDispatcher::dispatchControlKey(EventKeyboard::KeyCode keyCode)
  198. {
  199. do
  200. {
  201. CC_BREAK_IF(!_impl);
  202. // there is no delegate attached to IME
  203. CC_BREAK_IF(!_impl->_delegateWithIme);
  204. _impl->_delegateWithIme->controlKey(keyCode);
  205. } while (0);
  206. }
  207. const std::string& IMEDispatcher::getContentText()
  208. {
  209. if (_impl && _impl->_delegateWithIme)
  210. {
  211. return _impl->_delegateWithIme->getContentText();
  212. }
  213. return STD_STRING_EMPTY;
  214. }
  215. //////////////////////////////////////////////////////////////////////////
  216. // dispatch keyboard message
  217. //////////////////////////////////////////////////////////////////////////
  218. void IMEDispatcher::dispatchKeyboardWillShow(IMEKeyboardNotificationInfo& info)
  219. {
  220. if (_impl)
  221. {
  222. IMEDelegate * delegate = nullptr;
  223. DelegateIter last = _impl->_delegateList.end();
  224. for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
  225. {
  226. delegate = *(first);
  227. if (delegate)
  228. {
  229. delegate->keyboardWillShow(info);
  230. }
  231. }
  232. }
  233. }
  234. void IMEDispatcher::dispatchKeyboardDidShow(IMEKeyboardNotificationInfo& info)
  235. {
  236. if (_impl)
  237. {
  238. IMEDelegate * delegate = nullptr;
  239. DelegateIter last = _impl->_delegateList.end();
  240. for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
  241. {
  242. delegate = *(first);
  243. if (delegate)
  244. {
  245. delegate->keyboardDidShow(info);
  246. }
  247. }
  248. }
  249. }
  250. void IMEDispatcher::dispatchKeyboardWillHide(IMEKeyboardNotificationInfo& info)
  251. {
  252. if (_impl)
  253. {
  254. IMEDelegate * delegate = nullptr;
  255. DelegateIter last = _impl->_delegateList.end();
  256. for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
  257. {
  258. delegate = *(first);
  259. if (delegate)
  260. {
  261. delegate->keyboardWillHide(info);
  262. }
  263. }
  264. }
  265. }
  266. void IMEDispatcher::dispatchKeyboardDidHide(IMEKeyboardNotificationInfo& info)
  267. {
  268. if (_impl)
  269. {
  270. IMEDelegate * delegate = nullptr;
  271. DelegateIter last = _impl->_delegateList.end();
  272. for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
  273. {
  274. delegate = *(first);
  275. if (delegate)
  276. {
  277. delegate->keyboardDidHide(info);
  278. }
  279. }
  280. }
  281. }
  282. //////////////////////////////////////////////////////////////////////////
  283. // protected member function
  284. //////////////////////////////////////////////////////////////////////////
  285. //////////////////////////////////////////////////////////////////////////
  286. // static member function
  287. //////////////////////////////////////////////////////////////////////////
  288. IMEDispatcher* IMEDispatcher::sharedDispatcher()
  289. {
  290. static IMEDispatcher s_instance;
  291. return &s_instance;
  292. }
  293. NS_CC_END