Context menu to help build regular expressions. To be used in Find dialog boxes with regex support.
Inheritance: HelperMenu
Exemple #1
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Initializes a new instance of the <see cref="T:SimpleMatchDlg"/> class.
		/// </summary>
		/// <param name="wsf">The WSF.</param>
		/// <param name="ws">The ws.</param>
		/// <param name="ss">The ss.</param>
		/// ------------------------------------------------------------------------------------
		public SimpleMatchDlg(ILgWritingSystemFactory wsf, int ws, IVwStylesheet ss)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// We do this outside the designer-controlled code because it does funny things
			// to FwTextBoxes, owing to the need for a writing system factory, and some
			// properties it should not persist but I can't persuade it not to.
			this.m_textBox = new FwTextBox();
			this.m_textBox.WritingSystemFactory = wsf; // set ASAP.
			this.m_textBox.WritingSystemCode = ws;
			this.m_textBox.StyleSheet = ss; // before setting text, otherwise it gets confused about height needed.
			this.m_textBox.Location = new System.Drawing.Point(8, 24);
			this.m_textBox.Name = "m_textBox";
			this.m_textBox.Size = new System.Drawing.Size(450, 32);
			this.m_textBox.TabIndex = 0;
			this.m_textBox.Text = "";
			this.Controls.Add(this.m_textBox);

			regexContextMenu = new RegexHelperMenu(m_textBox, FwApp.App);

			m_ivwpattern = VwPatternClass.Create();

			helpProvider = new System.Windows.Forms.HelpProvider();
			helpProvider.HelpNamespace = FwApp.App.HelpFile;
			helpProvider.SetHelpKeyword(this, FwApp.App.GetHelpString(s_helpTopic, 0));
			helpProvider.SetHelpNavigator(this, System.Windows.Forms.HelpNavigator.Topic);
		}
		public override void Init(Mediator mediator, XmlNode configurationParameters)
		{
			CheckDisposed();
			base.Init(mediator, configurationParameters);

			m_tbSearchText.WritingSystemFactory = m_cache.LanguageWritingSystemFactoryAccessor;
			m_tbSearchText.AdjustForStyleSheet(FontHeightAdjuster.StyleSheetFromMediator(mediator));
			m_tbSearchText.Text = String.Empty;
			m_tbSearchText.TextChanged += m_tbSearchText_TextChanged;
			m_tbSearchText.KeyDown += m_tbSearchText_KeyDown;
			FillLineComboList();

			m_fwtbItem.WritingSystemFactory = m_cache.LanguageWritingSystemFactoryAccessor;
			m_fwtbItem.StyleSheet = FontHeightAdjuster.StyleSheetFromMediator(m_mediator);
			m_fwtbItem.WritingSystemCode = m_cache.DefaultVernWs;
			m_fwtbItem.Text = String.Empty;
			m_fwtbItem.Visible = false; // Needed to prevent LT-12162 unneeded text box.

			// Set some default values.

			m_rbtnAnywhere.Checked = true;
			m_btnRegExp.Enabled = false;
			m_chkMatchDiacritics.Checked = false;
			m_chkMatchCase.Checked = false;
			m_btnSearch.Enabled = false;

			m_regexContextMenu = new RegexHelperMenu(m_tbSearchText, m_helpTopicProvider);

			if (m_helpTopicProvider != null)
				this.helpProvider.HelpNamespace = m_helpTopicProvider.HelpFile;
			this.helpProvider.SetHelpNavigator(this, HelpNavigator.Topic);
			this.helpProvider.SetShowHelp(this, true);
			if (m_helpTopicProvider != null)
			{
				helpProvider.SetHelpKeyword(this, "khtpSpecConcordanceCrit");
				m_btnHelp.Enabled = true;
			}

			m_cbSearchText.WritingSystemFactory = m_cache.LanguageWritingSystemFactoryAccessor;

			if (m_clerk.SuspendLoadingRecordUntilOnJumpToRecord)
			{
				return;	// we're bound to process OnJumpToRecord, so skip any further initialization.
			}
			// Load any saved settings.
			LoadSettings();
		}
		public ConcordanceControl()
		{
			InitializeComponent();

			m_regexContextMenu = new RegexHelperMenu(m_tbSearchText, FwApp.App);
			m_vwPattern = VwPatternClass.Create();

			if (FwApp.App != null)
				this.helpProvider.HelpNamespace = FwApp.App.HelpFile;
			this.helpProvider.SetHelpNavigator(this, System.Windows.Forms.HelpNavigator.Topic);
			this.helpProvider.SetShowHelp(this, true);
			if (FwApp.App != null)
			{
				helpProvider.SetHelpKeyword(this, "khtpSpecConcordanceCrit");
				m_btnHelp.Enabled = true;
			}
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Sets the initial values for the dialog controls, prior to displaying the dialog.
		/// This private overload takes all possible arguments, and is called in various ways
		/// by the others.
		/// </summary>
		/// <param name="cache"></param>
		/// <param name="vwPattern">Find/replace values</param>
		/// <param name="rootSite">view (may be null!)</param>
		/// <param name="fReplace"><c>true</c> to initially display replace dialog page</param>
		/// <param name="fOverlays">ignored for now</param>
		/// <param name="sUserWs">UI writing system</param>
		/// <param name="hwnd">The window handle of the main window that owns the rootsite
		/// </param>
		/// <param name="helpTopicProvider">help topic provider allows the dialog box class
		/// to specify the appropriate help topic path for this dialog
		/// </param>
		/// <param name="wsEdit">writing sytem for the find and replace edit boxes</param>
		/// <remarks>ENHANCE JohnT: it may need more arguments, for example, the name of the
		/// kind of object we can restrict the search to, a list of fields.</remarks>
		/// <returns>true if the dialog was initialized properly, otherwise false.
		/// False indicates some problem and the find/replace dialog should not be
		/// shown at this time.</returns>
		/// ------------------------------------------------------------------------------------
		private bool SetDialogValues(FdoCache cache, IVwPattern vwPattern, IVwRootSite rootSite,
			bool fReplace, bool fOverlays, string sUserWs, IntPtr hwnd,
			IHelpTopicProvider helpTopicProvider, int wsEdit)
		{
			fweditFindText.controlID = "Find";
			fweditReplaceText.controlID = "Replace";
			// save the pattern and put the text into the find edit box.
			if (vwPattern == null)
				throw new ArgumentNullException("vwPattern");
			m_vwPattern = vwPattern;

			m_helpTopicProvider = helpTopicProvider;

			SetOwner(cache, rootSite, hwnd, vwPattern, wsEdit);
			tabControls.SelectedTab = fReplace ? tabReplace : tabFind;
			tabControls_SelectedIndexChanged(null, new EventArgs());

			if (m_helpTopicProvider != null) // Will be null when running tests
			{
				this.helpProvider.HelpNamespace = DirectoryFinder.FWCodeDirectory +
					m_helpTopicProvider.GetHelpString("UserHelpFile", 0);
			}

			SetCheckboxStates(vwPattern);

			regexContextMenuFind = new RegexHelperMenu(fweditFindText, m_helpTopicProvider);
			regexContextMenuReplace = new RegexHelperMenu(fweditReplaceText, m_helpTopicProvider, false);

			// The Regex context menus need to be turned off in TE due to a UI freeze
			if (Application.ProductName != "Language Explorer")
			{
				btnRegexMenuFind.Visible = btnRegexMenuFind.Enabled = false;
				btnRegexMenuReplace.Visible = btnRegexMenuReplace.Enabled = false;

				// Now grow the text fields to fill the empty space
				fweditFindText.Width = fweditReplaceText.Width = 472;
			}

			// everything seems ok to proceed with find/replace
			return true;
		}
		/// -----------------------------------------------------------------------------------
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged
		/// resources; <c>false</c> to release only unmanaged resources.
		/// </param>
		/// -----------------------------------------------------------------------------------
		protected override void Dispose(bool disposing)
		{
			System.Diagnostics.Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + ". ****** ");
			// Must not be run more than once.
			if (IsDisposed)
				return;

			m_lastTextBoxInFocus = null;

			if (disposing)
			{
				if (components != null)
					components.Dispose();
				if (m_cacheMadeLocally && m_cache != null)
					m_cache.Dispose();
				if (regexContextMenuFind != null)
					regexContextMenuFind.Dispose();
				if (regexContextMenuReplace != null)
					regexContextMenuReplace.Dispose();
				//if (m_helpTopicProvider != null && (m_helpTopicProvider is IDisposable)) // No, since the client provides it.
				//	(m_helpTopicProvider as IDisposable).Dispose();
				if (m_messageFilterInstalled)
				{
					Application.RemoveMessageFilter(this);
					m_messageFilterInstalled = false;
				}
			}
			m_helpTopicProvider = null;
			m_searchKiller = null;
			m_prevSearchText = null;
			m_vwRootsite = null;
			m_vwFindPattern = null;
			m_cache = null;
			regexContextMenuReplace = null;
			regexContextMenuFind = null;
			base.Dispose(disposing);
		}
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Sets the initial values for the dialog controls, prior to displaying the dialog.
		/// This method should be called after creating, but prior to calling DoModeless. This
		/// overload is meant to be called from managed code.
		/// </summary>
		/// <param name="cache">The cache.</param>
		/// <param name="vwPattern">Find/replace values</param>
		/// <param name="rootSite">view</param>
		/// <param name="fReplace"><c>true</c> to initially display replace dialog page</param>
		/// <param name="fOverlays">ignored for now</param>
		/// <param name="owner">The main window that owns the rootsite</param>
		/// <param name="helpTopicProvider">help topic provider allows the dialog box class
		/// to specify the appropriate help topic path for this dialog</param>
		/// <param name="app">The application</param>
		/// <param name="wsEdit">writing system for the find and replace edit boxes</param>
		/// <returns>
		/// true if the dialog was initialized properly, otherwise false.
		/// False indicates some problem and the find/replace dialog should not be
		/// shown at this time.
		/// </returns>
		/// <remarks>ENHANCE JohnT: it may need more arguments, for example, the name of the
		/// kind of object we can restrict the search to, a list of fields.</remarks>
		/// ------------------------------------------------------------------------------------
		public bool SetDialogValues(FdoCache cache, IVwPattern vwPattern, IVwRootSite rootSite,
			bool fReplace, bool fOverlays, Form owner, IHelpTopicProvider helpTopicProvider,
			IApp app, int wsEdit)
		{
			CheckDisposed();

			fweditFindText.controlID = "Find";
			fweditReplaceText.controlID = "Replace";
			// save the pattern and put the text into the find edit box.
			if (vwPattern == null)
				throw new ArgumentNullException("vwPattern");
			if (cache == null)
				throw new ArgumentNullException("cache");
			m_vwFindPattern = vwPattern;
			m_cache = cache;
			m_helpTopicProvider = helpTopicProvider;
			m_app = app;

			SetOwner(rootSite, owner, vwPattern, wsEdit);
			bool readOnly = rootSite is SimpleRootSite && ((SimpleRootSite)rootSite).ReadOnlyView;
			if (readOnly)
			{
				if (tabControls.Controls.Contains(tabReplace))
					tabControls.Controls.Remove(tabReplace);
			}
			else
			{
				if (!tabControls.Controls.Contains(tabReplace))
					tabControls.Controls.Add(tabReplace);
			}

			ILgWritingSystemFactory wsf = m_cache.WritingSystemFactory;
			fweditFindText.WritingSystemFactory = fweditReplaceText.WritingSystemFactory = wsf;
			ITsStrFactory strFact = m_cache.TsStrFactory;
			IWritingSystem defVernWs = m_cache.ServiceLocator.WritingSystems.DefaultVernacularWritingSystem;
			FindText = strFact.MakeString(string.Empty, defVernWs.Handle);
			ReplaceText = strFact.MakeString(string.Empty, defVernWs.Handle);
			// Make sure each of the edit boxes has a reasonable writing system assigned.
			// (See LT-5130 for what can happen otherwise.)
			fweditFindText.WritingSystemCode = wsEdit;
			fweditReplaceText.WritingSystemCode = wsEdit;
			FindText = EnsureValidWs(wsEdit, vwPattern.Pattern);
			ReplaceText = EnsureValidWs(wsEdit, vwPattern.ReplaceWith);

			tabControls.SelectedTab = fReplace ? tabReplace : tabFind;
			tabControls_SelectedIndexChanged(null, new EventArgs());

			if (m_helpTopicProvider != null) // Will be null when running tests
			{
				helpProvider.HelpNamespace = FwDirectoryFinder.CodeDirectory +
					m_helpTopicProvider.GetHelpString("UserHelpFile");
			}

			SetCheckboxStates(vwPattern);

			if (regexContextMenuFind != null)
				regexContextMenuFind.Dispose();
			regexContextMenuFind = new RegexHelperMenu(fweditFindText, m_helpTopicProvider);
			if (regexContextMenuReplace != null)
				regexContextMenuReplace.Dispose();
			regexContextMenuReplace = new RegexHelperMenu(fweditReplaceText, m_helpTopicProvider, false);

			EnableRegexMenuReplaceButton();

			// get the current selection text (if available) to fill in the find pattern.
			IVwSelection sel = null;
			if (rootSite != null && rootSite.RootBox != null)
				sel = rootSite.RootBox.Selection;
			if (sel == null)
			{
				// Set the TSS of the edit box to an empty string if it isn't set.
				if (FindText == null)
				{
					FindText = m_cache.TsStrFactory.MakeString(
						string.Empty,
						m_cache.ServiceLocator.WritingSystems.DefaultVernacularWritingSystem.Handle);
				}
			}
			else
			{
				// Get the selected text as the initial contents of the find box. Make a new TS String without
				// any character style so the character style from the selection will not be used. Also, if the
				// selection ends with a paragraph end sequence (CR/LF) then remove it.
				ITsString tssSel;
				bool fGotItAll;
				sel.GetFirstParaString(out tssSel, " ", out fGotItAll);
				if (tssSel == null)
				{
					// Not able to get ITsString from selection (e.g. if it is a picture)...
					SetFormatLabels();
					return true;
				}
				ITsStrBldr bldr = tssSel.GetBldr();
				bldr.SetStrPropValue(0, bldr.Length, (int)FwTextPropType.ktptNamedStyle, null);
				// Unfortunately, the TsString returned by sel.GetFirstParaString() can have an
				// empty TsTextProps for at least the first run.  If that happens, we blow up
				// when we try to get the string later.
				for (int irun = 0; irun < bldr.RunCount; ++irun)
				{
					ITsTextProps ttp = bldr.get_Properties(irun);
					int var;
					if (ttp.GetIntPropValues((int)FwTextPropType.ktptWs, out var) <= 0)
					{
						TsRunInfo tri;
						bldr.FetchRunInfo(irun, out tri);
						bldr.SetIntPropValues(tri.ichMin, tri.ichLim,
							(int)FwTextPropType.ktptWs, 0, m_cache.DefaultAnalWs);
					}
				}
				RemoveEndOfPara(bldr);
				// We don't want to copy a multi-line selection into the find box.
				// Currently treating it as don't copy anything; another plausible option would be to copy the first line.
				var text = bldr.Text;
				if (text != null && (text.IndexOf('\r') >= 0 || text.IndexOf('\n') > 0))
				{
					bldr.Replace(0, bldr.Length, "", null);
				}
				// Set the TSS of the edit box if there is any text to set, or if there is no
				// TSS for the box, or if there is no text in the find box AND the selection is not a user prompt.
				// If the current selection is an IP AND we have a previous find text, we want to use that
				// instead of the current selection (TE-5127 and TE-5126).
				int nVar; //dummy for out params
				if (bldr.Length == 0 && vwPattern.Pattern != null)
				{
					FindText = vwPattern.Pattern;
				}
				else if ((bldr.Length != 0 || FindText == null || FindText.Length == 0)
					&& tssSel.get_Properties(0).GetIntPropValues(SimpleRootSite.ktptUserPrompt, out nVar) != 1)
				{
					FindText = bldr.GetString();
				}
				if (FindText != null)
				{
					// Set the replace text box properties to be the same as the find text box.
					// The best we can do is take the properties of the first run which should
					// be fine for most cases.
					ITsTextProps props = FindText.get_Properties(0);
					ITsStrBldr replaceBldr = TsStrBldrClass.Create();
					replaceBldr.Replace(0, 0, "", props);
					ReplaceText = replaceBldr.GetString();
				}
			}

			SetFormatLabels();
			return true;
		}