Example #1
0
		/// -----------------------------------------------------------------------------------
		/// <summary>
		/// When the user has selected a keyboard from the system tray, adjust the language of
		/// the selection to something that matches, if possible.
		/// </summary>
		/// <param name="vwsel">Selection</param>
		/// <param name="wsMatch">Writing system determined from keyboard change</param>
		/// -----------------------------------------------------------------------------------
		public virtual void HandleKeyboardChange(IVwSelection vwsel, int wsMatch)
		{
			CheckDisposed();
			// Get the writing system factory associated with the root box.
			if (m_rootb == null || !GotCacheOrWs)
				return; // For paranoia.

			if (vwsel == null)
			{
				// Delay handling it until we get a selection.
				m_wsPending = wsMatch;
				return;
			}
			bool fRange = vwsel.IsRange;
			if (fRange)
			{
				// Delay handling it until we get an insertion point.
				m_wsPending = wsMatch;
				return;
			}

			ITsTextProps[] vttp;
			IVwPropertyStore[] vvps;
			int cttp;

			SelectionHelper.GetSelectionProps(vwsel, out vttp, out vvps, out cttp);

			if (cttp == 0)
				return;
			Debug.Assert(cttp == 1);

			// If nothing changed, avoid the infinite loop that happens when we change the selection
			// and update the system keyboard, which in turn tells the program to change its writing system.
			// (This is a problem on Windows 98.)
			int wsTmp;
			int var;
			wsTmp = vttp[0].GetIntPropValues((int)FwTextPropType.ktptWs, out var);
			if (wsTmp == wsMatch)
				return;

			ITsPropsBldr tpb = vttp[0].GetBldr();
			tpb.SetIntPropValues((int)FwTextPropType.ktptWs,
				(int)FwTextPropVar.ktpvDefault, wsMatch);
			vttp[0] = tpb.GetTextProps();
			vwsel.SetSelectionProps(cttp, vttp);
			SelectionChanged(m_rootb, m_rootb.Selection); // might NOT be vwsel any more
		}
