// bts - The Bug Tracking System
// Copyright (C) 2004 - Eugene Konkov
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
/********************************************************************
This is a part of Eugene Konkov base class library (ECL)
Copyright (c) 2004 Eugene Konkov
*********************************************************************/
using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;
using ekon.BaseObjects.Registry;
namespace ekon.BaseObjects.GUI{
/// <summary>
/// Automatic form persistence.
/// </summary>
public class FormPersistence
{
private System.Windows.Forms.Form mainFrame;
private RegistryProfile profile;
private bool restoreComplete = false;
public FormPersistence(System.Windows.Forms.Form mainFrame,string subkey)
{
this.mainFrame = mainFrame;
this.profile = new RegistryProfile(subkey);
this.mainFrame.Closing+=new System.ComponentModel.CancelEventHandler(this.MainFrame_Closing);
this.mainFrame.Load+=new System.EventHandler(this.MainFrame_Load);
this.mainFrame.Resize+=new System.EventHandler(this.MainFrame_Resize);
this.mainFrame.Move+=new System.EventHandler(this.MainFrame_Move);
}
/// <summary>
/// Restores windows state and position from registry.
/// </summary>
public void Restore()
{
try
{
this.restoreComplete=false;
mainFrame.DesktopBounds=Profile.Read(Names.Bounds+mainFrame.Name);
mainFrame.WindowState=(FormWindowState)Profile.ReadInt(Names.WindowState+mainFrame.Name);
foreach(Control control in mainFrame.Controls)
{
RestoreControl(control);
}
}
catch(Exception)
{
}
finally
{
this.restoreComplete=true;
}
}
/// <summary>
/// Saves windows state and position under registry.
/// </summary>
public void Save()
{
if(this.restoreComplete)
{
Profile.Write(Names.Bounds+mainFrame.Name, mainFrame.DesktopBounds);
SaveWindowState();
foreach(Control control in mainFrame.Controls)
{
SaveControl(control);
}
}
}
private void SaveWindowState()
{
Profile.Write(Names.WindowState+mainFrame.Name,(int)mainFrame.WindowState);
}
private void SaveColumnsOnly()
{
foreach(Control control in mainFrame.Controls)
{
SaveColumns(control);
}
}
private RegistryProfile Profile
{
get
{
return this.profile;
}
}
private void RestoreControl(Control control)
{
Type controlType=control.GetType();
bool isToolBar=(controlType==typeof(ToolBar));
bool isStatusBar=(controlType==typeof(StatusBar));
if(!isToolBar && !isStatusBar)
{
control.Bounds=Profile.Read(Names.Bounds+control.Name);
}
bool isListView=(controlType==typeof(ListView));
if(isListView)
{
RestoreColumns((ListView)control);
}
}
private void SaveControl(Control control)
{
Type controlType=control.GetType();
bool isToolBar=(controlType==typeof(ToolBar));
bool isStatusBar=(controlType==typeof(StatusBar));
if(!isToolBar && !isStatusBar)
{
Profile.Write(Names.Bounds+control.Name, control.Bounds);
}
SaveColumns(control);
}
private void RestoreColumns(ListView listView)
{
foreach(ColumnHeader column in listView.Columns)
{
column.Width=Profile.ReadInt(Names.Column+column.Index);
}
}
private void SaveColumns(Control control)
{
Type controlType= control.GetType();
bool isListView=(controlType==typeof(ListView));
if(isListView)
{
SaveColumns((ListView)control);
}
}
private void SaveColumns(ListView listView)
{
foreach(ColumnHeader column in listView.Columns)
{
Profile.Write(Names.Column + column.Index, column.Width);
}
}
private void MainFrame_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(mainFrame.WindowState==FormWindowState.Normal)
{
Save();
}
else
{
SaveColumnsOnly();
}
}
private void MainFrame_Load(object sender, System.EventArgs e)
{
Restore();
}
private void MainFrame_Resize(object sender, System.EventArgs e)
{
if(mainFrame.WindowState==FormWindowState.Normal)
{
Save();
}
else
{
SaveWindowState();
}
}
private void MainFrame_Move(object sender, System.EventArgs e)
{
if(mainFrame.WindowState==FormWindowState.Normal)
{
Save();
}
else
{
SaveWindowState();
}
}
}
}
|