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.

372 lines
8.7 KiB

  1. // VarSetEditDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #import "\bin\McsVarSetMin.tlb" no_namespace, named_guids
  5. #include "Driver.h"
  6. #include "VSEdit.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CVarSetEditDlg dialog
  14. CVarSetEditDlg::CVarSetEditDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CVarSetEditDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CVarSetEditDlg)
  18. m_bCaseSensitive = FALSE;
  19. m_Filename = _T("");
  20. m_Key = _T("");
  21. m_Value = _T("");
  22. m_varset = NULL;
  23. m_bIndexed = FALSE;
  24. //}}AFX_DATA_INIT
  25. }
  26. void CVarSetEditDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28. CDialog::DoDataExchange(pDX);
  29. //{{AFX_DATA_MAP(CVarSetEditDlg)
  30. DDX_Control(pDX, IDC_LIST, m_List);
  31. DDX_Check(pDX, IDC_CASE_SENSITIVE, m_bCaseSensitive);
  32. DDX_Text(pDX, IDC_FILENAME, m_Filename);
  33. DDX_Text(pDX, IDC_KEY, m_Key);
  34. DDX_Text(pDX, IDC_VALUE, m_Value);
  35. DDX_Check(pDX, IDC_INDEXED, m_bIndexed);
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(CVarSetEditDlg, CDialog)
  39. //{{AFX_MSG_MAP(CVarSetEditDlg)
  40. ON_BN_CLICKED(IDC_CASE_SENSITIVE, OnCaseSensitive)
  41. ON_BN_CLICKED(IDC_CLEAR, OnClear)
  42. ON_BN_CLICKED(IDC_DUMP, OnDump)
  43. ON_BN_CLICKED(IDC_ENUM, OnEnum)
  44. ON_BN_CLICKED(IDC_GET_COUNT, OnGetCount)
  45. ON_BN_CLICKED(IDC_GETVALUE, OnGetvalue)
  46. ON_BN_CLICKED(IDC_INDEXED, OnIndexed)
  47. ON_BN_CLICKED(IDC_LOAD, OnLoad)
  48. ON_BN_CLICKED(IDC_SAVE, OnSave)
  49. ON_BN_CLICKED(IDC_SETVALUE, OnSetvalue)
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CVarSetEditDlg message handlers
  54. void CVarSetEditDlg::OnCaseSensitive()
  55. {
  56. CWaitCursor w;
  57. UpdateData(TRUE);
  58. if ( m_varset )
  59. {
  60. m_varset->CaseSensitive = m_bCaseSensitive;
  61. }
  62. }
  63. void CVarSetEditDlg::OnClear()
  64. {
  65. CWaitCursor w;
  66. UpdateData(TRUE);
  67. HRESULT hr;
  68. IVarSet * pVS = NULL;
  69. if ( m_varset )
  70. {
  71. hr = m_varset->raw_getReference(m_Key.AllocSysString(),&pVS);
  72. if ( SUCCEEDED(hr) )
  73. {
  74. pVS->Clear();
  75. pVS->Release();
  76. }
  77. else
  78. {
  79. MessageBox(L"ERROR!");
  80. }
  81. }
  82. UpdateData(FALSE);
  83. }
  84. void CVarSetEditDlg::OnDump()
  85. {
  86. CWaitCursor w;
  87. UpdateData(TRUE);
  88. if ( m_varset )
  89. {
  90. m_varset->DumpToFile(m_Filename.AllocSysString());
  91. MessageBox(L"Finished!");
  92. }
  93. }
  94. void CVarSetEditDlg::OnEnum()
  95. {
  96. DoEnum(m_varset);
  97. }
  98. void CVarSetEditDlg::DoEnum(IVarSet * vs)
  99. {
  100. CWaitCursor w;
  101. _variant_t value;
  102. CString key;
  103. CString val;
  104. CString result;
  105. IEnumVARIANT * henum = NULL;
  106. HRESULT hr = 0;
  107. ULONG nGot;
  108. _bstr_t keyB;
  109. if ( vs )
  110. {
  111. m_List.ResetContent();
  112. // This exercises the method used by VB's For Each.
  113. // Get an IEnumVARIANT interface
  114. hr = vs->get__NewEnum((LPUNKNOWN*)&henum);
  115. // use the IEnumVARIANT interface to get the values
  116. // for simplicity, retrieve them one at a time
  117. if ( SUCCEEDED(hr) )
  118. {
  119. while ( SUCCEEDED(hr = henum->Next(1,&value,&nGot)) )
  120. {
  121. if ( nGot==1 )
  122. {
  123. key = value.bstrVal;
  124. keyB = key;
  125. value = vs->get(keyB);
  126. if ( value.vt == VT_BSTR )
  127. {
  128. val = value.bstrVal;
  129. result.Format(L"%s : %s",key,val);
  130. }
  131. else if ( value.vt == VT_I4 )
  132. {
  133. result.Format(L"%s : %ld",key,value.lVal);
  134. }
  135. else if ( value.vt == VT_EMPTY )
  136. {
  137. result.Format(L"%s : <Empty>",key);
  138. }
  139. else
  140. {
  141. result.Format(L"%s : vt=0x%lx",key,value.vt);
  142. }
  143. m_List.AddString(result);
  144. }
  145. else
  146. {
  147. break;
  148. }
  149. }
  150. if ( henum )
  151. henum->Release();
  152. }
  153. henum = NULL;
  154. }
  155. if (FAILED(hr) )
  156. {
  157. CString errMsg;
  158. errMsg.Format(L"Error: hr=%lx",hr);
  159. MessageBox(errMsg);
  160. }
  161. }
  162. void CVarSetEditDlg::OnGetCount()
  163. {
  164. CWaitCursor w;
  165. CString msg;
  166. // This is the total number of items in the VarSet.
  167. if ( m_varset )
  168. {
  169. msg.Format(L"%ld items.",m_varset->Count);
  170. MessageBox(msg);
  171. }
  172. }
  173. void CVarSetEditDlg::OnGetvalue()
  174. {
  175. CWaitCursor w;
  176. UpdateData(TRUE);
  177. _variant_t value;
  178. _bstr_t key;
  179. // This retrieves a value from the VarSet
  180. // If the value does not exist, m_varset->get will return a variant of type VT_EMPTY.
  181. if ( m_varset )
  182. {
  183. key = m_Key;
  184. value = m_varset->get(key);
  185. if ( value.vt == VT_BSTR )
  186. {
  187. m_Value = (WCHAR *)(_bstr_t)value;
  188. }
  189. else if ( value.vt == VT_I4 )
  190. {
  191. m_Value.Format(L"%ld",value.lVal);
  192. }
  193. else
  194. {
  195. m_Value.Format(L"Variant: Type=%ld",value.vt);
  196. }
  197. }
  198. UpdateData(FALSE);
  199. }
  200. void CVarSetEditDlg::OnIndexed()
  201. {
  202. CWaitCursor w;
  203. UpdateData(TRUE);
  204. // Turning indexing off is always fast, but turning indexing on
  205. // may be very slow if the VarSet is large.
  206. // It takes O(n lg n) to build the index.
  207. if ( m_varset )
  208. {
  209. m_varset->Indexed = m_bIndexed;
  210. }
  211. }
  212. void CVarSetEditDlg::OnLoad()
  213. {
  214. CWaitCursor w;
  215. IPersistStoragePtr ps = NULL;
  216. HRESULT hr = 0;
  217. IStoragePtr store = NULL;
  218. IVarSetPtr vs = NULL;
  219. IOleClientSite * site = NULL;
  220. UpdateData(TRUE);
  221. if ( m_varset )
  222. {
  223. hr = m_varset->QueryInterface(IID_IPersistStorage,(void**)&ps);
  224. if ( SUCCEEDED(hr) )
  225. {
  226. hr = StgOpenStorage(m_Filename.GetBuffer(0),NULL,STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE,NULL,0,&store);
  227. if ( SUCCEEDED(hr) )
  228. {
  229. // Load the data into a new varset
  230. hr = OleLoad(store,IID_IUnknown,site,(void **)&vs);
  231. if ( SUCCEEDED(hr) )
  232. {
  233. // release the old varset
  234. vs->AddRef();
  235. if ( m_varset ) m_varset->Release();
  236. m_varset = vs;
  237. // reload the property values
  238. m_bCaseSensitive = m_varset->CaseSensitive;
  239. m_bIndexed = m_varset->Indexed;
  240. UpdateData(FALSE);
  241. }
  242. }
  243. }
  244. if ( FAILED(hr) )
  245. MessageBox(L"Failed");
  246. else
  247. MessageBox(L"Finished!");
  248. }
  249. }
  250. void CVarSetEditDlg::OnSave()
  251. {
  252. CWaitCursor w;
  253. IPersistStoragePtr ps = NULL;
  254. HRESULT hr = 0;
  255. IStoragePtr store = NULL;
  256. UpdateData(TRUE);
  257. // Save the varset to a file
  258. if ( m_varset )
  259. {
  260. hr = m_varset->QueryInterface(IID_IPersistStorage,(void**)&ps);
  261. if ( SUCCEEDED(hr) )
  262. {
  263. hr = StgCreateDocfile(m_Filename.GetBuffer(0),STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE |STGM_FAILIFTHERE,0,&store);
  264. if ( SUCCEEDED(hr) )
  265. {
  266. hr = OleSave(ps,store,FALSE);
  267. }
  268. }
  269. if ( FAILED(hr) )
  270. MessageBox(L"Failed");
  271. else
  272. MessageBox(L"Finished!");
  273. }
  274. }
  275. void CVarSetEditDlg::OnSetvalue()
  276. {
  277. CWaitCursor w;
  278. UpdateData(TRUE);
  279. _variant_t value;
  280. _bstr_t key;
  281. HRESULT hr = 0;
  282. CString myKey;
  283. CString myValue;
  284. if ( m_varset )
  285. {
  286. // add a single item to the VarSet
  287. // Keys are represented as BSTRs, and values are represented as VARIANTs
  288. key = m_Key;
  289. value = m_Value;
  290. hr = m_varset->put(key,value);
  291. if ( FAILED(hr) )
  292. {
  293. MessageBox(L"Failed");
  294. }
  295. else
  296. {
  297. m_Value.Empty();
  298. }
  299. }
  300. GetDlgItem(IDC_KEY)->SetFocus();
  301. OnEnum();
  302. UpdateData(FALSE); }
  303. void CVarSetEditDlg::OnOK()
  304. {
  305. CDialog::OnOK();
  306. }
  307. BOOL CVarSetEditDlg::OnInitDialog()
  308. {
  309. CDialog::OnInitDialog();
  310. if ( ! m_varset )
  311. {
  312. HRESULT hr = CoCreateInstance(CLSID_VarSet,NULL,CLSCTX_ALL,IID_IVarSet,(void**)&m_varset);
  313. if ( FAILED(hr) )
  314. {
  315. CString msg;
  316. msg.Format(L"Failed to create varset. CoCreateInstance returned %lx",hr);
  317. MessageBox(msg);
  318. }
  319. }
  320. return TRUE; // return TRUE unless you set the focus to a control
  321. // EXCEPTION: OCX Property Pages should return FALSE
  322. }