CCIMEDispatcher.cpp 9.5 KB

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