#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Media;
using System.Windows.Forms;
using MiniSqlQuery.Core;
using MiniSqlQuery.PlugIns.SearchTools.Commands;
namespace MiniSqlQuery.PlugIns.SearchTools
{
/// The find replace form.
public partial class FindReplaceForm : Form, IFindReplaceWindow
{
/// The _services.
private readonly IApplicationServices _services;
/// Initializes a new instance of the class.
/// The services.
public FindReplaceForm(IApplicationServices services)
{
InitializeComponent();
StartPosition = FormStartPosition.CenterParent;
_services = services;
}
/// Gets or sets FindString.
public string FindString
{
get { return txtFindString.Text; }
set { txtFindString.Text = value; }
}
/// Gets or sets ReplaceString.
public string ReplaceString
{
get { return txtReplaceText.Text; }
set { txtReplaceText.Text = value; }
}
/// The create find request.
/// The provider.
/// The find string.
/// The replace value.
private void CreateFindRequest(IFindReplaceProvider provider, string findString, string replaceValue)
{
int key = provider.GetHashCode();
FindTextRequest request;
if (SearchToolsCommon.FindReplaceTextRequests.ContainsKey(key))
{
request = SearchToolsCommon.FindReplaceTextRequests[key];
if (request.SearchValue != findString)
{
// reset find text and set the starting position to the current cursor location
request.SearchValue = findString;
request.ReplaceValue = replaceValue;
request.Position = provider.CursorOffset;
}
}
else
{
request = new FindTextRequest(provider, findString);
request.ReplaceValue = replaceValue;
}
SearchToolsCommon.FindReplaceTextRequests[key] = request;
}
/// The dim form.
private void DimForm()
{
Opacity = 0.8;
}
/// The find replace form_ activated.
/// The sender.
/// The e.
private void FindReplaceForm_Activated(object sender, EventArgs e)
{
if (txtFindString.Focused | txtReplaceText.Focused)
{
UnDimForm();
}
else
{
DimForm();
}
}
/// The find replace form_ deactivate.
/// The sender.
/// The e.
private void FindReplaceForm_Deactivate(object sender, EventArgs e)
{
DimForm();
}
/// The find replace form_ form closing.
/// The sender.
/// The e.
private void FindReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
/// The find replace form_ key up.
/// The sender.
/// The e.
private void FindReplaceForm_KeyUp(object sender, KeyEventArgs e)
{
// simulate close
if (e.KeyCode == Keys.Escape)
{
e.Handled = true;
Hide();
}
}
/// The handle find next.
/// The provider.
/// The find string.
/// The replace value.
private void HandleFindNext(IFindReplaceProvider provider, string findString, string replaceValue)
{
CreateFindRequest(provider, findString, replaceValue);
CommandManager.GetCommandInstance().Execute();
}
/// The handle replace next.
/// The provider.
/// The find string.
/// The replace value.
private void HandleReplaceNext(IFindReplaceProvider provider, string findString, string replaceValue)
{
CommandManager.GetCommandInstance().Execute();
}
/// The un dim form.
private void UnDimForm()
{
Opacity = 1.0;
}
/// The btn cancel_ click.
/// The sender.
/// The e.
private void btnCancel_Click(object sender, EventArgs e)
{
Hide();
}
/// The btn find next_ click.
/// The sender.
/// The e.
private void btnFindNext_Click(object sender, EventArgs e)
{
IFindReplaceProvider provider = _services.HostWindow.ActiveChildForm as IFindReplaceProvider;
if (provider == null)
{
SystemSounds.Beep.Play();
}
else
{
HandleFindNext(provider, FindString, ReplaceString);
}
}
/// The btn replace_ click.
/// The sender.
/// The e.
private void btnReplace_Click(object sender, EventArgs e)
{
IFindReplaceProvider provider = _services.HostWindow.ActiveChildForm as IFindReplaceProvider;
if (provider == null)
{
SystemSounds.Beep.Play();
}
else
{
HandleReplaceNext(provider, FindString, ReplaceString);
}
}
/// The txt find string_ enter.
/// The sender.
/// The e.
private void txtFindString_Enter(object sender, EventArgs e)
{
UnDimForm();
}
/// The txt find string_ leave.
/// The sender.
/// The e.
private void txtFindString_Leave(object sender, EventArgs e)
{
DimForm();
}
}
}