math-suite/Source/Help/MainWindow.xaml.cs

220 lines
6.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Help
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string searchSavedHtml = string.Empty;
int searchLastType = -1;
public MainWindow()
{
InitializeComponent();
PopulateChapters();
if (HelpSystem.HelpManager.Chapters.Count != 0)
PopulateTopics();
// Check for errors, exceptions
if (Application.Current.Properties.Contains("ERROR"))
{
var ex = Application.Current.Properties["ERROR"] as Exception;
if (ex != null) MessageBox.Show(ex.Message, "Could not load the help documents.", MessageBoxButton.OK, MessageBoxImage.Error);
}
// Check for arguments
if (Application.Current.Properties.Contains("APPLICATION"))
SetApplication(Application.Current.Properties["APPLICATION"].ToString());
}
public void SetApplication(string application)
{
int item = HelpSystem.HelpManager.Chapters.FindIndex(x => x.Application == application);
if (item != -1) this.listChapters.SelectedItem = HelpSystem.HelpManager.Chapters[item];
}
#region Populate routines
public void PopulateChapters()
{
foreach (var i in HelpSystem.HelpManager.Chapters)
listChapters.Items.Add(i);
listChapters.SelectedIndex = 0;
}
public void PopulateTopics()
{
var chapter = listChapters.SelectedItem as HelpSystem.HelpChapter;
if (chapter == null) return;
this.listTopics.Items.Clear();
// Populate
foreach (var i in chapter.Topics)
listTopics.Items.Add(i);
}
public void PopulateTopics(IEnumerable<HelpSystem.HelpTopic> topics)
{
this.listTopics.Items.Clear();
foreach (var i in topics) listTopics.Items.Add(i);
}
#endregion
#region User input
private void Button_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
Button b = sender as Button;
if (b == null) return;
b.Opacity = (b.IsEnabled) ? 1.0 : .5;
}
#region Search function
private void bSearchOptions_Click(object sender, RoutedEventArgs e)
{
bSearchOptions.ContextMenu.IsOpen = true;
}
private void bSearchHandler()
{
if (textSearch.Text == "") return;
// Hide search button, and show clear button
bClear.Visibility = System.Windows.Visibility.Visible;
bSearch.Visibility = System.Windows.Visibility.Collapsed;
// Clear previous searches
if (searchLastType != -1) clearSearchCommon();
// Search title only
if (searchTitleOnly.IsChecked.HasValue && searchTitleOnly.IsChecked.Value)
{
var temp = listChapters.SelectedItem as HelpSystem.HelpChapter;
if (temp == null) return;
listTopics.Items.Clear();
PopulateTopics(HelpSystem.HelpManager.SearchTitle(textSearch.Text, temp));
}
else if (searchContent.IsChecked.HasValue && searchContent.IsChecked.Value)
{
var temp = listChapters.SelectedItem as HelpSystem.HelpChapter;
if (temp == null) return;
listTopics.Items.Clear();
PopulateTopics(HelpSystem.HelpManager.SearchText(textSearch.Text, temp));
}
else if (searchPage.IsChecked.HasValue && searchPage.IsChecked.Value)
{
searchSavedHtml = HelpSystem.HelpHtmlWrapper.GetHtml(webBrowser.Document);
HelpSystem.HelpHtmlWrapper.Find(webBrowser.Document, textSearch.Text);
searchLastType = 3;
}
}
private void clearSearchCommon()
{
if (searchLastType == 3) HelpSystem.HelpHtmlWrapper.SetHtml(webBrowser.Document, searchSavedHtml);
else PopulateTopics();
searchLastType = -1;
}
private void bClear_Click(object sender, RoutedEventArgs e)
{
bClear.Visibility = System.Windows.Visibility.Collapsed;
bSearch.Visibility = System.Windows.Visibility.Visible;
textSearch.Clear();
clearSearchCommon();
}
private void bSearch_Click(object sender, RoutedEventArgs e)
{
bSearchHandler();
}
private void textSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
e.Handled = true;
bSearchHandler();
}
}
#endregion
#region Web Browser
private void bBrowseBack_Click(object sender, RoutedEventArgs e)
{
webBrowser.GoBack();
}
private void bBrowseForward_Click(object sender, RoutedEventArgs e)
{
webBrowser.GoForward();
}
private void bBrowseHome_Click(object sender, RoutedEventArgs e)
{
var item = listChapters.SelectedItem as HelpSystem.HelpChapter;
if (item != null) webBrowser.Navigate(new Uri(HelpSystem.HelpManager.GetFullPath(item.Filename)));
}
private void webBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (searchLastType == 3) bClear_Click(this, new RoutedEventArgs());
bBrowseBack.IsEnabled = webBrowser.CanGoBack;
bBrowseForward.IsEnabled = webBrowser.CanGoForward;
}
#endregion
#region Navigation
private void listTopics_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var sel = listTopics.SelectedItem as HelpSystem.HelpTopic;
if (sel != null) webBrowser.Navigate(new Uri(HelpSystem.HelpManager.GetFullPath(sel.Filename)));
}
private void listChapters_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var sel = listChapters.SelectedItem as HelpSystem.HelpChapter;
if (sel != null)
{
webBrowser.Navigate(new Uri(HelpSystem.HelpManager.GetFullPath(sel.Filename)));
PopulateTopics();
}
}
#endregion
#endregion
}
}