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

135 lines
4.5 KiB
C#
Raw Normal View History

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;
2014-07-26 10:49:11 +00:00
using RainmeterStudio.Business;
2014-08-30 07:24:01 +00:00
using RainmeterStudio.Core.Documents;
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-07-26 10:49:11 +00:00
public DocumentController DocumentController { get; set; }
public ProjectController ProjectController { get; set; }
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>();
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);
// Subscribe to events
2014-07-26 10:49:11 +00:00
DocumentController.DocumentOpened += documentController_DocumentOpened;
// Initialize panels
projectPanel.Controller = ProjectController;
}
void documentController_DocumentOpened(object sender, DocumentOpenedEventArgs e)
{
2014-08-30 07:24:01 +00:00
// Create a new panel
LayoutDocument document = new LayoutDocument();
2014-08-30 07:24:01 +00:00
_openedDocuments.Add(document, e.Editor);
document.Content = e.Editor.EditorUI;
document.Closing += document_Closing;
2014-08-30 07:24:01 +00:00
document.Closed += document_Closed;
2014-09-08 19:53:00 +00:00
document.Title = GetDocumentTitle(e.Document);
document.IsActiveChanged += new EventHandler((sender2, e2) =>
{
if (document.IsActive)
DocumentController.ActiveDocumentEditor = e.Editor;
});
documentPane.Children.Add(document);
documentPane.SelectedContentIndex = documentPane.IndexOf(document);
2014-08-30 07:24:01 +00:00
e.Document.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler((obj, args) =>
{
2014-09-08 19:53:00 +00:00
// Update document title
document.Title = GetDocumentTitle(e.Document);
2014-08-30 07:24:01 +00:00
});
}
2014-09-08 19:53:00 +00:00
private string GetDocumentTitle(IDocument document)
{
string documentName;
// Get title
if (ProjectController.ActiveProject == null || !ProjectController.ActiveProject.Contains(document.Reference))
{
documentName = document.Reference.StoragePath ?? "New document";
}
else
{
documentName = document.Reference.Name;
}
// Is document dirty? Append star
if (document.IsDirty)
{
documentName += "*";
}
return documentName;
}
2014-08-30 07:24:01 +00:00
void document_Closed(object sender, EventArgs e)
{
var layoutDocument = (LayoutDocument)sender;
}
void document_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Get editor
var document = (LayoutDocument)sender;
var editor = _openedDocuments[document];
2014-08-30 07:24:01 +00:00
// Try to close active document
if (!DocumentController.Close(editor))
{
e.Cancel = true;
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Try to close
if (!DocumentController.CloseAll())
{
e.Cancel = true;
}
}
}
2014-07-23 20:27:14 +00:00
}