PolycodeView.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 nWidth, nHeight;
  37. bool useDefault = false;
  38. if(!core)
  39. return DefWindowProc(hWnd, message, wParam, lParam);
  40. switch (message)
  41. {
  42. case WM_COPYDATA:
  43. {
  44. COPYDATASTRUCT *cp = (COPYDATASTRUCT*)lParam;
  45. wchar_t *stringData = (wchar_t*)cp->lpData;
  46. core->copyDataString = String(stringData);
  47. core->hasCopyDataString = true;
  48. }
  49. break;
  50. case WM_SIZE:
  51. nWidth = LOWORD(lParam);
  52. nHeight = HIWORD(lParam);
  53. if(core) {
  54. core->handleViewResize(nWidth, nHeight);
  55. }
  56. break;
  57. case WM_MOUSEMOVE:
  58. if(core)
  59. core->handleMouseMove(lParam,wParam);
  60. break;
  61. case WM_MOUSEWHEEL:
  62. if(core)
  63. core->handleMouseWheel(lParam,wParam);
  64. break;
  65. case WM_LBUTTONDOWN:
  66. if(core)
  67. core->handleMouseDown(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  68. break;
  69. case WM_LBUTTONUP:
  70. if(core)
  71. core->handleMouseUp(CoreInput::MOUSE_BUTTON1, lParam,wParam);
  72. break;
  73. case WM_RBUTTONDOWN:
  74. if(core)
  75. core->handleMouseDown(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  76. break;
  77. case WM_RBUTTONUP:
  78. if(core)
  79. core->handleMouseUp(CoreInput::MOUSE_BUTTON2, lParam,wParam);
  80. break;
  81. #ifndef NO_TOUCH_API
  82. case WM_TOUCH:
  83. if(core) {
  84. if(core->isMultiTouchEnabled()) {
  85. core->handleTouchEvent(lParam, wParam);
  86. }
  87. }
  88. break;
  89. #endif
  90. case WM_MBUTTONDOWN:
  91. if(core)
  92. core->handleMouseDown(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  93. break;
  94. case WM_MBUTTONUP:
  95. if(core)
  96. core->handleMouseUp(CoreInput::MOUSE_BUTTON3, lParam,wParam);
  97. break;
  98. case WM_KEYDOWN:
  99. case WM_SYSKEYDOWN:
  100. if(core) {
  101. wchar_t unicodeChar = 0;
  102. MSG m;
  103. m.hwnd = hWnd;
  104. m.message = message;
  105. m.wParam = wParam;
  106. m.lParam = lParam;
  107. m.time = 0;
  108. if ( PeekMessage(&m, hWnd, 0, WM_USER, PM_NOREMOVE) && (m.message == WM_CHAR) ) {
  109. GetMessage(&m, hWnd, 0, WM_USER);
  110. unicodeChar = (wchar_t)m.wParam;
  111. }
  112. core->handleKeyDown(lParam,wParam, unicodeChar);
  113. }
  114. break;
  115. case WM_KEYUP:
  116. case WM_SYSKEYUP:
  117. if(core)
  118. core->handleKeyUp(lParam,wParam);
  119. break;
  120. case WM_CLOSE:
  121. if(core)
  122. core->Shutdown();
  123. useDefault = true;
  124. break;
  125. case WM_DESTROY:
  126. PostQuitMessage(0);
  127. break;
  128. default:
  129. useDefault = true;
  130. break;
  131. }
  132. if (useDefault)
  133. return DefWindowProc(hWnd, message, wParam, lParam);
  134. else
  135. return 0;
  136. }
  137. PolycodeView::PolycodeView(HINSTANCE hInstance, int nCmdShow, LPCTSTR windowTitle, bool resizable, bool showDebugConsole) : PolycodeViewBase() {
  138. /*
  139. typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
  140. SetProcessDPIAwarePtr set_process_dpi_aware_func = GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware");
  141. if (set_process_dpi_aware_func) {
  142. set_process_dpi_aware_func();
  143. }
  144. */
  145. WNDCLASSEX wcex;
  146. wcex.cbSize = sizeof(WNDCLASSEX);
  147. wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  148. wcex.lpfnWndProc = WndProc;
  149. wcex.cbClsExtra = 0;
  150. wcex.cbWndExtra = 0;
  151. wcex.hInstance = hInstance;
  152. wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  153. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  154. wcex.hbrBackground = NULL;
  155. wcex.lpszMenuName = NULL;
  156. wcex.lpszClassName = L"POLYCODEAPPLICATION";
  157. wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
  158. RegisterClassEx(&wcex);
  159. this->resizable = resizable;
  160. if(resizable) {
  161. hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"POLYCODEAPPLICATION", windowTitle, WS_OVERLAPPEDWINDOW | WS_SYSMENU, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
  162. } else {
  163. hwnd = CreateWindowEx(WS_EX_APPWINDOW, L"POLYCODEAPPLICATION", windowTitle, WS_CAPTION | WS_POPUP | WS_SYSMENU, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
  164. }
  165. windowData = (void*)&hwnd;
  166. ShowWindow(hwnd, nCmdShow);
  167. UpdateWindow(hwnd);
  168. if(showDebugConsole) {
  169. OpenConsole();
  170. }
  171. }
  172. PolycodeView::~PolycodeView() {
  173. }