using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.ComponentModel;
using NESDecompiler.GUI.ViewModels;
using ICSharpCode.AvalonEdit.Highlighting;
using System.Xml;
using System.IO;
using System.Reflection;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using static NESDecompiler.GUI.ViewModels.ProjectItemViewModel;
using System.Windows.Media;
using NESDecompiler.GUI.Themes;
using System.Collections.Generic;
using ICSharpCode.AvalonEdit;
namespace NESDecompiler.GUI
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
private MainViewModel viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new MainViewModel();
DataContext = viewModel;
LoadSyntaxHighlighting();
Loaded += MainWindow_Loaded;
Closing += MainWindow_Closing;
}
///
/// Loads custom syntax highlighting for ASM 6502
///
private void LoadSyntaxHighlighting()
{
try
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NESDecompiler.GUI.Resources.ASM6502.xshd"))
{
if (stream != null)
{
using (var reader = new XmlTextReader(stream))
{
var highlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
HighlightingManager.Instance.RegisterHighlighting("ASM6502", new[] { ".asm" }, highlighting);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load syntax highlighting: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
viewModel.Initialize();
if (viewModel is INotifyPropertyChanged notifyViewModel)
{
notifyViewModel.PropertyChanged += ViewModel_PropertyChanged;
}
cCodeEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
headerEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
try
{
disassemblyEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("ASM6502");
}
catch (Exception)
{
disassemblyEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML");
}
ThemeManager.Instance.ThemeChanged += (s, args) => ApplyThemeToEditors();
ApplyThemeToEditors();
SetupEditorEvents();
}
private void ApplyThemeToEditors()
{
bool isDarkTheme = ThemeManager.Instance.IsDarkTheme;
SolidColorBrush backgroundBrush = Application.Current.Resources["SecondaryBackgroundBrush"] as SolidColorBrush;
SolidColorBrush foregroundBrush = Application.Current.Resources["PrimaryForegroundBrush"] as SolidColorBrush;
if (backgroundBrush != null && foregroundBrush != null)
{
ApplyThemeToEditor(disassemblyEditor, backgroundBrush, foregroundBrush);
ApplyThemeToEditor(cCodeEditor, backgroundBrush, foregroundBrush);
ApplyThemeToEditor(headerEditor, backgroundBrush, foregroundBrush);
}
}
private void ApplyThemeToEditor(TextEditor editor, SolidColorBrush background, SolidColorBrush foreground)
{
if (editor != null)
{
editor.Background = background;
editor.Foreground = foreground;
}
}
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(MainViewModel.DisassemblyText))
{
disassemblyEditor.Text = viewModel.DisassemblyText;
}
else if (e.PropertyName == nameof(MainViewModel.CCodeText))
{
cCodeEditor.Text = viewModel.CCodeText;
}
else if (e.PropertyName == nameof(MainViewModel.HeaderText))
{
headerEditor.Text = viewModel.HeaderText;
}
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
if (viewModel is INotifyPropertyChanged notifyViewModel)
{
notifyViewModel.PropertyChanged -= ViewModel_PropertyChanged;
}
if (viewModel.HasUnsavedChanges)
{
var result = MessageBox.Show(
"You have unsaved changes. Do you want to save before exiting?",
"Save Changes",
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);
if (result == MessageBoxResult.Cancel)
{
e.Cancel = true;
return;
}
if (result == MessageBoxResult.Yes)
{
viewModel.SaveAllCommand.Execute(null);
}
}
}
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs