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.

288 lines
8.0 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // Dialog class
  4. //
  5. // 10-15-97 sburns
  6. #ifndef DIALOG_HPP_INCLUDED
  7. #define DIALOG_HPP_INCLUDED
  8. // Wraps a Windows dialog box, cracks and demuxes the dialog messages, and
  9. // provides a mechanism for tracking the control IDs that have been touched
  10. // over a given period. Also provides what-is help.
  11. //
  12. // May only be used as a base class.
  13. class Dialog
  14. {
  15. public:
  16. // can't use an enum 'cause the type needs to be DWORD
  17. #define NO_HELP (static_cast<DWORD>(-1))
  18. // Executes the dialog modally via the Win DialogBox API. Blocks until the
  19. // Win EndDialog API is called; returns the value passed to EndDialog.
  20. // Derived classes must call EndDialog in their message processing to
  21. // terminate the execution of the dialog.
  22. //
  23. // parent - handle to the parent window, which will be disabled during
  24. // the execution of the dialog.
  25. INT_PTR
  26. ModalExecute(HWND parent);
  27. INT_PTR
  28. ModalExecute(const Dialog& parent);
  29. // Executes the dialog box modelessly, returning immediately. Derived
  30. // classes should call EndModelessExecution to close a Dialog executing
  31. // modelessly, or, destroy the Dialog instance.
  32. //
  33. // Example:
  34. //
  35. // MyFooDialog* dlg = new MyFooDialog(....);
  36. // dlg->ModlessExecute();
  37. //
  38. // // ... later
  39. //
  40. // dlg->EndModelessExecution();
  41. //
  42. // // or just:
  43. //
  44. // delete dlg;
  45. //
  46. // parent - handle to the parent window of this dialog, for establishing
  47. // z-order.
  48. void
  49. ModelessExecute(HWND parent);
  50. void
  51. ModelessExecute(const Dialog& parent);
  52. // Terminates the modeless execution of a dialog that has been started
  53. // with ModalExecute. It is illegal to call this method before calling
  54. // ModalExecute.
  55. void
  56. EndModelessExecution();
  57. // Returns the window handle of the dialog. This method is only valid
  58. // after the window has been created and initialized by receiving
  59. // WM_INITDIALOG (which will be the case before any virtual message
  60. // handling functions are invoked).
  61. HWND
  62. GetHWND() const;
  63. // Returns the resource ID with which this instance was created.
  64. unsigned
  65. GetResID() const;
  66. // Returns a pointer to the Dialog instance that has been previously
  67. // stashed in the DWL_USER portion of the window structure. This method is
  68. // only valid after the window has been created and initialized by
  69. // receiving WM_INITDIALOG (which will be the case before any virtual
  70. // message handling functions are invoked).
  71. //
  72. // dialog - handle to initialized dialog window.
  73. static
  74. Dialog*
  75. GetInstance(HWND dialog);
  76. protected:
  77. // Constructs a new instance. Declared protected so that this class
  78. // only functions as base class
  79. //
  80. // resID - resource identifier of the dialog template resource.
  81. //
  82. // helpMap - array mapping dialog control IDs to context help IDs. The
  83. // array must be in the following form:
  84. // {
  85. // controlID1, helpID1,
  86. // controlID2, helpID2,
  87. // controlIDn, helpIDn,
  88. // 0, 0
  89. // }
  90. //
  91. // To indicate that a control does not have help, the value of its
  92. // corresponding help ID should be NO_HELP. This array is copied
  93. // by the constructor.
  94. Dialog(
  95. unsigned resID,
  96. const DWORD helpMap[]);
  97. // Destroys an instance. If the ModelessExecute method has been called,
  98. // and the EndModlessExecution method has not yet been called,
  99. // EndModelessExecution is called.
  100. virtual ~Dialog();
  101. // Resets the control ID change map.
  102. void
  103. ClearChanges();
  104. // Marks a control ID as changed. Derived classes should call this from
  105. // their processing of OnCommand or OnNotify as appropriate to indicate
  106. // that a control has changed state. This is useful for keeping track of
  107. // the fields in a database that need to be changed, etc.
  108. //
  109. // controlResID - the resource ID of the control that changed.
  110. void
  111. SetChanged(UINT_PTR controlResID);
  112. // Queries the control ID change map, returning true if the specified
  113. // control ID was previously marked as changed (with SetChanged), false
  114. // if not.
  115. //
  116. // controlResID - the resource ID of the control that changed.
  117. bool
  118. WasChanged(UINT_PTR controlResID) const;
  119. // Returns true if SetChanged was called for any control on the dialog,
  120. // false if not.
  121. bool
  122. WasChanged() const;
  123. // (For debugging) dumps the state of the control ID change map using
  124. // OutputDebugString. For non-debug builds, a no-op.
  125. void
  126. DumpChangeMap() const;
  127. // Invoked upon receipt of WM_DESTROY. The default implementation does
  128. // nothing.
  129. virtual
  130. void
  131. OnDestroy();
  132. // Invoked upon receipt of WM_COMMAND. Derived class' implementation
  133. // should return true they handle the message, false if not. The default
  134. // implementation returns false.
  135. //
  136. // windowFrom - the HWND of the child window (control) that originated the
  137. // message.
  138. //
  139. // controlIDFrom - The resource ID of the child window corresponding to the
  140. // window indicated in the windowFrom parameter.
  141. //
  142. // code - the notification code.
  143. virtual
  144. bool
  145. OnCommand(
  146. HWND windowFrom,
  147. unsigned controlIdFrom,
  148. unsigned code);
  149. // Invoked upon receipt of WM_INITDIALOG, after having set things up such
  150. // that GetHWND and GetInstance will work correctly. The default
  151. // implementation does nothing.
  152. virtual
  153. void
  154. OnInit();
  155. // Invoked upon receipt of any window message that is not dispatched to
  156. // other member functions of this class, including user-defined messages.
  157. // Derived class' implementation should return true they handle the
  158. // message, false if not. The default implementation returns false.
  159. //
  160. // message - the message code passed to the dialog window.
  161. //
  162. // wparam - the WPARAM parameter accompanying the message.
  163. //
  164. // lparam - the LPARAM parameter accompanying the message.
  165. virtual
  166. bool
  167. OnMessage(
  168. UINT message,
  169. WPARAM wparam,
  170. LPARAM lparam);
  171. // Invoked upon receipt of WM_NOTIFY. Derived class' implementation should
  172. // return true they handle the message, false if not. The default
  173. // implementation returns false.
  174. //
  175. // windowFrom - the HWND of the child window (control) that originated the
  176. // message.
  177. //
  178. // controlIDFrom - The resource ID of the child window corresponding to the
  179. // window indicated in the windowFrom parameter.
  180. //
  181. // code - the notification code.
  182. //
  183. // lparam - the LPARAM parameter of the WM_NOTIFY message, which may,
  184. // depending on the window that originated the message, be a pointer to a
  185. // structure indicating other parameters to the notification.
  186. virtual
  187. bool
  188. OnNotify(
  189. HWND windowFrom,
  190. UINT_PTR controlIDFrom,
  191. UINT code,
  192. LPARAM lparam);
  193. // Stores the window handle of the dialog in this instance, so that it can
  194. // be later retrieved by GetHWND. This is typically called by derived
  195. // classes that are doing their own processing of WM_INITDIALOG. This
  196. // function may be legally called only once in the lifetime of any Dialog
  197. // instance.
  198. //
  199. // window - handle to the dialog window.
  200. void
  201. SetHWND(HWND window);
  202. // The window handle of corresponding to this instance. Only valid after
  203. // WM_INITDIALOG has been received.
  204. HWND hwnd;
  205. // accessible to PropertyPage, etc.
  206. static
  207. INT_PTR CALLBACK
  208. dialogProc(HWND dialog, UINT message, WPARAM wparam, LPARAM lparam);
  209. private:
  210. // not implemented: no copying allowed
  211. Dialog(const Dialog&);
  212. const Dialog& operator=(const Dialog&);
  213. typedef
  214. std::map<
  215. UINT_PTR,
  216. bool,
  217. std::less<UINT_PTR>,
  218. Burnslib::Heap::Allocator<bool> >
  219. ChangeMap;
  220. // 'mutable' required for non-const map<>::operator[].
  221. mutable ChangeMap changemap;
  222. DWORD* helpMap;
  223. bool isEnded;
  224. bool isModeless;
  225. unsigned resID;
  226. };
  227. #endif // DIALOG_HPP_INCLUDED