2019-06-16 23:34:47 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2019-07-05 22:05:42 +00:00
|
|
|
|
using System.ComponentModel;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-07-05 22:05:42 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen.WpfGui {
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Edit Address dialog.
|
|
|
|
|
/// </summary>
|
2019-07-05 22:05:42 +00:00
|
|
|
|
public partial class EditAddress : Window, INotifyPropertyChanged {
|
2019-06-16 23:34:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Address typed by user. Only valid after the dialog returns OK. Will be set to -1
|
|
|
|
|
/// if the user is attempting to delete the address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Address { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maximum allowed address value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int mMaxAddressValue;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Bound two-way property.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string AddressText { get; set; }
|
|
|
|
|
|
2019-07-05 22:05:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set to true when input is valid. Controls whether the OK button is enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsValid {
|
|
|
|
|
get { return mIsValid; }
|
|
|
|
|
set {
|
|
|
|
|
mIsValid = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private bool mIsValid;
|
|
|
|
|
|
|
|
|
|
// INotifyPropertyChanged implementation
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = "") {
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
2019-06-16 23:46:40 +00:00
|
|
|
|
public EditAddress(Window owner, int initialAddr, int maxAddressValue) {
|
2019-06-16 23:34:47 +00:00
|
|
|
|
// Set the property before initializing the window -- we don't have a property
|
|
|
|
|
// change notifier.
|
|
|
|
|
Address = -2;
|
|
|
|
|
mMaxAddressValue = maxAddressValue;
|
|
|
|
|
AddressText = Asm65.Address.AddressToString(initialAddr, false);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
2019-06-16 23:46:40 +00:00
|
|
|
|
Owner = owner;
|
2019-07-05 22:05:42 +00:00
|
|
|
|
DataContext = this;
|
2019-06-16 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-21 22:24:39 +00:00
|
|
|
|
private void Window_ContentRendered(object sender, EventArgs e) {
|
2019-07-07 00:24:42 +00:00
|
|
|
|
addrTextBox.SelectAll();
|
|
|
|
|
addrTextBox.Focus();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 23:34:47 +00:00
|
|
|
|
private void OkButton_Click(object sender, RoutedEventArgs e) {
|
|
|
|
|
if (AddressText.Length == 0) {
|
|
|
|
|
Address = -1;
|
|
|
|
|
} else {
|
|
|
|
|
Asm65.Address.ParseAddress(AddressText, mMaxAddressValue, out int addr);
|
|
|
|
|
Address = addr;
|
|
|
|
|
}
|
|
|
|
|
DialogResult = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles a TextChanged event on the address text box.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Must have UpdateSourceTrigger=PropertyChanged set for this to work. The default
|
|
|
|
|
/// for TextBox is LostFocus.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) {
|
|
|
|
|
if (IsLoaded) {
|
2019-07-05 22:05:42 +00:00
|
|
|
|
string text = AddressText;
|
|
|
|
|
IsValid = (text.Length == 0) ||
|
|
|
|
|
Asm65.Address.ParseAddress(text, mMaxAddressValue, out int unused);
|
2019-06-16 23:34:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-05 22:05:42 +00:00
|
|
|
|
// This might be better with validation rules, but it's sort of awkward to pass parameters
|
|
|
|
|
// (like MaxAddressValue) in.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
// https://social.technet.microsoft.com/wiki/contents/articles/31422.wpf-passing-a-data-bound-value-to-a-validation-rule.aspx
|
|
|
|
|
//
|
2019-07-05 22:05:42 +00:00
|
|
|
|
// Speaking of awkward, updating the OK button's IsEnable value through validation
|
|
|
|
|
// requires MultiDataTrigger.
|
2019-06-16 23:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public class AddressValidationRule : ValidationRule {
|
|
|
|
|
// public int MaxAddress { get; set; }
|
|
|
|
|
|
|
|
|
|
// public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
|
|
|
|
|
// string text = value.ToString();
|
|
|
|
|
// Debug.WriteLine("VALIDATE " + text);
|
|
|
|
|
// if ((text.Length == 0) ||
|
|
|
|
|
// Asm65.Address.ParseAddress(text, MaxAddress, out int unused)) {
|
|
|
|
|
// return new ValidationResult(true, null);
|
|
|
|
|
// } else {
|
|
|
|
|
// return new ValidationResult(false, "Invalid address");
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
}
|