ime.cpp

来自「Windows CE 6.0 Word Application 源码」· C++ 代码 · 共 2,215 行 · 第 1/5 页

CPP
2,215 行
字号
		Assert ( retCode );

		ed.TxImmReleaseContext( hIMC );
	}
	else
	{
		CGenUndoBuilder undobldr(&ed, UB_AUTOCOMMIT);
		// for some reason, we didn't have a context, yet we thought we were still in IME
		// compostition mode.  Just force a shutdown here.
		EndCompositionGlue(ed, undobldr);
	}
}


/*
 *	CIme_Lev2::CIme_Lev2()
 *
 *	@mfunc
 *		CIme_Lev2 Constructor/Destructor.
 *
 *	@comm
 *		Needed to make sure _iFormatSave was handled properly.
 *
 */
CIme_Lev2::CIme_Lev2( 	
	CTxtEdit &ed )		// @parm the containing text edit.
{
	CTxtSelection		* const psel	= ed.GetSel();

	Assert ( psel );
	LONG	iFormat;

	SetIMECaretWidth ( dxCaret );			// setup initial caret width
	CIme::CheckKeyboardFontMatching ( GetKeyboardCodePage(), ed, &iFormat );
	_iFormatSave = iFormat;

	// hold notification unless client has set IMF_IMEALWAYSSENDNOTIFY via EM_SETLANGOPTIONS msg
	_fHoldNotify = ed._fIMEAlwaysNotify ? FALSE : TRUE ;

}

CIme_Lev2::~CIme_Lev2()
{
	ReleaseFormats( _iFormatSave, -1 );				// Release _iFormatSave.
}

/*
 *	HRESULT CIme_Lev2::StartComposition( CTxtEdit &ed, IUndoBuilder &undobldr )
 *
 *	@mfunc
 *		Begin IME Level 2 composition string processing.		
 *
 *	@comm
 *		Set the font, and location of the composition window which includes
 *		a bounding rect and the start position of the cursor. Also, reset
 *		the candidate window to allow the IME to set its position.
 *
 *	@rdesc
 *		HRESULT-S_FALSE for DefWindowProc processing, S_OK if not.
 */
HRESULT CIme_Lev2::StartComposition(
	CTxtEdit &ed,		// @parm the containing text edit.
	IUndoBuilder &undobldr)		// @parm required to modify the text.
{
	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CIme_Lev2::StartComposition");

	CTxtSelection		*psel;
	BOOL				bUnderLineMode = FALSE;

	_imeLevel = IME_LEVEL_2;

	SetCompositionFont( ed, &bUnderLineMode );						// Set font, & comp window.
	SetCompositionForm( ed );
	_fUnderLineMode = bUnderLineMode;

	// hide the caret since the IME is displays its own caret in level 2 comp.
	psel = ed.GetSel();							// Get the font cache for
	if ( psel )
	{
		psel->ShowCaret(FALSE);
	}

	return S_FALSE;									// Allow DefWindowProc
}													//  processing.

/*
 * BOOL CIme_Lev2::GetUndeterminedInfo(INT *picp, INT *pich)
 *  
 *  Gets the cp and cch of any ime undetermined text.
 *
 *  Returns FALSE if no errors, else TRUE
 */
BOOL CIme_Lev2::GetUndeterminedInfo(
    INT *picp, 
    INT *pich
    )
{
    // Level 2 IMEs don't support this function at all
    if(picp)
    {
        *picp = 0;
    }

    if(pich)
    {
        *pich = 0;
    }

    return TRUE;
}

/*
 *	HRESULT CIme_Lev2::CompositionString( const LPARAM lparam, CTxtEdit &ed,
 *				IUndoBuilder &undobldr )
 *
 *	@mfunc
 *		Handle Level 2 WM_IME_COMPOSITION messages.
 *
 *	@rdesc
 *		HRESULT-S_FALSE for DefWindowProc processing.
 *		
 *		Side effect: _fDestroy is set to notify that composition string
 *			processing is finished and this object should be deleted.
 *			The Host needs to mask out the lparam before calling DefWindowProc to
 *			prevent unnessary WM_IME_CHAR messages.
 */
