/ThirdParty/OpenTK.1.0/Source/OpenTK/Platform/Windows/WinWindowInfo.cs

https://github.com/virtualglobebook/OpenGlobe · C# · 173 lines · 90 code · 30 blank · 53 comment · 16 complexity · 8be974f0943338ce273b1e30540320e1 MD5 · raw file

  1. #region License
  2. //
  3. // The Open Toolkit Library License
  4. //
  5. // Copyright (c) 2006 - 2009 the Open Toolkit library.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights to
  10. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. // the Software, and to permit persons to whom the Software is furnished to do
  12. // so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. #endregion
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.Diagnostics;
  31. using System.Runtime.InteropServices;
  32. namespace OpenTK.Platform.Windows
  33. {
  34. /// \internal
  35. /// <summary>Describes a win32 window.</summary>
  36. sealed class WinWindowInfo : IWindowInfo
  37. {
  38. IntPtr handle, dc;
  39. WinWindowInfo parent;
  40. bool disposed;
  41. #region --- Constructors ---
  42. /// <summary>
  43. /// Constructs a new instance.
  44. /// </summary>
  45. public WinWindowInfo()
  46. {
  47. }
  48. /// <summary>
  49. /// Constructs a new instance with the specified window handle and paren.t
  50. /// </summary>
  51. /// <param name="handle">The window handle for this instance.</param>
  52. /// <param name="parent">The parent window of this instance (may be null).</param>
  53. public WinWindowInfo(IntPtr handle, WinWindowInfo parent)
  54. {
  55. this.handle = handle;
  56. this.parent = parent;
  57. }
  58. #endregion
  59. #region --- Public Methods ---
  60. /// <summary>
  61. /// Gets or sets the handle of the window.
  62. /// </summary>
  63. public IntPtr WindowHandle { get { return handle; } set { handle = value; } }
  64. /// <summary>
  65. /// Gets or sets the Parent of the window (may be null).
  66. /// </summary>
  67. public WinWindowInfo Parent { get { return parent; } set { parent = value; } }
  68. /// <summary>
  69. /// Gets the device context for this window instance.
  70. /// </summary>
  71. public IntPtr DeviceContext
  72. {
  73. get
  74. {
  75. if (dc == IntPtr.Zero)
  76. dc = Functions.GetDC(this.WindowHandle);
  77. //dc = Functions.GetWindowDC(this.WindowHandle);
  78. return dc;
  79. }
  80. }
  81. #region public override string ToString()
  82. /// <summary>Returns a System.String that represents the current window.</summary>
  83. /// <returns>A System.String that represents the current window.</returns>
  84. public override string ToString()
  85. {
  86. return String.Format("Windows.WindowInfo: Handle {0}, Parent ({1})",
  87. this.WindowHandle, this.Parent != null ? this.Parent.ToString() : "null");
  88. }
  89. /// <summary>Checks if <c>this</c> and <c>obj</c> reference the same win32 window.</summary>
  90. /// <param name="obj">The object to check against.</param>
  91. /// <returns>True if <c>this</c> and <c>obj</c> reference the same win32 window; false otherwise.</returns>
  92. public override bool Equals(object obj)
  93. {
  94. if (obj == null) return false;
  95. if (this.GetType() != obj.GetType()) return false;
  96. WinWindowInfo info = (WinWindowInfo)obj;
  97. if (info == null) return false;
  98. // TODO: Assumes windows will always have unique handles.
  99. return handle.Equals(info.handle);
  100. }
  101. /// <summary>Returns the hash code for this instance.</summary>
  102. /// <returns>A hash code for the current <c>WinWindowInfo</c>.</returns>
  103. public override int GetHashCode()
  104. {
  105. return handle.GetHashCode();
  106. }
  107. #endregion
  108. #endregion
  109. #region --- IDisposable ---
  110. #region public void Dispose()
  111. /// <summary>Releases the unmanaged resources consumed by this instance.</summary>
  112. public void Dispose()
  113. {
  114. this.Dispose(true);
  115. GC.SuppressFinalize(this);
  116. }
  117. #endregion
  118. #region void Dispose(bool manual)
  119. void Dispose(bool manual)
  120. {
  121. if (!disposed)
  122. {
  123. if (this.dc != IntPtr.Zero)
  124. if (!Functions.ReleaseDC(this.handle, this.dc))
  125. Debug.Print("[Warning] Failed to release device context {0}. Windows error: {1}.", this.dc, Marshal.GetLastWin32Error());
  126. if (manual)
  127. {
  128. if (parent != null)
  129. parent.Dispose();
  130. }
  131. disposed = true;
  132. }
  133. }
  134. #endregion
  135. #region ~WinWindowInfo()
  136. ~WinWindowInfo()
  137. {
  138. this.Dispose(false);
  139. }
  140. #endregion
  141. #endregion
  142. }
  143. }