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

180 lines
5.3 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;
using System.Threading;
namespace Factorizator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
#region Error variable
private bool error = false;
private bool Error
{
get { return error; }
set
{
error = value;
inputNumber.Background = (value) ? (Brushes.Pink) : (Brushes.White);
}
}
#endregion
#region Vars
Thread worker = null;
#endregion
#region Constructor
public MainWindow()
{
InitializeComponent();
}
#endregion
#region Keyboard handler
private void Window_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
var text = Keyboard.FocusedElement as TextBox;
if (e.KeyboardDevice.Modifiers == ModifierKeys.None)
{
switch (e.Key)
{
case Key.Enter: buttonCalculate_Click(this, new RoutedEventArgs()); break;
case Key.Escape: statusCancel_Click(this, new RoutedEventArgs()); break;
case Key.F1: DynamicLink.Launcher.StartModule("Help", "factorizator"); break;
default: e.Handled = false; break;
}
}
else if (text == null && e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.C)
contextCopyRem_Click(this, new RoutedEventArgs());
else if (text == null && e.KeyboardDevice.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift) && e.Key == Key.C)
contextCopyDiv_Click(this, new RoutedEventArgs());
else e.Handled = false;
}
#endregion
#region UI handlers
private void inputNumber_TextChanged(object sender, TextChangedEventArgs e)
{
if (Error)
{
Error = false;
statusMessage.Text = "Ready";
}
}
private void buttonCalculate_Click(object sender, RoutedEventArgs e)
{
if (inputNumber.Text == "") return;
decimal d;
if (!decimal.TryParse(inputNumber.Text, out d))
{
Error = true;
statusMessage.Text = "Bad number.";
}
else BeginWork(d);
}
private void contextCopyRem_Click(object sender, RoutedEventArgs e)
{
DecimalPair pair = factorizationDisplay.SelectedItem as DecimalPair;
if (pair == null) return;
Clipboard.SetText(pair.First.ToString());
}
private void contextCopyDiv_Click(object sender, RoutedEventArgs e)
{
DecimalPair pair = factorizationDisplay.SelectedItem as DecimalPair;
if (pair == null) return;
Clipboard.SetText(pair.Second.ToString());
}
private void contextClear_Click(object sender, RoutedEventArgs e)
{
factorizationDisplay.Items.Clear();
}
private void statusCancel_Click(object sender, RoutedEventArgs e)
{
FactorizatorAlgorithm.Abort = true;
}
private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
{
DynamicLink.Launcher.StartModule("Help", "factorizator");
}
#endregion
#region Worker
void BeginWork(decimal num)
{
num = Math.Truncate(num);
// Visual stuff
statusMessage.Text = "Calculating...";
statusProgress.Visibility = System.Windows.Visibility.Visible;
statusCancel.Visibility = System.Windows.Visibility.Visible;
// Start thread
worker = new Thread(new ParameterizedThreadStart(DoWork));
worker.Start(num);
}
delegate void FinishWorkDelegate(decimal[] resultFact, decimal[] resultNum);
void DoWork(object param)
{
decimal number = (decimal)param;
decimal[] resultFact, resultNum;
FactorizatorAlgorithm.Factorize(number, out resultFact, out resultNum);
Dispatcher.Invoke(new FinishWorkDelegate(FinishWork), resultFact, resultNum);
}
void FinishWork(decimal[] resultFact, decimal[] resultNum)
{
factorizationDisplay.Items.Clear();
// Show results
for (int i = 0; i < resultNum.Length; i++)
{
if (i < resultFact.Length)
factorizationDisplay.Items.Add(new DecimalPair(resultNum[i], resultFact[i]));
else factorizationDisplay.Items.Add(new DecimalPair(resultNum[i], 0));
}
// Update visual stuff
statusMessage.Text = "Ready";
statusProgress.Visibility = System.Windows.Visibility.Hidden;
statusCancel.Visibility = System.Windows.Visibility.Hidden;
}
#endregion
}
}