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.

367 lines
8.1 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. // QueryColPg.cpp : implementation file
  8. //
  9. #include "stdafx.h"
  10. #include "WMITest.h"
  11. #include "QueryColPg.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CQueryColPg property page
  19. IMPLEMENT_DYNCREATE(CQueryColPg, CPropertyPage)
  20. CQueryColPg::CQueryColPg() : CPropertyPage(CQueryColPg::IDD)
  21. {
  22. //{{AFX_DATA_INIT(CQueryColPg)
  23. // NOTE: the ClassWizard will add member initialization here
  24. //}}AFX_DATA_INIT
  25. }
  26. CQueryColPg::~CQueryColPg()
  27. {
  28. }
  29. void CQueryColPg::DoDataExchange(CDataExchange* pDX)
  30. {
  31. CPropertyPage::DoDataExchange(pDX);
  32. //{{AFX_DATA_MAP(CQueryColPg)
  33. DDX_Control(pDX, IDC_SELECTED, m_ctlSelected);
  34. DDX_Control(pDX, IDC_AVAILABLE, m_ctlAvailable);
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(CQueryColPg, CPropertyPage)
  38. //{{AFX_MSG_MAP(CQueryColPg)
  39. ON_BN_CLICKED(IDC_ADD, OnAdd)
  40. ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  41. ON_BN_CLICKED(IDC_ADD_ALL, OnAddAll)
  42. ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  43. ON_LBN_SELCHANGE(IDC_SELECTED, OnSelchangeSelected)
  44. ON_LBN_SELCHANGE(IDC_AVAILABLE, OnSelchangeAvailable)
  45. ON_LBN_DBLCLK(IDC_SELECTED, OnDblclkSelected)
  46. ON_LBN_DBLCLK(IDC_AVAILABLE, OnDblclkAvailable)
  47. ON_BN_CLICKED(IDC_UP, OnUp)
  48. ON_BN_CLICKED(IDC_DOWN, OnDown)
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CQueryColPg message handlers
  53. void CQueryColPg::OnAdd()
  54. {
  55. int nCount = m_ctlAvailable.GetSelCount();
  56. if (nCount)
  57. {
  58. int *piItems = new int[nCount];
  59. CString strProp;
  60. m_ctlAvailable.GetSelItems(nCount, piItems);
  61. for (int i = 0; i < nCount; i++)
  62. {
  63. int iWhere = piItems[i] - i;
  64. m_ctlAvailable.GetText(iWhere, strProp);
  65. m_ctlAvailable.DeleteString(iWhere);
  66. m_ctlSelected.AddString(strProp);
  67. }
  68. delete piItems;
  69. }
  70. UpdateButtons();
  71. }
  72. void CQueryColPg::OnRemove()
  73. {
  74. int nCount = m_ctlSelected.GetSelCount();
  75. if (nCount)
  76. {
  77. int *piItems = new int[nCount];
  78. CString strProp;
  79. m_ctlSelected.GetSelItems(nCount, piItems);
  80. for (int i = 0; i < nCount; i++)
  81. {
  82. int iWhere = piItems[i] - i;
  83. m_ctlSelected.GetText(iWhere, strProp);
  84. m_ctlSelected.DeleteString(iWhere);
  85. m_ctlAvailable.AddString(strProp);
  86. }
  87. delete piItems;
  88. }
  89. UpdateButtons();
  90. }
  91. void CQueryColPg::OnAddAll()
  92. {
  93. int nCount = m_ctlAvailable.GetCount(),
  94. iWhere = m_ctlSelected.GetCurSel();
  95. CString strProp;
  96. if (iWhere == -1)
  97. iWhere = 0;
  98. for (int i = 0; i < nCount; i++)
  99. {
  100. m_ctlAvailable.GetText(i, strProp);
  101. m_ctlSelected.InsertString(iWhere + i, strProp);
  102. }
  103. m_ctlAvailable.ResetContent();
  104. UpdateButtons();
  105. }
  106. void CQueryColPg::OnRemoveAll()
  107. {
  108. int nCount = m_ctlSelected.GetCount();
  109. CString strProp;
  110. for (int i = 0; i < nCount; i++)
  111. {
  112. m_ctlSelected.GetText(i, strProp);
  113. m_ctlAvailable.AddString(strProp);
  114. }
  115. m_ctlSelected.ResetContent();
  116. UpdateButtons();
  117. }
  118. BOOL CQueryColPg::OnSetActive()
  119. {
  120. if (m_pSheet->m_bColsNeeded)
  121. {
  122. m_pSheet->m_bColsNeeded = FALSE;
  123. LoadList();
  124. }
  125. UpdateButtons();
  126. return CPropertyPage::OnSetActive();
  127. }
  128. void CQueryColPg::LoadList()
  129. {
  130. CWaitCursor wait;
  131. SAFEARRAY *pArr = NULL;
  132. HRESULT hr;
  133. IWbemClassObject *pClass = NULL;
  134. m_ctlSelected.ResetContent();
  135. m_ctlAvailable.ResetContent();
  136. if (SUCCEEDED(hr = m_pSheet->m_pNamespace->GetObject(
  137. _bstr_t(m_pSheet->m_strClass),
  138. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  139. NULL,
  140. &pClass,
  141. NULL)))
  142. {
  143. hr =
  144. pClass->GetNames(
  145. NULL,
  146. WBEM_FLAG_ALWAYS,
  147. NULL,
  148. &pArr);
  149. if (SUCCEEDED(hr))
  150. {
  151. BSTR *pNames = (BSTR*) pArr->pvData;
  152. for (int i = 0;
  153. i < (int) pArr->rgsabound[0].cElements;
  154. i++)
  155. {
  156. m_ctlAvailable.AddString(_bstr_t(pNames[i]));
  157. }
  158. SafeArrayDestroy(pArr);
  159. }
  160. pClass->Release();
  161. }
  162. }
  163. #define IDC_FINISH 0x3025
  164. void CQueryColPg::UpdateButtons()
  165. {
  166. BOOL bSelColSelected = m_ctlSelected.GetSelCount() != 0,
  167. bAllSelected = m_ctlSelected.GetSelCount() == m_ctlSelected.GetCount();
  168. GetDlgItem(IDC_ADD)->EnableWindow(m_ctlAvailable.GetSelCount() != 0);
  169. GetDlgItem(IDC_REMOVE)->EnableWindow(bSelColSelected);
  170. GetDlgItem(IDC_UP)->EnableWindow(bSelColSelected && !bAllSelected);
  171. GetDlgItem(IDC_DOWN)->EnableWindow(bSelColSelected && !bAllSelected);
  172. GetDlgItem(IDC_ADD_ALL)->EnableWindow(m_ctlAvailable.GetCount() != 0);
  173. GetDlgItem(IDC_REMOVE_ALL)->EnableWindow(m_ctlSelected.GetCount() != 0);
  174. m_pSheet->SetWizardButtons(PSWIZB_BACK |
  175. (m_ctlSelected.GetCount() != 0 ? PSWIZB_FINISH : PSWIZB_DISABLEDFINISH));
  176. }
  177. void CQueryColPg::OnSelchangeSelected()
  178. {
  179. UpdateButtons();
  180. }
  181. void CQueryColPg::OnSelchangeAvailable()
  182. {
  183. UpdateButtons();
  184. }
  185. void CQueryColPg::OnDblclkSelected()
  186. {
  187. OnRemove();
  188. }
  189. void CQueryColPg::OnDblclkAvailable()
  190. {
  191. OnAdd();
  192. }
  193. BOOL CQueryColPg::OnWizardFinish()
  194. {
  195. // If m_ctlAvailable is empty we can assume they want all the
  196. // properties.
  197. if (m_ctlAvailable.GetCount() == 0)
  198. m_pSheet->m_strQuery.Format(
  199. _T("select * from %s"),
  200. (LPCTSTR) m_pSheet->m_strClass);
  201. else
  202. {
  203. CString strProps,
  204. strProp;
  205. int nProps = m_ctlSelected.GetCount();
  206. for (int i = 0; i < nProps; i++)
  207. {
  208. if (i != 0)
  209. strProps += _T(",");
  210. m_ctlSelected.GetText(i, strProp);
  211. strProps += strProp;
  212. }
  213. m_pSheet->m_strQuery.Format(
  214. _T("select %s from %s"),
  215. (LPCTSTR) strProps,
  216. (LPCTSTR) m_pSheet->m_strClass);
  217. }
  218. return CPropertyPage::OnWizardFinish();
  219. }
  220. void CQueryColPg::OnUp()
  221. {
  222. int nCount = m_ctlSelected.GetSelCount();
  223. if (nCount)
  224. {
  225. int *piItems = new int[nCount],
  226. iBegin;
  227. CString strProp;
  228. m_ctlSelected.GetSelItems(nCount, piItems);
  229. // Find the first one we can move up.
  230. for (iBegin = 0; iBegin < nCount && piItems[iBegin] == iBegin;
  231. iBegin++)
  232. {
  233. }
  234. for (int i = iBegin; i < nCount; i++)
  235. {
  236. int iWhere = piItems[i];
  237. if (iWhere != 0)
  238. {
  239. m_ctlSelected.GetText(iWhere, strProp);
  240. m_ctlSelected.DeleteString(iWhere);
  241. m_ctlSelected.InsertString(iWhere - 1, strProp);
  242. m_ctlSelected.SetSel(iWhere - 1, TRUE);
  243. }
  244. }
  245. delete piItems;
  246. }
  247. UpdateButtons();
  248. }
  249. void CQueryColPg::OnDown()
  250. {
  251. int nCount = m_ctlSelected.GetSelCount();
  252. if (nCount)
  253. {
  254. int *piItems = new int[nCount],
  255. nListCount = m_ctlSelected.GetCount(),
  256. iBegin,
  257. i;
  258. CString strProp;
  259. m_ctlSelected.GetSelItems(nCount, piItems);
  260. // Find the first one we can move down.
  261. for (i = nListCount - 1, iBegin = nCount - 1;
  262. iBegin >= 0 && piItems[iBegin] == i;
  263. iBegin--, i--)
  264. {
  265. }
  266. for (i = iBegin; i >= 0; i--)
  267. {
  268. int iWhere = piItems[i];
  269. if (iWhere != m_ctlSelected.GetCount() - 1)
  270. {
  271. m_ctlSelected.GetText(iWhere, strProp);
  272. m_ctlSelected.DeleteString(iWhere);
  273. m_ctlSelected.InsertString(iWhere + 1, strProp);
  274. m_ctlSelected.SetSel(iWhere + 1, TRUE);
  275. }
  276. }
  277. delete piItems;
  278. }
  279. UpdateButtons();
  280. }