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(); DisposeUPP();
Disposed(this, EventArgs.Empty);
} }
~CarbonGLNative() ~CarbonGLNative()
@ -419,11 +420,16 @@ namespace OpenTK.Platform.MacOS
case WindowEventKind.WindowBoundsChanged: case WindowEventKind.WindowBoundsChanged:
int thisWidth = Width; int thisWidth = Width;
int thisHeight = Height; int thisHeight = Height;
int thisX = X;
int thisY = Y;
LoadSize(); LoadSize();
if (thisX != X || thisY != Y)
Move(this, EventArgs.Empty);
if (thisWidth != Width || thisHeight != Height) if (thisWidth != Width || thisHeight != Height)
OnResize(); Resize(this, EventArgs.Empty);
return OSStatus.EventNotHandled; return OSStatus.EventNotHandled;
@ -661,15 +667,6 @@ namespace OpenTK.Platform.MacOS
API.SizeWindow(window.WindowRef, width, height, true); API.SizeWindow(window.WindowRef, width, height, true);
} }
protected void OnResize()
{
LoadSize();
if (Resize != null)
{
Resize(this, EventArgs.Empty);
}
}
private void LoadSize() private void LoadSize()
{ {
@ -734,8 +731,14 @@ namespace OpenTK.Platform.MacOS
public Icon Icon public Icon Icon
{ {
get { return mIcon; } get { return mIcon; }
set { set
{
if (value != Icon)
{
SetIcon(value); SetIcon(value);
mIcon = value;
IconChanged(this, EventArgs.Empty);
}
} }
} }
@ -797,9 +800,13 @@ namespace OpenTK.Platform.MacOS
return title; return title;
} }
set set
{
if (value != Title)
{ {
API.SetWindowTitle(window.WindowRef, value); API.SetWindowTitle(window.WindowRef, value);
title = value; title = value;
TitleChanged(this, EventArgs.Empty);
}
} }
} }
@ -808,10 +815,15 @@ namespace OpenTK.Platform.MacOS
get { return API.IsWindowVisible(window.WindowRef); } get { return API.IsWindowVisible(window.WindowRef); }
set set
{ {
if (value && Visible == false) if (value != Visible)
{
if (value)
Show(); Show();
else else
Hide(); Hide();
VisibleChanged(this, EventArgs.Empty);
}
} }
} }
@ -917,7 +929,8 @@ namespace OpenTK.Platform.MacOS
set set
{ {
API.SizeWindow(window.WindowRef, (short)value.Width, (short)value.Height, true); 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(); WindowStateChanged(this, EventArgs.Empty);
LoadSize();
OnResize(); Resize(this, EventArgs.Empty);
} }
public WindowBorder WindowBorder public WindowBorder WindowBorder
@ -1048,7 +1061,6 @@ namespace OpenTK.Platform.MacOS
WindowAttributes.Resizable | WindowAttributes.FullZoom); 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) private void OnKeyPress(KeyPressEventArgs keyPressArgs)
{ {
if (KeyPress != null)
KeyPress(this, keyPressArgs); KeyPress(this, keyPressArgs);
} }
private void OnWindowStateChanged() private void OnWindowStateChanged()
{ {
if (WindowStateChanged != null)
WindowStateChanged(this, EventArgs.Empty); WindowStateChanged(this, EventArgs.Empty);
} }
protected virtual void OnClosing(CancelEventArgs e) protected virtual void OnClosing(CancelEventArgs e)
{ {
if (Closing != null)
Closing(this, e); Closing(this, e);
} }
protected virtual void OnClosed() protected virtual void OnClosed()
{ {
if (Closed != null)
Closed(this, EventArgs.Empty); Closed(this, EventArgs.Empty);
} }
private void OnMouseLeave() private void OnMouseLeave()
{ {
if (MouseLeave != null)
MouseLeave(this, EventArgs.Empty); MouseLeave(this, EventArgs.Empty);
} }
private void OnMouseEnter() private void OnMouseEnter()
{ {
if (MouseEnter != null)
MouseEnter(this, EventArgs.Empty); MouseEnter(this, EventArgs.Empty);
} }
private void OnActivate() private void OnActivate()
{ {
mIsActive = true; mIsActive = true;
if (FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty); FocusedChanged(this, EventArgs.Empty);
} }
private void OnDeactivate() private void OnDeactivate()
{ {
mIsActive = false; mIsActive = false;
if (FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty); FocusedChanged(this, EventArgs.Empty);
} }
#endregion #endregion
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<CancelEventArgs> Closing = delegate { };
public event EventHandler<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<EventArgs> MouseLeave = delegate { };
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseLeave;
#endregion #endregion
} }

View file

