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