Esempio n. 1
0
 public TabControl()
 {
     base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.Opaque | ControlStyles.UserPaint, true);
     this._tabCollection = new TabPageCollection(this);
     this._selectedIndex = -1;
     this._selectedTabPage = null;
     this._toolTip = new RegionToolTip(this);
     this._placement = Microsoft.Matrix.UIComponents.TabPlacement.Top;
     this._mode = TabControlMode.TextAndImage;
     this._effectiveMode = TabControlMode.TextAndImage;
     this._tabRectanglesCalculated = false;
 }
Esempio n. 2
0
        // UNDONE: �޸�TabPage��RectangleֵʹTabControl֧��TabPlacement.Left��TabPlacement.Left
        private void EnsureTabRectangles(Graphics g, Font font, Rectangle clientRect)
        {
            #region if calculated
            if (this._tabRectanglesCalculated)
                return;

            if (this._tabCollection.Count < 2)
            {
                this._tabRectanglesCalculated = true;
                return;
            }
            #endregion

            #region Remove Tooltip Region
            if (this._toolTip.IsToolTipCreated)
            {
                foreach (TabPage page in this._tabCollection)
                {
                    if (page.ToolTipID != -1)
                    {
                        this._toolTip.RemoveToolTipRegion(page.ToolTipID);
                        page.ToolTipID = -1;
                    }
                }
            }
            #endregion

            #region Prepare clientRect, grephics and font etc
            this._effectiveMode = this._mode;
            if (clientRect.IsEmpty)
            {
                clientRect = base.ClientRectangle;
            }
            if (font == null)
            {
                font = this.Font;
            }

            bool graphicsCreated = false;
            if (g == null)
            {
                g = base.CreateGraphics();
                graphicsCreated = true;
            }
            #endregion

            #region calc tabs width and save in an array
            int[] tabWidthArray = new int[this._tabCollection.Count];
            int totalTabWidth = 10;
            int minTabWidth = 0x7fffffff;
            int index = 0;
            int rectWidth;

            StringFormat verticalFormat = new StringFormat( StringFormatFlags.DirectionVertical);
            foreach (TabPage page2 in this._tabCollection)
            {
                rectWidth = 11;
                if (this._mode != TabControlMode.TextOnly)
                {
                    rectWidth += 0x10;
                }
                if (this._mode != TabControlMode.ImageOnly)
                {
                    SizeF ef;
                    rectWidth += 4;
                    if (this._placement == TabPlacement.Top || this._placement == TabPlacement.Bottom)
                    {
                        ef = g.MeasureString(page2.Text, font);
                        rectWidth += (int) ef.Width;
                    }
                    else
                    {
                        ef = g.MeasureString(page2.Text, font, font.Height, verticalFormat);
                        rectWidth += (int)ef.Height;
                    }
                }

                minTabWidth = Math.Min(minTabWidth, rectWidth);

                totalTabWidth += rectWidth;
                tabWidthArray[index++] = rectWidth;
            }
            #endregion

            #region tab�ܿ�ȴ���tabControl���ʱ����Ӧ����
            bool tabWidthAdjusted = false;
            int availableClientWidth;
            if (this._placement == TabPlacement.Top || this._placement == TabPlacement.Bottom)
                availableClientWidth = clientRect.Width - 10;
            else
                availableClientWidth = clientRect.Height - 10;

            if ((availableClientWidth > 0) && (totalTabWidth > availableClientWidth))
            {
                int averageTabWidth = availableClientWidth / this._tabCollection.Count;
                bool textOnly = false;
                tabWidthAdjusted = true;
                if (this._mode == TabControlMode.TextOnly)
                {
                    textOnly = true;
                }
                else
                {
                    int imageOnlyWidth = 0x1b;
                    if (averageTabWidth < imageOnlyWidth)
                    {
                        this._effectiveMode = TabControlMode.TextOnly;
                        for (int i = 0; i < tabWidthArray.Length; i++)
                        {
                            tabWidthArray[i] = averageTabWidth;
                        }
                    }
                    else if (Math.Abs((int) (averageTabWidth - imageOnlyWidth)) < 10)
                    {
                        this._effectiveMode = TabControlMode.ImageOnly;
                        for (int j = 0; j < tabWidthArray.Length; j++)
                        {
                            tabWidthArray[j] = imageOnlyWidth;
                        }
                    }
                    else
                    {
                        textOnly = true;
                    }
                }

                if (textOnly)
                {
                    int originAverageTabWidth = averageTabWidth;
                    int originAvailableClientWidth = availableClientWidth;
                    int num12 = 0;
                    for (int i = 0; i < tabWidthArray.Length; i++)
                    {
                        if (tabWidthArray[i] <= averageTabWidth)
                        {
                            originAvailableClientWidth -= averageTabWidth;
                            num12++;
                        }
                    }
                    if (this._tabCollection.Count == num12)
                    {
                        averageTabWidth = originAvailableClientWidth / this._tabCollection.Count;
                    }
                    else
                    {
                        averageTabWidth = originAvailableClientWidth / (this._tabCollection.Count - num12);
                    }
                    for (int m = 0; m < tabWidthArray.Length; m++)
                    {
                        if (tabWidthArray[m] > originAverageTabWidth)
                        {
                            tabWidthArray[m] = averageTabWidth;
                        }
                    }
                }
            }
            #endregion //tab�ܿ�ȴ���tabControl���ʱ����Ӧ����

            int rectX = 0;
            int rectY = 0;
            if (this._placement == TabPlacement.Top)
            {
                rectX = 5;
                rectY = 2;
            }
            else if (this._placement == TabPlacement.Bottom)
            {
                rectX = 5;
                rectY = clientRect.Height - this.TabWellHeight;
            }
            else if (this._placement == TabPlacement.Left)
            {
                rectY = 5;
                rectX = clientRect.X + 2;
            }
            else if (this._placement == TabPlacement.Right)
            {
                rectY = 5;
                rectX = clientRect.Width - this.TabWellHeight;
            }

            index = 0;
            foreach (TabPage page3 in this._tabCollection)
            {
                rectWidth = tabWidthArray[index];

                //added to support vertical layout
                if (this._placement == TabPlacement.Top || this._placement == TabPlacement.Bottom)
                {
                    page3.Rectangle = new Rectangle(rectX, rectY, rectWidth, this.TabHeight);
                    rectX += rectWidth;
                }
                else
                {
                    page3.Rectangle = new Rectangle(rectX, rectY, this.TabHeight, rectWidth);
                    rectY += rectWidth;
                }

                if (base.IsHandleCreated)
                {
                    string toolTipText = page3.ToolTipText;
                    if (((toolTipText.Length == 0) && tabWidthAdjusted) && (this._mode != TabControlMode.ImageOnly))
                    {
                        toolTipText = page3.Text;
                    }
                    if (toolTipText.Length != 0)
                    {
                        this._toolTip.AddToolTipRegion(toolTipText, index, page3.Rectangle);
                        page3.ToolTipID = index;
                    }
                }

                index++;
            }

            if (graphicsCreated)
            {
                g.Dispose();
            }
            this._tabRectanglesCalculated = true;
        }