2
0

PolycodeView.cpp 4.5 KB

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