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.

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