PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/Chap13/PopPad/PopFind.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 144 lines | 102 code | 31 blank | 11 comment | 6 complexity | 13fcc42fdcd1641b65fac3d15de5a4b2 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module PopFind;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf : count, toUTFz;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. pragma(lib, "comdlg32.lib");
  20. pragma(lib, "winmm.lib");
  21. pragma(lib, "advapi32.lib");
  22. import core.sys.windows.windef;
  23. import core.sys.windows.winuser;
  24. import core.sys.windows.wingdi;
  25. import core.sys.windows.winbase;
  26. import core.sys.windows.commdlg;
  27. import core.sys.windows.mmsystem;
  28. enum MAX_STRING_LEN = 256;
  29. wchar[MAX_STRING_LEN] szFindText = 0;
  30. wchar[MAX_STRING_LEN] szReplText = 0;
  31. wstring fromWStringz(const wchar* s)
  32. {
  33. if (s is null)
  34. return null;
  35. wchar* ptr;
  36. for (ptr = cast(wchar*)s; *ptr; ++ptr)
  37. {
  38. }
  39. return to!wstring(s[0..ptr - s]);
  40. }
  41. HWND PopFindFindDlg(HWND hwnd)
  42. {
  43. static FINDREPLACE fr; // must be static for modeless dialog
  44. fr.hwndOwner = hwnd;
  45. fr.hInstance = NULL;
  46. fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD;
  47. fr.lpstrFindWhat = szFindText.ptr;
  48. fr.lpstrReplaceWith = NULL;
  49. fr.wFindWhatLen = MAX_STRING_LEN;
  50. fr.wReplaceWithLen = 0;
  51. fr.lCustData = 0;
  52. fr.lpfnHook = NULL;
  53. fr.lpTemplateName = NULL;
  54. return FindText(&fr);
  55. }
  56. HWND PopFindReplaceDlg(HWND hwnd)
  57. {
  58. static FINDREPLACE fr; // must be static for modeless dialog
  59. fr.hwndOwner = hwnd;
  60. fr.hInstance = NULL;
  61. fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD;
  62. fr.lpstrFindWhat = szFindText.ptr;
  63. fr.lpstrReplaceWith = szReplText.ptr;
  64. fr.wFindWhatLen = MAX_STRING_LEN;
  65. fr.wReplaceWithLen = MAX_STRING_LEN;
  66. fr.lCustData = 0;
  67. fr.lpfnHook = NULL;
  68. fr.lpTemplateName = NULL;
  69. return ReplaceText(&fr);
  70. }
  71. BOOL PopFindFindText(HWND hwndEdit, int* piSearchOffset, LPFINDREPLACE pfr)
  72. {
  73. int iLength, iPos;
  74. PTSTR pstrDoc, pstrPos;
  75. // Read in the edit document
  76. iLength = GetWindowTextLength(hwndEdit);
  77. pstrDoc = cast(PTSTR)GC.malloc((iLength + 1) * TCHAR.sizeof);
  78. if (pstrDoc is null)
  79. return FALSE;
  80. GetWindowText(hwndEdit, pstrDoc, iLength + 1);
  81. // Search the document for the find string
  82. auto needle = fromWStringz(pfr.lpstrFindWhat);
  83. auto str = pstrDoc[0..iLength];
  84. auto pos = str[*piSearchOffset..$].indexOf(needle);
  85. // Return an error code if the string cannot be found
  86. if (pos == -1) return FALSE;
  87. // Find the position in the document and the new start offset
  88. auto oldPos = *piSearchOffset + pos;
  89. *piSearchOffset += pos + needle.count;
  90. // Select the found text
  91. SendMessage(hwndEdit, EM_SETSEL, oldPos, *piSearchOffset);
  92. SendMessage(hwndEdit, EM_SCROLLCARET, 0, 0);
  93. return TRUE;
  94. }
  95. BOOL PopFindNextText(HWND hwndEdit, int* piSearchOffset)
  96. {
  97. FINDREPLACE fr;
  98. fr.lpstrFindWhat = szFindText.ptr;
  99. return PopFindFindText(hwndEdit, piSearchOffset, &fr);
  100. }
  101. BOOL PopFindReplaceText(HWND hwndEdit, int* piSearchOffset, LPFINDREPLACE pfr)
  102. {
  103. // Find the text
  104. if (!PopFindFindText(hwndEdit, piSearchOffset, pfr))
  105. return FALSE;
  106. // Replace it
  107. SendMessage(hwndEdit, EM_REPLACESEL, 0, cast(LPARAM)pfr.lpstrReplaceWith);
  108. return TRUE;
  109. }
  110. BOOL PopFindValidFind()
  111. {
  112. return *(szFindText.ptr);
  113. }