Exemplo n.º 1
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView is null)
            {
                return(new Size(-1, -1));
            }

            ArgumentNullException.ThrowIfNull(cellStyle);

            Size      preferredSize;
            Rectangle borderWidthsRect              = StdBorderWidths;
            int       borderAndPaddingWidths        = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights       = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            object formattedValue = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize);
            Image  img            = formattedValue as Image;
            Icon   ico            = null;

            if (img is null)
            {
                ico = formattedValue as Icon;
            }

            if (freeDimension == DataGridViewFreeDimension.Height &&
                ImageLayout == DataGridViewImageCellLayout.Zoom)
            {
                if (img is not null || ico is not null)
                {
                    if (img is not null)
                    {
                        int imgWidthAllowed = constraintSize.Width - borderAndPaddingWidths;
                        if (imgWidthAllowed <= 0 || img.Width == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(0, Math.Min(img.Height, decimal.ToInt32((decimal)img.Height * imgWidthAllowed / img.Width)));
                        }
                    }
                    else
                    {
                        int icoWidthAllowed = constraintSize.Width - borderAndPaddingWidths;
                        if (icoWidthAllowed <= 0 || ico.Width == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(0, Math.Min(ico.Height, decimal.ToInt32((decimal)ico.Height * icoWidthAllowed / ico.Width)));
                        }
                    }
                }
                else
                {
                    preferredSize = new Size(0, 1);
                }
            }
Exemplo n.º 2
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView == null)
            {
                return(new Size(-1, -1));
            }

            if (cellStyle == null)
            {
                throw new ArgumentNullException(nameof(cellStyle));
            }

            Size      preferredSize;
            Rectangle borderWidthsRect              = StdBorderWidths;
            int       borderAndPaddingWidths        = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights       = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            object formattedValue = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize);
            Image  img            = formattedValue as Image;
            Icon   ico            = null;

            if (img == null)
            {
                ico = formattedValue as Icon;
            }

            if (freeDimension == DataGridViewFreeDimension.Height &&
                ImageLayout == DataGridViewImageCellLayout.Zoom)
            {
                if (img != null || ico != null)
                {
                    if (img != null)
                    {
                        int imgWidthAllowed = constraintSize.Width - borderAndPaddingWidths;
                        if (imgWidthAllowed <= 0 || img.Width == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(0, Math.Min(img.Height, decimal.ToInt32((decimal)img.Height * imgWidthAllowed / img.Width)));
                        }
                    }
                    else
                    {
                        int icoWidthAllowed = constraintSize.Width - borderAndPaddingWidths;
                        if (icoWidthAllowed <= 0 || ico.Width == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(0, Math.Min(ico.Height, decimal.ToInt32((decimal)ico.Height * icoWidthAllowed / ico.Width)));
                        }
                    }
                }
                else
                {
                    preferredSize = new Size(0, 1);
                }
            }
            else if (freeDimension == DataGridViewFreeDimension.Width &&
                     ImageLayout == DataGridViewImageCellLayout.Zoom)
            {
                if (img != null || ico != null)
                {
                    if (img != null)
                    {
                        int imgHeightAllowed = constraintSize.Height - borderAndPaddingHeights;
                        if (imgHeightAllowed <= 0 || img.Height == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(Math.Min(img.Width, decimal.ToInt32((decimal)img.Width * imgHeightAllowed / img.Height)), 0);
                        }
                    }
                    else
                    {
                        int icoHeightAllowed = constraintSize.Height - borderAndPaddingHeights;
                        if (icoHeightAllowed <= 0 || ico.Height == 0)
                        {
                            preferredSize = Size.Empty;
                        }
                        else
                        {
                            preferredSize = new Size(Math.Min(ico.Width, decimal.ToInt32((decimal)ico.Width * icoHeightAllowed / ico.Height)), 0);
                        }
                    }
                }
                else
                {
                    preferredSize = new Size(1, 0);
                }
            }
            else
            {
                if (img != null)
                {
                    preferredSize = new Size(img.Width, img.Height);
                }
                else if (ico != null)
                {
                    preferredSize = new Size(ico.Width, ico.Height);
                }
                else
                {
                    preferredSize = new Size(1, 1);
                }
                if (freeDimension == DataGridViewFreeDimension.Height)
                {
                    preferredSize.Width = 0;
                }
                else if (freeDimension == DataGridViewFreeDimension.Width)
                {
                    preferredSize.Height = 0;
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Height)
            {
                preferredSize.Width += borderAndPaddingWidths;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Width = Math.Max(preferredSize.Width, borderAndPaddingWidths + DATAGRIDVIEWCELL_iconMarginWidth * 2 + iconsWidth);
                }
            }
            if (freeDimension != DataGridViewFreeDimension.Width)
            {
                preferredSize.Height += borderAndPaddingHeights;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Height = Math.Max(preferredSize.Height, borderAndPaddingHeights + DATAGRIDVIEWCELL_iconMarginHeight * 2 + iconsHeight);
                }
            }
            return(preferredSize);
        }
