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.

256 lines
9.7 KiB

  1. // Copyright (C) 1993-1997 Microsoft Corporation. All rights reserved.
  2. #if _MSC_VER > 1000
  3. #pragma once
  4. #endif
  5. #ifndef _CDLG_H_
  6. #define _CDLG_H_
  7. #include "cstr.h"
  8. #ifndef _INC_COMMCTRL
  9. #include <commctrl.h>
  10. #endif
  11. #ifndef _HHCTRL_H_
  12. #include "hhctrl.h"
  13. #endif
  14. #include "clistbox.h"
  15. #include "lockout.h"
  16. void STDCALL CenterWindow(HWND hwndParent, HWND hwnd);
  17. class CDlgCheckListBox;
  18. /*
  19. * Similar to CDialog, but calls OnCommand() instead of using a message
  20. * map (i.e., this uses a smaller working set then CDialog). When
  21. * initializing or when IDOK is processed, Cdlg calls OnBeginOrEnd() (or
  22. * m_pBeginOrEnd() for non-subclassed instances). CDlg includes
  23. * CDlg_ and DDV_ variants which can be used instead of the MFC versions.
  24. * Unlike MFC, CDlg_Text works with either a CStr or an array.
  25. */
  26. class CDlg
  27. {
  28. public:
  29. typedef BOOL (* BEGIN_OR_END_PROC)(CDlg*);
  30. CDlg(HWND hwndParent, UINT idTemplate);
  31. CDlg(CHtmlHelpControl* phhCtrl, UINT idTemplate);
  32. ~CDlg();
  33. BOOL m_fInitializing;
  34. inline BOOL Initializing(void) { return m_fInitializing; }
  35. // Following defaults to TRUE to have dialog centered in it's parent
  36. BOOL m_fCenterWindow;
  37. int DoModal(void);
  38. HWND DoModeless(void);
  39. // To create the dialog as a Unicode dialog when possible, call SetUnicode(TRUE).
  40. // When turned on, we'll attempt to create the dialog using CreateDialog[Param]W.
  41. // If this fails (i.e. on Win9x) the dialog will be created as Ansi, and IsUnicode will
  42. // thereafter return FALSE. Thus IsUnicode won't be accurate until you've called
  43. // DoModal or DoModeless. It WILL be accurate during dialog init.
  44. void SetUnicode(BOOL fUnicode) { m_fUnicode = fUnicode; }
  45. BOOL IsUnicode() const { return m_fUnicode; }
  46. // Called before IDOK and IDCANCEL are processed. Return TRUE to continue
  47. // processing.
  48. virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam) { return OnMsg(wParam); };
  49. /*
  50. * Return FALSE to cancel ending of the dialog, TRUE to accept.
  51. * m_fInitializing will be set to TRUE if this function is being called
  52. * to initialize the dialog.
  53. */
  54. virtual BOOL OnBeginOrEnd() { return TRUE; };
  55. // The following functions are always called unless the dialog is
  56. // shutting down. You can use these instead of using a msg list.
  57. virtual void OnButton(UINT id) { }; // called before processing OKAY and Cancel
  58. virtual void OnSelChange(UINT id) { }; // either list box or combo box
  59. virtual void OnDoubleClick(UINT id) { }; // either list box or combo box
  60. virtual void OnEditChange(UINT id) { };
  61. // Called for everything except WM_INITDIALOG, WM_COMMAND, WM_HELP, and
  62. // WM_CONTEXTMENU.
  63. virtual LRESULT OnDlgMsg(UINT msg, WPARAM wParam, LPARAM lParam) { return FALSE; }
  64. void InitializeSpinControl(UINT idSpin, UINT idBuddy, int minVal, int maxVal) const
  65. {
  66. SendMessage(idSpin, UDM_SETBUDDY, (WPARAM) GetDlgItem(idBuddy));
  67. SendMessage(idSpin, UDM_SETRANGE, 0, MAKELPARAM(maxVal, minVal));
  68. }
  69. HWND GetDlgItem(int idDlgControl) const {
  70. #ifdef _DEBUG
  71. ASSERT_COMMENT(::GetDlgItem(m_hWnd, idDlgControl), "Invalid Dialog Item id");
  72. #endif
  73. return ::GetDlgItem(m_hWnd, idDlgControl); };
  74. void SetWindowText(int idControl, PCSTR pszText) const { (void) ::SetWindowText(GetDlgItem(idControl), pszText); };
  75. void SetWindowText(int idControl, int idResource) const { (void) ::SetWindowText(GetDlgItem(idControl), GetStringResource(idResource)); };
  76. void SetWindowText(PCSTR pszText) const { (void) ::SetWindowText(m_hWnd, pszText); };
  77. void GetWindowText(int idControl, PSTR pszText, int cchMax = MAX_PATH) const { (void) ::GetWindowText(GetDlgItem(idControl), pszText, cchMax); };
  78. int GetWindowTextLength(int id) const { return ::GetWindowTextLength(GetDlgItem(id)); };
  79. void SetFocus(int id) const { (void) ::SetFocus(GetDlgItem(id)); };
  80. void EndDialog(int result) const { (void) ::EndDialog(m_hWnd, result); };
  81. void EnableWindow(int id, BOOL fEnable = TRUE) const { (void) ::EnableWindow(GetDlgItem(id), fEnable); };
  82. void DisableWindow(int id) const { (void) ::EnableWindow(GetDlgItem(id), FALSE); };
  83. void SetWindowLong(int index, LONG lval) const { ::SetWindowLong(m_hWnd, index, lval); }
  84. int GetCheck(int id) const { return (int) SendDlgItemMessage(m_hWnd, id, BM_GETCHECK, 0, 0L); };
  85. void SetCheck(int id, int check = TRUE) const { (void) SendDlgItemMessage(m_hWnd, id, BM_SETCHECK, (WPARAM)(int)(check), 0); };
  86. void UnCheck(int id) const { (void) SendDlgItemMessage(m_hWnd, id, BM_SETCHECK, FALSE, 0); };
  87. INT_PTR SendMessage(int id, UINT msg, WPARAM wParam = 0, LPARAM lParam = 0) const { return ::SendDlgItemMessage(m_hWnd, id, msg, wParam, lParam); };
  88. void ShowWindow(int id, int flag = SW_SHOW) const { ::ShowWindow(GetDlgItem(id), flag); };
  89. void HideWindow(int id) const { ::ShowWindow(GetDlgItem(id), SW_HIDE); };
  90. // Point this to a function if you don't want to subclass CDlg.
  91. // The function will be called instead of OnBeginOrEnd().
  92. BEGIN_OR_END_PROC m_pBeginOrEnd;
  93. HWND m_hWnd;
  94. BOOL m_fFocusChanged; // set to TRUE if you change focus during initialization
  95. LPCTSTR m_lpDialogTemplate;
  96. /////////////////////////////////////////////////////////////////////
  97. /*
  98. Fill these in before calling DoModal() to get Context-Sensitive Help
  99. Alternatively, put the following in your constructor or OnBeginOrEnd():
  100. BEGIN_HELPIDS()
  101. IDHHA_CHECK_EXTENDED_INFO, IDH_CHECK_EXTENDED_INFO,
  102. END_HELPIDS(AfxGetApp()->m_pszHelpFilePath)
  103. */
  104. DWORD* m_aHelpIds;
  105. PCSTR m_pszHelpFile;
  106. #define BEGIN_HELPIDS() \
  107. static const DWORD aHelpIds[] = {
  108. #define END_HELPIDS(pszHelpFile) \
  109. 0, 0 \
  110. }; \
  111. m_aHelpIds = (DWORD*) aHelpIds; \
  112. m_pszHelpFile = pszHelpFile;
  113. /////////////////////////////////////////////////////////////////////
  114. // For notification messages (e.g., EN_CHANGE, BN_CLICKED)
  115. // initialize m_pmsglist and you don't need OnButton and OnCommand
  116. typedef void (CDlg::* CDLG_PROC)(void);
  117. typedef struct {
  118. UINT idMessage;
  119. UINT idControl;
  120. void (CDlg::* pfunc)(void);
  121. } CDLG_MESSAGE_LIST;
  122. #define ON_CDLG_BUTTON(idButton, pfunc) \
  123. { BN_CLICKED, idButton, (CDLG_PROC) pfunc },
  124. #define ON_CDLG_MSG(idMsg, idButton, pfunc) \
  125. { idMsg, idButton, (CDLG_PROC) pfunc },
  126. #define BEGIN_MSG_LIST() \
  127. static const CDLG_MESSAGE_LIST msglist[] = {
  128. #define END_MSG_LIST() \
  129. { 0, 0, 0 } }; \
  130. m_pmsglist = &msglist[0];
  131. /*
  132. Sample usage:
  133. BEGIN_MSG_LIST()
  134. ON_CDLG_BUTTON(IDHHA_ADD_FOO, AddFoo)
  135. ON_CDLG_MSG(EN_CHANGE, IDHHA_EDIT, FooChanged)
  136. END_MSG_LIST()
  137. SET_MSG_LIST; // use in constructor or OnBeginOrEnd()
  138. */
  139. const CDLG_MESSAGE_LIST* m_pmsglist;
  140. CHtmlHelpControl* m_phhCtrl;
  141. void MakeCheckedList(int idLB);
  142. CDlgCheckListBox* m_pCheckBox;
  143. operator HWND() const
  144. { return m_hWnd; }
  145. protected:
  146. friend BOOL STDCALL CDlgProc(HWND, UINT, WPARAM, LPARAM);
  147. LRESULT OnContextMenu(HWND hwnd);
  148. LRESULT OnHelp(HWND hwnd);
  149. BOOL OnMsg(WPARAM wParam);
  150. HWND m_hwndParent;
  151. BOOL m_fModeLess;
  152. BOOL m_fShuttingDown;
  153. CLockOut m_LockOut;
  154. private:
  155. BOOL m_fUnicode; // Do we attempt to create a Unicode dialog?
  156. };
  157. // Header-only classes for coding convenience
  158. class CDlgComboBox
  159. {
  160. public:
  161. CDlgComboBox() { m_hWnd = NULL; }
  162. CDlgComboBox(HWND hwndParent, int id) {
  163. m_hWnd = GetDlgItem(hwndParent, id);
  164. ASSERT_COMMENT(m_hWnd, "Invalid Combo-box id");
  165. }
  166. void Initialize(int id) { ASSERT(m_hWnd); m_hWnd = GetDlgItem(GetParent(m_hWnd), id); };
  167. void Initialize(HWND hdlg, int id) { m_hWnd = ::GetDlgItem(hdlg, id); };
  168. INT_PTR SendMessage(UINT msg, WPARAM wParam = 0, LPARAM lParam = 0) const { return ::SendMessage(m_hWnd, msg, wParam, lParam); };
  169. void Enable(BOOL fEnable = TRUE) const { EnableWindow(m_hWnd, fEnable); };
  170. int GetText(PSTR psz, int cchMax = MAX_PATH) const { return GetWindowText(m_hWnd, psz, cchMax); };
  171. INT_PTR GetLBText(PSTR psz, int iSel) const {
  172. return SendMessage(CB_GETLBTEXT, iSel, (LPARAM) psz); }
  173. int GetTextLength() const { return GetWindowTextLength(m_hWnd); };
  174. void SetText(PCSTR psz) const { (void) ::SetWindowText(m_hWnd, psz); };
  175. INT_PTR GetCount() const { return SendMessage(CB_GETCOUNT); };
  176. void ResetContent() const { SendMessage(CB_RESETCONTENT); };
  177. void Reset() const { SendMessage(CB_RESETCONTENT); };
  178. INT_PTR AddString(PCSTR psz) const { return SendMessage(CB_ADDSTRING, 0, (LPARAM) psz); };
  179. INT_PTR InsertString(int index, PCSTR psz) const { return SendMessage(CB_INSERTSTRING, index, (LPARAM) psz); };
  180. INT_PTR DeleteString(int index) const { return SendMessage(CB_DELETESTRING, index); };
  181. INT_PTR GetItemData(int index) const { return SendMessage(CB_GETITEMDATA, index); };
  182. INT_PTR SetItemData(int index, int data) const { return SendMessage(CB_SETITEMDATA, index, data); };
  183. INT_PTR GetCurSel() const { return SendMessage(CB_GETCURSEL); };
  184. void SetCurSel(int index = 0) const { (void) SendMessage(CB_SETCURSEL, index); };
  185. void SetEditSel(int iStart, int iEnd) const { (void) SendMessage(CB_SETEDITSEL, 0, MAKELPARAM(iStart, iEnd)); };
  186. void SelectEditContol(void) const { SendMessage(CB_SETEDITSEL, 0, MAKELPARAM(0, -1)); };
  187. INT_PTR FindString(PCSTR pszString, int iStart = -1) const { return SendMessage(CB_FINDSTRING, iStart, (LPARAM) pszString); };
  188. INT_PTR SelectString(PCSTR pszString, int iStart = -1) const { return SendMessage(CB_SELECTSTRING, iStart, (LPARAM) pszString); };
  189. HWND m_hWnd;
  190. };
  191. #endif // _CDLG_H_