ToeCracker/ToeCracker/MainForm.cs

134 lines
5.2 KiB
C#

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Net;
using System.Windows.Forms;
using System.Xml;
namespace ToeCracker {
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
/// <summary>
/// Create a new tab page in the form's TabControl to represent a list of words returned in a query.
/// </summary>
/// <param name="title">Title of the tab page.</param>
/// <param name="words">List of words to display.</param>
private void CreateTab(string title, string[] words) {
if (Thesaurus.Translations.ContainsKey(title))
title = Thesaurus.Translations[title];
TabPage page = new TabPage(title);
ListBox listBox = new ListBox();
listBox.Items.AddRange(words);
listBox.Dock = DockStyle.Fill;
listBox.ContextMenuStrip = this.wordContextMenuStrip;
// Double-clicking a word is equivalent to right click -> Look Up
listBox.DoubleClick += lookUpToolStripMenuItem_Click;
page.Controls.Add(listBox);
this.tabControl.TabPages.Add(page);
}
/// <summary>
/// Perform a search query and set the results in the tab control or display an error message.
/// </summary>
/// <param name="query"></param>
private void Search(string query) {
this.Enabled = false;
Dictionary<string, string[]> results = new Dictionary<string, string[]>();
try {
results = Thesaurus.Search(query);
} catch (WebException e) {
MessageBox.Show(
Properties.Resources.RequestError + "\n\n" + e.ToString(),
Properties.Resources.RequestErrorTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
// No early return: we let the below code handle a "no results" case with the default empty dictionary.
} catch (XmlException e) {
MessageBox.Show(
Properties.Resources.ParsingError + "\n\n" + e.ToString(),
Properties.Resources.ParsingErrorTitle,
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
this.tabControl.TabPages.Clear();
if (results.Count > 0) {
foreach (KeyValuePair<string, string[]> kv in results)
this.CreateTab(kv.Key, kv.Value);
} else {
// Show the "no results" tab as there were no results in the dictionary.
this.tabControl.TabPages.Add(this.defaultTabPage);
}
this.Enabled = true;
}
private string SelectedWord {
get {
// Ignore when there are no tabs at all
if (this.tabControl.SelectedTab == null)
return null;
if (this.tabControl.SelectedTab.Controls.Count > 1)
throw new NotSupportedException("Unexpected control count in tab");
// Ignore an empty "No results" tab
if (this.tabControl.SelectedTab.Controls.Count == 0)
return null;
string word = (string)((ListBox)this.tabControl.SelectedTab.Controls[0]).SelectedItem;
if (word == null)
return null;
// Ignore empty lines and subgroups. Subgroups are represented as "-- ANTONYMS --"
word = word.Trim();
if (word == String.Empty || word.StartsWith("-- "))
return null;
return word;
}
}
private void searchTextBox_TextChanged(object sender, EventArgs e) {
this.searchButton.Enabled = this.searchTextBox.Text.Trim().Length > 0;
}
private void searchButton_Click(object sender, EventArgs e) {
string query = this.searchTextBox.Text.Trim();
if (query.Length > 0)
this.Search(query);
}
private void wordContextMenuStrip_Opening(object sender, CancelEventArgs e) {
// Disable the context menu items when the user right-clicks but no word was selected
this.copyToolStripMenuItem.Enabled = this.SelectedWord != null;
this.lookUpToolStripMenuItem.Enabled = this.SelectedWord != null;
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
if (this.SelectedWord != null)
Clipboard.SetText(this.SelectedWord, TextDataFormat.UnicodeText);
}
private void lookUpToolStripMenuItem_Click(object sender, EventArgs e) {
if (this.SelectedWord != null) {
this.searchTextBox.Text = this.SelectedWord;
this.Search(this.SelectedWord);
}
}
}
}