⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabstub.cs

📁 Magic Library 1.7,有说明文档
💻 CS
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
//  (c) Crownwood Consulting Limited 2002
//  All rights reserved. The software and associated documentation
//  supplied hereunder are the proprietary information of Crownwood Consulting
//	Limited, Haxey, North Lincolnshire, England and are supplied subject to
//	licence terms.
//
//  Magic Version 1.7 	www.dotnetmagic.com
// *****************************************************************************

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using Microsoft.Win32;
using Crownwood.Magic.Win32;
using Crownwood.Magic.Common;
using Crownwood.Magic.Controls;
using Crownwood.Magic.Collections;

namespace Crownwood.Magic.Docking
{
    [ToolboxItem(false)]
    public class TabStub : UserControl
    {
		private class DrawTab
		{
			protected int _index;
			protected Rectangle _drawRect;
            protected Crownwood.Magic.Controls.TabPage _tabPage;

			public DrawTab(Crownwood.Magic.Controls.TabPage tabPage, Rectangle drawRect, int index)
			{
				_index = index;
				_tabPage = tabPage;
				_drawRect = drawRect;
			}

			public Crownwood.Magic.Controls.TabPage TabPage  { get { return _tabPage; } }
            public Rectangle DrawRect                        { get { return _drawRect; } }
			public int Index                                 { get { return _index; } }
		}

        // Class constants
		protected static int _imageGap = 3;
		protected static int _imageGaps = 6;
        protected static int _imageVector = 16;
        protected static int _beginGap = 2;
        protected static int _endGap = 8;
        protected static int _sideGap = 2;
		protected static int _hoverInterval = 500;

		// Instance fields
		protected Edge _edge;
		protected int _hoverOver;
		protected int _hoverItem;
		protected int _selectedIndex;
    	protected bool _defaultFont;
		protected bool _defaultColor;
		protected Color _backIDE;
        protected Timer _hoverTimer;
        protected TabPageCollection _tabPages;
		protected WindowContentTabbed _wct;
		protected ArrayList _drawTabs;
        protected VisualStyle _style;

        public delegate void TabStubIndexHandler(TabStub sender, int pageIndex);
        public delegate void TabStubHandler(TabStub sender);

        // Exposed events
        public event TabStubIndexHandler PageClicked;
        public event TabStubIndexHandler PageOver;
        public event TabStubHandler PagesLeave;

		public TabStub(VisualStyle style)
		{
			// Default state
			_wct = null;
			_style = style;
            _hoverOver = -1;
            _hoverItem = -1;
            _selectedIndex = -1;
            _defaultFont = true;
			_defaultColor = true;
			_edge = Edge.None;
			_drawTabs = new ArrayList();
            _tabPages = new TabPageCollection();
            base.Font = SystemInformation.MenuFont;

            // Hookup to collection events
            _tabPages.Cleared += new CollectionClear(OnClearedPages);
            _tabPages.Inserted += new CollectionChange(OnInsertedPage);
            _tabPages.Removing += new CollectionChange(OnRemovingPage);
            _tabPages.Removed += new CollectionChange(OnRemovedPage);

            // Need notification when the MenuFont is changed
            Microsoft.Win32.SystemEvents.UserPreferenceChanged += new 
                UserPreferenceChangedEventHandler(OnPreferenceChanged);

			// Default default colors
			DefineBackColor(SystemColors.Control);

			// Create the Timer for handling hovering over items
			_hoverTimer = new Timer();
			_hoverTimer.Interval = _hoverInterval;
			_hoverTimer.Tick += new EventHandler(OnTimerExpire);
		}

        protected override void Dispose(bool disposing)
        {
            if(disposing)
            {
                // Remove notifications
                Microsoft.Win32.SystemEvents.UserPreferenceChanged -= new 
                    UserPreferenceChangedEventHandler(OnPreferenceChanged);
            }
            base.Dispose(disposing);
        }

        public TabPageCollection TabPages
        {
            get { return _tabPages; }

            set
            {
                _tabPages.Clear();
                _tabPages = value;
            }
        }

		public Edge Edging
		{
			get { return _edge; }

			set
			{
				if (value != _edge)
				{
					_edge = value;
					ResizeControl();
					Recalculate();
					Invalidate();
				}
		    }
		}

		public int SelectedIndex
		{
			get { return _selectedIndex; }

			set
			{
				if (value != _selectedIndex)
				{
					_selectedIndex = value;
					Recalculate();
					Invalidate();
				}
			}
		}

        public override Font Font
        {
            get { return base.Font; }

            set
            {
				if (value != null)
				{
					if (value != base.Font)
					{
						_defaultFont = (value == SystemInformation.MenuFont);

						base.Font = value;
						ResizeControl();
						Recalculate();
						Invalidate();
					}
				}
            }
        }

        public override Color BackColor
        {
            get { return base.BackColor; }

            set
            {
                if (this.BackColor != value)
                {
                    _defaultColor = (value == SystemColors.Control);
					DefineBackColor(value);
                    Invalidate();
                }
            }
        }

        public WindowContentTabbed WindowContentTabbed
        {
            get { return _wct; }
            set { _wct = value; }
        }
        
