PolycodeView.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "PolycodeView.h"
  2. #include "PolyWinCore.h"
  3. #include "PolyCoreServices.h"
  4. #include "PolyCoreInput.h"
  5. #include "PolyRenderer.h"
  6. #include <io.h>
  7. #include <fcntl.h>
  8. using namespace Polycode;
  9. Win32Core *core = NULL;
  10. static void OpenConsole()
  11. {
  12. int outHandle, errHandle, inHandle;
  13. FILE *outFile, *errFile, *inFile;
  14. AllocConsole();
  15. CONSOLE_SCREEN_BUFFER_INFO coninfo;
  16. GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
  17. coninfo.dwSize.Y = 9999;
  18. SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
  19. outHandle = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
  20. errHandle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE),_O_TEXT);
  21. inHandle = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE),_O_TEXT );
  22. outFile = _fdopen(outHandle, "w" );
  23. errFile = _fdopen(errHandle, "w");
  24. inFile = _fdopen(inHandle, "r");
  25. *stdout = *outFile;
  26. *stderr = *errFile;
  27. *stdin = *inFile;
  28. setvbuf( stdout, NULL, _IONBF, 0 );
  29. setvbuf( stderr, NULL, _IONBF, 0 );
  30. setvbuf( stdin, NULL, _IONBF, 0 );
  31. std::ios::sync_with_stdio();
  32. }
  33. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  34. {
  35. int wmId, wmEvent;
  36. PAINTSTRUCT ps;
  37. HDC hdc;
  38. int nWidth, nHeight;
  39. bool useDefault = false;
  40. if(!core)
  41. return DefWindowProc(hWnd, message, wParam, lParam);
  42. switch (message)
  43. {
  44. case WM_SIZE:
  45. nWidth = LOWORD(lParam);
  46. nHeight = HIWORD(lParam);
  47. if(core) {
  48. core->handleViewResize(nWidth, nHeight);
  49. }
  50. break;
  51. case WM_MOUSEMOVE:
  52. if(core)
  53. core->handleMouseMove(lParam,wParam);
  54. break;
  55. case WM_MOUSEWHEEL:
  56. if(core)
  57. core->handleMouseWheel(lParam,wParam);
  58. break;
  59. case WM_LBUTTONDOWN:
  60. if(core)
  61. core->handleMouseDown(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  62. break;
  63. case WM_LBUTTONUP:
  64. if(core)
  65. core->handleMouseUp(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  66. break;
  67. case WM_RBUTTONDOWN:
  68. if(core)
  69. core->handleMouseDown(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  70. break;
  71. case WM_RBUTTONUP:
  72. if(core)
  73. core->handleMouseUp(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  74. break;
  75. case WM_TOUCH:
  76. if(core) {
  77. if(core->isMultiTouchEnabled()) {
  78. core->handleTouchEvent(lParam, wParam);
  79. }
  80. }
  81. break;
  82. case WM_MBUTTONDOWN:
  83. if(core)
  84. core->handleMouseDown(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  85. break;
  86. case WM_MBUTTONUP:
  87. if(core)
  88. core->handleMouseUp(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  89. break;
  90. case WM_KEYDOWN:
  91. if(core) {
  92. wchar_t unicodeChar = 0;
  93. MSG m;
  94. m.hwnd = hWnd;
  95. m.message = message;
  96. m.wParam = wParam;
  97. m.lParam = lParam;
  98. m.time = 0;
  99. if ( PeekMessage(&m, hWnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
  100. GetMessage(&m, hWnd, 0, WM_USER);
  101. unicodeChar = (wchar_t)m.wParam;
  102. }
  103. core->handleKeyDown(lParam,wParam, unicodeChar);
  104. }
  105. break;
  106. case WM_KEYUP:
  107. if(core)
  108. core->handleKeyUp(lParam,wParam);
  109. break;
  110. case WM_CLOSE:
  111. if(core)
  112. core->Shutdown();
  113. useDefault = true;
  114. break;
  115. case WM_DESTROY:
  116. PostQuitMessage(0);
  117. break;
  118. default:
  119. useDefault = true;
  120. break;
  121. }
  122. if (useDefault)
  123. return DefWindowProc(hWnd, message, wParam, lParam);
  124. else
  125. return 0;
  126. }
  127. PolycodeView::PolycodeView(HINSTANCE hInstance, int nCmdShow, LPCTSTR windowTitle, bool resizable, bool showDebugConsole) : PolycodeViewBase() {
  128. WNDCLASSEX wcex;
  129. wcex.cbSize = sizeof(WNDCLASSEX);
  130. wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  131. wcex.lpfnWndProc = WndProc;
  132. wcex.cbClsExtra = 0;
  133. wcex.cbWndExtra = 0;
  134. wcex.hInstance = hInstance;
  135. wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  136. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  137. wcex.hbrBackground = NULL;
  138. wcex.lpszMenuName = NULL;
  139. wcex.lpszClassName = L"POLYCODEAPPLICATION";
  140. wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
  141. RegisterClassEx(&wcex);
  142. if(resizable) {
  143. hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"POLYCODEAPPLICATION", windowTitle, WS_OVERLAPPEDWINDOW|WS_SYSMENU, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
  144. } else {
  145. hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"POLYCODEAPPLICATION", windowTitle, WS_OVERLAPPED|WS_SYSMENU, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
  146. }
  147. windowData = (void*)&hwnd;
  148. ShowWindow(hwnd, nCmdShow);
  149. UpdateWindow(hwnd);
  150. if(showDebugConsole) {
  151. OpenConsole();
  152. }
  153. }
  154. PolycodeView::~PolycodeView() {
  155. }