Exemplo n.º 3
0
        internal static Size GetPreferredRowHeaderSize(Graphics graphics,
                                                       string val,
                                                       DataGridViewCellStyle cellStyle,
                                                       int borderAndPaddingWidths,
                                                       int borderAndPaddingHeights,
                                                       bool showRowErrors,
                                                       bool showGlyph,
                                                       Size constraintSize,
                                                       TextFormatFlags flags)
        {
            Size preferredSize;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);

            switch (freeDimension)
            {
            case DataGridViewFreeDimension.Width:
            {
                int preferredWidth = 0, allowedHeight = constraintSize.Height - borderAndPaddingHeights;
                if (!string.IsNullOrEmpty(val))
                {
                    int maxHeight = allowedHeight - 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                    if (maxHeight > 0)
                    {
                        if (cellStyle.WrapMode == DataGridViewTriState.True)
                        {
                            preferredWidth = DataGridViewCell.MeasureTextWidth(graphics, val, cellStyle.Font, maxHeight, flags);
                        }
                        else
                        {
                            preferredWidth = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Width;
                        }
                        preferredWidth += 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth + DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft + DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                    }
                }
                if (allowedHeight >= DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight)
                {
                    if (showGlyph)
                    {
                        preferredWidth += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                    }
                    if (showRowErrors)
                    {
                        preferredWidth += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                    }
                }
                preferredWidth  = Math.Max(preferredWidth, 1);
                preferredWidth += borderAndPaddingWidths;
                return(new Size(preferredWidth, 0));
            }

            case DataGridViewFreeDimension.Height:
            {
                int minHeightIcon = 1, minHeightContent = 1;
                int allowedWidth = constraintSize.Width - borderAndPaddingWidths;
                if (!string.IsNullOrEmpty(val))
                {
                    if (showGlyph && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        // There is enough room for the status icon
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                        // Status icon takes priority
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth;
                    }
                    if (showRowErrors && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        // There is enough room for the error icon
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                        // There is enough room for both the status and error icons
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth;
                    }
                    if (allowedWidth > 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight)
                    {
                        // There is enough room for text
                        allowedWidth -= 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                                        DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                        if (cellStyle.WrapMode == DataGridViewTriState.True)
                        {
                            minHeightContent = DataGridViewCell.MeasureTextHeight(graphics, val, cellStyle.Font, allowedWidth, flags);
                        }
                        else
                        {
                            minHeightContent = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Height;
                        }
                        minHeightContent += 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                    }
                }
                else
                {
                    if ((showGlyph || showRowErrors) && allowedWidth >= 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth + DATAGRIDVIEWROWHEADERCELL_iconsWidth)
                    {
                        minHeightIcon = DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight;
                    }
                }
                return(new Size(0, Math.Max(minHeightIcon, minHeightContent) + borderAndPaddingHeights));
            }

            default:
            {
                if (!string.IsNullOrEmpty(val))
                {
                    if (cellStyle.WrapMode == DataGridViewTriState.True)
                    {
                        preferredSize = DataGridViewCell.MeasureTextPreferredSize(graphics, val, cellStyle.Font, 5.0F, flags);
                    }
                    else
                    {
                        preferredSize = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags);
                    }
                    preferredSize.Width += 2 * DATAGRIDVIEWROWHEADERCELL_contentMarginWidth +
                                           DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginLeft +
                                           DATAGRIDVIEWROWHEADERCELL_horizontalTextMarginRight;
                    preferredSize.Height += 2 * DATAGRIDVIEWROWHEADERCELL_verticalTextMargin;
                }
                else
                {
                    preferredSize = new Size(0, 1);
                }
                if (showGlyph)
                {
                    preferredSize.Width += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                }
                if (showRowErrors)
                {
                    preferredSize.Width += DATAGRIDVIEWROWHEADERCELL_iconsWidth + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginWidth;
                }
                if (showGlyph || showRowErrors)
                {
                    preferredSize.Height = Math.Max(preferredSize.Height,
                                                    DATAGRIDVIEWROWHEADERCELL_iconsHeight + 2 * DATAGRIDVIEWROWHEADERCELL_iconMarginHeight);
                }
                preferredSize.Width  += borderAndPaddingWidths;
                preferredSize.Height += borderAndPaddingHeights;
                return(preferredSize);
            }
            }
        }
