From f218071fa74b1a684182271db2514316da813be7 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 3 Nov 2008 23:34:54 +0000 Subject: [PATCH] Use different implementations for accessing GDI+ internals on .Net and Mono. --- Source/OpenTK/GdiPlus.cs | 34 ++++++-------- Source/OpenTK/Platform/IGdiPlusInternals.cs | 21 +++++++++ .../Platform/Windows/WinGdiPlusInternals.cs | 46 +++++++++++++++++++ .../Platform/X11/X11GdiPlusInternals.cs | 28 +++++++++++ 4 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 Source/OpenTK/Platform/IGdiPlusInternals.cs create mode 100644 Source/OpenTK/Platform/Windows/WinGdiPlusInternals.cs create mode 100644 Source/OpenTK/Platform/X11/X11GdiPlusInternals.cs diff --git a/Source/OpenTK/GdiPlus.cs b/Source/OpenTK/GdiPlus.cs index b8f16ca8..295fabb9 100644 --- a/Source/OpenTK/GdiPlus.cs +++ b/Source/OpenTK/GdiPlus.cs @@ -18,6 +18,8 @@ namespace OpenTK.Platform { internal static class GdiPlus { + static IGdiPlusInternals internals; + const string gdi_plus_library = "gdiplus.dll"; static readonly PropertyInfo native_graphics_property, native_font_property; static readonly FieldInfo native_string_format_field; @@ -26,33 +28,29 @@ namespace OpenTK.Platform static GdiPlus() { - native_graphics_property = - typeof(System.Drawing.Graphics).GetProperty("NativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic); - - native_font_property = - typeof(System.Drawing.Font).GetProperty("NativeFont", BindingFlags.Instance | BindingFlags.NonPublic); - - native_string_format_field = - typeof(System.Drawing.StringFormat).GetField("nativeFormat", BindingFlags.Instance | BindingFlags.NonPublic); + if (Configuration.RunningOnWindows) + internals = new Windows.WinGdiPlusInternals(); + else + internals = new X11.X11GdiPlusInternals(); } #endregion - #region --- Reflection --- + #region --- Public Methods --- public static IntPtr GetNativeGraphics(System.Drawing.Graphics graphics) { - return (IntPtr)native_graphics_property.GetValue(graphics, null); + return internals.GetNativeGraphics(graphics); } public static IntPtr GetNativeFont(Font font) { - return (IntPtr)native_font_property.GetValue(font, null); + return internals.GetNativeFont(font); } public static IntPtr GetNativeStringFormat(StringFormat format) { - return (IntPtr)native_string_format_field.GetValue(format); + return internals.GetNativeStringFormat(format); } public static int MaxMeasurableCharacterRanges @@ -63,11 +61,7 @@ namespace OpenTK.Platform } } - #endregion - - #region --- Methods --- - - [DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint="GdipSetStringFormatMeasurableCharacterRanges")] + [DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint = "GdipSetStringFormatMeasurableCharacterRanges")] public static extern int SetStringFormatMeasurableCharacterRanges(HandleRef format, int rangeCount, [In, Out] CharacterRange[] range); [DllImport(gdi_plus_library, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true, EntryPoint = "GdipGetStringFormatMeasurableCharacterRangeCount")] @@ -87,6 +81,7 @@ namespace OpenTK.Platform #endregion +#if false [StructLayout(LayoutKind.Sequential)] internal struct GPRECTF { @@ -122,9 +117,6 @@ namespace OpenTK.Platform return new RectangleF(this.X, this.Y, this.Width, this.Height); } } - - +#endif } - - } diff --git a/Source/OpenTK/Platform/IGdiPlusInternals.cs b/Source/OpenTK/Platform/IGdiPlusInternals.cs new file mode 100644 index 00000000..3304d10b --- /dev/null +++ b/Source/OpenTK/Platform/IGdiPlusInternals.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Platform +{ + // Provides methods to access internal GdiPlus fields. This is necessary for + // managed <-> native GdiPlus interoperability. + // Note that the fields are named differently between .Net and Mono. + // GdiPlus is considered deprecated by Microsoft - it is highly unlikely that + // future framework upgrades will break this code, but it is something to + // keep in mind. + interface IGdiPlusInternals + { + IntPtr GetNativeGraphics(System.Drawing.Graphics graphics); + + IntPtr GetNativeFont(System.Drawing.Font font); + + IntPtr GetNativeStringFormat(System.Drawing.StringFormat format); + } +} diff --git a/Source/OpenTK/Platform/Windows/WinGdiPlusInternals.cs b/Source/OpenTK/Platform/Windows/WinGdiPlusInternals.cs new file mode 100644 index 00000000..7f32c17f --- /dev/null +++ b/Source/OpenTK/Platform/Windows/WinGdiPlusInternals.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.Reflection; + +namespace OpenTK.Platform.Windows +{ + class WinGdiPlusInternals : IGdiPlusInternals + { + const string gdi_plus_library = "gdiplus.dll"; + static readonly PropertyInfo native_graphics_property, native_font_property; + static readonly FieldInfo native_string_format_field; + + static WinGdiPlusInternals() + { + native_graphics_property = + typeof(System.Drawing.Graphics).GetProperty("NativeGraphics", BindingFlags.Instance | BindingFlags.NonPublic); + + native_font_property = + typeof(System.Drawing.Font).GetProperty("NativeFont", BindingFlags.Instance | BindingFlags.NonPublic); + + native_string_format_field = + typeof(System.Drawing.StringFormat).GetField("nativeFormat", BindingFlags.Instance | BindingFlags.NonPublic); + } + + #region --- IGdiPlusInternals Members --- + + public IntPtr GetNativeGraphics(System.Drawing.Graphics graphics) + { + return (IntPtr)native_graphics_property.GetValue(graphics, null); + } + + public IntPtr GetNativeFont(Font font) + { + return (IntPtr)native_font_property.GetValue(font, null); + } + + public IntPtr GetNativeStringFormat(StringFormat format) + { + return (IntPtr)native_string_format_field.GetValue(format); + } + + #endregion + } +} diff --git a/Source/OpenTK/Platform/X11/X11GdiPlusInternals.cs b/Source/OpenTK/Platform/X11/X11GdiPlusInternals.cs new file mode 100644 index 00000000..6459cd15 --- /dev/null +++ b/Source/OpenTK/Platform/X11/X11GdiPlusInternals.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenTK.Platform.X11 +{ + class X11GdiPlusInternals : IGdiPlusInternals + { + #region IGdiPlusInternals Members + + public IntPtr GetNativeGraphics(System.Drawing.Graphics graphics) + { + throw new NotImplementedException(); + } + + public IntPtr GetNativeFont(System.Drawing.Font font) + { + throw new NotImplementedException(); + } + + public IntPtr GetNativeStringFormat(System.Drawing.StringFormat format) + { + throw new NotImplementedException(); + } + + #endregion + } +}