dockpane.cs
来自「全功能c#编译器」· CS 代码 · 共 1,899 行 · 第 1/4 页
CS
1,899 行
// *****************************************************************************
//
// Copyright 2004, Weifen Luo
// All rights reserved. The software and associated documentation
// supplied hereunder are the proprietary information of Weifen Luo
// and are supplied subject to licence terms.
//
// WinFormsUI Library Version 1.0
// *****************************************************************************
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WeifenLuo.WinFormsUI
{
public class DockPane : UserControl
{
protected enum DockPaneAppearance
{
ToolWindow,
Document
}
protected enum HitTestArea
{
Caption,
TabStrip,
Content,
None
}
protected struct HitTestResult
{
public HitTestArea HitArea;
public int Index;
public HitTestResult(HitTestArea hitTestArea)
{
HitArea = hitTestArea;
Index = -1;
}
public HitTestResult(HitTestArea hitTestArea, int index)
{
HitArea = hitTestArea;
Index = index;
}
}
private const int WM_DOCKSTATECHANGED = (int)Win32.Msgs.WM_USER + 1;
static private Bitmap ImageDockWindowCloseEnabled;
static private Bitmap ImageDockWindowCloseDisabled;
static private Bitmap ImageAutoHideYes;
static private Bitmap ImageAutoHideNo;
static private StringFormat StringFormatDockWindowCaption;
static private StringFormat StringFormatDockWindowTab;
static private string ToolTipDockWindowClose;
static private string ToolTipAutoHide;
static private Bitmap ImageDocumentWindowCloseEnabled;
static private Bitmap ImageDocumentWindowCloseDisabled;
static private Bitmap ImageScrollLeftEnabled;
static private Bitmap ImageScrollLeftDisabled;
static private Bitmap ImageScrollRightEnabled;
static private Bitmap ImageScrollRightDisabled;
static private string ToolTipDocumentWindowClose;
static private string ToolTipScrollLeft;
static private string ToolTipScrollRight;
static private StringFormat StringFormatDocumentWindowTab;
static DockPane()
{
//For Tool Window style
ImageDockWindowCloseEnabled = ResourceHelper.LoadBitmap("DockPane.ToolWindowCloseEnabled.bmp");
ImageDockWindowCloseDisabled = ResourceHelper.LoadBitmap("DockPane.ToolWindowCloseDisabled.bmp");
ImageAutoHideYes = ResourceHelper.LoadBitmap("DockPane.AutoHideYes.bmp");
ImageAutoHideNo = ResourceHelper.LoadBitmap("DockPane.AutoHideNo.bmp");
StringFormatDockWindowCaption = new StringFormat();
StringFormatDockWindowCaption.Trimming = StringTrimming.EllipsisCharacter;
StringFormatDockWindowCaption.LineAlignment = StringAlignment.Center;
StringFormatDockWindowCaption.FormatFlags = StringFormatFlags.NoWrap;
StringFormatDockWindowTab = new StringFormat(StringFormat.GenericTypographic);
StringFormatDockWindowTab.Trimming = StringTrimming.EllipsisCharacter;
StringFormatDockWindowTab.LineAlignment = StringAlignment.Center;
StringFormatDockWindowTab.FormatFlags = StringFormatFlags.NoWrap;
ToolTipDockWindowClose = ResourceHelper.GetString("DockPane.ToolTipDockWindowClose");
ToolTipAutoHide = ResourceHelper.GetString("DockPane.ToolTipAutoHide");
//For Document style
ImageDocumentWindowCloseEnabled = ResourceHelper.LoadBitmap("DockPane.DocumentCloseEnabled.bmp");
ImageDocumentWindowCloseDisabled = ResourceHelper.LoadBitmap("DockPane.DocumentCloseDisabled.bmp");
ImageScrollLeftEnabled = ResourceHelper.LoadBitmap("DockPane.ScrollLeftEnabled.bmp");
ImageScrollLeftDisabled = ResourceHelper.LoadBitmap("DockPane.ScrollLeftDisabled.bmp");
ImageScrollRightEnabled = ResourceHelper.LoadBitmap("DockPane.ScrollRightEnabled.bmp");
ImageScrollRightDisabled = ResourceHelper.LoadBitmap("DockPane.ScrollRightDisabled.bmp");
ToolTipDocumentWindowClose = ResourceHelper.GetString("DockPane.ToolTipDocumentWindowClose");
ToolTipScrollLeft = ResourceHelper.GetString("DockPane.ToolTipScrollLeft");
ToolTipScrollRight = ResourceHelper.GetString("DockPane.ToolTipScrollRight");
StringFormatDocumentWindowTab = new StringFormat(StringFormat.GenericTypographic);
StringFormatDocumentWindowTab.Alignment = StringAlignment.Center;
StringFormatDocumentWindowTab.Trimming = StringTrimming.EllipsisPath;
StringFormatDocumentWindowTab.LineAlignment = StringAlignment.Center;
StringFormatDocumentWindowTab.FormatFlags = StringFormatFlags.NoWrap;
}
private InertButton m_buttonDockWindowClose, m_buttonAutoHide;
private InertButton m_buttonDocumentWindowClose, m_buttonScrollLeft, m_buttonScrollRight;
private NestedDockingStatus m_nestedDockingNormal, m_nestedDockingFloat;
private int m_offset = 0;
private ToolTip m_toolTip;
public DockPane(DockContent content, DockState visibleState, bool isHidden)
{
InternalConstruct(content, visibleState, isHidden, null, false, Rectangle.Empty);
}
public DockPane(DockContent content, DockState visibleState, bool isHidden, FloatWindow floatWindow)
{
InternalConstruct(content, visibleState, isHidden, floatWindow, false, Rectangle.Empty);
}
public DockPane(DockContent content, Rectangle floatWindowBounds)
{
InternalConstruct(content, DockState.Float, false, null, true, floatWindowBounds);
}
private void InternalConstruct(DockContent content, DockState visibleState, bool isHidden, FloatWindow floatWindow, bool flagBounds, Rectangle floatWindowBounds)
{
if (content == null)
throw new ArgumentNullException(ResourceHelper.GetString("DockPane.Constructor.NullContent"));
if (content.DockPanel == null)
throw new ArgumentException(ResourceHelper.GetString("DockPane.Constructor.NullDockPanel"));
if (visibleState == DockState.Hidden || visibleState == DockState.Unknown)
throw new ArgumentException(ResourceHelper.GetString("DockPane.VisibleState.InvalidState"));
SuspendLayout();
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.Selectable, true);
m_contents = new DockContentCollection();
m_dockPanel = content.DockPanel;
m_dockPanel.AddPane(this);
m_components = new Container();
m_toolTip = new ToolTip(Components);
m_buttonDockWindowClose = new InertButton(ImageDockWindowCloseEnabled, ImageDockWindowCloseDisabled);
m_buttonAutoHide = new InertButton();
m_buttonDockWindowClose.ToolTipText = ToolTipDockWindowClose;
m_buttonDockWindowClose.Anchor = AnchorStyles.Top | AnchorStyles.Right;
m_buttonDockWindowClose.Click += new EventHandler(this.Close_Click);
m_buttonAutoHide.ToolTipText = ToolTipAutoHide;
m_buttonAutoHide.Anchor = AnchorStyles.Top | AnchorStyles.Right;
m_buttonAutoHide.Click += new EventHandler(AutoHide_Click);
m_buttonDocumentWindowClose = new InertButton(ImageDocumentWindowCloseEnabled, ImageDocumentWindowCloseDisabled);
m_buttonScrollLeft = new InertButton(ImageScrollLeftEnabled, ImageScrollLeftDisabled);
m_buttonScrollRight = new InertButton(ImageScrollRightEnabled, ImageScrollRightDisabled);
m_buttonDocumentWindowClose.ToolTipText = ToolTipDocumentWindowClose;
m_buttonDocumentWindowClose.Anchor = AnchorStyles.Top | AnchorStyles.Right;
m_buttonDocumentWindowClose.Click += new EventHandler(this.Close_Click);
m_buttonScrollLeft.Enabled = false;
m_buttonScrollLeft.ToolTipText = ToolTipScrollLeft;
m_buttonScrollLeft.Anchor = AnchorStyles.Top | AnchorStyles.Right;
m_buttonScrollLeft.Click += new EventHandler(ScrollLeft_Click);
m_buttonScrollRight.Enabled = false;
m_buttonScrollRight.ToolTipText = ToolTipScrollRight;
m_buttonScrollRight.Anchor = AnchorStyles.Top | AnchorStyles.Right;
m_buttonScrollRight.Click += new EventHandler(ScrollRight_Click);
m_splitter = new DockPaneSplitter(this);
Controls.AddRange(new Control[] { m_buttonAutoHide,
m_buttonDockWindowClose,
m_buttonScrollLeft,
m_buttonScrollRight,
m_buttonDocumentWindowClose });
m_nestedDockingNormal = new NestedDockingStatus(this);
m_nestedDockingFloat = new NestedDockingStatus(this);
Font = SystemInformation.MenuFont;
if (floatWindow != null)
FloatWindow = floatWindow;
else if (flagBounds)
DockPanel.FloatWindowFactory.CreateFloatWindow(DockPanel, this, floatWindowBounds);
content.Pane = this;
m_visibleState = visibleState;
m_isHidden = isHidden;
SetDockState();
ResumeLayout();
}
private void Close_Click(object sender, EventArgs e)
{
CloseActiveContent();
if (ActiveContent != null)
ActiveContent.Activate();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Components.Dispose();
Contents.Dispose();
m_visibleState = DockState.Unknown;
m_isHidden = false;
m_dockState = DockState.Unknown;
if (FloatWindow != null)
FloatWindow = null;
if (DockWindow != null)
DockWindow = null;
if (DockPanel != null)
{
DockPanel.RemovePane(this);
m_dockPanel = null;
}
m_splitter.Dispose();
}
base.Dispose(disposing);
}
private DockContent m_activeContent = null;
public virtual DockContent ActiveContent
{
get { return m_activeContent; }
set
{
if (ActiveContent == value)
return;
if (value != null)
{
if (GetIndexOfVisibleContents(value) == -1)
throw(new InvalidOperationException(ResourceHelper.GetString("DockPane.ActiveContent.InvalidValue")));
}
else
{
if (CountOfVisibleContents != 0)
throw(new InvalidOperationException(ResourceHelper.GetString("DockPane.ActiveContent.InvalidValue")));
}
DockContent oldValue = m_activeContent;
if (DockPanel.ActiveAutoHideContent == oldValue)
DockPanel.ActiveAutoHideContent = null;
m_activeContent = value;
if (m_activeContent != null)
EnsureTabVisible(m_activeContent);
if (FloatWindow != null)
FloatWindow.SetText();
Invalidate();
DockPanel.RefreshActiveWindow();
}
}
private bool m_allowRedocking = true;
public virtual bool AllowRedocking
{
get { return m_allowRedocking; }
set { m_allowRedocking = value; }
}
protected virtual Rectangle CaptionRectangle
{
get { return Appearance == DockPaneAppearance.ToolWindow ? CaptionRectangle_ToolWindow : Rectangle.Empty; }
}
private Rectangle CaptionRectangle_ToolWindow
{
get
{
if (!HasCaption)
return Rectangle.Empty;
Rectangle rectWindow = DisplayingRectangle;
int x, y, width;
x = rectWindow.X;
y = rectWindow.Y;
width = rectWindow.Width;
int height = Font.Height + MeasureToolWindowCaption.TextGapTop + MeasureToolWindowCaption.TextGapBottom;
if (height < ImageDockWindowCloseEnabled.Height + MeasureToolWindowCaption.ButtonGapTop + MeasureToolWindowCaption.ButtonGapBottom)
height = ImageDockWindowCloseEnabled.Height + MeasureToolWindowCaption.ButtonGapTop + MeasureToolWindowCaption.ButtonGapBottom;
return new Rectangle(x, y, width, height);
}
}
public virtual string CaptionText
{
get { return ActiveContent == null ? string.Empty : ActiveContent.Text; }
}
private IContainer m_components;
protected IContainer Components
{
get { return m_components; }
}
protected virtual Rectangle ContentRectangle
{
get
{
Rectangle rectWindow = DisplayingRectangle;
Rectangle rectCaption = CaptionRectangle;
Rectangle rectTabStrip = GetTabStripRectangle();
int x = rectWindow.X;
int y = rectWindow.Y + (rectCaption.IsEmpty ? 0 : rectCaption.Height) +
(DockState == DockState.Document ? rectTabStrip.Height : 0);
int width = rectWindow.Width;
int height = rectWindow.Height - rectCaption.Height - rectTabStrip.Height;
return new Rectangle(x, y, width, height);
}
}
private DockContentCollection m_contents;
public DockContentCollection Contents
{
get { return m_contents; }
}
public int CountOfVisibleContents
{
get
{
int count = 0;
foreach (DockContent content in Contents)
{
if (!content.IsHidden)
count ++;
}
return count;
}
}
protected override Size DefaultSize
{
get { return Size.Empty; }
}
private DockPanel m_dockPanel;
public DockPanel DockPanel
{
get { return m_dockPanel; }
}
protected bool HasCaption
{
get
{
if (DockState == DockState.Document ||
DockState == DockState.Hidden ||
DockState == DockState.Unknown ||
(DockState == DockState.Float && FloatWindow.DisplayingList.Count <= 1))
return false;
else
return true;
}
}
private bool m_isActivated = false;
public bool IsActivated
{
get { return m_isActivated; }
}
internal void SetIsActivated(bool value)
{
if (m_isActivated == value)
return;
m_isActivated = value;
if (DockState != DockState.Document)
Invalidate();
OnIsActivatedChanged(EventArgs.Empty);
}
private bool m_isActiveDocumentPane = false;
public bool IsActiveDocumentPane
{
get { return m_isActiveDocumentPane; }
}
internal void SetIsActiveDocumentPane(bool value)
{
if (m_isActiveDocumentPane == value)
return;
m_isActiveDocumentPane = value;
if (DockState == DockState.Document)
Refresh();
OnIsActiveDocumentPaneChanged(EventArgs.Empty);
}
public bool IsDockStateValid(DockState dockState)
{
foreach (DockContent content in Contents)
if (!content.IsDockStateValid(dockState))
return false;
return true;
}
public bool IsAutoHide
{
get { return DockHelper.IsDockStateAutoHide(DockState); }
}
private bool m_isHidden = false;
public bool IsHidden
{
get { return m_isHidden; }
set
{
if (m_isHidden == value)
return;
m_isHidden = value;
SetDockState();
}
}
private DockPaneSplitter m_splitter;
internal DockPaneSplitter Splitter
{
get { return m_splitter; }
}
protected DockPaneAppearance Appearance
{
get { return (DockState == DockState.Document) ? DockPaneAppearance.Document : DockPaneAppearance.ToolWindow; }
}
private Rectangle TabStripRectangle
{
get { return GetTabStripRectangle(); }
}
protected virtual Rectangle DisplayingRectangle
{
get { return ClientRectangle; }
}
public void Activate()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?