1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialogBase class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DIALOG_H_BASE_
12 #define _WX_DIALOG_H_BASE_
14 #include "wx/toplevel.h"
15 #include "wx/containr.h"
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
;
26 // Also see the bit summary table in wx/toplevel.h.
28 #define wxDIALOG_NO_PARENT 0x00000020 // Don't make owned by apps top window
31 #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxMAXIMIZE | wxCLOSE_BOX | wxNO_BORDER)
33 #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX)
36 // Layout adaptation levels, for SetLayoutAdaptationLevel
38 // Don't do any layout adaptation
39 #define wxDIALOG_ADAPTATION_NONE 0
41 // Only look for wxStdDialogButtonSizer for non-scrolling part
42 #define wxDIALOG_ADAPTATION_STANDARD_SIZER 1
44 // Also look for any suitable sizer for non-scrolling part
45 #define wxDIALOG_ADAPTATION_ANY_SIZER 2
47 // Also look for 'loose' standard buttons for non-scrolling part
48 #define wxDIALOG_ADAPTATION_LOOSE_BUTTONS 3
50 // Layout adaptation mode, for SetLayoutAdaptationMode
51 enum wxDialogLayoutAdaptationMode
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
60 wxDIALOG_MODALITY_NONE
= 0,
61 wxDIALOG_MODALITY_WINDOW_MODAL
= 1,
62 wxDIALOG_MODALITY_APP_MODAL
= 2
65 extern WXDLLIMPEXP_DATA_CORE(const char) wxDialogNameStr
[];
67 class WXDLLIMPEXP_CORE wxDialogBase
: public wxNavigationEnabled
<wxTopLevelWindow
>
71 virtual ~wxDialogBase() { }
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
);
82 #ifdef wxHAS_EVENT_BIND
83 template<typename Functor
>
84 void ShowWindowModalThenDo(const Functor
& onEndModal
);
85 #endif // wxHAS_EVENT_BIND
87 // Modal dialogs have a return code - usually the id of the last
89 void SetReturnCode(int returnCode
) { m_returnCode
= returnCode
; }
90 int GetReturnCode() const { return m_returnCode
; }
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
; }
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
; }
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.
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.
112 // This function always returns a valid top level window or NULL.
113 wxWindow
*GetParentForModalDialog(wxWindow
*parent
, long style
) const;
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
119 return GetParentForModalDialog(GetParent(), GetWindowStyle());
122 #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
123 // splits text up at newlines and places the lines into a vertical
125 wxSizer
*CreateTextSizer( const wxString
& message
);
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
133 // returns a horizontal wxBoxSizer containing the given buttons
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
);
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
);
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)
148 // this is just a combination of CreateButtonSizer() and
149 // CreateSeparatedSizer()
150 wxSizer
*CreateSeparatedButtonSizer(long flags
);
153 wxStdDialogButtonSizer
*CreateStdDialogButtonSizer( long flags
);
154 #endif // wxUSE_BUTTON
156 // Do layout adaptation
157 virtual bool DoLayoutAdaptation();
159 // Can we do layout adaptation?
160 virtual bool CanDoLayoutAdaptation();
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
; }
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
; }
170 // Is this id in the main button id array?
171 bool IsMainButtonId(wxWindowID id
) const { return (m_mainButtonIds
.Index((int) id
) != wxNOT_FOUND
); }
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
177 void SetLayoutAdaptationLevel(int level
) { m_layoutAdaptationLevel
= level
; }
178 int GetLayoutAdaptationLevel() const { return m_layoutAdaptationLevel
; }
180 /// Override global adaptation enabled/disabled status
181 void SetLayoutAdaptationMode(wxDialogLayoutAdaptationMode mode
) { m_layoutAdaptationMode
= mode
; }
182 wxDialogLayoutAdaptationMode
GetLayoutAdaptationMode() const { return m_layoutAdaptationMode
; }
184 // Returns true if the adaptation has been done
185 void SetLayoutAdaptationDone(bool adaptationDone
) { m_layoutAdaptationDone
= adaptationDone
; }
186 bool GetLayoutAdaptationDone() const { return m_layoutAdaptationDone
; }
188 // Set layout adapter class, returning old adapter
189 static wxDialogLayoutAdapter
* SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
);
190 static wxDialogLayoutAdapter
* GetLayoutAdapter() { return sm_layoutAdapter
; }
192 // Global switch for layout adaptation
193 static bool IsLayoutAdaptationEnabled() { return sm_layoutAdaptation
; }
194 static void EnableLayoutAdaptation(bool enable
) { sm_layoutAdaptation
= enable
; }
197 virtual wxDialogModality
GetModality() const;
199 // emulate click of a button with the given id if it's present in the dialog
201 // return true if button was "clicked" or false if we don't have it
202 bool EmulateButtonClickIfPresent(int id
);
204 // this function is used by OnCharHook() to decide whether the given key
205 // should close the dialog
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
);
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
);
216 // call Validate() and TransferDataFromWindow() and close dialog with
217 // wxID_OK return code
218 void AcceptAndClose();
220 // The return code from modal dialog
223 // The identifier for the affirmative button (usually wxID_OK)
226 // The identifier for cancel button (usually wxID_CANCEL)
229 // Flags whether layout adaptation has been done for this dialog
230 bool m_layoutAdaptationDone
;
232 // Extra button identifiers to be taken as 'main' button identifiers
233 // to be placed in the non-scrolling area
234 wxArrayInt m_mainButtonIds
;
237 int m_layoutAdaptationLevel
;
239 // Local override for global adaptation enabled status
240 wxDialogLayoutAdaptationMode m_layoutAdaptationMode
;
242 // Global layout adapter
243 static wxDialogLayoutAdapter
* sm_layoutAdapter
;
245 // Global adaptation switch
246 static bool sm_layoutAdaptation
;
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;
253 // Helper of OnCharHook() and OnCloseWindow(): find the appropriate button
254 // for closing the dialog and send a click event for it.
256 // Return true if we found a button to close the dialog and "clicked" it or
258 bool SendCloseButtonClickEvent();
260 // handle Esc key presses
261 void OnCharHook(wxKeyEvent
& event
);
263 // handle closing the dialog window
264 void OnCloseWindow(wxCloseEvent
& event
);
266 // handle the standard buttons
267 void OnButton(wxCommandEvent
& event
);
269 // update the background colour
270 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
273 wxDECLARE_NO_COPY_CLASS(wxDialogBase
);
274 DECLARE_EVENT_TABLE()
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
284 class WXDLLIMPEXP_CORE wxDialogLayoutAdapter
: public wxObject
286 DECLARE_CLASS(wxDialogLayoutAdapter
)
288 wxDialogLayoutAdapter() {}
290 // Override this function to indicate that adaptation should be done
291 virtual bool CanDoLayoutAdaptation(wxDialog
* dialog
) = 0;
293 // Override this function to do the adaptation
294 virtual bool DoLayoutAdaptation(wxDialog
* dialog
) = 0;
298 * Standard adapter. Does scrolling adaptation for paged and regular dialogs.
302 class WXDLLIMPEXP_CORE wxStandardDialogLayoutAdapter
: public wxDialogLayoutAdapter
304 DECLARE_CLASS(wxStandardDialogLayoutAdapter
)
306 wxStandardDialogLayoutAdapter() {}
310 // Indicate that adaptation should be done
311 virtual bool CanDoLayoutAdaptation(wxDialog
* dialog
);
313 // Do layout adaptation
314 virtual bool DoLayoutAdaptation(wxDialog
* dialog
);
318 // Create the scrolled window
319 virtual wxScrolledWindow
* CreateScrolledWindow(wxWindow
* parent
);
322 // Find a standard or horizontal box sizer
323 virtual wxSizer
* FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
= 0);
325 // Check if this sizer contains standard buttons, and so can be repositioned in the dialog
326 virtual bool IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
);
328 // Check if this is a standard button
329 virtual bool IsStandardButton(wxDialog
* dialog
, wxButton
* button
);
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
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
);
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
);
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
);
351 #if defined(__WXUNIVERSAL__) && !defined(__WXMICROWIN__)
352 #include "wx/univ/dialog.h"
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"
371 class WXDLLIMPEXP_CORE wxWindowModalDialogEvent
: public wxCommandEvent
374 wxWindowModalDialogEvent (wxEventType commandType
= wxEVT_NULL
, int id
= 0)
375 : wxCommandEvent(commandType
, id
) { }
377 wxDialog
*GetDialog() const
378 { return wxStaticCast(GetEventObject(), wxDialog
); }
380 int GetReturnCode() const
381 { return GetDialog()->GetReturnCode(); }
383 virtual wxEvent
*Clone() const { return new wxWindowModalDialogEvent (*this); }
386 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxWindowModalDialogEvent
)
389 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CORE
, wxEVT_WINDOW_MODAL_DIALOG_CLOSED
, wxWindowModalDialogEvent
);
391 typedef void (wxEvtHandler::*wxWindowModalDialogEventFunction
)(wxWindowModalDialogEvent
&);
393 #define wxWindowModalDialogEventHandler(func) \
394 wxEVENT_HANDLER_CAST(wxWindowModalDialogEventFunction, func)
396 #define EVT_WINDOW_MODAL_DIALOG_CLOSED(winid, func) \
397 wx__DECLARE_EVT1(wxEVT_WINDOW_MODAL_DIALOG_CLOSED, winid, wxWindowModalDialogEventHandler(func))
399 #ifdef wxHAS_EVENT_BIND
400 template<typename Functor
>
401 class wxWindowModalDialogEventFunctor
404 wxWindowModalDialogEventFunctor(const Functor
& f
)
405 : m_f(f
), m_wasCalled(false)
408 void operator()(wxWindowModalDialogEvent
& event
)
417 m_f(event
.GetReturnCode());
425 template<typename Functor
>
426 void wxDialogBase::ShowWindowModalThenDo(const Functor
& onEndModal
)
428 Bind(wxEVT_WINDOW_MODAL_DIALOG_CLOSED
,
429 wxWindowModalDialogEventFunctor
<Functor
>(onEndModal
));
432 #endif // wxHAS_EVENT_BIND
435 // _WX_DIALOG_H_BASE_