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.

411 lines
9.5 KiB

  1. #include "xerox.h"
  2. #include "group.h"
  3. PGROUP pGroups = NULL;
  4. LPSTR pszCurrentGroup = NULL;
  5. /*
  6. * Adds the names of all defined groups to the listbox/combo box given.
  7. *
  8. * Returns TRUE if success and there are >0 groups defined.
  9. */
  10. BOOL GroupListInit(
  11. HWND hwnd,
  12. BOOL fIsCB)
  13. {
  14. UINT addStringMsg = LB_ADDSTRING;
  15. PGROUP pGroup;
  16. if (pGroups == NULL) {
  17. return(FALSE);
  18. }
  19. if (fIsCB) {
  20. addStringMsg = CB_ADDSTRING;
  21. }
  22. pGroup = pGroups;
  23. while (pGroup) {
  24. SendMessage(hwnd, addStringMsg, 0, (LONG_PTR)pGroup->pszName);
  25. pGroup = pGroup->next;
  26. }
  27. return(TRUE);
  28. }
  29. BOOL DeleteGroupDefinition(
  30. LPSTR szName)
  31. {
  32. PGROUP pGroup, pGroupPrev;
  33. PTITLELIST ptl;
  34. pGroupPrev = NULL;
  35. pGroup = pGroups;
  36. while (pGroup) {
  37. if (!_stricmp(pGroup->pszName, szName)) {
  38. if (pGroupPrev == NULL) {
  39. pGroups = pGroup->next;
  40. } else {
  41. pGroupPrev->next = pGroup->next;
  42. }
  43. while (pGroup->ptl != NULL) {
  44. ptl = pGroup->ptl;
  45. pGroup->ptl = ptl->next;
  46. Free(ptl->pszTitle);
  47. Free(ptl->pszClass);
  48. Free(ptl);
  49. }
  50. Free(pGroup);
  51. if (pGroups != NULL) {
  52. pszCurrentGroup = pGroups->pszName;
  53. } else {
  54. pszCurrentGroup = NULL;
  55. }
  56. return(TRUE);
  57. }
  58. pGroupPrev = pGroup;
  59. pGroup = pGroup->next;
  60. }
  61. return(FALSE);
  62. }
  63. BOOL AddGroupDefinition(
  64. LPSTR szName,
  65. HWND hwndList)
  66. {
  67. PGROUP pGroup, pGroupEnd;
  68. PTITLELIST ptl;
  69. HWND hwnd;
  70. int cItems, cb;
  71. char szClass[MAX_STRING_BYTES];
  72. /*
  73. * Don't allow duplicate groups.
  74. * This is how an existing group can be replaced.
  75. */
  76. DeleteGroupDefinition(szName);
  77. pGroup = Alloc(sizeof(GROUP));
  78. if (pGroup == NULL) {
  79. return(FALSE);
  80. }
  81. pGroup->pszName = Alloc(strlen(szName) + 1);
  82. if (pGroup->pszName == NULL) {
  83. Free(pGroup);
  84. return(FALSE);
  85. }
  86. strcpy(pGroup->pszName, szName);
  87. pGroup->ptl = NULL;
  88. cItems = (int)SendMessage(hwndList, LB_GETCOUNT, 0, 0);
  89. while (cItems--) {
  90. ptl = Alloc(sizeof(TITLELIST));
  91. if (ptl != NULL) {
  92. cb = (int)SendMessage(hwndList, LB_GETTEXTLEN, cItems, 0);
  93. if (cb) {
  94. ptl->pszTitle = Alloc(cb + 1);
  95. if (ptl->pszTitle != NULL) {
  96. SendMessage(hwndList, LB_GETTEXT, cItems, (LONG_PTR)ptl->pszTitle);
  97. hwnd = (HWND)SendMessage(hwndList, LB_GETITEMDATA, cItems, 0);
  98. GetClassName(hwnd, szClass, sizeof(szClass));
  99. ptl->pszClass = Alloc(strlen(szClass) + 1);
  100. if (ptl->pszClass != NULL) {
  101. strcpy(ptl->pszClass, szClass);
  102. ptl->next = pGroup->ptl;
  103. pGroup->ptl = ptl;
  104. } else {
  105. Free(ptl->pszTitle);
  106. Free(ptl);
  107. }
  108. } else{
  109. Free(ptl);
  110. }
  111. } else {
  112. Free(ptl);
  113. }
  114. }
  115. }
  116. if (pGroup->ptl == NULL) {
  117. Free(pGroup);
  118. return(FALSE);
  119. }
  120. /*
  121. * Put it on the end of the list. This is an awkward attempt
  122. * at making the initial group the first group defined. Probably
  123. * should save this info in the registry.
  124. */
  125. pGroup->next = NULL;
  126. if (pGroups != NULL) {
  127. pGroupEnd = pGroups;
  128. while (pGroupEnd->next != NULL) {
  129. pGroupEnd = pGroupEnd->next;
  130. }
  131. pGroupEnd->next = pGroup;
  132. } else {
  133. pGroups = pGroup;
  134. }
  135. pszCurrentGroup = pGroup->pszName;
  136. return(TRUE);
  137. }
  138. BOOL SelectGroupDefinition(
  139. LPSTR szName,
  140. HWND hwndList,
  141. BOOL DisplayMissingWin)
  142. {
  143. PGROUP pGroup;
  144. HWND hwndAdd;
  145. PTITLELIST ptl;
  146. if (szName == NULL) {
  147. return(FALSE);
  148. }
  149. pGroup = FindGroup(szName);
  150. if (pGroup == NULL) {
  151. return(FALSE);
  152. }
  153. while (SendMessage(hwndList, LB_GETCOUNT, 0, 0)) {
  154. SendMessage(hwndList, LB_DELETESTRING, 0, 0);
  155. }
  156. ptl = pGroup->ptl;
  157. while (ptl) {
  158. hwndAdd = FindWindow(ptl->pszClass, ptl->pszTitle);
  159. if (DisplayMissingWin || (hwndAdd != NULL)) {
  160. if (hwndAdd == NULL) {
  161. hwndAdd = INVALID_HANDLE_VALUE;
  162. }
  163. AddLBItemhwnd(hwndList, ptl->pszTitle, (LONG_PTR)hwndAdd);
  164. }
  165. ptl = ptl->next;
  166. }
  167. pszCurrentGroup = pGroup->pszName;
  168. return(TRUE);
  169. }
  170. LPSTR GetCurrentGroup()
  171. {
  172. return(pszCurrentGroup);
  173. }
  174. VOID SetNoCurrentGroup(HWND hwnd, LPSTR szTitle)
  175. {
  176. SetWindowText(hwnd, szTitle);
  177. pszCurrentGroup = NULL;
  178. }
  179. PGROUP FindGroup(
  180. LPSTR szName)
  181. {
  182. PGROUP pGroup;
  183. pGroup = pGroups;
  184. while (pGroup) {
  185. if (!_stricmp(szName, pGroup->pszName)) {
  186. return(pGroup);
  187. }
  188. pGroup = pGroup->next;
  189. }
  190. return(NULL);
  191. }
  192. int CountGroups()
  193. {
  194. PGROUP pGroup;
  195. int c = 0;
  196. pGroup = pGroups;
  197. while (pGroup) {
  198. c++;
  199. pGroup = pGroup->next;
  200. }
  201. return(c);
  202. }
  203. VOID SaveGroups()
  204. {
  205. DWORD cbSave = 1; // for last NULL terminator.
  206. int cTitles;
  207. PGROUP pGroup;
  208. LPSTR pBuf, psz;
  209. LPWORD pw;
  210. HKEY hKey;
  211. PTITLELIST ptl;
  212. if (ERROR_SUCCESS !=
  213. RegCreateKey(HKEY_CURRENT_USER,
  214. "Software\\Microsoft\\Xerox", &hKey)) {
  215. return;
  216. }
  217. pGroup = pGroups;
  218. while (pGroup) {
  219. cbSave += strlen(pGroup->pszName) + 1;
  220. ptl = pGroup->ptl;
  221. while (ptl) {
  222. cbSave += strlen(ptl->pszTitle) + 2 + strlen(ptl->pszClass) + 2;
  223. ptl = ptl->next;
  224. }
  225. pGroup = pGroup->next;
  226. }
  227. if (cbSave == 0) {
  228. return;
  229. }
  230. pBuf = psz = Alloc(cbSave);
  231. if (pBuf == NULL) {
  232. return;
  233. }
  234. RegSetValueEx(hKey, "Groups", 0, REG_MULTI_SZ, "\0\0", 2);
  235. pGroup = pGroups;
  236. while (pGroup) {
  237. strcpy(psz, pGroup->pszName);
  238. psz += strlen(psz) + 1;
  239. ptl = pGroup->ptl;
  240. while (ptl) {
  241. *psz++ = '\t';
  242. strcpy(psz, ptl->pszTitle);
  243. psz += strlen(psz) + 1;
  244. *psz++ = '\t';
  245. strcpy(psz, ptl->pszClass);
  246. psz += strlen(psz) + 1;
  247. ptl = ptl->next;
  248. }
  249. pGroup = pGroup->next;
  250. }
  251. *psz = '\0'; // double NULL terminate last string.
  252. RegSetValueEx(hKey, "Groups", 0, REG_MULTI_SZ, pBuf, cbSave);
  253. RegCloseKey(hKey);
  254. Free(pBuf);
  255. }
  256. VOID FreeGroups()
  257. {
  258. PTITLELIST ptl;
  259. PGROUP pGroup;
  260. pszCurrentGroup = NULL;
  261. while (pGroups) {
  262. while (pGroups->ptl) {
  263. ptl = pGroups->ptl;
  264. Free(ptl->pszTitle);
  265. Free(ptl->pszClass);
  266. pGroups->ptl = ptl->next;
  267. Free(ptl);
  268. }
  269. pGroup = pGroups;
  270. pGroups = pGroups->next;
  271. Free(pGroup);
  272. }
  273. }
  274. VOID LoadGroups()
  275. {
  276. int cTitles;
  277. HKEY hKey;
  278. DWORD cbLoad = 0;
  279. DWORD dwType;
  280. LPSTR pBuf, psz;
  281. PTITLELIST ptl;
  282. PGROUP pGroup, pGroupEnd = NULL;
  283. FreeGroups();
  284. if (ERROR_SUCCESS !=
  285. RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Xerox", &hKey)) {
  286. return;
  287. }
  288. RegQueryValueEx(hKey, "Groups", 0, &dwType, NULL, &cbLoad);
  289. if (dwType != REG_MULTI_SZ) {
  290. RegCloseKey(hKey);
  291. return;
  292. }
  293. if (cbLoad) {
  294. pBuf = psz = Alloc(cbLoad);
  295. if (pBuf == NULL) {
  296. return;
  297. }
  298. if (ERROR_SUCCESS != RegQueryValueEx(hKey, "Groups", 0, &dwType, pBuf, &cbLoad)) {
  299. Free(pBuf);
  300. return;
  301. }
  302. while (*psz) {
  303. pGroup = Alloc(sizeof(GROUP));
  304. if (pGroup == NULL) {
  305. Free(pBuf);
  306. return;
  307. }
  308. pGroup->pszName = Alloc(strlen(psz) + 1);
  309. if (pGroup->pszName == NULL) {
  310. Free(pBuf);
  311. Free(pGroup);
  312. return;
  313. }
  314. strcpy(pGroup->pszName, psz);
  315. pGroup->ptl = NULL;
  316. psz += strlen(psz) + 1;
  317. while (*psz == '\t') {
  318. psz++;
  319. ptl = Alloc(sizeof(TITLELIST));
  320. if (ptl == NULL) {
  321. Free(pBuf);
  322. return;
  323. }
  324. ptl->pszTitle = Alloc(strlen(psz));
  325. if (ptl->pszTitle == NULL) {
  326. Free(pBuf);
  327. Free(ptl);
  328. return;
  329. }
  330. strcpy(ptl->pszTitle, psz);
  331. psz += strlen(psz) + 2;
  332. ptl->pszClass = Alloc(strlen(psz));
  333. if (ptl->pszClass == NULL) {
  334. Free(pBuf);
  335. Free(ptl);
  336. return;
  337. }
  338. strcpy(ptl->pszClass, psz);
  339. psz += strlen(psz) + 1;
  340. ptl->next = pGroup->ptl;
  341. pGroup->ptl = ptl;
  342. }
  343. /*
  344. * Restore groups to origional order.
  345. */
  346. if (pGroupEnd == NULL) {
  347. pGroups = pGroup;
  348. pGroupEnd = pGroup;
  349. } else {
  350. pGroupEnd->next = pGroup;
  351. pGroupEnd = pGroup;
  352. }
  353. pGroup->next = NULL;
  354. }
  355. Free(pBuf);
  356. /*
  357. * Set default current group to first in list.
  358. */
  359. if (pGroups != NULL) {
  360. pszCurrentGroup = pGroups->pszName;
  361. }
  362. }
  363. }