[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")]     // using "\t" to figure out the width of tab
        private void  CalculateInternalLayoutMetrics()
        {
            Size maxTextSize        = Size.Empty;
            Size maxImageSize       = Size.Empty;
            Size maxCheckSize       = scaledDefaultImageSize;
            Size maxArrowSize       = Size.Empty;
            Size maxNonMenuItemSize = Size.Empty;

            // determine Text Metrics
            for (int i = 0; i < Items.Count; i++)
            {
                ToolStripItem     item     = Items[i];
                ToolStripMenuItem menuItem = item as ToolStripMenuItem;

                if (menuItem != null)
                {
                    Size menuItemTextSize = menuItem.GetTextSize();

                    if (menuItem.ShowShortcutKeys)
                    {
                        Size shortcutTextSize = menuItem.GetShortcutTextSize();
                        if (tabWidth == -1)
                        {
                            tabWidth = TextRenderer.MeasureText("\t", this.Font).Width;
                        }
                        menuItemTextSize.Width += tabWidth + shortcutTextSize.Width;
                        menuItemTextSize.Height = Math.Max(menuItemTextSize.Height, shortcutTextSize.Height);
                    }

                    // we truly only care about the maximum size we find.
                    maxTextSize.Width  = Math.Max(maxTextSize.Width, menuItemTextSize.Width);
                    maxTextSize.Height = Math.Max(maxTextSize.Height, menuItemTextSize.Height);

                    // determine Image Metrics
                    Size imageSize = Size.Empty;
                    if (menuItem.Image != null)
                    {
                        imageSize = (menuItem.ImageScaling == ToolStripItemImageScaling.SizeToFit) ? ImageScalingSize : menuItem.Image.Size;
                    }

                    maxImageSize.Width  = Math.Max(maxImageSize.Width, imageSize.Width);
                    maxImageSize.Height = Math.Max(maxImageSize.Height, imageSize.Height);

                    if (menuItem.CheckedImage != null)
                    {
                        Size checkedImageSize = menuItem.CheckedImage.Size;
                        maxCheckSize.Width  = Math.Max(checkedImageSize.Width, maxCheckSize.Width);
                        maxCheckSize.Height = Math.Max(checkedImageSize.Height, maxCheckSize.Height);
                    }
                }
                else if (!(item is ToolStripSeparator))
                {
                    maxNonMenuItemSize.Height = Math.Max(item.Bounds.Height, maxNonMenuItemSize.Height);
                    maxNonMenuItemSize.Width  = Math.Max(item.Bounds.Width, maxNonMenuItemSize.Width);
                }
            }

            this.maxItemSize.Height = Math.Max(maxTextSize.Height + scaledTextPadding.Vertical, Math.Max(maxCheckSize.Height + scaledCheckPadding.Vertical, maxArrowSize.Height + scaledArrowPadding.Vertical));

            if (ShowImageMargin)
            {
                // only add in the image into the calculation if we're going to render it.
                this.maxItemSize.Height = Math.Max(maxImageSize.Height + scaledImagePadding.Vertical, maxItemSize.Height);
            }

            bool useDefaultCheckMarginWidth = (ShowCheckMargin && (maxCheckSize.Width == 0));
            bool useDefaultImageMarginWidth = (ShowImageMargin && (maxImageSize.Width == 0));

            // Always save space for an arrow
            maxArrowSize = new Size(scaledArrowSize, maxItemSize.Height);

            maxTextSize.Height  = maxItemSize.Height - scaledTextPadding.Vertical;
            maxImageSize.Height = maxItemSize.Height - scaledImagePadding.Vertical;
            maxCheckSize.Height = maxItemSize.Height - scaledCheckPadding.Vertical;

            // fixup if there are non-menu items that are larger than our normal menu items
            maxTextSize.Width = Math.Max(maxTextSize.Width, maxNonMenuItemSize.Width);

            Point nextPoint = Point.Empty;
            int   checkAndImageMarginWidth = 0;

            int extraImageWidth = Math.Max(0, maxImageSize.Width - scaledDefaultImageSize.Width);

            if (ShowCheckMargin && ShowImageMargin)
            {
                // double column - check margin then image margin
                // default to 46px - grow if necessary.
                checkAndImageMarginWidth = scaledDefaultImageAndCheckMarginWidth;

                // add in the extra space for the image... since the check size is locked down to 16x16.
                checkAndImageMarginWidth += extraImageWidth;

                // align the checkmark
                nextPoint      = new Point(scaledCheckPadding.Left, scaledCheckPadding.Top);
                checkRectangle = LayoutUtils.Align(maxCheckSize, new Rectangle(nextPoint.X, nextPoint.Y, maxCheckSize.Width, maxItemSize.Height), ContentAlignment.MiddleCenter);

                // align the image rectangle
                nextPoint.X    = checkRectangle.Right + scaledCheckPadding.Right + scaledImagePadding.Left;
                nextPoint.Y    = scaledImagePadding.Top;
                imageRectangle = LayoutUtils.Align(maxImageSize, new Rectangle(nextPoint.X, nextPoint.Y, maxImageSize.Width, maxItemSize.Height), ContentAlignment.MiddleCenter);
            }
            else if (ShowCheckMargin)
            {
                // no images should be shown in a ShowCheckMargin only scenario.
                // default to 24px - grow if necessary.
                checkAndImageMarginWidth = scaledDefaultImageMarginWidth;

                // align the checkmark
                nextPoint = new Point(1, scaledCheckPadding.Top);
                //    nextPoint = new Point(scaledCheckPadding.Left, scaledCheckPadding.Top);
                checkRectangle = LayoutUtils.Align(maxCheckSize, new Rectangle(nextPoint.X, nextPoint.Y, checkAndImageMarginWidth, maxItemSize.Height), ContentAlignment.MiddleCenter);

                imageRectangle = Rectangle.Empty;
            }
            else if (ShowImageMargin)
            {
                // checks and images render in the same area.

                // default to 24px - grow if necessary.
                checkAndImageMarginWidth = scaledDefaultImageMarginWidth;

                // add in the extra space for the image... since the check size is locked down to 16x16.
                checkAndImageMarginWidth += extraImageWidth;

                // NOTE due to the Padding property, we're going to have to recalc the vertical alignment in ToolStripMenuItemInternalLayout.
                // Dont fuss here over the Y, X is what's critical.

                // check and image rect are the same - take the max of the image size and the check size and align
                nextPoint      = new Point(1, scaledCheckPadding.Top);
                checkRectangle = LayoutUtils.Align(LayoutUtils.UnionSizes(maxCheckSize, maxImageSize), new Rectangle(nextPoint.X, nextPoint.Y, checkAndImageMarginWidth - 1, maxItemSize.Height), ContentAlignment.MiddleCenter);

                // align the image
                imageRectangle = checkRectangle;
            }
            else
            {
                checkAndImageMarginWidth = 0;
            }
            nextPoint.X = checkAndImageMarginWidth + 1;


            // calculate space for image
            // if we didnt have a check - make sure to ignore check padding

            // consider: should we constrain to a reasonable width?
            //imageMarginBounds = new Rectangle(0, 0, Math.Max(imageMarginWidth,DefaultImageMarginWidth), this.Height);
            imageMarginBounds = new Rectangle(0, 0, checkAndImageMarginWidth, this.Height);

            // calculate space for shortcut and text
            nextPoint.X   = imageMarginBounds.Right + scaledTextPadding.Left;
            nextPoint.Y   = scaledTextPadding.Top;
            textRectangle = new Rectangle(nextPoint, maxTextSize);

            // calculate space for arrow
            nextPoint.X    = textRectangle.Right + scaledTextPadding.Right + scaledArrowPadding.Left;
            nextPoint.Y    = scaledArrowPadding.Top;
            arrowRectangle = new Rectangle(nextPoint, maxArrowSize);

            // calculate space required for all of these pieces
            this.maxItemSize.Width = (arrowRectangle.Right + scaledArrowPadding.Right) - imageMarginBounds.Left;

            this.Padding = DefaultPadding;
            int trimPadding = imageMarginBounds.Width;

            if (RightToLeft == RightToLeft.Yes)
            {
                // reverse the rectangle alignment in RightToLeft.Yes
                trimPadding += scaledTextPadding.Right;
                int width = maxItemSize.Width;
                checkRectangle.X    = width - checkRectangle.Right;
                imageRectangle.X    = width - imageRectangle.Right;
                textRectangle.X     = width - textRectangle.Right;
                arrowRectangle.X    = width - arrowRectangle.Right;
                imageMarginBounds.X = width - imageMarginBounds.Right;
            }
            else
            {
                trimPadding += scaledTextPadding.Left;
            }



            // We need to make sure that the text really appears vertically centered - this can be a problem in
            // systems which force the text rectangle to be odd.

            // force this to be an even height.
            this.maxItemSize.Height += this.maxItemSize.Height % 2;

            textRectangle.Y  = LayoutUtils.VAlign(textRectangle.Size, new Rectangle(Point.Empty, maxItemSize), ContentAlignment.MiddleCenter).Y;
            textRectangle.Y += (textRectangle.Height % 2);    // if the height is odd, push down by one px
            state[stateMaxItemSizeValid] = true;
            this.PaddingToTrim           = trimPadding;
        }
Exemple #2
0
        private void CalculateInternalLayoutMetrics()
        {
            Size empty            = Size.Empty;
            Size alignThis        = Size.Empty;
            Size defaultImageSize = ToolStripDropDownMenu.defaultImageSize;
            Size size             = Size.Empty;
            Size size5            = Size.Empty;

            for (int i = 0; i < this.Items.Count; i++)
            {
                ToolStripItem     item  = this.Items[i];
                ToolStripMenuItem item2 = item as ToolStripMenuItem;
                if (item2 != null)
                {
                    Size textSize = item2.GetTextSize();
                    if (item2.ShowShortcutKeys)
                    {
                        Size shortcutTextSize = item2.GetShortcutTextSize();
                        if (this.tabWidth == -1)
                        {
                            this.tabWidth = TextRenderer.MeasureText("\t", this.Font).Width;
                        }
                        textSize.Width += this.tabWidth + shortcutTextSize.Width;
                        textSize.Height = Math.Max(textSize.Height, shortcutTextSize.Height);
                    }
                    empty.Width  = Math.Max(empty.Width, textSize.Width);
                    empty.Height = Math.Max(empty.Height, textSize.Height);
                    Size size8 = Size.Empty;
                    if (item2.Image != null)
                    {
                        size8 = (item2.ImageScaling == ToolStripItemImageScaling.SizeToFit) ? base.ImageScalingSize : item2.Image.Size;
                    }
                    alignThis.Width  = Math.Max(alignThis.Width, size8.Width);
                    alignThis.Height = Math.Max(alignThis.Height, size8.Height);
                    if (item2.CheckedImage != null)
                    {
                        Size size9 = item2.CheckedImage.Size;
                        defaultImageSize.Width  = Math.Max(size9.Width, defaultImageSize.Width);
                        defaultImageSize.Height = Math.Max(size9.Height, defaultImageSize.Height);
                    }
                }
                else if (!(item is ToolStripSeparator))
                {
                    size5.Height = Math.Max(item.Bounds.Height, size5.Height);
                    size5.Width  = Math.Max(item.Bounds.Width, size5.Width);
                }
            }
            this.maxItemSize.Height = Math.Max(empty.Height + TextPadding.Vertical, Math.Max((int)(defaultImageSize.Height + CheckPadding.Vertical), (int)(size.Height + ArrowPadding.Vertical)));
            if (this.ShowImageMargin)
            {
                this.maxItemSize.Height = Math.Max(alignThis.Height + ImagePadding.Vertical, this.maxItemSize.Height);
            }
            if (this.ShowCheckMargin)
            {
                int num1 = defaultImageSize.Width;
            }
            if (this.ShowImageMargin)
            {
                int num6 = alignThis.Width;
            }
            size                    = new Size(10, this.maxItemSize.Height);
            empty.Height            = this.maxItemSize.Height - TextPadding.Vertical;
            alignThis.Height        = this.maxItemSize.Height - ImagePadding.Vertical;
            defaultImageSize.Height = this.maxItemSize.Height - CheckPadding.Vertical;
            empty.Width             = Math.Max(empty.Width, size5.Width);
            Point location = Point.Empty;
            int   width    = 0;
            int   num3     = Math.Max(0, alignThis.Width - ToolStripDropDownMenu.defaultImageSize.Width);

            if (this.ShowCheckMargin && this.ShowImageMargin)
            {
                width               = DefaultImageAndCheckMarginWidth + num3;
                location            = new Point(CheckPadding.Left, CheckPadding.Top);
                this.checkRectangle = LayoutUtils.Align(defaultImageSize, new Rectangle(location.X, location.Y, defaultImageSize.Width, this.maxItemSize.Height), ContentAlignment.MiddleCenter);
                location.X          = (this.checkRectangle.Right + CheckPadding.Right) + ImagePadding.Left;
                location.Y          = ImagePadding.Top;
                this.imageRectangle = LayoutUtils.Align(alignThis, new Rectangle(location.X, location.Y, alignThis.Width, this.maxItemSize.Height), ContentAlignment.MiddleCenter);
            }
            else if (this.ShowCheckMargin)
            {
                width               = DefaultImageMarginWidth;
                location            = new Point(1, CheckPadding.Top);
                this.checkRectangle = LayoutUtils.Align(defaultImageSize, new Rectangle(location.X, location.Y, width, this.maxItemSize.Height), ContentAlignment.MiddleCenter);
                this.imageRectangle = Rectangle.Empty;
            }
            else if (this.ShowImageMargin)
            {
                width               = DefaultImageMarginWidth + num3;
                location            = new Point(1, CheckPadding.Top);
                this.checkRectangle = LayoutUtils.Align(LayoutUtils.UnionSizes(defaultImageSize, alignThis), new Rectangle(location.X, location.Y, width - 1, this.maxItemSize.Height), ContentAlignment.MiddleCenter);
                this.imageRectangle = this.checkRectangle;
            }
            else
            {
                width = 0;
            }
            location.X             = width + 1;
            this.imageMarginBounds = new Rectangle(0, 0, width, base.Height);
            location.X             = this.imageMarginBounds.Right + TextPadding.Left;
            location.Y             = TextPadding.Top;
            this.textRectangle     = new Rectangle(location, empty);
            location.X             = (this.textRectangle.Right + TextPadding.Right) + ArrowPadding.Left;
            location.Y             = ArrowPadding.Top;
            this.arrowRectangle    = new Rectangle(location, size);
            this.maxItemSize.Width = (this.arrowRectangle.Right + ArrowPadding.Right) - this.imageMarginBounds.Left;
            base.Padding           = this.DefaultPadding;
            int num4 = this.imageMarginBounds.Width;

            if (this.RightToLeft == RightToLeft.Yes)
            {
                num4 += TextPadding.Right;
                int num5 = this.maxItemSize.Width;
                this.checkRectangle.X    = num5 - this.checkRectangle.Right;
                this.imageRectangle.X    = num5 - this.imageRectangle.Right;
                this.textRectangle.X     = num5 - this.textRectangle.Right;
                this.arrowRectangle.X    = num5 - this.arrowRectangle.Right;
                this.imageMarginBounds.X = num5 - this.imageMarginBounds.Right;
            }
            else
            {
                num4 += TextPadding.Left;
            }
            this.maxItemSize.Height          += this.maxItemSize.Height % 2;
            this.textRectangle.Y              = LayoutUtils.VAlign(this.textRectangle.Size, new Rectangle(Point.Empty, this.maxItemSize), ContentAlignment.MiddleCenter).Y;
            this.textRectangle.Y             += this.textRectangle.Height % 2;
            this.state[stateMaxItemSizeValid] = true;
            this.PaddingToTrim = num4;
        }