@ -156,7 +156,7 @@ namespace OpenTK.Platform.Windows
else else
focused = (wParam.ToInt64() & 0xFFFF) != 0; focused = (wParam.ToInt64() & 0xFFFF) != 0;
if (new_focused_state != Focused && FocusedChanged != null) if (new_focused_state != Focused)
FocusedChanged(this, EventArgs.Empty); FocusedChanged(this, EventArgs.Empty);
break; break;
@ -188,7 +188,6 @@ namespace OpenTK.Platform.Windows
if (Location != new_location) if (Location != new_location)
{ {
bounds.Location = new_location; bounds.Location = new_location;
if (Move != null)
Move(this, EventArgs.Empty); Move(this, EventArgs.Empty);
} }
@ -206,7 +205,7 @@ namespace OpenTK.Platform.Windows
SetWindowPosFlags.NOZORDER | SetWindowPosFlags.NOOWNERZORDER | SetWindowPosFlags.NOZORDER | SetWindowPosFlags.NOOWNERZORDER |
SetWindowPosFlags.NOACTIVATE | SetWindowPosFlags.NOSENDCHANGING); SetWindowPosFlags.NOACTIVATE | SetWindowPosFlags.NOSENDCHANGING);
if (suppress_resize <= 0 && Resize != null) if (suppress_resize <= 0)
Resize(this, EventArgs.Empty); Resize(this, EventArgs.Empty);
} }
@ -254,7 +253,6 @@ namespace OpenTK.Platform.Windows
if (new_state != windowState) if (new_state != windowState)
{ {
windowState = new_state; windowState = new_state;
if (WindowStateChanged != null)
WindowStateChanged(this, EventArgs.Empty); WindowStateChanged(this, EventArgs.Empty);
} }
@ -274,7 +272,6 @@ namespace OpenTK.Platform.Windows
else else
key_press.KeyChar = (char)wParam.ToInt64(); key_press.KeyChar = (char)wParam.ToInt64();
if (KeyPress != null)
KeyPress(this, key_press); KeyPress(this, key_press);
break; break;
@ -291,7 +288,6 @@ namespace OpenTK.Platform.Windows
mouse_outside_window = false; mouse_outside_window = false;
EnableMouseTracking(); EnableMouseTracking();
if (MouseEnter != null)
MouseEnter(this, EventArgs.Empty); MouseEnter(this, EventArgs.Empty);
} }
break; break;
@ -300,7 +296,6 @@ namespace OpenTK.Platform.Windows
mouse_outside_window = true; mouse_outside_window = true;
// Mouse tracking is disabled automatically by the OS // Mouse tracking is disabled automatically by the OS
if (MouseLeave != null)
MouseLeave(this, EventArgs.Empty); MouseLeave(this, EventArgs.Empty);
break; break;
@ -453,12 +448,10 @@ namespace OpenTK.Platform.Windows
case WindowMessage.CLOSE: case WindowMessage.CLOSE:
System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs(); System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs();
if (Closing != null)
Closing(this, e); Closing(this, e);
if (!e.Cancel) if (!e.Cancel)
{ {
if (Unload != null)
Unload(this, EventArgs.Empty); Unload(this, EventArgs.Empty);
DestroyWindow(); DestroyWindow();
@ -474,7 +467,6 @@ namespace OpenTK.Platform.Windows
window.Dispose(); window.Dispose();
child_window.Dispose(); child_window.Dispose();
if (Closed != null)
Closed(this, EventArgs.Empty); Closed(this, EventArgs.Empty);
break; break;
@ -794,6 +786,8 @@ namespace OpenTK.Platform.Windows
return icon; return icon;
} }
set set
{
if (value != icon)
{ {
icon = value; icon = value;
if (window.WindowHandle != IntPtr.Zero) if (window.WindowHandle != IntPtr.Zero)
@ -801,6 +795,8 @@ namespace OpenTK.Platform.Windows
Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)0, icon == null ? IntPtr.Zero : value.Handle); 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); Functions.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle);
} }
IconChanged(this, EventArgs.Empty);
}
} }
} }
@ -828,9 +824,13 @@ namespace OpenTK.Platform.Windows
return sb_title.ToString(); return sb_title.ToString();
} }
set set
{
if (Title != value)
{ {
if (!Functions.SetWindowText(window.WindowHandle, 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()); Debug.Print("Failed to change window title (window:{0}, new title:{1}, reason:{2}).", window.WindowHandle, value, Marshal.GetLastWin32Error());
TitleChanged(this, EventArgs.Empty);
}
} }
} }
@ -845,6 +845,8 @@ namespace OpenTK.Platform.Windows
return Functions.IsWindowVisible(window.WindowHandle); return Functions.IsWindowVisible(window.WindowHandle);
} }
set set
{
if (value != Visible)
{ {
if (value) if (value)
{ {
@ -859,6 +861,9 @@ namespace OpenTK.Platform.Windows
{ {
Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.HIDE); Functions.ShowWindow(window.WindowHandle, ShowWindowCommand.HIDE);
} }
VisibleChanged(this, EventArgs.Empty);
}
} }
} }
@ -1076,7 +1081,6 @@ namespace OpenTK.Platform.Windows
WindowState = state; WindowState = state;
if (WindowBorderChanged != null)
WindowBorderChanged(this, EventArgs.Empty); WindowBorderChanged(this, EventArgs.Empty);
} }
} }
@ -1113,43 +1117,37 @@ namespace OpenTK.Platform.Windows
#region Events #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<EventArgs> MouseLeave = delegate { };
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseLeave;
#endregion #endregion
@ -1280,6 +1278,7 @@ namespace OpenTK.Platform.Windows
Debug.Print("[Warning] INativeWindow leaked ({0}). Did you forget to call INativeWindow.Dispose()?", this); Debug.Print("[Warning] INativeWindow leaked ({0}). Did you forget to call INativeWindow.Dispose()?", this);
} }
Disposed(this, EventArgs.Empty);
disposed = true; disposed = true;
} }
} }