HRESULT CIme_Lev2::CompositionString (
	const LPARAM lparam,		// @parm associated with message.
	CTxtEdit &ed,				// @parm the containing text edit.
	IUndoBuilder &undobldr)	// @parm required to modify the text.
{
	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CIme_Lev2::CompositionString");

	_fIgnoreIMEChar = FALSE;
	if ( HAVE_RESULT_STRING )
	{
		CTxtSelection	*psel;

		psel = ed.GetSel();

        if (NULL == psel)
        {
		    Assert(psel);
		    return E_OUTOFMEMORY;
		}

		// if selection, delete it.
		// This is to fix a problem where autofont matching is not working
		// on a selection.  We set the correct font during IME create.  Somehow,
		// when replacing a selection, somewhere the font is switched back to
		// the non-matching font.  So, I followed what we did with Level 3 IME - just
		// delete the selection before insert the result string.
		
		if ( psel->GetCpMost() != psel->GetCpMin() )		
		{
			psel->Set_iCF(_iFormatSave);
			psel->ReplaceRange( 0, NULL, &undobldr, SELRR_REMEMBERRANGE );					
		}

		psel->Set_iCF(_iFormatSave);

		CheckInsertResultString( lparam, ed, undobldr );

		psel->Set_iCF(_iFormatSave);
		SetCompositionForm( ed );					// Move Composition window.
		_fHoldNotify = FALSE;						// OK notify client for change

		// In case our host is not turning off the ResultString bit
		// we need to ignore WM_IME_CHAR or else we will get the same
		// DBC again.
		if ( !ed.fInOurHost() )
			_fIgnoreIMEChar = TRUE;
	}

	// Always return S_FALSE so the DefWindowProc will handle the rest.
	// Host has to mask out the ResultString bit to avoid WM_IME_CHAR coming in.
	return S_FALSE;																	
}

/*
 *	HRESULT CIme::CheckInsertResultString ( const LPARAM lparam, CTxtEdit &ed,
 *				IUndoBuilder &undobldr )
 *
 *	@mfunc
 *		handle inserting of GCS_RESULTSTR text, the final composed text.
 *
 *	@comm
 *		When the final composition string arrives we grab it and set it into the text.
 *
 *	@devnote
 *		A GCS_RESULTSTR message can arrive and the IME will *still* be in
 *		composition string mode. This occurs because the IME's internal
 *		buffers overflowed and it needs to convert the beginning of the buffer
 *		to clear out some room.	When this happens we need to insert the
 *		converted text as normal, but remain in composition processing mode.
 *
 *	@rdesc
 *		HRESULT-S_FALSE for DefWindowProc processing, S_OK if not.
 *		Side effect: _fDestroy is set to notify that composition string
 *			processing is finished and this object should be deleted.
 */
HRESULT CIme::CheckInsertResultString (
	const LPARAM lparam,		// @parm associated with message.
	CTxtEdit &ed,				// @parm the containing text edit.
	IUndoBuilder &undobldr )	// @parm required to modify the text.
{
	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CheckInsertResultString");

	HRESULT			hr = S_FALSE;

	HIMC 			hIMC;
	INT				cch;
	WCHAR			uniCompStr[256];

	CTxtSelection	*psel;
	DWORD			cp;

	if ( CLEANUP_COMPOSITION_STRING || HAVE_RESULT_STRING )						// If result string..
	{
		psel = ed.GetSel();
		Assert(psel);											


		hIMC = ed.TxImmGetContext();			// Get host's IME context.

		cch = 0;
		if ( hIMC )								// Get result string.
		{
			cch = GetCompositionStringInfo( hIMC, GCS_RESULTSTR,
							uniCompStr,
							sizeof(uniCompStr)/sizeof(uniCompStr[0]),
							NULL, 0, NULL, NULL );
			ed.TxImmReleaseContext( hIMC );		// Done with IME context.
		}

		if ( psel )								// Copy to backing store.
		{
			// Don't need to replace range when there isn't any text. Otherwise, the character format is
			// reset to previous run.
			if ( cch )
			{
#ifdef PWD_JUPITER
                // GuyBark Jupiter:
                // If we're inserting some J text, make sure a J font is current.

                // Make sure the Unicode string is null terminated. In theory
                // we could be losing a character by doing this. But we have
                // an arbitrary limit of 256 anyway.
                if(cch == sizeof(uniCompStr)/sizeof(uniCompStr[0]))
                {
                    --cch;
                }

                uniCompStr[cch] = '\0';
                psel->SetupJFont(uniCompStr);

                // GuyBark JupiterJ 50771:
                // The device IME can lead to the null character being inserted, (and other
                // control characters). Remove these here, as these aren't characters which
                // the user can see in the document, and they will lead to special properties
                // if saved out later as a Word document, (eg section breaks can appear).
                // So strip any control characters from the ime text here. Don't do this
                // beneath CleanseAndReplaceRange() below as that's called for other things
                // than ime, and I want destabilize a little as possible here. Note: The edit
                // control on the device removes the characters when the text is determined,
                // but it's simplest here just to replace them with spaces.

                INT i;

                // Whatever we do, don't overwrite the string's final null terminator!
                for(i = 0; i < cch; i++)
                {
                    // All Unicode here, so don't worry about lead/trail bytes.
                    if(uniCompStr[i] < 0x20)
                    {
                        uniCompStr[i] = ' ';
                    }
                }

#endif //PWD_JUPITER

				psel->CleanseAndReplaceRange( cch, uniCompStr, TRUE, &undobldr );

				cp = psel->GetCp();				
				psel->SetSelection(	cp, cp );
				undobldr.StopGroupTyping();
			}
			hr = S_OK;							// Don't want WM_IME_CHARs.
		}
	}

	return hr;
}

