Advertisement
Click16

AbideTabControl

Oct 1st, 2013
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 33.82 KB | None | 0 0
  1. using Potential_Software.Win32;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Drawing.Design;
  8. using System.Drawing.Drawing2D;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.Design;
  11.  
  12. namespace Visual_Studio_Theme.Controls
  13. {
  14.     [Designer(typeof(AbideTabControlDesigner))]
  15.     public class AbideTabControl : Control
  16.     {
  17.         //Properties
  18.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  19.         public AbideTabPageCollection TabPages
  20.         {
  21.             get { return tabPages; }
  22.             protected set { tabPages = value; }
  23.         }
  24.         [Browsable(false)]
  25.         public AbideTabPage TabPage
  26.         {
  27.             get { if (selectedIndex == -1) { return null; } else { return TabPages[selectedIndex]; } }
  28.         }
  29.         [Browsable(false)]
  30.         public int SelectedIndex
  31.         {
  32.             get { return selectedIndex; }
  33.             set { selectedIndex = value; OnSelectedIndexChanged(new EventArgs()); }
  34.         }
  35.         [Browsable(false)]
  36.         int HighlightedIndex
  37.         {
  38.             get { return highlightedIndex; }
  39.             set { highlightedIndex = value; Refresh(); }
  40.         }
  41.  
  42.         //Events
  43.         public event TabPageAddedEventHandler TabPageAdded;
  44.         public event EventHandler TabPageChanged;
  45.         public event EventHandler TabPageRemoved;
  46.         public event EventHandler SelectedIndexChanged;
  47.  
  48.         //Fields
  49.         List<HitTestItem> tabAreas = new List<HitTestItem>();
  50.         List<HitTestItem> closeAreas = new List<HitTestItem>();
  51.         HitTestItem CloseItem = null;
  52.         HitTestItem ClientArea = null;
  53.         AbideTabPageCollection tabPages;
  54.         Image closeDisplayImg = new Bitmap(1, 1);
  55.         int selectedIndex = 0;
  56.         int highlightedIndex = -1;
  57.         int closeHighlightIndex = -1;
  58.         int closePressedIndex = -1;
  59.  
  60.         //Methods
  61.         public AbideTabControl()
  62.         {
  63.             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  64.             this.DoubleBuffered = true;
  65.             this.BackColor = Color.Transparent;
  66.             this.selectedIndex = -1;
  67.             base.Text = string.Empty;
  68.  
  69.             if (tabPages == null)
  70.                 this.tabPages = new AbideTabPageCollection(this);
  71.  
  72.             ClientArea = new HitTestItem(new Rectangle(3, 24, ClientSize.Width - 6, ClientSize.Height - 27), WinUser.HTCLIENT);
  73.             CloseItem = new HitTestItem(new Rectangle(Width - 23, 4, 20, 20), WinUser.HTCLIENT);
  74.         }
  75.  
  76.         //Overrides...
  77.         protected override void WndProc(ref Message m)
  78.         {
  79.             Point MousePt = Point.Empty;
  80.             Messages Msg = (Messages)m.Msg;
  81.  
  82.             if (DesignMode)
  83.             {
  84.                 switch (Msg)
  85.                 {
  86.                     case Messages.WM_NCHITTEST:
  87.                         //Set Default
  88.                         m.Result = (IntPtr)WinUser.HTCLIENT;
  89.  
  90.                         //Get Cursor Location...
  91.                         MousePt = PointToClient(new Point(m.LParam.ToInt32()));
  92.  
  93.                         //Check...
  94.                         if (CloseItem != null && CloseItem.HitRegion.IsVisible(MousePt) && CloseItem.Active)
  95.                         {
  96.                             m.Result = (IntPtr)CloseItem.TestReturn;
  97.                         }
  98.  
  99.                         if (ClientArea != null && ClientArea.HitRegion.IsVisible(MousePt) && ClientArea.Active)
  100.                         {
  101.                             m.Result = (IntPtr)ClientArea.TestReturn;
  102.                         }
  103.  
  104.                         foreach (HitTestItem Item in tabAreas)
  105.                         {
  106.                             if (Item.HitRegion.IsVisible(MousePt))
  107.                             {
  108.                                 m.Result = (IntPtr)Item.TestReturn;
  109.                             }
  110.                         }
  111.  
  112.                         foreach (HitTestItem Item in closeAreas)
  113.                         {
  114.                             if (Item.HitRegion.IsVisible(MousePt))
  115.                             {
  116.                                 m.Result = (IntPtr)Item.TestReturn;
  117.                             }
  118.                         }
  119.                         break;
  120.  
  121.                     default:
  122.                         base.WndProc(ref m);
  123.                         break;
  124.                 }
  125.                 return;
  126.             }
  127.  
  128.             switch (Msg)
  129.             {
  130.                 case Messages.WM_NCHITTEST:
  131.  
  132.                     //Set Default Result...
  133.                     m.Result = (IntPtr)WinUser.HTTRANSPARENT;
  134.  
  135.                     //Get Cursor Location...
  136.                     MousePt = PointToClient(new Point(m.LParam.ToInt32()));
  137.  
  138.                     //Check...
  139.                     if (CloseItem != null && CloseItem.HitRegion.IsVisible(MousePt) && CloseItem.Active)
  140.                     {
  141.                         m.Result = (IntPtr)CloseItem.TestReturn;
  142.                     }
  143.  
  144.                     if (ClientArea != null && ClientArea.HitRegion.IsVisible(MousePt) && ClientArea.Active)
  145.                     {
  146.                         m.Result = (IntPtr)ClientArea.TestReturn;
  147.                     }
  148.  
  149.                     foreach (HitTestItem Item in tabAreas)
  150.                     {
  151.                         if (Item.HitRegion.IsVisible(MousePt))
  152.                         {
  153.                             m.Result = (IntPtr)Item.TestReturn;
  154.                         }
  155.                     }
  156.                     break;
  157.  
  158.                 default:
  159.                     base.WndProc(ref m);
  160.                     break;
  161.             }
  162.         }
  163.         protected override void OnPaint(PaintEventArgs e)
  164.         {
  165.             //Prepare Brushes & Pens
  166.             Brush Edge = new SolidBrush(Color.FromArgb(0x00, 0x7a, 0xcc));
  167.             Brush BackColor = new SolidBrush(this.BackColor);
  168.  
  169.             //Clear Background
  170.             if (DesignMode)
  171.                 e.Graphics.Clear(this.BackColor);
  172.  
  173.             //Create Client Rectangle;
  174.             Rectangle ControlArea = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
  175.             Rectangle ContentArea = new Rectangle(0, 19, ClientSize.Width, ClientSize.Height - 19);
  176.             Rectangle InnerContentArea = new Rectangle(0, 21, ClientSize.Width, ClientSize.Height - 21);
  177.  
  178.             //Update Hit Area
  179.             ClientArea = new HitTestItem(ContentArea, WinUser.HTCLIENT);
  180.  
  181.             //Get Tab Count...
  182.             int TabCount = (Width - 30) / 120;
  183.             int VisibleTabCount = 0;
  184.  
  185.             if (tabPages.Count == 0 || TabCount == 0)
  186.                 CloseItem.Active = false;
  187.  
  188.             //Loop through each tab...
  189.             for (int i = 0; i < tabPages.Count; i++)
  190.             {
  191.                 if (i <= TabCount - 1)
  192.                 {
  193.                     int x = (i * 120);
  194.                     int y = 0;
  195.                     int width = 120;
  196.                     int height = 20;
  197.  
  198.                     OnPaintTab(new TabPaintEventArgs(e.Graphics, new Rectangle(x, y, width, height), i));
  199.  
  200.                     VisibleTabCount++;
  201.                 }
  202.             }
  203.  
  204.             //Fill
  205.             e.Graphics.FillRectangle(Edge, ContentArea);
  206.             e.Graphics.FillRectangle(BackColor, InnerContentArea);
  207.  
  208.             //Cleanup
  209.             Edge.Dispose();
  210.             BackColor.Dispose();
  211.  
  212.             //Default Painting...
  213.             base.OnPaint(e);
  214.         }
  215.         protected override void OnMouseMove(MouseEventArgs e)
  216.         {
  217.             base.OnMouseMove(e);
  218.  
  219.             for (int i = 0; i < closeAreas.Count; i++)
  220.             {
  221.                 if (closeAreas[i].HitRegion.IsVisible(e.Location) && closeAreas[i].Active)
  222.                 {
  223.                     if (this.closeHighlightIndex != i)
  224.                     {
  225.                         this.closeHighlightIndex = i;
  226.                         Refresh();
  227.                     }
  228.                     break;
  229.                 }
  230.                 else
  231.                 {
  232.                     if (this.closeHighlightIndex == i)
  233.                     {
  234.                         this.closeHighlightIndex = -1;
  235.                         Refresh();
  236.                     }
  237.                 }
  238.             }
  239.  
  240.             foreach (HitTestItem item in tabAreas)
  241.             {
  242.                 if (item.HitRegion.IsVisible(e.Location) && item.Active)
  243.                 {
  244.                     if (item.Tag != null && item.Tag is int)
  245.                     {
  246.                         int Index = (int)item.Tag;
  247.  
  248.                         if (HighlightedIndex != Index)
  249.                         {
  250.                             HighlightedIndex = Index;
  251.                         }
  252.                         break;
  253.                     }
  254.                 }
  255.             }
  256.         }
  257.         protected override void OnMouseLeave(EventArgs e)
  258.         {
  259.             base.OnMouseLeave(e);
  260.  
  261.             HighlightedIndex = -1;
  262.             closeHighlightIndex = -1;
  263.             closePressedIndex = -1;
  264.         }
  265.         protected override void OnMouseDown(MouseEventArgs e)
  266.         {
  267.             base.OnMouseDown(e);
  268.  
  269.             for (int i = 0; i < closeAreas.Count; i++)
  270.             {
  271.                 if (closeAreas[i].HitRegion.IsVisible(e.Location) && closeAreas[i].Active)
  272.                 {
  273.                     if (this.closePressedIndex != i)
  274.                     {
  275.                         this.closePressedIndex = i;
  276.                         Refresh();
  277.                     }
  278.                     break;
  279.                 }
  280.                 else
  281.                 {
  282.                     if (this.closePressedIndex == i)
  283.                     {
  284.                         this.closePressedIndex = -1;
  285.                         Refresh();
  286.                     }
  287.                 }
  288.             }
  289.  
  290.             foreach (HitTestItem item in tabAreas)
  291.             {
  292.                 if (item.HitRegion.IsVisible(e.Location) && item.Active)
  293.                 {
  294.                     if (item.Tag != null && item.Tag is int)
  295.                     {
  296.                         int Index = (int)item.Tag;
  297.  
  298.                         this.SelectedIndex = Index;
  299.                         break;
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.         protected override void OnMouseUp(MouseEventArgs e)
  305.         {
  306.             base.OnMouseUp(e);
  307.  
  308.             if (!DesignMode)
  309.             {
  310.                 for (int i = 0; i < closeAreas.Count; i++)
  311.                 {
  312.                     if (closeAreas[i].HitRegion.IsVisible(e.Location) && closeAreas[i].Active)
  313.                     {
  314.                         if (closeAreas[i].Tag != null && closeAreas[i].Tag is int)
  315.                         {
  316.                             closeAreas.RemoveAt(i);
  317.                             tabAreas.RemoveAt(i);
  318.                             tabPages.RemoveAt(i);
  319.                         }
  320.                     }
  321.                 }
  322.             }
  323.  
  324.             closeHighlightIndex = -1;
  325.             closePressedIndex = -1;
  326.  
  327.             Refresh();
  328.         }
  329.         protected override void OnSizeChanged(EventArgs e)
  330.         {
  331.             base.OnSizeChanged(e);
  332.  
  333.             //Get Tab Count...
  334.             int TabCount = (Width - 30) / 120;
  335.  
  336.             for (int i = TabCount; i < tabAreas.Count; i++)
  337.             {
  338.                 tabAreas[i].Active = false;
  339.             }
  340.  
  341.             if (selectedIndex + 1 > TabCount)
  342.                 selectedIndex = TabCount - 1;
  343.  
  344.             if (selectedIndex < 0 && this.tabPages.Count > 0)
  345.                 selectedIndex = 0;
  346.  
  347.             if (SelectedIndexChanged != null)
  348.                 SelectedIndexChanged(this, e);
  349.  
  350.             //Refresh...
  351.             Refresh();
  352.         }
  353.         protected override void Dispose(bool disposing)
  354.         {
  355.             base.Dispose(disposing);
  356.         }
  357.         protected override void OnControlRemoved(ControlEventArgs e)
  358.         {
  359.             base.OnControlRemoved(e);
  360.  
  361.             if (e.Control is AbideTabPage)
  362.             {
  363.                 tabPages.Remove((AbideTabPage)e.Control);
  364.             }
  365.         }
  366.  
  367.         //Virtuals...
  368.         protected virtual void OnTabPageAdded(TabPageAddedEventArgs e)
  369.         {
  370.             if (TabPageAdded != null)
  371.                 TabPageAdded(this, e);
  372.  
  373.             //Set Index
  374.             selectedIndex = tabPages.IndexOf(e.TabPage);
  375.  
  376.             //Change Selected Index
  377.             OnSelectedIndexChanged(new EventArgs());
  378.  
  379.             //Reference Rectangle
  380.             Rectangle InnerContentArea = new Rectangle(0, 21, ClientSize.Width, ClientSize.Height - 21);
  381.  
  382.             //Add Panel...
  383.             SuspendLayout();
  384.  
  385.             AbideTabPage TabCtrl = tabPages[selectedIndex];
  386.             TabCtrl.Size = new Size(InnerContentArea.Width - 2, InnerContentArea.Height - 2);
  387.             TabCtrl.Location = new Point(InnerContentArea.X + 1, InnerContentArea.Y + 1);
  388.             TabCtrl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  389.             TabCtrl.Hide();
  390.  
  391.             Controls.Add(TabCtrl);
  392.  
  393.             if (selectedIndex == Controls.IndexOf(TabCtrl))
  394.                 TabCtrl.Show();
  395.  
  396.             ResumeLayout();
  397.  
  398.             //Check Count
  399.             if (tabPages.Count > 0)
  400.             {
  401.                 SelectedIndex = tabPages.IndexOf(e.TabPage);
  402.             }
  403.  
  404.             //Refresh
  405.             this.Refresh();
  406.         }
  407.         protected virtual void OnTabPageRemoved(EventArgs e)
  408.         {
  409.             //Trigger Event
  410.             if (TabPageRemoved != null)
  411.                 TabPageRemoved(this, e);
  412.  
  413.             //Edit Selection Index
  414.             if (selectedIndex >= tabPages.Count)
  415.                 selectedIndex = tabPages.Count - 1;
  416.  
  417.             //Change Selected Index...
  418.             OnSelectedIndexChanged(e);
  419.         }
  420.         protected virtual void OnTabPageChanged(EventArgs e)
  421.         {
  422.             if (TabPageChanged != null)
  423.                 TabPageChanged(this, e);
  424.  
  425.             //Refresh
  426.             this.Refresh();
  427.         }
  428.         protected virtual void OnPaintTab(TabPaintEventArgs e)
  429.         {
  430.             //Prepare Brushes & Pens
  431.             Brush Active = new SolidBrush(Color.FromArgb(0x00, 0x7a, 0xcc));
  432.             Brush Inactive = new SolidBrush(Color.FromArgb(0x2d, 0x2d, 0x30));
  433.             Brush Hot = new SolidBrush(Color.FromArgb(0x1c, 0x97, 0xea));
  434.             Brush TextBrush = new SolidBrush(Color.White);
  435.  
  436.             //Prepare Gfx
  437.             e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  438.  
  439.             //Get rounded Rectangle...
  440.             Rectangle TabRect = e.ClipRectangle;
  441.             Rectangle CloseRect = new Rectangle(TabRect.X + (TabRect.Width - 18), 3, 15, 15);
  442.  
  443.             //Fill...
  444.             if (e.TabIndex == selectedIndex)
  445.             {
  446.                 e.Graphics.FillRectangle(Active, TabRect);
  447.             }
  448.             else if (e.TabIndex == highlightedIndex)
  449.             {
  450.                 e.Graphics.FillRectangle(Hot, TabRect);
  451.             }
  452.             else
  453.             {
  454.                 e.Graphics.FillRectangle(Inactive, TabRect);
  455.             }
  456.  
  457.             //Draw Close Image
  458.             if (!DesignMode)
  459.             {
  460.                 if (e.TabIndex == selectedIndex)
  461.                 {
  462.                     if (closePressedIndex == e.TabIndex)
  463.                         e.Graphics.DrawImage(Properties.Resources.CloseSelected_Down, CloseRect);
  464.                     else if (closeHighlightIndex == e.TabIndex)
  465.                         e.Graphics.DrawImage(Properties.Resources.CloseSelected_Hot, CloseRect);
  466.                     else
  467.                         e.Graphics.DrawImage(Properties.Resources.CloseSelected_Normal, CloseRect);
  468.                 }
  469.                 else if (e.TabIndex == highlightedIndex)
  470.                 {
  471.                     if (closePressedIndex == e.TabIndex)
  472.                         e.Graphics.DrawImage(Properties.Resources.CloseHot_Pressed, CloseRect);
  473.                     else if (closeHighlightIndex == e.TabIndex)
  474.                         e.Graphics.DrawImage(Properties.Resources.CloseHot_Hot, CloseRect);
  475.                     else
  476.                         e.Graphics.DrawImage(Properties.Resources.CloseHot_Normal, CloseRect);
  477.                 }
  478.             }
  479.  
  480.             //Prepare
  481.             RectangleF TextRect = new RectangleF(e.ClipRectangle.X + 4, e.ClipRectangle.Y, e.ClipRectangle.Width - 18, e.ClipRectangle.Height);
  482.             StringFormat format = new StringFormat();
  483.             format.LineAlignment = StringAlignment.Center;
  484.             format.Alignment = StringAlignment.Near;
  485.  
  486.             //Measure String
  487.             Size textSize = TextRenderer.MeasureText(tabPages[e.TabIndex].Text, this.Font);
  488.             string Text = string.Empty;
  489.             if (textSize.Width < 120) { Text = tabPages[e.TabIndex].Text; } else { Text = tabPages[e.TabIndex].Text.Substring(0, 12) + "..."; }
  490.  
  491.             //Draw
  492.             e.Graphics.DrawString(Text, Font, TextBrush, TextRect, format);
  493.  
  494.             //Cleanup
  495.             format.Dispose();
  496.             Active.Dispose();
  497.             Inactive.Dispose();
  498.             Active.Dispose();
  499.             Hot.Dispose();
  500.  
  501.             //Create Close HitTest...
  502.             HitTestItem closeItem = new HitTestItem(CloseRect, WinUser.HTCLIENT);
  503.             closeItem.Tag = e.TabIndex;
  504.             closeItem.Label = tabPages[e.TabIndex].Text;
  505.             if (closeAreas.Count > e.TabIndex)
  506.             {
  507.                 closeAreas[e.TabIndex] = closeItem;
  508.             }
  509.             else
  510.             {
  511.                 closeAreas.Add(closeItem);
  512.             }
  513.  
  514.             //Create Tab HitTest...
  515.             HitTestItem tabItem = new HitTestItem(TabRect, WinUser.HTCLIENT);
  516.             tabItem.Tag = e.TabIndex;
  517.             tabItem.Label = tabPages[e.TabIndex].Text;
  518.             if (tabAreas.Count > e.TabIndex)
  519.             {
  520.                 tabAreas[e.TabIndex] = tabItem;
  521.             }
  522.             else
  523.             {
  524.                 tabAreas.Add(tabItem);
  525.             }
  526.         }
  527.         protected virtual void OnSelectedIndexChanged(EventArgs e)
  528.         {
  529.             //Redraw
  530.             Refresh();
  531.  
  532.             //Suspend
  533.             SuspendLayout();
  534.  
  535.             //Check...
  536.             HideAll();
  537.             Show(selectedIndex);
  538.  
  539.             //Resume
  540.             ResumeLayout();
  541.  
  542.             //Event
  543.             if (SelectedIndexChanged != null)
  544.                 SelectedIndexChanged(this, e);
  545.         }
  546.        
  547.         //Functions...
  548.         public void Clear()
  549.         {
  550.             //Clear All Pages
  551.             Controls.Clear();
  552.         }
  553.         private void HideAll()
  554.         {
  555.             for (int i = 0; i < Controls.Count; i++)
  556.             {
  557.                 Controls[i].Hide();
  558.             }
  559.         }
  560.         private void Show(int Index)
  561.         {
  562.             if (Index > -1 && Index < Controls.Count)
  563.             {
  564.                 Controls[Index].Show();
  565.             }
  566.         }
  567.  
  568.         //Nested Types
  569.         public new class ControlCollection : Control.ControlCollection
  570.         {
  571.             AbideTabControl TabControlOwner
  572.             {
  573.                 get { return this.Owner as AbideTabControl; }
  574.             }
  575.  
  576.             public ControlCollection(AbideTabControl owner)
  577.                 : base(owner) { }
  578.  
  579.             public override void Add(Control value)
  580.             {
  581.                 if (value.GetType() == typeof(object))
  582.                 {
  583.                     TabControlOwner.tabPages.Add(value as AbideTabPage);
  584.                 }
  585.                 else
  586.                 {
  587.                     base.Add(value);
  588.                 }
  589.             }
  590.             public override void Remove(Control value)
  591.             {
  592.                 if (value.GetType() == typeof(object))
  593.                 {
  594.                     TabControlOwner.tabPages.Remove(value as AbideTabPage);
  595.                 }
  596.                 else
  597.                 {
  598.                     base.Add(value);
  599.                 }
  600.             }
  601.         }
  602.         internal class HitTestItem : IDisposable
  603.         {
  604.             public string Label
  605.             {
  606.                 get { return label; }
  607.                 set { label = value; }
  608.             }
  609.  
  610.             public object Tag
  611.             {
  612.                 get;
  613.                 set;
  614.             }
  615.  
  616.             public GraphicsPath HitRegion
  617.             {
  618.                 get { return hitRegion; }
  619.             }
  620.  
  621.             public int TestReturn
  622.             {
  623.                 get { return hitResult; }
  624.             }
  625.  
  626.             public bool Active
  627.             {
  628.                 get { return active; }
  629.                 set { active = value; }
  630.             }
  631.  
  632.             string label = string.Empty;
  633.             bool active = true;
  634.             GraphicsPath hitRegion = null;
  635.             int hitResult = WinUser.HTCLIENT;
  636.  
  637.             public HitTestItem(GraphicsPath Region, int ReturnValue)
  638.             {
  639.                 this.hitRegion = Region;
  640.                 this.hitResult = ReturnValue;
  641.             }
  642.  
  643.             public HitTestItem(Rectangle Region, int ReturnValue)
  644.             {
  645.                 this.hitRegion = new GraphicsPath();
  646.                 hitRegion.AddRectangle(Region);
  647.                 this.hitResult = ReturnValue;
  648.             }
  649.  
  650.             public override string ToString()
  651.             {
  652.                 return label;
  653.             }
  654.  
  655.             public void Dispose()
  656.             {
  657.                 hitRegion.Dispose();
  658.             }
  659.         }
  660.         public class AbideTabPageCollection : IList, ICollection, IEnumerable
  661.         {
  662.             ArrayList InnerList;
  663.             AbideTabControl owner;
  664.  
  665.             public AbideTabPageCollection(AbideTabControl Owner)
  666.             {
  667.                 this.InnerList = new ArrayList(new List<AbideTabPage>());
  668.                 this.owner = Owner;
  669.             }
  670.  
  671.             bool IList.IsReadOnly
  672.             {
  673.                 get { return InnerList.IsReadOnly; }
  674.             }
  675.             public bool IsReadOnly
  676.             {
  677.                 get { return InnerList.IsReadOnly; }
  678.             }
  679.  
  680.             object IList.this[int index]
  681.             {
  682.                 get { return InnerList[index]; }
  683.                 set { InnerList[index] = value; owner.OnTabPageChanged(new EventArgs()); }
  684.             }
  685.             public AbideTabPage this[int index]
  686.             {
  687.                 get { return InnerList[index] as AbideTabPage; }
  688.                 set { InnerList[index] = value; }
  689.             }
  690.  
  691.             int IList.Add(object value)
  692.             {
  693.                 return InnerList.Add(value);
  694.             }
  695.             public void Add(AbideTabPage page)
  696.             {
  697.                 InnerList.Add(page);
  698.                 owner.OnTabPageAdded(new TabPageAddedEventArgs(page));
  699.             }
  700.             public void Add(string text)
  701.             {
  702.                 InnerList.Add(new AbideTabPage(text));
  703.                 owner.OnTabPageAdded(new TabPageAddedEventArgs(new AbideTabPage(text)));
  704.             }
  705.             public void AddRange(AbideTabPage[] pages)
  706.             {
  707.                 InnerList.AddRange(pages);
  708.                 foreach (AbideTabPage page in pages)
  709.                 {
  710.                     owner.OnTabPageAdded(new TabPageAddedEventArgs(page));
  711.                 }
  712.             }
  713.  
  714.             void IList.Clear()
  715.             {
  716.                 InnerList.Clear();
  717.             }
  718.             public void Clear()
  719.             {
  720.                 InnerList.Clear();
  721.             }
  722.  
  723.             bool IList.Contains(object value)
  724.             {
  725.                 return InnerList.Contains(value);
  726.             }
  727.             public bool Contains(AbideTabPage page)
  728.             {
  729.                 return InnerList.Contains(page);
  730.             }
  731.  
  732.             IEnumerator IEnumerable.GetEnumerator()
  733.             {
  734.                 return InnerList.GetEnumerator();
  735.             }
  736.             public IEnumerator GetEnumerator()
  737.             {
  738.                 return InnerList.GetEnumerator();
  739.             }
  740.  
  741.             int IList.IndexOf(object value)
  742.             {
  743.                 return InnerList.IndexOf(value);
  744.             }
  745.             public int IndexOf(AbideTabPage page)
  746.             {
  747.                 return InnerList.IndexOf(page);
  748.             }
  749.  
  750.             void IList.Insert(int index, object value)
  751.             {
  752.                 InnerList.Insert(index, value);
  753.             }
  754.             public void Insert(int index, AbideTabPage page)
  755.             {
  756.                 InnerList.Insert(index, page);
  757.             }
  758.             public void Insert(int index, string text)
  759.             {
  760.                 InnerList.Insert(index, new AbideTabPage(text));
  761.             }
  762.  
  763.             bool IList.IsFixedSize
  764.             {
  765.                 get { return InnerList.IsFixedSize; }
  766.             }
  767.  
  768.             void IList.Remove(object value)
  769.             {
  770.                 InnerList.Remove(value);
  771.             }
  772.             public bool Remove(AbideTabPage page)
  773.             {
  774.                 if (InnerList.Contains(page))
  775.                 {
  776.                     InnerList.Remove(page);
  777.                     owner.Controls.Remove(page);
  778.                     owner.OnTabPageRemoved(new EventArgs());
  779.                     return true;
  780.                 }
  781.                 else
  782.                     return false;
  783.             }
  784.  
  785.             void IList.RemoveAt(int index)
  786.             {
  787.                 InnerList.RemoveAt(index);
  788.             }
  789.             public void RemoveAt(int index)
  790.             {
  791.                 InnerList.RemoveAt(index);
  792.                 owner.OnTabPageRemoved(new EventArgs());
  793.             }
  794.  
  795.             int ICollection.Count
  796.             {
  797.                 get { return InnerList.Count; }
  798.             }
  799.             public int Count
  800.             {
  801.                 get { return InnerList.Count; }
  802.             }
  803.  
  804.             void ICollection.CopyTo(Array array, int index)
  805.             {
  806.                 InnerList.CopyTo(array, index);
  807.             }
  808.             bool ICollection.IsSynchronized
  809.             {
  810.                 get { return InnerList.IsSynchronized; }
  811.             }
  812.             object ICollection.SyncRoot
  813.             {
  814.                 get { return InnerList.SyncRoot; }
  815.             }
  816.         }
  817.         internal class AbideTabControlDesigner : ControlDesigner
  818.         {
  819.             protected override bool GetHitTest(Point point)
  820.             {
  821.                 AbideTabControl Control = (AbideTabControl)this.Control;
  822.                 Point LocalPoint = Control.PointToClient(point);
  823.  
  824.                 bool ReturnValue = false;
  825.  
  826.                 foreach (HitTestItem Item in Control.tabAreas)
  827.                 {
  828.                     if (Item.HitRegion.IsVisible(LocalPoint))
  829.                     {
  830.                         ReturnValue = true;
  831.                     }
  832.                 }
  833.  
  834.                 if (ReturnValue == false) { Control.highlightedIndex = -1; Control.Refresh(); }
  835.  
  836.                 return ReturnValue;
  837.             }
  838.         }
  839.  
  840.         //Delegates
  841.         public delegate void TabPageAddedEventHandler(object sender, TabPageAddedEventArgs e);
  842.     }
  843.  
  844.     [ToolboxItem(false), Designer(typeof(AbideTabPageDesigner))]
  845.     public class AbideTabPage : Panel
  846.     {
  847.         /// <summary>
  848.         /// Gets or sets the text for this tab.
  849.         /// </summary>
  850.         [Browsable(true)]
  851.         [Category("Appearance")]
  852.         public override string Text
  853.         {
  854.             get { return base.Text; }
  855.             set { base.Text = value; }
  856.         }
  857.         [Browsable(false)]
  858.         public new Size Size
  859.         {
  860.             get { return base.Size; }
  861.             set { base.Size = value; }
  862.         }
  863.         [Browsable(false)]
  864.         public new Point Location
  865.         {
  866.             get { return base.Location; }
  867.             set { base.Location = value; }
  868.         }
  869.         [Browsable(false)]
  870.         public new AnchorStyles Anchor
  871.         {
  872.             get { return base.Anchor; }
  873.             set { base.Anchor = value; }
  874.         }
  875.         [Browsable(false)]
  876.         public new DockStyle Dock
  877.         {
  878.             get { return base.Dock; }
  879.             set { base.Dock = value; }
  880.         }
  881.  
  882.         public event EventHandler Closing;
  883.  
  884.         public AbideTabPage()
  885.             : base()
  886.         {
  887.             this.DoubleBuffered = true;
  888.             this.SetStyle(ControlStyles.FixedHeight, true);
  889.             this.SetStyle(ControlStyles.FixedWidth, true);
  890.         }
  891.         public AbideTabPage(string Text)
  892.             : base()
  893.         {
  894.             this.Text = Text;
  895.             this.DoubleBuffered = true;
  896.             this.SetStyle(ControlStyles.FixedHeight, true);
  897.             this.SetStyle(ControlStyles.FixedWidth, true);
  898.         }
  899.  
  900.         protected override void InitLayout()
  901.         {
  902.             if (this.Parent.GetType() != typeof(AbideTabControl))
  903.                 throw new ArgumentException(string.Format("{0} cannot be added to {1}. It can only be added to {2}",
  904.                     typeof(AbideTabPage).ToString(),
  905.                     this.Parent.GetType().ToString(),
  906.                     typeof(AbideTabControl).ToString()));
  907.  
  908.             base.InitLayout();
  909.         }
  910.         public override string ToString()
  911.         {
  912.             return Text;
  913.         }
  914.  
  915.         public virtual void OnClosing(EventArgs e)
  916.         {
  917.             if (Closing != null)
  918.                 Closing(this, e);
  919.  
  920.             this.Dispose();
  921.         }
  922.  
  923.         internal class AbideTabPageDesigner : ControlDesigner
  924.         {
  925.             public override SelectionRules SelectionRules
  926.             {
  927.                 get
  928.                 {
  929.                     return SelectionRules.None;
  930.                 }
  931.             }
  932.         }
  933.     }
  934.     public class TabPaintEventArgs : PaintEventArgs
  935.     {
  936.         public int TabIndex
  937.         {
  938.             get { return tabIndex; }
  939.         }
  940.  
  941.         int tabIndex = 0;
  942.  
  943.         public TabPaintEventArgs(Graphics graphics, Rectangle clipRect, int TabIndex)
  944.             : base(graphics, clipRect)
  945.         {
  946.             tabIndex = TabIndex;
  947.         }
  948.     }
  949.     public class TabPageAddedEventArgs : EventArgs
  950.     {
  951.         public AbideTabPage TabPage
  952.         {
  953.             get;
  954.             protected set;
  955.         }
  956.  
  957.         public TabPageAddedEventArgs(AbideTabPage TabPage)
  958.         {
  959.             this.TabPage = TabPage;
  960.         }
  961.     }
  962.  
  963.     /// <summary>
  964.     /// Provides data from the WM_NCCALCSIZE Window Message.
  965.     /// </summary>
  966.     public class NonClientCalcSizeEventArgs : EventArgs
  967.     {
  968.         #region Fields
  969.         RECT windowRect;
  970.         RECT clientRect;
  971.         #endregion
  972.  
  973.         #region Properties
  974.         /// <summary>
  975.         /// Gets the proposed Client Rectangle
  976.         /// </summary>
  977.         public Rectangle ClientRectangle
  978.         {
  979.             get
  980.             {
  981.                 return new Rectangle(
  982.                     clientRect.left - windowRect.left,
  983.                     clientRect.top - windowRect.top,
  984.                     (clientRect.right - clientRect.left) - windowRect.left,
  985.                     (clientRect.bottom - clientRect.top) - windowRect.top);
  986.             }
  987.         }
  988.  
  989.         /// <summary>
  990.         /// Gets a rectangle that represents the Window size and position.
  991.         /// </summary>
  992.         public Rectangle WindowRectangle
  993.         {
  994.             get { return new Rectangle(windowRect.left, windowRect.right, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top); }
  995.         }
  996.  
  997.  
  998.         /// <summary>
  999.         /// Sets the distance (in pixels) of the top of the client area from the window.
  1000.         /// Ex. 0 will remove the caption
  1001.         /// </summary>
  1002.         public int Top
  1003.         {
  1004.             get { return (int)(clientRect.top - windowRect.top); }
  1005.             set { clientRect.top = (windowRect.top + value); }
  1006.         }
  1007.  
  1008.         /// <summary>
  1009.         /// Sets the distance (int pixels) of the bottom of the client area from the window.
  1010.         /// Ex. 0 will remove the bottom border
  1011.         /// </summary>
  1012.         public int Bottom
  1013.         {
  1014.             get { return (int)(windowRect.bottom - clientRect.bottom); }
  1015.             set { clientRect.bottom = (windowRect.bottom - value); }
  1016.         }
  1017.  
  1018.         /// <summary>
  1019.         /// Sets the distance (int pixels) of the left of the client area from the window.
  1020.         /// Ex. 0 will remove the left border
  1021.         /// </summary>
  1022.         public int Left
  1023.         {
  1024.             get { return (int)(clientRect.left - windowRect.left); }
  1025.             set { clientRect.left = (windowRect.left + value); }
  1026.         }
  1027.  
  1028.         /// <summary>
  1029.         /// Sets the distance (int pixels) of the right of the client area from the window.
  1030.         /// Ex. 0 will remove the right border
  1031.         /// </summary>
  1032.         public int Right
  1033.         {
  1034.             get { return (int)(windowRect.right - clientRect.right); }
  1035.             set { clientRect.right = (windowRect.right - value); }
  1036.         }
  1037.         #endregion
  1038.  
  1039.         #region Constructor
  1040.         public NonClientCalcSizeEventArgs(RECT WindowRect, RECT ClientRect)
  1041.         {
  1042.             this.clientRect = ClientRect;
  1043.             this.windowRect = WindowRect;
  1044.         }
  1045.         #endregion
  1046.  
  1047.         #region Methods
  1048.         /// <summary>
  1049.         /// Removes the non-client area
  1050.         /// </summary>
  1051.         public void RemoveNonClientArea()
  1052.         {
  1053.             clientRect.top = windowRect.top;
  1054.             clientRect.bottom = windowRect.bottom;
  1055.             clientRect.left = windowRect.left;
  1056.             clientRect.right = windowRect.right;
  1057.         }
  1058.  
  1059.         /// <summary>
  1060.         /// Used to retrieve the RECT that represents the Client Area...
  1061.         /// </summary>
  1062.         /// <returns>RECT that represents the Client Area.</returns>
  1063.         public RECT GetClientRECT()
  1064.         {
  1065.             return clientRect;
  1066.         }
  1067.         #endregion
  1068.     }
  1069. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement