@ working on find & replace in script editor

This commit is contained in:
codeimp 2009-03-04 21:40:58 +00:00
parent 8593fd62dd
commit c4b71597b4
6 changed files with 111 additions and 19 deletions

View file

@ -96,6 +96,7 @@ namespace CodeImp.DoomBuilder.Controls
editor.OnExplicitSaveTab += panel.ExplicitSaveCurrentTab;
editor.OnOpenScriptBrowser += panel.OpenBrowseScript;
editor.OnOpenFindAndReplace += panel.OpenFindAndReplace;
editor.OnFindNext += panel.FindNext;
}
// Disposer
@ -105,6 +106,7 @@ namespace CodeImp.DoomBuilder.Controls
editor.OnExplicitSaveTab -= panel.ExplicitSaveCurrentTab;
editor.OnOpenScriptBrowser -= panel.OpenBrowseScript;
editor.OnOpenFindAndReplace -= panel.OpenFindAndReplace;
editor.OnFindNext -= panel.FindNext;
base.Dispose(disposing);
}
@ -221,6 +223,7 @@ namespace CodeImp.DoomBuilder.Controls
string text = Encoding.GetEncoding(config.CodePage).GetString(data);
StringComparison mode = options.CaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
int startpos = Math.Max(editor.SelectionStart, editor.SelectionEnd);
bool wrapped = false;
while(true)
{
@ -230,7 +233,13 @@ namespace CodeImp.DoomBuilder.Controls
// Check to see if it is the whole word
if(options.WholeWord)
{
// Veryfy that we have found a whole word
string foundword = editor.GetWordAt(result + 1);
if(foundword.Length != options.FindText.Length)
{
startpos = result + 1;
result = -1;
}
}
// Still ok?
@ -246,9 +255,10 @@ namespace CodeImp.DoomBuilder.Controls
else
{
// If we haven't tried from the start, try from the start now
if(startpos > 0)
if((startpos > 0) && !wrapped)
{
startpos = 0;
wrapped = true;
}
else
{

View file

@ -60,10 +60,12 @@ namespace CodeImp.DoomBuilder.Controls
public delegate void ExplicitSaveTabDelegate();
public delegate void OpenScriptBrowserDelegate();
public delegate void OpenFindReplaceDelegate();
public delegate void FindNextDelegate();
public event ExplicitSaveTabDelegate OnExplicitSaveTab;
public event OpenScriptBrowserDelegate OnOpenScriptBrowser;
public event OpenFindReplaceDelegate OnOpenFindAndReplace;
public event FindNextDelegate OnFindNext;
#endregion
@ -351,15 +353,22 @@ namespace CodeImp.DoomBuilder.Controls
// This returns the current word (where the caret is at)
public string GetCurrentWord()
{
int wordstart = scriptedit.WordStartPosition(scriptedit.CurrentPos, true);
int wordend = scriptedit.WordEndPosition(scriptedit.CurrentPos, true);
return GetWordAt(scriptedit.CurrentPos);
}
// This returns the word at the given position
public string GetWordAt(int position)
{
int wordstart = scriptedit.WordStartPosition(position, true);
int wordend = scriptedit.WordEndPosition(position, true);
// Decode the text
byte[] scripttextdata = scriptedit.GetText(scriptedit.TextSize);
Encoding encoder = Encoding.GetEncoding(scriptedit.CodePage);
string scripttext = encoder.GetString(scripttextdata);
if(wordstart > wordend)
if(wordstart < wordend)
return scripttext.Substring(wordstart, wordend - wordstart);
else
return "";
@ -625,6 +634,13 @@ namespace CodeImp.DoomBuilder.Controls
if((e.KeyCode == Keys.N) && ((e.Modifiers & Keys.Control) == Keys.Control)) e.Handled = true;
if((e.KeyCode == Keys.M) && ((e.Modifiers & Keys.Control) == Keys.Control)) e.Handled = true;
// F3 for Find Next
if((e.KeyCode == Keys.F3) && (e.Modifiers == Keys.None))
{
if(OnFindNext != null) OnFindNext();
e.Handled = true;
}
// CTRL+F for find & replace
if((e.KeyCode == Keys.F) && ((e.Modifiers & Keys.Control) == Keys.Control))
{

View file

@ -49,8 +49,9 @@ namespace CodeImp.DoomBuilder.Controls
private List<ScriptConfiguration> scriptconfigs;
private List<CompilerError> compilererrors;
// Windows
// Find/Replace
private ScriptFindReplaceForm findreplaceform;
private FindReplaceOptions findoptions;
#endregion
@ -169,7 +170,41 @@ namespace CodeImp.DoomBuilder.Controls
#region ================== Methods
// This opens the Find & Replace sub window
// Find Next
public void FindNext(FindReplaceOptions options)
{
// Save the options
findoptions = options;
FindNext();
}
// Find Next with saved options
public void FindNext()
{
if(!string.IsNullOrEmpty(findoptions.FindText))
{
if(!ActiveTab.FindNext(findoptions))
{
General.MainWindow.DisplayStatus(StatusType.Warning, "Can't find any occurence of \"" + findoptions.FindText + "\".");
}
}
else
{
General.MessageBeep(MessageBeepType.Default);
}
}
// This closed the Find & Replace subwindow
public void CloseFindReplace(bool closing)
{
if(findreplaceform != null)
{
if(!closing) findreplaceform.Close();
findreplaceform = null;
}
}
// This opens the Find & Replace subwindow
public void OpenFindAndReplace()
{
if(findreplaceform == null)

View file

@ -131,6 +131,7 @@ namespace CodeImp.DoomBuilder.Windows
this.closebutton.TabIndex = 9;
this.closebutton.Text = "Close";
this.closebutton.UseVisualStyleBackColor = true;
this.closebutton.Click += new System.EventHandler(this.closebutton_Click);
//
// replacebutton
//
@ -163,6 +164,7 @@ namespace CodeImp.DoomBuilder.Windows
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "ScriptFindReplaceForm";
this.Text = "Find and Replace";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ScriptFindReplaceForm_FormClosing);
this.ResumeLayout(false);
this.PerformLayout();

View file

@ -35,6 +35,8 @@ namespace CodeImp.DoomBuilder.Windows
{
#region ================== Variables
private bool appclose;
#endregion
#region ================== Properties
@ -51,24 +53,51 @@ namespace CodeImp.DoomBuilder.Windows
#endregion
#region ================== Events
#region ================== Methods
// Find Next
private void findnextbutton_Click(object sender, EventArgs e)
// This makes the Find & Replace options
private FindReplaceOptions MakeOptions()
{
FindReplaceOptions options = new FindReplaceOptions();
options.FindText = findtext.Text;
options.CaseSensitive = casesensitive.Checked;
options.WholeWord = wordonly.Checked;
options.ReplaceWith = replacetext.Text;
return options;
}
if(!General.Map.ScriptEditor.Editor.ActiveTab.FindNext(options))
// Close the window
new public void Close()
{
appclose = true;
base.Close();
}
#endregion
#region ================== Events
// Form is closing
private void ScriptFindReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(!appclose)
{
// No such thing
General.MainWindow.DisplayStatus(StatusType.Warning, "Can't find any occurence of \"" + findtext.Text + "\".");
General.Map.ScriptEditor.Editor.CloseFindReplace(true);
}
}
// Find Next
private void findnextbutton_Click(object sender, EventArgs e)
{
General.Map.ScriptEditor.Editor.FindNext(MakeOptions());
}
// Close
private void closebutton_Click(object sender, EventArgs e)
{
General.Map.ScriptEditor.Editor.CloseFindReplace(false);
}
#endregion
}
}