// Makes use of the code provided by Tony Tanzilla located at
// http://www.theswamp.org/index.php?topic=42179.msg476706#msg476706
namespace Autodesk.AutoCAD.Windows;
{
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
/// <summary>
/// An enhanced version of the PaletteSet that will insure that the
/// visibility of the palette is <see langword="false" /> when the
/// application enters a Zero Document State or the current document is
/// null. When exiting a Zero Document State or a <see langword="null"/>
/// document the palette will regain its previous visibility setting.
/// </summary>
public class EnhancedPaletteSet : PaletteSet
{
#region Private Fields
/// <summary>
/// Determines if the idle event was handled.
/// </summary>
private bool _idleHandled;
/// <summary>
/// Determines if the class has been initialized.
/// </summary>
private bool _initialized;
/// <summary>
/// Determines if the palette was rolled up.
/// </summary>
private bool _wasRolledUp;
/// <summary>
/// Stores the previous state of the palette
/// </summary>
private bool _wasVisible;
/// <summary>
/// Determines if the palette was auto closed.
/// </summary>
private bool _autoClosed;
#endregion Private Fields
#region Public Constructors
/// <summary>
/// Initializes a new instance of the
/// <see cref="EnhancedPaletteSet" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="cmd">The command.</param>
/// <param name="toolId">The tool identifier.</param>
public EnhancedPaletteSet(string name, string cmd, Guid toolId)
: base(name, cmd, toolId)
{
this.Initialize();
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="EnhancedPaletteSet" /> class.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="toolId">The tool identifier.</param>
public EnhancedPaletteSet(string name, Guid toolId)
: base(name, toolId)
{
this.Initialize();
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="EnhancedPaletteSet" /> class.
/// </summary>
/// <param name="name">The name.</param>
public EnhancedPaletteSet(string name)
: base(name)
{
this.Initialize();
}
#endregion Public Constructors
#region Public Events
/// <summary>
/// Occurs when the palette set is closed by the user.
/// </summary>
public event EventHandler PaletteSetClosed;
#endregion Public Events
#region Public Methods
/// <summary>
/// Toggles this instance.
/// </summary>
public void Toggle()
{
this.Visible = !this.Visible;
}
#endregion Public Methods
#region Protected Methods
/// <summary>
/// Called when the palette set is closed If you derive your
/// PaletteSet class from this class your derived class can
/// <see langword="override"/> this method rather than handling the
/// <see cref="PaletteSetClosed"/> event but make sure to call this
/// method from the overridden method.
/// </summary>
protected virtual void OnPaletteSetClosed()
{
if (!this._autoClosed)
{
if (this.PaletteSetClosed != null)
{
this.PaletteSetClosed(this, EventArgs.Empty);
}
}
if (!this._autoClosed)
{
this._wasVisible = false;
}
}
#endregion Protected Methods
#region Private Methods
/// <summary>
/// Handles the OnIdle event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="System.EventArgs" /> instance containing the event
/// data.
/// </param>
private void Application_OnIdle(object sender, EventArgs e)
{
Application.Idle -= this.Application_OnIdle;
this._idleHandled = false;
if (!(this.Visible || (this._wasRolledUp ^ this.RolledUp)))
{
this.OnPaletteSetClosed();
}
}
/// <summary>
/// Handles the DocumentActivated event of the DocumentCollection
/// control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The
/// <see cref="Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs" />
/// instance containing the event data.
/// </param>
private void DocumentCollection_DocumentActivated(object sender, DocumentCollectionEventArgs e)
{
if (Active.Document == null)
{
if (this.Visible || this.RolledUp)
{
this.Visible = false;
this._autoClosed = true;
this._wasVisible = true;
}
}
else
{
if (this._wasVisible)
{
this.Visible = true;
this._autoClosed = false;
}
}
}
/// <summary>
/// If we have exited zero document state or the new document is not
/// <see langword="null"/> then this method will execute.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The
/// <see cref="Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs" />
/// instance containing the event data.
/// </param>
private void DocumentCollection_DocumentCreatedFromZeroDocumentState(object sender, DocumentCollectionEventArgs e)
{
var documentCollection = (DocumentCollection)sender;
if (documentCollection.Count > 1 && Active.Document != null)
{
if (this._wasVisible)
{
this.Visible = true;
this._autoClosed = false;
}
}
}
/// <summary>
/// If in zero document state or if the active document is
/// <see langword="null"/> then this method will execute.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">
/// The
/// <see cref="Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs" />
/// instance containing the event data.
/// </param>
private void DocumentCollection_DocumentToBeDestroyedPreventZeroDocumentState(object sender, DocumentCollectionEventArgs e)
{
var documentCollection = (DocumentCollection)sender;
if (documentCollection.Count == 1 || Active.Document == null)
{
if (this.Visible)
{
this.Visible = false;
this._wasVisible = true;
this._autoClosed = true;
}
else
{
this._wasVisible = false;
}
}
}
/// <summary>
/// Initializes the class. If you overload the construction in a
/// derived type, your overload must call one of the above
/// constructors or it must call this method.
/// </summary>
private void Initialize()
{
if (!this._initialized)
{
this._initialized = true;
var documentCollection = Application.DocumentManager;
documentCollection.DocumentToBeDestroyed += this.DocumentCollection_DocumentToBeDestroyedPreventZeroDocumentState;
documentCollection.DocumentCreated += this.DocumentCollection_DocumentCreatedFromZeroDocumentState;
documentCollection.DocumentActivated += this.DocumentCollection_DocumentActivated;
this.StateChanged += this.PaletteSet_StateChanged;
}
}
/// <summary>
/// Handles the StateChanged event of the PaletteSet control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// The <see cref="Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs" />
/// instance containing the event data.
/// </param>
private void PaletteSet_StateChanged(object sender, PaletteSetStateEventArgs e)
{
if (!this._idleHandled && e.NewState == StateEventIndex.Hide)
{
this._idleHandled = true;
this._wasRolledUp = this.RolledUp;
Application.Idle += this.Application_OnIdle;
}
else if (this._idleHandled && e.NewState == StateEventIndex.Show)
{
this._idleHandled = false;
Application.Idle -= this.Application_OnIdle;
}
}
#endregion Private Methods
}}