PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap09/Environ/Environ.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 235 lines | 173 code | 49 blank | 13 comment | 18 complexity | 75605d0ed4cc84cc01d2715bf57fef4b MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Environ;
  6. import core.runtime;
  7. import core.thread;
  8. import std.algorithm;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. import std.traits;
  19. pragma(lib, "gdi32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. enum ID_SMALLER = 1; // button window unique id
  25. enum ID_LARGER = 2; // same
  26. enum ID_LIST = 1;
  27. enum ID_TEXT = 2;
  28. enum BTN_WIDTH = "(8 * cxChar)";
  29. enum BTN_HEIGHT = "(4 * cyChar)";
  30. int idFocus;
  31. WNDPROC[3] OldScroll;
  32. HINSTANCE hInst;
  33. extern (Windows)
  34. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  35. {
  36. int result;
  37. try
  38. {
  39. Runtime.initialize();
  40. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  41. Runtime.terminate();
  42. }
  43. catch (Throwable o)
  44. {
  45. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  46. result = 0;
  47. }
  48. return result;
  49. }
  50. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  51. {
  52. hInst = hInstance;
  53. string appName = "Environ";
  54. HWND hwnd;
  55. MSG msg;
  56. WNDCLASS wndclass;
  57. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  58. wndclass.lpfnWndProc = &WndProc;
  59. wndclass.cbClsExtra = 0;
  60. wndclass.cbWndExtra = 0;
  61. wndclass.hInstance = hInstance;
  62. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  63. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  64. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  65. wndclass.lpszMenuName = NULL;
  66. wndclass.lpszClassName = appName.toUTF16z;
  67. if (!RegisterClass(&wndclass))
  68. {
  69. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  70. return 0;
  71. }
  72. hwnd = CreateWindow(appName.toUTF16z, // window class name
  73. "Environment List Box", // window caption
  74. WS_OVERLAPPEDWINDOW, // window style
  75. CW_USEDEFAULT, // initial x position
  76. CW_USEDEFAULT, // initial y position
  77. CW_USEDEFAULT, // initial x size
  78. CW_USEDEFAULT, // initial y size
  79. NULL, // parent window handle
  80. NULL, // window menu handle
  81. hInstance, // program instance handle
  82. NULL); // creation parameters
  83. ShowWindow(hwnd, iCmdShow);
  84. UpdateWindow(hwnd);
  85. while (GetMessage(&msg, NULL, 0, 0))
  86. {
  87. TranslateMessage(&msg);
  88. DispatchMessage(&msg);
  89. }
  90. return msg.wParam;
  91. }
  92. wstring fromWStringz(const wchar* s)
  93. {
  94. if (s is null) return null;
  95. wchar* ptr;
  96. for (ptr = cast(wchar*)s; *ptr; ++ptr) {}
  97. return to!wstring(s[0..ptr-s]);
  98. }
  99. // Note: this is a raw translation from C code.
  100. // It's completely unsafe to handle wide strings this way,
  101. // so don't use this in production code.
  102. void FillListBox(HWND hwndList)
  103. {
  104. wchar* l_EnvStr;
  105. l_EnvStr = GetEnvironmentStringsW();
  106. LPTSTR l_str = l_EnvStr;
  107. int count = 0;
  108. while (true)
  109. {
  110. if (*l_str == 0)
  111. break;
  112. while (*l_str != 0)
  113. l_str++;
  114. l_str++;
  115. count++;
  116. }
  117. for (int i = 0; i < count; i++)
  118. {
  119. auto str = fromWStringz(l_EnvStr);
  120. str.length = str.countUntil("=");
  121. if (str.length)
  122. SendMessage(hwndList, LB_ADDSTRING, 0, cast(LPARAM)((str ~ "\0").ptr));
  123. while (*l_EnvStr != '\0')
  124. l_EnvStr++;
  125. l_EnvStr++;
  126. }
  127. FreeEnvironmentStrings(l_EnvStr);
  128. }
  129. extern (Windows)
  130. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  131. {
  132. scope (failure) assert(0);
  133. static HWND hwndList, hwndText;
  134. int iIndex, iLength, cxChar, cyChar;
  135. wchar[] pVarName, pVarValue;
  136. switch (message)
  137. {
  138. case WM_CREATE:
  139. cxChar = LOWORD(GetDialogBaseUnits());
  140. cyChar = HIWORD(GetDialogBaseUnits());
  141. // Create listbox
  142. hwndList = CreateWindow("listbox", NULL,
  143. WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  144. cxChar, cyChar * 6,
  145. cxChar * 48 + GetSystemMetrics(SM_CXVSCROLL),
  146. cyChar * 24,
  147. hwnd, cast(HMENU)ID_LIST,
  148. cast(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),
  149. NULL);
  150. // static text window with word wrapping
  151. hwndText = CreateWindow("static", NULL,
  152. WS_CHILD | WS_VISIBLE | SS_LEFT,
  153. cxChar, cyChar,
  154. GetSystemMetrics(SM_CXSCREEN), cyChar * 4,
  155. hwnd, cast(HMENU)ID_TEXT,
  156. cast(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE),
  157. NULL);
  158. FillListBox(hwndList);
  159. return 0;
  160. case WM_SETFOCUS:
  161. SetFocus(hwndList);
  162. return 0;
  163. case WM_COMMAND:
  164. if (LOWORD(wParam) == ID_LIST && HIWORD(wParam) == LBN_SELCHANGE)
  165. {
  166. // Get current selection.
  167. iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
  168. iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1;
  169. pVarName.length = iLength;
  170. // could use D strings here
  171. SendMessage(hwndList, LB_GETTEXT, iIndex, cast(LPARAM)pVarName.ptr);
  172. // Get environment string.
  173. iLength = GetEnvironmentVariable(pVarName.ptr, NULL, 0);
  174. pVarValue.length = iLength;
  175. GetEnvironmentVariable((pVarName ~ "\0").ptr, pVarValue.ptr, iLength);
  176. // Show it in window.
  177. SetWindowText(hwndText, pVarValue.ptr);
  178. }
  179. return 0;
  180. case WM_DESTROY:
  181. PostQuitMessage(0);
  182. return 0;
  183. default:
  184. }
  185. return DefWindowProc(hwnd, message, wParam, lParam);
  186. }