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