Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 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 | |||
| 28 | using System; |
||
| 29 | using System.Collections.Generic; |
||
| 30 | using System.Text; |
||
| 31 | using System.Diagnostics; |
||
| 32 | using System.Runtime.InteropServices; |
||
| 33 | |||
| 34 | namespace OpenTK.Platform.Windows |
||
| 35 | { |
||
| 36 | /// \internal |
||
| 37 | /// <summary>Describes a win32 window.</summary> |
||
| 38 | sealed class WinWindowInfo : IWindowInfo |
||
| 39 | { |
||
| 40 | IntPtr handle, dc; |
||
| 41 | WinWindowInfo parent; |
||
| 42 | bool disposed; |
||
| 43 | |||
| 44 | #region --- Constructors --- |
||
| 45 | |||
| 46 | /// <summary> |
||
| 47 | /// Constructs a new instance. |
||
| 48 | /// </summary> |
||
| 49 | public WinWindowInfo() |
||
| 50 | { |
||
| 51 | } |
||
| 52 | |||
| 53 | /// <summary> |
||
| 54 | /// Constructs a new instance with the specified window handle and paren.t |
||
| 55 | /// </summary> |
||
| 56 | /// <param name="handle">The window handle for this instance.</param> |
||
| 57 | /// <param name="parent">The parent window of this instance (may be null).</param> |
||
| 58 | public WinWindowInfo(IntPtr handle, WinWindowInfo parent) |
||
| 59 | { |
||
| 60 | this.handle = handle; |
||
| 61 | this.parent = parent; |
||
| 62 | } |
||
| 63 | |||
| 64 | #endregion |
||
| 65 | |||
| 66 | #region --- Public Methods --- |
||
| 67 | |||
| 68 | /// <summary> |
||
| 69 | /// Gets or sets the handle of the window. |
||
| 70 | /// </summary> |
||
| 71 | public IntPtr WindowHandle { get { return handle; } set { handle = value; } } |
||
| 72 | |||
| 73 | /// <summary> |
||
| 74 | /// Gets or sets the Parent of the window (may be null). |
||
| 75 | /// </summary> |
||
| 76 | public WinWindowInfo Parent { get { return parent; } set { parent = value; } } |
||
| 77 | |||
| 78 | /// <summary> |
||
| 79 | /// Gets the device context for this window instance. |
||
| 80 | /// </summary> |
||
| 81 | public IntPtr DeviceContext |
||
| 82 | { |
||
| 83 | get |
||
| 84 | { |
||
| 85 | if (dc == IntPtr.Zero) |
||
| 86 | dc = Functions.GetDC(this.WindowHandle); |
||
| 87 | //dc = Functions.GetWindowDC(this.WindowHandle); |
||
| 88 | return dc; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | #region public override string ToString() |
||
| 93 | |||
| 94 | /// <summary>Returns a System.String that represents the current window.</summary> |
||
| 95 | /// <returns>A System.String that represents the current window.</returns> |
||
| 96 | public override string ToString() |
||
| 97 | { |
||
| 98 | return String.Format("Windows.WindowInfo: Handle {0}, Parent ({1})", |
||
| 99 | this.WindowHandle, this.Parent != null ? this.Parent.ToString() : "null"); |
||
| 100 | } |
||
| 101 | |||
| 102 | /// <summary>Checks if <c>this</c> and <c>obj</c> reference the same win32 window.</summary> |
||
| 103 | /// <param name="obj">The object to check against.</param> |
||
| 104 | /// <returns>True if <c>this</c> and <c>obj</c> reference the same win32 window; false otherwise.</returns> |
||
| 105 | public override bool Equals(object obj) |
||
| 106 | { |
||
| 107 | if (obj == null) return false; |
||
| 108 | if (this.GetType() != obj.GetType()) return false; |
||
| 109 | WinWindowInfo info = (WinWindowInfo)obj; |
||
| 110 | |||
| 111 | if (info == null) return false; |
||
| 112 | // TODO: Assumes windows will always have unique handles. |
||
| 113 | return handle.Equals(info.handle); |
||
| 114 | } |
||
| 115 | |||
| 116 | /// <summary>Returns the hash code for this instance.</summary> |
||
| 117 | /// <returns>A hash code for the current <c>WinWindowInfo</c>.</returns> |
||
| 118 | public override int GetHashCode() |
||
| 119 | { |
||
| 120 | return handle.GetHashCode(); |
||
| 121 | } |
||
| 122 | |||
| 123 | #endregion |
||
| 124 | |||
| 125 | #endregion |
||
| 126 | |||
| 127 | #region --- IDisposable --- |
||
| 128 | |||
| 129 | #region public void Dispose() |
||
| 130 | |||
| 131 | /// <summary>Releases the unmanaged resources consumed by this instance.</summary> |
||
| 132 | public void Dispose() |
||
| 133 | { |
||
| 134 | this.Dispose(true); |
||
| 135 | GC.SuppressFinalize(this); |
||
| 136 | } |
||
| 137 | |||
| 138 | #endregion |
||
| 139 | |||
| 140 | #region void Dispose(bool manual) |
||
| 141 | |||
| 142 | void Dispose(bool manual) |
||
| 143 | { |
||
| 144 | if (!disposed) |
||
| 145 | { |
||
| 146 | if (this.dc != IntPtr.Zero) |
||
| 147 | if (!Functions.ReleaseDC(this.handle, this.dc)) |
||
| 148 | Debug.Print("[Warning] Failed to release device context {0}. Windows error: {1}.", this.dc, Marshal.GetLastWin32Error()); |
||
| 149 | |||
| 150 | if (manual) |
||
| 151 | { |
||
| 152 | if (parent != null) |
||
| 153 | parent.Dispose(); |
||
| 154 | } |
||
| 155 | |||
| 156 | disposed = true; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | #endregion |
||
| 161 | |||
| 162 | #region ~WinWindowInfo() |
||
| 163 | |||
| 164 | ~WinWindowInfo() |
||
| 165 | { |
||
| 166 | this.Dispose(false); |
||
| 167 | } |
||
| 168 | |||
| 169 | #endregion |
||
| 170 | |||
| 171 | #endregion |
||
| 172 | } |
||
| 173 | } |