Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.0 KiB

  1. //// TEXTREP - Text representation
  2. //
  3. // For this demonstration, text representation is rather simple
  4. #include "precomp.hxx"
  5. #include "global.h"
  6. #include <tchar.h>
  7. //// InitText - Initialise text buffer
  8. //
  9. //
  10. void InitText(INT id) {
  11. HMODULE hmod;
  12. HRSRC hrsrc;
  13. HGLOBAL hglob;
  14. WCHAR *pwcIt; // Initial text
  15. int i;
  16. g_iTextLen = 0;
  17. hmod = GetModuleHandle(NULL);
  18. hrsrc = FindResource(hmod, MAKEINTRESOURCE(id), _TEXT("INITIALTEXT"));
  19. hglob = LoadResource(hmod, hrsrc);
  20. g_iTextLen = SizeofResource(hmod, hrsrc) - 2; // Remove leading byte order mark
  21. pwcIt = (WCHAR*) LockResource(hglob);
  22. if (!hmod || !hrsrc || !hglob || !pwcIt || !g_iTextLen) {
  23. ASSERTS(hmod, "GetModuleHandle(usptest.exe) failed");
  24. ASSERTS(hrsrc, "FindResource(110, INITIALTEXT) failed");
  25. ASSERTS(hglob, "LoadResource(110, INITIALTEXT) failed");
  26. ASSERTS(pwcIt, "LockResource(110, INITIALTEXT) failed");
  27. ASSERTS(g_iTextLen, "INITIALTEXT length zero");
  28. g_iTextLen = 0;
  29. }
  30. if (g_iTextLen >= sizeof(g_wcBuf)) {
  31. g_iTextLen = sizeof(g_wcBuf);
  32. }
  33. memcpy(g_wcBuf, pwcIt+1, g_iTextLen);
  34. g_iTextLen >>= 1; // Bytes to characters
  35. // Drop any zero padding
  36. i = 0;
  37. while ( (i < g_iTextLen)
  38. && g_wcBuf[i]) {
  39. i++;
  40. }
  41. g_iTextLen = i;
  42. // Construct initial formatting style run covering the entire text
  43. StyleExtendRange(0, g_iTextLen);
  44. ASSERT(StyleCheckRange());
  45. }
  46. //// textDelete - Delete text from buffer
  47. //
  48. BOOL TextDelete(int iPos, int iLen) {
  49. if ( iPos < 0
  50. || iLen < 0
  51. || iPos +iLen > g_iTextLen) {
  52. return FALSE;
  53. }
  54. if (iPos + iLen >= g_iTextLen) {
  55. g_iTextLen = iPos;
  56. StyleDeleteRange(iPos, iLen);
  57. ASSERT(StyleCheckRange());
  58. return TRUE;
  59. }
  60. if (iLen == 0) {
  61. return TRUE;
  62. }
  63. memcpy(g_wcBuf + iPos, g_wcBuf + iPos + iLen, sizeof(WCHAR) * (g_iTextLen - (iPos + iLen)));
  64. g_iTextLen -= iLen;
  65. StyleDeleteRange(iPos, iLen);
  66. ASSERT(StyleCheckRange());
  67. return TRUE;
  68. }
  69. //// textInsert - Insert new characters in the buffer at the given insertion point
  70. //
  71. BOOL TextInsert(int iPos, PWCH pwc, int iLen) {
  72. if ( iPos < 0
  73. || iLen < 0
  74. || iPos + iLen >= MAX_TEXT
  75. || iPos > g_iTextLen) {
  76. return FALSE;
  77. }
  78. // Shift text above iPos up the buffer
  79. if (iPos < g_iTextLen) {
  80. memmove(g_wcBuf+iPos+iLen, g_wcBuf+iPos, sizeof(WCHAR)*(g_iTextLen-iPos));
  81. }
  82. // Copy new text into buffer
  83. memcpy(g_wcBuf+iPos, pwc, sizeof(WCHAR)*iLen);
  84. g_iTextLen += iLen;
  85. // Give the new characters the same style as the original character they follow
  86. StyleExtendRange(iPos, iLen);
  87. ASSERT(StyleCheckRange());
  88. return TRUE;
  89. }