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.

742 lines
15 KiB

  1. /*++
  2. Copyright (c) 1994-95 Microsoft Corporation
  3. Module Name:
  4. utils.cpp
  5. Abstract:
  6. Utiltities.
  7. Author:
  8. Don Ryan (donryan) 04-Jan-1995
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. Jeff Parham (jeffparh) 12-Nov-1995
  13. Copied from LLSMGR, stripped Tv (Tree view) functions,
  14. removed OLE support
  15. --*/
  16. #include "stdafx.h"
  17. #include "ccfapi.h"
  18. #include "utils.h"
  19. #define _AFX_NO_OLE_SUPPORT
  20. //
  21. // List view utilities
  22. //
  23. void LvInitColumns(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo)
  24. /*++
  25. Routine Description:
  26. Initializes list view columns.
  27. Arguments:
  28. pListCtrl - list control.
  29. plvColumnInfo - column information.
  30. Return Values:
  31. None.
  32. --*/
  33. {
  34. ASSERT(plvColumnInfo);
  35. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  36. int nStringId;
  37. CString strText;
  38. LV_COLUMN lvColumn;
  39. int nColumns = plvColumnInfo->nColumns;
  40. ASSERT(NULL != plvColumnInfo);
  41. PLV_COLUMN_ENTRY plvColumnEntry = plvColumnInfo->lvColumnEntry;
  42. lvColumn.mask = LVCF_FMT|
  43. LVCF_TEXT|
  44. LVCF_SUBITEM;
  45. lvColumn.fmt = LVCFMT_LEFT;
  46. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  47. while (nColumns--)
  48. {
  49. lvColumn.iSubItem = plvColumnEntry->iSubItem;
  50. if (0 < (nStringId = plvColumnEntry->nStringId))
  51. {
  52. strText.LoadString(nStringId);
  53. }
  54. else
  55. {
  56. strText = _T("");
  57. }
  58. lvColumn.pszText = strText.GetBuffer(0);
  59. int nColumnInserted = pListCtrl->InsertColumn( lvColumn.iSubItem, &lvColumn );
  60. ASSERT( -1 != nColumnInserted );
  61. plvColumnEntry++;
  62. strText.ReleaseBuffer();
  63. }
  64. SetDefaultFont(pListCtrl);
  65. LvResizeColumns(pListCtrl, plvColumnInfo);
  66. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  67. }
  68. void LvResizeColumns(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo)
  69. /*++
  70. Routine Description:
  71. Resizes list view columns.
  72. Arguments:
  73. pListCtrl - list control.
  74. plvColumnInfo - column information.
  75. Return Values:
  76. None.
  77. --*/
  78. {
  79. ASSERT(plvColumnInfo);
  80. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  81. int nColumnWidth;
  82. int nRelativeWidth;
  83. int nEntireWidthSoFar = 0;
  84. int nColumns = plvColumnInfo->nColumns;
  85. PLV_COLUMN_ENTRY plvColumnEntry = plvColumnInfo->lvColumnEntry;
  86. CRect clientRect;
  87. ASSERT(NULL != pListCtrl);
  88. pListCtrl->GetClientRect(clientRect);
  89. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  90. while ((nRelativeWidth = plvColumnEntry->nRelativeWidth) != -1)
  91. {
  92. nColumnWidth = (nRelativeWidth * clientRect.Width()) / 100;
  93. pListCtrl->SetColumnWidth(plvColumnEntry->iSubItem, nColumnWidth);
  94. nEntireWidthSoFar += nColumnWidth;
  95. plvColumnEntry++;
  96. }
  97. nColumnWidth = clientRect.Width() - nEntireWidthSoFar;
  98. pListCtrl->SetColumnWidth(plvColumnEntry->iSubItem, nColumnWidth);
  99. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  100. }
  101. void LvChangeFormat(CListCtrl* pListCtrl, UINT nFormatId)
  102. /*++
  103. Routine Description:
  104. Changes window style of list view.
  105. Arguments:
  106. pListCtrl - list control.
  107. nFormatId - format specification.
  108. Return Values:
  109. None.
  110. --*/
  111. {
  112. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  113. DWORD dwStyle = ::GetWindowLong(pListCtrl->GetSafeHwnd(), GWL_STYLE);
  114. ASSERT(NULL != pListCtrl);
  115. pListCtrl->BeginWaitCursor();
  116. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  117. if ((dwStyle & LVS_TYPEMASK) != nFormatId)
  118. {
  119. ::SetWindowLong(
  120. pListCtrl->GetSafeHwnd(),
  121. GWL_STYLE,
  122. (dwStyle & ~LVS_TYPEMASK) | nFormatId
  123. );
  124. }
  125. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  126. pListCtrl->EndWaitCursor();
  127. }
  128. LPVOID LvGetSelObj(CListCtrl* pListCtrl)
  129. /*++
  130. Routine Description:
  131. Retrieves the object selected (assumes one) from list view.
  132. Arguments:
  133. pListCtrl - list control.
  134. Return Values:
  135. Same as LvGetNextObj.
  136. --*/
  137. {
  138. int iItem = -1;
  139. return LvGetNextObj(pListCtrl, &iItem);
  140. }
  141. LPVOID LvGetNextObj(CListCtrl* pListCtrl, LPINT piItem, int nType)
  142. /*++
  143. Routine Description:
  144. Retrieves the next object selected from list view.
  145. Arguments:
  146. pListCtrl - list control.
  147. piItem - starting index (updated).
  148. nType - specifies search criteria.
  149. Return Values:
  150. Returns object pointer or null.
  151. --*/
  152. {
  153. ASSERT(piItem);
  154. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  155. LV_ITEM lvItem;
  156. ASSERT(NULL != pListCtrl);
  157. if ((lvItem.iItem = pListCtrl->GetNextItem(*piItem, nType)) != -1)
  158. {
  159. lvItem.mask = LVIF_PARAM;
  160. lvItem.iSubItem = 0;
  161. if (pListCtrl->GetItem(&lvItem))
  162. {
  163. *piItem = lvItem.iItem;
  164. return (LPVOID)lvItem.lParam;
  165. }
  166. }
  167. return NULL;
  168. }
  169. BOOL LvInsertObArray(CListCtrl* pListCtrl, PLV_COLUMN_INFO plvColumnInfo, CObArray* pObArray)
  170. /*++
  171. Routine Description:
  172. Insert object array into list view.
  173. Note list view must be unsorted and support LVN_GETDISPINFO.
  174. Arguments:
  175. pListCtrl - list control.
  176. plvColumnInfo - column info.
  177. pObArray - object array.
  178. Return Values:
  179. VT_BOOL.
  180. --*/
  181. {
  182. VALIDATE_OBJECT(pObArray, CObArray);
  183. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  184. ASSERT(plvColumnInfo);
  185. ASSERT(NULL != pListCtrl);
  186. ASSERT(pListCtrl->GetItemCount() == 0);
  187. BOOL bItemsInserted = FALSE;
  188. LV_ITEM lvItem;
  189. lvItem.mask = LVIF_TEXT|
  190. LVIF_PARAM|
  191. LVIF_IMAGE;
  192. lvItem.pszText = LPSTR_TEXTCALLBACK;
  193. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  194. lvItem.iImage = I_IMAGECALLBACK;
  195. lvItem.iSubItem = 0;
  196. int iItem;
  197. int iSubItem;
  198. int nItems = (int)pObArray->GetSize();
  199. ASSERT(nItems != -1); // iItem is -1 if error...
  200. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  201. pListCtrl->SetItemCount(nItems);
  202. CObject* pObject = NULL;
  203. for (iItem = 0; (-1 != iItem) && (iItem < nItems) && (NULL != (pObject = pObArray->GetAt(iItem))); iItem++)
  204. {
  205. VALIDATE_OBJECT(pObject, CObject);
  206. lvItem.iItem = iItem;
  207. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  208. iItem = pListCtrl->InsertItem(&lvItem);
  209. ASSERT((iItem == lvItem.iItem) || (iItem == -1));
  210. if ( -1 != iItem )
  211. {
  212. for (iSubItem = 1; iSubItem < plvColumnInfo->nColumns; iSubItem++)
  213. {
  214. BOOL ok = pListCtrl->SetItemText(iItem, iSubItem, LPSTR_TEXTCALLBACK);
  215. ASSERT( ok );
  216. }
  217. }
  218. }
  219. if (iItem == nItems)
  220. {
  221. bItemsInserted = TRUE;
  222. VERIFY(pListCtrl->SetItemState(
  223. 0,
  224. LVIS_FOCUSED|
  225. LVIS_SELECTED,
  226. LVIS_FOCUSED|
  227. LVIS_SELECTED
  228. ));
  229. }
  230. else
  231. {
  232. theApp.SetLastError(ERROR_OUTOFMEMORY);
  233. VERIFY(pListCtrl->DeleteAllItems());
  234. }
  235. LvResizeColumns(pListCtrl, plvColumnInfo);
  236. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  237. return bItemsInserted;
  238. }
  239. BOOL
  240. LvRefreshObArray(
  241. CListCtrl* pListCtrl,
  242. PLV_COLUMN_INFO plvColumnInfo,
  243. CObArray* pObArray
  244. )
  245. /*++
  246. Routine Description:
  247. Refresh object array in list view.
  248. Arguments:
  249. pListCtrl - list control.
  250. plvColumnInfo - column info.
  251. pObArray - object array.
  252. Return Values:
  253. VT_BOOL.
  254. --*/
  255. {
  256. ASSERT(plvColumnInfo);
  257. VALIDATE_OBJECT(pObArray, CObArray);
  258. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  259. long nObjects = (long)pObArray->GetSize();
  260. long nObjectsInList = pListCtrl->GetItemCount();
  261. if (!nObjects)
  262. {
  263. LvReleaseObArray(pListCtrl);
  264. return TRUE;
  265. }
  266. else if (!nObjectsInList)
  267. {
  268. return LvInsertObArray(
  269. pListCtrl,
  270. plvColumnInfo,
  271. pObArray
  272. );
  273. }
  274. CObject* pObject;
  275. int iObject = 0;
  276. int iObjectInList = 0;
  277. LV_ITEM lvItem;
  278. ASSERT(NULL != pListCtrl);
  279. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  280. while (nObjectsInList--)
  281. {
  282. lvItem.mask = LVIF_PARAM;
  283. lvItem.iItem = iObjectInList;
  284. lvItem.iSubItem = 0;
  285. VERIFY(pListCtrl->GetItem(&lvItem));
  286. pObject = (CObject*)lvItem.lParam;
  287. VALIDATE_OBJECT(pObject, CObject);
  288. if (iObject < nObjects)
  289. {
  290. pObject = pObArray->GetAt(iObject++);
  291. VALIDATE_OBJECT(pObject, CObject);
  292. lvItem.mask = LVIF_TEXT|LVIF_PARAM;
  293. lvItem.pszText = LPSTR_TEXTCALLBACK;
  294. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  295. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  296. VERIFY(pListCtrl->SetItem(&lvItem)); // overwrite...
  297. iObjectInList++; // increment count...
  298. }
  299. else
  300. {
  301. VERIFY(pListCtrl->DeleteItem(iObjectInList));
  302. }
  303. }
  304. lvItem.mask = LVIF_TEXT|
  305. LVIF_PARAM|
  306. LVIF_IMAGE;
  307. lvItem.pszText = LPSTR_TEXTCALLBACK;
  308. lvItem.cchTextMax = LPSTR_TEXTCALLBACK_MAX;
  309. lvItem.iImage = I_IMAGECALLBACK;
  310. lvItem.iSubItem = 0;
  311. int iItem;
  312. int iSubItem;
  313. while (iObject < nObjects)
  314. {
  315. lvItem.iItem = iObject;
  316. pObject = pObArray->GetAt(iObject++);
  317. VALIDATE_OBJECT(pObject, CObject);
  318. lvItem.lParam = (LPARAM)(LPVOID)pObject;
  319. iItem = pListCtrl->InsertItem(&lvItem);
  320. ASSERT((iItem == lvItem.iItem) && (iItem != -1));
  321. for (iSubItem = 1; iSubItem < plvColumnInfo->nColumns; iSubItem++)
  322. {
  323. VERIFY(pListCtrl->SetItemText(iItem, iSubItem, LPSTR_TEXTCALLBACK));
  324. }
  325. }
  326. LvResizeColumns(pListCtrl, plvColumnInfo);
  327. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  328. return TRUE;
  329. }
  330. void LvReleaseObArray(CListCtrl* pListCtrl)
  331. /*++
  332. Routine Description:
  333. Release objects inserted into list view.
  334. Arguments:
  335. pListCtrl - list control.
  336. Return Values:
  337. None.
  338. --*/
  339. {
  340. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  341. LV_ITEM lvItem;
  342. CObject* pObject;
  343. lvItem.mask = LVIF_PARAM;
  344. lvItem.iItem = 0;
  345. lvItem.iSubItem = 0;
  346. ASSERT(NULL != pListCtrl);
  347. int nObjectsInList = pListCtrl->GetItemCount();
  348. pListCtrl->BeginWaitCursor();
  349. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  350. while (nObjectsInList--)
  351. {
  352. VERIFY(pListCtrl->GetItem(&lvItem));
  353. pObject = (CObject*)lvItem.lParam;
  354. VALIDATE_OBJECT(pObject, CObject);
  355. VERIFY(pListCtrl->DeleteItem(lvItem.iItem));
  356. }
  357. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  358. pListCtrl->EndWaitCursor();
  359. }
  360. void LvReleaseSelObjs(CListCtrl* pListCtrl)
  361. /*++
  362. Routine Description:
  363. Release selected objects in list view.
  364. Arguments:
  365. pListCtrl - list control.
  366. Return Values:
  367. None.
  368. --*/
  369. {
  370. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  371. LV_ITEM lvItem;
  372. lvItem.mask = LVIF_PARAM;
  373. lvItem.iSubItem = 0;
  374. CObject* pObject;
  375. ASSERT(NULL != pListCtrl);
  376. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  377. int iItem = pListCtrl->GetNextItem(-1, LVNI_ALL|LVNI_SELECTED);
  378. while (iItem != -1)
  379. {
  380. lvItem.iItem = iItem;
  381. VERIFY(pListCtrl->GetItem(&lvItem));
  382. pObject = (CObject*)lvItem.lParam;
  383. VALIDATE_OBJECT(pObject, CObject);
  384. iItem = pListCtrl->GetNextItem(lvItem.iItem, LVNI_ALL|LVNI_SELECTED);
  385. VERIFY(pListCtrl->DeleteItem(lvItem.iItem));
  386. }
  387. LvSelObjIfNecessary(pListCtrl);
  388. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  389. }
  390. void LvSelObjIfNecessary(CListCtrl* pListCtrl, BOOL bSetFocus)
  391. /*++
  392. Routine Description:
  393. Ensure that object selected.
  394. Arguments:
  395. pListCtrl - list control.
  396. bSetFocus - true if focus to be set focus as well.
  397. Return Values:
  398. None.
  399. --*/
  400. {
  401. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  402. if (!IsItemSelectedInList(pListCtrl) && pListCtrl->GetItemCount())
  403. {
  404. pListCtrl->SendMessage(WM_KEYDOWN, VK_RIGHT); // HACKHACK...
  405. int iItem = pListCtrl->GetNextItem(-1, LVNI_FOCUSED|LVNI_ALL);
  406. int nState = bSetFocus ? (LVIS_SELECTED|LVIS_FOCUSED) : LVIS_SELECTED;
  407. VERIFY(pListCtrl->SetItemState((iItem == -1) ? 0 : iItem, nState, nState));
  408. }
  409. }
  410. #ifdef _DEBUG
  411. void LvDumpObArray(CListCtrl* pListCtrl)
  412. /*++
  413. Routine Description:
  414. Release objects inserted into list view.
  415. Arguments:
  416. pListCtrl - list control.
  417. Return Values:
  418. None.
  419. --*/
  420. {
  421. VALIDATE_OBJECT(pListCtrl, CListCtrl);
  422. LV_ITEM lvItem;
  423. CString strDump;
  424. CObject* pObject;
  425. lvItem.mask = LVIF_STATE|LVIF_PARAM;
  426. lvItem.stateMask = (DWORD)-1;
  427. lvItem.iSubItem = 0;
  428. ASSERT(NULL != pListCtrl);
  429. int nObjectsInList = pListCtrl->GetItemCount();
  430. pListCtrl->SetRedraw(FALSE); // turn off drawing...
  431. while (nObjectsInList--)
  432. {
  433. lvItem.iItem = nObjectsInList;
  434. VERIFY(pListCtrl->GetItem(&lvItem));
  435. pObject = (CObject*)lvItem.lParam;
  436. VALIDATE_OBJECT(pObject, CObject);
  437. strDump.Format(_T("iItem %d"), lvItem.iItem);
  438. strDump += (lvItem.state & LVIS_CUT) ? _T(" LVIS_CUT ") : _T("");
  439. strDump += (lvItem.state & LVIS_FOCUSED) ? _T(" LVIS_FOCUSED ") : _T("");
  440. strDump += (lvItem.state & LVIS_SELECTED) ? _T(" LVIS_SELECTED ") : _T("");
  441. strDump += _T("\r\n");
  442. afxDump << strDump;
  443. }
  444. pListCtrl->SetRedraw(TRUE); // turn on drawing...
  445. }
  446. #endif
  447. void SetDefaultFont(CWnd* pWnd)
  448. /*++
  449. Routine Description:
  450. Set default font.
  451. Arguments:
  452. pWnd - window to change font.
  453. Return Values:
  454. None.
  455. --*/
  456. {
  457. VALIDATE_OBJECT(pWnd, CWnd);
  458. HFONT hFont;
  459. LOGFONT lFont;
  460. CHARSETINFO csi;
  461. DWORD dw = ::GetACP();
  462. TCHAR szData[7] ;
  463. LANGID wLang = GetUserDefaultUILanguage();
  464. csi.ciCharset = DEFAULT_CHARSET;
  465. if( GetLocaleInfo(MAKELCID(wLang, SORT_DEFAULT), LOCALE_IDEFAULTANSICODEPAGE, szData, (sizeof( szData ) / sizeof( TCHAR ))) > 0)
  466. {
  467. UINT uiCp = _ttoi(szData);
  468. TranslateCharsetInfo((DWORD*) (DWORD_PTR)uiCp, &csi, TCI_SRCCODEPAGE);
  469. }
  470. ZeroMemory(&lFont, sizeof(lFont)); // initialize
  471. //
  472. // Merged from FE NT 4.0.
  473. //
  474. // if (!::TranslateCharsetInfo((DWORD*)dw, &csi, TCI_SRCCODEPAGE))
  475. // csi.ciCharset = DEFAULT_CHARSET;
  476. lFont.lfCharSet = (BYTE)csi.ciCharset;
  477. lFont.lfHeight = 13;
  478. lFont.lfWeight = 200; // non-bold
  479. hFont = ::CreateFontIndirect(&lFont);
  480. pWnd->SetFont(CFont::FromHandle(hFont));
  481. }