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).

This commit is contained in:
the_fiddler 2010-10-28 09:31:00 +00:00
parent b7a0a7c800
commit e13a8e25ae
3 changed files with 148 additions and 165 deletions

View file

@ -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<EventArgs> Idle;
public event EventHandler<EventArgs> Load;
public event EventHandler<EventArgs> Unload;
public event EventHandler<EventArgs> Move;
public event EventHandler<EventArgs> Resize;
public event EventHandler<CancelEventArgs> Closing;
public event EventHandler<EventArgs> Closed;
public event EventHandler<EventArgs> Disposed;
public event EventHandler<EventArgs> IconChanged;
public event EventHandler<EventArgs> TitleChanged;
public event EventHandler<EventArgs> ClientSizeChanged;
public event EventHandler<EventArgs> VisibleChanged;
public event EventHandler<EventArgs> WindowInfoChanged;
public event EventHandler<EventArgs> FocusedChanged;
public event EventHandler<EventArgs> WindowBorderChanged;
public event EventHandler<EventArgs> WindowStateChanged;
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseLeave;
public event EventHandler<EventArgs> Load = delegate { };
public event EventHandler<EventArgs> Unload = delegate { };
public event EventHandler<EventArgs> Move = delegate { };
public event EventHandler<EventArgs> Resize = delegate { };
public event EventHandler<CancelEventArgs> Closing = delegate { };
public event EventHandler<EventArgs> Closed = delegate { };
public event EventHandler<EventArgs> Disposed = delegate { };
public event EventHandler<EventArgs> IconChanged = delegate { };
public event EventHandler<EventArgs> TitleChanged = delegate { };
public event EventHandler<EventArgs> VisibleChanged = delegate { };
public event EventHandler<EventArgs> FocusedChanged = delegate { };
public event EventHandler<EventArgs> WindowBorderChanged = delegate { };
public event EventHandler<EventArgs> WindowStateChanged = delegate { };
public event EventHandler<KeyPressEventArgs> KeyPress = delegate { };
public event EventHandler<EventArgs> MouseEnter = delegate { };
public event EventHandler<EventArgs> MouseLeave = delegate { };
#endregion
}

View file

@ -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<EventArgs> Idle;
public event EventHandler<EventArgs> Load =delegate { };
public event EventHandler<EventArgs> Load;
public event EventHandler<EventArgs> Unload = delegate { };
public event EventHandler<EventArgs> Unload;
public event EventHandler<EventArgs> Move = delegate { };
public event EventHandler<EventArgs> Move;
public event EventHandler<EventArgs> Resize = delegate { };
public event EventHandler<EventArgs> Resize;
public event EventHandler<System.ComponentModel.CancelEventArgs> Closing = delegate { };
public event EventHandler<System.ComponentModel.CancelEventArgs> Closing;
public event EventHandler<EventArgs> Closed = delegate { };
public event EventHandler<EventArgs> Closed;
public event EventHandler<EventArgs> Disposed = delegate { };
public event EventHandler<EventArgs> Disposed;
public event EventHandler<EventArgs> IconChanged = delegate { };
public event EventHandler<EventArgs> IconChanged;
public event EventHandler<EventArgs> TitleChanged = delegate { };
public event EventHandler<EventArgs> TitleChanged;
public event EventHandler<EventArgs> VisibleChanged = delegate { };
public event EventHandler<EventArgs> ClientSizeChanged;
public event EventHandler<EventArgs> FocusedChanged = delegate { };
public event EventHandler<EventArgs> VisibleChanged;
public event EventHandler<EventArgs> WindowBorderChanged = delegate { };
public event EventHandler<EventArgs> WindowInfoChanged;
public event EventHandler<EventArgs> WindowStateChanged = delegate { };
public event EventHandler<EventArgs> FocusedChanged;
public event EventHandler<KeyPressEventArgs> KeyPress = delegate { };
public event EventHandler<EventArgs> WindowBorderChanged;
public event EventHandler<EventArgs> MouseEnter = delegate { };
public event EventHandler<EventArgs> WindowStateChanged;
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseLeave;
public event EventHandler<EventArgs> 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;
}
}

View file

@ -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<EventArgs> Load;
public event EventHandler<EventArgs> Load = delegate { };
public event EventHandler<EventArgs> Unload;
public event EventHandler<EventArgs> Unload = delegate { };
public event EventHandler<EventArgs> Move;
public event EventHandler<EventArgs> Move = delegate { };
public event EventHandler<EventArgs> Resize;
public event EventHandler<EventArgs> Resize = delegate { };
public event EventHandler<System.ComponentModel.CancelEventArgs> Closing;
public event EventHandler<System.ComponentModel.CancelEventArgs> Closing = delegate { };
public event EventHandler<EventArgs> Closed;
public event EventHandler<EventArgs> Closed = delegate { };
public event EventHandler<EventArgs> Disposed;
public event EventHandler<EventArgs> Disposed = delegate { };
public event EventHandler<EventArgs> IconChanged;
public event EventHandler<EventArgs> IconChanged = delegate { };
public event EventHandler<EventArgs> TitleChanged;
public event EventHandler<EventArgs> TitleChanged = delegate { };
public event EventHandler<EventArgs> VisibleChanged;
public event EventHandler<EventArgs> VisibleChanged = delegate { };
public event EventHandler<EventArgs> FocusedChanged;
public event EventHandler<EventArgs> FocusedChanged = delegate { };
public event EventHandler<EventArgs> WindowBorderChanged;
public event EventHandler<EventArgs> WindowBorderChanged = delegate { };
public event EventHandler<EventArgs> WindowStateChanged;
public event EventHandler<EventArgs> WindowStateChanged = delegate { };
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<KeyPressEventArgs> KeyPress = delegate { };
public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseEnter = delegate { };
public event EventHandler<EventArgs> MouseLeave;
public event EventHandler<EventArgs> MouseLeave = delegate { };
#endregion
@ -1427,8 +1411,7 @@ namespace OpenTK.Platform.X11
}
}
if (TitleChanged != null)
TitleChanged(this, EventArgs.Empty);
TitleChanged(this, EventArgs.Empty);
}
}