Source code of Windows XP (NT5)
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.

185 lines
4.6 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. HGLOBAL AllocateUser(TCHAR * szName,DWORD dwType);
  7. HGLOBAL hClipboardUser = NULL;
  8. BOOL OnCopy(HWND hwndApp,HWND hwndList)
  9. {
  10. int iSel;
  11. HGLOBAL hUser;
  12. USERDATA * pUserData;
  13. DWORD dwType;
  14. // make sure we can copy... shouldn't get called otherwise, but safety first
  15. if (!CanCopy(hwndList))
  16. return FALSE;
  17. iSel = ListView_GetNextItem(hwndList,-1,LVNI_SELECTED);
  18. if (iSel < 0)
  19. return FALSE;
  20. hUser = (HGLOBAL) LongToHandle(ListView_GetItemParm(hwndList,iSel));
  21. if (!hUser || !(pUserData = (USERDATA *) GlobalLock(hUser)))
  22. return FALSE;
  23. dwType = (pUserData->hdr.dwType & UT_MASK);
  24. GlobalUnlock(hUser);
  25. // free clipboard user if already allocated
  26. if (hClipboardUser) {
  27. GlobalFree(hClipboardUser);
  28. hClipboardUser = NULL;
  29. }
  30. // allocate a clipboard user handle
  31. hClipboardUser = AllocateUser(TEXT(""),dwType);
  32. if (!hClipboardUser) {
  33. MsgBox(hwndApp,IDS_ErrOUTOFMEMORY,MB_ICONEXCLAMATION,MB_OK);
  34. return FALSE;
  35. }
  36. if (!CopyUser(hUser,hClipboardUser)) {
  37. MsgBox(hwndApp,IDS_ErrOUTOFMEMORY,MB_ICONEXCLAMATION,MB_OK);
  38. return FALSE;
  39. }
  40. EnableMenuItems(hwndApp,dwAppState);
  41. return TRUE;
  42. }
  43. BOOL OnPaste(HWND hwndApp,HWND hwndList)
  44. {
  45. int iItem = -1,iSelItem;
  46. DWORD dwClipboardUserType;
  47. HGLOBAL hUser;
  48. UINT nSelItems=0;
  49. // make sure we can paste... shouldn't get called otherwise, but safety first
  50. if (!CanPaste(hwndList))
  51. return FALSE;
  52. // find out how many items are selected
  53. while ((iItem=ListView_GetNextItem(hwndList,iItem,LVNI_SELECTED))
  54. >=0) {
  55. iSelItem = iItem;
  56. nSelItems++;
  57. }
  58. dwClipboardUserType = GetClipboardUserType();
  59. // display appropriate confirmation message depending on whether 1 user,
  60. // 1 computer, or multiple items selected
  61. if (nSelItems == 1) {
  62. HGLOBAL hUser;
  63. USERDATA *pUserData;
  64. UINT uMsg;
  65. int nRet;
  66. if (!(hUser = (HGLOBAL) LongToHandle(ListView_GetItemParm(hwndList,iSelItem))) ||
  67. !(pUserData = (USERDATA *) GlobalLock(hUser)))
  68. return FALSE;
  69. uMsg = pUserData->hdr.dwType & UT_USER ? IDS_QUERYPASTE_USER
  70. : IDS_QUERYPASTE_WORKSTA;
  71. nRet=MsgBoxParam(hwndApp,uMsg,pUserData->hdr.szName,MB_ICONQUESTION,
  72. MB_YESNO);
  73. GlobalUnlock(hUser);
  74. if (nRet != IDYES)
  75. return FALSE;
  76. } else {
  77. if (MsgBox(hwndApp,IDS_QUERYPASTE_MULTIPLE,MB_ICONQUESTION,MB_YESNO)
  78. != IDYES)
  79. return FALSE;
  80. }
  81. iItem = -1;
  82. while ((iItem=ListView_GetNextItem(hwndList,iItem,LVNI_SELECTED)) > -1) {
  83. if ((hUser = (HGLOBAL) LongToHandle(ListView_GetItemParm(hwndList,iItem)))) {
  84. if (!CopyUser(hClipboardUser,hUser)) {
  85. MsgBox(hwndApp,IDS_ErrOUTOFMEMORY,MB_ICONEXCLAMATION,MB_OK);
  86. return FALSE;
  87. }
  88. }
  89. }
  90. return TRUE;
  91. }
  92. // returns the type of user (user or computer) pasted to the "clipboard"
  93. UINT GetClipboardUserType(VOID)
  94. {
  95. USERDATA * pUserData;
  96. DWORD dwType;
  97. if (!hClipboardUser || !(pUserData = (USERDATA *) GlobalLock(hClipboardUser)))
  98. return 0;
  99. dwType = (pUserData->hdr.dwType & UT_MASK);
  100. GlobalUnlock(hClipboardUser);
  101. return dwType;
  102. }
  103. // returns TRUE if the copy menu item should be enabled. This will
  104. // happen if in policy file mode and exactly one item selected in listview
  105. BOOL CanCopy(HWND hwndList)
  106. {
  107. int iItem=-1;
  108. UINT nCount = 0;
  109. if (dwAppState & AS_POLICYFILE) {
  110. while ((iItem=ListView_GetNextItem(hwndList,iItem,LVNI_SELECTED)) > -1)
  111. nCount ++;
  112. }
  113. return (nCount == 1);
  114. }
  115. // returns TRUE if the paste menu item should be enabled. This will happen
  116. // if in policy file mode, have a user on the clipboard, there is at least one
  117. // selected item and all the selected items are the same type (user or computer)
  118. // as the item on the clipboard
  119. BOOL CanPaste(HWND hwndList)
  120. {
  121. int iItem = -1;
  122. DWORD dwClipboardUserType;
  123. UINT nCount = 0;
  124. HGLOBAL hUser;
  125. USERDATA * pUserData;
  126. BOOL fMatch;
  127. if (!(dwAppState & AS_POLICYFILE) || !(hClipboardUser))
  128. return FALSE;
  129. dwClipboardUserType = GetClipboardUserType();
  130. while ((iItem=ListView_GetNextItem(hwndList,iItem,LVNI_SELECTED)) > -1) {
  131. nCount ++;
  132. if ((hUser = (HGLOBAL) LongToHandle(ListView_GetItemParm(hwndList,iItem)))) {
  133. if (!(pUserData = (USERDATA *) GlobalLock(hUser)))
  134. return FALSE;
  135. // is this user the same type as the one on the clipboard?
  136. fMatch = (pUserData->hdr.dwType & dwClipboardUserType);
  137. GlobalUnlock(hUser);
  138. if (!fMatch)
  139. return FALSE;
  140. }
  141. }
  142. if (!nCount)
  143. return FALSE;
  144. return TRUE;
  145. }