00001 #region --- License ---
00002
00003
00004
00005 #endregion
00006
00007 #region --- Using Directives ---
00008
00009 using System;
00010 using System.Collections.Generic;
00011 using System.Text;
00012 using System.Windows.Forms;
00013 using System.Runtime.InteropServices;
00014 using System.Reflection;
00015 using System.Diagnostics;
00016 using OpenTK.Graphics;
00017
00018 #endregion
00019
00020 namespace OpenTK.Platform
00021 {
00025 public static class Utilities
00026 {
00027 #region internal static bool ThrowOnX11Error
00028
00029 static bool throw_on_error;
00030 internal static bool ThrowOnX11Error
00031 {
00032 get { return throw_on_error; }
00033 set
00034 {
00035 if (value && !throw_on_error)
00036 {
00037 Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms")
00038 .GetField("ErrorExceptions", System.Reflection.BindingFlags.Static |
00039 System.Reflection.BindingFlags.NonPublic)
00040 .SetValue(null, true);
00041 throw_on_error = true;
00042 }
00043 else if (!value && throw_on_error)
00044 {
00045 Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms")
00046 .GetField("ErrorExceptions", System.Reflection.BindingFlags.Static |
00047 System.Reflection.BindingFlags.NonPublic)
00048 .SetValue(null, false);
00049 throw_on_error = false;
00050 }
00051 }
00052 }
00053
00054 #endregion
00055
00056 #region internal static void LoadExtensions(Type type)
00057
00058 delegate Delegate LoadDelegateFunction(string name, Type signature);
00059
00073 internal static void LoadExtensions(Type type)
00074 {
00075
00076
00077
00078
00079 int supported = 0;
00080 Type extensions_class = type.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00081 if (extensions_class == null)
00082 throw new InvalidOperationException("The specified type does not have any loadable extensions.");
00083
00084 FieldInfo[] delegates = extensions_class.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00085 if (delegates == null)
00086 throw new InvalidOperationException("The specified type does not have any loadable extensions.");
00087
00088 MethodInfo load_delegate_method_info = type.GetMethod("LoadDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00089 if (load_delegate_method_info == null)
00090 throw new InvalidOperationException(type.ToString() + " does not contain a static LoadDelegate method.");
00091 LoadDelegateFunction LoadDelegate = (LoadDelegateFunction)Delegate.CreateDelegate(
00092 typeof(LoadDelegateFunction), load_delegate_method_info);
00093
00094 Debug.Write("Load extensions for " + type.ToString() + "... ");
00095
00096 System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch();
00097 time.Reset();
00098 time.Start();
00099
00100 foreach (FieldInfo f in delegates)
00101 {
00102 Delegate d = LoadDelegate(f.Name, f.FieldType);
00103 if (d != null)
00104 ++supported;
00105
00106 f.SetValue(null, d);
00107 }
00108
00109 FieldInfo rebuildExtensionList = type.GetField("rebuildExtensionList", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00110 if (rebuildExtensionList != null)
00111 rebuildExtensionList.SetValue(null, true);
00112
00113 time.Stop();
00114 Debug.Print("{0} extensions loaded in {1} ms.", supported, time.ElapsedMilliseconds);
00115 time.Reset();
00116 }
00117
00118 #endregion
00119
00120 #region internal static bool TryLoadExtension(Type type, string extension)
00121
00136 internal static bool TryLoadExtension(Type type, string extension)
00137 {
00138 Type extensions_class = type.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00139 if (extensions_class == null)
00140 {
00141 Debug.Print(type.ToString(), " does not contain extensions.");
00142 return false;
00143 }
00144
00145 LoadDelegateFunction LoadDelegate = (LoadDelegateFunction)Delegate.CreateDelegate(typeof(LoadDelegateFunction),
00146 type.GetMethod("LoadDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
00147 if (LoadDelegate == null)
00148 {
00149 Debug.Print(type.ToString(), " does not contain a static LoadDelegate method.");
00150 return false;
00151 }
00152
00153 FieldInfo f = extensions_class.GetField(extension, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00154 if (f == null)
00155 {
00156 Debug.Print("Extension \"", extension, "\" not found in ", type.ToString());
00157 return false;
00158 }
00159
00160 Delegate old = f.GetValue(null) as Delegate;
00161 Delegate @new = LoadDelegate(f.Name, f.FieldType);
00162 if ((old != null ? old.Target : null) != (@new != null ? @new.Target : null))
00163 {
00164 f.SetValue(null, @new);
00165 FieldInfo rebuildExtensionList = type.GetField("rebuildExtensionList", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
00166 if (rebuildExtensionList != null)
00167 rebuildExtensionList.SetValue(null, true);
00168 }
00169 return @new != null;
00170 }
00171
00172 #endregion
00173
00174 #region --- Creating a Graphics Context ---
00175
00185 [Obsolete("Call new OpenTK.Graphics.GraphicsContext() directly, instead.")]
00186 public static IGraphicsContext CreateGraphicsContext(
00187 GraphicsMode mode, IWindowInfo window,
00188 int major, int minor, GraphicsContextFlags flags)
00189 {
00190 GraphicsContext context = new GraphicsContext(mode, window, major, minor, flags);
00191 context.MakeCurrent(window);
00192
00193 (context as IGraphicsContextInternal).LoadAll();
00194
00195 return context;
00196 }
00197
00198 #region CreateX11WindowInfo
00199
00209 public static IWindowInfo CreateX11WindowInfo(IntPtr display, int screen, IntPtr windowHandle, IntPtr rootWindow, IntPtr visualInfo)
00210 {
00211 Platform.X11.X11WindowInfo window = new OpenTK.Platform.X11.X11WindowInfo();
00212 window.Display = display;
00213 window.Screen = screen;
00214 window.WindowHandle = windowHandle;
00215 window.RootWindow = rootWindow;
00216 window.VisualInfo = (X11.XVisualInfo)Marshal.PtrToStructure(visualInfo, typeof(X11.XVisualInfo));
00217
00218 return window;
00219 }
00220
00221 #endregion
00222
00223 #region CreateWindowsWindowInfo
00224
00230 public static IWindowInfo CreateWindowsWindowInfo(IntPtr windowHandle)
00231 {
00232 return new OpenTK.Platform.Windows.WinWindowInfo(windowHandle, null);
00233 }
00234
00235 #endregion
00236
00237 #region CreateMacOSCarbonWindowInfo
00238
00246 public static IWindowInfo CreateMacOSCarbonWindowInfo(IntPtr windowHandle, bool ownHandle, bool isControl)
00247 {
00248 return new OpenTK.Platform.MacOS.CarbonWindowInfo(windowHandle, false, isControl);
00249 }
00250
00251 #endregion
00252
00253 #region CreateDummyWindowInfo
00254
00259 public static IWindowInfo CreateDummyWindowInfo()
00260 {
00261 return new Dummy.DummyWindowInfo();
00262 }
00263
00264 #endregion
00265
00266 #endregion
00267
00268
00269 }
00270 }