Exemplo n.º 4
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView is null)
            {
                return(new Size(-1, -1));
            }

            ArgumentNullException.ThrowIfNull(cellStyle);

            Size      preferredSize;
            Rectangle borderWidthsRect              = StdBorderWidths;
            int       borderAndPaddingWidths        = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights       = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            object formattedValue  = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize);
            string formattedString = formattedValue as string;

            if (string.IsNullOrEmpty(formattedString))
            {
                formattedString = " ";
            }

            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1)
            {
                switch (freeDimension)
                {
                case DataGridViewFreeDimension.Width:
                {
                    int maxHeight = constraintSize.Height - borderAndPaddingHeights - VerticalTextMarginTop - VerticalTextMarginBottom;
                    if ((cellStyle.Alignment & AnyBottom) != 0)
                    {
                        maxHeight--;
                    }

                    preferredSize = new Size(DataGridViewCell.MeasureTextWidth(graphics,
                                                                               formattedString,
                                                                               cellStyle.Font,
                                                                               Math.Max(1, maxHeight),
                                                                               flags),
                                             0);
                    break;
                }

                case DataGridViewFreeDimension.Height:
                {
                    preferredSize = new Size(0,
                                             DataGridViewCell.MeasureTextHeight(graphics,
                                                                                formattedString,
                                                                                cellStyle.Font,
                                                                                Math.Max(1, constraintSize.Width - borderAndPaddingWidths - HorizontalTextMarginLeft - HorizontalTextMarginRight),
                                                                                flags));
                    break;
                }

                default:
                {
                    preferredSize = DataGridViewCell.MeasureTextPreferredSize(graphics,
                                                                              formattedString,
                                                                              cellStyle.Font,
                                                                              5.0F,
                                                                              flags);
                    break;
                }
                }
            }
            else
            {
                switch (freeDimension)
                {
                case DataGridViewFreeDimension.Width:
                {
                    preferredSize = new Size(DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Width,
                                             0);
                    break;
                }

                case DataGridViewFreeDimension.Height:
                {
                    preferredSize = new Size(0,
                                             DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Height);
                    break;
                }

                default:
                {
                    preferredSize = DataGridViewCell.MeasureTextSize(graphics, formattedString, cellStyle.Font, flags);
                    break;
                }
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Height)
            {
                preferredSize.Width += HorizontalTextMarginLeft + HorizontalTextMarginRight + borderAndPaddingWidths;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Width = Math.Max(preferredSize.Width, borderAndPaddingWidths + IconMarginWidth * 2 + s_iconsWidth);
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Width)
            {
                preferredSize.Height += VerticalTextMarginTop + VerticalTextMarginBottom + borderAndPaddingHeights;
                if ((cellStyle.Alignment & AnyBottom) != 0)
                {
                    preferredSize.Height += VerticalTextMarginBottom;
                }

                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Height = Math.Max(preferredSize.Height, borderAndPaddingHeights + IconMarginHeight * 2 + s_iconsHeight);
                }
            }

            return(preferredSize);
        }
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            Size empty;

            if (base.DataGridView == null)
            {
                return(new Size(-1, -1));
            }
            if (cellStyle == null)
            {
                throw new ArgumentNullException("cellStyle");
            }
            Rectangle stdBorderWidths = base.StdBorderWidths;
            int       num             = (stdBorderWidths.Left + stdBorderWidths.Width) + cellStyle.Padding.Horizontal;
            int       num2            = (stdBorderWidths.Top + stdBorderWidths.Height) + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimensionFromConstraint = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            object obj2  = base.GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.PreferredSize | DataGridViewDataErrorContexts.Formatting);
            Image  image = obj2 as Image;
            Icon   icon  = null;

            if (image == null)
            {
                icon = obj2 as Icon;
            }
            if ((freeDimensionFromConstraint == DataGridViewFreeDimension.Height) && (this.ImageLayout == DataGridViewImageCellLayout.Zoom))
            {
                if ((image != null) || (icon != null))
                {
                    if (image != null)
                    {
                        int num3 = constraintSize.Width - num;
                        if ((num3 <= 0) || (image.Width == 0))
                        {
                            empty = Size.Empty;
                        }
                        else
                        {
                            empty = new Size(0, Math.Min(image.Height, decimal.ToInt32((image.Height * num3) / image.Width)));
                        }
                    }
                    else
                    {
                        int num4 = constraintSize.Width - num;
                        if ((num4 <= 0) || (icon.Width == 0))
                        {
                            empty = Size.Empty;
                        }
                        else
                        {
                            empty = new Size(0, Math.Min(icon.Height, decimal.ToInt32((icon.Height * num4) / icon.Width)));
                        }
                    }
                }
                else
                {
                    empty = new Size(0, 1);
                }
            }
            else if ((freeDimensionFromConstraint == DataGridViewFreeDimension.Width) && (this.ImageLayout == DataGridViewImageCellLayout.Zoom))
            {
                if ((image != null) || (icon != null))
                {
                    if (image != null)
                    {
                        int num5 = constraintSize.Height - num2;
                        if ((num5 <= 0) || (image.Height == 0))
                        {
                            empty = Size.Empty;
                        }
                        else
                        {
                            empty = new Size(Math.Min(image.Width, decimal.ToInt32((image.Width * num5) / image.Height)), 0);
                        }
                    }
                    else
                    {
                        int num6 = constraintSize.Height - num2;
                        if ((num6 <= 0) || (icon.Height == 0))
                        {
                            empty = Size.Empty;
                        }
                        else
                        {
                            empty = new Size(Math.Min(icon.Width, decimal.ToInt32((icon.Width * num6) / icon.Height)), 0);
                        }
                    }
                }
                else
                {
                    empty = new Size(1, 0);
                }
            }
            else
            {
                if (image != null)
                {
                    empty = new Size(image.Width, image.Height);
                }
                else if (icon != null)
                {
                    empty = new Size(icon.Width, icon.Height);
                }
                else
                {
                    empty = new Size(1, 1);
                }
                switch (freeDimensionFromConstraint)
                {
                case DataGridViewFreeDimension.Height:
                    empty.Width = 0;
                    break;

                case DataGridViewFreeDimension.Width:
                    empty.Height = 0;
                    break;
                }
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
            {
                empty.Width += num;
                if (base.DataGridView.ShowCellErrors)
                {
                    empty.Width = Math.Max(empty.Width, (num + 8) + 12);
                }
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
            {
                empty.Height += num2;
                if (base.DataGridView.ShowCellErrors)
                {
                    empty.Height = Math.Max(empty.Height, (num2 + 8) + 11);
                }
            }
            return(empty);
        }
Exemplo n.º 6
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            if (DataGridView == null)
            {
                return(new Size(-1, -1));
            }

            if (cellStyle == null)
            {
                throw new ArgumentNullException(nameof(cellStyle));
            }

            Size      preferredSize;
            Rectangle borderWidthsRect = StdBorderWidths;
            int       borderAndPaddingWidths = borderWidthsRect.Left + borderWidthsRect.Width + cellStyle.Padding.Horizontal;
            int       borderAndPaddingHeights = borderWidthsRect.Top + borderWidthsRect.Height + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimension = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            int    marginWidths, marginHeights;
            string formattedString = GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.Formatting | DataGridViewDataErrorContexts.PreferredSize) as string;

            if (string.IsNullOrEmpty(formattedString))
            {
                formattedString = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            // Adding space for text padding.
            if (DataGridView.ApplyVisualStylesToInnerCells)
            {
                Rectangle rectThemeMargins = DataGridViewButtonCell.GetThemeMargins(graphics);
                marginWidths  = rectThemeMargins.X + rectThemeMargins.Width;
                marginHeights = rectThemeMargins.Y + rectThemeMargins.Height;
            }
            else
            {
                // Hardcoding 5 for the button borders for now.
                marginWidths = marginHeights = DATAGRIDVIEWBUTTONCELL_textPadding;
            }

            switch (freeDimension)
            {
            case DataGridViewFreeDimension.Width:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1 &&
                    constraintSize.Height - borderAndPaddingHeights - marginHeights - 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin > 0)
                {
                    preferredSize = new Size(
                        MeasureTextWidth(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            constraintSize.Height - borderAndPaddingHeights - marginHeights - 2
                            * DATAGRIDVIEWBUTTONCELL_verticalTextMargin,
                            flags),
                        0);
                }
                else
                {
                    preferredSize = new Size(
                        MeasureTextSize(graphics, formattedString, cellStyle.Font, flags).Width,
                        0);
                }
                break;
            }

            case DataGridViewFreeDimension.Height:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1 &&
                    constraintSize.Width - borderAndPaddingWidths - marginWidths - 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin > 0)
                {
                    preferredSize = new Size(
                        0,
                        MeasureTextHeight(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            constraintSize.Width - borderAndPaddingWidths - marginWidths - 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin,
                            flags));
                }
                else
                {
                    preferredSize = new Size(
                        0,
                        MeasureTextSize(
                            graphics,
                            formattedString,
                            cellStyle.Font,
                            flags).Height);
                }
                break;
            }

            default:
            {
                if (cellStyle.WrapMode == DataGridViewTriState.True && formattedString.Length > 1)
                {
                    preferredSize = MeasureTextPreferredSize(graphics, formattedString, cellStyle.Font, 5.0F, flags);
                }
                else
                {
                    preferredSize = MeasureTextSize(graphics, formattedString, cellStyle.Font, flags);
                }
                break;
            }
            }

            if (freeDimension != DataGridViewFreeDimension.Height)
            {
                preferredSize.Width += borderAndPaddingWidths + marginWidths + 2 * DATAGRIDVIEWBUTTONCELL_horizontalTextMargin;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Width = Math.Max(preferredSize.Width, borderAndPaddingWidths + IconMarginWidth * 2 + s_iconsWidth);
                }
            }

            if (freeDimension != DataGridViewFreeDimension.Width)
            {
                preferredSize.Height += borderAndPaddingHeights + marginHeights + 2 * DATAGRIDVIEWBUTTONCELL_verticalTextMargin;
                if (DataGridView.ShowCellErrors)
                {
                    // Making sure that there is enough room for the potential error icon
                    preferredSize.Height = Math.Max(preferredSize.Height, borderAndPaddingHeights + IconMarginHeight * 2 + s_iconsHeight);
                }
            }

            return(preferredSize);
        }
