From a24ce9ba118c8b7a7fb38b80c8dc9e1a47493659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olle=20H=C3=A5kansson?= Date: Sun, 27 Apr 2014 09:58:05 +0200 Subject: [PATCH] Fixed creating contexts without native windows. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To create a context for the GTK GLWidget, you need to be able to specify the exact NSView you want the OpenGL context to apply to. Also, you don’t want to initialise the NSApplication in this situation, which the CocoaContext did before (unintentionally). --- Source/OpenTK/Platform/MacOS/Cocoa/Cocoa.cs | 2 -- .../Platform/MacOS/CocoaNativeWindow.cs | 7 +---- .../OpenTK/Platform/MacOS/CocoaWindowInfo.cs | 29 ++++++++++++------- Source/OpenTK/Platform/Utilities.cs | 12 ++++++++ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/Cocoa/Cocoa.cs b/Source/OpenTK/Platform/MacOS/Cocoa/Cocoa.cs index 9d84f690..a294e51f 100644 --- a/Source/OpenTK/Platform/MacOS/Cocoa/Cocoa.cs +++ b/Source/OpenTK/Platform/MacOS/Cocoa/Cocoa.cs @@ -223,8 +223,6 @@ namespace OpenTK.Platform.MacOS AppKitLibrary = NS.LoadLibrary("/System/Library/Frameworks/AppKit.framework/AppKit"); FoundationLibrary = NS.LoadLibrary("/System/Library/Frameworks/Foundation.framework/Foundation"); - - NSApplication.Initialize(); } } } diff --git a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs index 71f0ded6..8752a8f0 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs @@ -61,7 +61,6 @@ namespace OpenTK.Platform.MacOS static readonly IntPtr selNextEventMatchingMask = Selector.Get("nextEventMatchingMask:untilDate:inMode:dequeue:"); static readonly IntPtr selSendEvent = Selector.Get("sendEvent:"); //static readonly IntPtr selUpdateWindows = Selector.Get("updateWindows"); - static readonly IntPtr selContentView = Selector.Get("contentView"); static readonly IntPtr selConvertRectFromScreen = Selector.Get("convertRectFromScreen:"); static readonly IntPtr selConvertRectToScreen = Selector.Get("convertRectToScreen:"); static readonly IntPtr selPerformClose = Selector.Get("performClose:"); @@ -119,6 +118,7 @@ namespace OpenTK.Platform.MacOS static CocoaNativeWindow() { Cocoa.Initialize(); + NSApplication.Initialize(); // Problem: This does not allow creating a separate app and using CocoaNativeWindow. NSDefaultRunLoopMode = Cocoa.GetStringConstant(Cocoa.FoundationLibrary, "NSDefaultRunLoopMode"); NSCursor = Class.Get("NSCursor"); } @@ -939,11 +939,6 @@ namespace OpenTK.Platform.MacOS Dispose(false); } - public static IntPtr GetView(IntPtr windowHandle) - { - return Cocoa.SendIntPtr(windowHandle, selContentView); - } - private RectangleF GetContentViewFrame() { return Cocoa.SendRect(windowInfo.ViewHandle, selFrame); diff --git a/Source/OpenTK/Platform/MacOS/CocoaWindowInfo.cs b/Source/OpenTK/Platform/MacOS/CocoaWindowInfo.cs index 80e6fdf5..5858f804 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaWindowInfo.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaWindowInfo.cs @@ -40,7 +40,10 @@ namespace OpenTK.Platform.MacOS /// sealed class CocoaWindowInfo : IWindowInfo { + static readonly IntPtr selContentView = Selector.Get("contentView"); + IntPtr nsWindowRef; + IntPtr nsViewRef; bool disposed = false; @@ -49,10 +52,22 @@ namespace OpenTK.Platform.MacOS /// /// Constructs a new instance with the specified parameters. /// - /// A valid NSView reference. - public CocoaWindowInfo(IntPtr nsWindowRef) + /// This constructor assumes that the NSWindow's contentView is the NSView we want to attach to our context. + /// A valid NSWindow reference. + public CocoaWindowInfo(IntPtr nsWindowRef) : this(nsWindowRef, Cocoa.SendIntPtr(nsWindowRef, selContentView)) + { + + } + + /// + /// Constructs a new instance with the specified parameters. + /// + /// A valid NSWindow reference. + /// A valid NSView reference. + public CocoaWindowInfo(IntPtr nsWindowRef, IntPtr nsViewRef) { this.nsWindowRef = nsWindowRef; + this.nsViewRef = nsViewRef; } #endregion @@ -67,19 +82,13 @@ namespace OpenTK.Platform.MacOS /// /// Gets the view reference for this instance. /// - public IntPtr ViewHandle - { - get - { - return CocoaNativeWindow.GetView(nsWindowRef); - } - } + public IntPtr ViewHandle { get { return nsViewRef; } } /// Returns a System.String that represents the current window. /// A System.String that represents the current window. public override string ToString() { - return String.Format("MacOS.CocoaWindowInfo: NSWindow {0}", nsWindowRef); + return String.Format("MacOS.CocoaWindowInfo: NSWindow {0}, NSView {1}", nsWindowRef, nsViewRef); } #endregion diff --git a/Source/OpenTK/Platform/Utilities.cs b/Source/OpenTK/Platform/Utilities.cs index 8e4c78e4..a7b801eb 100644 --- a/Source/OpenTK/Platform/Utilities.cs +++ b/Source/OpenTK/Platform/Utilities.cs @@ -309,12 +309,24 @@ namespace OpenTK.Platform /// Creates an IWindowInfo instance for the Mac OS X platform. /// /// The handle of the NSWindow. + /// Assumes that the NSWindow's contentView is the NSView we want to attach to our context. /// A new IWindowInfo instance. public static IWindowInfo CreateMacOSWindowInfo(IntPtr windowHandle) { return new OpenTK.Platform.MacOS.CocoaWindowInfo(windowHandle); } + /// + /// Creates an IWindowInfo instance for the Mac OS X platform. + /// + /// The handle of the NSWindow. + /// The handle of the NSView. + /// A new IWindowInfo instance. + public static IWindowInfo CreateMacOSWindowInfo(IntPtr windowHandle, IntPtr viewHandle) + { + return new OpenTK.Platform.MacOS.CocoaWindowInfo(windowHandle, viewHandle); + } + #endregion #region CreateDummyWindowInfo