Browse Source

Merge pull request #1348 from ermshiperete/ImproveButtonTextLayout

[MWF] Improve button text layout
Miguel de Icaza 11 years ago
parent
commit
84472a332e

+ 11 - 3
mcs/class/Managed.Windows.Forms/System.Windows.Forms/TextRenderer.cs

@@ -258,11 +258,19 @@ namespace System.Windows.Forms
 			StringFormat sf = FlagsToStringFormat (flags);
 			StringFormat sf = FlagsToStringFormat (flags);
 
 
 				Size retval;
 				Size retval;
-				
+
+				int proposedWidth;
+				if (proposedSize.Width == 0)
+					proposedWidth = Int32.MaxValue;
+				else {
+					proposedWidth = proposedSize.Width;
+					if ((flags & TextFormatFlags.NoPadding) == 0)
+						proposedWidth -= 9;
+				}
 				if (dc is Graphics)
 				if (dc is Graphics)
-					retval = (dc as Graphics).MeasureString (text, font, proposedSize.Width == 0 ? Int32.MaxValue : proposedSize.Width, sf).ToSize ();
+					retval = (dc as Graphics).MeasureString (text, font, proposedWidth, sf).ToSize ();
 				else
 				else
-					retval = TextRenderer.MeasureString (text, font, proposedSize.Width == 0 ? Int32.MaxValue : proposedSize.Width, sf).ToSize ();
+					retval = TextRenderer.MeasureString (text, font, proposedWidth, sf).ToSize ();
 
 
 				if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0)
 				if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0)
 					retval.Width += 9;
 					retval.Width += 9;

+ 12 - 13
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ThemeWin32Classic.cs

@@ -369,21 +369,18 @@ namespace System.Windows.Forms
 			Image image = button.Image;
 			Image image = button.Image;
 			string text = button.Text;
 			string text = button.Text;
 			Rectangle content_rect = button.PaddingClientRectangle;
 			Rectangle content_rect = button.PaddingClientRectangle;
-			if (button.TextImageRelation != TextImageRelation.Overlay)
-				content_rect.Inflate(-4, -4);
-			Size text_size = TextRenderer.MeasureTextInternal (g, text, button.Font, content_rect.Size, button.TextFormatFlags, button.UseCompatibleTextRendering);
+			Size text_size = TextRenderer.MeasureTextInternal (g, text, button.Font, content_rect.Size, button.TextFormatFlags | TextFormatFlags.NoPadding, button.UseCompatibleTextRendering);
 			Size image_size = image == null ? Size.Empty : image.Size;
 			Size image_size = image == null ? Size.Empty : image.Size;
 
 
-			textRectangle = Rectangle.Empty;
+			textRectangle = Rectangle.Inflate (content_rect, -4, -4);
 			imageRectangle = Rectangle.Empty;
 			imageRectangle = Rectangle.Empty;
 			
 			
 			switch (button.TextImageRelation) {
 			switch (button.TextImageRelation) {
 				case TextImageRelation.Overlay:
 				case TextImageRelation.Overlay:
 					// Overlay is easy, text always goes here
 					// Overlay is easy, text always goes here
-					textRectangle = Rectangle.Inflate (content_rect, -4, -4);
 
 
-					if (button.Pressed)
-						textRectangle.Offset (1, 1);
+						if (button.Pressed)
+							textRectangle.Offset (1, 1);
 						
 						
 					// Image is dependent on ImageAlign
 					// Image is dependent on ImageAlign
 					if (image == null)
 					if (image == null)
@@ -440,16 +437,16 @@ namespace System.Windows.Forms
 					imageRectangle = new Rectangle (image_x, image_y, image_width, image_height);
 					imageRectangle = new Rectangle (image_x, image_y, image_width, image_height);
 					break;
 					break;
 				case TextImageRelation.ImageAboveText:
 				case TextImageRelation.ImageAboveText:
-					LayoutTextAboveOrBelowImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+					LayoutTextAboveOrBelowImage (textRectangle, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
 					break;
 					break;
 				case TextImageRelation.TextAboveImage:
 				case TextImageRelation.TextAboveImage:
-					LayoutTextAboveOrBelowImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+					LayoutTextAboveOrBelowImage (textRectangle, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
 					break;
 					break;
 				case TextImageRelation.ImageBeforeText:
 				case TextImageRelation.ImageBeforeText:
-					LayoutTextBeforeOrAfterImage (content_rect, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+					LayoutTextBeforeOrAfterImage (textRectangle, false, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
 					break;
 					break;
 				case TextImageRelation.TextBeforeImage:
 				case TextImageRelation.TextBeforeImage:
-					LayoutTextBeforeOrAfterImage (content_rect, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
+					LayoutTextBeforeOrAfterImage (textRectangle, true, text_size, image_size, button.TextAlign, button.ImageAlign, out textRectangle, out imageRectangle);
 					break;
 					break;
 			}
 			}
 		}
 		}
@@ -535,12 +532,14 @@ namespace System.Windows.Forms
 				offset += (int)(2 * (excess_height / 3));
 				offset += (int)(2 * (excess_height / 3));
 
 
 			if (textFirst) {
 			if (textFirst) {
-				final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, totalArea.Top + offset, textSize.Width, textSize.Height);
+				var textHeight = excess_height >= 0 ? totalArea.Height - imageSize.Height - element_spacing: textSize.Height;
+				final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, totalArea.Top + offset, textSize.Width, textHeight);
 				final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, final_text_rect.Bottom + element_spacing, imageSize.Width, imageSize.Height);
 				final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, final_text_rect.Bottom + element_spacing, imageSize.Width, imageSize.Height);
 			}
 			}
 			else {
 			else {
 				final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, totalArea.Top + offset, imageSize.Width, imageSize.Height);
 				final_image_rect = new Rectangle (AlignInRectangle (totalArea, imageSize, imageAlign).Left, totalArea.Top + offset, imageSize.Width, imageSize.Height);
-				final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, final_image_rect.Bottom + element_spacing, textSize.Width, textSize.Height);
+				var textHeight = excess_height >= 0 ? totalArea.Height - final_image_rect.Height : textSize.Height;
+				final_text_rect = new Rectangle (AlignInRectangle (totalArea, textSize, textAlign).Left, final_image_rect.Bottom + element_spacing, textSize.Width, textHeight);
 				
 				
 				if (final_text_rect.Bottom > totalArea.Bottom)
 				if (final_text_rect.Bottom > totalArea.Bottom)
 					final_text_rect.Y = totalArea.Top;
 					final_text_rect.Y = totalArea.Top;