Exemplo n.º 7
0
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            Size size;

            if (base.DataGridView == null)
            {
                return(new Size(-1, -1));
            }
            if (cellStyle == null)
            {
                throw new ArgumentNullException("cellStyle");
            }
            Rectangle stdBorderWidths = base.StdBorderWidths;
            int       num             = (stdBorderWidths.Left + stdBorderWidths.Width) + cellStyle.Padding.Horizontal;
            int       num2            = (stdBorderWidths.Top + stdBorderWidths.Height) + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimensionFromConstraint = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            string str = base.GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.PreferredSize | DataGridViewDataErrorContexts.Formatting) as string;

            if (string.IsNullOrEmpty(str))
            {
                str = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            if ((cellStyle.WrapMode == DataGridViewTriState.True) && (str.Length > 1))
            {
                switch (freeDimensionFromConstraint)
                {
                case DataGridViewFreeDimension.Height:
                    size = new Size(0, DataGridViewCell.MeasureTextHeight(graphics, str, cellStyle.Font, Math.Max(1, constraintSize.Width - num), flags));
                    goto Label_01C3;

                case DataGridViewFreeDimension.Width:
                    size = new Size(DataGridViewCell.MeasureTextWidth(graphics, str, cellStyle.Font, Math.Max(1, ((constraintSize.Height - num2) - 1) - 1), flags), 0);
                    goto Label_01C3;
                }
                size = DataGridViewCell.MeasureTextPreferredSize(graphics, str, cellStyle.Font, 5f, flags);
            }
            else
            {
                switch (freeDimensionFromConstraint)
                {
                case DataGridViewFreeDimension.Height:
                    size = new Size(0, DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Height);
                    goto Label_01C3;

                case DataGridViewFreeDimension.Width:
                    size = new Size(DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Width, 0);
                    goto Label_01C3;
                }
                size = DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags);
            }