/*
 *	HRESULT CIme_Lev2::IMENotify( const WPARAM wparam, const LPARAM lparam,
 *					CTxtEdit &ed)
 *
 *	@mfunc
 *		Handle Level 2 WM_IME_NOTIFY messages.
 *
 *	@comm
 *		Currently we are only interested in knowing when to reset
 *		the candidate window's position.
 *
 *	@rdesc
 *		HRESULT-S_FALSE for DefWindowProc processing, S_OK if not.
 */
HRESULT CIme_Lev2::IMENotify(
	const WPARAM wparam,		// @parm associated with message.
	const LPARAM lparam,		// @parm associated with message.
	CTxtEdit &ed)				// @parm the containing text edit.
{
 	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CIme_Lev2::IMENotify");

	if ( IMN_OPENCANDIDATE == wparam )
	{
		Assert ( 0 != lparam );

#ifndef MACPORT

		HIMC			hIMC;							// Host's IME context.

		INT				index;							// Candidate window index.
		CANDIDATEFORM	cdCandForm;

		hIMC = ed.TxImmGetContext();				// Get host's IME context.

		if ( hIMC )
		{
													// Convert bitID to INDEX.
			for (index = 0; index < 32; index++ )	//  because of API.
			{
				if ( 0 != ((1 << index) & lparam) )
					break;
			}
			Assert ( ((1 << index) & lparam) == lparam);	// Only 1 set?
			Assert ( index < 32 );						
													// Reset to CFS_DEFAULT
			if ( pImmGetCandidateWindow( hIMC, index, &cdCandForm)
					&& CFS_DEFAULT != cdCandForm.dwStyle )
			{
				cdCandForm.dwStyle = CFS_DEFAULT;
				pImmSetCandidateWindow(hIMC, &cdCandForm);
			}

			ed.TxImmReleaseContext( hIMC );			// Done with IME context.
		}
#endif
	}	

	return S_FALSE;									// Allow DefWindowProc
}													//  processing.

/*
 *	void CIme_Lev2::PostIMEChar ( CTxtEdit &ed )
 *
 *	@mfunc
 *		Called after processing a single WM_IME_CHAR in order to
 *		update the position of the IME's composition window.		
 *
 */
void CIme_Lev2::PostIMEChar (
	CTxtEdit &ed)				// @parm the containing text edit.
{
 	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CIme_Lev2::PostIMEChar");

	SetCompositionForm( ed );						// Move Composition window.
}


/*
 *	HRESULT CIme_Lev3::StartComposition( CTxtEdit &ed, IUndoBuilder &undobldr )
 *
 *	@mfunc
 *		Begin IME Level 3 composition string processing.		
 *
 *	@comm
 *		For rudimentary processing, remember the start and
 *		length of the selection. Set the font in case the
 *		candidate window actually uses this information.
 *
 *	@rdesc
 *		This is a rudimentary solution for remembering were
 *		the composition is in the text. There needs to be work
 *		to replace this with a composition "range".
 *
 *	@rdesc
 *		HRESULT-S_FALSE for DefWindowProc processing, S_OK if not.
 */
HRESULT CIme_Lev3::StartComposition(
	CTxtEdit &ed ,		// @parm the containing text edit.
	IUndoBuilder &undobldr)		// @parm required to modify the text.
{
	TRACEBEGIN(TRCSUBSYSFE, TRCSCOPEINTERN, "CIme_Lev3::StartComposition");

	CTxtSelection		* const psel	= ed.GetSel();
	BOOL				bUnderLineMode = FALSE;
	
	Assert (psel);

	_imeLevel		= IME_LEVEL_3;		
	_ichStart		= psel ? psel->GetCpMin() : 0;
	_cchCompStr		= 0;

	// GuyBark JupiterJ IME: No candidate list open yet.
	_dwIndex = 0xFFFF;

#ifndef MACPORT										// Korean IME check.
	HIMC				hIMC;						// Get host's IME context.
	DWORD				dwConversion, dwSentence;

	hIMC = ed.TxImmGetContext();					// Get host's IME context.

	if ( hIMC )										// Set _fKorean flag.
	{												//  for block cursor.
		if ( pImmGetConversionStatus ( hIMC, &dwConversion, &dwSentence ) )
		{
			// NOTE:- the following is set for all FE system during IME input, so,
			// we also need to check keyboard codepage as well.
			if ( dwConversion & IME_CMODE_HANGEUL )
				_fKorean = (_KOREAN_CP == GetKeyboardCodePage());			
		}
		ed.TxImmReleaseContext( hIMC );				// Done with IME context.
	}
#endif

	SetCompositionFont ( ed, &bUnderLineMode );
	_fUnderLineMode = bUnderLineMode;

	if ( psel && psel->GetCpMost() != _ichStart )		// If selection, delete it.
	{
		ed._DeleteBeforeConvert = 1;	// Fix for WinCEOS RAID #12736

		psel->AdjustEndEOP(NEWCHARS);
		psel->ReplaceRange( 0, NULL, &undobldr, SELRR_REMEMBERRANGE );
	}

	return S_OK;									// No DefWindowProc
}													//  processing.

/*
 *	HRESULT CIme_Lev3::CompositionString( const LPARAM lparam, CTxtEdit &ed,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?