From 8cf88a138c6dc94bc1cebf19c57324b70d67ac87 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 13 Jul 2018 01:25:53 +1000 Subject: [PATCH] eliminate ClientRectangle, ClientSize is good enough --- OpenTK/INativeWindow.cs | 7 +-- OpenTK/Mouse.cs | 3 +- OpenTK/Platform/MacOS/CarbonBindings/Agl.cs | 34 +++++++-------- .../MacOS/CarbonBindings/CarbonAPI.cs | 27 +----------- OpenTK/Platform/MacOS/CarbonWindow.cs | 32 ++++++-------- OpenTK/Platform/Windows/WinWindow.cs | 43 ++++++++----------- OpenTK/Platform/X11/X11GLContext.cs | 24 +++++------ OpenTK/Platform/X11/X11Window.cs | 29 +++++-------- src/Client/WinWindow.c | 37 +++++++--------- src/Client/Window.h | 2 - 10 files changed, 86 insertions(+), 152 deletions(-) diff --git a/OpenTK/INativeWindow.cs b/OpenTK/INativeWindow.cs index 65d112480..536af5436 100644 --- a/OpenTK/INativeWindow.cs +++ b/OpenTK/INativeWindow.cs @@ -74,11 +74,8 @@ namespace OpenTK { /// Gets or sets a structure that contains the external size of this window. public abstract Size Size { get; set; } - /// Gets or sets a structure that contains the internal bounds of this window, in client coordinates. - /// The internal bounds include the drawing area of the window, but exclude the titlebar and window borders. - public abstract Rectangle ClientRectangle { get; set; } - - /// Gets or sets a structure that contains the internal size this window. + /// Gets or sets a structure that contains the internal size of this window; + /// The internal size include the drawing area of the window, but exclude the titlebar and window borders. public abstract Size ClientSize { get; set; } /// Closes this window. diff --git a/OpenTK/Mouse.cs b/OpenTK/Mouse.cs index dbea6e778..51c8d766c 100644 --- a/OpenTK/Mouse.cs +++ b/OpenTK/Mouse.cs @@ -25,8 +25,7 @@ // #endregion -using System; -using System.Drawing; +using System; namespace OpenTK.Input { diff --git a/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs b/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs index 3a30468ec..f82dfbf48 100644 --- a/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs +++ b/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs @@ -3,15 +3,11 @@ using System; using System.Runtime.InteropServices; -using AGLContext = System.IntPtr; -using AGLDevice = System.IntPtr; -using AGLDrawable = System.IntPtr; -using AGLPixelFormat = System.IntPtr; namespace OpenTK.Platform.MacOS { public unsafe static class Agl { - const string agl = "/System/Library/Frameworks/AGL.framework/Versions/Current/AGL"; + const string lib = "/System/Library/Frameworks/AGL.framework/Versions/Current/AGL"; // Attribute names for aglChoosePixelFormat and aglDescribePixelFormat. internal enum PixelFormatAttribute { @@ -30,38 +26,38 @@ namespace OpenTK.Platform.MacOS { internal const int AGL_SWAP_INTERVAL = 222; /* 0 -> Don't sync, n -> Sync every n retrace */ internal const int AGL_BAD_PIXEL_FORMAT = 0002; /* invalid pixel format */ - [DllImport(agl)] + [DllImport(lib)] internal static extern IntPtr aglChoosePixelFormat(ref IntPtr gdevs, int ndev, int[] attribs); - [DllImport(agl)] + [DllImport(lib)] internal static extern IntPtr aglChoosePixelFormat(IntPtr gdevs, int ndev, int[] attribs); - [DllImport(agl)] + [DllImport(lib)] internal static extern void aglDestroyPixelFormat(IntPtr pix); - [DllImport(agl)] + [DllImport(lib)] internal static extern IntPtr aglCreateContext(IntPtr pix, IntPtr share); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglDestroyContext(IntPtr ctx); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglUpdateContext(IntPtr ctx); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglSetCurrentContext(IntPtr ctx); - [DllImport(agl)] + [DllImport(lib)] internal static extern IntPtr aglGetCurrentContext(); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglSetDrawable(IntPtr ctx, IntPtr draw); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglSetFullScreen(IntPtr ctx, int width, int height, int freq, int device); - [DllImport(agl)] + [DllImport(lib)] internal static extern void aglSwapBuffers(IntPtr ctx); - [DllImport(agl)] + [DllImport(lib)] internal static extern byte aglSetInteger(IntPtr ctx, int pname, ref int @params); - [DllImport(agl)] + [DllImport(lib)] internal static extern int aglGetError(); - [DllImport(agl)] + [DllImport(lib)] static extern IntPtr aglErrorString(int code); internal static void CheckReturnValue(byte code, string function) { diff --git a/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs b/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs index 02be24f4a..827ea8dac 100644 --- a/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs +++ b/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs @@ -337,38 +337,15 @@ namespace OpenTK.Platform.MacOS { StandardFloating = (CloseBox | CollapseBox) } - public enum WindowPositionMethod : uint - { + public enum WindowPositionMethod : uint { CenterOnMainScreen = 1, - CenterOnParentWindow = 2, - CenterOnParentWindowScreen = 3, - CascadeOnMainScreen = 4, - CascadeOnParentWindow = 5, - CascadeOnParentWindowScreen = 6, - CascadeStartAtParentWindowScreen = 10, - AlertPositionOnMainScreen = 7, - AlertPositionOnParentWindow = 8, - AlertPositionOnParentWindowScreen = 9 } public delegate OSStatus MacOSEventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData); - public enum WindowPartCode : short - { - inDesk = 0, - inNoWindow = 0, - inMenuBar = 1, - inSysWindow = 2, - inContent = 3, - inDrag = 4, - inGrow = 5, - inGoAway = 6, + public enum WindowPartCode : short { inZoomIn = 7, inZoomOut = 8, - inCollapseBox = 11, - inProxyIcon = 12, - inToolbarButton = 13, - inStructure = 15, } #endregion diff --git a/OpenTK/Platform/MacOS/CarbonWindow.cs b/OpenTK/Platform/MacOS/CarbonWindow.cs index eeac4ff05..a6404eed4 100644 --- a/OpenTK/Platform/MacOS/CarbonWindow.cs +++ b/OpenTK/Platform/MacOS/CarbonWindow.cs @@ -38,7 +38,8 @@ namespace OpenTK.Platform.MacOS { internal bool goFullScreenHack, goWindowedHack; string title = "OpenTK Window"; - Rectangle bounds, clientRectangle; + Rectangle bounds; + Size clientSize; Rectangle windowedBounds; bool mIsDisposed = false; bool mExists = true; @@ -141,9 +142,9 @@ namespace OpenTK.Platform.MacOS { int width, height; context.SetFullScreen(this, out width, out height); - Debug.Print("Prev Size: {0}, {1}", ClientRectangle.Width, ClientRectangle.Height); - clientRectangle.Size = new Size(width, height); - Debug.Print("New Size: {0}, {1}", ClientRectangle.Width, ClientRectangle.Height); + Debug.Print("Prev Size: {0}, {1}", clientSize.Width, clientSize.Height); + clientSize = new Size(width, height); + Debug.Print("New Size: {0}, {1}", clientSize.Width, clientSize.Height); // TODO: if we go full screen we need to make this use the device specified. bounds = Display.Bounds; @@ -230,11 +231,11 @@ namespace OpenTK.Platform.MacOS { return OSStatus.NoError; case WindowEventKind.WindowBoundsChanged: - int curWidth = ClientRectangle.Width; - int curHeight = ClientRectangle.Height; + int curWidth = clientSize.Width; + int curHeight = clientSize.Height; LoadSize(); - if (curWidth != ClientRectangle.Width || curHeight != ClientRectangle.Height) + if (curWidth != clientSize.Width || curHeight != clientSize.Height) OnResize(); return OSStatus.EventNotHandled; @@ -362,8 +363,8 @@ namespace OpenTK.Platform.MacOS { // The bounds of the window should be the size specified, but // API.SizeWindow sets the content region size. So // we reduce the size to get the correct bounds. - width -= (short)(bounds.Width - clientRectangle.Width); - height -= (short)(bounds.Height - clientRectangle.Height); + width -= (short)(bounds.Width - clientSize.Width); + height -= (short)(bounds.Height - clientSize.Height); API.SizeWindow(WinHandle, width, height, true); } @@ -385,7 +386,7 @@ namespace OpenTK.Platform.MacOS { bounds = new Rectangle(r.X, r.Y, r.Width, r.Height); r = API.GetWindowBounds(WinHandle, WindowRegionCode.GlobalPortRegion); - clientRectangle = new Rectangle(0, 0, r.Width, r.Height); + clientSize = new Size(r.Width, r.Height); } IntPtr pbStr, utf16, utf8; @@ -589,17 +590,8 @@ namespace OpenTK.Platform.MacOS { set { SetSize((short)value.Width, (short)value.Height); } } - public override Rectangle ClientRectangle { - get { return clientRectangle; } - set { - // just set the size, and ignore the location value. - // this is the behavior of the Windows WinGLNative. - ClientSize = value.Size; - } - } - public override Size ClientSize { - get { return clientRectangle.Size; } + get { return clientSize; } set { API.SizeWindow(WinHandle, (short)value.Width, (short)value.Height, true); OnResize(); diff --git a/OpenTK/Platform/Windows/WinWindow.cs b/OpenTK/Platform/Windows/WinWindow.cs index 8d6ea69cc..30a7cb616 100644 --- a/OpenTK/Platform/Windows/WinWindow.cs +++ b/OpenTK/Platform/Windows/WinWindow.cs @@ -43,8 +43,8 @@ namespace OpenTK.Platform.Windows { bool invisible_since_creation; // Set by WindowsMessage.CREATE and consumed by Visible = true (calls BringWindowToFront). int suppress_resize; // Used in WindowBorder and WindowState in order to avoid rapid, consecutive resize events. - Rectangle bounds = new Rectangle(), client_rectangle = new Rectangle(), - previous_bounds = new Rectangle(); // Used to restore previous size when leaving fullscreen mode. + Rectangle bounds, previous_bounds; // Used to restore previous size when leaving fullscreen mode. + Size clientSize; Icon icon; static readonly WinKeyMap KeyMap = new WinKeyMap(); @@ -72,6 +72,13 @@ namespace OpenTK.Platform.Windows { API.SetCurrentProcessExplicitAppUserModelID("ClassicalSharp_" + new Random().Next()); } } + + void UpdateClientSize(IntPtr handle) { + Win32Rectangle rect; + API.GetClientRect(handle, out rect); + clientSize.Width = rect.Width; + clientSize.Height = rect.Height; + } unsafe IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam) { switch (message) { @@ -111,10 +118,7 @@ namespace OpenTK.Platform.Windows { if (Size != new_size) { bounds.Width = pos->cx; bounds.Height = pos->cy; - - Win32Rectangle rect; - API.GetClientRect(handle, out rect); - client_rectangle = rect.ToRectangle(); + UpdateClientSize(handle); API.SetWindowPos(WinHandle, IntPtr.Zero, bounds.X, bounds.Y, bounds.Width, bounds.Height, @@ -288,17 +292,12 @@ namespace OpenTK.Platform.Windows { case WindowMessage.CREATE: CreateStruct cs = (CreateStruct)Marshal.PtrToStructure(lParam, typeof(CreateStruct)); - if (cs.hwndParent == IntPtr.Zero) - { + if (cs.hwndParent == IntPtr.Zero) { bounds.X = cs.x; bounds.Y = cs.y; bounds.Width = cs.cx; bounds.Height = cs.cy; - - Win32Rectangle rect; - API.GetClientRect(handle, out rect); - client_rectangle = rect.ToRectangle(); - + UpdateClientSize(handle); invisible_since_creation = true; } break; @@ -473,20 +472,12 @@ namespace OpenTK.Platform.Windows { } } - public override Rectangle ClientRectangle { - get { - if (client_rectangle.Width == 0) - client_rectangle.Width = 1; - if (client_rectangle.Height == 0) - client_rectangle.Height = 1; - return client_rectangle; - } set { - ClientSize = value.Size; - } - } - public override Size ClientSize { - get { return ClientRectangle.Size; } + get { + if (clientSize.Width == 0) clientSize.Width = 1; + if (clientSize.Height == 0) clientSize.Height = 1; + return clientSize; + } set { WindowStyle style = (WindowStyle)API.GetWindowLong(WinHandle, GetWindowLongOffsets.STYLE); Win32Rectangle rect = Win32Rectangle.From(value); diff --git a/OpenTK/Platform/X11/X11GLContext.cs b/OpenTK/Platform/X11/X11GLContext.cs index 7d3d7f42c..73a844af4 100644 --- a/OpenTK/Platform/X11/X11GLContext.cs +++ b/OpenTK/Platform/X11/X11GLContext.cs @@ -98,18 +98,7 @@ namespace OpenTK.Platform.X11 { } ContextHandle = IntPtr.Zero; } -#endif - internal static GraphicsMode SelectGraphicsMode(GraphicsMode template, out XVisualInfo info) { - int[] attribs = GetVisualAttribs(template.ColorFormat, template.Depth, template.Stencil, template.Buffers); - IntPtr visual = SelectVisual(attribs); - if (visual == IntPtr.Zero) - throw new GraphicsModeException("Requested GraphicsMode not available."); - - info = (XVisualInfo)Marshal.PtrToStructure(visual, typeof(XVisualInfo)); - API.XFree(visual); - return GetGraphicsMode(info); - } - + internal static GraphicsMode GetGraphicsMode(XVisualInfo info) { // See what we *really* got: int r, g, b, a, depth, stencil, buffers; @@ -124,6 +113,17 @@ namespace OpenTK.Platform.X11 { ++buffers; // the above lines returns 0 - false and 1 - true. return new GraphicsMode(new ColorFormat(r, g, b, a), depth, stencil, buffers); + } +#endif + internal static XVisualInfo SelectGraphicsMode(GraphicsMode template) { + int[] attribs = GetVisualAttribs(template.ColorFormat, template.Depth, template.Stencil, template.Buffers); + IntPtr visual = SelectVisual(attribs); + if (visual == IntPtr.Zero) + throw new GraphicsModeException("Requested GraphicsMode not available."); + + XVisualInfo info = (XVisualInfo)Marshal.PtrToStructure(visual, typeof(XVisualInfo)); + API.XFree(visual); + return info; } // See http://www-01.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.opengl/doc/openglrf/glXChooseFBConfig.htm%23glxchoosefbconfig diff --git a/OpenTK/Platform/X11/X11Window.cs b/OpenTK/Platform/X11/X11Window.cs index 60f5e7519..384d0a90a 100644 --- a/OpenTK/Platform/X11/X11Window.cs +++ b/OpenTK/Platform/X11/X11Window.cs @@ -58,7 +58,8 @@ namespace OpenTK.Platform.X11 { static readonly IntPtr _add = (IntPtr)1; static readonly IntPtr _toggle = (IntPtr)2; - Rectangle bounds, client_rectangle; + Rectangle bounds; + Size clientSize; int borderLeft, borderRight, borderTop, borderBottom; Icon icon; bool has_focus, visible; @@ -85,9 +86,7 @@ namespace OpenTK.Platform.X11 { API.DefaultDisplay, API.DefaultScreen, API.RootWindow); RegisterAtoms(); - XVisualInfo info = new XVisualInfo(); - mode = X11GLContext.SelectGraphicsMode(mode, out info); - VisualInfo = info; + VisualInfo = X11GLContext.SelectGraphicsMode(mode); // Create a window on this display using the visual above Debug.Print("Opening render window... "); @@ -282,7 +281,7 @@ namespace OpenTK.Platform.X11 { if (bounds.Size != newSize) { bounds.Size = newSize; - client_rectangle.Size = new Size(e.ConfigureEvent.width, e.ConfigureEvent.height); + clientSize = new Size(e.ConfigureEvent.width, e.ConfigureEvent.height); RaiseResize(); } } @@ -520,14 +519,12 @@ namespace OpenTK.Platform.X11 { ProcessEvents(); } } - - public override Rectangle ClientRectangle { + + public override Size ClientSize { get { - if (client_rectangle.Width == 0) - client_rectangle.Width = 1; - if (client_rectangle.Height == 0) - client_rectangle.Height = 1; - return client_rectangle; + if (clientSize.Width == 0) clientSize.Width = 1; + if (clientSize.Height == 0) clientSize.Height = 1; + return clientSize; } set { API.XResizeWindow(API.DefaultDisplay, WinHandle, value.Width, value.Height); @@ -535,16 +532,10 @@ namespace OpenTK.Platform.X11 { } } - public override Size ClientSize { - get { return ClientRectangle.Size; } - set { ClientRectangle = new Rectangle(Point.Empty, value); } - } - public override Icon Icon { get { return icon; } set { - if (value == icon) - return; + if (value == icon) return; // Note: it seems that Gnome/Metacity does not respect the _NET_WM_ICON hint. // For this reason, we'll also set the icon using XSetWMHints. diff --git a/src/Client/WinWindow.c b/src/Client/WinWindow.c index d8fdd34c8..46fb7a294 100644 --- a/src/Client/WinWindow.c +++ b/src/Client/WinWindow.c @@ -24,7 +24,7 @@ bool invisible_since_creation; // Set by WindowsMessage.CREATE and consumed by V Int32 suppress_resize; // Used in WindowBorder and WindowState in order to avoid rapid, consecutive resize events. Rectangle2D win_Bounds; -Rectangle2D win_ClientRect; +Size2D win_ClientSize; Rectangle2D previous_bounds; // Used to restore previous size when leaving fullscreen mode. static Rectangle2D Window_FromRect(RECT rect) { @@ -165,6 +165,13 @@ static Key Window_MapKey(WPARAM key) { return Key_None; } +static void Window_UpdateClientSize(HWND handle) { + RECT rect; + GetClientRect(handle, &rect); + win_ClientSize.Width = RECT_WIDTH(rect); + win_ClientSize.Height = RECT_HEIGHT(rect); +} + static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wParam, LPARAM lParam) { bool new_focused_state; Real32 wheel_delta; @@ -207,10 +214,7 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara Size2D size = Window_GetSize(); if (size.Width != pos->cx || size.Height != pos->cy) { win_Bounds.Width = pos->cx; win_Bounds.Height = pos->cy; - - RECT rect; - GetClientRect(handle, &rect); - win_ClientRect = Window_FromRect(rect); + Window_UpdateClientSize(handle); SetWindowPos(win_Handle, NULL, win_Bounds.X, win_Bounds.Y, win_Bounds.Width, win_Bounds.Height, @@ -370,12 +374,9 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara case WM_CREATE: cs = (CREATESTRUCT*)lParam; if (cs->hwndParent == NULL) { - win_Bounds.X = cs->x; win_Bounds.Y = cs->y; + win_Bounds.X = cs->x; win_Bounds.Y = cs->y; win_Bounds.Width = cs->cx; win_Bounds.Height = cs->cy; - - RECT rect; - GetClientRect(handle, &rect); - win_ClientRect = Window_FromRect(rect); + Window_UpdateClientSize(handle); invisible_since_creation = true; } break; @@ -401,7 +402,8 @@ void Window_Create(Int32 x, Int32 y, Int32 width, Int32 height, STRING_REF Strin /* TODO: UngroupFromTaskbar(); */ /* Find out the final window rectangle, after the WM has added its chrome (titlebar, sidebars etc). */ - RECT rect; rect.left = x; rect.top = y; rect.right = x + width; rect.bottom = y + height; + RECT rect; rect.left = x; rect.top = y; + rect.right = x + width; rect.bottom = y + height; AdjustWindowRect(&rect, win_Style, false); WNDCLASSEXA wc = { 0 }; @@ -518,19 +520,10 @@ void Window_SetSize(Size2D size) { SetWindowPos(win_Handle, NULL, 0, 0, size.Width, size.Height, SWP_NOMOVE); } -Rectangle2D Window_GetClientRectangle(void) { return win_ClientRect; } -void Window_SetClientRectangle(Rectangle2D rect) { - Size2D size = { rect.Width, rect.Height }; - Window_SetClientSize(size); -} - -Size2D Window_GetClientSize(void) { - Size2D size = { win_ClientRect.Width, win_ClientRect.Height }; return size; -} +Size2D Window_GetClientSize(void) { return win_ClientSize; } void Window_SetClientSize(Size2D size) { DWORD style = GetWindowLongA(win_Handle, GWL_STYLE); - RECT rect; - rect.left = 0; rect.top = 0; + RECT rect; rect.left = 0; rect.top = 0; rect.right = size.Width; rect.bottom = size.Height; AdjustWindowRect(&rect, style, false); diff --git a/src/Client/Window.h b/src/Client/Window.h index 37f5381ad..d93f7d2c6 100644 --- a/src/Client/Window.h +++ b/src/Client/Window.h @@ -57,8 +57,6 @@ Point2D Window_GetLocation(void); void Window_SetLocation(Point2D point); Size2D Window_GetSize(void); void Window_SetSize(Size2D size); -Rectangle2D Window_GetClientRectangle(void); -void Window_SetClientRectangle(Rectangle2D rect); Size2D Window_GetClientSize(void); void Window_SetClientSize(Size2D size);