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