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