Label_01C3:
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
            {
                size.Width += num;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Width = Math.Max(size.Width, (num + 8) + 12);
                }
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
            {
                int num3 = (cellStyle.WrapMode == DataGridViewTriState.True) ? 1 : 2;
                size.Height += (num3 + 1) + num2;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Height = Math.Max(size.Height, (num2 + 8) + 11);
                }
            }
            return(size);
        }
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            Size size;

            if (rowIndex != -1)
            {
                throw new ArgumentOutOfRangeException("rowIndex");
            }
            if (base.DataGridView == null)
            {
                return(new Size(-1, -1));
            }
            if (cellStyle == null)
            {
                throw new ArgumentNullException("cellStyle");
            }
            DataGridViewFreeDimension       freeDimensionFromConstraint = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder = new DataGridViewAdvancedBorderStyle();
            DataGridViewAdvancedBorderStyle advancedBorderStyle = base.DataGridView.AdjustColumnHeaderBorderStyle(base.DataGridView.AdvancedColumnHeadersBorderStyle, dataGridViewAdvancedBorderStylePlaceholder, false, false);
            Rectangle       rectangle = this.BorderWidths(advancedBorderStyle);
            int             num       = (rectangle.Left + rectangle.Width) + cellStyle.Padding.Horizontal;
            int             num2      = (rectangle.Top + rectangle.Height) + cellStyle.Padding.Vertical;
            TextFormatFlags flags     = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);
            string          str       = this.GetValue(rowIndex) as string;

            switch (freeDimensionFromConstraint)
            {
            case DataGridViewFreeDimension.Height:
            {
                Size empty;
                int  num3 = constraintSize.Width - num;
                size = new Size(0, 0);
                if (((num3 < 0x11) || (base.OwningColumn == null)) || (base.OwningColumn.SortMode == DataGridViewColumnSortMode.NotSortable))
                {
                    empty = Size.Empty;
                }
                else
                {
                    empty = new Size(0x11, 7);
                }
                if ((((num3 - 2) - 2) > 0) && !string.IsNullOrEmpty(str))
                {
                    if (cellStyle.WrapMode == DataGridViewTriState.True)
                    {
                        if ((empty.Width > 0) && (((((num3 - 2) - 2) - 2) - empty.Width) > 0))
                        {
                            size = new Size(0, DataGridViewCell.MeasureTextHeight(graphics, str, cellStyle.Font, (((num3 - 2) - 2) - 2) - empty.Width, flags));
                        }
                        else
                        {
                            size = new Size(0, DataGridViewCell.MeasureTextHeight(graphics, str, cellStyle.Font, (num3 - 2) - 2, flags));
                        }
                    }
                    else
                    {
                        size = new Size(0, DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Height);
                    }
                }
                size.Height = Math.Max(size.Height, empty.Height);
                size.Height = Math.Max(size.Height, 1);
                goto Label_0391;
            }

            case DataGridViewFreeDimension.Width:
                size = new Size(0, 0);
                if (!string.IsNullOrEmpty(str))
                {
                    if (cellStyle.WrapMode != DataGridViewTriState.True)
                    {
                        size = new Size(DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Width, 0);
                        break;
                    }
                    size = new Size(DataGridViewCell.MeasureTextWidth(graphics, str, cellStyle.Font, Math.Max(1, (constraintSize.Height - num2) - 2), flags), 0);
                }
                break;

            default:
                if (!string.IsNullOrEmpty(str))
                {
                    if (cellStyle.WrapMode == DataGridViewTriState.True)
                    {
                        size = DataGridViewCell.MeasureTextPreferredSize(graphics, str, cellStyle.Font, 5f, flags);
                    }
                    else
                    {
                        size = DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags);
                    }
                }
                else
                {
                    size = new Size(0, 0);
                }
                if ((base.OwningColumn != null) && (base.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable))
                {
                    size.Width += 0x11;
                    if (!string.IsNullOrEmpty(str))
                    {
                        size.Width += 2;
                    }
                    size.Height = Math.Max(size.Height, 7);
                }
                size.Width  = Math.Max(size.Width, 1);
                size.Height = Math.Max(size.Height, 1);
                goto Label_0391;
            }
            if (((((constraintSize.Height - num2) - 2) > 7) && (base.OwningColumn != null)) && (base.OwningColumn.SortMode != DataGridViewColumnSortMode.NotSortable))
            {
                size.Width += 0x11;
                if (!string.IsNullOrEmpty(str))
                {
                    size.Width += 2;
                }
            }
            size.Width = Math.Max(size.Width, 1);
