protected override SizeF ArrangeOverride(SizeF finalSize) { SizeF fieldSize = finalSize; RectangleF layoutField = new RectangleF(PointF.Empty, finalSize); RectangleF imageBounds = RectangleF.Empty; RectangleF textBounds = RectangleF.Empty; SizeF textSize = SizeF.Empty; SizeF imageSize = SizeF.Empty; ContentAlignment imageAlign = this.ImageAlignment; ContentAlignment textAlign = this.TextAlignment; TextImageRelation textImageRelation = this.TextImageRelation; if (this.RightToLeft) { imageAlign = TelerikAlignHelper.RtlTranslateContent(ImageAlignment); textAlign = TelerikAlignHelper.RtlTranslateContent(TextAlignment); textImageRelation = TelerikAlignHelper.RtlTranslateRelation(TextImageRelation); } if ((this.textElement != null) && IsPrimitiveNullOrEmpty(this.imageElement)) { this.textElement.Arrange(layoutField); if (this.imageElement != null) { this.imageElement.Arrange(layoutField); } return(finalSize); } if ((this.imageElement != null) && IsPrimitiveNullOrEmpty(this.textElement)) { this.imageElement.Arrange(layoutField); if (this.textElement != null) { this.textElement.Arrange(layoutField); } return(finalSize); } textSize = this.GetInvariantLength(textElement.DesiredSize, textElement.Margin); imageSize = this.GetInvariantLength(imageElement.DesiredSize, imageElement.Margin); // Subtract the image size from the whole field size SizeF textSizeLeft = LayoutUtils.SubAlignedRegion(fieldSize, imageSize, textImageRelation); // Create a new size based on the text size and the image size SizeF newSize = LayoutUtils.AddAlignedRegion(textSize, imageSize, textImageRelation); // The new field which is a union of the new sizes RectangleF maxFieldRectangle = RectangleF.Empty; maxFieldRectangle.Size = LayoutUtils.UnionSizes(fieldSize, newSize); // Image doesn't overlay text bool imageAlignNoOverlay = (TelerikAlignHelper.ImageAlignToRelation(imageAlign) & textImageRelation) != TextImageRelation.Overlay; // Text doesn't overlay image bool textAlignNoOverlay = (TelerikAlignHelper.TextAlignToRelation(textAlign) & textImageRelation) != TextImageRelation.Overlay; if (imageAlignNoOverlay) { LayoutUtils.SplitRegion(maxFieldRectangle, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); } else if (textAlignNoOverlay) { LayoutUtils.SplitRegion(maxFieldRectangle, textSize, (AnchorStyles)LayoutUtils.GetOppositeTextImageRelation(textImageRelation), out textBounds, out imageBounds); } else { // Both image overlays text and text overlays image if (textImageRelation == TextImageRelation.Overlay) { LayoutUtils.SplitRegion(maxFieldRectangle, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); } else { RectangleF alignedField = LayoutUtils.Align(newSize, maxFieldRectangle, ContentAlignment.MiddleCenter); LayoutUtils.SplitRegion(alignedField, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); LayoutUtils.ExpandRegionsToFillBounds(maxFieldRectangle, (AnchorStyles)textImageRelation, ref imageBounds, ref textBounds); } } textBounds.Size = SizeF.Subtract(textBounds.Size, this.textElement.Margin.Size); imageBounds.Size = SizeF.Subtract(imageBounds.Size, this.imageElement.Margin.Size); this.textElement.Arrange(textBounds); this.imageElement.Arrange(imageBounds); return(finalSize); }
/// <summary> /// /// </summary> /// <param name="fieldSize"></param> /// <param name="layoutField"></param> /// <param name="newLayouts"></param> public void LayoutTextAndImage(Size fieldSize, Rectangle layoutField, bool newLayouts) { Rectangle imageBounds = Rectangle.Empty; Rectangle textBounds = Rectangle.Empty; Size textSize = Size.Empty; Size imageSize = Size.Empty; ContentAlignment imageAlign = ImageAlignment; ContentAlignment textAlign = TextAlignment; TextImageRelation textImageRelation = TextImageRelation; //DK_2006_07_28 if (!newLayouts) { List <PreferredSizeData> prefSizelist = new List <PreferredSizeData>(); FillList(prefSizelist, fieldSize); } if (this.RightToLeft) { imageAlign = TelerikAlignHelper.RtlTranslateContent(ImageAlignment); textAlign = TelerikAlignHelper.RtlTranslateContent(TextAlignment); textImageRelation = TelerikAlignHelper.RtlTranslateRelation(TextImageRelation); } if (textElement != null) { textSize = newLayouts ? Size.Ceiling(textElement.DesiredSize) : Size.Ceiling(textElement.GetPreferredSize(fieldSize)); textSize = this.GetInvariantLength(textSize, textElement.Margin); } if (imageElement != null) { imageSize = newLayouts ? Size.Ceiling(imageElement.DesiredSize) : Size.Ceiling(imageElement.GetPreferredSize(fieldSize)); imageSize = this.GetInvariantLength(imageSize, imageElement.Margin); } if ((textElement != null) && IsPrimitiveNullOrEmpty(this.imageElement)) { Rectangle bounds = LayoutUtils.Align(textSize, new Rectangle(Point.Empty, fieldSize), textAlign); bounds.Size = Size.Subtract(textSize, textElement.Margin.Size); if (newLayouts) { textElement.Arrange(bounds); } else { textElement.Bounds = bounds; } return; } if ((imageElement != null) && IsPrimitiveNullOrEmpty(this.textElement)) { Rectangle bounds = LayoutUtils.Align(imageSize, new Rectangle(Point.Empty, fieldSize), imageAlign); bounds.Size = Size.Subtract(imageSize, imageElement.Margin.Size); if (newLayouts) { imageElement.Arrange(bounds); } else { imageElement.Bounds = bounds; } return; } // Subtract the image size from the whole field size Size textSizeLeft = LayoutUtils.SubAlignedRegion(fieldSize, imageSize, textImageRelation); textSize = newLayouts ? Size.Ceiling(this.textElement.DesiredSize) : Size.Ceiling(this.textElement.GetPreferredSize(textSizeLeft)); textSize = this.GetInvariantLength(textSize, textElement.Margin); // Create a new size based on the text size and the image size Size newSize = LayoutUtils.AddAlignedRegion(textSize, imageSize, textImageRelation); // The new field which is a union of the new sizes Rectangle maxFieldRectangle = Rectangle.Empty; maxFieldRectangle.Size = LayoutUtils.UnionSizes(fieldSize, newSize); // Image doesn't overlay text bool imageAlignNoOverlay = (TelerikAlignHelper.ImageAlignToRelation(imageAlign) & textImageRelation) != TextImageRelation.Overlay; // Text doesn't overlay image bool textAlignNoOverlay = (TelerikAlignHelper.TextAlignToRelation(textAlign) & textImageRelation) != TextImageRelation.Overlay; if (imageAlignNoOverlay) { LayoutUtils.SplitRegion(maxFieldRectangle, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); } else if (textAlignNoOverlay) { LayoutUtils.SplitRegion(maxFieldRectangle, textSize, (AnchorStyles)LayoutUtils.GetOppositeTextImageRelation(textImageRelation), out textBounds, out imageBounds); } else { // Both image overlays text and text overlays image if (textImageRelation == TextImageRelation.Overlay) { LayoutUtils.SplitRegion(maxFieldRectangle, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); } else { Rectangle alignedField = LayoutUtils.Align(newSize, maxFieldRectangle, ContentAlignment.MiddleCenter); LayoutUtils.SplitRegion(alignedField, imageSize, (AnchorStyles)textImageRelation, out imageBounds, out textBounds); LayoutUtils.ExpandRegionsToFillBounds(maxFieldRectangle, (AnchorStyles)textImageRelation, ref imageBounds, ref textBounds); } } //set image and text bounds according the size of the field if ((textImageRelation == TextImageRelation.TextBeforeImage) || (textImageRelation == TextImageRelation.ImageBeforeText)) { int num1 = Math.Min(textBounds.Bottom, layoutField.Bottom); textBounds.Y = Math.Max(Math.Min(textBounds.Y, layoutField.Y + ((layoutField.Height - textBounds.Height) / 2)), layoutField.Y); textBounds.Height = num1 - textBounds.Y; } if ((textImageRelation == TextImageRelation.TextAboveImage) || (textImageRelation == TextImageRelation.ImageAboveText)) { int num2 = Math.Min(textBounds.Right, layoutField.Right); textBounds.X = Math.Max(Math.Min(textBounds.X, layoutField.X + ((layoutField.Width - textBounds.Width) / 2)), layoutField.X); textBounds.Width = num2 - textBounds.X; } if ((textImageRelation == TextImageRelation.ImageBeforeText) && (imageBounds.Size.Width != 0)) { imageBounds.Width = Math.Max(0, Math.Min(fieldSize.Width - textBounds.Width, imageBounds.Width)); textBounds.X = imageBounds.X + imageBounds.Width; } if ((textImageRelation == TextImageRelation.ImageAboveText) && (imageBounds.Size.Height != 0)) { imageBounds.Height = Math.Max(0, Math.Min(fieldSize.Height - textBounds.Height, imageBounds.Height)); textBounds.Y = imageBounds.Y + imageBounds.Height; } textBounds = Rectangle.Intersect(textBounds, layoutField); //align image and if its size is greater than the field's size it is become offseted as much as the difference Rectangle newImageBounds = LayoutUtils.Align(imageSize, imageBounds, imageAlign); if (newImageBounds.Width > imageBounds.Width) { newImageBounds.X = imageBounds.Width - imageSize.Width; } if (newImageBounds.Height > imageBounds.Height) { newImageBounds.Y = imageBounds.Height - imageSize.Height; } textBounds = LayoutUtils.Align(textSize, textBounds, textAlign); textBounds.Size = Size.Subtract(textBounds.Size, this.textElement.Margin.Size); imageBounds.Size = Size.Subtract(newImageBounds.Size, this.imageElement.Margin.Size); if (newLayouts) { this.textElement.Arrange(textBounds); this.imageElement.Arrange(newImageBounds); } else { this.textElement.Bounds = textBounds; this.imageElement.Bounds = newImageBounds; } }