rainmeter-studio/RainmeterStudio/UI/MainWindow.xaml.cs

265 lines
8.4 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
2014-07-26 10:49:11 +00:00
using RainmeterStudio.Business;
2014-09-12 10:26:52 +00:00
using RainmeterStudio.Core.Editor;
2014-10-11 07:33:59 +00:00
using RainmeterStudio.Core.Editor.Features;
2014-08-31 11:41:24 +00:00
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Model.Events;
using RainmeterStudio.UI.Controller;
2014-08-16 12:40:08 +00:00
using RainmeterStudio.UI.Panels;
using Xceed.Wpf.AvalonDock.Layout;
namespace RainmeterStudio.UI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
2014-10-11 07:33:59 +00:00
/// <summary>
/// Gets or sets the document controller
/// </summary>
2014-07-26 10:49:11 +00:00
public DocumentController DocumentController { get; set; }
2014-10-11 07:33:59 +00:00
/// <summary>
/// Gets or sets the project controller
/// </summary>
2014-07-26 10:49:11 +00:00
public ProjectController ProjectController { get; set; }
2014-10-11 07:33:59 +00:00
/// <summary>
/// Gets the project panel
/// </summary>
2014-08-16 12:40:08 +00:00
public ProjectPanel ProjectPanel { get { return projectPanel; } }
2014-08-30 07:24:01 +00:00
private Dictionary<LayoutDocument, IDocumentEditor> _openedDocuments = new Dictionary<LayoutDocument, IDocumentEditor>();
2014-10-11 07:33:59 +00:00
#region Constructor
/// <summary>
/// Initializes the main window
/// </summary>
/// <param name="projCtrl">The project controller</param>
/// <param name="docCtrl">The document controller</param>
public MainWindow(ProjectController projCtrl, DocumentController docCtrl)
{
InitializeComponent();
// Set fields
DataContext = this;
DocumentController = docCtrl;
ProjectController = projCtrl;
// Add key bindings
this.AddKeyBinding(DocumentController.DocumentCreateCommand);
this.AddKeyBinding(DocumentController.DocumentOpenCommand);
this.AddKeyBinding(DocumentController.DocumentSaveCommand);
this.AddKeyBinding(DocumentController.DocumentCloseCommand);
this.AddKeyBinding(ProjectController.ProjectCreateCommand);
this.AddKeyBinding(ProjectController.ProjectOpenCommand);
// Subscribe to events
2014-10-11 07:33:59 +00:00
DocumentController.DocumentOpened += DocumentController_DocumentOpened;
// Initialize panels
projectPanel.ProjectController = ProjectController;
projectPanel.DocumentController = DocumentController;
}
2014-10-11 07:33:59 +00:00
#endregion
#region Document opened event handler
void DocumentController_DocumentOpened(object sender, DocumentOpenedEventArgs e)
{
2014-10-11 07:33:59 +00:00
OpenDocument(e.Editor);
}
#endregion
2014-08-30 07:24:01 +00:00
2014-10-11 07:33:59 +00:00
#region Document events
void Document_IsActiveChanged(object sender, EventArgs e)
{
LayoutDocument document = (LayoutDocument)sender;
IDocumentEditor editor = _openedDocuments[document];
if (document.IsActive)
{
2014-10-11 07:33:59 +00:00
// Set active editor in controller
DocumentController.ActiveDocumentEditor = editor;
2014-10-11 07:33:59 +00:00
// Set up toolbox
SetUpToolbox(editor);
}
else
{
// Disable drop, so that we don't drop invalid items in an editor
editor.EditorUI.AllowDrop = false;
}
}
2014-08-30 07:24:01 +00:00
2014-10-11 07:33:59 +00:00
void Document_Closed(object sender, EventArgs e)
{
CloseDocument((LayoutDocument)sender);
}
void Document_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Get editor
var document = (LayoutDocument)sender;
var editor = _openedDocuments[document];
// Try to close active document
if (!DocumentController.Close(editor))
{
e.Cancel = true;
}
}
private void Document_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
IDocument document = (IDocument)sender;
// Find document object and update document title
foreach (var pair in _openedDocuments)
2014-08-30 07:24:01 +00:00
{
if (pair.Value.AttachedDocument == document)
{
pair.Key.Title = GetDocumentTitle(document);
}
}
// If the reference changed, subscribe to reference changes as well
if (e.PropertyName == "Reference" && document.Reference != null)
{
document.Reference.PropertyChanged += Reference_PropertyChanged;
}
}
void Reference_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Reference reference = (Reference)sender;
bool found = false;
// Find documents with this reference and update document title
foreach (var pair in _openedDocuments)
{
if (pair.Value.AttachedDocument.Reference == reference)
{
pair.Key.Title = GetDocumentTitle(pair.Value.AttachedDocument);
found = true;
}
}
// No document found? Unsubscribe
if (found == false)
{
reference.PropertyChanged -= Reference_PropertyChanged;
}
2014-08-30 07:24:01 +00:00
}
2014-10-11 07:33:59 +00:00
#endregion
#region Window events
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Try to close
if (!DocumentController.CloseAll())
{
e.Cancel = true;
}
}
#endregion
#region Helper methods
2014-09-08 19:53:00 +00:00
private string GetDocumentTitle(IDocument document)
{
string documentName;
// Get title
if (document.Reference == null)
{
documentName = "New document";
}
else if (ProjectController.ActiveProject == null || !ProjectController.ActiveProject.Contains(document.Reference))
2014-09-08 19:53:00 +00:00
{
documentName = document.Reference.StoragePath ?? "New document";
}
else
{
documentName = document.Reference.Name;
}
// Is document dirty? Append star
if (document.IsDirty)
{
documentName += "*";
}
return documentName;
}
2014-10-11 07:33:59 +00:00
private LayoutDocument OpenDocument(IDocumentEditor editor)
2014-08-30 07:24:01 +00:00
{
2014-10-11 07:33:59 +00:00
// Create document
LayoutDocument document = new LayoutDocument();
document.Content = editor.EditorUI;
document.Title = GetDocumentTitle(editor.AttachedDocument);
// Set up events
document.Closing += Document_Closing;
document.Closed += Document_Closed;
document.IsActiveChanged += Document_IsActiveChanged;
// Add to dictionary
_openedDocuments.Add(document, editor);
// Add to layout
documentPane.Children.Add(document);
documentPane.SelectedContentIndex = documentPane.IndexOf(document);
// Subscribe to document events
editor.AttachedDocument.PropertyChanged += Document_PropertyChanged;
if (editor.AttachedDocument.Reference != null)
editor.AttachedDocument.Reference.PropertyChanged += Reference_PropertyChanged;
return document;
}
2014-10-11 07:33:59 +00:00
private void CloseDocument(LayoutDocument document)
{
2014-10-11 07:33:59 +00:00
_openedDocuments.Remove(document);
}
2014-10-11 07:33:59 +00:00
private void SetUpToolbox(IDocumentEditor editor)
{
2014-10-11 07:33:59 +00:00
var toolboxProvider = editor as IToolboxProvider;
// Set toolbar panel
toolboxPanel.ItemsSource = toolboxProvider;
// Enable 'allow drop'
if (toolboxProvider != null)
{
2014-10-11 07:33:59 +00:00
editor.EditorUI.AllowDrop = true;
}
}
2014-10-11 07:33:59 +00:00
#endregion
}
2014-07-23 20:27:14 +00:00
}