PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap10/PopPad2/PopPad2.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 248 lines | 192 code | 52 blank | 4 comment | 17 complexity | f8ef79ffb89b7dbeff5f1753c403f100 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module PopPad2;
  6. import core.runtime;
  7. import core.thread;
  8. import std.conv;
  9. import std.math;
  10. import std.range;
  11. import std.string;
  12. import std.utf;
  13. auto toUTF16z(S)(S s)
  14. {
  15. return toUTFz!(const(wchar)*)(s);
  16. }
  17. pragma(lib, "gdi32.lib");
  18. import core.sys.windows.windef;
  19. import core.sys.windows.winuser;
  20. import core.sys.windows.wingdi;
  21. import core.sys.windows.winbase;
  22. import resource;
  23. string appName = "PopPad2";
  24. enum ID_TIMER = 1;
  25. enum ID_EDIT = 1;
  26. HINSTANCE hinst;
  27. extern (Windows)
  28. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  29. {
  30. int result;
  31. try
  32. {
  33. Runtime.initialize();
  34. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  35. Runtime.terminate();
  36. }
  37. catch (Throwable o)
  38. {
  39. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  40. result = 0;
  41. }
  42. return result;
  43. }
  44. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  45. {
  46. HWND hwnd;
  47. MSG msg;
  48. WNDCLASS wndclass;
  49. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  50. wndclass.lpfnWndProc = &WndProc;
  51. wndclass.cbClsExtra = 0;
  52. wndclass.cbWndExtra = 0;
  53. wndclass.hInstance = hInstance;
  54. wndclass.hIcon = LoadIcon (hInstance, appName.toUTF16z);
  55. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  56. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  57. wndclass.lpszMenuName = appName.toUTF16z;
  58. wndclass.lpszClassName = appName.toUTF16z;
  59. if (!RegisterClass(&wndclass))
  60. {
  61. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  62. return 0;
  63. }
  64. hwnd = CreateWindow(appName.toUTF16z, // window class name
  65. "description", // window caption
  66. WS_OVERLAPPEDWINDOW, // window style
  67. CW_USEDEFAULT, // initial x position
  68. CW_USEDEFAULT, // initial y position
  69. CW_USEDEFAULT, // initial x size
  70. CW_USEDEFAULT, // initial y size
  71. NULL, // parent window handle
  72. NULL, // window menu handle
  73. hInstance, // program instance handle
  74. NULL); // creation parameters
  75. ShowWindow(hwnd, iCmdShow);
  76. UpdateWindow(hwnd);
  77. while (GetMessage(&msg, NULL, 0, 0))
  78. {
  79. TranslateMessage(&msg);
  80. DispatchMessage(&msg);
  81. }
  82. return msg.wParam;
  83. }
  84. auto AskConfirmation(HWND hwnd)
  85. {
  86. return MessageBox(hwnd, "Really want to close PopPad2?", appName.toUTF16z, MB_YESNO | MB_ICONQUESTION);
  87. }
  88. extern (Windows)
  89. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  90. {
  91. scope (failure) assert(0);
  92. static HWND hwndEdit;
  93. int iSelect, iEnable;
  94. switch (message)
  95. {
  96. case WM_CREATE:
  97. hwndEdit = CreateWindow( "edit", NULL,
  98. WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
  99. WS_BORDER | ES_LEFT | ES_MULTILINE |
  100. ES_AUTOHSCROLL | ES_AUTOVSCROLL,
  101. 0, 0, 0, 0, hwnd, cast(HMENU)ID_EDIT,
  102. (cast(LPCREATESTRUCT)lParam).hInstance, NULL);
  103. return 0;
  104. case WM_SETFOCUS:
  105. SetFocus(hwndEdit);
  106. return 0;
  107. case WM_SIZE:
  108. MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  109. return 0;
  110. case WM_INITMENUPOPUP:
  111. if (lParam == 1)
  112. {
  113. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_UNDO,
  114. SendMessage(hwndEdit, EM_CANUNDO, 0, 0) ?
  115. MF_ENABLED : MF_GRAYED);
  116. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_PASTE,
  117. IsClipboardFormatAvailable(CF_TEXT) ?
  118. MF_ENABLED : MF_GRAYED);
  119. iSelect = SendMessage(hwndEdit, EM_GETSEL, 0, 0);
  120. if (HIWORD(iSelect) == LOWORD(iSelect))
  121. iEnable = MF_GRAYED;
  122. else
  123. iEnable = MF_ENABLED;
  124. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_CUT, iEnable);
  125. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_COPY, iEnable);
  126. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_CLEAR, iEnable);
  127. return 0;
  128. }
  129. break;
  130. case WM_COMMAND:
  131. if (lParam)
  132. {
  133. if (LOWORD(lParam) == ID_EDIT &&
  134. (HIWORD(wParam) == EN_ERRSPACE ||
  135. HIWORD(wParam) == EN_MAXTEXT))
  136. MessageBox(hwnd, "Edit control out of space.", appName.toUTF16z, MB_OK | MB_ICONSTOP);
  137. return 0;
  138. }
  139. else
  140. switch (LOWORD(wParam))
  141. {
  142. case IDM_FILE_NEW:
  143. case IDM_FILE_OPEN:
  144. case IDM_FILE_SAVE:
  145. case IDM_FILE_SAVE_AS:
  146. case IDM_FILE_PRINT:
  147. MessageBeep(0);
  148. return 0;
  149. case IDM_APP_EXIT:
  150. SendMessage(hwnd, WM_CLOSE, 0, 0);
  151. return 0;
  152. case IDM_EDIT_UNDO:
  153. SendMessage(hwndEdit, WM_UNDO, 0, 0);
  154. return 0;
  155. case IDM_EDIT_CUT:
  156. SendMessage(hwndEdit, WM_CUT, 0, 0);
  157. return 0;
  158. case IDM_EDIT_COPY:
  159. SendMessage(hwndEdit, WM_COPY, 0, 0);
  160. return 0;
  161. case IDM_EDIT_PASTE:
  162. SendMessage(hwndEdit, WM_PASTE, 0, 0);
  163. return 0;
  164. case IDM_EDIT_CLEAR:
  165. SendMessage(hwndEdit, WM_CLEAR, 0, 0);
  166. return 0;
  167. case IDM_EDIT_SELECT_ALL:
  168. SendMessage(hwndEdit, EM_SETSEL, 0, -1);
  169. return 0;
  170. case IDM_HELP_HELP:
  171. MessageBox(hwnd, "Help not yet implemented!",
  172. appName.toUTF16z, MB_OK | MB_ICONEXCLAMATION);
  173. return 0;
  174. case IDM_APP_ABOUT:
  175. MessageBox(hwnd, "POPPAD2 (c) Charles Petzold, 1998",
  176. appName.toUTF16z, MB_OK | MB_ICONINFORMATION);
  177. return 0;
  178. default:
  179. }
  180. break;
  181. case WM_CLOSE:
  182. if (IDYES == AskConfirmation(hwnd))
  183. DestroyWindow(hwnd);
  184. return 0;
  185. case WM_QUERYENDSESSION:
  186. if (IDYES == AskConfirmation(hwnd))
  187. return 1;
  188. else
  189. return 0;
  190. case WM_DESTROY:
  191. PostQuitMessage(0);
  192. return 0;
  193. default:
  194. }
  195. return DefWindowProc(hwnd, message, wParam, lParam);
  196. }