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.

422 lines
13 KiB

  1. //
  2. // dlgbase.cpp: base class for dialogs
  3. //
  4. #include "stdafx.h"
  5. #include "dlgbase.h"
  6. #include "resource.h"
  7. #include "wuiids.h"
  8. #define TRC_GROUP TRC_GROUP_UI
  9. #define TRC_FILE "dlgbase"
  10. #include <atrcapi.h>
  11. CDlgBase::CDlgBase(HWND hwndOwner, HINSTANCE hInst, DCINT dlgResId) :
  12. _hwndOwner(hwndOwner), _hInstance(hInst), _dlgResId(dlgResId)
  13. {
  14. _hwndDlg = NULL;
  15. _startupLeft = _startupTop = 0;
  16. }
  17. CDlgBase::~CDlgBase()
  18. {
  19. }
  20. /****************************************************************************/
  21. /* Name: DialogBoxProc */
  22. /* */
  23. /* Purpose: Provides message handling for basic operation */
  24. /* */
  25. /* Returns: TRUE - if message dealt with */
  26. /* FALSE otherwise */
  27. /* */
  28. /* Params: See windows documentation */
  29. /* */
  30. /****************************************************************************/
  31. INT_PTR CALLBACK CDlgBase::DialogBoxProc(HWND hwndDlg,
  32. UINT uMsg,
  33. WPARAM wParam,
  34. LPARAM lParam)
  35. {
  36. INT_PTR rc = FALSE;
  37. DC_BEGIN_FN("DialogBoxProc");
  38. DC_IGNORE_PARAMETER(lParam);
  39. /************************************************************************/
  40. /* Handle dialog messages */
  41. /************************************************************************/
  42. switch(uMsg)
  43. {
  44. case WM_INITDIALOG:
  45. {
  46. SetDialogAppIcon(hwndDlg);
  47. rc = TRUE;
  48. }
  49. break;
  50. case WM_COMMAND:
  51. {
  52. switch(DC_GET_WM_COMMAND_ID(wParam))
  53. {
  54. case IDCANCEL:
  55. {
  56. /********************************************************/
  57. /* Closes the dialog */
  58. /********************************************************/
  59. TRC_NRM((TB, _T("Close dialog")));
  60. if(hwndDlg)
  61. {
  62. EndDialog(hwndDlg, IDCANCEL);
  63. }
  64. rc = TRUE;
  65. }
  66. break;
  67. default:
  68. {
  69. /********************************************************/
  70. /* Do Nothing */
  71. /********************************************************/
  72. }
  73. break;
  74. }
  75. }
  76. break;
  77. case WM_CLOSE:
  78. {
  79. /****************************************************************/
  80. /* Closes the dialog */
  81. /****************************************************************/
  82. TRC_NRM((TB, _T("Close dialog")));
  83. if(IsWindow(hwndDlg))
  84. {
  85. EndDialog(hwndDlg, IDCANCEL);
  86. }
  87. rc = 0;
  88. }
  89. break;
  90. default:
  91. {
  92. /****************************************************************/
  93. /* Do Nothing */
  94. /****************************************************************/
  95. }
  96. break;
  97. }
  98. DC_END_FN();
  99. return(rc);
  100. } /* DialogBoxProc */
  101. /****************************************************************************/
  102. /* Name: SetDialogAppIcon */
  103. /* */
  104. /* Purpose: Sets the icon for the dialog to the application icon */
  105. /* */
  106. /* Returns: Yes, it does */
  107. /* */
  108. /* Params: IN HWND the dialog for which we want to set the icon */
  109. /* */
  110. /* */
  111. /****************************************************************************/
  112. void CDlgBase::SetDialogAppIcon(HWND hwndDlg)
  113. {
  114. #ifdef OS_WINCE
  115. DC_IGNORE_PARAMETER(hwndDlg);
  116. #else // !OS_WINCE
  117. HICON hIcon = NULL;
  118. hIcon = LoadIcon(_hInstance, MAKEINTRESOURCE(UI_IDI_ICON));
  119. if(hIcon)
  120. {
  121. #ifdef OS_WIN32
  122. SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  123. #else //OS_WIN32
  124. SetClassWord(hwndDlg, GCW_HICON, (WORD)hIcon);
  125. #endif //OS_WIN32
  126. }
  127. #endif // OS_WINCE
  128. } /* UISetDialogAppIcon */
  129. /****************************************************************************/
  130. /* Name: EnableDlgItem */
  131. /* */
  132. /* Purpose: Enables or disables a specified dialog control */
  133. /* */
  134. /* Returns: Nothing. */
  135. /* */
  136. /* Params: hwndDlg - dialog window handle */
  137. /* dlgItemId - dialog control id */
  138. /* enabled - TRUE enables the control, FALSE disables it */
  139. /* */
  140. /****************************************************************************/
  141. DCVOID DCINTERNAL CDlgBase::EnableDlgItem( HWND hwndDlg,
  142. DCUINT dlgItemId,
  143. DCBOOL enabled )
  144. {
  145. HWND hwndDlgItem = NULL;
  146. DC_BEGIN_FN("EnableDlgItem");
  147. if(hwndDlg)
  148. {
  149. hwndDlgItem = GetDlgItem(hwndDlg, dlgItemId);
  150. }
  151. if(hwndDlgItem)
  152. {
  153. EnableWindow(hwndDlgItem, enabled);
  154. }
  155. DC_END_FN();
  156. return;
  157. }
  158. /****************************************************************************/
  159. /* Name: CenterWindowOnParent */
  160. /* */
  161. /* Purpose: Center a window inside another */
  162. /* */
  163. /* Returns: None */
  164. /* */
  165. /* Params: HWND hwndCenterOn (window to center on) */
  166. /* xRatio - horizontal centering factor e.g 2 for (1/2) */
  167. /* yRatio - vertical centering factor e.g 3 for (1/3)
  168. /* */
  169. /* */
  170. /****************************************************************************/
  171. VOID CDlgBase::CenterWindow(HWND hwndCenterOn,
  172. INT xRatio,
  173. INT yRatio)
  174. {
  175. RECT childRect;
  176. RECT parentRect;
  177. DCINT xPos;
  178. DCINT yPos;
  179. LONG desktopX = GetSystemMetrics(SM_CXSCREEN);
  180. LONG desktopY = GetSystemMetrics(SM_CYSCREEN);
  181. BOOL center = TRUE;
  182. DC_BEGIN_FN("CenterWindowOnParent");
  183. TRC_ASSERT(_hwndDlg, (TB, _T("_hwndDlg is NULL...was it set in WM_INITDIALOG?\n")));
  184. if (!_hwndDlg)
  185. {
  186. TRC_ALT((TB, _T("Window doesn't exist")));
  187. DC_QUIT;
  188. }
  189. if (!xRatio)
  190. {
  191. xRatio = 2;
  192. }
  193. if (!yRatio)
  194. {
  195. yRatio = 2;
  196. }
  197. #ifndef OS_WINCE
  198. if(!hwndCenterOn)
  199. {
  200. hwndCenterOn = GetDesktopWindow();
  201. }
  202. GetWindowRect(hwndCenterOn, &parentRect);
  203. #else /*OS_WINCE*/
  204. if(!hwndCenterOn)
  205. {
  206. //
  207. // WinCE doesn't have GetDesktopWindow()
  208. //
  209. #if 0
  210. if (g_CEConfig != CE_CONFIG_WBT)
  211. {
  212. SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&parentRect, 0);
  213. }
  214. else
  215. #endif
  216. {
  217. parentRect.left = 0;
  218. parentRect.top = 0;
  219. parentRect.right = desktopX;
  220. parentRect.bottom = desktopY;
  221. }
  222. }
  223. else
  224. {
  225. GetWindowRect(hwndCenterOn, &parentRect);
  226. }
  227. #endif/*OS_WINCE*/
  228. GetWindowRect(_hwndDlg, &childRect);
  229. /************************************************************************/
  230. /* Calculate the top left - centered in the parent window. */
  231. /************************************************************************/
  232. xPos = ( (parentRect.right + parentRect.left) -
  233. (childRect.right - childRect.left)) / xRatio;
  234. yPos = ( (parentRect.bottom + parentRect.top) -
  235. (childRect.bottom - childRect.top)) / yRatio;
  236. /************************************************************************/
  237. /* Constrain to the desktop */
  238. /************************************************************************/
  239. if (xPos < 0)
  240. {
  241. xPos = 0;
  242. }
  243. else if (xPos > (desktopX - (childRect.right - childRect.left)))
  244. {
  245. xPos = desktopX - (childRect.right - childRect.left);
  246. }
  247. if (yPos < 0)
  248. {
  249. yPos = 0;
  250. }
  251. else if (yPos > (desktopY - (childRect.bottom - childRect.top)))
  252. {
  253. yPos = desktopY - (childRect.bottom - childRect.top);
  254. }
  255. TRC_DBG((TB, _T("Set dialog position to %u %u"), xPos, yPos));
  256. SetWindowPos(_hwndDlg,
  257. NULL,
  258. xPos, yPos,
  259. 0, 0,
  260. SWP_NOSIZE | SWP_NOACTIVATE);
  261. DC_EXIT_POINT:
  262. DC_END_FN();
  263. return;
  264. } /* CenterWindowOnParent */
  265. #ifndef OS_WINCE
  266. //
  267. // Retreive current dialog position
  268. //
  269. BOOL CDlgBase::GetPosition(int* pLeft, int* pTop)
  270. {
  271. if(!pLeft || !pTop)
  272. {
  273. return FALSE;
  274. }
  275. if(!_hwndDlg)
  276. {
  277. return FALSE;
  278. }
  279. WINDOWPLACEMENT wndPlc;
  280. wndPlc.length = sizeof(WINDOWPLACEMENT);
  281. if(GetWindowPlacement(_hwndDlg, &wndPlc))
  282. {
  283. *pLeft = wndPlc.rcNormalPosition.left;
  284. *pTop = wndPlc.rcNormalPosition.top;
  285. return TRUE;
  286. }
  287. return FALSE;
  288. }
  289. #endif
  290. BOOL CDlgBase::SetPosition(int left, int top)
  291. {
  292. if(!_hwndDlg)
  293. {
  294. return FALSE;
  295. }
  296. if(!::SetWindowPos(_hwndDlg,
  297. NULL,
  298. left,
  299. top,
  300. 0,
  301. 0,
  302. SWP_NOZORDER | SWP_NOSIZE))
  303. {
  304. return FALSE;
  305. }
  306. return TRUE;
  307. }
  308. //
  309. // Move the dialog controls
  310. //
  311. void CDlgBase::RepositionControls(int moveDeltaX, int moveDeltaY, UINT* ctlIDs, int numID)
  312. {
  313. if(_hwndDlg)
  314. {
  315. for(int i=0; i< numID; i++)
  316. {
  317. HWND hwndCtrl = GetDlgItem(_hwndDlg, ctlIDs[i]);
  318. if( hwndCtrl)
  319. {
  320. RECT rc;
  321. GetWindowRect( hwndCtrl, &rc);
  322. MapWindowPoints( NULL, _hwndDlg, (LPPOINT)&rc, 2);
  323. OffsetRect( &rc, moveDeltaX, moveDeltaY);
  324. SetWindowPos( hwndCtrl, NULL, rc.left, rc.top, 0, 0,
  325. SWP_NOZORDER | SWP_NOSIZE);
  326. }
  327. }
  328. }
  329. }
  330. //
  331. // Shows+enable or Hide+disable controls
  332. //
  333. void CDlgBase::EnableControls(UINT* ctlIDs, int numID, BOOL bEnable)
  334. {
  335. if(_hwndDlg)
  336. {
  337. for(int i=0; i< numID; i++)
  338. {
  339. HWND hwndCtrl = GetDlgItem(_hwndDlg, ctlIDs[i]);
  340. if( hwndCtrl)
  341. {
  342. EnableWindow( hwndCtrl, bEnable);
  343. ShowWindow(hwndCtrl, bEnable ? SW_SHOW : SW_HIDE);
  344. }
  345. }
  346. }
  347. }
  348. //
  349. // DoLockDlgRes - loads and locks a dialog template
  350. // returns address of locked resource
  351. // lpszResName - name of resource
  352. //
  353. DLGTEMPLATE* CDlgBase::DoLockDlgRes(LPCTSTR lpszResName)
  354. {
  355. #ifdef OS_WINCE
  356. HRSRC hrsrc = FindResource(GetModuleHandle(NULL), lpszResName, RT_DIALOG);
  357. #else
  358. HRSRC hrsrc = FindResource(NULL, lpszResName, RT_DIALOG);
  359. #endif
  360. if(!hrsrc)
  361. {
  362. return NULL;
  363. }
  364. HGLOBAL hglb = LoadResource( _hInstance, hrsrc);
  365. return (DLGTEMPLATE*) LockResource(hglb);
  366. }