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.

311 lines
8.3 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: ModalDlg.cpp
  4. //
  5. // Module: Connection manager
  6. //
  7. // Synopsis: Implementation of the classes CWindowWithHelp, CModalDlg
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: fengsun Created 02/17/98
  12. //
  13. //+----------------------------------------------------------------------------
  14. //+----------------------------------------------------------------------------
  15. //
  16. // Function: CWindowWithHelp::CWindowWithHelp
  17. //
  18. // Synopsis: Constructor
  19. //
  20. // Arguments: const DWORD* pHelpPairs - The pairs of control-ID/Help-ID
  21. // const TCHAR* lpszHelpFile - The help file name, default is NULL
  22. // Call also call SetHelpFileName() to provide help file
  23. //
  24. // Returns: Nothing
  25. //
  26. // History: fengsun Created Header 2/20/98
  27. //
  28. //+----------------------------------------------------------------------------
  29. CWindowWithHelp::CWindowWithHelp(const DWORD* pHelpPairs, const TCHAR* lpszHelpFile)
  30. {
  31. m_lpszHelpFile = NULL;
  32. m_hWnd = NULL;
  33. m_pHelpPairs = pHelpPairs;
  34. if (lpszHelpFile)
  35. {
  36. SetHelpFileName(lpszHelpFile);
  37. }
  38. }
  39. //+----------------------------------------------------------------------------
  40. //
  41. // Function: CWindowWithHelp::~CWindowWithHelp
  42. //
  43. // Synopsis: Destructor
  44. //
  45. // Arguments: None
  46. //
  47. // Returns: Nothing
  48. //
  49. // History: Created Header 2/20/98
  50. //
  51. //+----------------------------------------------------------------------------
  52. CWindowWithHelp::~CWindowWithHelp()
  53. {
  54. CmFree(m_lpszHelpFile);
  55. }
  56. //+----------------------------------------------------------------------------
  57. //
  58. // Function: CWindowWithHelp::SetHelpFileName
  59. //
  60. // Synopsis: Set the help file name of the window
  61. //
  62. // Arguments: const TCHAR* lpszHelpFile - the help file name to set
  63. //
  64. // Returns: Nothing
  65. //
  66. // History: fengsun Created Header 2/20/98
  67. //
  68. //+----------------------------------------------------------------------------
  69. void CWindowWithHelp::SetHelpFileName(const TCHAR* lpszHelpFile)
  70. {
  71. MYDBGASSERT(m_lpszHelpFile == NULL);
  72. MYDBGASSERT(lpszHelpFile);
  73. CmFree(m_lpszHelpFile);
  74. m_lpszHelpFile = NULL;
  75. if (lpszHelpFile && lpszHelpFile[0])
  76. {
  77. m_lpszHelpFile = CmStrCpyAlloc(lpszHelpFile);
  78. MYDBGASSERT(m_lpszHelpFile);
  79. }
  80. }
  81. //+----------------------------------------------------------------------------
  82. //
  83. // Function: CWindowWithHelp::HasContextHelp
  84. //
  85. // Synopsis: Whether a control has context help
  86. //
  87. // Arguments: HWND hWndCtrl - The window handle of the control
  88. //
  89. // Returns: BOOL - TRUE , if the control has context help
  90. //
  91. // History: fengsun Created Header 2/20/98
  92. //
  93. //+----------------------------------------------------------------------------
  94. BOOL CWindowWithHelp::HasContextHelp(HWND hWndCtrl) const
  95. {
  96. if (hWndCtrl == NULL || m_pHelpPairs == NULL)
  97. {
  98. return FALSE;
  99. }
  100. //
  101. // looks through the help pairs for the control
  102. //
  103. for (int i=0; m_pHelpPairs[i]!=0; i+=2)
  104. {
  105. if (m_pHelpPairs[i] == (DWORD)GetDlgCtrlID(hWndCtrl))
  106. {
  107. CMTRACE3(TEXT("HasContextHelp() - hwndCtrl %d has Ctrl ID %d and context help ID %d"), hWndCtrl, m_pHelpPairs[i], m_pHelpPairs[i+1]);
  108. return TRUE;
  109. }
  110. }
  111. CMTRACE1(TEXT("HasContextHelp() - hwndCtrl %d has no context help"), hWndCtrl);
  112. return FALSE;
  113. }
  114. //+----------------------------------------------------------------------------
  115. //
  116. // Function: CWindowWithHelp::OnHelp
  117. //
  118. // Synopsis: Call on WM_HELP message. Which means F1 is pressed
  119. //
  120. // Arguments: const HELPINFO* pHelpInfo - lParam of WM_HELP
  121. //
  122. // Returns: Nothing
  123. //
  124. // History: Created Header 2/17/98
  125. //
  126. //+----------------------------------------------------------------------------
  127. void CWindowWithHelp::OnHelp(const HELPINFO* pHelpInfo)
  128. {
  129. //
  130. // If help file exist and the help id exist WinHelp
  131. //
  132. if (m_lpszHelpFile && m_lpszHelpFile[0] && HasContextHelp((HWND) pHelpInfo->hItemHandle))
  133. {
  134. CmWinHelp((HWND)pHelpInfo->hItemHandle, (HWND)pHelpInfo->hItemHandle, m_lpszHelpFile, HELP_WM_HELP,
  135. (ULONG_PTR)(LPSTR)m_pHelpPairs);
  136. }
  137. }
  138. //+----------------------------------------------------------------------------
  139. //
  140. // Function: CWindowWithHelp::OnContextMenu
  141. //
  142. // Synopsis: called upon WM_CONTEXTMENU message (Right click or '?')
  143. //
  144. // Arguments: HWND hWnd - Handle to the window in which the user right clicked
  145. // the mouse
  146. // POINT& pos - position of the cursor
  147. //
  148. // Returns: BOOL, TRUE if the message is processed
  149. //
  150. // History: fengsun Created Header 2/17/98
  151. //
  152. //+----------------------------------------------------------------------------
  153. BOOL CWindowWithHelp::OnContextMenu( HWND hWnd, POINT& pos )
  154. {
  155. HWND hWndChild;
  156. ScreenToClient(m_hWnd, &pos);
  157. //
  158. // If more than one child window contains the specified point, ChildWindowFromPoint()
  159. // returns a handle to the first window in the list that contains the point.
  160. // This becomes a problem if we have controls inside groupbox
  161. //
  162. if (m_lpszHelpFile && m_lpszHelpFile[0] &&
  163. HasContextHelp((hWndChild = ChildWindowFromPointEx(m_hWnd, pos,CWP_SKIPINVISIBLE))) )
  164. {
  165. CMTRACE2(TEXT("OnContextMenu() - Calling WinHelp hWnd is %d, m_hWnd is %d"), hWnd, m_hWnd);
  166. CmWinHelp(hWnd,hWndChild,m_lpszHelpFile, HELP_CONTEXTMENU, (ULONG_PTR)m_pHelpPairs);
  167. return TRUE;
  168. }
  169. return FALSE; // Return FALSE, DefaultWindowProc will handle this message then.
  170. }
  171. //+----------------------------------------------------------------------------
  172. //
  173. // Function: CModalDlg::DoDialogBox
  174. //
  175. // Synopsis: Same as DialogBox
  176. //
  177. // Arguments: HINSTANCE hInstance - Same as ::DialogBox
  178. // LPCTSTR lpTemplateName -
  179. // HWND hWndParent -
  180. //
  181. // Returns: int - Same as DialogBox
  182. //
  183. // History: Created Header 2/17/98
  184. //
  185. //+----------------------------------------------------------------------------
  186. INT_PTR CModalDlg::DoDialogBox(HINSTANCE hInstance,
  187. LPCTSTR lpTemplateName,
  188. HWND hWndParent)
  189. {
  190. INT_PTR iRet = ::DialogBoxParamU(hInstance, lpTemplateName, hWndParent,
  191. (DLGPROC)ModalDialogProc, (LPARAM)this);
  192. m_hWnd = NULL;
  193. return iRet;
  194. }
  195. //+----------------------------------------------------------------------------
  196. //
  197. // Function: CModalDlg::ModalDialogProc
  198. //
  199. // Synopsis: The dialog window procedure for all dialogbox derived
  200. //
  201. // Arguments: HWND hwndDlg -
  202. // UINT uMsg -
  203. // WPARAM wParam -
  204. // LPARAM lParam -
  205. //
  206. // Returns: BOOL CALLBACK -
  207. //
  208. // History: Created Header 2/17/98
  209. //
  210. //+----------------------------------------------------------------------------
  211. BOOL CALLBACK CModalDlg::ModalDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM lParam)
  212. {
  213. CModalDlg* pDlg;
  214. //
  215. // Save the object pointer on WM_INITDIALOG
  216. // lParam is the pointer
  217. //
  218. if (uMsg == WM_INITDIALOG)
  219. {
  220. pDlg = (CModalDlg*) lParam;
  221. MYDBGASSERT(lParam);
  222. MYDBGASSERT(((CModalDlg*)lParam)->m_hWnd == NULL);
  223. //
  224. // Save the object pointer, this is implementation detail
  225. // The user of this class should not be aware of this
  226. //
  227. ::SetWindowLongU(hwndDlg, DWLP_USER, (LONG_PTR)lParam);
  228. pDlg->m_hWnd = hwndDlg;
  229. }
  230. else
  231. {
  232. pDlg = (CModalDlg*)GetWindowLongU(hwndDlg, DWLP_USER);
  233. //
  234. // some msgs can come before WM_INITDIALOG
  235. //
  236. if (pDlg == NULL)
  237. {
  238. return FALSE;
  239. }
  240. }
  241. MYDBGASSERT(pDlg->m_hWnd == hwndDlg);
  242. ASSERT_VALID(pDlg);
  243. switch(uMsg)
  244. {
  245. case WM_INITDIALOG:
  246. return pDlg->OnInitDialog();
  247. case WM_HELP:
  248. pDlg->OnHelp((LPHELPINFO)lParam);
  249. return TRUE;
  250. case WM_CONTEXTMENU:
  251. {
  252. POINT pos = {LOWORD(lParam), HIWORD(lParam)};
  253. return pDlg->OnContextMenu((HWND) wParam, pos);
  254. }
  255. case WM_COMMAND:
  256. switch (LOWORD(wParam))
  257. {
  258. case IDOK:
  259. pDlg->OnOK();
  260. return FALSE;
  261. case IDCANCEL:
  262. pDlg->OnCancel();
  263. return FALSE;
  264. default:
  265. return pDlg->OnOtherCommand(wParam,lParam);
  266. }
  267. default:
  268. return pDlg->OnOtherMessage(uMsg, wParam, lParam);
  269. }
  270. }