📄 searchpad.cs
字号:
/* ***********************************************************
*
* Help 2.0 Environment for SharpDevelop
* Search Pad
* Copyright (c) 2005, Mathias Simmack. All rights reserved.
*
* ********************************************************* */
namespace HtmlHelp2
{
using System;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.Core.Services;
using HtmlHelp2Service;
using MSHelpServices;
public class ShowSearchMenuCommand : AbstractMenuCommand
{
public override void Run()
{
HtmlHelp2SearchPad search = (HtmlHelp2SearchPad)WorkbenchSingleton.Workbench.GetPad(typeof(HtmlHelp2SearchPad));
if(search != null) search.BringPadToFront();
}
}
public class HtmlHelp2SearchPad : AbstractPadContent
{
HtmlHelp2Environment h2env = null;
Panel mainPanel = new Panel();
Button searchButton = new Button();
ComboBox filterCombobox = new ComboBox();
ComboBox searchTerm = new ComboBox();
CheckBox titlesOnly = new CheckBox();
CheckBox enableStemming = new CheckBox();
CheckBox reuseMatches = new CheckBox();
CheckBox hiliteTopics = new CheckBox();
Label label1 = new Label();
Label label2 = new Label();
string selectedQuery = "";
bool searchIsBusy = false;
public override Control Control
{
get { return mainPanel; }
}
public override void RedrawContent()
{
StringParserService sps = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
searchButton.Text = sps.Parse("${res:AddIns.HtmlHelp2.Search}");
titlesOnly.Text = sps.Parse("${res:AddIns.HtmlHelp2.SearchInTitlesOnly}");
enableStemming.Text = sps.Parse("${res:AddIns.HtmlHelp2.LookForSimilarWords}");
reuseMatches.Text = sps.Parse("${res:AddIns.HtmlHelp2.SearchInPreviouslyFoundTopics}");
hiliteTopics.Text = sps.Parse("${res:AddIns.HtmlHelp2.HighlightMatches}");
label1.Text = sps.Parse("${res:AddIns.HtmlHelp2.FilteredBy}");
label2.Text = sps.Parse("${res:AddIns.HtmlHelp2.LookFor}");
}
public bool HiliteEnabled
{
get { return hiliteTopics.Checked; }
}
public HtmlHelp2SearchPad() : base("${res:AddIns.HtmlHelp2.Search}", "HtmlHelp2.16x16.Search")
{
this.InitializeComponents();
}
private void InitializeComponents()
{
StringParserService sps = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
h2env = (HtmlHelp2Environment)ServiceManager.Services.GetService(typeof(HtmlHelp2Environment));
bool Help2EnvIsReady = (h2env != null && h2env.Help2EnvironmentIsReady);
// Search controls
Panel panel3 = new Panel();
mainPanel.Controls.Add(panel3);
panel3.Width = 500;
int pw = panel3.Width;
panel3.Controls.Add(searchButton);
searchButton.Enabled = false;
searchButton.Text = sps.Parse("${res:AddIns.HtmlHelp2.Search}");
searchButton.Click += new EventHandler(this.SearchButtonClick);
panel3.Controls.Add(titlesOnly);
panel3.Controls.Add(enableStemming);
panel3.Controls.Add(reuseMatches);
panel3.Controls.Add(hiliteTopics);
titlesOnly.Width = pw;
titlesOnly.Text = sps.Parse("${res:AddIns.HtmlHelp2.SearchInTitlesOnly}");
titlesOnly.Top = searchButton.Top + searchButton.Height + 10;
titlesOnly.TextAlign = ContentAlignment.MiddleLeft;
titlesOnly.Enabled = Help2EnvIsReady;
enableStemming.Width = pw;
enableStemming.Text = sps.Parse("${res:AddIns.HtmlHelp2.LookForSimilarWords}");
enableStemming.Top = titlesOnly.Top + titlesOnly.Height - 4;
enableStemming.TextAlign = ContentAlignment.MiddleLeft;
enableStemming.Enabled = Help2EnvIsReady;
reuseMatches.Width = pw;
reuseMatches.Top = enableStemming.Top + enableStemming.Height - 4;
reuseMatches.Text = sps.Parse("${res:AddIns.HtmlHelp2.SearchInPreviouslyFoundTopics}");
reuseMatches.Enabled = false;
reuseMatches.TextAlign = ContentAlignment.MiddleLeft;
hiliteTopics.Width = pw;
hiliteTopics.Top = reuseMatches.Top + reuseMatches.Height - 4;
hiliteTopics.Text = sps.Parse("${res:AddIns.HtmlHelp2.HighlightMatches}");
hiliteTopics.TextAlign = ContentAlignment.MiddleLeft;
hiliteTopics.Enabled = Help2EnvIsReady;
panel3.Dock = DockStyle.Fill;
// Filter Combobox
Panel panel1 = new Panel();
mainPanel.Controls.Add(panel1);
panel1.Dock = DockStyle.Top;
panel1.Height = filterCombobox.Height + 15;
panel1.Controls.Add(filterCombobox);
filterCombobox.Dock = DockStyle.Top;
filterCombobox.DropDownStyle = ComboBoxStyle.DropDownList;
filterCombobox.Sorted = true;
filterCombobox.Enabled = Help2EnvIsReady;
filterCombobox.SelectedIndexChanged += new EventHandler(this.FilterChanged);
if(Help2EnvIsReady)
{
h2env.BuildFilterList(filterCombobox);
h2env.FilterQueryChanged += new EventHandler(this.FilterQueryChanged);
h2env.NamespaceReloaded += new EventHandler(this.NamespaceReloaded);
}
// Filter label
mainPanel.Controls.Add(label1);
label1.Text = sps.Parse("${res:AddIns.HtmlHelp2.FilteredBy}");
label1.Dock = DockStyle.Top;
label1.TextAlign = ContentAlignment.MiddleLeft;
label1.Enabled = Help2EnvIsReady;
// SearchTerm Combobox
Panel panel2 = new Panel();
mainPanel.Controls.Add(panel2);
panel2.Dock = DockStyle.Top;
panel2.Height = searchTerm.Height + 7;
panel2.Controls.Add(searchTerm);
searchTerm.Dock = DockStyle.Top;
searchTerm.TextChanged += new EventHandler(this.SearchTextChanged);
searchTerm.KeyPress += new KeyPressEventHandler(this.SearchTextKeyPress);
searchTerm.Enabled = Help2EnvIsReady;
mainPanel.Controls.Add(label2);
label2.Text = sps.Parse("${res:AddIns.HtmlHelp2.LookFor}");
label2.Dock = DockStyle.Top;
label2.TextAlign = ContentAlignment.MiddleLeft;
label2.Enabled = Help2EnvIsReady;
}
private void FilterChanged(object sender, EventArgs e)
{
object selectedItem = filterCombobox.SelectedItem;
if(selectedItem != null)
{
selectedQuery = h2env.FindFilterQuery(selectedItem.ToString());
}
}
#region Help 2.0 Environment Events
private void FilterQueryChanged(object sender, EventArgs e)
{
Application.DoEvents();
string currentFilterName = filterCombobox.SelectedItem.ToString();
if(String.Compare(currentFilterName, h2env.CurrentFilterName) != 0)
{
filterCombobox.SelectedIndexChanged -= new EventHandler(FilterChanged);
filterCombobox.SelectedIndex = filterCombobox.Items.IndexOf(h2env.CurrentFilterName);
selectedQuery = h2env.CurrentFilterQuery;
filterCombobox.SelectedIndexChanged += new EventHandler(FilterChanged);
}
}
private void NamespaceReloaded(object sender, EventArgs e)
{
searchTerm.Text = "";
searchTerm.Items.Clear();
filterCombobox.SelectedIndexChanged -= new EventHandler(FilterChanged);
h2env.BuildFilterList(filterCombobox);
filterCombobox.SelectedIndexChanged += new EventHandler(FilterChanged);
}
#endregion
private void SearchButtonClick(object sender, EventArgs e)
{
if(searchTerm.Text != "")
{
this.AddTermToList(searchTerm.Text);
this.PerformFTS(searchTerm.Text);
}
}
private void SearchTextChanged(object sender, EventArgs e)
{
searchButton.Enabled = (searchTerm.Text != "");
}
private void SearchTextKeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == (char)13 && searchTerm.Text != null)
{
this.AddTermToList(searchTerm.Text);
this.PerformFTS(searchTerm.Text);
}
}
private void AddTermToList(string searchText)
{
if(searchTerm.Items.IndexOf(searchText) == -1)
{
searchTerm.Items.Insert(0, searchText);
if(searchTerm.Items.Count > 10) searchTerm.Items.RemoveAt(10);
searchTerm.SelectedIndex = 0;
}
}
#region FTS
private void PerformFTS(string searchWord)
{
StringParserService sps = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
if(h2env == null || !h2env.Help2EnvironmentIsReady || searchIsBusy)
return;
HtmlHelp2SearchResultsPad searchResults = (HtmlHelp2SearchResultsPad)WorkbenchSingleton.Workbench.GetPad(typeof(HtmlHelp2SearchResultsPad));
if(searchResults == null)
return;
HtmlHelp2Dialog searchDialog = new HtmlHelp2Dialog();
try
{
searchIsBusy = true;
HxQuery_Options searchFlags = HxQuery_Options.HxQuery_No_Option;
searchFlags |= (titlesOnly.Checked)?HxQuery_Options.HxQuery_FullTextSearch_Title_Only:HxQuery_Options.HxQuery_No_Option;
searchFlags |= (enableStemming.Checked)?HxQuery_Options.HxQuery_FullTextSearch_Enable_Stemming:HxQuery_Options.HxQuery_No_Option;
searchFlags |= (reuseMatches.Checked)?HxQuery_Options.HxQuery_FullTextSearch_SearchPrevious:HxQuery_Options.HxQuery_No_Option;
searchDialog.Text = sps.Parse("${res:AddIns.HtmlHelp2.HelpSearchCaption}");
searchDialog.ActionLabel = sps.Parse("${res:AddIns.HtmlHelp2.HelpSearchInProgress}", new string[,] {{"0", searchWord}});
searchDialog.Show();
Application.DoEvents();
Cursor.Current = Cursors.WaitCursor;
IHxTopicList matchingTopics = h2env.FTS.Query(searchWord, searchFlags);
Cursor.Current = Cursors.Default;
try
{
searchResults.CleanUp();
searchResults.SearchResultsListView.BeginUpdate();
foreach(IHxTopic topic in matchingTopics)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = topic.get_Title(HxTopicGetTitleType.HxTopicGetRLTitle,HxTopicGetTitleDefVal.HxTopicGetTitleFileName);
lvi.SubItems.Add(topic.Location);
lvi.SubItems.Add(topic.Rank.ToString());
lvi.Tag = topic;
searchResults.SearchResultsListView.Items.Add(lvi);
}
reuseMatches.Enabled = true;
}
finally
{
searchResults.SearchResultsListView.EndUpdate();
searchResults.SetStatusMessage(searchTerm.Text);
searchResults.BringPadToFront();
searchIsBusy = false;
}
}
catch
{
}
finally
{
searchDialog.Dispose();
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -