2008-09-01 12:14:35 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Text;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using Microsoft.Win32;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using CodeImp.DoomBuilder.Actions;
|
|
|
|
using CodeImp.DoomBuilder.Data;
|
|
|
|
using CodeImp.DoomBuilder.Config;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using CodeImp.DoomBuilder.Controls;
|
|
|
|
using CodeImp.DoomBuilder.Windows;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes
|
|
|
|
{
|
|
|
|
public partial class FindReplaceForm : DelayedForm
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
private FindReplaceType newfinder;
|
2008-09-01 12:14:35 +00:00
|
|
|
private FindReplaceType finder;
|
2009-01-25 23:30:19 +00:00
|
|
|
bool controlpressed = false;
|
|
|
|
bool shiftpressed = false;
|
|
|
|
bool suppressevents = false;
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
internal FindReplaceType Finder { get { return finder; } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-01-15 21:25:29 +00:00
|
|
|
#region ================== Constructor
|
2008-09-01 12:14:35 +00:00
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public FindReplaceForm()
|
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
// Find all find/replace types
|
2008-10-23 11:36:00 +00:00
|
|
|
Type[] findtypes = BuilderPlug.Me.FindClasses(typeof(FindReplaceType));
|
2008-09-01 12:14:35 +00:00
|
|
|
foreach(Type t in findtypes)
|
|
|
|
{
|
|
|
|
FindReplaceType finderinst;
|
|
|
|
object[] attr = t.GetCustomAttributes(typeof(FindReplaceAttribute), true);
|
|
|
|
if(attr.Length > 0)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Create instance
|
|
|
|
finderinst = (FindReplaceType)Assembly.GetExecutingAssembly().CreateInstance(t.FullName, false, BindingFlags.Default, null, null, CultureInfo.CurrentCulture, new object[0]);
|
|
|
|
}
|
|
|
|
catch(TargetInvocationException ex)
|
|
|
|
{
|
|
|
|
// Error!
|
|
|
|
General.WriteLogLine("ERROR: Failed to create class instance '" + t.Name + "'!");
|
|
|
|
General.WriteLogLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
// Error!
|
|
|
|
General.WriteLogLine("ERROR: Failed to create class instance '" + t.Name + "'!");
|
|
|
|
General.WriteLogLine(ex.GetType().Name + ": " + ex.Message);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the finder to the list
|
|
|
|
searchtypes.Items.Add(finderinst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select first
|
|
|
|
searchtypes.SelectedIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Events
|
|
|
|
|
|
|
|
// Replace (un)checked
|
|
|
|
private void doreplace_CheckedChanged(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if(doreplace.Checked)
|
|
|
|
{
|
|
|
|
findbutton.Text = "Replace";
|
|
|
|
groupreplace.Enabled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
findbutton.Text = "Find";
|
|
|
|
groupreplace.Enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search type selected
|
|
|
|
private void searchtypes_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
// Create instance of the selected type
|
2009-01-25 23:30:19 +00:00
|
|
|
newfinder = (FindReplaceType)searchtypes.SelectedItem;
|
2008-09-01 12:14:35 +00:00
|
|
|
|
|
|
|
// Now setup the interface
|
2009-01-25 23:30:19 +00:00
|
|
|
browsefind.Enabled = newfinder.Attributes.BrowseButton;
|
|
|
|
browsereplace.Enabled = newfinder.Attributes.BrowseButton;
|
2008-09-01 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Browse find button clicked
|
|
|
|
private void browsefind_Click(object sender, EventArgs e)
|
|
|
|
{
|
2009-01-25 23:30:19 +00:00
|
|
|
findinput.Text = newfinder.Browse(findinput.Text);
|
2008-09-01 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Browse replacement clicked
|
|
|
|
private void browsereplace_Click(object sender, EventArgs e)
|
|
|
|
{
|
2009-01-25 23:30:19 +00:00
|
|
|
replaceinput.Text = newfinder.Browse(replaceinput.Text);
|
2008-09-01 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find / Replace clicked
|
|
|
|
private void findbutton_Click(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
// Reset results
|
|
|
|
resultslist.Items.Clear();
|
2009-01-25 23:30:19 +00:00
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// Hide object information
|
|
|
|
General.Interface.HideInfo();
|
2009-01-26 14:09:11 +00:00
|
|
|
General.Map.Map.ClearAllSelected();
|
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
// Keep the finder we used for the search
|
|
|
|
finder = newfinder;
|
2009-01-26 14:09:11 +00:00
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
// Enable/disable buttons
|
|
|
|
editbutton.Enabled = false;
|
|
|
|
deletebutton.Visible = finder.AllowDelete;
|
|
|
|
deletebutton.Enabled = false;
|
2009-01-26 14:09:11 +00:00
|
|
|
suppressevents = true;
|
|
|
|
resultslist.BeginUpdate();
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// Perform the search / replace and show the results
|
|
|
|
if(doreplace.Checked)
|
|
|
|
{
|
|
|
|
resultslist.Items.AddRange(finder.Find(findinput.Text, withinselection.Checked, replaceinput.Text, keepselection.Checked));
|
|
|
|
resultscount.Text = resultslist.Items.Count + " items found and replaced.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resultslist.Items.AddRange(finder.Find(findinput.Text, withinselection.Checked, null, false));
|
|
|
|
resultscount.Text = resultslist.Items.Count + " items found.";
|
|
|
|
}
|
|
|
|
|
2009-01-26 14:09:11 +00:00
|
|
|
// Select all results
|
|
|
|
for(int i = 0; i < resultslist.Items.Count; i++)
|
|
|
|
resultslist.SelectedIndices.Add(i);
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// Open results part of window
|
|
|
|
this.Size = new Size(this.Width, this.Height - this.ClientSize.Height + resultspanel.Top + resultspanel.Height);
|
|
|
|
resultspanel.Visible = true;
|
2009-01-26 14:09:11 +00:00
|
|
|
resultslist.EndUpdate();
|
|
|
|
suppressevents = false;
|
2008-09-01 12:14:35 +00:00
|
|
|
|
|
|
|
// Redraw the screen, this will show the selection
|
|
|
|
General.Interface.RedrawDisplay();
|
|
|
|
}
|
2009-01-26 14:09:11 +00:00
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// Found item selected
|
|
|
|
private void resultslist_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
{
|
2009-01-25 23:30:19 +00:00
|
|
|
if(!suppressevents)
|
2008-09-01 12:14:35 +00:00
|
|
|
{
|
2009-01-26 14:09:11 +00:00
|
|
|
// Let the finder know about the selection
|
|
|
|
FindReplaceObject[] selection = GetSelection();
|
|
|
|
finder.ObjectSelected(selection);
|
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
// Enable/disable buttons
|
|
|
|
editbutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
deletebutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// Redraw the screen, this will show the selection
|
|
|
|
General.Interface.RedrawDisplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
// Mouse released
|
|
|
|
private void resultslist_MouseUp(object sender, MouseEventArgs e)
|
|
|
|
{
|
|
|
|
// Right-clicked?
|
|
|
|
if(e.Button == MouseButtons.Right)
|
|
|
|
{
|
|
|
|
int index = resultslist.IndexFromPoint(e.Location);
|
|
|
|
if(index != ListBox.NoMatches)
|
|
|
|
{
|
|
|
|
// Select the right-clicked item, if not selected
|
|
|
|
if(!resultslist.SelectedIndices.Contains(index))
|
|
|
|
{
|
|
|
|
if(!controlpressed && !shiftpressed) resultslist.SelectedItems.Clear();
|
|
|
|
resultslist.SelectedIndices.Add(index);
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit selected objects
|
|
|
|
editbutton_Click(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable/disable buttons
|
|
|
|
editbutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
deletebutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
}
|
|
|
|
|
2008-09-01 12:41:45 +00:00
|
|
|
// Window closing
|
|
|
|
private void FindReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
{
|
|
|
|
// If the user closes the form, then just cancel the mode
|
|
|
|
if(e.CloseReason == CloseReason.UserClosing)
|
|
|
|
{
|
|
|
|
e.Cancel = true;
|
2008-11-27 11:59:17 +00:00
|
|
|
General.Editing.CancelMode();
|
2008-09-01 12:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close button clicked
|
|
|
|
private void closebutton_Click(object sender, EventArgs e)
|
|
|
|
{
|
2009-01-17 08:28:20 +00:00
|
|
|
General.Interface.Focus();
|
2008-11-27 11:59:17 +00:00
|
|
|
General.Editing.CancelMode();
|
2008-09-01 12:41:45 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 23:30:19 +00:00
|
|
|
// Key pressed
|
|
|
|
private void FindReplaceForm_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
// Keep modifiers
|
|
|
|
controlpressed = e.Control;
|
|
|
|
shiftpressed = e.Shift;
|
|
|
|
|
|
|
|
// Enable/disable buttons
|
|
|
|
editbutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
deletebutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key released
|
|
|
|
private void FindReplaceForm_KeyUp(object sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
// Keep modifiers
|
|
|
|
controlpressed = e.Control;
|
|
|
|
shiftpressed = e.Shift;
|
|
|
|
|
|
|
|
// Enable/disable buttons
|
|
|
|
editbutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
deletebutton.Enabled = (resultslist.SelectedIndex > -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit Selection
|
|
|
|
private void editbutton_Click(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
FindReplaceObject[] items = GetSelection();
|
|
|
|
if(items.Length > 0)
|
|
|
|
{
|
|
|
|
// Edit selected objects
|
|
|
|
suppressevents = true;
|
|
|
|
finder.EditObjects(items);
|
|
|
|
suppressevents = false;
|
|
|
|
resultslist_SelectedIndexChanged(sender, e);
|
|
|
|
|
|
|
|
// Redraw the screen, this will show the selection
|
|
|
|
General.Interface.RedrawDisplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete Selection
|
|
|
|
private void deletebutton_Click(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
// Delete selected objects
|
|
|
|
FindReplaceObject[] items = GetSelection();
|
|
|
|
if(items.Length > 0)
|
|
|
|
{
|
|
|
|
suppressevents = true;
|
|
|
|
finder.DeleteObjects(items);
|
|
|
|
foreach(FindReplaceObject obj in items)
|
|
|
|
{
|
|
|
|
resultslist.Items.Remove(obj);
|
|
|
|
}
|
|
|
|
suppressevents = false;
|
|
|
|
resultslist_SelectedIndexChanged(sender, e);
|
|
|
|
|
|
|
|
// Redraw the screen, this will show the selection
|
|
|
|
General.Interface.RedrawDisplay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
2009-01-15 21:25:29 +00:00
|
|
|
// This shows the window
|
|
|
|
public void Show(Form owner)
|
|
|
|
{
|
|
|
|
// First time showing?
|
|
|
|
//if((this.Location.X == 0) && (this.Location.Y == 0))
|
|
|
|
{
|
|
|
|
// Position at left-top of owner
|
|
|
|
this.Location = new Point(owner.Location.X + 20, owner.Location.Y + 90);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close results part
|
|
|
|
resultspanel.Visible = false;
|
|
|
|
this.Size = new Size(this.Width, this.Height - this.ClientSize.Height + resultspanel.Top);
|
|
|
|
|
|
|
|
// Show window
|
|
|
|
base.Show(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This hides the window
|
|
|
|
new public void Hide()
|
|
|
|
{
|
|
|
|
base.Hide();
|
|
|
|
|
|
|
|
// Clear search results
|
|
|
|
resultslist.Items.Clear();
|
|
|
|
}
|
|
|
|
|
2008-09-01 12:14:35 +00:00
|
|
|
// This returns the selected item(s)
|
|
|
|
internal FindReplaceObject[] GetSelection()
|
|
|
|
{
|
2009-01-26 14:09:11 +00:00
|
|
|
// Return selected objects
|
|
|
|
FindReplaceObject[] list = new FindReplaceObject[resultslist.SelectedItems.Count];
|
|
|
|
int index = 0;
|
|
|
|
foreach(object obj in resultslist.SelectedItems)
|
2008-09-01 12:14:35 +00:00
|
|
|
{
|
2009-01-26 14:09:11 +00:00
|
|
|
list[index++] = (FindReplaceObject)obj;
|
2008-09-01 12:14:35 +00:00
|
|
|
}
|
2009-01-26 14:09:11 +00:00
|
|
|
return list;
|
2008-09-01 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|