/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastForward.Monitor;
namespace FastForward.WinCore
{
///
/// Manages multiple occurances of windows.
///
public class WindowManager
where TWindow : Form
{
#region Private fields
private Dictionary windows = new Dictionary();
private List configuredWindows = new List();
#endregion
#region Public properties
#region ConfiguredWindows
///
/// Gets the configured windows.
///
public List ConfiguredWindows
{
get { return configuredWindows; }
}
#endregion
#endregion
#region Public methods
#region Show()
///
/// Shows a window.
///
/// The key of the window to show.
public void Show(TKey key)
{
var window = windows[key];
if (window.Visible)
{
window.BringToFront();
}
else
{
window.Show();
}
}
///
/// Shows a window, initialising it if it does not exist.
///
/// The key of the window to show.
/// The function to initialise the new window.
/// The name of the window.
public void Show(TKey key, Func initialise, string name)
{
if (!windows.ContainsKey(key))
{
Add(key, initialise(key), name);
}
Show(key);
}
#endregion
#region ContainsKey()
///
/// Checks if the key is already assigned.
///
/// The key to check.
/// True if the key has been assigned, false otherwise.
public bool ContainsKey(TKey key)
{
return windows.ContainsKey(key);
}
#endregion
#region Add()
///
/// Adds a new window.
///
/// The key for the window.
/// The window to show.
/// The name of the window.
public void Add(TKey key, TWindow window, string name)
{
// Find the configuration
var config = (from record in configuredWindows
where record.Name == name
select record).SingleOrDefault();
if (config == null)
{
// Start a new configuration
config = new WindowConfig
{
Name = name
};
configuredWindows.Add(config);
}
else
{
// Load the configuration settings
window.StartPosition = FormStartPosition.Manual;
if (config.Left != int.MinValue) window.Left = config.Left;
if (config.Top != int.MinValue) window.Top = config.Top;
if (config.Width != int.MinValue) window.Width = config.Width;
if (config.Height != int.MinValue) window.Height = config.Height;
window.WindowState = config.State;
}
// Add the window and add the event handlers
windows.Add(key, window);
window.FormClosing += (o, e) =>
{
// Store the current settings
if (window.WindowState == FormWindowState.Normal)
{
config.Left = window.Left;
config.Top = window.Top;
config.Width = window.Width;
config.Height = window.Height;
}
config.State = window.WindowState;
};
window.FormClosed += (o, e) =>
{
windows.Remove(key);
};
}
#endregion
#endregion
}
}