		public virtual void OnPageClicked(int pageIndex)
		{
            // Has anyone registered for the event?
			if (PageClicked != null)
				PageClicked(this, pageIndex);
		}

		public virtual void OnPageOver(int pageIndex)
		{
            // Has anyone registered for the event?
			if (PageOver != null)
				PageOver(this, pageIndex);
		}

        public virtual void OnPagesLeave()
        {
            // Has anyone registered for the event?
            if (PagesLeave != null)
                PagesLeave(this);
        }
        
        public void PropogateNameValue(PropogateName name, object value)
        {
            switch(name)
            {
                case PropogateName.BackColor:
                    this.BackColor = (Color)value;
                    Invalidate();
                    break;
                case PropogateName.InactiveTextColor:
                    this.ForeColor = (Color)value;
                    Invalidate();
                    break;
                case PropogateName.CaptionFont:
                    this.Font = (Font)value;
                    break;
            }
            
            // Pass onto the contained WCT
            _wct.PropogateNameValue(name, value);
        }
                
        protected void DefineBackColor(Color backColor)
		{
			base.BackColor = backColor;
			
            _backIDE = ColorHelper.TabBackgroundFromBaseColor(backColor);
		}

		protected void OnPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
		{
			// Are we using the default menu or a user defined value?
			if (_defaultFont)
			{
				base.Font = SystemInformation.MenuFont;
				ResizeControl();
				Recalculate();
				Invalidate();
			}
		}

		protected override void OnSystemColorsChanged(EventArgs e)
		{
			// If still using the Default color when we were created
			if (_defaultColor)
			{
				this.BackColor = SystemColors.Control;
				Invalidate();
			}

			base.OnSystemColorsChanged(e);
		}

        protected void OnClearedPages()
        {
            // Cancel any hover selection
            CancelHoverItem();

			// Cancel any current selection
			_selectedIndex = -1;

			ResizeControl();
			Recalculate();
			Invalidate();
		}
		
        protected void OnInsertedPage(int index, object value)
		{
			// If no page is currently selected
			if (_selectedIndex == -1)
			{
				// Then make the inserted page selected
				_selectedIndex = index;
			}

			ResizeControl();
			Recalculate();
			Invalidate();
		}

        protected void OnRemovingPage(int index, object value)
        {
            // Removed page involved in hover calculations?
            if ((_hoverOver == index) || (_hoverItem == index))
                CancelHoverItem();
        
            // Removing the last page?
            if (_tabPages.Count == 1)
            {
                // Get rid of any selection
                _selectedIndex = -1;
            }
            else
            {
                // If removing a page before the selected one...
			    if (index < _selectedIndex)
			    {
                    // ...then the selected index must be decremented to match
					_selectedIndex--;
			    }
			    else
			    {
			        // If the selected page is the last one then...
			        if (_selectedIndex == (_tabPages.Count-1))
			        {
			            // Must reduce selected index
                        _selectedIndex--;
                    }
			    }
	        }
        }

        protected void OnRemovedPage(int index, object value)
        {
			ResizeControl();
			Recalculate();
			Invalidate();
		}

        protected void CancelHoverItem()
        {
            // Currently timing a hover change?
            if (_hoverOver != -1)
            {
                // Prevent timer from expiring
                _hoverTimer.Stop();
                
                // No item being timed
                _hoverOver = -1;
            }

            // Any current hover item?
            if (_hoverItem != -1)
            {
                // No item is being hovered
                _hoverItem = -1;
		        
                // Generate event for end of hover
                OnPagesLeave();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
			// Create a point representing current mouse position
			Point mousePos = new Point(e.X, e.Y);

			int index = 0;
			int count = _drawTabs.Count;

			// Search each draw cell
			for(; index<count; index++)
			{
				DrawTab dt = _drawTabs[index] as DrawTab;

				// Is mouse over this cell?
				if (dt.DrawRect.Contains(mousePos))
				{
					// If the mouse is not over the hover item
					if (_hoverItem != dt.Index)
					{
					    // And we are not already timing this change in hover
					    if (_hoverOver != dt.Index)
					    {
					        // Start timing the hover change
						    _hoverTimer.Start();
						    
						    // Remember which item we are timing
						    _hoverOver = dt.Index;
				        }
					}

    				break;
				}
			}

			// Failed to find an item?
			if (index == count)
			{
				// If we have a hover item or timing a hover change
				if ((_hoverOver != -1) || (_hoverItem != -1))
				{
				    // Stop any timing
				    CancelHoverItem();
				}
			}

			base.OnMouseMove(e);
		}

        protected override void OnMouseLeave(EventArgs e)
        {
            // Remove any hover state
            CancelHoverItem();
    
			base.OnMouseLeave(e);
		}

		protected void OnTimerExpire(object sender, EventArgs e)
		{
		    // Prevent the timer from firing again
			_hoverTimer.Stop();

            // A change in hover still valid?
            if (_hoverItem != _hoverOver)
            {
                // This item becomes the current hover item
                _hoverItem = _hoverOver;
                
                // No longer in a timing state
                _hoverOver = -1;

			    // Do we need a change in selection?
			    if (_selectedIndex != _hoverItem)
			    {
				    // Change selection and redraw
				    _selectedIndex = _hoverItem;

⌨️ 快捷键说明

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