1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialogBase class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DIALOG_H_BASE_
13 #define _WX_DIALOG_H_BASE_
16 #include "wx/toplevel.h"
18 class WXDLLIMPEXP_FWD_CORE wxSizer
;
19 class WXDLLIMPEXP_FWD_CORE wxStdDialogButtonSizer
;
20 class WXDLLIMPEXP_FWD_CORE wxBoxSizer
;
21 class WXDLLIMPEXP_FWD_CORE wxDialogLayoutAdapter
;
22 class WXDLLIMPEXP_FWD_CORE wxDialog
;
23 class WXDLLIMPEXP_FWD_CORE wxButton
;
24 class WXDLLIMPEXP_FWD_CORE wxScrolledWindow
;
26 #define wxDIALOG_NO_PARENT 0x0001 // Don't make owned by apps top window
29 #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxMAXIMIZE | wxCLOSE_BOX | wxNO_BORDER)
31 #define wxDEFAULT_DIALOG_STYLE (wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX)
34 // Layout adaptation levels, for SetLayoutAdaptationLevel
36 // Don't do any layout adaptation
37 #define wxDIALOG_ADAPTATION_NONE 0
39 // Only look for wxStdDialogButtonSizer for non-scrolling part
40 #define wxDIALOG_ADAPTATION_STANDARD_SIZER 1
42 // Also look for any suitable sizer for non-scrolling part
43 #define wxDIALOG_ADAPTATION_ANY_SIZER 2
45 // Also look for 'loose' standard buttons for non-scrolling part
46 #define wxDIALOG_ADAPTATION_LOOSE_BUTTONS 3
48 // Layout adaptation mode, for SetLayoutAdaptationMode
49 enum wxDialogLayoutAdaptationMode
51 wxDIALOG_ADAPTATION_MODE_DEFAULT
= 0, // use global adaptation enabled status
52 wxDIALOG_ADAPTATION_MODE_ENABLED
= 1, // enable this dialog overriding global status
53 wxDIALOG_ADAPTATION_MODE_DISABLED
= 2 // disable this dialog overriding global status
56 extern WXDLLIMPEXP_DATA_CORE(const char) wxDialogNameStr
[];
58 class WXDLLIMPEXP_CORE wxDialogBase
: public wxTopLevelWindow
63 // all flags allowed in wxDialogBase::CreateButtonSizer()
64 ButtonSizerFlags
= wxOK
|wxCANCEL
|wxYES
|wxNO
|wxHELP
|wxNO_DEFAULT
67 wxDialogBase() { Init(); }
68 virtual ~wxDialogBase() { }
70 // define public wxDialog methods to be implemented by the derived classes
71 virtual int ShowModal() = 0;
72 virtual void EndModal(int retCode
) = 0;
73 virtual bool IsModal() const = 0;
76 // Modal dialogs have a return code - usually the id of the last
78 void SetReturnCode(int returnCode
) { m_returnCode
= returnCode
; }
79 int GetReturnCode() const { return m_returnCode
; }
81 // Set the identifier for the affirmative button: this button will close
82 // the dialog after validating data and calling TransferDataFromWindow()
83 void SetAffirmativeId(int affirmativeId
);
84 int GetAffirmativeId() const { return m_affirmativeId
; }
86 // Set identifier for Esc key translation: the button with this id will
87 // close the dialog without doing anything else; special value wxID_NONE
88 // means to not handle Esc at all while wxID_ANY means to map Esc to
89 // wxID_CANCEL if present and GetAffirmativeId() otherwise
90 void SetEscapeId(int escapeId
);
91 int GetEscapeId() const { return m_escapeId
; }
93 // Find the parent to use for modal dialog: try to use the specified parent
94 // but fall back to the current active window or main application window as
95 // last resort if it is unsuitable.
97 // This function always returns a valid top level window or NULL.
98 wxWindow
*GetParentForModalDialog(wxWindow
*parent
= NULL
) const;
100 #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
101 // splits text up at newlines and places the
102 // lines into a vertical wxBoxSizer
103 wxSizer
*CreateTextSizer( const wxString
&message
);
104 #endif // wxUSE_STATTEXT // && wxUSE_TEXTCTRL
106 // returns a horizontal wxBoxSizer containing the given buttons
108 // notice that the returned sizer can be NULL if no buttons are put in the
109 // sizer (this mostly happens under smart phones and other atypical
110 // platforms which have hardware buttons replacing OK/Cancel and such)
111 wxSizer
*CreateButtonSizer(long flags
);
113 // returns the sizer containing CreateButtonSizer() below a separating
114 // static line for the platforms which use static lines for items
115 // separation (i.e. not Mac)
116 wxSizer
*CreateSeparatedButtonSizer(long flags
);
119 wxStdDialogButtonSizer
*CreateStdDialogButtonSizer( long flags
);
120 #endif // wxUSE_BUTTON
122 // Do layout adaptation
123 virtual bool DoLayoutAdaptation();
125 // Can we do layout adaptation?
126 virtual bool CanDoLayoutAdaptation();
128 // Returns a content window if there is one. This can be used by the layout adapter, for
129 // example to make the pages of a book control into scrolling windows
130 virtual wxWindow
* GetContentWindow() const { return NULL
; }
132 // Add an id to the list of main button identifiers that should be in the button sizer
133 void AddMainButtonId(wxWindowID id
) { m_mainButtonIds
.Add((int) id
); }
134 wxArrayInt
& GetMainButtonIds() { return m_mainButtonIds
; }
136 // Is this id in the main button id array?
137 bool IsMainButtonId(wxWindowID id
) const { return (m_mainButtonIds
.Index((int) id
) != wxNOT_FOUND
); }
139 // Level of adaptation, from none (Level 0) to full (Level 3). To disable adaptation,
140 // set level 0, for example in your dialog constructor. You might
141 // do this if you know that you are displaying on a large screen and you don't want the
143 void SetLayoutAdaptationLevel(int level
) { m_layoutAdaptationLevel
= level
; }
144 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
; }
150 // Returns true if the adaptation has been done
151 void SetLayoutAdaptationDone(bool adaptationDone
) { m_layoutAdaptationDone
= adaptationDone
; }
152 bool GetLayoutAdaptationDone() const { return m_layoutAdaptationDone
; }
154 // Set layout adapter class, returning old adapter
155 static wxDialogLayoutAdapter
* SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
);
156 static wxDialogLayoutAdapter
* GetLayoutAdapter() { return sm_layoutAdapter
; }
158 // Global switch for layout adaptation
159 static bool IsLayoutAdaptationEnabled() { return sm_layoutAdaptation
; }
160 static void EnableLayoutAdaptation(bool enable
) { sm_layoutAdaptation
= enable
; }
163 // emulate click of a button with the given id if it's present in the dialog
165 // return true if button was "clicked" or false if we don't have it
166 bool EmulateButtonClickIfPresent(int id
);
168 // this function is used by OnCharHook() to decide whether the given key
169 // should close the dialog
171 // for most platforms the default implementation (which just checks for
172 // Esc) is sufficient, but Mac port also adds Cmd-. here and other ports
173 // could do something different if needed
174 virtual bool IsEscapeKey(const wxKeyEvent
& event
);
176 // end either modal or modeless dialog, for the modal dialog rc is used as
177 // the dialog return code
178 void EndDialog(int rc
);
180 // call Validate() and TransferDataFromWindow() and close dialog with
181 // wxID_OK return code
182 void AcceptAndClose();
185 // The return code from modal dialog
188 // The identifier for the affirmative button (usually wxID_OK)
191 // The identifier for cancel button (usually wxID_CANCEL)
194 // Flags whether layout adaptation has been done for this dialog
195 bool m_layoutAdaptationDone
;
197 // Extra button identifiers to be taken as 'main' button identifiers
198 // to be placed in the non-scrolling area
199 wxArrayInt m_mainButtonIds
;
202 int m_layoutAdaptationLevel
;
204 // Local override for global adaptation enabled status
205 wxDialogLayoutAdaptationMode m_layoutAdaptationMode
;
207 // Global layout adapter
208 static wxDialogLayoutAdapter
* sm_layoutAdapter
;
210 // Global adaptation switch
211 static bool sm_layoutAdaptation
;
214 // common part of all ctors
217 // helper of GetParentForModalDialog(): returns the passed in window if it
218 // can be used as our parent or NULL if it can't
219 wxWindow
*CheckIfCanBeUsedAsParent(wxWindow
*parent
) const;
221 // handle Esc key presses
222 void OnCharHook(wxKeyEvent
& event
);
224 // handle closing the dialog window
225 void OnCloseWindow(wxCloseEvent
& event
);
227 // handle the standard buttons
228 void OnButton(wxCommandEvent
& event
);
230 // update the background colour
231 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
234 wxDECLARE_NO_COPY_CLASS(wxDialogBase
);
235 DECLARE_EVENT_TABLE()
239 * Base class for layout adapters - code that, for example, turns a dialog into a
240 * scrolling dialog if there isn't enough screen space. You can derive further
241 * adapter classes to do any other kind of adaptation, such as applying a watermark, or adding
245 class WXDLLIMPEXP_CORE wxDialogLayoutAdapter
: public wxObject
247 DECLARE_CLASS(wxDialogLayoutAdapter
)
249 wxDialogLayoutAdapter() {}
251 // Override this function to indicate that adaptation should be done
252 virtual bool CanDoLayoutAdaptation(wxDialog
* dialog
) = 0;
254 // Override this function to do the adaptation
255 virtual bool DoLayoutAdaptation(wxDialog
* dialog
) = 0;
259 * Standard adapter. Does scrolling adaptation for paged and regular dialogs.
263 class WXDLLIMPEXP_CORE wxStandardDialogLayoutAdapter
: public wxDialogLayoutAdapter
265 DECLARE_CLASS(wxStandardDialogLayoutAdapter
)
267 wxStandardDialogLayoutAdapter() {}
271 // Indicate that adaptation should be done
272 virtual bool CanDoLayoutAdaptation(wxDialog
* dialog
);
274 // Do layout adaptation
275 virtual bool DoLayoutAdaptation(wxDialog
* dialog
);
279 // Create the scrolled window
280 virtual wxScrolledWindow
* CreateScrolledWindow(wxWindow
* parent
);
282 // Find a standard or horizontal box sizer
283 virtual wxSizer
* FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
= 0);
285 // Check if this sizer contains standard buttons, and so can be repositioned in the dialog
286 virtual bool IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
);
288 // Check if this is a standard button
289 virtual bool IsStandardButton(wxDialog
* dialog
, wxButton
* button
);
291 // Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
292 virtual bool FindLooseButtons(wxDialog
* dialog
, wxStdDialogButtonSizer
* buttonSizer
, wxSizer
* sizer
, int& count
);
294 // Reparent the controls to the scrolled window, except those in buttonSizer
295 virtual void ReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
= NULL
);
296 static void DoReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
= NULL
);
298 // A function to fit the dialog around its contents, and then adjust for screen size.
299 // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
300 virtual bool FitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
);
301 virtual bool FitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
);
302 static bool DoFitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
);
303 static bool DoFitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
);
305 // Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
306 virtual int MustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
);
307 static int DoMustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
);
310 #if defined(__WXUNIVERSAL__) && !defined(__WXMICROWIN__)
311 #include "wx/univ/dialog.h"
313 #if defined(__WXPALMOS__)
314 #include "wx/palmos/dialog.h"
315 #elif defined(__WXMSW__)
316 #include "wx/msw/dialog.h"
317 #elif defined(__WXMOTIF__)
318 #include "wx/motif/dialog.h"
319 #elif defined(__WXGTK20__)
320 #include "wx/gtk/dialog.h"
321 #elif defined(__WXGTK__)
322 #include "wx/gtk1/dialog.h"
323 #elif defined(__WXMAC__)
324 #include "wx/osx/dialog.h"
325 #elif defined(__WXCOCOA__)
326 #include "wx/cocoa/dialog.h"
327 #elif defined(__WXPM__)
328 #include "wx/os2/dialog.h"
333 // _WX_DIALOG_H_BASE_