From 2af357c10607a2d787c5243f321ee6444d030672 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Fri, 6 Nov 2009 09:49:06 +0000 Subject: [PATCH] * DisplayDevice.cs: Made DisplayDevice properties settable internally (simplifies device detection code). Bounds property is now independent of the default resolution. * DisplayResolution.cs: Obsoleted the Bounds property, as this becomes invalid as soon as the resolution of any display device is changed. --- Source/OpenTK/DisplayDevice.cs | 71 +++++++++++++++++++++++------- Source/OpenTK/DisplayResolution.cs | 25 +++++++++-- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/Source/OpenTK/DisplayDevice.cs b/Source/OpenTK/DisplayDevice.cs index 0862b76c..82863555 100644 --- a/Source/OpenTK/DisplayDevice.cs +++ b/Source/OpenTK/DisplayDevice.cs @@ -1,4 +1,4 @@ -#region --- License --- +#region --- License --- /* Licensed under the MIT/X11 license. * Copyright (c) 2006-2008 the OpenTK team. * This notice may not be removed. @@ -29,11 +29,13 @@ namespace OpenTK #region --- Fields --- - DisplayResolution current_resolution, original_resolution; - readonly List available_resolutions = new List(); - readonly IList available_resolutions_readonly; + DisplayResolution current_resolution = new DisplayResolution(), original_resolution; + List available_resolutions = new List(); + IList available_resolutions_readonly; bool primary; + Rectangle bounds; + static readonly List available_displays = new List(); static readonly IList available_displays_readonly; static readonly object display_lock = new object(); @@ -51,16 +53,8 @@ namespace OpenTK available_displays_readonly = available_displays.AsReadOnly(); } - internal DisplayDevice(DisplayResolution currentResolution, bool primary, - IEnumerable availableResolutions) + internal DisplayDevice() { - this.current_resolution = currentResolution; - this.primary = primary; - this.available_resolutions.AddRange(availableResolutions); - - Debug.Print("DisplayDevice {0} ({1}) supports {2} resolutions.", - available_displays.Count, primary ? "primary" : "secondary", available_resolutions.Count); - lock (display_lock) { available_displays.Add(this); @@ -71,6 +65,18 @@ namespace OpenTK available_resolutions_readonly = available_resolutions.AsReadOnly(); } + internal DisplayDevice(DisplayResolution currentResolution, bool primary, + IEnumerable availableResolutions, Rectangle bounds) + : this() + { + this.current_resolution = currentResolution; + this.primary = primary; + this.available_resolutions.AddRange(availableResolutions); + + Debug.Print("DisplayDevice {0} ({1}) supports {2} resolutions.", + available_displays.Count, primary ? "primary" : "secondary", available_resolutions.Count); + } + #endregion #region --- Public Methods --- @@ -78,11 +84,17 @@ namespace OpenTK #region public Rectangle Bounds /// - /// Gets a System.Drawing.Rectangle that contains the current bounds of this DisplayDevice. + /// Gets the bounds of this instance in pixel coordinates.. /// public Rectangle Bounds { - get { return current_resolution.Bounds; } + get { return bounds; } + internal set + { + bounds = value; + current_resolution.Height = bounds.Height; + current_resolution.Width = bounds.Width; + } } #endregion @@ -104,7 +116,11 @@ namespace OpenTK #region public int BitsPerPixel /// Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32. - public int BitsPerPixel { get { return current_resolution.BitsPerPixel; } } + public int BitsPerPixel + { + get { return current_resolution.BitsPerPixel; } + internal set { current_resolution.BitsPerPixel = value; } + } #endregion @@ -116,6 +132,7 @@ namespace OpenTK public float RefreshRate { get { return current_resolution.RefreshRate; } + internal set { current_resolution.RefreshRate = value; } } #endregion @@ -123,7 +140,22 @@ namespace OpenTK #region public bool IsPrimary /// Gets a System.Boolean that indicates whether this Display is the primary Display in systems with multiple Displays. - public bool IsPrimary { get { return primary; } } + public bool IsPrimary + { + get { return primary; } + internal set + { + if (primary_display != null && primary_display != this) + primary_display.IsPrimary = false; + + lock (display_lock) + { + primary = value; + if (value) + primary_display = this; + } + } + } #endregion @@ -167,6 +199,11 @@ namespace OpenTK public IList AvailableResolutions { get { return available_resolutions_readonly; } + internal set + { + available_resolutions = (List)value; + available_resolutions_readonly = available_resolutions.AsReadOnly(); + } } #endregion diff --git a/Source/OpenTK/DisplayResolution.cs b/Source/OpenTK/DisplayResolution.cs index 56f71a6a..3d5b17b8 100644 --- a/Source/OpenTK/DisplayResolution.cs +++ b/Source/OpenTK/DisplayResolution.cs @@ -1,4 +1,4 @@ -#region --- License --- +#region --- License --- /* Licensed under the MIT/X11 license. * Copyright (c) 2006-2008 the OpenTK team. * This notice may not be removed. @@ -23,6 +23,8 @@ namespace OpenTK #region --- Constructors --- + internal DisplayResolution() { } + #region public DisplayResolution(int width, int height, int bitsPerPixel, float refreshRate) // Creates a new DisplayResolution object for the primary DisplayDevice. @@ -82,6 +84,8 @@ namespace OpenTK /// /// Gets a System.Drawing.Rectangle that contains the bounds of this display device. /// + [Obsolete("This property will return invalid results if a monitor changes resolution. Use DisplayDevice.Bounds instead.")] + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public Rectangle Bounds { get { return bounds; } @@ -92,21 +96,33 @@ namespace OpenTK #region public int Width /// Gets a System.Int32 that contains the width of this display in pixels. - public int Width { get { return bounds.Width; } } + public int Width + { + get { return bounds.Width; } + internal set { bounds.Width = value; } + } #endregion #region public int Height /// Gets a System.Int32 that contains the height of this display in pixels. - public int Height { get { return bounds.Height; } } + public int Height + { + get { return bounds.Height; } + internal set { bounds.Height = value; } + } #endregion #region public int BitsPerPixel /// Gets a System.Int32 that contains number of bits per pixel of this display. Typical values include 8, 16, 24 and 32. - public int BitsPerPixel { get { return bits_per_pixel; } } + public int BitsPerPixel + { + get { return bits_per_pixel; } + internal set { bits_per_pixel = value; } + } #endregion @@ -118,6 +134,7 @@ namespace OpenTK public float RefreshRate { get { return refresh_rate; } + internal set { refresh_rate = value; } } #endregion