PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap12/ClipText/ClipText.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 229 lines | 175 code | 50 blank | 4 comment | 12 complexity | 732dd2a2e71a4e456e1198bf5b092775 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module ClipText;
  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. import std.windows.charset;
  18. pragma(lib, "gdi32.lib");
  19. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. import core.sys.windows.winbase;
  23. import resource;
  24. string appName = "ClipText";
  25. string description = "Clipboard Text Transfers - Unicode Version";
  26. enum ID_TIMER = 1;
  27. HINSTANCE hinst;
  28. enum szDefaultText = "Default Text - Unicode Version";
  29. enum szCaption = "Clipboard Text Transfers - Unicode Version";
  30. alias CF_UNICODETEXT CF_TCHAR;
  31. enum GMEM_SHARE = 8192;
  32. extern (Windows)
  33. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  34. {
  35. int result;
  36. try
  37. {
  38. Runtime.initialize();
  39. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  40. Runtime.terminate();
  41. }
  42. catch (Throwable o)
  43. {
  44. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  45. result = 0;
  46. }
  47. return result;
  48. }
  49. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  50. {
  51. hinst = hInstance;
  52. HWND hwnd;
  53. MSG msg;
  54. WNDCLASS wndclass;
  55. HACCEL hAccel;
  56. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  57. wndclass.lpfnWndProc = &WndProc;
  58. wndclass.cbClsExtra = 0;
  59. wndclass.cbWndExtra = 0;
  60. wndclass.hInstance = hInstance;
  61. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  62. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  63. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  64. wndclass.lpszMenuName = appName.toUTF16z;
  65. wndclass.lpszClassName = appName.toUTF16z;
  66. if (!RegisterClass(&wndclass))
  67. {
  68. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  69. return 0;
  70. }
  71. hwnd = CreateWindow(appName.toUTF16z, // window class name
  72. description.toUTF16z, // window caption
  73. WS_OVERLAPPEDWINDOW, // window style
  74. CW_USEDEFAULT, // initial x position
  75. CW_USEDEFAULT, // initial y position
  76. CW_USEDEFAULT, // initial x size
  77. CW_USEDEFAULT, // initial y size
  78. NULL, // parent window handle
  79. NULL, // window menu handle
  80. hInstance, // program instance handle
  81. NULL); // creation parameters
  82. ShowWindow(hwnd, iCmdShow);
  83. UpdateWindow(hwnd);
  84. hAccel = LoadAccelerators(hInstance, appName.toUTF16z);
  85. while (GetMessage(&msg, NULL, 0, 0))
  86. {
  87. if (!TranslateAccelerator(hwnd, hAccel, &msg))
  88. {
  89. TranslateMessage(&msg);
  90. DispatchMessage(&msg);
  91. }
  92. }
  93. return msg.wParam;
  94. }
  95. wstring fromWStringz(const wchar* s)
  96. {
  97. if (s is null) return null;
  98. wchar* ptr;
  99. for (ptr = cast(wchar*)s; *ptr; ++ptr) {}
  100. return to!wstring(s[0..ptr-s]);
  101. }
  102. extern (Windows)
  103. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  104. {
  105. scope (failure) assert(0);
  106. static wstring pText;
  107. BOOL bEnable;
  108. HGLOBAL hGlobal;
  109. HDC hdc;
  110. PTSTR pGlobal;
  111. PAINTSTRUCT ps;
  112. RECT rect;
  113. switch (message)
  114. {
  115. case WM_CREATE:
  116. SendMessage(hwnd, WM_COMMAND, IDM_EDIT_RESET, 0);
  117. return 0;
  118. case WM_INITMENUPOPUP:
  119. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_PASTE,
  120. IsClipboardFormatAvailable(CF_TCHAR) ? MF_ENABLED : MF_GRAYED);
  121. bEnable = pText.length ? MF_ENABLED : MF_GRAYED;
  122. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_CUT, bEnable);
  123. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_COPY, bEnable);
  124. EnableMenuItem(cast(HMENU)wParam, IDM_EDIT_CLEAR, bEnable);
  125. break;
  126. case WM_COMMAND:
  127. switch (LOWORD(wParam))
  128. {
  129. case IDM_EDIT_PASTE:
  130. OpenClipboard(hwnd);
  131. hGlobal = GetClipboardData(CF_TCHAR);
  132. if (hGlobal !is null)
  133. {
  134. pGlobal = cast(wchar*)GlobalLock(hGlobal);
  135. pText = fromWStringz(pGlobal);
  136. InvalidateRect(hwnd, NULL, TRUE);
  137. }
  138. CloseClipboard();
  139. return 0;
  140. case IDM_EDIT_CUT:
  141. case IDM_EDIT_COPY:
  142. if (!pText.length)
  143. return 0;
  144. hGlobal = GlobalAlloc(GHND | GMEM_SHARE, (pText.length + 1) * wchar.sizeof);
  145. pGlobal = cast(wchar*) GlobalLock(hGlobal);
  146. pGlobal[0..pText.length] = pText[];
  147. GlobalUnlock(hGlobal);
  148. OpenClipboard(hwnd);
  149. EmptyClipboard();
  150. SetClipboardData(CF_TCHAR, hGlobal);
  151. CloseClipboard();
  152. if (LOWORD(wParam) == IDM_EDIT_COPY)
  153. return 0;
  154. goto case IDM_EDIT_CLEAR;
  155. case IDM_EDIT_CLEAR:
  156. pText = "";
  157. InvalidateRect(hwnd, NULL, TRUE);
  158. return 0;
  159. case IDM_EDIT_RESET:
  160. pText = szDefaultText;
  161. InvalidateRect(hwnd, NULL, TRUE);
  162. return 0;
  163. default:
  164. }
  165. break;
  166. case WM_PAINT:
  167. hdc = BeginPaint(hwnd, &ps);
  168. GetClientRect(hwnd, &rect);
  169. if (pText.length)
  170. DrawText(hdc, to!string(pText).toUTF16z, -1, &rect, DT_EXPANDTABS | DT_WORDBREAK);
  171. EndPaint(hwnd, &ps);
  172. return 0;
  173. case WM_DESTROY:
  174. PostQuitMessage(0);
  175. return 0;
  176. default:
  177. }
  178. return DefWindowProc(hwnd, message, wParam, lParam);
  179. }