Label_0391:
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    size.Width += 4;
                }
                size.Width += num;
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
            {
                size.Height += 2 + num2;
            }
            if (base.DataGridView.ApplyVisualStylesToHeaderCells)
            {
                Rectangle themeMargins = DataGridViewHeaderCell.GetThemeMargins(graphics);
                if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
                {
                    size.Width += themeMargins.X + themeMargins.Width;
                }
                if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
                {
                    size.Height += themeMargins.Y + themeMargins.Height;
                }
            }
            return(size);
        }
        internal static Size GetPreferredRowHeaderSize(Graphics graphics, string val, DataGridViewCellStyle cellStyle, int borderAndPaddingWidths, int borderAndPaddingHeights, bool showRowErrors, bool showGlyph, Size constraintSize, TextFormatFlags flags)
        {
            int width;
            int num2;

            switch (DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize))
            {
            case DataGridViewFreeDimension.Height:
            {
                int num4     = 1;
                int height   = 1;
                int maxWidth = constraintSize.Width - borderAndPaddingWidths;
                if (string.IsNullOrEmpty(val))
                {
                    if ((showGlyph || showRowErrors) && (maxWidth >= 0x12))
                    {
                        num4 = 15;
                    }
                }
                else
                {
                    if (showGlyph && (maxWidth >= 0x12))
                    {
                        num4      = 15;
                        maxWidth -= 0x12;
                    }
                    if (showRowErrors && (maxWidth >= 0x12))
                    {
                        num4      = 15;
                        maxWidth -= 0x12;
                    }
                    if (maxWidth > 9)
                    {
                        maxWidth -= 9;
                        if (cellStyle.WrapMode == DataGridViewTriState.True)
                        {
                            height = DataGridViewCell.MeasureTextHeight(graphics, val, cellStyle.Font, maxWidth, flags);
                        }
                        else
                        {
                            height = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Height;
                        }
                        height += 2;
                    }
                }
                return(new Size(0, Math.Max(num4, height) + borderAndPaddingHeights));
            }

            case DataGridViewFreeDimension.Width:
            {
                width = 0;
                num2  = constraintSize.Height - borderAndPaddingHeights;
                if (string.IsNullOrEmpty(val))
                {
                    goto Label_007B;
                }
                int maxHeight = num2 - 2;
                if (maxHeight <= 0)
                {
                    goto Label_007B;
                }
                if (cellStyle.WrapMode != DataGridViewTriState.True)
                {
                    width = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags).Width;
                    break;
                }
                width = DataGridViewCell.MeasureTextWidth(graphics, val, cellStyle.Font, maxHeight, flags);
                break;
            }

            default:
                Size size;
                if (!string.IsNullOrEmpty(val))
                {
                    if (cellStyle.WrapMode == DataGridViewTriState.True)
                    {
                        size = DataGridViewCell.MeasureTextPreferredSize(graphics, val, cellStyle.Font, 5f, flags);
                    }
                    else
                    {
                        size = DataGridViewCell.MeasureTextSize(graphics, val, cellStyle.Font, flags);
                    }
                    size.Width  += 9;
                    size.Height += 2;
                }
                else
                {
                    size = new Size(0, 1);
                }
                if (showGlyph)
                {
                    size.Width += 0x12;
                }
                if (showRowErrors)
                {
                    size.Width += 0x12;
                }
                if (showGlyph || showRowErrors)
                {
                    size.Height = Math.Max(size.Height, 15);
                }
                size.Width  += borderAndPaddingWidths;
                size.Height += borderAndPaddingHeights;
                return(size);
            }
            width += 9;