View file

@ -659,7 +659,6 @@ namespace OpenTK.Platform.X11
if (Location != new_location) if (Location != new_location)
{ {
bounds.Location = new_location; bounds.Location = new_location;
if (Move != null)
Move(this, EventArgs.Empty); Move(this, EventArgs.Empty);
} }
@ -673,13 +672,9 @@ namespace OpenTK.Platform.X11
bounds.Size = new_size; bounds.Size = new_size;
client_rectangle.Size = new Size(e.ConfigureEvent.width, e.ConfigureEvent.height); 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);
} }
} }
}
static IntPtr CreateEmptyCursor(X11WindowInfo window) static IntPtr CreateEmptyCursor(X11WindowInfo window)
{ {
@ -723,7 +718,6 @@ namespace OpenTK.Platform.X11
bool previous_visible = visible; bool previous_visible = visible;
visible = true; visible = true;
if (visible != previous_visible) if (visible != previous_visible)
if (VisibleChanged != null)
VisibleChanged(this, EventArgs.Empty); VisibleChanged(this, EventArgs.Empty);
} }
return; return;
@ -733,7 +727,6 @@ namespace OpenTK.Platform.X11
bool previous_visible = visible; bool previous_visible = visible;
visible = false; visible = false;
if (visible != previous_visible) if (visible != previous_visible)
if (VisibleChanged != null)
VisibleChanged(this, EventArgs.Empty); VisibleChanged(this, EventArgs.Empty);
} }
break; break;
@ -747,7 +740,6 @@ namespace OpenTK.Platform.X11
{ {
Debug.WriteLine("Exit message received."); Debug.WriteLine("Exit message received.");
CancelEventArgs ce = new CancelEventArgs(); CancelEventArgs ce = new CancelEventArgs();
if (Closing != null)
Closing(this, ce); Closing(this, ce);
if (!ce.Cancel) if (!ce.Cancel)
@ -769,7 +761,6 @@ namespace OpenTK.Platform.X11
Debug.WriteLine("Window destroyed"); Debug.WriteLine("Window destroyed");
exists = false; exists = false;
if (Closed != null)
Closed(this, EventArgs.Empty); Closed(this, EventArgs.Empty);
return; return;
@ -813,7 +804,6 @@ namespace OpenTK.Platform.X11
bool previous_focus = has_focus; bool previous_focus = has_focus;
has_focus = true; has_focus = true;
if (has_focus != previous_focus) if (has_focus != previous_focus)
if (FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty); FocusedChanged(this, EventArgs.Empty);
} }
break; break;
@ -823,18 +813,15 @@ namespace OpenTK.Platform.X11
bool previous_focus = has_focus; bool previous_focus = has_focus;
has_focus = false; has_focus = false;
if (has_focus != previous_focus) if (has_focus != previous_focus)
if (FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty); FocusedChanged(this, EventArgs.Empty);
} }
break; break;
case XEventName.LeaveNotify: case XEventName.LeaveNotify:
if (MouseLeave != null)
MouseLeave(this, EventArgs.Empty); MouseLeave(this, EventArgs.Empty);
break; break;
case XEventName.EnterNotify: case XEventName.EnterNotify:
if (MouseEnter != null)
MouseEnter(this, EventArgs.Empty); MouseEnter(this, EventArgs.Empty);
break; break;
@ -850,7 +837,6 @@ namespace OpenTK.Platform.X11
case XEventName.PropertyNotify: case XEventName.PropertyNotify:
if (e.PropertyEvent.atom == _atom_net_wm_state) if (e.PropertyEvent.atom == _atom_net_wm_state)
{ {
if (WindowStateChanged != null)
WindowStateChanged(this, EventArgs.Empty); WindowStateChanged(this, EventArgs.Empty);
} }
@ -1078,7 +1064,6 @@ namespace OpenTK.Platform.X11
} }
icon = value; icon = value;
if (IconChanged != null)
IconChanged(this, EventArgs.Empty); IconChanged(this, EventArgs.Empty);
} }
} }
@ -1277,7 +1262,6 @@ namespace OpenTK.Platform.X11
break; break;
} }
if (WindowBorderChanged != null)
WindowBorderChanged(this, EventArgs.Empty); WindowBorderChanged(this, EventArgs.Empty);
} }
} }
@ -1286,37 +1270,37 @@ namespace OpenTK.Platform.X11
#region Events #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 #endregion
@ -1427,7 +1411,6 @@ namespace OpenTK.Platform.X11
} }
} }
if (TitleChanged != null)
TitleChanged(this, EventArgs.Empty); TitleChanged(this, EventArgs.Empty);
} }
} }