Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Licensed under the MIT/X11 license. |
||
| 3 | * Copyright (c) 2006-2008 the OpenTK team. |
||
| 4 | * This notice may not be removed. |
||
| 5 | * See license.txt for licensing detailed licensing details. |
||
| 6 | */ |
||
| 7 | #endregion |
||
| 8 | |||
| 9 | using System; |
||
| 10 | using System.Collections.Generic; |
||
| 11 | using System.Text; |
||
| 12 | using System.Diagnostics; |
||
| 13 | using System.Drawing; |
||
| 14 | |||
| 15 | namespace OpenTK |
||
| 16 | { |
||
| 17 | /// <summary>Contains information regarding a monitor's display resolution.</summary> |
||
| 18 | public class DisplayResolution |
||
| 19 | { |
||
| 20 | Rectangle bounds; |
||
| 21 | int bits_per_pixel; |
||
| 22 | float refresh_rate; |
||
| 23 | |||
| 24 | #region --- Constructors --- |
||
| 25 | |||
| 26 | internal DisplayResolution() { } |
||
| 27 | |||
| 28 | #region public DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate) |
||
| 29 | |||
| 30 | // Creates a new DisplayResolution object for the primary DisplayDevice. |
||
| 31 | internal DisplayResolution(int x, int y, int width, int height, int bitsPerPixel, float refreshRate) |
||
| 32 | { |
||
| 33 | // Refresh rate may be zero, since this information may not be available on some platforms. |
||
| 34 | if (width <= 0) throw new ArgumentOutOfRangeException("width", "Must be greater than zero."); |
||
| 35 | if (height <= 0) throw new ArgumentOutOfRangeException("height", "Must be greater than zero."); |
||
| 36 | if (bitsPerPixel <= 0) throw new ArgumentOutOfRangeException("bitsPerPixel", "Must be greater than zero."); |
||
| 37 | if (refreshRate < 0) throw new ArgumentOutOfRangeException("refreshRate", "Must be greater than, or equal to zero."); |
||
| 38 | |||
| 39 | this.bounds = new Rectangle(x, y, width, height); |
||
| 40 | this.bits_per_pixel = bitsPerPixel; |
||
| 41 | this.refresh_rate = refreshRate; |
||
| 42 | } |
||
| 43 | |||
| 44 | #endregion |
||
| 45 | |||
| 46 | #region public DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate, DisplayDevice device) |
||
| 47 | |||
| 48 | #if false |
||
| 49 | |||
| 50 | /// <summary> |
||
| 51 | /// Creates a new DisplayResolution object for the specified DisplayDevice. |
||
| 52 | /// </summary> |
||
| 53 | /// <param name="width">The requested width in pixels.</param> |
||
| 54 | /// <param name="height">The requested height in pixels.</param> |
||
| 55 | /// <param name="bitsPerPixel">The requested bits per pixel in bits.</param> |
||
| 56 | /// <param name="refreshRate">The requested refresh rate in hertz.</param> |
||
| 57 | /// <remarks>OpenTK will select the closest match between all available resolutions on the specified DisplayDevice.</remarks> |
||
| 58 | /// |
||
| 59 | public DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate, DisplayDevice device) |
||
| 60 | { |
||
| 61 | // Refresh rate may be zero, since this information may not be available on some platforms. |
||
| 62 | if (width <= 0) throw new ArgumentOutOfRangeException("width", "Must be greater than zero."); |
||
| 63 | if (height <= 0) throw new ArgumentOutOfRangeException("height", "Must be greater than zero."); |
||
| 64 | if (bitsPerPixel <= 0) throw new ArgumentOutOfRangeException("bitsPerPixel", "Must be greater than zero."); |
||
| 65 | if (refreshRate < 0) throw new ArgumentOutOfRangeException("refreshRate", "Must be greater than, or equal to zero."); |
||
| 66 | if (device == null) throw new ArgumentNullException("DisplayDevice", "Must be a valid DisplayDevice"); |
||
| 67 | |||
| 68 | DisplayResolution res = device.SelectResolution(width, height, bitsPerPixel, refreshRate); |
||
| 69 | |||
| 70 | this.width = res.width; |
||
| 71 | this.height = res.height; |
||
| 72 | this.bits_per_pixel = res.bits_per_pixel; |
||
| 73 | this.refresh_rate = res.refresh_rate; |
||
| 74 | } |
||
| 75 | #endif |
||
| 76 | #endregion |
||
| 77 | |||
| 78 | #endregion |
||
| 79 | |||
| 80 | #region --- Public Methods --- |
||
| 81 | |||
| 82 | #region Bounds |
||
| 83 | |||
| 84 | /// <summary> |
||
| 85 | /// Gets a System.Drawing.Rectangle that contains the bounds of this display device. |
||
| 86 | /// </summary> |
||
| 87 | [Obsolete("This property will return invalid results if a monitor changes resolution. Use DisplayDevice.Bounds instead.")] |
||
| 88 | [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] |
||
| 89 | public Rectangle Bounds |
||
| 90 | { |
||
| 91 | get { return bounds; } |
||
| 92 | } |
||
| 93 | |||
| 94 | #endregion |
||
| 95 | |||
| 96 | #region public int Width |
||
| 97 | |||
| 98 | /// <summary>Gets a System.Int32 that contains the width of this display in pixels.</summary> |
||
| 99 | public int Width |
||
| 100 | { |
||
| 101 | get { return bounds.Width; } |
||
| 102 | internal set { bounds.Width = value; } |
||
| 103 | } |
||
| 104 | |||
| 105 | #endregion |
||
| 106 | |||
| 107 | #region public int Height |
||
| 108 | |||
| 109 | /// <summary>Gets a System.Int32 that contains the height of this display in pixels.</summary> |
||
| 110 | public int Height |
||
| 111 | { |
||
| 112 | get { return bounds.Height; } |
||
| 113 | internal set { bounds.Height = value; } |
||
| 114 | } |
||
| 115 | |||
| 116 | #endregion |
||
| 117 | |||
| 118 | #region public int BitsPerPixel |
||
| 119 | |||
| 120 | /// <summary>Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32.</summary> |
||
| 121 | public int BitsPerPixel |
||
| 122 | { |
||
| 123 | get { return bits_per_pixel; } |
||
| 124 | internal set { bits_per_pixel = value; } |
||
| 125 | } |
||
| 126 | |||
| 127 | #endregion |
||
| 128 | |||
| 129 | #region public float RefreshRate |
||
| 130 | |||
| 131 | /// <summary> |
||
| 132 | /// Gets a System.Single representing the vertical refresh rate of this display. |
||
| 133 | /// </summary> |
||
| 134 | public float RefreshRate |
||
| 135 | { |
||
| 136 | get { return refresh_rate; } |
||
| 137 | internal set { refresh_rate = value; } |
||
| 138 | } |
||
| 139 | |||
| 140 | #endregion |
||
| 141 | |||
| 142 | #endregion |
||
| 143 | |||
| 144 | #region --- Overrides --- |
||
| 145 | |||
| 146 | #region public override string ToString() |
||
| 147 | |||
| 148 | /// <summary> |
||
| 149 | /// Returns a System.String representing this DisplayResolution. |
||
| 150 | /// </summary> |
||
| 151 | /// <returns>A System.String representing this DisplayResolution.</returns> |
||
| 152 | public override string ToString() |
||
| 153 | { |
||
| 154 | return String.Format("{0}x{1}@{2}Hz", Bounds, bits_per_pixel, refresh_rate); |
||
| 155 | } |
||
| 156 | |||
| 157 | #endregion |
||
| 158 | |||
| 159 | #region public override bool Equals(object obj) |
||
| 160 | |||
| 161 | /// <summary>Determines whether the specified resolutions are equal.</summary> |
||
| 162 | /// <param name="obj">The System.Object to check against.</param> |
||
| 163 | /// <returns>True if the System.Object is an equal DisplayResolution; false otherwise.</returns> |
||
| 164 | public override bool Equals(object obj) |
||
| 165 | { |
||
| 166 | if (obj == null) return false; |
||
| 167 | if (this.GetType() == obj.GetType()) |
||
| 168 | { |
||
| 169 | DisplayResolution res = (DisplayResolution)obj; |
||
| 170 | return |
||
| 171 | Width == res.Width && |
||
| 172 | Height == res.Height && |
||
| 173 | BitsPerPixel == res.BitsPerPixel && |
||
| 174 | RefreshRate == res.RefreshRate; |
||
| 175 | } |
||
| 176 | |||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | #endregion |
||
| 181 | |||
| 182 | #region public override int GetHashCode() |
||
| 183 | |||
| 184 | /// <summary>Returns a unique hash representing this resolution.</summary> |
||
| 185 | /// <returns>A System.Int32 that may serve as a hash code for this resolution.</returns> |
||
| 186 | public override int GetHashCode() |
||
| 187 | { |
||
| 188 | return Bounds.GetHashCode() ^ bits_per_pixel ^ refresh_rate.GetHashCode(); |
||
| 189 | } |
||
| 190 | |||
| 191 | #endregion |
||
| 192 | |||
| 193 | #endregion |
||
| 194 | |||
| 195 | #region --- Operator Overloads --- |
||
| 196 | |||
| 197 | /// <summary> |
||
| 198 | /// Compares two instances for equality. |
||
| 199 | /// </summary> |
||
| 200 | /// <param name="left">The first instance.</param> |
||
| 201 | /// <param name="right">The second instance.</param> |
||
| 202 | /// <returns>True, if left equals right; false otherwise.</returns> |
||
| 203 | public static bool operator== (DisplayResolution left, DisplayResolution right) |
||
| 204 | { |
||
| 205 | if (((object)left) == null && ((object)right) == null) |
||
| 206 | return true; |
||
| 207 | else if ((((object)left) == null && ((object)right) != null) || |
||
| 208 | (((object)left) != null && ((object)right) == null)) |
||
| 209 | return false; |
||
| 210 | return left.Equals(right); |
||
| 211 | } |
||
| 212 | |||
| 213 | /// <summary> |
||
| 214 | /// Compares two instances for inequality. |
||
| 215 | /// </summary> |
||
| 216 | /// <param name="left">The first instance.</param> |
||
| 217 | /// <param name="right">The second instance.</param> |
||
| 218 | /// <returns>True, if left does not equal right; false otherwise.</returns> |
||
| 219 | public static bool operator !=(DisplayResolution left, DisplayResolution right) |
||
| 220 | { |
||
| 221 | return !(left == right); |
||
| 222 | } |
||
| 223 | |||
| 224 | #endregion |
||
| 225 | } |
||
| 226 | } |