Example #2
0
		/// -----------------------------------------------------------------------------------
		/// <summary>
		/// When the user has selected a keyboard from the system tray, adjust the language of
		/// the selection to something that matches, if possible.
		/// </summary>
		/// <param name="vwsel">Selection</param>
		/// <param name="nLangId">Language identification</param>
		/// -----------------------------------------------------------------------------------
		public virtual void HandleKeyboardChange(IVwSelection vwsel, short nLangId)
		{
			CheckDisposed();
			// Get the writing system factory associated with the root box.
			if (m_rootb == null || !GotCacheOrWs)
				return; // For paranoia.

			//			Debug.WriteLine("HandleKeyboardChange nLangId=" + nLangId + "; " + Name +
			//				"/" + this);

			ILgWritingSystemFactory wsf = WritingSystemFactory;

			int cws = wsf.NumberOfWs;
			if (cws < 2)
				return;	// no writing systems to work with

			int[] vwsTemp = new int[0];
			using (ArrayPtr ptr = MarshalEx.ArrayToNative(cws, typeof(int)))
			{
				wsf.GetWritingSystems(ptr, cws);
				vwsTemp = (int[])MarshalEx.NativeToArray(ptr, cws, typeof(int));
			}

			// resize the array leaving slot 0 empty
			int[] vws = new int[++cws];
			Array.Copy(vwsTemp, 0, vws, 1, vwsTemp.Length);

			// Put the writing system of the selection first in the list, which gives it
			// priority -- we'll find it first if it matches.
			int wsSel = SelectionHelper.GetFirstWsOfSelection(vwsel);
			vws[0] = wsSel != 0 ? wsSel : vws[1];

			InputLanguage lngDefault = InputLanguage.DefaultInputLanguage;
			short defaultLangId = LcidHelper.LangIdFromLCID(lngDefault.Culture.LCID);
			int wsMatch = -1;
			int wsDefault = -1;
			int wsCurrentLang = -1; // used to note first ws whose CurrentInputLanguage matches.
			for (int iws = 0; iws < cws; iws++)
			{
				if (vws[iws] == 0)
					continue;

				IWritingSystem ws = wsf.get_EngineOrNull(vws[iws]);
				if (ws == null)
					continue;

				// REVIEW SteveMc, SharonC, KenZ, JohnT: nail down where the locale/langid belongs, in
				// the writing system or in the old writing system.
				int nLocale = ws.Locale;
				int nLangIdWs = LcidHelper.LangIdFromLCID(nLocale);

				if (nLangIdWs != 0 && nLangIdWs == nLangId)
				{
					wsMatch = vws[iws];
					break;
				}
				if (iws == 0 && nLangIdWs == 0 && nLangId == defaultLangId)
				{
					// The writing system of the current selection doesn't have any keyboard specified,
					// and we've set the keyboard to the default. This is acceptable; leave as is.
					wsMatch = vws[iws];
					break;
				}
				if (nLangIdWs == 0 && nLangId == defaultLangId && wsDefault == -1)
				{
					// Use this old writing system as the default.
					wsDefault = vws[iws];
				}
				if (wsCurrentLang == -1)
				{
					int nLangIdCurrent = ws.CurrentInputLanguage;
					if (nLangId == nLangIdCurrent)
						wsCurrentLang = vws[iws];
				}
			}

			if (wsMatch == -1)
			{
				wsMatch = wsDefault;
			}
			m_wsPending = -1;
			// Next, see if it is the current langid of any ws. This will leave it -1 if we didn't find such a match.
			if (wsMatch == -1)
				wsMatch = wsCurrentLang;

			if (wsMatch == -1)
			{
				// Nothing matched.
				if (defaultLangId == nLangId) // We're trying to set to the default keyboard
				{
					// The default keyboard sets set for odd reasons. Just ignore it.
					// Review: what if the HKL's are different versions of the same language,
					// eg UK and US English?
				}
				else
				{
					// We will make this the current input language for the current writing system for the current session.
					IWritingSystem wsCurrent = wsf.get_EngineOrNull(wsSel);
					if (wsCurrent != null)
						wsCurrent.CurrentInputLanguage = nLangId;
				}
				return;
			}

			// We are going to make wsMatch the current writing system.
			// Make sure it is set to use the langid that the user just selected.
			// (This cleans up any earlier overrides).
			IWritingSystem wsMatchEng = wsf.get_EngineOrNull(wsMatch);
			if (wsMatchEng != null)
				wsMatchEng.CurrentInputLanguage = nLangId;

			if (vwsel == null)
			{
				// Delay handling it until we get a selection.
				m_wsPending = wsMatch;
				return;
			}
			bool fRange = vwsel.IsRange;
			if (fRange)
			{
				// Delay handling it until we get an insertion point.
				m_wsPending = wsMatch;
				return;
			}

			ITsTextProps[] vttp;
			IVwPropertyStore[] vvps;
			int cttp;

			SelectionHelper.GetSelectionProps(vwsel, out vttp, out vvps, out cttp);

			if (cttp == 0)
				return;
			Debug.Assert(cttp == 1);

			// If nothing changed, avoid the infinite loop that happens when we change the selection
			// and update the system keyboard, which in turn tells the program to change its writing system.
			// (This is a problem on Windows 98.)
			int wsTmp;
			int var;
			wsTmp = vttp[0].GetIntPropValues((int)FwTextPropType.ktptWs, out var);
			if (wsTmp == wsMatch)
				return;

			ITsPropsBldr tpb = vttp[0].GetBldr();
			tpb.SetIntPropValues((int)FwTextPropType.ktptWs,
				(int)FwTextPropVar.ktpvDefault, wsMatch);
			vttp[0] = tpb.GetTextProps();
			vwsel.SetSelectionProps(cttp, vttp);
			SelectionChanged(m_rootb, vwsel);
		}