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.

293 lines
8.2 KiB

  1. #include "stdafx.h"
  2. #include "source.h"
  3. #include "tcsource.h"
  4. #include "regkey.h"
  5. #include "utils.h"
  6. #include "globals.h"
  7. #include "trapreg.h"
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CTcSource
  10. CTcSource::CTcSource()
  11. {
  12. }
  13. CTcSource::~CTcSource()
  14. {
  15. }
  16. BEGIN_MESSAGE_MAP(CTcSource, CTreeCtrl)
  17. //{{AFX_MSG_MAP(CTcSource)
  18. //}}AFX_MSG_MAP
  19. END_MESSAGE_MAP()
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CTcSource message handlers
  22. void CTcSource::LoadImageList()
  23. {
  24. m_ImageList.Create(16, 16, ILC_COLOR | ILC_MASK, 2, 0);
  25. CBitmap* pFolder;
  26. pFolder = new CBitmap;
  27. pFolder->LoadBitmap(IDB_FOLDERCLOSE);
  28. m_ImageList.Add(pFolder, (COLORREF)0xff00ff);
  29. delete pFolder;
  30. pFolder = new CBitmap;
  31. pFolder->LoadBitmap(IDB_FOLDEROPEN);
  32. m_ImageList.Add(pFolder, (COLORREF)0xff00ff);
  33. delete pFolder;
  34. SetImageList(&m_ImageList, TVSIL_NORMAL);
  35. }
  36. SCODE CTcSource::LoadTreeFromRegistry()
  37. {
  38. TV_INSERTSTRUCT TreeCtrlItem;
  39. TreeCtrlItem.hInsertAfter = TVI_LAST;
  40. // Iterate through each of the event logs and add each log to the tree.
  41. LONG nLogs = g_reg.m_aEventLogs.GetSize();
  42. for (LONG iLog=0; iLog < nLogs; ++iLog)
  43. {
  44. CXEventLog* pEventLog = g_reg.m_aEventLogs[iLog];
  45. TreeCtrlItem.hParent = TVI_ROOT;
  46. TreeCtrlItem.item.pszText = (LPTSTR)(LPCTSTR)pEventLog->m_sName;
  47. TreeCtrlItem.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  48. TreeCtrlItem.item.iImage = 0;
  49. TreeCtrlItem.item.iSelectedImage = 0;
  50. TreeCtrlItem.item.lParam = (LPARAM) pEventLog;
  51. HTREEITEM htiParent = InsertItem(&TreeCtrlItem);
  52. TreeCtrlItem.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
  53. TreeCtrlItem.hParent = htiParent;
  54. TreeCtrlItem.item.iImage = 0;
  55. TreeCtrlItem.item.iSelectedImage = 1;
  56. // Insert each source as a child.
  57. LONG nSources = pEventLog->m_aEventSources.GetSize();
  58. for (LONG iSource = 0; iSource < nSources; ++iSource) {
  59. CXEventSource* pEventSource = pEventLog->m_aEventSources.GetAt(iSource);
  60. TreeCtrlItem.item.pszText = (LPTSTR)(LPCTSTR)pEventSource->m_sName;
  61. TreeCtrlItem.item.lParam = (LPARAM) pEventSource;
  62. InsertItem(&TreeCtrlItem);
  63. }
  64. }
  65. SortChildren(NULL);
  66. return S_OK;
  67. }
  68. SCODE CTcSource::CreateWindowEpilogue()
  69. {
  70. LoadImageList();
  71. LoadTreeFromRegistry();
  72. return S_OK;
  73. }
  74. //******************************************************************
  75. // CTcSource::GetSelectedEventSource
  76. //
  77. // Get name of the event source and log for the currently selected
  78. // event source.
  79. //
  80. // Parameters:
  81. // CString& sLog
  82. // This is where the event log name is returned.
  83. //
  84. // CString& sEventSource
  85. // This is where the name of the event source (application)
  86. // is returned.
  87. //
  88. // Returns:
  89. // SCODE
  90. // S_OK if an event source was selected and the log and event source
  91. // names are returned.
  92. //
  93. // E_FAIL if no event source was selected. The log and event source
  94. // names are returned as empty when this occurs.
  95. //
  96. //******************************************************************
  97. CXEventSource* CTcSource::GetSelectedEventSource()
  98. {
  99. HTREEITEM htiParent, htiSelected;
  100. // Get the selected item.
  101. htiSelected = GetSelectedItem();
  102. if (htiSelected == NULL)
  103. return NULL;
  104. // If the selected item is an event source (application), then
  105. // its parent should be the log. To get the log name, we must
  106. // get the parent name.
  107. htiParent = GetParentItem(htiSelected);
  108. if (htiParent == NULL)
  109. return NULL;
  110. // The application name is the selected item.
  111. TV_ITEM tvi;
  112. tvi.hItem = htiSelected;
  113. tvi.mask = TVIF_HANDLE | TVIF_PARAM;
  114. if (GetItem(&tvi) == FALSE)
  115. return NULL;
  116. return (CXEventSource*) (void*) tvi.lParam;
  117. }
  118. //******************************************************************
  119. // CTcSource::Find
  120. //
  121. // Find the specified event source in the tree.
  122. //
  123. // Parameters:
  124. // CString& sText
  125. // A string containing the text to search for.
  126. //
  127. // BOOL bWholeWord
  128. // TRUE if this is a "whole word" search. False if it
  129. // is OK to match a partial word.
  130. //
  131. // BOOL bMatchCase
  132. // TRUE if a case-sensitive comparison should be used.
  133. //
  134. // Returns:
  135. // BOOL
  136. // TRUE if the string was found, FALSE otherwise. If the specified
  137. // text is found, then the selection is set on the corresponding
  138. // tree item.
  139. //
  140. //******************************************************************
  141. BOOL CTcSource::Find(CString& sText, BOOL bWholeWord, BOOL bMatchCase)
  142. {
  143. // Search the source tree for sText. We are only looking at the source
  144. // names, not the types.
  145. HTREEITEM hCurrentItem, hStartItem, hSourceItem, hRootItem;
  146. TV_ITEM tvItem;
  147. CString sSource;
  148. TCHAR szBuffer[256];
  149. BOOL bItemFound = FALSE, bCompleteLoop = FALSE;
  150. // Get the selected item and keep track of it.
  151. hCurrentItem = GetSelectedItem();
  152. if (hCurrentItem == NULL)
  153. {
  154. // Nothing selected; get the root.
  155. hCurrentItem = GetRootItem();
  156. if (hCurrentItem == NULL)
  157. return FALSE;
  158. }
  159. hStartItem = hCurrentItem;
  160. // Loop until we find a match or we are back where we started.
  161. while (!bItemFound && !bCompleteLoop)
  162. {
  163. hSourceItem = NULL;
  164. // Get the next item.
  165. // Current item is root; get the first child.
  166. hRootItem = GetParentItem(hCurrentItem);
  167. if (hRootItem == NULL)
  168. hSourceItem = GetChildItem(hCurrentItem);
  169. // Current item is a source; get the next sibling.
  170. else
  171. {
  172. hSourceItem = GetNextItem(hCurrentItem, TVGN_NEXT);
  173. // No sibling; get the parent and set it as the current item.
  174. if (hSourceItem == NULL)
  175. {
  176. hRootItem = GetParentItem(hCurrentItem);
  177. if (hRootItem == NULL)
  178. return FALSE; // No parent; something is wrong.
  179. hCurrentItem = hRootItem;
  180. }
  181. }
  182. // We have a source; get it and compare.
  183. if (hSourceItem != NULL)
  184. {
  185. hCurrentItem = hSourceItem;
  186. tvItem.mask = TVIF_HANDLE | TVIF_TEXT;
  187. tvItem.hItem = hSourceItem;
  188. tvItem.pszText = szBuffer;
  189. tvItem.cchTextMax = 256;
  190. if (GetItem(&tvItem) == FALSE)
  191. return FALSE; // Something is wrong.
  192. sSource = szBuffer;
  193. // Compare the whole word.
  194. if (bWholeWord)
  195. {
  196. int nRetVal;
  197. // No case compare.
  198. if (bMatchCase)
  199. nRetVal = sSource.Compare(sText);
  200. // Case compare
  201. else
  202. nRetVal = sSource.CompareNoCase(sText);
  203. if (nRetVal == 0)
  204. bItemFound = TRUE;
  205. }
  206. // Look for a substring.
  207. else
  208. {
  209. // Make everything upper.
  210. if (!bMatchCase)
  211. {
  212. sSource.MakeUpper();
  213. sText.MakeUpper();
  214. }
  215. if (sSource.Find(sText) >= 0)
  216. bItemFound = TRUE;
  217. }
  218. }
  219. // Get the next root.
  220. else
  221. {
  222. hRootItem = GetNextItem(hCurrentItem, TVGN_NEXT);
  223. // No more roots in the tree; go to the top of the tree.
  224. if (hRootItem == NULL)
  225. hCurrentItem = GetRootItem();
  226. else
  227. hCurrentItem = hRootItem;
  228. }
  229. if (hCurrentItem == hStartItem)
  230. bCompleteLoop = TRUE;
  231. }
  232. // Found a match; select it.
  233. if (bItemFound)
  234. {
  235. SelectItem(hCurrentItem);
  236. EnsureVisible(hCurrentItem);
  237. SetFocus();
  238. return TRUE;
  239. }
  240. return FALSE;
  241. }