compilation fixes for PCH-less build
[wxWidgets.git] / src / common / dlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dlgcmn.cpp
3 // Purpose: common (to all ports) wxDialog functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 28.06.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dialog.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
33 #include "wx/intl.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
36 #include "wx/sizer.h"
37 #include "wx/containr.h"
38 #endif
39
40 #include "wx/statline.h"
41 #include "wx/sysopt.h"
42 #include "wx/private/stattext.h"
43
44
45 // ----------------------------------------------------------------------------
46 // wxDialogBase
47 // ----------------------------------------------------------------------------
48
49 BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow)
50 EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton)
51
52 EVT_CLOSE(wxDialogBase::OnCloseWindow)
53
54 EVT_CHAR_HOOK(wxDialogBase::OnCharHook)
55
56 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase)
57 END_EVENT_TABLE()
58
59 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase, wxTopLevelWindow)
60
61 void wxDialogBase::Init()
62 {
63 m_returnCode = 0;
64 m_affirmativeId = wxID_OK;
65 m_escapeId = wxID_ANY;
66
67 // the dialogs have this flag on by default to prevent the events from the
68 // dialog controls from reaching the parent frame which is usually
69 // undesirable and can lead to unexpected and hard to find bugs
70 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS);
71
72 WX_INIT_CONTROL_CONTAINER();
73 }
74
75 wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const
76 {
77 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
78 // in an unfocused dialog, so try to find a valid parent for it:
79 if ( parent )
80 parent = wxGetTopLevelParent(parent);
81
82 if ( !parent || parent->HasExtraStyle(wxWS_EX_TRANSIENT) )
83 parent = wxTheApp->GetTopWindow();
84
85 if ( parent && parent->HasExtraStyle(wxWS_EX_TRANSIENT) )
86 {
87 // can't use this one, it's going to disappear
88 parent = NULL;
89 }
90
91 return parent;
92 }
93
94 #if wxUSE_STATTEXT
95
96 class wxTextSizerWrapper : public wxTextWrapper
97 {
98 public:
99 wxTextSizerWrapper(wxWindow *win)
100 {
101 m_win = win;
102 m_hLine = 0;
103 }
104
105 wxSizer *CreateSizer(const wxString& text, int widthMax)
106 {
107 m_sizer = new wxBoxSizer(wxVERTICAL);
108 Wrap(m_win, text, widthMax);
109 return m_sizer;
110 }
111
112 protected:
113 virtual void OnOutputLine(const wxString& line)
114 {
115 if ( !line.empty() )
116 {
117 m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line));
118 }
119 else // empty line, no need to create a control for it
120 {
121 if ( !m_hLine )
122 m_hLine = m_win->GetCharHeight();
123
124 m_sizer->Add(5, m_hLine);
125 }
126 }
127
128 private:
129 wxWindow *m_win;
130 wxSizer *m_sizer;
131 int m_hLine;
132 };
133
134 wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
135 {
136 // I admit that this is complete bogus, but it makes
137 // message boxes work for pda screens temporarily..
138 int widthMax = -1;
139 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
140 if (is_pda)
141 {
142 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
143 }
144
145 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
146 // the static messages created by CreateTextSizer() (used by wxMessageBox,
147 // for example), we don't want this special meaning, so we need to quote it
148 wxString text(message);
149 text.Replace(_T("&"), _T("&&"));
150
151 wxTextSizerWrapper wrapper(this);
152
153 return wrapper.CreateSizer(text, widthMax);
154 }
155
156 #endif // wxUSE_STATTEXT
157
158 wxSizer *wxDialogBase::CreateButtonSizer(long flags)
159 {
160 wxSizer *sizer = NULL;
161
162 #ifdef __SMARTPHONE__
163 wxDialog* dialog = (wxDialog*) this;
164 if ( flags & wxOK )
165 dialog->SetLeftMenu(wxID_OK);
166
167 if ( flags & wxCANCEL )
168 dialog->SetRightMenu(wxID_CANCEL);
169
170 if ( flags & wxYES )
171 dialog->SetLeftMenu(wxID_YES);
172
173 if ( flags & wxNO )
174 dialog->SetRightMenu(wxID_NO);
175 #else // !__SMARTPHONE__
176
177 #if wxUSE_BUTTON
178
179 #ifdef __POCKETPC__
180 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
181 // located inside caption bar and implement Cancel functionality through
182 // Undo outside dialog. As native behaviour this will be default here but
183 // can be replaced with real wxButtons by setting the option below to 1
184 if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK ||
185 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
186 #endif // __POCKETPC__
187 {
188 sizer = CreateStdDialogButtonSizer(flags);
189 }
190 #endif // wxUSE_BUTTON
191
192 #endif // __SMARTPHONE__/!__SMARTPHONE__
193
194 return sizer;
195 }
196
197 wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags)
198 {
199 wxSizer *sizer = CreateButtonSizer(flags);
200 if ( !sizer )
201 return NULL;
202
203 // Mac Human Interface Guidelines recommend not to use static lines as
204 // grouping elements
205 #if wxUSE_STATLINE && !defined(__WXMAC__)
206 wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL);
207 topsizer->Add(new wxStaticLine(this),
208 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM));
209 topsizer->Add(sizer, wxSizerFlags().Expand());
210 sizer = topsizer;
211 #endif // wxUSE_STATLINE
212
213 return sizer;
214 }
215
216 #if wxUSE_BUTTON
217
218 wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
219 {
220 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
221
222 wxButton *ok = NULL;
223 wxButton *yes = NULL;
224 wxButton *no = NULL;
225
226 if (flags & wxOK)
227 {
228 ok = new wxButton(this, wxID_OK);
229 sizer->AddButton(ok);
230 }
231
232 if (flags & wxCANCEL)
233 {
234 wxButton *cancel = new wxButton(this, wxID_CANCEL);
235 sizer->AddButton(cancel);
236 }
237
238 if (flags & wxYES)
239 {
240 yes = new wxButton(this, wxID_YES);
241 sizer->AddButton(yes);
242 }
243
244 if (flags & wxNO)
245 {
246 no = new wxButton(this, wxID_NO);
247 sizer->AddButton(no);
248 }
249
250 if (flags & wxAPPLY)
251 {
252 wxButton *apply = new wxButton(this, wxID_APPLY);
253 sizer->AddButton(apply);
254 }
255
256 if (flags & wxCLOSE)
257 {
258 wxButton *close = new wxButton(this, wxID_CLOSE);
259 sizer->AddButton(close);
260 }
261
262 if (flags & wxHELP)
263 {
264 wxButton *help = new wxButton(this, wxID_HELP);
265 sizer->AddButton(help);
266 }
267
268 if (flags & wxNO_DEFAULT)
269 {
270 if (no)
271 {
272 no->SetDefault();
273 no->SetFocus();
274 }
275 }
276 else
277 {
278 if (ok)
279 {
280 ok->SetDefault();
281 ok->SetFocus();
282 }
283 else if (yes)
284 {
285 yes->SetDefault();
286 yes->SetFocus();
287 }
288 }
289
290 if (flags & wxOK)
291 SetAffirmativeId(wxID_OK);
292 else if (flags & wxYES)
293 SetAffirmativeId(wxID_YES);
294
295 sizer->Realize();
296
297 return sizer;
298 }
299
300 #endif // wxUSE_BUTTON
301
302 // ----------------------------------------------------------------------------
303 // standard buttons handling
304 // ----------------------------------------------------------------------------
305
306 void wxDialogBase::EndDialog(int rc)
307 {
308 if ( IsModal() )
309 EndModal(rc);
310 else
311 Hide();
312 }
313
314 void wxDialogBase::AcceptAndClose()
315 {
316 if ( Validate() && TransferDataFromWindow() )
317 {
318 EndDialog(m_affirmativeId);
319 }
320 }
321
322 void wxDialogBase::SetAffirmativeId(int affirmativeId)
323 {
324 m_affirmativeId = affirmativeId;
325 }
326
327 void wxDialogBase::SetEscapeId(int escapeId)
328 {
329 m_escapeId = escapeId;
330 }
331
332 bool wxDialogBase::EmulateButtonClickIfPresent(int id)
333 {
334 #if wxUSE_BUTTON
335 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
336
337 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
338 return false;
339
340 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id);
341 event.SetEventObject(btn);
342 btn->GetEventHandler()->ProcessEvent(event);
343
344 return true;
345 #else // !wxUSE_BUTTON
346 wxUnusedVar(id);
347 return false;
348 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
349 }
350
351 bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event)
352 {
353 // for most platforms, Esc key is used to close the dialogs
354 return event.GetKeyCode() == WXK_ESCAPE &&
355 event.GetModifiers() == wxMOD_NONE;
356 }
357
358 void wxDialogBase::OnCharHook(wxKeyEvent& event)
359 {
360 if ( event.GetKeyCode() == WXK_ESCAPE )
361 {
362 int idCancel = GetEscapeId();
363 switch ( idCancel )
364 {
365 case wxID_NONE:
366 // don't handle Esc specially at all
367 break;
368
369 case wxID_ANY:
370 // this value is special: it means translate Esc to wxID_CANCEL
371 // but if there is no such button, then fall back to wxID_OK
372 if ( EmulateButtonClickIfPresent(wxID_CANCEL) )
373 return;
374 idCancel = GetAffirmativeId();
375 // fall through
376
377 default:
378 // translate Esc to button press for the button with given id
379 if ( EmulateButtonClickIfPresent(idCancel) )
380 return;
381 }
382 }
383
384 event.Skip();
385 }
386
387 void wxDialogBase::OnButton(wxCommandEvent& event)
388 {
389 const int id = event.GetId();
390 if ( id == GetAffirmativeId() )
391 {
392 AcceptAndClose();
393 }
394 else if ( id == wxID_APPLY )
395 {
396 if ( Validate() )
397 TransferDataFromWindow();
398
399 // TODO: disable the Apply button until things change again
400 }
401 else if ( id == GetEscapeId() ||
402 (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
403 {
404 EndDialog(wxID_CANCEL);
405 }
406 else // not a standard button
407 {
408 event.Skip();
409 }
410 }
411
412 // ----------------------------------------------------------------------------
413 // other event handlers
414 // ----------------------------------------------------------------------------
415
416 void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
417 {
418 // We'll send a Cancel message by default, which may close the dialog.
419 // Check for looping if the Cancel event handler calls Close().
420
421 // Note that if a cancel button and handler aren't present in the dialog,
422 // nothing will happen when you close the dialog via the window manager, or
423 // via Close(). We wouldn't want to destroy the dialog by default, since
424 // the dialog may have been created on the stack. However, this does mean
425 // that calling dialog->Close() won't delete the dialog unless the handler
426 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
427 // destroy the dialog. The default OnCancel (above) simply ends a modal
428 // dialog, and hides a modeless dialog.
429
430 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
431 // lists here? don't dare to change it now, but should be done later!
432 static wxList closing;
433
434 if ( closing.Member(this) )
435 return;
436
437 closing.Append(this);
438
439 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
440 cancelEvent.SetEventObject( this );
441 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
442
443 closing.DeleteObject(this);
444 }
445
446 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
447 {
448 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
449 Refresh();
450 }