wizard.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/wizard.h
  3. // Purpose: wxWizard class: a GUI control presenting the user with a
  4. // sequence of dialogs which allows to simply perform some task
  5. // Author: Vadim Zeitlin (partly based on work by Ron Kuris and Kevin B.
  6. // Smith)
  7. // Modified by: Robert Cavanaugh
  8. // Added capability to use .WXR resource files in Wizard pages
  9. // Added wxWIZARD_HELP event
  10. // Robert Vazan (sizers)
  11. // Created: 15.08.99
  12. // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  13. // Licence: wxWindows licence
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #ifndef _WX_WIZARD_H_
  16. #define _WX_WIZARD_H_
  17. #include "wx/defs.h"
  18. #if wxUSE_WIZARDDLG
  19. // ----------------------------------------------------------------------------
  20. // headers and other simple declarations
  21. // ----------------------------------------------------------------------------
  22. #include "wx/dialog.h" // the base class
  23. #include "wx/panel.h" // ditto
  24. #include "wx/event.h" // wxEVT_XXX constants
  25. #include "wx/bitmap.h"
  26. // Extended style to specify a help button
  27. #define wxWIZARD_EX_HELPBUTTON 0x00000010
  28. // Placement flags
  29. #define wxWIZARD_VALIGN_TOP 0x01
  30. #define wxWIZARD_VALIGN_CENTRE 0x02
  31. #define wxWIZARD_VALIGN_BOTTOM 0x04
  32. #define wxWIZARD_HALIGN_LEFT 0x08
  33. #define wxWIZARD_HALIGN_CENTRE 0x10
  34. #define wxWIZARD_HALIGN_RIGHT 0x20
  35. #define wxWIZARD_TILE 0x40
  36. // forward declarations
  37. class WXDLLIMPEXP_FWD_ADV wxWizard;
  38. // ----------------------------------------------------------------------------
  39. // wxWizardPage is one of the wizards screen: it must know what are the
  40. // following and preceding pages (which may be NULL for the first/last page).
  41. //
  42. // Other than GetNext/Prev() functions, wxWizardPage is just a panel and may be
  43. // used as such (i.e. controls may be placed directly on it &c).
  44. // ----------------------------------------------------------------------------
  45. class WXDLLIMPEXP_ADV wxWizardPage : public wxPanel
  46. {
  47. public:
  48. wxWizardPage() { Init(); }
  49. // ctor accepts an optional bitmap which will be used for this page instead
  50. // of the default one for this wizard (should be of the same size). Notice
  51. // that no other parameters are needed because the wizard will resize and
  52. // reposition the page anyhow
  53. wxWizardPage(wxWizard *parent,
  54. const wxBitmap& bitmap = wxNullBitmap);
  55. bool Create(wxWizard *parent,
  56. const wxBitmap& bitmap = wxNullBitmap);
  57. // these functions are used by the wizard to show another page when the
  58. // user chooses "Back" or "Next" button
  59. virtual wxWizardPage *GetPrev() const = 0;
  60. virtual wxWizardPage *GetNext() const = 0;
  61. // default GetBitmap() will just return m_bitmap which is ok in 99% of
  62. // cases - override this method if you want to create the bitmap to be used
  63. // dynamically or to do something even more fancy. It's ok to return
  64. // wxNullBitmap from here - the default one will be used then.
  65. virtual wxBitmap GetBitmap() const { return m_bitmap; }
  66. #if wxUSE_VALIDATORS
  67. // Override the base functions to allow a validator to be assigned to this page.
  68. virtual bool TransferDataToWindow()
  69. {
  70. return GetValidator() ? GetValidator()->TransferToWindow()
  71. : wxPanel::TransferDataToWindow();
  72. }
  73. virtual bool TransferDataFromWindow()
  74. {
  75. return GetValidator() ? GetValidator()->TransferFromWindow()
  76. : wxPanel::TransferDataFromWindow();
  77. }
  78. virtual bool Validate()
  79. {
  80. return GetValidator() ? GetValidator()->Validate(this)
  81. : wxPanel::Validate();
  82. }
  83. #endif // wxUSE_VALIDATORS
  84. protected:
  85. // common part of ctors:
  86. void Init();
  87. wxBitmap m_bitmap;
  88. private:
  89. DECLARE_DYNAMIC_CLASS_NO_COPY(wxWizardPage)
  90. };
  91. // ----------------------------------------------------------------------------
  92. // wxWizardPageSimple just returns the pointers given to the ctor and is useful
  93. // to create a simple wizard where the order of pages never changes.
  94. //
  95. // OTOH, it is also possible to dynamically decide which page to return (i.e.
  96. // depending on the user's choices) as the wizard sample shows - in order to do
  97. // this, you must derive from wxWizardPage directly.
  98. // ----------------------------------------------------------------------------
  99. class WXDLLIMPEXP_ADV wxWizardPageSimple : public wxWizardPage
  100. {
  101. public:
  102. wxWizardPageSimple() { Init(); }
  103. // ctor takes the previous and next pages
  104. wxWizardPageSimple(wxWizard *parent,
  105. wxWizardPage *prev = NULL,
  106. wxWizardPage *next = NULL,
  107. const wxBitmap& bitmap = wxNullBitmap)
  108. {
  109. Create(parent, prev, next, bitmap);
  110. }
  111. bool Create(wxWizard *parent = NULL, // let it be default ctor too
  112. wxWizardPage *prev = NULL,
  113. wxWizardPage *next = NULL,
  114. const wxBitmap& bitmap = wxNullBitmap)
  115. {
  116. m_prev = prev;
  117. m_next = next;
  118. return wxWizardPage::Create(parent, bitmap);
  119. }
  120. // the pointers may be also set later - but before starting the wizard
  121. void SetPrev(wxWizardPage *prev) { m_prev = prev; }
  122. void SetNext(wxWizardPage *next) { m_next = next; }
  123. // Convenience functions to make the pages follow each other without having
  124. // to call their SetPrev() or SetNext() explicitly.
  125. wxWizardPageSimple& Chain(wxWizardPageSimple* next)
  126. {
  127. SetNext(next);
  128. next->SetPrev(this);
  129. return *next;
  130. }
  131. static void Chain(wxWizardPageSimple *first, wxWizardPageSimple *second)
  132. {
  133. wxCHECK_RET( first && second,
  134. wxT("NULL passed to wxWizardPageSimple::Chain") );
  135. first->SetNext(second);
  136. second->SetPrev(first);
  137. }
  138. // base class pure virtuals
  139. virtual wxWizardPage *GetPrev() const;
  140. virtual wxWizardPage *GetNext() const;
  141. private:
  142. // common part of ctors:
  143. void Init()
  144. {
  145. m_prev = m_next = NULL;
  146. }
  147. // pointers are private, the derived classes shouldn't mess with them -
  148. // just derive from wxWizardPage directly to implement different behaviour
  149. wxWizardPage *m_prev,
  150. *m_next;
  151. DECLARE_DYNAMIC_CLASS_NO_COPY(wxWizardPageSimple)
  152. };
  153. // ----------------------------------------------------------------------------
  154. // wxWizard
  155. // ----------------------------------------------------------------------------
  156. class WXDLLIMPEXP_ADV wxWizardBase : public wxDialog
  157. {
  158. public:
  159. /*
  160. The derived class (i.e. the real wxWizard) has a ctor and Create()
  161. function taking the following arguments:
  162. wxWizard(wxWindow *parent,
  163. int id = wxID_ANY,
  164. const wxString& title = wxEmptyString,
  165. const wxBitmap& bitmap = wxNullBitmap,
  166. const wxPoint& pos = wxDefaultPosition,
  167. long style = wxDEFAULT_DIALOG_STYLE);
  168. */
  169. wxWizardBase() { }
  170. // executes the wizard starting from the given page, returns true if it was
  171. // successfully finished, false if user cancelled it
  172. virtual bool RunWizard(wxWizardPage *firstPage) = 0;
  173. // get the current page (NULL if RunWizard() isn't running)
  174. virtual wxWizardPage *GetCurrentPage() const = 0;
  175. // set the min size which should be available for the pages: a
  176. // wizard will take into account the size of the bitmap (if any)
  177. // itself and will never be less than some predefined fixed size
  178. virtual void SetPageSize(const wxSize& size) = 0;
  179. // get the size available for the page
  180. virtual wxSize GetPageSize() const = 0;
  181. // set the best size for the wizard, i.e. make it big enough to contain all
  182. // of the pages starting from the given one
  183. //
  184. // this function may be called several times and possible with different
  185. // pages in which case it will only increase the page size if needed (this
  186. // may be useful if not all pages are accessible from the first one by
  187. // default)
  188. virtual void FitToPage(const wxWizardPage *firstPage) = 0;
  189. // Adding pages to page area sizer enlarges wizard
  190. virtual wxSizer *GetPageAreaSizer() const = 0;
  191. // Set border around page area. Default is 0 if you add at least one
  192. // page to GetPageAreaSizer and 5 if you don't.
  193. virtual void SetBorder(int border) = 0;
  194. // the methods below may be overridden by the derived classes to provide
  195. // custom logic for determining the pages order
  196. virtual bool HasNextPage(wxWizardPage *page)
  197. { return page->GetNext() != NULL; }
  198. virtual bool HasPrevPage(wxWizardPage *page)
  199. { return page->GetPrev() != NULL; }
  200. /// Override these functions to stop InitDialog from calling TransferDataToWindow
  201. /// for _all_ pages when the wizard starts. Instead 'ShowPage' will call
  202. /// TransferDataToWindow for the first page only.
  203. bool TransferDataToWindow() { return true; }
  204. bool TransferDataFromWindow() { return true; }
  205. bool Validate() { return true; }
  206. private:
  207. wxDECLARE_NO_COPY_CLASS(wxWizardBase);
  208. };
  209. // include the real class declaration
  210. #include "wx/generic/wizard.h"
  211. // ----------------------------------------------------------------------------
  212. // wxWizardEvent class represents an event generated by the wizard: this event
  213. // is first sent to the page itself and, if not processed there, goes up the
  214. // window hierarchy as usual
  215. // ----------------------------------------------------------------------------
  216. class WXDLLIMPEXP_ADV wxWizardEvent : public wxNotifyEvent
  217. {
  218. public:
  219. wxWizardEvent(wxEventType type = wxEVT_NULL,
  220. int id = wxID_ANY,
  221. bool direction = true,
  222. wxWizardPage* page = NULL);
  223. // for EVT_WIZARD_PAGE_CHANGING, return true if we're going forward or
  224. // false otherwise and for EVT_WIZARD_PAGE_CHANGED return true if we came
  225. // from the previous page and false if we returned from the next one
  226. // (this function doesn't make sense for CANCEL events)
  227. bool GetDirection() const { return m_direction; }
  228. wxWizardPage* GetPage() const { return m_page; }
  229. virtual wxEvent *Clone() const { return new wxWizardEvent(*this); }
  230. private:
  231. bool m_direction;
  232. wxWizardPage* m_page;
  233. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWizardEvent)
  234. };
  235. // ----------------------------------------------------------------------------
  236. // macros for handling wxWizardEvents
  237. // ----------------------------------------------------------------------------
  238. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_PAGE_CHANGED, wxWizardEvent );
  239. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_PAGE_CHANGING, wxWizardEvent );
  240. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_CANCEL, wxWizardEvent );
  241. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_HELP, wxWizardEvent );
  242. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_FINISHED, wxWizardEvent );
  243. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_PAGE_SHOWN, wxWizardEvent );
  244. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_ADV, wxEVT_WIZARD_BEFORE_PAGE_CHANGED, wxWizardEvent );
  245. typedef void (wxEvtHandler::*wxWizardEventFunction)(wxWizardEvent&);
  246. #define wxWizardEventHandler(func) \
  247. wxEVENT_HANDLER_CAST(wxWizardEventFunction, func)
  248. #define wx__DECLARE_WIZARDEVT(evt, id, fn) \
  249. wx__DECLARE_EVT1(wxEVT_WIZARD_ ## evt, id, wxWizardEventHandler(fn))
  250. // notifies that the page has just been changed (can't be vetoed)
  251. #define EVT_WIZARD_PAGE_CHANGED(id, fn) wx__DECLARE_WIZARDEVT(PAGE_CHANGED, id, fn)
  252. // the user pressed "<Back" or "Next>" button and the page is going to be
  253. // changed - unless the event handler vetoes the event
  254. #define EVT_WIZARD_PAGE_CHANGING(id, fn) wx__DECLARE_WIZARDEVT(PAGE_CHANGING, id, fn)
  255. // Called before GetNext/GetPrev is called, so that the handler can change state that will be
  256. // used when GetNext/GetPrev is called. PAGE_CHANGING is called too late to influence GetNext/GetPrev.
  257. #define EVT_WIZARD_BEFORE_PAGE_CHANGED(id, fn) wx__DECLARE_WIZARDEVT(BEFORE_PAGE_CHANGED, id, fn)
  258. // the user pressed "Cancel" button and the wizard is going to be dismissed -
  259. // unless the event handler vetoes the event
  260. #define EVT_WIZARD_CANCEL(id, fn) wx__DECLARE_WIZARDEVT(CANCEL, id, fn)
  261. // the user pressed "Finish" button and the wizard is going to be dismissed -
  262. #define EVT_WIZARD_FINISHED(id, fn) wx__DECLARE_WIZARDEVT(FINISHED, id, fn)
  263. // the user pressed "Help" button
  264. #define EVT_WIZARD_HELP(id, fn) wx__DECLARE_WIZARDEVT(HELP, id, fn)
  265. // the page was just shown and laid out
  266. #define EVT_WIZARD_PAGE_SHOWN(id, fn) wx__DECLARE_WIZARDEVT(PAGE_SHOWN, id, fn)
  267. #endif // wxUSE_WIZARDDLG
  268. #endif // _WX_WIZARD_H_