[Mono-bugs] [Bug 61148][Wis] New - Fix to bug 60297

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sun, 4 Jul 2004 14:50:26 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by software@solmersa.com.

http://bugzilla.ximian.com/show_bug.cgi?id=61148

--- shadow/61148	2004-07-04 14:50:26.000000000 -0400
+++ shadow/61148.tmp.20511	2004-07-04 14:50:26.000000000 -0400
@@ -0,0 +1,169 @@
+Bug#: 61148
+Product: Mono: Class Libraries
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: Sys.Drawing.
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: software@solmersa.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Fix to bug 60297
+
+Some changes are needed in font.cs (System.Drawing), I pasted the changes 
+below.
+
+Basically I added a private member to hold the HFONT handle (hFont) when 
+initialized from a HFONT (method FromHfont), this is needed because there 
+is not conversion from a GdipFont to a HFONT.
+
+Also I fixed the method FromHfont, only the windows part, to :
+1. obtain a GdipGraphics
+2. Pass this GdipGraphics to GdipGetLogFontA, it was using a IntPtr.Zero 
+argument, wich caused an exception.
+3. Delete the GdipGraphics
+4. Hold the Hfont argument in hFont
+
+A minor fix in ToHfont for the windows version.
+
+I tested it in my machine and got it working, displaying some controls 
+(textbox, checkbox, label, tabcontrol, button), so I thing it's ok.
+
+
+
+Best Regards
+
+SDavila
+
+
+
+// Changes here
+
+		private IntPtr hFont = IntPtr.Zero;
+
+		public static Font FromHfont (IntPtr Hfont)
+		{
+			System.OperatingSystem	osInfo = 
+System.Environment.OSVersion;
+			IntPtr			newObject;
+			IntPtr			hdc;
+			IntPtr			oldFont;
+			FontStyle	
+	newStyle=FontStyle.Regular;
+			float			newSize;
+			LOGFONTA		lf = new LOGFONTA();
+
+			// Sanity. Should we throw an exception?
+			if (Hfont==IntPtr.Zero) {
+				Font result = new Font("Arial", (float)
+10.0, FontStyle.Regular);
+				return(result);
+			}
+
+			if ((int)osInfo.Platform==128) {
+				// If we're on Unix we use our private 
+gdiplus API to avoid Wine 
+				// dependencies in S.D
+
+				Status s = GDIPlus.GdipCreateFontFromHfont
+(Hfont, out newObject, ref lf);
+				GDIPlus.CheckStatus (s);
+			} else {
+
+				// This needs testing, but I don't have a 
+working win32 mono
+				// environment. 
+
+				// GetDC, SelectObject, ReleaseDC 
+GetTextMetric and GetFontFace are not 
+				// really GDIPlus, see gdipFunctions.cs
+
+				newStyle=FontStyle.Regular;
+
+				hdc=GDIPlus.GetDC (IntPtr.Zero);
+				int graphics;
+
+				if (hdc==IntPtr.Zero)
+					throw new ArgumentException ("hdc 
+is zero");
+				GDIPlus.GdipCreateFromHDC(hdc, out 
+graphics);
+				if (graphics == 0)
+					throw new ArgumentException 
+("graphics is zero");
+				oldFont=GDIPlus.SelectObject (hdc, Hfont);
+
+				if (oldFont == IntPtr.Zero)
+						throw new 
+ArgumentException ("An error in select object");
+				GDIPlus.CheckStatus 
+(GDIPlus.GdipCreateFontFromDC (hdc, out newObject));
+				if (newObject == IntPtr.Zero)
+					throw new ArgumentException ("An 
+error in createFontFromDC");
+
+				GDIPlus.CheckStatus 
+(GDIPlus.GdipGetLogFontA (newObject, new System.IntPtr(graphics),ref lf));
+				GDIPlus.GdipDeleteGraphics(new 
+System.IntPtr(graphics));
+				
+				GDIPlus.SelectObject (hdc, oldFont);
+				GDIPlus.ReleaseDC (hdc);
+			}
+
+			if (lf.lfItalic!=0) {
+				newStyle |= FontStyle.Italic;
+			}
+			if (lf.lfUnderline!=0) {
+				newStyle |= FontStyle.Underline;
+			}
+			if (lf.lfStrikeOut!=0) {
+				newStyle |= FontStyle.Strikeout;
+			}
+			if (lf.lfWeight>400) {
+				newStyle |= FontStyle.Bold;
+			}
+			if (lf.lfHeight<0) {
+				newSize=lf.lfHeight*-1;
+			} else {
+				newSize=lf.lfHeight;
+			}
+			Font rFont = new Font(newObject, lf.lfFaceName, 
+newStyle, newSize);
+			rFont.hFont = Hfont;
+
+			return (rFont);
+		}
+
+		public IntPtr ToHfont ()
+		{
+			IntPtr			Hfont;
+			System.OperatingSystem	osInfo = 
+System.Environment.OSVersion;
+
+			// Sanity. Should we throw an exception?
+			if (fontObject==IntPtr.Zero){			
+	
+				return(IntPtr.Zero);
+			}
+
+			if ((int)osInfo.Platform==128) {
+				// If we're on Unix we use our private 
+gdiplus API
+				GDIPlus.CheckStatus (GDIPlus.GdipGetHfont 
+(fontObject, out Hfont));
+			} else {
+				// This needs testing, but I don't have a 
+working win32 mono
+				// environment. 
+				return (hFont);
+
+			}
+			return(Hfont);
+		}