From e13a8e25aefe75784ace2361ae9d2ed902a4d1b6 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Thu, 28 Oct 2010 09:31:00 +0000 Subject: [PATCH] Added code to raise all available events. Removed unused events from old OpenTK versions. Fixed potential race condition when raising events (an event might become null between the null check and the actual raising). --- .../OpenTK/Platform/MacOS/CarbonGLNative.cs | 123 +++++++++--------- Source/OpenTK/Platform/Windows/WinGLNative.cs | 113 ++++++++-------- Source/OpenTK/Platform/X11/X11GLNative.cs | 77 +++++------ 3 files changed, 148 insertions(+), 165 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs index ea517095..fcf94717 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -144,6 +144,7 @@ namespace OpenTK.Platform.MacOS DisposeUPP(); + Disposed(this, EventArgs.Empty); } ~CarbonGLNative() @@ -419,11 +420,16 @@ namespace OpenTK.Platform.MacOS case WindowEventKind.WindowBoundsChanged: int thisWidth = Width; int thisHeight = Height; + int thisX = X; + int thisY = Y; LoadSize(); + if (thisX != X || thisY != Y) + Move(this, EventArgs.Empty); + if (thisWidth != Width || thisHeight != Height) - OnResize(); + Resize(this, EventArgs.Empty); return OSStatus.EventNotHandled; @@ -661,15 +667,6 @@ namespace OpenTK.Platform.MacOS API.SizeWindow(window.WindowRef, width, height, true); } - protected void OnResize() - { - LoadSize(); - - if (Resize != null) - { - Resize(this, EventArgs.Empty); - } - } private void LoadSize() { @@ -733,10 +730,16 @@ namespace OpenTK.Platform.MacOS public Icon Icon { - get { return mIcon; } - set { - SetIcon(value); - } + get { return mIcon; } + set + { + if (value != Icon) + { + SetIcon(value); + mIcon = value; + IconChanged(this, EventArgs.Empty); + } + } } private void SetIcon(Icon icon) @@ -798,8 +801,12 @@ namespace OpenTK.Platform.MacOS } set { - API.SetWindowTitle(window.WindowRef, value); - title = value; + if (value != Title) + { + API.SetWindowTitle(window.WindowRef, value); + title = value; + TitleChanged(this, EventArgs.Empty); + } } } @@ -808,10 +815,15 @@ namespace OpenTK.Platform.MacOS get { return API.IsWindowVisible(window.WindowRef); } set { - if (value && Visible == false) - Show(); - else - Hide(); + if (value != Visible) + { + if (value) + Show(); + else + Hide(); + + VisibleChanged(this, EventArgs.Empty); + } } } @@ -917,7 +929,8 @@ namespace OpenTK.Platform.MacOS set { API.SizeWindow(window.WindowRef, (short)value.Width, (short)value.Height, true); - OnResize(); + LoadSize(); + Resize(this, EventArgs.Empty); } } @@ -1019,9 +1032,9 @@ namespace OpenTK.Platform.MacOS } - OnWindowStateChanged(); - - OnResize(); + WindowStateChanged(this, EventArgs.Empty); + LoadSize(); + Resize(this, EventArgs.Empty); } public WindowBorder WindowBorder @@ -1048,8 +1061,7 @@ namespace OpenTK.Platform.MacOS WindowAttributes.Resizable | WindowAttributes.FullZoom); } - if (WindowBorderChanged != null) - WindowBorderChanged(this, EventArgs.Empty); + WindowBorderChanged(this, EventArgs.Empty); } } @@ -1057,76 +1069,65 @@ namespace OpenTK.Platform.MacOS private void OnKeyPress(KeyPressEventArgs keyPressArgs) { - if (KeyPress != null) - KeyPress(this, keyPressArgs); + KeyPress(this, keyPressArgs); } private void OnWindowStateChanged() { - if (WindowStateChanged != null) - WindowStateChanged(this, EventArgs.Empty); + WindowStateChanged(this, EventArgs.Empty); } protected virtual void OnClosing(CancelEventArgs e) { - if (Closing != null) - Closing(this, e); + Closing(this, e); } protected virtual void OnClosed() { - if (Closed != null) - Closed(this, EventArgs.Empty); + Closed(this, EventArgs.Empty); } private void OnMouseLeave() { - if (MouseLeave != null) - MouseLeave(this, EventArgs.Empty); + MouseLeave(this, EventArgs.Empty); } private void OnMouseEnter() { - if (MouseEnter != null) - MouseEnter(this, EventArgs.Empty); + MouseEnter(this, EventArgs.Empty); } private void OnActivate() { mIsActive = true; - if (FocusedChanged != null) - FocusedChanged(this, EventArgs.Empty); + FocusedChanged(this, EventArgs.Empty); } private void OnDeactivate() { mIsActive = false; - if (FocusedChanged != null) - FocusedChanged(this, EventArgs.Empty); + FocusedChanged(this, EventArgs.Empty); } #endregion - public event EventHandler Idle; - public event EventHandler Load; - public event EventHandler Unload; - public event EventHandler Move; - public event EventHandler Resize; - public event EventHandler Closing; - public event EventHandler Closed; - public event EventHandler Disposed; - public event EventHandler IconChanged; - public event EventHandler TitleChanged; - public event EventHandler ClientSizeChanged; - public event EventHandler VisibleChanged; - public event EventHandler WindowInfoChanged; - public event EventHandler FocusedChanged; - public event EventHandler WindowBorderChanged; - public event EventHandler WindowStateChanged; - public event EventHandler KeyPress; - public event EventHandler MouseEnter; - public event EventHandler MouseLeave; + public event EventHandler Load = delegate { }; + public event EventHandler Unload = delegate { }; + public event EventHandler Move = delegate { }; + public event EventHandler Resize = delegate { }; + public event EventHandler Closing = delegate { }; + public event EventHandler Closed = delegate { }; + public event EventHandler Disposed = delegate { }; + public event EventHandler IconChanged = delegate { }; + public event EventHandler TitleChanged = delegate { }; + public event EventHandler VisibleChanged = delegate { }; + public event EventHandler FocusedChanged = delegate { }; + public event EventHandler WindowBorderChanged = delegate { }; + public event EventHandler WindowStateChanged = delegate { }; + public event EventHandler KeyPress = delegate { }; + public event EventHandler MouseEnter = delegate { }; + public event EventHandler MouseLeave = delegate { }; #endregion } diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index f0bf5633..0650a819 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -156,7 +156,7 @@ namespace OpenTK.Platform.Windows else focused = (wParam.ToInt64() & 0xFFFF) != 0; - if (new_focused_state != Focused && FocusedChanged != null) + if (new_focused_state != Focused) FocusedChanged(this, EventArgs.Empty); break; @@ -188,8 +188,7 @@ namespace OpenTK.Platform.Windows if (Location != new_location) { bounds.Location = new_location; - if (Move != null) - Move(this, EventArgs.Empty); + Move(this, EventArgs.Empty); } Size new_size = new Size(pos->cx, pos->cy); @@ -206,7 +205,7 @@ namespace OpenTK.Platform.Windows SetWindowPosFlags.NOZORDER | SetWindowPosFlags.NOOWNERZORDER | SetWindowPosFlags.NOACTIVATE | SetWindowPosFlags.NOSENDCHANGING); - if (suppress_resize <= 0 && Resize != null) + if (suppress_resize <= 0) Resize(this, EventArgs.Empty); } @@ -254,8 +253,7 @@ namespace OpenTK.Platform.Windows if (new_state != windowState) { windowState = new_state; - if (WindowStateChanged != null) - WindowStateChanged(this, EventArgs.Empty); + WindowStateChanged(this, EventArgs.Empty); } // Ensure cursor remains grabbed @@ -274,8 +272,7 @@ namespace OpenTK.Platform.Windows else key_press.KeyChar = (char)wParam.ToInt64(); - if (KeyPress != null) - KeyPress(this, key_press); + KeyPress(this, key_press); break; case WindowMessage.MOUSEMOVE: @@ -291,8 +288,7 @@ namespace OpenTK.Platform.Windows mouse_outside_window = false; EnableMouseTracking(); - if (MouseEnter != null) - MouseEnter(this, EventArgs.Empty); + MouseEnter(this, EventArgs.Empty); } break; @@ -300,8 +296,7 @@ namespace OpenTK.Platform.Windows mouse_outside_window = true; // Mouse tracking is disabled automatically by the OS - if (MouseLeave != null) - MouseLeave(this, EventArgs.Empty); + MouseLeave(this, EventArgs.Empty); break; case WindowMessage.MOUSEWHEEL: @@ -453,13 +448,11 @@ namespace OpenTK.Platform.Windows case WindowMessage.CLOSE: System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs(); - if (Closing != null) - Closing(this, e); + Closing(this, e); if (!e.Cancel) { - if (Unload != null) - Unload(this, EventArgs.Empty); + Unload(this, EventArgs.Empty); DestroyWindow(); break; @@ -474,8 +467,7 @@ namespace OpenTK.Platform.Windows window.Dispose(); child_window.Dispose(); - if (Closed != null) - Closed(this, EventArgs.Empty); + Closed(this, EventArgs.Empty); break; @@ -795,11 +787,15 @@ namespace OpenTK.Platform.Windows } set { - icon = value; - if (window.WindowHandle != IntPtr.Zero) + if (value != icon) { - Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)0, icon == null ? IntPtr.Zero : value.Handle); - Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle); + icon = value; + if (window.WindowHandle != IntPtr.Zero) + { + Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)0, icon == null ? IntPtr.Zero : value.Handle); + Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle); + } + IconChanged(this, EventArgs.Empty); } } } @@ -829,8 +825,12 @@ namespace OpenTK.Platform.Windows } set { - if (!Functions.SetWindowText(window.WindowHandle, value)) - Debug.Print("Failed to change window title (window:{0}, new title:{1}, reason:{2}).", window.WindowHandle, value, Marshal.GetLastWin32Error()); + if (Title != value) + { + if (!Functions.SetWindowText(window.WindowHandle, value)) + Debug.Print("Failed to change window title (window:{0}, new title:{1}, reason:{2}).", window.WindowHandle, value, Marshal.GetLastWin32Error()); + TitleChanged(this, EventArgs.Empty); + } } } @@ -846,18 +846,23 @@ namespace OpenTK.Platform.Windows } set { - if (value) + if (value != Visible) { - Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.SHOW); - if (invisible_since_creation) + if (value) { - Functions.BringWindowToTop(window.WindowHandle); - Functions.SetForegroundWindow(window.WindowHandle); + Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.SHOW); + if (invisible_since_creation) + { + Functions.BringWindowToTop(window.WindowHandle); + Functions.SetForegroundWindow(window.WindowHandle); + } } - } - else if (!value) - { - Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.HIDE); + else if (!value) + { + Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.HIDE); + } + + VisibleChanged(this, EventArgs.Empty); } } } @@ -1076,8 +1081,7 @@ namespace OpenTK.Platform.Windows WindowState = state; - if (WindowBorderChanged != null) - WindowBorderChanged(this, EventArgs.Empty); + WindowBorderChanged(this, EventArgs.Empty); } } @@ -1113,43 +1117,37 @@ namespace OpenTK.Platform.Windows #region Events - public event EventHandler Idle; + public event EventHandler Load =delegate { }; - public event EventHandler Load; + public event EventHandler Unload = delegate { }; - public event EventHandler Unload; + public event EventHandler Move = delegate { }; - public event EventHandler Move; + public event EventHandler Resize = delegate { }; - public event EventHandler Resize; + public event EventHandler Closing = delegate { }; - public event EventHandler Closing; + public event EventHandler Closed = delegate { }; - public event EventHandler Closed; + public event EventHandler Disposed = delegate { }; - public event EventHandler Disposed; + public event EventHandler IconChanged = delegate { }; - public event EventHandler IconChanged; + public event EventHandler TitleChanged = delegate { }; - public event EventHandler TitleChanged; + public event EventHandler VisibleChanged = delegate { }; - public event EventHandler ClientSizeChanged; + public event EventHandler FocusedChanged = delegate { }; - public event EventHandler VisibleChanged; + public event EventHandler WindowBorderChanged = delegate { }; - public event EventHandler WindowInfoChanged; + public event EventHandler WindowStateChanged = delegate { }; - public event EventHandler FocusedChanged; + public event EventHandler KeyPress = delegate { }; - public event EventHandler WindowBorderChanged; + public event EventHandler MouseEnter = delegate { }; - public event EventHandler WindowStateChanged; - - public event EventHandler KeyPress; - - public event EventHandler MouseEnter; - - public event EventHandler MouseLeave; + public event EventHandler MouseLeave = delegate { }; #endregion @@ -1280,6 +1278,7 @@ namespace OpenTK.Platform.Windows Debug.Print("[Warning] INativeWindow leaked ({0}). Did you forget to call INativeWindow.Dispose()?", this); } + Disposed(this, EventArgs.Empty); disposed = true; } } diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 9ef2de18..92e928bd 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -659,8 +659,7 @@ namespace OpenTK.Platform.X11 if (Location != new_location) { bounds.Location = new_location; - if (Move != null) - Move(this, EventArgs.Empty); + Move(this, EventArgs.Empty); } // Note: width and height denote the internal (client) size. @@ -673,11 +672,7 @@ namespace OpenTK.Platform.X11 bounds.Size = new_size; client_rectangle.Size = new Size(e.ConfigureEvent.width, e.ConfigureEvent.height); - if (this.Resize != null) - { - //Debug.WriteLine(new System.Diagnostics.StackTrace()); - Resize(this, EventArgs.Empty); - } + Resize(this, EventArgs.Empty); } } @@ -723,8 +718,7 @@ namespace OpenTK.Platform.X11 bool previous_visible = visible; visible = true; if (visible != previous_visible) - if (VisibleChanged != null) - VisibleChanged(this, EventArgs.Empty); + VisibleChanged(this, EventArgs.Empty); } return; @@ -733,8 +727,7 @@ namespace OpenTK.Platform.X11 bool previous_visible = visible; visible = false; if (visible != previous_visible) - if (VisibleChanged != null) - VisibleChanged(this, EventArgs.Empty); + VisibleChanged(this, EventArgs.Empty); } break; @@ -747,8 +740,7 @@ namespace OpenTK.Platform.X11 { Debug.WriteLine("Exit message received."); CancelEventArgs ce = new CancelEventArgs(); - if (Closing != null) - Closing(this, ce); + Closing(this, ce); if (!ce.Cancel) { @@ -769,8 +761,7 @@ namespace OpenTK.Platform.X11 Debug.WriteLine("Window destroyed"); exists = false; - if (Closed != null) - Closed(this, EventArgs.Empty); + Closed(this, EventArgs.Empty); return; @@ -813,8 +804,7 @@ namespace OpenTK.Platform.X11 bool previous_focus = has_focus; has_focus = true; if (has_focus != previous_focus) - if (FocusedChanged != null) - FocusedChanged(this, EventArgs.Empty); + FocusedChanged(this, EventArgs.Empty); } break; @@ -823,19 +813,16 @@ namespace OpenTK.Platform.X11 bool previous_focus = has_focus; has_focus = false; if (has_focus != previous_focus) - if (FocusedChanged != null) - FocusedChanged(this, EventArgs.Empty); + FocusedChanged(this, EventArgs.Empty); } break; case XEventName.LeaveNotify: - if (MouseLeave != null) - MouseLeave(this, EventArgs.Empty); + MouseLeave(this, EventArgs.Empty); break; case XEventName.EnterNotify: - if (MouseEnter != null) - MouseEnter(this, EventArgs.Empty); + MouseEnter(this, EventArgs.Empty); break; case XEventName.MappingNotify: @@ -850,8 +837,7 @@ namespace OpenTK.Platform.X11 case XEventName.PropertyNotify: if (e.PropertyEvent.atom == _atom_net_wm_state) { - if (WindowStateChanged != null) - WindowStateChanged(this, EventArgs.Empty); + WindowStateChanged(this, EventArgs.Empty); } //if (e.PropertyEvent.atom == _atom_net_frame_extents) @@ -1078,8 +1064,7 @@ namespace OpenTK.Platform.X11 } icon = value; - if (IconChanged != null) - IconChanged(this, EventArgs.Empty); + IconChanged(this, EventArgs.Empty); } } @@ -1277,8 +1262,7 @@ namespace OpenTK.Platform.X11 break; } - if (WindowBorderChanged != null) - WindowBorderChanged(this, EventArgs.Empty); + WindowBorderChanged(this, EventArgs.Empty); } } @@ -1286,37 +1270,37 @@ namespace OpenTK.Platform.X11 #region Events - public event EventHandler Load; + public event EventHandler Load = delegate { }; - public event EventHandler Unload; + public event EventHandler Unload = delegate { }; - public event EventHandler Move; + public event EventHandler Move = delegate { }; - public event EventHandler Resize; + public event EventHandler Resize = delegate { }; - public event EventHandler Closing; + public event EventHandler Closing = delegate { }; - public event EventHandler Closed; + public event EventHandler Closed = delegate { }; - public event EventHandler Disposed; + public event EventHandler Disposed = delegate { }; - public event EventHandler IconChanged; + public event EventHandler IconChanged = delegate { }; - public event EventHandler TitleChanged; + public event EventHandler TitleChanged = delegate { }; - public event EventHandler VisibleChanged; + public event EventHandler VisibleChanged = delegate { }; - public event EventHandler FocusedChanged; + public event EventHandler FocusedChanged = delegate { }; - public event EventHandler WindowBorderChanged; + public event EventHandler WindowBorderChanged = delegate { }; - public event EventHandler WindowStateChanged; + public event EventHandler WindowStateChanged = delegate { }; - public event EventHandler KeyPress; + public event EventHandler KeyPress = delegate { }; - public event EventHandler MouseEnter; + public event EventHandler MouseEnter = delegate { }; - public event EventHandler MouseLeave; + public event EventHandler MouseLeave = delegate { }; #endregion @@ -1427,8 +1411,7 @@ namespace OpenTK.Platform.X11 } } - if (TitleChanged != null) - TitleChanged(this, EventArgs.Empty); + TitleChanged(this, EventArgs.Empty); } }