2011-04-19 09:32:33 +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);
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
bool CCIMEDelegate::detachWithIME()
|
|
|
|
{
|
|
|
|
return CCIMEDispatcher::sharedDispatcher()->detachDelegateWithIME(this);
|
|
|
|
}
|
|
|
|
|
2011-04-19 09:32:33 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
typedef std::list< CCIMEDelegate * > DelegateList;
|
|
|
|
typedef std::list< CCIMEDelegate * >::iterator DelegateIter;
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delegate List manage class
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class CCIMEDispatcher::Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Impl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Impl()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
m_DelegateWithIme = 0;
|
2011-04-19 09:32:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DelegateIter findDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
DelegateIter end = m_DelegateList.end();
|
|
|
|
for (DelegateIter iter = m_DelegateList.begin(); iter != end; ++iter)
|
|
|
|
{
|
|
|
|
if (pDelegate == *iter)
|
|
|
|
{
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
DelegateList m_DelegateList;
|
|
|
|
CCIMEDelegate* m_DelegateWithIme;
|
2011-04-19 09:32:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Cons/Destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCIMEDispatcher::CCIMEDispatcher()
|
|
|
|
: m_pImpl(new CCIMEDispatcher::Impl)
|
|
|
|
{
|
|
|
|
m_pImpl->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCIMEDispatcher::~CCIMEDispatcher()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(m_pImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Add/Attach/Remove CCIMEDelegate
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void CCIMEDispatcher::addDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
if (! pDelegate || ! m_pImpl)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_pImpl->m_DelegateList.end() != m_pImpl->findDelegate(pDelegate))
|
|
|
|
{
|
|
|
|
// pDelegate already in list
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_pImpl->m_DelegateList.push_front(pDelegate);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDispatcher::attachDelegateWithIME(CCIMEDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! m_pImpl || ! pDelegate);
|
|
|
|
|
|
|
|
DelegateIter end = m_pImpl->m_DelegateList.end();
|
|
|
|
DelegateIter iter = m_pImpl->findDelegate(pDelegate);
|
|
|
|
|
|
|
|
// if pDelegate is not in delegate list, return
|
|
|
|
CC_BREAK_IF(end == iter);
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
if (m_pImpl->m_DelegateWithIme)
|
2011-04-19 09:32:33 +08:00
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
// if old delegate canDetachWithIME return false
|
2011-04-19 09:32:33 +08:00
|
|
|
// or pDelegate canAttachWithIME return false,
|
|
|
|
// do nothing.
|
2011-04-26 16:33:20 +08:00
|
|
|
CC_BREAK_IF(! m_pImpl->m_DelegateWithIme->canDetachWithIME()
|
2011-04-19 09:32:33 +08:00
|
|
|
|| ! pDelegate->canAttachWithIME());
|
|
|
|
|
|
|
|
// detach first
|
2011-04-26 16:33:20 +08:00
|
|
|
CCIMEDelegate * pOldDelegate = m_pImpl->m_DelegateWithIme;
|
|
|
|
m_pImpl->m_DelegateWithIme = 0;
|
|
|
|
pOldDelegate->didDetachWithIME();
|
2011-04-29 16:46:29 +08:00
|
|
|
|
|
|
|
m_pImpl->m_DelegateWithIme = *iter;
|
|
|
|
pDelegate->didAttachWithIME();
|
|
|
|
bRet = true;
|
|
|
|
break;
|
2011-04-19 09:32:33 +08:00
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
|
|
|
// havn't delegate attached with IME yet
|
|
|
|
CC_BREAK_IF(! pDelegate->canAttachWithIME());
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
m_pImpl->m_DelegateWithIme = *iter;
|
|
|
|
pDelegate->didAttachWithIME();
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCIMEDispatcher::detachDelegateWithIME(CCIMEDelegate * pDelegate)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! m_pImpl || ! pDelegate);
|
|
|
|
|
|
|
|
// if pDelegate is not the current delegate attached with ime, return
|
|
|
|
CC_BREAK_IF(m_pImpl->m_DelegateWithIme != pDelegate);
|
|
|
|
|
|
|
|
CC_BREAK_IF(! pDelegate->canDetachWithIME());
|
|
|
|
|
|
|
|
m_pImpl->m_DelegateWithIme = 0;
|
|
|
|
pDelegate->didDetachWithIME();
|
2011-04-19 09:32:33 +08:00
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::removeDelegate(CCIMEDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pDelegate || ! m_pImpl);
|
|
|
|
|
|
|
|
DelegateIter iter = m_pImpl->findDelegate(pDelegate);
|
|
|
|
DelegateIter end = m_pImpl->m_DelegateList.end();
|
|
|
|
CC_BREAK_IF(end == iter);
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
if (m_pImpl->m_DelegateWithIme)
|
|
|
|
|
|
|
|
if (*iter == m_pImpl->m_DelegateWithIme)
|
2011-04-19 09:32:33 +08:00
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
m_pImpl->m_DelegateWithIme = 0;
|
2011-04-19 09:32:33 +08:00
|
|
|
}
|
|
|
|
m_pImpl->m_DelegateList.erase(iter);
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2011-04-19 18:11:57 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// dispatch text message
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-19 09:32:33 +08:00
|
|
|
void CCIMEDispatcher::dispatchInsertText(const char * pText, int nLen)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! m_pImpl || ! pText || nLen <= 0);
|
|
|
|
|
|
|
|
// there is no delegate attach with ime
|
2011-04-26 16:33:20 +08:00
|
|
|
CC_BREAK_IF(! m_pImpl->m_DelegateWithIme);
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
m_pImpl->m_DelegateWithIme->insertText(pText, nLen);
|
2011-04-19 09:32:33 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCIMEDispatcher::dispatchDeleteBackward()
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(! m_pImpl);
|
|
|
|
|
|
|
|
// there is no delegate attach with ime
|
2011-04-26 16:33:20 +08:00
|
|
|
CC_BREAK_IF(! m_pImpl->m_DelegateWithIme);
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
m_pImpl->m_DelegateWithIme->deleteBackward();
|
2011-04-19 09:32:33 +08:00
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
2011-05-30 16:36:12 +08:00
|
|
|
const char * CCIMEDispatcher::getContentText()
|
|
|
|
{
|
|
|
|
const char * pszContentText = 0;
|
|
|
|
if (m_pImpl && m_pImpl->m_DelegateWithIme)
|
|
|
|
{
|
|
|
|
pszContentText = m_pImpl->m_DelegateWithIme->getContentText();
|
|
|
|
}
|
|
|
|
return (pszContentText) ? pszContentText : "";
|
|
|
|
}
|
|
|
|
|
2011-04-19 18:11:57 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// dispatch keyboard message
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
void CCIMEDispatcher::dispatchKeyboardWillShow(CCIMEKeyboardNotificationInfo& info)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
|
|
|
if (m_pImpl)
|
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
|
|
|
DelegateIter last = m_pImpl->m_DelegateList.end();
|
|
|
|
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
|
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
pDelegate->keyboardWillShow(info);
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
void CCIMEDispatcher::dispatchKeyboardDidShow(CCIMEKeyboardNotificationInfo& info)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
|
|
|
if (m_pImpl)
|
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
|
|
|
DelegateIter last = m_pImpl->m_DelegateList.end();
|
|
|
|
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
|
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
pDelegate->keyboardDidShow(info);
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
void CCIMEDispatcher::dispatchKeyboardWillHide(CCIMEKeyboardNotificationInfo& info)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
|
|
|
if (m_pImpl)
|
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
|
|
|
DelegateIter last = m_pImpl->m_DelegateList.end();
|
|
|
|
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
|
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
pDelegate->keyboardWillHide(info);
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 16:33:20 +08:00
|
|
|
void CCIMEDispatcher::dispatchKeyboardDidHide(CCIMEKeyboardNotificationInfo& info)
|
2011-04-19 18:11:57 +08:00
|
|
|
{
|
|
|
|
if (m_pImpl)
|
|
|
|
{
|
|
|
|
CCIMEDelegate * pDelegate = 0;
|
|
|
|
DelegateIter last = m_pImpl->m_DelegateList.end();
|
|
|
|
for (DelegateIter first = m_pImpl->m_DelegateList.begin(); first != last; ++first)
|
|
|
|
{
|
|
|
|
pDelegate = *(first);
|
|
|
|
if (pDelegate)
|
|
|
|
{
|
2011-04-26 16:33:20 +08:00
|
|
|
pDelegate->keyboardDidHide(info);
|
2011-04-19 18:11:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-19 09:32:33 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// protected member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static member function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCIMEDispatcher* CCIMEDispatcher::sharedDispatcher()
|
|
|
|
{
|
|
|
|
static CCIMEDispatcher s_instance;
|
|
|
|
return &s_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|