dockpane.cs

来自「全功能c#编译器」· CS 代码 · 共 1,899 行 · 第 1/4 页

CS
1,899
字号

			using (Graphics g = CreateGraphics())
			{
				SizeF sizeText;
				if (content == ActiveContent && IsActiveDocumentPane)
				{
					using (Font boldFont = new Font(this.Font, FontStyle.Bold))
					{
						sizeText = g.MeasureString(content.Text, boldFont, MeasureDocumentTab.TabMaxWidth, StringFormatDocumentWindowTab);
					}
				}
				else
					sizeText = g.MeasureString(content.Text, Font, MeasureDocumentTab.TabMaxWidth, StringFormatDocumentWindowTab);

				return (int)sizeText.Width + 1 + MeasureDocumentTab.TextExtraWidth;
			}
		}

		private Rectangle GetTabRectangle(int index)
		{
			if (Appearance == DockPaneAppearance.ToolWindow)
				return GetTabRectangle_ToolWindow(index);
			else
				return GetTabRectangle_Document(index);
		}

		private Rectangle GetTabRectangle_ToolWindow(int index)
		{
			Rectangle rectTabStrip = GetTabStripRectangle();

			DockContent content = GetVisibleContent(index);
			return new Rectangle(content.TabX, rectTabStrip.Y, content.TabWidth, rectTabStrip.Height);
		}

		private Rectangle GetTabRectangle_Document(int index)
		{
			Rectangle rectTabStrip = GetTabStripRectangle();
			DockContent content = GetVisibleContent(index);

			return new Rectangle(content.TabX, rectTabStrip.Y + MeasureDocumentTab.TabGapTop,
				content.TabWidth, rectTabStrip.Height - MeasureDocumentTab.TabGapTop);
		}

		private Rectangle GetTabStripRectangle()
		{
			return GetTabStripRectangle(false);
		}

		private Rectangle GetTabStripRectangle(bool tabOnly)
		{
			if (Appearance == DockPaneAppearance.ToolWindow)
				return GetTabStripRectangle_ToolWindow(tabOnly);
			else
				return GetTabStripRectangle_Document(tabOnly);
		}

		protected Rectangle GetTabStripRectangle_ToolWindow(bool tabOnly)
		{
			if (CountOfVisibleContents <= 1 || IsAutoHide)
				return Rectangle.Empty;

			Rectangle rectWindow = DisplayingRectangle;

			int width = rectWindow.Width;
			int height = Math.Max(Font.Height, MeasureToolWindowTab.ImageHeight)
				+ MeasureToolWindowTab.ImageGapTop
				+ MeasureToolWindowTab.ImageGapBottom;
			int x = rectWindow.X;
			int y = rectWindow.Bottom - height;
			Rectangle rectCaption = CaptionRectangle;
			if (rectCaption.Contains(x, y))
				y = rectCaption.Y + rectCaption.Height;

			return new Rectangle(x, y, width, height);
		}

		protected Rectangle GetTabStripRectangle_Document(bool tabOnly)
		{
			if (CountOfVisibleContents == 0)
				return Rectangle.Empty;

			Rectangle rectWindow = DisplayingRectangle;
			int x = rectWindow.X;
			int y = rectWindow.Y;
			int width = rectWindow.Width;
			int height = Math.Max(Font.Height + MeasureDocumentTab.TabGapTop + MeasureDocumentTab.TextExtraHeight,
				ImageDocumentWindowCloseEnabled.Height + MeasureDocumentTab.ButtonGapTop + MeasureDocumentTab.ButtonGapBottom);

			if (tabOnly)
			{
				x += MeasureDocumentTab.TabGapLeft;
				width -= MeasureDocumentTab.TabGapLeft + 
					MeasureDocumentTab.TabGapRight +
					MeasureDocumentTab.ButtonGapRight +
					m_buttonDocumentWindowClose.Width +
					m_buttonScrollRight.Width +
					m_buttonScrollLeft.Width +
					2 * MeasureDocumentTab.ButtonGapBetween;
			}

			return new Rectangle(x, y, width, height);
		}

		private Region GetTestDropOutline(DockStyle dockStyle, int contentIndex)
		{
			if (Appearance == DockPaneAppearance.ToolWindow)
				return GetTestDropOutline_ToolWindow(dockStyle, contentIndex);
			else
				return GetTestDropOutline_Document(dockStyle, contentIndex);
		}

		private Region GetTestDropOutline_ToolWindow(DockStyle dock, int contentIndex)
		{
			int dragSize = MeasurePane.DragSize;

			if (dock != DockStyle.Fill)
			{
				Rectangle rect = DisplayingRectangle;
				if (dock == DockStyle.Right)
					rect.X += rect.Width / 2;
				if (dock == DockStyle.Bottom)
					rect.Y += rect.Height / 2;
				if (dock == DockStyle.Left || dock == DockStyle.Right)
					rect.Width -= rect.Width / 2;
				if (dock == DockStyle.Top || dock == DockStyle.Bottom)
					rect.Height -= rect.Height / 2;
				rect.Location = PointToScreen(rect.Location);

				return DrawHelper.CreateDragOutline(rect, dragSize);
			}
			else
			{
				Rectangle[] rects = new Rectangle[3];
				rects[2] = Rectangle.Empty;
				rects[0] = DisplayingRectangle;
				if (contentIndex != -1)
					rects[1] = GetTabRectangle(contentIndex);
				else
					rects[1] = Rectangle.Empty;

				if (!rects[1].IsEmpty)
				{
					rects[0].Height = rects[1].Top - rects[0].Top;
					rects[2].X = rects[1].X + dragSize;
					rects[2].Y = rects[1].Y - dragSize;
					rects[2].Width = rects[1].Width - 2 * dragSize;
					rects[2].Height = 2 * dragSize;
				}

				rects[0].Location = PointToScreen(rects[0].Location);
				rects[1].Location = PointToScreen(rects[1].Location);
				rects[2].Location = PointToScreen(rects[2].Location);
				return DrawHelper.CreateDragOutline(rects, dragSize);
			}
		}

		private Region GetTestDropOutline_Document(DockStyle dock, int contentIndex)
		{
			int dragSize = MeasurePane.DragSize;

			Rectangle[] rects = new Rectangle[3];

			rects[0] = DisplayingRectangle;
			if (dock == DockStyle.Right)
				rects[0].X += rects[0].Width / 2;
			else if (dock == DockStyle.Bottom)
				rects[0].Y += rects[0].Height / 2;
			if (dock == DockStyle.Left || dock == DockStyle.Right)
				rects[0].Width -= rects[0].Width / 2;
			else if (dock == DockStyle.Top || dock == DockStyle.Bottom)
				rects[0].Height -= rects[0].Height / 2;

			if (dock != DockStyle.Fill)
				rects[1] = new Rectangle(rects[0].X + MeasureDocumentTab.TabGapLeft, rects[0].Y,
					rects[0].Width - 2 * MeasureDocumentTab.TabGapLeft, GetTabStripRectangle().Height);
			else if (contentIndex != -1)
				rects[1] = GetTabRectangle(contentIndex);
			else
				rects[1] = GetTabRectangle(0);

			rects[0].Y = rects[1].Top + rects[1].Height;
			rects[0].Height -= rects[1].Height;
			rects[2] = new Rectangle(rects[1].X + dragSize, rects[0].Y - dragSize,
				rects[1].Width - 2 * dragSize, 2 * dragSize);

			rects[0].Location = PointToScreen(rects[0].Location);
			rects[1].Location = PointToScreen(rects[1].Location);
			rects[2].Location = PointToScreen(rects[2].Location);
			return DrawHelper.CreateDragOutline(rects, dragSize);
		}

		public DockContent GetVisibleContent(int index)
		{
			int currentIndex = -1;
			foreach (DockContent content in Contents)
			{
				if (!content.IsHidden)
					currentIndex ++;

				if (currentIndex == index)
					return content;
			}
			throw(new ArgumentOutOfRangeException());
		}

		public new void Hide()
		{
			IsHidden = true;
		}

		protected override void OnLayout(LayoutEventArgs e)
		{
			SetInertButtons();

			Rectangle rectContent = ContentRectangle;

			foreach (DockContent content in Contents)
			{
				content.Parent = this;
				content.Visible = (content == ActiveContent);
				content.Bounds = rectContent;
			}

			base.OnLayout(e);
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			HitTestResult hitTestResult = GetHitTest();
			HitTestArea hitArea = hitTestResult.HitArea;
			int index = hitTestResult.Index;
			string toolTip = string.Empty;

			base.OnMouseMove(e);

			if (hitArea == HitTestArea.TabStrip && index != -1)
			{
				Rectangle rectTab = GetTabRectangle(index);
				if (GetVisibleContent(index).ToolTipText != null)
					toolTip = GetVisibleContent(index).ToolTipText;
				else if (rectTab.Width < GetTabOriginalWidth(index))
					toolTip = GetVisibleContent(index).TabText;
			}

			if (m_toolTip.GetToolTip(this) != toolTip)
			{
				m_toolTip.Active = false;
				m_toolTip.SetToolTip(this, toolTip);
				m_toolTip.Active = true;
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			if (ActiveContent != null)
			{
				SetInertButtons();
				DrawCaption(e.Graphics);
				DrawTabStrip(e.Graphics);
				PerformLayout();
			}

			base.OnPaint(e);
		}

		public override void Refresh()
		{
			base.Refresh();
			if (DockHelper.IsDockStateAutoHide(DockState) && DockPanel != null)
			{
				DockPanel.Invalidate();
				DockPanel.PerformLayout();
			}
		}

		internal void RemoveContent(DockContent content)
		{
			if (!Contents.Contains(content))
				return;
			
			int index = GetIndexOfVisibleContents(content);

			Contents.Remove(content);
			content.SetParent(null);
			SetDockState();
			ValidateActiveContent();

			if (Contents.Count == 0)
				Dispose();

			if (index != -1)
				Invalidate();
		}

		private void ScrollLeft_Click(object sender, EventArgs e)
		{
			Rectangle rectTabStrip = GetTabStripRectangle(true);

			int index;
			for (index=0; index<CountOfVisibleContents; index++)
				if (GetTabRectangle(index).IntersectsWith(rectTabStrip))
					break;

			Rectangle rectTab = GetTabRectangle(index);
			if (rectTab.Left < rectTabStrip.Left)
				m_offset += rectTabStrip.Left - rectTab.Left;
			else if (index == 0)
				m_offset = 0;
			else
				m_offset += rectTabStrip.Left - GetTabRectangle(index - 1).Left;

			Invalidate();
		}
	
		private void ScrollRight_Click(object sender, EventArgs e)
		{
			Rectangle rectTabStrip = GetTabStripRectangle(true);

			int index;
			int countOfVisibleContents = CountOfVisibleContents;
			for (index=0; index<countOfVisibleContents; index++)
				if (GetTabRectangle(index).IntersectsWith(rectTabStrip))
					break;

			if (index + 1 < countOfVisibleContents)
			{
				m_offset -= GetTabRectangle(index + 1).Left - rectTabStrip.Left;
				CalculateTabs();
			}

			Rectangle rectLastTab = GetTabRectangle(countOfVisibleContents - 1);
			if (rectLastTab.Right < rectTabStrip.Right)
				m_offset += rectTabStrip.Right - rectLastTab.Right;

			Invalidate();
		}

		private void SetInertButtons()
		{
			if (DockState == DockState.Document)
			{
				m_buttonAutoHide.Visible =	m_buttonDockWindowClose.Visible = false;
				m_buttonScrollLeft.Visible = m_buttonScrollRight.Visible = m_buttonDocumentWindowClose.Visible = true;
			}
			else if (DockState == DockState.Float)
			{
				m_buttonAutoHide.Visible = m_buttonScrollLeft.Visible = m_buttonScrollRight.Visible = m_buttonDocumentWindowClose.Visible = false;
				m_buttonDockWindowClose.Visible = FloatWindow.DisplayingList.Count > 1;
			}
			else
			{
				m_buttonAutoHide.Visible =	m_buttonDockWindowClose.Visible = true;
				m_buttonScrollLeft.Visible = m_buttonScrollRight.Visible = m_buttonDocumentWindowClose.Visible = false;
			}
		
			// set the size and location for close and auto-hide buttons
			Rectangle rectCaption = CaptionRectangle;
			int buttonWidth = ImageDockWindowCloseEnabled.Width;
			int buttonHeight = ImageDockWindowCloseEnabled.Height;
			int height = rectCaption.Height - MeasureToolWindowCaption.ButtonGapTop - MeasureToolWindowCaption.ButtonGapBottom;
			if (buttonHeight < height)
			{
				buttonWidth = buttonWidth * (height / buttonHeight);
				buttonHeight = height;
			}
			m_buttonDockWindowClose.SuspendLayout();
			m_buttonAutoHide.SuspendLayout();
			Size buttonSize = new Size(buttonWidth, buttonHeight);
			m_buttonDockWindowClose.Size = m_buttonAutoHide.Size = buttonSize;
			int x = rectCaption.X + rectCaption.Width - 1 - MeasureToolWindowCaption.ButtonGapRight - m_buttonDockWindowClose.Width;
			int y = rectCaption.Y + MeasureToolWindowCaption.ButtonGapTop;
			Point point = m_buttonDockWindowClose.Location = new Point(x, y);
			point.Offset(-(m_buttonAutoHide.Width + MeasureToolWindowCaption.ButtonGapBetween), 0);
			m_buttonAutoHide.Location = point;
			m_buttonAutoHide.ImageEnabled = IsAutoHide ? ImageAutoHideYes : ImageAutoHideNo;
			m_buttonDockWindowClose.ResumeLayout();
			m_buttonAutoHide.ResumeLayout();
		}

		public void SetContentIndex(DockContent content, int index)
		{
			int oldIndex = Contents.IndexOf(content);
			if (oldIndex == -1)
				throw(new ArgumentException(ResourceHelper.GetString("DockPane.SetContentIndex.InvalidContent")));

			if (index < 0 || index > Contents.Count - 1)
				if (index != -1)
					throw(new ArgumentOutOfRangeException(ResourceHelper.GetString("DockPane.SetContentIndex.InvalidIndex")));
				
			if (oldIndex == index)
				return;
			if (oldIndex == Contents.Count - 1 && index == -1)
				return;

			Contents.Remove(content);
			if (index == -1)
				Contents.Add(content);
			else if (oldIndex < index)
				Contents.AddAt(content, index - 1);
			else
				Contents.AddAt(content, index);

			Refresh();
		}

		private void SetParent()
		{
			if (DockState == DockState.Unknown || DockState == DockState.Hidden)
			{
				SetParent(DockPanel.DummyControl);
				Splitter.Parent = DockPanel.DummyControl;
			}
			else if (DockState == DockState.Float)
			{
				SetParent(FloatWindow);
				Splitter.Parent = FloatWindow;
			}
			else if (DockHelper.IsDockStateAutoHide(DockState))
			{
				SetParent(DockPanel.AutoHideWindow);
				Splitter.Parent = DockPanel.DummyControl;
			}
			else
			{
				SetParent(DockPanel.DockWindows[DockState]);
				Splitter.Parent = this.Parent;
			}
		}

		private void SetParent(Control value)
		{
			if (Parent == value)
				return;

			Control oldParent = Parent;

			///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			/// Workaround for .Net Framework bug: removing control from Form may cause form
			/// unclosable. Set focus to another dummy control.
			///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			Form form = FindForm();
			if (ContainsFocus)
			{
				if (form is FloatWindow)
				{
					((FloatWindow)form).DummyControl.Focus();
					form.ActiveControl = ((FloatWindow)form).DummyControl;
				}
				else if (DockPanel != null)
				{
					DockPanel.DummyControl.Focus();
					if (form != null)
						form.ActiveControl = DockPanel.DummyControl;
				}
			}
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

			Parent = value;
			m_splitter.Parent = value;
		}

		public new void Show()
		{
			IsHidden = false;
			Activate();
		}

		internal void TestDrop(DragHandler dragHandler, Point pt)
		{
			if (DockState == DockState.Document)
				DockPanel.TestDrop(dragHandler, pt);

			if (dragHandler.DropTarget.DropTo != null)
				return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?