dialog.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dialog.h
  3. // Purpose: wxDialogBase class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 29.06.99
  7. // Copyright: (c) Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DIALOG_H_BASE_
  11. #define _WX_DIALOG_H_BASE_
  12. #include "wx/toplevel.h"
  13. #include "wx/containr.h"
  14. #include "wx/sharedptr.h"
  15. class WXDLLIMPEXP_FWD_CORE wxSizer;
  16. class WXDLLIMPEXP_FWD_CORE wxStdDialogButtonSizer;
  17. class WXDLLIMPEXP_FWD_CORE wxBoxSizer;
  18. class WXDLLIMPEXP_FWD_CORE wxDialogLayoutAdapter;
  19. class WXDLLIMPEXP_FWD_CORE wxDialog;
  20. class WXDLLIMPEXP_FWD_CORE wxButton;
  21. class WXDLLIMPEXP_FWD_CORE wxScrolledWindow;
  22. class wxTextSizerWrapper;
  23. // Also see the bit summary table in wx/toplevel.h.
  24. #define wxDIALOG_NO_PARENT 0x00000020 // Don't make owned by apps top window
  25. #ifdef __WXWINCE__
  26. #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxMAXIMIZE | wxCLOSE_BOX | wxNO_BORDER)
  27. #else
  28. #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX)
  29. #endif
  30. // Layout adaptation levels, for SetLayoutAdaptationLevel
  31. // Don't do any layout adaptation
  32. #define wxDIALOG_ADAPTATION_NONE 0
  33. // Only look for wxStdDialogButtonSizer for non-scrolling part
  34. #define wxDIALOG_ADAPTATION_STANDARD_SIZER 1
  35. // Also look for any suitable sizer for non-scrolling part
  36. #define wxDIALOG_ADAPTATION_ANY_SIZER 2
  37. // Also look for 'loose' standard buttons for non-scrolling part
  38. #define wxDIALOG_ADAPTATION_LOOSE_BUTTONS 3
  39. // Layout adaptation mode, for SetLayoutAdaptationMode
  40. enum wxDialogLayoutAdaptationMode
  41. {
  42. wxDIALOG_ADAPTATION_MODE_DEFAULT = 0, // use global adaptation enabled status
  43. wxDIALOG_ADAPTATION_MODE_ENABLED = 1, // enable this dialog overriding global status
  44. wxDIALOG_ADAPTATION_MODE_DISABLED = 2 // disable this dialog overriding global status
  45. };
  46. enum wxDialogModality
  47. {
  48. wxDIALOG_MODALITY_NONE = 0,
  49. wxDIALOG_MODALITY_WINDOW_MODAL = 1,
  50. wxDIALOG_MODALITY_APP_MODAL = 2
  51. };
  52. extern WXDLLIMPEXP_DATA_CORE(const char) wxDialogNameStr[];
  53. class WXDLLIMPEXP_CORE wxDialogBase : public wxNavigationEnabled<wxTopLevelWindow>
  54. {
  55. public:
  56. wxDialogBase();
  57. virtual ~wxDialogBase() { }
  58. // define public wxDialog methods to be implemented by the derived classes
  59. virtual int ShowModal() = 0;
  60. virtual void EndModal(int retCode) = 0;
  61. virtual bool IsModal() const = 0;
  62. // show the dialog frame-modally (needs a parent), using app-modal
  63. // dialogs on platforms that don't support it
  64. virtual void ShowWindowModal () ;
  65. virtual void SendWindowModalDialogEvent ( wxEventType type );
  66. #ifdef wxHAS_EVENT_BIND
  67. template<typename Functor>
  68. void ShowWindowModalThenDo(const Functor& onEndModal);
  69. #endif // wxHAS_EVENT_BIND
  70. // Modal dialogs have a return code - usually the id of the last
  71. // pressed button
  72. void SetReturnCode(int returnCode) { m_returnCode = returnCode; }
  73. int GetReturnCode() const { return m_returnCode; }
  74. // Set the identifier for the affirmative button: this button will close
  75. // the dialog after validating data and calling TransferDataFromWindow()
  76. void SetAffirmativeId(int affirmativeId);
  77. int GetAffirmativeId() const { return m_affirmativeId; }
  78. // Set identifier for Esc key translation: the button with this id will
  79. // close the dialog without doing anything else; special value wxID_NONE
  80. // means to not handle Esc at all while wxID_ANY means to map Esc to
  81. // wxID_CANCEL if present and GetAffirmativeId() otherwise
  82. void SetEscapeId(int escapeId);
  83. int GetEscapeId() const { return m_escapeId; }
  84. // Find the parent to use for modal dialog: try to use the specified parent
  85. // but fall back to the current active window or main application window as
  86. // last resort if it is unsuitable.
  87. //
  88. // As this function is often called from the ctor, the window style may be
  89. // not set yet and hence must be passed explicitly to it so that we could
  90. // check whether it contains wxDIALOG_NO_PARENT bit.
  91. //
  92. // This function always returns a valid top level window or NULL.
  93. wxWindow *GetParentForModalDialog(wxWindow *parent, long style) const;
  94. // This overload can only be used for already initialized windows, i.e. not
  95. // from the ctor. It uses the current window parent and style.
  96. wxWindow *GetParentForModalDialog() const
  97. {
  98. return GetParentForModalDialog(GetParent(), GetWindowStyle());
  99. }
  100. #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
  101. // splits text up at newlines and places the lines into a vertical
  102. // wxBoxSizer
  103. wxSizer *CreateTextSizer( const wxString& message );
  104. // same as above but uses a customized wxTextSizerWrapper to create
  105. // non-standard controls for the lines
  106. wxSizer *CreateTextSizer( const wxString& message,
  107. wxTextSizerWrapper& wrapper );
  108. #endif // wxUSE_STATTEXT // && wxUSE_TEXTCTRL
  109. // returns a horizontal wxBoxSizer containing the given buttons
  110. //
  111. // notice that the returned sizer can be NULL if no buttons are put in the
  112. // sizer (this mostly happens under smart phones and other atypical
  113. // platforms which have hardware buttons replacing OK/Cancel and such)
  114. wxSizer *CreateButtonSizer(long flags);
  115. // returns a sizer containing the given one and a static line separating it
  116. // from the preceding elements if it's appropriate for the current platform
  117. wxSizer *CreateSeparatedSizer(wxSizer *sizer);
  118. // returns the sizer containing CreateButtonSizer() below a separating
  119. // static line for the platforms which use static lines for items
  120. // separation (i.e. not Mac)
  121. //
  122. // this is just a combination of CreateButtonSizer() and
  123. // CreateSeparatedSizer()
  124. wxSizer *CreateSeparatedButtonSizer(long flags);
  125. #if wxUSE_BUTTON
  126. wxStdDialogButtonSizer *CreateStdDialogButtonSizer( long flags );
  127. #endif // wxUSE_BUTTON
  128. // Do layout adaptation
  129. virtual bool DoLayoutAdaptation();
  130. // Can we do layout adaptation?
  131. virtual bool CanDoLayoutAdaptation();
  132. // Returns a content window if there is one. This can be used by the layout adapter, for
  133. // example to make the pages of a book control into scrolling windows
  134. virtual wxWindow* GetContentWindow() const { return NULL; }
  135. // Add an id to the list of main button identifiers that should be in the button sizer
  136. void AddMainButtonId(wxWindowID id) { m_mainButtonIds.Add((int) id); }
  137. wxArrayInt& GetMainButtonIds() { return m_mainButtonIds; }
  138. // Is this id in the main button id array?
  139. bool IsMainButtonId(wxWindowID id) const { return (m_mainButtonIds.Index((int) id) != wxNOT_FOUND); }
  140. // Level of adaptation, from none (Level 0) to full (Level 3). To disable adaptation,
  141. // set level 0, for example in your dialog constructor. You might
  142. // do this if you know that you are displaying on a large screen and you don't want the
  143. // dialog changed.
  144. void SetLayoutAdaptationLevel(int level) { m_layoutAdaptationLevel = level; }
  145. int GetLayoutAdaptationLevel() const { return m_layoutAdaptationLevel; }
  146. /// Override global adaptation enabled/disabled status
  147. void SetLayoutAdaptationMode(wxDialogLayoutAdaptationMode mode) { m_layoutAdaptationMode = mode; }
  148. wxDialogLayoutAdaptationMode GetLayoutAdaptationMode() const { return m_layoutAdaptationMode; }
  149. // Returns true if the adaptation has been done
  150. void SetLayoutAdaptationDone(bool adaptationDone) { m_layoutAdaptationDone = adaptationDone; }
  151. bool GetLayoutAdaptationDone() const { return m_layoutAdaptationDone; }
  152. // Set layout adapter class, returning old adapter
  153. static wxDialogLayoutAdapter* SetLayoutAdapter(wxDialogLayoutAdapter* adapter);
  154. static wxDialogLayoutAdapter* GetLayoutAdapter() { return sm_layoutAdapter; }
  155. // Global switch for layout adaptation
  156. static bool IsLayoutAdaptationEnabled() { return sm_layoutAdaptation; }
  157. static void EnableLayoutAdaptation(bool enable) { sm_layoutAdaptation = enable; }
  158. // modality kind
  159. virtual wxDialogModality GetModality() const;
  160. protected:
  161. // emulate click of a button with the given id if it's present in the dialog
  162. //
  163. // return true if button was "clicked" or false if we don't have it
  164. bool EmulateButtonClickIfPresent(int id);
  165. // this function is used by OnCharHook() to decide whether the given key
  166. // should close the dialog
  167. //
  168. // for most platforms the default implementation (which just checks for
  169. // Esc) is sufficient, but Mac port also adds Cmd-. here and other ports
  170. // could do something different if needed
  171. virtual bool IsEscapeKey(const wxKeyEvent& event);
  172. // end either modal or modeless dialog, for the modal dialog rc is used as
  173. // the dialog return code
  174. void EndDialog(int rc);
  175. // call Validate() and TransferDataFromWindow() and close dialog with
  176. // wxID_OK return code
  177. void AcceptAndClose();
  178. // The return code from modal dialog
  179. int m_returnCode;
  180. // The identifier for the affirmative button (usually wxID_OK)
  181. int m_affirmativeId;
  182. // The identifier for cancel button (usually wxID_CANCEL)
  183. int m_escapeId;
  184. // Flags whether layout adaptation has been done for this dialog
  185. bool m_layoutAdaptationDone;
  186. // Extra button identifiers to be taken as 'main' button identifiers
  187. // to be placed in the non-scrolling area
  188. wxArrayInt m_mainButtonIds;
  189. // Adaptation level
  190. int m_layoutAdaptationLevel;
  191. // Local override for global adaptation enabled status
  192. wxDialogLayoutAdaptationMode m_layoutAdaptationMode;
  193. // Global layout adapter
  194. static wxDialogLayoutAdapter* sm_layoutAdapter;
  195. // Global adaptation switch
  196. static bool sm_layoutAdaptation;
  197. private:
  198. // helper of GetParentForModalDialog(): returns the passed in window if it
  199. // can be used as our parent or NULL if it can't
  200. wxWindow *CheckIfCanBeUsedAsParent(wxWindow *parent) const;
  201. // Helper of OnCharHook() and OnCloseWindow(): find the appropriate button
  202. // for closing the dialog and send a click event for it.
  203. //
  204. // Return true if we found a button to close the dialog and "clicked" it or
  205. // false otherwise.
  206. bool SendCloseButtonClickEvent();
  207. // handle Esc key presses
  208. void OnCharHook(wxKeyEvent& event);
  209. // handle closing the dialog window
  210. void OnCloseWindow(wxCloseEvent& event);
  211. // handle the standard buttons
  212. void OnButton(wxCommandEvent& event);
  213. // update the background colour
  214. void OnSysColourChanged(wxSysColourChangedEvent& event);
  215. wxDECLARE_NO_COPY_CLASS(wxDialogBase);
  216. DECLARE_EVENT_TABLE()
  217. };
  218. /*!
  219. * Base class for layout adapters - code that, for example, turns a dialog into a
  220. * scrolling dialog if there isn't enough screen space. You can derive further
  221. * adapter classes to do any other kind of adaptation, such as applying a watermark, or adding
  222. * a help mechanism.
  223. */
  224. class WXDLLIMPEXP_CORE wxDialogLayoutAdapter: public wxObject
  225. {
  226. DECLARE_CLASS(wxDialogLayoutAdapter)
  227. public:
  228. wxDialogLayoutAdapter() {}
  229. // Override this function to indicate that adaptation should be done
  230. virtual bool CanDoLayoutAdaptation(wxDialog* dialog) = 0;
  231. // Override this function to do the adaptation
  232. virtual bool DoLayoutAdaptation(wxDialog* dialog) = 0;
  233. };
  234. /*!
  235. * Standard adapter. Does scrolling adaptation for paged and regular dialogs.
  236. *
  237. */
  238. class WXDLLIMPEXP_CORE wxStandardDialogLayoutAdapter: public wxDialogLayoutAdapter
  239. {
  240. DECLARE_CLASS(wxStandardDialogLayoutAdapter)
  241. public:
  242. wxStandardDialogLayoutAdapter() {}
  243. // Overrides
  244. // Indicate that adaptation should be done
  245. virtual bool CanDoLayoutAdaptation(wxDialog* dialog);
  246. // Do layout adaptation
  247. virtual bool DoLayoutAdaptation(wxDialog* dialog);
  248. // Implementation
  249. // Create the scrolled window
  250. virtual wxScrolledWindow* CreateScrolledWindow(wxWindow* parent);
  251. #if wxUSE_BUTTON
  252. // Find a standard or horizontal box sizer
  253. virtual wxSizer* FindButtonSizer(bool stdButtonSizer, wxDialog* dialog, wxSizer* sizer, int& retBorder, int accumlatedBorder = 0);
  254. // Check if this sizer contains standard buttons, and so can be repositioned in the dialog
  255. virtual bool IsOrdinaryButtonSizer(wxDialog* dialog, wxBoxSizer* sizer);
  256. // Check if this is a standard button
  257. virtual bool IsStandardButton(wxDialog* dialog, wxButton* button);
  258. // Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
  259. virtual bool FindLooseButtons(wxDialog* dialog, wxStdDialogButtonSizer* buttonSizer, wxSizer* sizer, int& count);
  260. #endif // wxUSE_BUTTON
  261. // Reparent the controls to the scrolled window, except those in buttonSizer
  262. virtual void ReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer = NULL);
  263. static void DoReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer = NULL);
  264. // A function to fit the dialog around its contents, and then adjust for screen size.
  265. // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
  266. virtual bool FitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow);
  267. virtual bool FitWithScrolling(wxDialog* dialog, wxWindowList& windows);
  268. static bool DoFitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow);
  269. static bool DoFitWithScrolling(wxDialog* dialog, wxWindowList& windows);
  270. // Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
  271. virtual int MustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize);
  272. static int DoMustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize);
  273. };
  274. #if defined(__WXUNIVERSAL__) && !defined(__WXMICROWIN__)
  275. #include "wx/univ/dialog.h"
  276. #else
  277. #if defined(__WXMSW__)
  278. #include "wx/msw/dialog.h"
  279. #elif defined(__WXMOTIF__)
  280. #include "wx/motif/dialog.h"
  281. #elif defined(__WXGTK20__)
  282. #include "wx/gtk/dialog.h"
  283. #elif defined(__WXGTK__)
  284. #include "wx/gtk1/dialog.h"
  285. #elif defined(__WXMAC__)
  286. #include "wx/osx/dialog.h"
  287. #elif defined(__WXCOCOA__)
  288. #include "wx/cocoa/dialog.h"
  289. #elif defined(__WXPM__)
  290. #include "wx/os2/dialog.h"
  291. #endif
  292. #endif
  293. class WXDLLIMPEXP_CORE wxWindowModalDialogEvent : public wxCommandEvent
  294. {
  295. public:
  296. wxWindowModalDialogEvent (wxEventType commandType = wxEVT_NULL, int id = 0)
  297. : wxCommandEvent(commandType, id) { }
  298. wxDialog *GetDialog() const
  299. { return wxStaticCast(GetEventObject(), wxDialog); }
  300. int GetReturnCode() const
  301. { return GetDialog()->GetReturnCode(); }
  302. virtual wxEvent *Clone() const { return new wxWindowModalDialogEvent (*this); }
  303. private:
  304. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWindowModalDialogEvent )
  305. };
  306. wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE, wxEVT_WINDOW_MODAL_DIALOG_CLOSED , wxWindowModalDialogEvent );
  307. typedef void (wxEvtHandler::*wxWindowModalDialogEventFunction)(wxWindowModalDialogEvent &);
  308. #define wxWindowModalDialogEventHandler(func) \
  309. wxEVENT_HANDLER_CAST(wxWindowModalDialogEventFunction, func)
  310. #define EVT_WINDOW_MODAL_DIALOG_CLOSED(winid, func) \
  311. wx__DECLARE_EVT1(wxEVT_WINDOW_MODAL_DIALOG_CLOSED, winid, wxWindowModalDialogEventHandler(func))
  312. #ifdef wxHAS_EVENT_BIND
  313. template<typename Functor>
  314. class wxWindowModalDialogEventFunctor
  315. {
  316. public:
  317. wxWindowModalDialogEventFunctor(const Functor& f)
  318. : m_f(new Functor(f))
  319. {}
  320. void operator()(wxWindowModalDialogEvent& event)
  321. {
  322. if ( m_f )
  323. {
  324. // We only want to call this handler once. Also, by deleting
  325. // the functor here, its data (such as wxWindowPtr pointing to
  326. // the dialog) are freed immediately after exiting this operator().
  327. wxSharedPtr<Functor> functor(m_f);
  328. m_f.reset();
  329. (*functor)(event.GetReturnCode());
  330. }
  331. else // was already called once
  332. {
  333. event.Skip();
  334. }
  335. }
  336. private:
  337. wxSharedPtr<Functor> m_f;
  338. };
  339. template<typename Functor>
  340. void wxDialogBase::ShowWindowModalThenDo(const Functor& onEndModal)
  341. {
  342. Bind(wxEVT_WINDOW_MODAL_DIALOG_CLOSED,
  343. wxWindowModalDialogEventFunctor<Functor>(onEndModal));
  344. ShowWindowModal();
  345. }
  346. #endif // wxHAS_EVENT_BIND
  347. #endif
  348. // _WX_DIALOG_H_BASE_