コード例 #1
0
ファイル: TeEditingHelper.cs プロジェクト: sillsdev/WorldPad
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Prevents IP-only selections from being associated with a verse number run.
		/// </summary>
		/// <param name="vwselNew">The new selection.</param>
		/// <param name="prootb">The root box.</param>
		/// <param name="updatedSelection"></param>
		/// ------------------------------------------------------------------------------------
		private void PreventIPAssociationWithVerseRun(IVwSelection vwselNew, IVwRootBox prootb,
			ref bool updatedSelection)
		{
			// Need to commit any editing in progress - otherwise attempt to read
			// paragraph that follows may get stale data.  This follows the example in
			// EditingHelper.SelectionChanged(). Also verify selection is still valid.
			Commit(vwselNew);
			if (!vwselNew.IsValid)
				return;

			SelectionHelper helper = SelectionHelper.Create(vwselNew, prootb.Site);

			// Get the tss that the IP is in
			ITsString tssSelectedScrPara = null;

			if (helper.LevelInfo.Length > 0)
			{
				switch (m_cache.GetClassOfObject(helper.LevelInfo[0].hvo))
				{
					case CmTranslation.kclsidCmTranslation:
						int hvo = helper.LevelInfo[0].hvo;
						int btWs = Callbacks.GetWritingSystemForHvo(hvo);
						CmTranslation trans = new CmTranslation(m_cache, hvo);
						tssSelectedScrPara = trans.Translation.GetAlternative(btWs).UnderlyingTsString;
						break;

					case StTxtPara.kclsidStTxtPara:
						StTxtPara para = new StTxtPara(m_cache, helper.LevelInfo[0].hvo);
						tssSelectedScrPara = para.Contents.UnderlyingTsString;
						break;
					case CmPicture.kclsidCmPicture:
					case StFootnote.kclsidStFootnote:
					default:
						// Not sure what it is, but it probably doesn't contain verse text...
						break;
				}
			}

			// Following code includes checking zero-length paragraphs for association with
			// the VerseNumber style so that empty paras will use the default character style
			if (tssSelectedScrPara == null)
				return;

			// Get the text props and run info of the run the IP is associating with
			int charPos = helper.IchAnchor;

			if (helper.AssocPrev && charPos > 0)
				charPos -= 1;

			if (charPos > tssSelectedScrPara.Length) // workaround for TE-5561
				charPos = tssSelectedScrPara.Length;

			TsRunInfo tri;
			ITsTextProps ttp = tssSelectedScrPara.FetchRunInfoAt(charPos, out tri);

			// These are the boundary conditions that require our intervention
			// regarding verse numbers.
			bool fEdgeOfTss = (helper.IchAnchor == 0 ||
			helper.IchAnchor == tssSelectedScrPara.Length);
			bool fBeginOfRun = (helper.IchAnchor == tri.ichMin);
			bool fEndOfRun = (helper.IchAnchor == tri.ichLim);

			if (!fEdgeOfTss && !fBeginOfRun && !fEndOfRun)
				return;

			// If the IP is associated with a verse number style run
			if (ttp.GetStrPropValue((int)FwTextPropType.ktptNamedStyle) != ScrStyleNames.VerseNumber)
				return;

			try
			{
				m_selectionUpdateInProcess = true; //set our semaphore

				// We must disassociate the IP from the verse number style run...

				// If IP is at beginning or end of paragraph, need to reset selection to
				// default paragraph chars (null style).
				if (fEdgeOfTss)
				{
					// REVIEW: Should we be re-getting this, or just using the one we have?
					vwselNew = prootb.Selection;
					ITsPropsBldr bldr = ttp.GetBldr();
					bldr.SetStrPropValue((int) FwTextPropType.ktptNamedStyle, null);
					vwselNew.SetIpTypingProps(bldr.GetTextProps());
				}
				else
				{
					// Else make the selection be associated with the other adjacent run.
					helper.AssocPrev = fBeginOfRun;
					helper.SetSelection(true);
				}

				updatedSelection = true;
			}
			finally
			{
				m_selectionUpdateInProcess = false; // reset semaphore
			}
		}