Label_007B:
            if (num2 >= 15)
            {
                if (showGlyph)
                {
                    width += 0x12;
                }
                if (showRowErrors)
                {
                    width += 0x12;
                }
            }
            return(new Size(Math.Max(width, 1) + borderAndPaddingWidths, 0));
        }
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            Size size;
            int  num3;
            int  num4;

            if (base.DataGridView == null)
            {
                return(new Size(-1, -1));
            }
            if (cellStyle == null)
            {
                throw new ArgumentNullException("cellStyle");
            }
            Rectangle stdBorderWidths = base.StdBorderWidths;
            int       num             = (stdBorderWidths.Left + stdBorderWidths.Width) + cellStyle.Padding.Horizontal;
            int       num2            = (stdBorderWidths.Top + stdBorderWidths.Height) + cellStyle.Padding.Vertical;
            DataGridViewFreeDimension freeDimensionFromConstraint = DataGridViewCell.GetFreeDimensionFromConstraint(constraintSize);
            string str = base.GetFormattedValue(rowIndex, ref cellStyle, DataGridViewDataErrorContexts.PreferredSize | DataGridViewDataErrorContexts.Formatting) as string;

            if (string.IsNullOrEmpty(str))
            {
                str = " ";
            }
            TextFormatFlags flags = DataGridViewUtilities.ComputeTextFormatFlagsForCellStyleAlignment(base.DataGridView.RightToLeftInternal, cellStyle.Alignment, cellStyle.WrapMode);

            if (base.DataGridView.ApplyVisualStylesToInnerCells)
            {
                Rectangle themeMargins = GetThemeMargins(graphics);
                num3 = themeMargins.X + themeMargins.Width;
                num4 = themeMargins.Y + themeMargins.Height;
            }
            else
            {
                num3 = num4 = 5;
            }
            switch (freeDimensionFromConstraint)
            {
            case DataGridViewFreeDimension.Height:
                if (((cellStyle.WrapMode != DataGridViewTriState.True) || (str.Length <= 1)) || ((((constraintSize.Width - num) - num3) - 4) <= 0))
                {
                    size = new Size(0, DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Height);
                    break;
                }
                size = new Size(0, DataGridViewCell.MeasureTextHeight(graphics, str, cellStyle.Font, ((constraintSize.Width - num) - num3) - 4, flags));
                break;

            case DataGridViewFreeDimension.Width:
                if (((cellStyle.WrapMode != DataGridViewTriState.True) || (str.Length <= 1)) || ((((constraintSize.Height - num2) - num4) - 2) <= 0))
                {
                    size = new Size(DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags).Width, 0);
                    break;
                }
                size = new Size(DataGridViewCell.MeasureTextWidth(graphics, str, cellStyle.Font, ((constraintSize.Height - num2) - num4) - 2, flags), 0);
                break;

            default:
                if ((cellStyle.WrapMode == DataGridViewTriState.True) && (str.Length > 1))
                {
                    size = DataGridViewCell.MeasureTextPreferredSize(graphics, str, cellStyle.Font, 5f, flags);
                }
                else
                {
                    size = DataGridViewCell.MeasureTextSize(graphics, str, cellStyle.Font, flags);
                }
                break;
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Height)
            {
                size.Width += (num + num3) + 4;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Width = Math.Max(size.Width, (num + 8) + 12);
                }
            }
            if (freeDimensionFromConstraint != DataGridViewFreeDimension.Width)
            {
                size.Height += (num2 + num4) + 2;
                if (base.DataGridView.ShowCellErrors)
                {
                    size.Height = Math.Max(size.Height, (num2 + 8) + 11);
                }
            }
            return(size);
        }