[Win] Refactor huge wndproc into functions

This commit is contained in:
Stefanos A. 2014-01-08 00:02:27 +01:00
parent eacc896605
commit e260a42954

View file

@ -240,15 +240,10 @@ namespace OpenTK.Platform.Windows
#endregion
#region WindowProcedure
#region Message Handlers
IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
void HandleActivate(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
switch (message)
{
#region Size / Move / Style events
case WindowMessage.ACTIVATE:
// See http://msdn.microsoft.com/en-us/library/ms646274(VS.85).aspx (WM_ACTIVATE notification):
// wParam: The low-order word specifies whether the window is being activated or deactivated.
bool new_focused_state = Focused;
@ -259,10 +254,10 @@ namespace OpenTK.Platform.Windows
if (new_focused_state != Focused)
FocusedChanged(this, EventArgs.Empty);
break;
}
case WindowMessage.ENTERMENULOOP:
case WindowMessage.ENTERSIZEMOVE:
void HandleEnterModalLoop(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
// Entering the modal size/move loop: we don't want rendering to
// stop during this time, so we register a timer callback to continue
// processing from time to time.
@ -271,10 +266,10 @@ namespace OpenTK.Platform.Windows
if (!CursorVisible)
UngrabCursor();
break;
}
case WindowMessage.EXITMENULOOP:
case WindowMessage.EXITSIZEMOVE:
void HandleExitModalLoop(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
// Exiting from Modal size/move loop: the timer callback is no longer
// necessary.
is_in_modal_loop = false;
@ -283,12 +278,10 @@ namespace OpenTK.Platform.Windows
// Ensure cursor remains grabbed
if (!CursorVisible)
GrabCursor();
break;
}
case WindowMessage.ERASEBKGND:
return new IntPtr(1);
case WindowMessage.WINDOWPOSCHANGED:
void HandleWindowPositionChanged(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
unsafe
{
WindowPosition* pos = (WindowPosition*)lParam;
@ -329,14 +322,17 @@ namespace OpenTK.Platform.Windows
}
}
}
break;
}
case WindowMessage.STYLECHANGED:
void HandleStyleChanged(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
unsafe
{
if (wParam.ToInt64() == (long)GWL.STYLE)
Debug.WriteLine(wParam.ToString());
if (wParam == new IntPtr((int)GWL.STYLE))
{
WindowStyle style = ((StyleStruct*)lParam)->New;
Debug.WriteLine(style.ToString());
if ((style & WindowStyle.Popup) != 0)
windowBorder = WindowBorder.Hidden;
else if ((style & WindowStyle.ThickFrame) != 0)
@ -349,10 +345,10 @@ namespace OpenTK.Platform.Windows
// Ensure cursor remains grabbed
if (!CursorVisible)
GrabCursor();
}
break;
case WindowMessage.SIZE:
void HandleSize(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
SizeMessage state = (SizeMessage)wParam.ToInt64();
WindowState new_state = windowState;
switch (state)
@ -374,23 +370,21 @@ namespace OpenTK.Platform.Windows
if (!CursorVisible)
GrabCursor();
}
}
break;
void HandleChar(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
#endregion
#region Input events
case WindowMessage.CHAR:
if (IntPtr.Size == 4)
key_press.KeyChar = (char)wParam.ToInt32();
else
key_press.KeyChar = (char)wParam.ToInt64();
KeyPress(this, key_press);
break;
}
case WindowMessage.MOUSEMOVE:
void HandleMouseMove(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Point point = new Point(
(short)((uint)lParam.ToInt32() & 0x0000FFFF),
(short)(((uint)lParam.ToInt32() & 0xFFFF0000) >> 16));
@ -405,68 +399,75 @@ namespace OpenTK.Platform.Windows
MouseEnter(this, EventArgs.Empty);
}
break;
}
case WindowMessage.MOUSELEAVE:
void HandleMouseLeave(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
mouse_outside_window = true;
// Mouse tracking is disabled automatically by the OS
MouseLeave(this, EventArgs.Empty);
break;
}
case WindowMessage.MOUSEWHEEL:
void HandleMouseWheel(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
// This is due to inconsistent behavior of the WParam value on 64bit arch, whese
// wparam = 0xffffffffff880000 or wparam = 0x00000000ff100000
mouse.WheelPrecise += ((long)wParam << 32 >> 48) / 120.0f;
break;
}
case WindowMessage.LBUTTONDOWN:
void HandleLButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
mouse[MouseButton.Left] = true;
break;
}
case WindowMessage.MBUTTONDOWN:
void HandleMButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
mouse[MouseButton.Middle] = true;
break;
}
case WindowMessage.RBUTTONDOWN:
void HandleRButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
mouse[MouseButton.Right] = true;
break;
}
case WindowMessage.XBUTTONDOWN:
void HandleXButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
mouse[((wParam.ToInt32() & 0xFFFF0000) >> 16) !=
(int)MouseKeys.XButton1 ? MouseButton.Button1 : MouseButton.Button2] = true;
break;
}
case WindowMessage.LBUTTONUP:
void HandleLButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
mouse[MouseButton.Left] = false;
break;
}
case WindowMessage.MBUTTONUP:
void HandleMButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
mouse[MouseButton.Middle] = false;
break;
}
case WindowMessage.RBUTTONUP:
void HandleRButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
mouse[MouseButton.Right] = false;
break;
}
case WindowMessage.XBUTTONUP:
void HandleXButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
mouse[((wParam.ToInt32() & 0xFFFF0000) >> 16) !=
(int)MouseKeys.XButton1 ? MouseButton.Button1 : MouseButton.Button2] = false;
break;
}
// Keyboard events:
case WindowMessage.KEYDOWN:
case WindowMessage.KEYUP:
case WindowMessage.SYSKEYDOWN:
case WindowMessage.SYSKEYUP:
void HandleKeyboard(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
bool pressed =
message == WindowMessage.KEYDOWN ||
message == WindowMessage.SYSKEYDOWN;
@ -498,23 +499,16 @@ namespace OpenTK.Platform.Windows
key_up.Key = key;
KeyUp(this, key_up);
}
}
}
return IntPtr.Zero;
case WindowMessage.SYSCHAR:
return IntPtr.Zero;
case WindowMessage.KILLFOCUS:
void HandleKillFocus(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
keyboard.ClearKeys();
break;
}
#endregion
#region Creation / Destruction events
case WindowMessage.CREATE:
void HandleCreate(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
CreateStruct cs = (CreateStruct)Marshal.PtrToStructure(lParam, typeof(CreateStruct));
if (cs.hwndParent == IntPtr.Zero)
{
@ -529,9 +523,10 @@ namespace OpenTK.Platform.Windows
invisible_since_creation = true;
}
break;
}
case WindowMessage.CLOSE:
void HandleClose(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs();
Closing(this, e);
@ -539,12 +534,11 @@ namespace OpenTK.Platform.Windows
if (!e.Cancel)
{
DestroyWindow();
break;
}
}
return IntPtr.Zero;
case WindowMessage.DESTROY:
void HandleDestroy(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
exists = false;
Functions.UnregisterClass(ClassName, Instance);
@ -552,7 +546,128 @@ namespace OpenTK.Platform.Windows
child_window.Dispose();
Closed(this, EventArgs.Empty);
}
#endregion
#region WindowProcedure
IntPtr WindowProcedure(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
switch (message)
{
#region Size / Move / Style events
case WindowMessage.ACTIVATE:
HandleActivate(handle, message, wParam, lParam);
break;
case WindowMessage.ENTERMENULOOP:
case WindowMessage.ENTERSIZEMOVE:
HandleEnterModalLoop(handle, message, wParam, lParam);
break;
case WindowMessage.EXITMENULOOP:
case WindowMessage.EXITSIZEMOVE:
HandleExitModalLoop(handle, message, wParam, lParam);
break;
case WindowMessage.ERASEBKGND:
return new IntPtr(1);
case WindowMessage.WINDOWPOSCHANGED:
HandleWindowPositionChanged(handle, message, wParam, lParam);
break;
case WindowMessage.STYLECHANGED:
HandleStyleChanged(handle, message, wParam, lParam);
break;
case WindowMessage.SIZE:
HandleSize(handle, message, wParam, lParam);
break;
#endregion
#region Input events
case WindowMessage.CHAR:
HandleChar(handle, message, wParam, lParam);
break;
case WindowMessage.MOUSEMOVE:
HandleMouseMove(handle, message, wParam, lParam);
break;
case WindowMessage.MOUSELEAVE:
HandleMouseLeave(handle, message, wParam, lParam);
break;
case WindowMessage.MOUSEWHEEL:
HandleMouseWheel(handle, message, wParam, lParam);
break;
case WindowMessage.LBUTTONDOWN:
HandleLButtonDown(handle, message, wParam, lParam);
break;
case WindowMessage.MBUTTONDOWN:
HandleMButtonDown(handle, message, wParam, lParam);
break;
case WindowMessage.RBUTTONDOWN:
HandleRButtonDown(handle, message, wParam, lParam);
break;
case WindowMessage.XBUTTONDOWN:
HandleXButtonDown(handle, message, wParam, lParam);
break;
case WindowMessage.LBUTTONUP:
HandleLButtonUp(handle, message, wParam, lParam);
break;
case WindowMessage.MBUTTONUP:
HandleMButtonUp(handle, message, wParam, lParam);
break;
case WindowMessage.RBUTTONUP:
HandleRButtonUp(handle, message, wParam, lParam);
break;
case WindowMessage.XBUTTONUP:
HandleXButtonUp(handle, message, wParam, lParam);
break;
// Keyboard events:
case WindowMessage.KEYDOWN:
case WindowMessage.KEYUP:
case WindowMessage.SYSKEYDOWN:
case WindowMessage.SYSKEYUP:
HandleKeyboard(handle, message, wParam, lParam);
return IntPtr.Zero;
case WindowMessage.SYSCHAR:
return IntPtr.Zero;
case WindowMessage.KILLFOCUS:
HandleKillFocus(handle, message, wParam, lParam);
break;
#endregion
#region Creation / Destruction events
case WindowMessage.CREATE:
HandleCreate(handle, message, wParam, lParam);
break;
case WindowMessage.CLOSE:
HandleClose(handle, message, wParam, lParam);
return IntPtr.Zero;
case WindowMessage.DESTROY:
HandleDestroy(handle, message, wParam, lParam);
break;
#endregion