2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCIMEDispatcher.h"
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// add/remove delegate in CCIMEDelegate Cons/Destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCIMEDelegate::CCIMEDelegate()
|
|
|
|
{
|
|
|
|
CCIMEDispatcher::sharedDispatcher()->addDelegate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCIMEDelegate::~CCIMEDelegate()
|
|
|
|
{
|
|
|
|
CCIMEDispatcher::sharedDispatcher()->removeDelegate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDelegate::attachWithIME()
|
|
|
|
{
|
|
|
|
return CCIMEDispatcher::sharedDispatcher()->attachDelegateWithIME(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDelegate::detachWithIME()
|
|
|
|
{
|
|
|
|
return CCIMEDispatcher::sharedDispatcher()->detachDelegateWithIME(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef std::list< CCIMEDelegate * > DelegateList;
|
|
|
|
typedef std::list< CCIMEDelegate * >::iterator DelegateIter;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delegate List manage class
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class CCIMEDispatcher::Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Impl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Impl()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegateWithIme = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DelegateIter findDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter end = _delegateList.end();
|
|
|
|
for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (pDelegate == *iter)
|
|
|
|
{
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateList _delegateList;
|
|
|
|
CCIMEDelegate* _delegateWithIme;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cons/Destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCIMEDispatcher::CCIMEDispatcher()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _impl(new CCIMEDispatcher::Impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->init();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCIMEDispatcher::~CCIMEDispatcher()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_impl);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add/Attach/Remove CCIMEDelegate
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CCIMEDispatcher::addDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! pDelegate || ! _impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl->_delegateList.end() != _impl->findDelegate(pDelegate))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// pDelegate already in list
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateList.push_front(pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDispatcher::attachDelegateWithIME(CCIMEDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl || ! pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter end = _impl->_delegateList.end();
|
|
|
|
DelegateIter iter = _impl->findDelegate(pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// if pDelegate is not in delegate list, return
|
|
|
|
CC_BREAK_IF(end == iter);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl->_delegateWithIme)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// if old delegate canDetachWithIME return false
|
|
|
|
// or pDelegate canAttachWithIME return false,
|
|
|
|
// do nothing.
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl->_delegateWithIme->canDetachWithIME()
|
2012-04-19 14:35:52 +08:00
|
|
|
|| ! pDelegate->canAttachWithIME());
|
|
|
|
|
|
|
|
// detach first
|
2013-06-15 14:03:30 +08:00
|
|
|
CCIMEDelegate * pOldDelegate = _impl->_delegateWithIme;
|
|
|
|
_impl->_delegateWithIme = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
pOldDelegate->didDetachWithIME();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme = *iter;
|
2012-04-19 14:35:52 +08:00
|
|
|
pDelegate->didAttachWithIME();
|
|
|
|
bRet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// delegate hasn't attached to IME yet
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(! pDelegate->canAttachWithIME());
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme = *iter;
|
2012-04-19 14:35:52 +08:00
|
|
|
pDelegate->didAttachWithIME();
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDispatcher::detachDelegateWithIME(CCIMEDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl || ! pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// if pDelegate is not the current delegate attached to IME, return
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(_impl->_delegateWithIme != pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_BREAK_IF(! pDelegate->canDetachWithIME());
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
pDelegate->didDetachWithIME();
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::removeDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! pDelegate || ! _impl);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter iter = _impl->findDelegate(pDelegate);
|
|
|
|
DelegateIter end = _impl->_delegateList.end();
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_BREAK_IF(end == iter);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl->_delegateWithIme)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (*iter == _impl->_delegateWithIme)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateList.erase(iter);
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// dispatch text message
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchInsertText(const char * pText, int nLen)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl || ! pText || nLen <= 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// there is no delegate attached to IME
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl->_delegateWithIme);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme->insertText(pText, nLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchDeleteBackward()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// there is no delegate attached to IME
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(! _impl->_delegateWithIme);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_impl->_delegateWithIme->deleteBackward();
|
2012-04-19 14:35:52 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * CCIMEDispatcher::getContentText()
|
|
|
|
{
|
|
|
|
const char * pszContentText = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl && _impl->_delegateWithIme)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
pszContentText = _impl->_delegateWithIme->getContentText();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return (pszContentText) ? pszContentText : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// dispatch keyboard message
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchKeyboardWillShow(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter last = _impl->_delegateList.end();
|
|
|
|
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
|
|
|
pDelegate->keyboardWillShow(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchKeyboardDidShow(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter last = _impl->_delegateList.end();
|
|
|
|
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
|
|
|
pDelegate->keyboardDidShow(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchKeyboardWillHide(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter last = _impl->_delegateList.end();
|
|
|
|
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
|
|
|
pDelegate->keyboardWillHide(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchKeyboardDidHide(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_impl)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
DelegateIter last = _impl->_delegateList.end();
|
|
|
|
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
|
|
|
pDelegate->keyboardDidHide(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// protected member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCIMEDispatcher* CCIMEDispatcher::sharedDispatcher()
|
|
|
|
{
|
|
|
|
static CCIMEDispatcher s_instance;
|
|
|
|
return &s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|