461 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VEPROMS.CSLA.Library;
using Volian.Base.Library;
using JR.Utils.GUI.Forms;
namespace Volian.Controls.Library
{
// C2023-016 - Upgrade - have Find/Replace spin through all of the Step Editor sections within the current procedure
public partial class FindReplace : DevComponents.DotNetBar.Office2007Form
{
private bool doingfind = false;
private bool findingbookmarks = false;
private bool _FoundIt = false;
private ItemInfo _StartingItemInfo = null;// C2023-016 used to remember where we started doing the Find/Replace
public bool FoundIt
{
get { return _FoundIt; }
set { _FoundIt = value; }
}
private bool IsFound
{
get
{
// B2022-067 get display text version to convert 8209 dash to normal (keyboard) dash
string selStr = ItemInfo.ConvertToDisplayText(MyEditItem.MyStepRTB.SelectedText);
if (!cbxCaseSensitive.Checked)
return selStr.ToUpper() == cmboFindText.Text.ToUpper();
else
return selStr == cmboFindText.Text;
}
}
private EditItem _MyEditItem;
public EditItem MyEditItem
{
get { return _MyEditItem; }
set
{
_MyEditItem = value;
if (_MyEditItem == null) return;
_MyDocVersion = _MyEditItem.MyItemInfo.MyProcedure.ActiveParent as DocVersionInfo;
SetupContextMenu();
}
}
private DocVersionInfo _MyDocVersion;
public DocVersionInfo Mydocversion
{
get { return _MyDocVersion; }
set {_MyDocVersion = value; }
}
private DisplayBookMarks _myDisplayBookMarks;
public DisplayBookMarks MyDisplayBookMarks
{
get { return _myDisplayBookMarks; }
set { _myDisplayBookMarks = value; }
}
private bool _InApproved;
public bool InApproved
{
get { return _InApproved; }
set { _InApproved = value; }
}
public FindReplace()
{
InitializeComponent();
LoadFindReplaceTextListBox();
cmbScope.SelectedIndex = 0;
btnBookMrkAll.Enabled = false;
btnFindNext.Enabled = false;
btnReplace.Enabled = false;
btnRplAll.Enabled = false;
cmboFindText.Focus();
}
private void LoadFindReplaceTextListBox()
{
// Setup cmboFindText Combo box
cmboFindText.Items.Clear();
if (Properties.Settings.Default["FindTextList"] != null && Properties.Settings.Default.FindTextList.Count > 0)
{
foreach (string str in Properties.Settings.Default.FindTextList)
cmboFindText.Items.Add(str);
}
// Setup cmboReplaceText Combo box
cmboReplaceText.Items.Clear();
if (Properties.Settings.Default["ReplaceTextList"] != null && Properties.Settings.Default.ReplaceTextList.Count > 0)
{
foreach (string str in Properties.Settings.Default.ReplaceTextList)
cmboReplaceText.Items.Add(str);
}
}
// bug fix: B2016-107, when Find/Replace is initially opened, set the cursor focus to the Find Text box (called from frmVEPROMS.cs)
public void SetFocusToTextBox()
{
cmboFindText.Focus();
}
private bool DoingClick = false; // Don't allow a button to be clicked until processing is complete for the last button
private void btnReplace_Click(object sender, EventArgs e)
{
if (DoingClick) return;// Don't allow a button to be clicked until processing is complete for the last button
DoingClick = true;// We started processing a click
DoReplace();
DoingClick = false;// we're done processing the click
}
private void DoReplace()
{
if (!IsFound)
DoFindNext();
else
MyEditItem.ReplaceText(cmboReplaceText.Text, cmboFindText.Text, cbxCaseSensitive.Checked, cbxWholeWord.Checked, cbxReverse.Checked, false, null);
}
private void btnRplAll_Click(object sender, EventArgs e)
{
if (DoingClick) return;// Don't allow a button to be clicked until processing is complete for the last button
DoingClick = true; // We started processing a click
DoRplAll();
DoingClick = false; // we're done processing the click
}
private void DoRplAll()
{
if (!IsFound)
DoFindNext();
else
{
DoReplace();
DoFindNext();
}
}
private void btnBookMrkAll_Click(object sender, EventArgs e)
{
if (DoingClick) return;// Don't allow a button to be clicked until processing is complete for the last button
DoingClick = true; // We started processing a click
DoBookMrkAll();
DoingClick = false; // we're done processing the click
}
private void DoBookMrkAll()
{
findingbookmarks = true;
if (!IsFound) DoFindNext();
FoundIt = true;
while (FoundIt && this.Visible) // found is set in btnFindNext_Click() - Don't continue if the form is invisible (Done button pressed).
{
MyDisplayBookMarks.AddBookMark(MyEditItem.MyItemInfo);
Application.DoEvents();
DoFindNext();
}
StepPanelTabDisplayEventArgs args = new StepPanelTabDisplayEventArgs("Bookmarks");
MyEditItem.MyStepPanel.OnTabDisplay(btnBookMrkAll, args);
findingbookmarks = false;
}
private void btnFndRplDone_Click(object sender, EventArgs e)
{
doingfind = false;
this.Visible = false;
}
private string FixSymbol(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if (c > 0xff || c == 0xA9 || c == 0xAE) // xA9 is Copyright, xAE is Registered
sb.Append(string.Format(@"\u{0}?", (int)c));
else
sb.Append(c);
}
return sb.ToString();
}
// B2015-131 Break up the find string on the spaces
// look for each word in the text string
// this is used only to see if the next item should be looked at, the real Find function (i.e MyEditItem.FindText())
// will do the actual evaluation
private bool ContainsFndStr(string text, string fndStr)
{
char [] sep ={' '}; // break string up on spaces
string [] fndStrAry = fndStr.Split(sep, StringSplitOptions.None);
int pos = 0;
foreach (string fStr in fndStrAry)
{
pos = text.IndexOf(fStr, pos);
if (pos == -1) break;
}
return pos != -1;
}
private void btnFindNext_Click(object sender, EventArgs e)
{
if (DoingClick) return;// Don't allow a button to be clicked until processing is complete for the last button
DoingClick = true;// We started processing a click
DoFindNext();
DoingClick = false;// we're done processing the click
}
// C2023-016 Find the next element, but stay in the same procedure
private ItemInfo FindNextStepElement(ItemInfo curStepElem, string fndStr)
{
ItemInfo nxtStepElem = curStepElem;
if (_StartingItemInfo == null)
_StartingItemInfo = MyEditItem.MyItemInfo;
if (cbxReverse.Checked)
{
if (curStepElem.IsProcedure)
nxtStepElem = curStepElem.MyProcedure.SearchBottom; // at the top of the procedure, so next step would be the last step of the last section (or the section title if the last section is a Word section)
else
nxtStepElem = curStepElem.SearchPrev;
}
else if (curStepElem.SearchNext.IsProcedure) // searching(finding) forward but next is the next procedure, so jump to the top of the procedure (the procedure title in the step editor)
nxtStepElem = curStepElem.MyProcedure;
else
nxtStepElem = curStepElem.SearchNext;
// B2015-131 Find was not finding series of words when one or more of the words were bolded or underlined (ex finding "Any SG" where Any was bolded)
while (nxtStepElem != null && nxtStepElem.ItemID != _StartingItemInfo.ItemID)
{
if (nxtStepElem.MyContent != null && ContainsFndStr(RtfTools.RTFConvertedSymbolsToUnicode(nxtStepElem.MyContent.Text).ToUpper(), fndStr.ToUpper()))
break;
if (cbxReverse.Checked)
{
if (nxtStepElem.IsProcedure)
nxtStepElem = nxtStepElem.MyProcedure.SearchBottom;// at the top of the procedure, so next step would be the last step of the last section (or the section title if the last section is a Word section)
else
nxtStepElem = nxtStepElem.SearchPrev;
}
else if (nxtStepElem.SearchNext.IsProcedure)
nxtStepElem = nxtStepElem.MyProcedure;// searching(finding) forward but next is the next procedure, so jump to the top of the procedure(the procedure title in the step editor)
else
nxtStepElem = nxtStepElem.SearchNext;
}
return nxtStepElem;
}
private void DoFindNext()
{
AddToComboLists();
doingfind = true;
string fndStr = FixSymbol(cmboFindText.Text.Replace(@"\", @"\u9586?").Replace("-", @"\u8209?").Replace("\xa0", @"\u160?"));
while (!MyEditItem.FindText(cmboFindText.Text, cbxCaseSensitive.Checked, cbxWholeWord.Checked, cbxReverse.Checked))
{
ItemInfo next = FindNextStepElement(MyEditItem.MyItemInfo, fndStr);
MyEditItem.MyStepPanel.MyStepTabPanel.MyDisplayTabControl.OpenItem(next,true,true);// C2023-016 open item (next), set focus (true), doing Find/Replace so don't open Word section (true)
if (cbxReverse.Checked)
MyEditItem.PositionToEnd();
if (next.IsTable && !cbxReverse.Checked) MyEditItem.PositionToStart();
//C2023-016 if we spun through all of the step editor sections, stop and display a message
if (MyEditItem.MyItemInfo.ItemID == _StartingItemInfo.ItemID)
{
FoundIt = false;
string msg = string.Format("Back to the starting point of the Find/Replace.{0}",(findingbookmarks)?"\n\n The Tools Panel will display showing the locations.":"");
FlexibleMessageBox.Show(this, msg, "Find/Replace Completed");
_StartingItemInfo = null;
return;
}
}
if (MyEditItem.MyStepRTB != null && !MyEditItem.MyStepRTB.Visible)
{
MyEditItem.MyStepRTB.Visible = true;
MyEditItem.MyStepRTB.Focus();
}
FoundIt = true;
}
private bool FindNextText(ItemInfo next)
{
if (next.MyContent.MyGrid!=null) return true;
return next.MyContent.Text.ToUpper().Contains(cmboFindText.Text.ToUpper());
}
private void AddToComboLists()
{
bool hastext = this.cmboFindText.Text.Length > 0;
if (hastext && !cmboFindText.Items.Contains(cmboFindText.Text))
{
if (cmboFindText.Items.Count >= 10)
cmboFindText.Items.RemoveAt(9);
cmboFindText.Items.Insert(0, cmboFindText.Text);
}
hastext = this.cmboReplaceText.Text.Length > 0;
if (hastext && btnReplace.Enabled && !cmboReplaceText.Items.Contains(cmboReplaceText.Text))
{
if (cmboReplaceText.Items.Count >= 10)
cmboReplaceText.Items.RemoveAt(9);
cmboReplaceText.Items.Insert(0, cmboReplaceText.Text);
}
}
private void cmboFindText_TextChanged(object sender, EventArgs e)
{
bool hastext = this.cmboFindText.Text.Length > 0;
btnFindNext.Enabled = hastext;
btnBookMrkAll.Enabled = hastext;
btnReplace.Enabled = btnRplAll.Enabled = hastext && !InApproved;
_StartingItemInfo = null; //C2023-016 reset the starting position
}
private void cmboReplaceText_TextChanged(object sender, EventArgs e)
{
btnReplace.Enabled = btnRplAll.Enabled = !InApproved && cmboFindText.Text.Length > 0;
_StartingItemInfo = null;//C2023-016 reset the starting position
}
public void PerformFindNext()
{
btnFindNext.PerformClick();
}
private void tabFind_Click(object sender, EventArgs e)
{
lblRplTxt.Visible = false;
cmboReplaceText.Visible = false;
btnReplace.Visible = false;
btnRplAll.Visible = false;
}
private void tabReplace_Click(object sender, EventArgs e)
{
lblRplTxt.Visible = true;
cmboReplaceText.Visible = true;
btnReplace.Visible = true;
btnRplAll.Visible = true;
}
private void FindReplace_FormClosing(object sender, FormClosingEventArgs e)
{
btnFndRplDone_Click(sender, e);
e.Cancel = true; // cancel the form close event - the Done_Click() will hide it instead
}
private void btnCmCut_Click(object sender, EventArgs e)
{
Clipboard.Clear();
DataObject myDO = new DataObject(DataFormats.Text, cmboReplaceText.Focused ? cmboReplaceText.SelectedText : cmboFindText.SelectedText);
Clipboard.SetDataObject(myDO);
// Need to check which combo box activated the context menu so that we know where to take/put selected text
if (cmboFindText.Focused)
cmboFindText.SelectedText = "";
else if (cmboReplaceText.Focused)
cmboReplaceText.SelectedText = "";
}
private void btnCmCopy_Click(object sender, EventArgs e)
{
// Need to check which combo box activated the context menu so that we know where to take/put selected text
DataObject myDO = new DataObject(DataFormats.Text, cmboReplaceText.Focused ? cmboReplaceText.SelectedText : cmboFindText.SelectedText);
if (cmboFindText.Focused || cmboReplaceText.Focused)
{
Clipboard.Clear();
Clipboard.SetDataObject(myDO);
}
}
private void btnCmPaste_Click(object sender, EventArgs e)
{
// Need to check which combo box activated the context menu so that we know where to take/put selected text
IDataObject myDO = Clipboard.GetDataObject();
if (myDO.GetDataPresent(DataFormats.Rtf))
{
RichTextBox rtb = new RichTextBox();
rtb.SelectedRtf = ItemInfo.StripLinks(myDO.GetData(DataFormats.Rtf).ToString());
if (cmboReplaceText.Focused)
cmboReplaceText.SelectedText = rtb.Text;
else if (cmboFindText.Focused)
cmboFindText.SelectedText = rtb.Text;
}
else if (myDO.GetDataPresent(DataFormats.Text))
if (cmboReplaceText.Focused)
cmboReplaceText.SelectedText = Clipboard.GetText();
else if (cmboFindText.Focused)
cmboFindText.SelectedText = Clipboard.GetText();
}
private void btnCmHardSp_Click(object sender, EventArgs e)
{
// We use \u00A0 to represent a hard space because it show in the search text field as a blank
// It will get translated to a real hard space character prior to searching
if (cmboReplaceText.Focused)
cmboReplaceText.SelectedText = "\u00A0";
else if (cmboFindText.Focused)
cmboFindText.SelectedText = "\u00A0";
}
private void btnSym_Click(object sender, EventArgs e)
{
DevComponents.DotNetBar.ButtonItem b = (DevComponents.DotNetBar.ButtonItem)sender;
if (cmboReplaceText.Focused)
cmboReplaceText.SelectedText = (string)b.Text;
else if (cmboFindText.Focused)
cmboFindText.SelectedText = (string)b.Text;
}
private void SetupContextMenu()
{
galSymbols.SubItems.Clear();
if (_MyDocVersion != null)
{
FormatData fmtdata = _MyDocVersion.ActiveFormat.PlantFormat.FormatData;
SymbolList sl = fmtdata.SymbolList;
if (sl == null || sl.Count <= 0)
{
FlexibleMessageBox.Show(this, "No symbols are available, check with administrator");
return;
}
foreach (Symbol sym in sl)
{
DevComponents.DotNetBar.ButtonItem btnCM = new DevComponents.DotNetBar.ButtonItem();
btnCM.Text = string.Format("{0}", (char)sym.Unicode);
// to name button use unicode rather than desc, desc may have spaces or odd chars
btnCM.Name = "btnCM" + sym.Unicode.ToString();
btnCM.Tooltip = sym.Desc;
btnCM.Tag = string.Format(@"{0}", sym.Unicode);
btnCM.FontBold = true;
btnCM.Click += new System.EventHandler(btnSym_Click);
galSymbols.SubItems.Add(btnCM);
}
}
}
private void FindReplace_Activated(object sender, EventArgs e)
{
if (!doingfind)
{
string find = MyEditItem.SelectedTextForFind;
if (find != null) cmboFindText.Text = find;
}
}
public void ToggleReplaceTab(E_ViewMode vm)
{
if (vm == E_ViewMode.View)
{
if (tabReplace.IsSelected)
tabFind.PerformClick();
tabReplace.Visible = false;
Text = "Find"; //C2021-021 change the dialog title
}
else
{
tabReplace.Visible = true;
Text = "Find and Replace";//C2021-021 change the dialog title
}
_StartingItemInfo = null;//C2023-016 reset the starting position
}
private void cbxCaseSensitive_CheckedChanged(object sender, EventArgs e)
{
_StartingItemInfo = null;//C2023-016 reset the starting position
}
private void cbxWholeWord_CheckedChanged(object sender, EventArgs e)
{
_StartingItemInfo = null;//C2023-016 reset the starting position
}
private void cbxReverse_CheckedChanged(object sender, EventArgs e)
{
_StartingItemInfo = null;//C2023-016 reset the starting position
}
}
}