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.

460 lines
12 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // FILE : dlgutils.cpp //
  3. // //
  4. // DESCRIPTION : dialog utility funcs //
  5. // //
  6. // AUTHOR : yossg //
  7. // //
  8. // HISTORY : //
  9. // Dec 30 1999 yossg Welcome to Fax Server. //
  10. // Aug 10 2000 yossg Add TimeFormat functions //
  11. // //
  12. // Copyright (C) 1998 - 2000 Microsoft Corporation All Rights Reserved //
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "dlgutils.h"
  16. HRESULT
  17. ConsoleMsgBox(
  18. IConsole * pConsole,
  19. int ids,
  20. LPTSTR lptstrTitle,
  21. UINT fuStyle,
  22. int *piRetval,
  23. BOOL StringFromCommonDll)
  24. {
  25. UNREFERENCED_PARAMETER(StringFromCommonDll);
  26. HRESULT hr;
  27. int dummy, rc;
  28. WCHAR szText[256];
  29. int *pres = (piRetval)? piRetval: &dummy;
  30. ATLASSERT(pConsole);
  31. rc = ::LoadString(
  32. _Module.GetResourceInstance(),ids, szText, 256);
  33. if (rc <= 0)
  34. {
  35. return E_FAIL;
  36. }
  37. //
  38. // Display the message box
  39. //
  40. if(IsRTLUILanguage())
  41. {
  42. fuStyle |= MB_RTLREADING | MB_RIGHT;
  43. }
  44. hr = pConsole->MessageBox(szText, lptstrTitle, fuStyle, pres);
  45. return hr;
  46. }
  47. void PageError(int ids, HWND hWnd, HINSTANCE hInst /* = NULL */)
  48. {
  49. WCHAR msg[FXS_MAX_ERROR_MSG_LEN+1], title[FXS_MAX_TITLE_LEN+1];
  50. if (!hInst)
  51. {
  52. hInst = _Module.GetResourceInstance();
  53. }
  54. LoadString(hInst, ids, msg, FXS_MAX_ERROR_MSG_LEN);
  55. LoadString(hInst, IDS_ERROR, title, FXS_MAX_TITLE_LEN);
  56. AlignedMessageBox(hWnd, msg, title, MB_OK|MB_ICONERROR);
  57. }
  58. void PageErrorEx(int idsHeader, int ids, HWND hWnd, HINSTANCE hInst /* = NULL */)
  59. {
  60. WCHAR msg[FXS_MAX_ERROR_MSG_LEN+1];
  61. WCHAR title[FXS_MAX_TITLE_LEN+1];
  62. if (!hInst)
  63. {
  64. hInst = _Module.GetResourceInstance();
  65. }
  66. LoadString(hInst, idsHeader, title, FXS_MAX_TITLE_LEN);
  67. LoadString(hInst, ids, msg, FXS_MAX_ERROR_MSG_LEN);
  68. AlignedMessageBox(hWnd, msg, title, MB_OK|MB_ICONERROR);
  69. }
  70. HRESULT
  71. SetComboBoxItem (CComboBox combo,
  72. DWORD comboBoxIndex,
  73. LPCTSTR lpctstrFieldText,
  74. DWORD dwItemData,
  75. HINSTANCE hInst)
  76. {
  77. DEBUG_FUNCTION_NAME( _T("SetComboBoxItem"));
  78. int iRes;
  79. if (!hInst)
  80. {
  81. hInst = _Module.GetResourceInstance();
  82. }
  83. //
  84. // place the string in the combobox
  85. //
  86. iRes = combo.InsertString (comboBoxIndex, lpctstrFieldText);
  87. if (CB_ERR == iRes)
  88. {
  89. DebugPrintEx(
  90. DEBUG_ERR,
  91. _T("failed to insert string '%s' to combobox at index %d"),
  92. lpctstrFieldText,
  93. comboBoxIndex);
  94. goto Cleanup;
  95. }
  96. //
  97. // attach to the combobox item its index (usually, its his enumerated type)
  98. //
  99. iRes = combo.SetItemData (comboBoxIndex, dwItemData);
  100. if (CB_ERR == iRes)
  101. {
  102. DebugPrintEx(
  103. DEBUG_ERR,
  104. _T("SetItemData failed when setting items %s data to the value of %d"),
  105. lpctstrFieldText,
  106. dwItemData);
  107. goto Cleanup;
  108. }
  109. Cleanup:
  110. return (CB_ERR == iRes) ? E_FAIL : S_OK;
  111. }
  112. HRESULT
  113. AddComboBoxItem (CComboBox combo,
  114. LPCTSTR lpctstrFieldText,
  115. DWORD dwItemData,
  116. HINSTANCE hInst)
  117. {
  118. DEBUG_FUNCTION_NAME( _T("SetComboBoxItem"));
  119. int iRes;
  120. int iIndex;
  121. if (!hInst)
  122. {
  123. hInst = _Module.GetResourceInstance();
  124. }
  125. //
  126. // place the string in the combobox
  127. //
  128. iIndex = combo.AddString(lpctstrFieldText);
  129. if (iIndex == CB_ERR)
  130. {
  131. DebugPrintEx(
  132. DEBUG_ERR,
  133. _T("failed to insert string '%s' to combobox "),
  134. lpctstrFieldText);
  135. return E_FAIL;
  136. }
  137. //
  138. // attach to the combobox item its index (usually, its his enumerated type)
  139. //
  140. iRes = combo.SetItemData (iIndex, dwItemData);
  141. if (CB_ERR == iRes)
  142. {
  143. DebugPrintEx(
  144. DEBUG_ERR,
  145. _T("SetItemData failed when setting items %s data to the value of %d"),
  146. lpctstrFieldText,
  147. dwItemData);
  148. return E_FAIL;
  149. }
  150. return S_OK;
  151. }
  152. HRESULT
  153. SelectComboBoxItemData (CComboBox combo, DWORD_PTR dwItemData)
  154. {
  155. HRESULT hRc = S_OK;
  156. int NumItems;
  157. int i;
  158. int selectedItem;
  159. DWORD_PTR currItemData;
  160. DEBUG_FUNCTION_NAME( _T("SelectComboBoxItemData"));
  161. //
  162. // scan the items in the combobox and find the item with the specific data
  163. //
  164. i = 0;
  165. NumItems = combo.GetCount ();
  166. for (i = 0; i < NumItems; i++)
  167. {
  168. currItemData = combo.GetItemData (i);
  169. ATLASSERT (currItemData != CB_ERR);// Cant get the data of item %d of combobox, i
  170. if (currItemData == dwItemData)
  171. {
  172. //
  173. // select it
  174. //
  175. selectedItem = combo.SetCurSel (i);
  176. ATLASSERT (selectedItem != CB_ERR); //Cant select item %d of combobox, i
  177. DebugPrintEx(
  178. DEBUG_MSG,
  179. _T("Selected item %d (with data %d) of combobox"), i, dwItemData);
  180. goto Cleanup;
  181. }
  182. }
  183. Cleanup:
  184. return hRc;
  185. }
  186. DWORD
  187. WinContextHelp(
  188. ULONG_PTR dwHelpId,
  189. HWND hWnd
  190. )
  191. /*++
  192. Routine name : WinContextHelp
  193. Routine description:
  194. Open context sensetive help popup 'tooltip' with WinHelp
  195. Arguments:
  196. dwHelpId [in] - help ID
  197. hWnd [in] - parent window handler
  198. Return Value:
  199. None.
  200. --*/
  201. {
  202. DWORD dwRes = ERROR_SUCCESS;
  203. if (0 == dwHelpId)
  204. {
  205. return dwRes;
  206. }
  207. if(!IsFaxComponentInstalled(FAX_COMPONENT_HELP_ADMIN_HLP))
  208. {
  209. //
  210. // The help file is not installed
  211. //
  212. return dwRes;
  213. }
  214. WinHelp(hWnd,
  215. FXS_ADMIN_HLP_FILE,
  216. HELP_CONTEXTPOPUP,
  217. dwHelpId);
  218. return dwRes;
  219. }
  220. HRESULT
  221. DisplayContextHelp(
  222. IDisplayHelp* pDisplayHelp,
  223. LPOLESTR helpFile,
  224. WCHAR* szTopic
  225. )
  226. /*++
  227. Routine name : WinContextHelp
  228. Routine description:
  229. Display context sensetive help
  230. Arguments:
  231. pDisplayHelp [in] - IDisplayHelp interface
  232. helpFile [in] - help file name
  233. szTopic [in] - help topic name
  234. Return Value:
  235. None.
  236. --*/
  237. {
  238. if(!pDisplayHelp || !helpFile || !szTopic)
  239. {
  240. return E_FAIL;
  241. }
  242. WCHAR szTopicName[MAX_PATH] = {0};
  243. _snwprintf(szTopicName, ARR_SIZE(szTopicName)-1, L"%s%s", helpFile, szTopic);
  244. LPOLESTR pszTopic = static_cast<LPOLESTR>(CoTaskMemAlloc((wcslen(szTopicName) + 1) * sizeof(_TCHAR)));
  245. if (pszTopic)
  246. {
  247. _tcscpy(pszTopic, szTopicName);
  248. return pDisplayHelp->ShowTopic(pszTopic);
  249. }
  250. else
  251. {
  252. return E_OUTOFMEMORY;
  253. }
  254. }
  255. HRESULT
  256. InvokePropSheet(
  257. CSnapInItem* pNode,
  258. DATA_OBJECT_TYPES type,
  259. LPUNKNOWN lpUnknown,
  260. LPCWSTR szTitle,
  261. DWORD dwPage)
  262. /*++
  263. Routine name : InvokePropSheet
  264. Routine description:
  265. Invoke MMC property sheet
  266. Taken from the MSDN "Using IPropertySheetProvider Directly"
  267. Arguments:
  268. pNode [in] - Snapin node that should open the sheet
  269. type [in] - Node type [CCT_SCOPE, CCT_RESULT, CCT_SNAPIN_MANAGER, CCT_UNINITIALIZED]
  270. lpUnknown [in] - Pointer to an IComponent or IComponentData
  271. szTitle [in] - Pointer to a null-terminated string that contains the title of the property page.
  272. dwPage [in] - Specifies which page on the property sheet is shown. It is zero-indexed.
  273. Return Value:
  274. OLE error code
  275. --*/
  276. {
  277. DEBUG_FUNCTION_NAME( _T("InvokePropSheet"));
  278. HRESULT hr = E_FAIL;
  279. if(!pNode || !szTitle || !lpUnknown)
  280. {
  281. ATLASSERT(FALSE);
  282. return hr;
  283. }
  284. MMC_COOKIE cookie = (MMC_COOKIE)pNode;
  285. //
  286. // Get node data object
  287. //
  288. IDataObject* pDataObject = NULL;
  289. hr = pNode->GetDataObject(&pDataObject, type);
  290. if (FAILED(hr))
  291. {
  292. DebugPrintEx(DEBUG_ERR, TEXT("CSnapinNode::GetDataObject() failed with %ld"), hr);
  293. return hr;
  294. }
  295. //
  296. // CoCreate an instance of the MMC Node Manager to obtain
  297. // an IPropertySheetProvider interface pointer
  298. //
  299. IPropertySheetProvider* pPropertySheetProvider = NULL;
  300. hr = CoCreateInstance (CLSID_NodeManager,
  301. NULL,
  302. CLSCTX_INPROC_SERVER,
  303. IID_IPropertySheetProvider,
  304. (void **)&pPropertySheetProvider);
  305. if (FAILED(hr))
  306. {
  307. DebugPrintEx(DEBUG_ERR, TEXT("CoCreateInstance(CLSID_NodeManager) failed with %ld"), hr);
  308. goto exit;
  309. }
  310. hr = pPropertySheetProvider->FindPropertySheet(cookie, NULL, pDataObject);
  311. //
  312. // S_OK - The property sheet was successfully located and was brought to the foreground.
  313. // S_FALSE - A property sheet with this cookie was not found.
  314. //
  315. if(S_OK == hr)
  316. {
  317. //
  318. // The page already opened
  319. //
  320. goto exit;
  321. }
  322. //
  323. // Create the property sheet
  324. //
  325. hr = pPropertySheetProvider->CreatePropertySheet(szTitle, // pointer to the property page title
  326. TRUE, // property sheet
  327. cookie, // cookie of current object - can be NULL
  328. // for extension snap-ins
  329. pDataObject, // data object of selected node
  330. NULL); // specifies flags set by the method call
  331. if (FAILED(hr))
  332. {
  333. DebugPrintEx(DEBUG_ERR, TEXT("IPropertySheetProvider::CreatePropertySheet() failed with %ld"), hr);
  334. goto exit;
  335. }
  336. //
  337. // Call AddPrimaryPages. MMC will then call the
  338. // IExtendPropertySheet methods of our
  339. // property sheet extension object
  340. //
  341. hr = pPropertySheetProvider->AddPrimaryPages(lpUnknown, // pointer to our object's IUnknown
  342. TRUE, // specifies whether to create a notification handle
  343. NULL, // must be NULL
  344. FALSE); // TRUE for scope pane; FALSE for result pane
  345. if (FAILED(hr))
  346. {
  347. DebugPrintEx(DEBUG_ERR, TEXT("IPropertySheetProvider::AddPrimaryPages() failed with %ld"), hr);
  348. goto exit;
  349. }
  350. //
  351. // Allow property page extensions to add
  352. // their own pages to the property sheet
  353. //
  354. hr = pPropertySheetProvider->AddExtensionPages();
  355. if (FAILED(hr))
  356. {
  357. DebugPrintEx(DEBUG_ERR, TEXT("IPropertySheetProvider::AddExtensionPages() failed with %ld"), hr);
  358. goto exit;
  359. }
  360. //
  361. // Display property sheet
  362. //
  363. hr = pPropertySheetProvider->Show(NULL, dwPage); // NULL is allowed for modeless prop sheet
  364. if (FAILED(hr))
  365. {
  366. DebugPrintEx(DEBUG_ERR, TEXT("IPropertySheetProvider::Show() failed with %ld"), hr);
  367. goto exit;
  368. }
  369. //
  370. // Release IPropertySheetProvider interface
  371. //
  372. exit:
  373. if(pPropertySheetProvider)
  374. {
  375. pPropertySheetProvider->Release();
  376. }
  377. if(pDataObject)
  378. {
  379. pDataObject->Release();
  380. }
  381. return hr;
  382. } // InvokePropSheet