Windows.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. GWEN
  3. Copyright (c) 2011 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #ifndef GWEN_INPUT_WINDOWS_H
  7. #define GWEN_INPUT_WINDOWS_H
  8. #include "Gwen/InputHandler.h"
  9. #include "Gwen/Gwen.h"
  10. #include "Gwen/Controls/Canvas.h"
  11. #include <windows.h>
  12. namespace Gwen
  13. {
  14. namespace Input
  15. {
  16. class Windows
  17. {
  18. public:
  19. Windows()
  20. {
  21. m_Canvas = NULL;
  22. m_MouseX = 0;
  23. m_MouseY = 0;
  24. }
  25. void Initialize( Gwen::Controls::Canvas* c )
  26. {
  27. m_Canvas = c;
  28. }
  29. bool ProcessMessage( MSG msg )
  30. {
  31. if ( !m_Canvas ) return false;
  32. switch ( msg.message )
  33. {
  34. case WM_MOUSEMOVE:
  35. {
  36. int x = (signed short)LOWORD( msg.lParam );
  37. int y = (signed short)HIWORD( msg.lParam );
  38. int dx = x - m_MouseX;
  39. int dy = y - m_MouseY;
  40. m_MouseX = x;
  41. m_MouseY = y;
  42. return m_Canvas->InputMouseMoved( x, y, dx, dy );
  43. }
  44. case WM_CHAR:
  45. {
  46. Gwen::UnicodeChar chr = (Gwen::UnicodeChar)msg.wParam;
  47. return m_Canvas->InputCharacter( chr );
  48. }
  49. case WM_MOUSEWHEEL:
  50. {
  51. return m_Canvas->InputMouseWheel( (short)HIWORD( msg.wParam ) );
  52. }
  53. case WM_LBUTTONDOWN:
  54. {
  55. SetCapture( msg.hwnd );
  56. return m_Canvas->InputMouseButton( 0, true );
  57. }
  58. case WM_LBUTTONUP:
  59. {
  60. ReleaseCapture();
  61. return m_Canvas->InputMouseButton( 0, false );
  62. }
  63. case WM_RBUTTONDOWN:
  64. {
  65. SetCapture( msg.hwnd );
  66. return m_Canvas->InputMouseButton( 1, true );
  67. }
  68. case WM_RBUTTONUP:
  69. {
  70. ReleaseCapture();
  71. return m_Canvas->InputMouseButton( 1, false );
  72. }
  73. case WM_MBUTTONDOWN:
  74. {
  75. SetCapture( msg.hwnd );
  76. return m_Canvas->InputMouseButton( 2, true );
  77. }
  78. case WM_MBUTTONUP:
  79. {
  80. ReleaseCapture();
  81. return m_Canvas->InputMouseButton( 2, true );
  82. }
  83. case WM_LBUTTONDBLCLK:
  84. case WM_RBUTTONDBLCLK:
  85. case WM_MBUTTONDBLCLK:
  86. {
  87. // Filter out those events from the application
  88. return true;
  89. }
  90. case WM_KEYDOWN:
  91. case WM_KEYUP:
  92. {
  93. bool bDown = msg.message == WM_KEYDOWN;
  94. int iKey = -1;
  95. // These aren't sent by WM_CHAR when CTRL is down - but we need
  96. // them internally for copy and paste etc..
  97. if ( bDown && GetKeyState( VK_CONTROL ) & 0x80 && msg.wParam >= 'A' && msg.wParam <= 'Z' )
  98. {
  99. Gwen::UnicodeChar chr = (Gwen::UnicodeChar)msg.wParam;
  100. return m_Canvas->InputCharacter( chr );
  101. }
  102. if ( msg.wParam == VK_SHIFT ) iKey = Gwen::Key::Shift;
  103. else if ( msg.wParam == VK_RETURN ) iKey = Gwen::Key::Return;
  104. else if ( msg.wParam == VK_BACK ) iKey = Gwen::Key::Backspace;
  105. else if ( msg.wParam == VK_DELETE ) iKey = Gwen::Key::Delete;
  106. else if ( msg.wParam == VK_LEFT ) iKey = Gwen::Key::Left;
  107. else if ( msg.wParam == VK_RIGHT ) iKey = Gwen::Key::Right;
  108. else if ( msg.wParam == VK_TAB ) iKey = Gwen::Key::Tab;
  109. else if ( msg.wParam == VK_SPACE ) iKey = Gwen::Key::Space;
  110. else if ( msg.wParam == VK_HOME ) iKey = Gwen::Key::Home;
  111. else if ( msg.wParam == VK_END ) iKey = Gwen::Key::End;
  112. else if ( msg.wParam == VK_CONTROL ) iKey = Gwen::Key::Control;
  113. else if ( msg.wParam == VK_SPACE ) iKey = Gwen::Key::Space;
  114. else if ( msg.wParam == VK_UP ) iKey = Gwen::Key::Up;
  115. else if ( msg.wParam == VK_DOWN ) iKey = Gwen::Key::Down;
  116. if ( iKey != -1 )
  117. {
  118. return m_Canvas->InputKey( iKey, bDown );
  119. }
  120. break;
  121. }
  122. default:
  123. {
  124. break;
  125. }
  126. }
  127. return false;
  128. }
  129. protected:
  130. Gwen::Controls::Canvas* m_Canvas;
  131. int m_MouseX;
  132. int m_MouseY;
  133. };
  134. }
  135. }
  136. #endif