示例#1
0
        public override void DrawRect(CGRect dirtyRect)
        {
            if (Gradient != null)
            {
                Gradient.DrawInRect(dirtyRect, GradientAngle);
            }

            // -----------------------------------------
            nfloat textOffset   = 0f;
            nfloat imageXOffset = 0f;
            nfloat imageWidth   = 0f;

            if (Image != null)
            {
                if (Image.Size.Height > dirtyRect.Height)
                {
                    Image.Size = new CGSize(Image.Size.Height, dirtyRect.Height);
                }
                if (Image.Size.Width > dirtyRect.Width)
                {
                    Image.Size = new CGSize(dirtyRect.Width, Image.Size.Width);
                }

                imageWidth = Image.Size.Width;

                nfloat minXOffset = (dirtyRect.Height - Image.Size.Height) / 2f;

                imageXOffset = CornerRadius;
                if (imageXOffset < minXOffset)
                {
                    imageXOffset = minXOffset;
                }

                if (AttributedTitle == null)
                {
                    imageXOffset = dirtyRect.Width / 2f - Image.Size.Width / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_BeforeCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f - AttributedTitle.Size.Width / 2f - Image.Size.Width - minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Left_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width / 2f + AttributedTitle.Size.Width / 2f + minXOffset / 2f;
                }
                else if (IconLocation == IconLocationEnum.Right_AfterCenteredText)
                {
                    imageXOffset = dirtyRect.Width - minXOffset / 2f - Image.Size.Width;
                }
                else if (IconLocation == IconLocationEnum.Right)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    textOffset   = space;
                    imageXOffset = space + AttributedTitle.Size.Width + space;
                }
                else if (IconLocation == IconLocationEnum.Left)
                {
                    nfloat space = (dirtyRect.Width - AttributedTitle.Size.Width - Image.Size.Width) / 3;
                    imageXOffset = space;
                    textOffset   = space + Image.Size.Width + space;
                }

                CGRect imgRect = new CGRect(dirtyRect.X + imageXOffset,
                                            dirtyRect.Y + dirtyRect.Height / 2f - Image.Size.Height / 2f,
                                            Image.Size.Width,
                                            Image.Size.Height);
                Image.Draw(imgRect);
            }

            if (AttributedTitle != null)
            {
                if (IconLocation == IconLocationEnum.Right_BeforeCenteredText || IconLocation == IconLocationEnum.Left_BeforeCenteredText)
                {
                    if ((imageXOffset + imageWidth) > (dirtyRect.Width - AttributedTitle.Size.Width) / 2f)
                    {
                        textOffset = imageXOffset + imageWidth;
                    }
                }

                CGRect titleRect = new CGRect(dirtyRect.X + textOffset,
                                              dirtyRect.Y + dirtyRect.Height / 2f - AttributedTitle.Size.Height / 2f - 1f,
                                              dirtyRect.Width - textOffset,
                                              AttributedTitle.Size.Height);

                this.AttributedTitle.DrawInRect(titleRect);
            }

            if (Highlighted)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }

            if (!this.Enabled && DoNotChangeColorWhenDisabled != true)
            {
                NSColor fillColor = HighlitedColorOverlay;
                if (fillColor == null)
                {
                    fillColor = NSColor.FromRgba(0, 0, 0, 0.1f);
                }
                fillColor.Set();

                NSBezierPath highlitedFill = new NSBezierPath();
                highlitedFill.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
                highlitedFill.Fill();
            }
            // -----------------------------------------
            //base.DrawRect (dirtyRect);

            if (!Highlighted && BorderShadow != null)
            {
                BorderShadow.Set();
            }


            NSBezierPath bounds = new NSBezierPath();

            bounds.AppendPathWithRoundedRect(dirtyRect, CornerRadius, CornerRadius);
            bounds.AddClip();

            bounds.LineWidth = BorderLineWidth;

            if (BorderColor != null)
            {
                BorderColor.SetStroke();
            }

            bounds.Stroke();
        }