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.

463 lines
11 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. WBEMDLG.CPP
  5. Abstract:
  6. History:
  7. --*/
  8. #include "precomp.h"
  9. #include <wbemdlg.h>
  10. #include <stdio.h>
  11. CBasicWbemDialog::CBasicWbemDialog(HWND hParent)
  12. {
  13. m_lRefCount = 0;
  14. m_hParent = hParent;
  15. m_hDlg = NULL;
  16. m_bDeleteOnClose = FALSE;
  17. m_pOwner = NULL;
  18. }
  19. CBasicWbemDialog::~CBasicWbemDialog()
  20. {
  21. if(m_pOwner)
  22. m_pOwner->Release();
  23. }
  24. INT_PTR CALLBACK CBasicWbemDialog::staticDlgProc(
  25. HWND hDlg,
  26. UINT uMsg,
  27. WPARAM wParam,
  28. LPARAM lParam
  29. )
  30. {
  31. CBasicWbemDialog* pThis;
  32. if(uMsg == WM_INITDIALOG)
  33. {
  34. SetWindowLongPtr(hDlg, DWLP_USER, lParam);
  35. pThis = (CBasicWbemDialog*)lParam;
  36. pThis->m_hDlg = hDlg;
  37. ShowWindow(hDlg, SW_HIDE);
  38. }
  39. else
  40. {
  41. pThis = (CBasicWbemDialog*)GetWindowLongPtr(hDlg, DWLP_USER);
  42. }
  43. if(pThis)
  44. {
  45. return pThis->DlgProc(hDlg, uMsg, wParam, lParam);
  46. }
  47. else return FALSE;
  48. }
  49. BOOL CBasicWbemDialog::DlgProc(
  50. HWND hDlg,
  51. UINT uMsg,
  52. WPARAM wParam,
  53. LPARAM lParam
  54. )
  55. {
  56. switch(uMsg)
  57. {
  58. case WM_INITDIALOG:
  59. {
  60. CenterOnParent();
  61. return OnInitDialog();
  62. }
  63. case WM_COMMAND:
  64. if(LOWORD(wParam) == IDOK)
  65. return OnOK();
  66. else if(LOWORD(wParam) == IDCANCEL)
  67. return OnCancel();
  68. else if(HIWORD(wParam) == LBN_DBLCLK)
  69. return OnDoubleClick(LOWORD(wParam));
  70. else if(HIWORD(wParam) == LBN_SELCHANGE || HIWORD(wParam)==CBN_SELCHANGE)
  71. return OnSelChange(LOWORD(wParam));
  72. else
  73. return OnCommand(HIWORD(wParam), LOWORD(wParam));
  74. case WM_ACTIVATE:
  75. if(!m_bModal)
  76. {
  77. if(LOWORD(wParam) != WA_INACTIVE)
  78. {
  79. ms_hCurrentModeless = m_hDlg;
  80. }
  81. else
  82. {
  83. ms_hCurrentModeless = NULL;
  84. }
  85. }
  86. return TRUE;
  87. case WM_CLOSE:
  88. EndDialog(IDCANCEL);
  89. DestroyWindow(hDlg);
  90. return TRUE;
  91. case WM_NCDESTROY:
  92. if(m_bDeleteOnClose)
  93. {
  94. delete this;
  95. }
  96. return TRUE;
  97. case WM_APP: // safe private message range (doesn't overlap with dialog controls)
  98. return OnUser(wParam, lParam);
  99. }
  100. return FALSE;
  101. }
  102. BOOL CBasicWbemDialog::OnOK()
  103. {
  104. if(Verify())
  105. {
  106. EndDialog(IDOK);
  107. return TRUE;
  108. }
  109. else return FALSE;
  110. }
  111. BOOL CBasicWbemDialog::OnCancel()
  112. {
  113. EndDialog(IDCANCEL);
  114. return TRUE;
  115. }
  116. BOOL CBasicWbemDialog::EndDialog(int nResult)
  117. {
  118. if(m_bModal)
  119. {
  120. // PostMessage(m_hDlg, WM_DESTROY, 0, (LPARAM)nResult);
  121. // return TRUE;
  122. return ::EndDialog(m_hDlg, nResult);
  123. }
  124. else
  125. {
  126. DestroyWindow(m_hDlg);
  127. return TRUE;
  128. }
  129. }
  130. WORD CBasicWbemDialog::GetCheck(int nID)
  131. {
  132. return (WORD)SendMessage(GetDlgItem(nID), BM_GETCHECK, 0, 0);
  133. }
  134. void CBasicWbemDialog::SetCheck(int nID, WORD wCheck)
  135. {
  136. SendMessage(GetDlgItem(nID), BM_SETCHECK, wCheck, 0);
  137. }
  138. UINT CBasicWbemDialog::GetDlgItemTextX(
  139. int nDlgItem,
  140. LPWSTR pStr,
  141. int nMaxCount
  142. )
  143. ///////////////////////////////////////////////////////////////////
  144. //
  145. // Starts a loop which creates a buffer of an initial conservative
  146. // size and attempts to retrieve the dialog text using the buffer.
  147. // If the buffer overflows, iterate the loop, deleting the current
  148. // buffer, and recreating it incrementally larger. Continue until
  149. // full text is retrieved. If pStr is NULL, then the inner buffer
  150. // is not copied.
  151. //
  152. // PARAMETERS: The dialog item ID, a buffer pointer to receive
  153. // the string, the size of the buffer
  154. //
  155. // RETURNS: The size of the total amount of text in the dialog
  156. // item, or 0 on failure/empty buffer. It is up
  157. // to the caller to compare the buffer size to the
  158. // return value to determine of all text was recieved
  159. //
  160. ///////////////////////////////////////////////////////////////////
  161. {
  162. size_t uLen = 0; // Size of the internal buffer
  163. char *pTmpStr = 0; // The internal buffer
  164. UINT uRes; // The return value
  165. do {
  166. // Delete the previous buffer
  167. if (pTmpStr)
  168. delete [] pTmpStr;
  169. // Increase the size of the buffer
  170. uLen += 2048;
  171. pTmpStr = new char[(uLen+1)*2];
  172. if (0 == pTmpStr)
  173. return 0;
  174. // Get the text
  175. uRes = GetDlgItemTextA(m_hDlg, nDlgItem, pTmpStr, uLen);
  176. // Verify the text
  177. if (uRes == 0 || _mbstrlen(pTmpStr) == 0)
  178. return 0;
  179. // If the buffer is smaller than the text,
  180. // then continue to expand the buffer
  181. } while(uRes >= (uLen - 1));
  182. if (NULL != pStr)
  183. mbstowcs(pStr, pTmpStr, nMaxCount);
  184. delete [] pTmpStr;
  185. // Return the size of the text in the dlg box - regardless of
  186. // whether it was copied or not.
  187. return uRes;
  188. }
  189. BOOL CBasicWbemDialog::SetDlgItemTextX(int ID, WCHAR * pwc)
  190. {
  191. int iLen = 2*(wcslen(pwc))+1;
  192. char * pTemp = new char[iLen];
  193. if(pTemp == NULL)
  194. return FALSE;
  195. wcstombs(pTemp, pwc, iLen);
  196. SetDlgItemText(ID, pTemp);
  197. delete [] pTemp;
  198. return TRUE;
  199. }
  200. void CBasicWbemDialog::AddStringToCombo(int nID, LPSTR szString, DWORD dwItemData)
  201. {
  202. HWND hControl=GetDlgItem(nID);
  203. if (hControl!=NULL)
  204. {
  205. int pos=(int)SendMessage(GetDlgItem(nID), CB_ADDSTRING, 0, LPARAM(szString));
  206. if (dwItemData!=CB_ERR && pos!=CB_ERR)
  207. {
  208. SendMessage (hControl, CB_SETITEMDATA, pos, dwItemData);
  209. }
  210. }
  211. }
  212. void CBasicWbemDialog::SetComboSelection (int nID, DWORD dwItemData)
  213. {
  214. HWND hControl=GetDlgItem(nID);
  215. int count=(int)SendMessage (hControl, CB_GETCOUNT, 0, 0L);
  216. for (int pos=0; pos<count; pos++)
  217. {
  218. DWORD dwTestData=(DWORD)SendMessage (hControl, CB_GETITEMDATA, pos, 0L);
  219. if (dwTestData==dwItemData)
  220. {
  221. SendMessage (hControl, CB_SETCURSEL, pos, 0L);
  222. }
  223. }
  224. }
  225. void CBasicWbemDialog::AddStringToList(int nID, LPSTR szString)
  226. {
  227. SendMessage(GetDlgItem(nID), LB_ADDSTRING, 0, LPARAM(szString));
  228. }
  229. void CBasicWbemDialog::CenterOnParent()
  230. {
  231. RECT rParent;
  232. if(m_hParent == NULL)
  233. {
  234. GetWindowRect(GetDesktopWindow(), &rParent);
  235. }
  236. else
  237. {
  238. GetWindowRect(m_hParent, &rParent);
  239. }
  240. RECT rUs;
  241. GetWindowRect(m_hDlg, &rUs);
  242. int nHeight = rUs.bottom - rUs.top;
  243. int nWidth = rUs.right - rUs.left;
  244. int nX = ((rParent.right - rParent.left) - nWidth) / 2;
  245. /// if(nX < 0) nX = 0;
  246. int nY = ((rParent.bottom - rParent.top) - nHeight) / 2;
  247. /// if(nY < 0) nY = 0;
  248. if ( NULL != m_hParent )
  249. {
  250. RECT rDesktop;
  251. HWND hProgMan = FindWindowEx( NULL, NULL, "Progman", "Program Manager" );
  252. HWND hShell = FindWindowEx( hProgMan, NULL, "SHELLDLL_DefView", NULL );
  253. if ( ( NULL != hProgMan ) && ( NULL != hShell ) )
  254. {
  255. if ( GetClientRect( hShell, &rDesktop ) )
  256. {
  257. if ( ( nHeight < ( rDesktop.bottom ) ) &&
  258. ( ( rParent.top + nY + nHeight ) > rDesktop.bottom ) )
  259. {
  260. nY = ( rDesktop.bottom - nHeight ) - rParent.top;
  261. }
  262. if ( ( nWidth < ( rDesktop.right ) ) &&
  263. ( ( rParent.left + nX + nWidth ) > rDesktop.right ) )
  264. {
  265. nX = ( rDesktop.right - nWidth ) - rParent.left;
  266. }
  267. }
  268. }
  269. }
  270. MoveWindow(m_hDlg, rParent.left + nX, rParent.top + nY,
  271. nWidth, nHeight, TRUE);
  272. }
  273. void CBasicWbemDialog::SetOwner(CRefCountable* pOwner)
  274. {
  275. if(m_pOwner)
  276. m_pOwner->Release();
  277. m_pOwner = pOwner;
  278. if(m_pOwner)
  279. m_pOwner->AddRef();
  280. }
  281. LRESULT CBasicWbemDialog::GetLBCurSel(int nID)
  282. {
  283. return SendMessage(GetDlgItem(nID), LB_GETCURSEL, 0, 0);
  284. }
  285. LPSTR CBasicWbemDialog::GetLBCurSelString(int nID)
  286. {
  287. LRESULT nIndex = SendMessage(GetDlgItem(nID), LB_GETCURSEL, 0, 0);
  288. if(nIndex == LB_ERR)
  289. return NULL;
  290. LRESULT nLength = SendMessage(GetDlgItem(nID), LB_GETTEXTLEN,
  291. (WPARAM)nIndex, 0);
  292. char* sz = new char[nLength+3];
  293. SendMessage(GetDlgItem(nID), LB_GETTEXT, (WPARAM)nIndex, (LPARAM)sz);
  294. return sz;
  295. }
  296. LRESULT CBasicWbemDialog::GetCBCurSel(int nID)
  297. {
  298. return SendMessage(GetDlgItem(nID), CB_GETCURSEL, 0, 0);
  299. }
  300. LPSTR CBasicWbemDialog::GetCBCurSelString(int nID)
  301. {
  302. LRESULT nIndex = SendMessage(GetDlgItem(nID), CB_GETCURSEL, 0, 0);
  303. if(nIndex == CB_ERR)
  304. return NULL;
  305. LRESULT nLength = SendMessage(GetDlgItem(nID), CB_GETLBTEXTLEN,
  306. (WPARAM)nIndex, 0);
  307. char* sz = new char[nLength+3];
  308. SendMessage(GetDlgItem(nID), CB_GETLBTEXT, (WPARAM)nIndex, (LPARAM)sz);
  309. return sz;
  310. }
  311. HWND CBasicWbemDialog::ms_hCurrentModeless = NULL;
  312. BOOL CBasicWbemDialog::IsDialogMessage(MSG* pMsg)
  313. {
  314. if(ms_hCurrentModeless != NULL && IsWindow(ms_hCurrentModeless))
  315. return ::IsDialogMessage(ms_hCurrentModeless, pMsg);
  316. else
  317. return FALSE;
  318. }
  319. LRESULT CBasicWbemDialog::MessageLoop()
  320. {
  321. MSG msg;
  322. while (GetMessage(&msg, (HWND) NULL, 0, 0))
  323. {
  324. if(msg.message == WM_DESTROY && msg.hwnd == m_hDlg)
  325. {
  326. DispatchMessage(&msg);
  327. return msg.lParam;
  328. }
  329. if(CBasicWbemDialog::IsDialogMessage(&msg))
  330. continue;
  331. TranslateMessage(&msg);
  332. DispatchMessage(&msg);
  333. }
  334. return FALSE;
  335. }
  336. BOOL CBasicWbemDialog::PostUserMessage(HWND hWnd, WPARAM wParam, LPARAM lParam)
  337. {
  338. // Since user messages don't seem to be queued (even when
  339. // posted) mouse & keyboard input messages aren't getting
  340. // processed. So we will process them here first.
  341. MSG msg;
  342. while (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE))
  343. {
  344. if (CBasicWbemDialog::IsDialogMessage(&msg))
  345. continue;
  346. TranslateMessage(&msg);
  347. DispatchMessage(&msg);
  348. }
  349. BOOL result;
  350. while (result = PostMessage(hWnd, WM_APP, wParam, lParam) == 0 && IsWindow(hWnd))
  351. { SwitchToThread();};
  352. return result;
  353. }
  354. int CBasicWbemDialog::MessageBox(UINT uTextId, UINT uCaptionId, UINT uType)
  355. {
  356. return MessageBox(m_hDlg, uTextId, uCaptionId, uType);
  357. }
  358. int CBasicWbemDialog::MessageBox(HWND hDlg, UINT uTextId, UINT uCaptionId, UINT uType)
  359. {
  360. char szText[2048];
  361. if(LoadString(GetModuleHandle(NULL), uTextId, szText, 2048) <= 0)
  362. {
  363. strcpy(szText, "Unable to load resource string");
  364. }
  365. char szCaption[2048];
  366. if(LoadString(GetModuleHandle(NULL), uCaptionId, szCaption, 2048) <= 0)
  367. {
  368. strcpy(szCaption, "Unable to load resource string");
  369. }
  370. return ::MessageBox(hDlg, szText, szCaption, uType);
  371. }
  372. BOOL CBasicWbemDialog::SetDlgItemText(int nID, UINT uTextId)
  373. {
  374. char szText[2048];
  375. if(LoadString(GetModuleHandle(NULL), uTextId, szText, 2048) <= 0)
  376. {
  377. strcpy(szText, "Unable to load resource string");
  378. }
  379. return SetDlgItemText(nID, szText);
  380. }
  381. BOOL CALLBACK EnableWindowsProc(HWND hWnd, LPARAM lParam)
  382. {
  383. DWORD dwPid;
  384. DWORD dwTid = GetWindowThreadProcessId(hWnd, &dwPid);
  385. if(dwPid == GetCurrentProcessId())
  386. {
  387. EnableWindow(hWnd, (BOOL)lParam);
  388. }
  389. return TRUE;
  390. }
  391. void CBasicWbemDialog::EnableAllWindows(BOOL bEnable)
  392. {
  393. EnumWindows((WNDENUMPROC)EnableWindowsProc, (LPARAM)bEnable);
  394. }