added wxAPPLY and wxCLOSE flags to CreateStdDialogButtonSizer() (Marcin Wojdyr)
[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 & wxAPPLY)
231 {
232 wxButton *apply = new wxButton(this, wxID_APPLY);
233 sizer->AddButton(apply);
234 }
235
236 if (flags & wxCLOSE)
237 {
238 wxButton *close = new wxButton(this, wxID_CLOSE);
239 sizer->AddButton(close);
240 }
241
242 if (flags & wxHELP)
243 {
244 wxButton *help = new wxButton(this, wxID_HELP);
245 sizer->AddButton(help);
246 }
247
248 if (flags & wxNO_DEFAULT)
249 {
250 if (no)
251 {
252 no->SetDefault();
253 no->SetFocus();
254 }
255 }
256 else
257 {
258 if (ok)
259 {
260 ok->SetDefault();
261 ok->SetFocus();
262 }
263 else if (yes)
264 {
265 yes->SetDefault();
266 yes->SetFocus();
267 }
268 }
269
270 if (flags & wxOK)
271 SetAffirmativeId(wxID_OK);
272 else if (flags & wxYES)
273 SetAffirmativeId(wxID_YES);
274
275 sizer->Realize();
276
277 return sizer;
278 }
279
280 #endif // wxUSE_BUTTON
281
282 // ----------------------------------------------------------------------------
283 // standard buttons handling
284 // ----------------------------------------------------------------------------
285
286 void wxDialogBase::EndDialog(int rc)
287 {
288 if ( IsModal() )
289 EndModal(rc);
290 else
291 Hide();
292 }
293
294 void wxDialogBase::AcceptAndClose()
295 {
296 if ( Validate() && TransferDataFromWindow() )
297 {
298 EndDialog(m_affirmativeId);
299 }
300 }
301
302 void wxDialogBase::SetAffirmativeId(int affirmativeId)
303 {
304 m_affirmativeId = affirmativeId;
305 }
306
307 void wxDialogBase::SetEscapeId(int escapeId)
308 {
309 m_escapeId = escapeId;
310 }
311
312 bool wxDialogBase::EmulateButtonClickIfPresent(int id)
313 {
314 #if wxUSE_BUTTON
315 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
316
317 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
318 return false;
319
320 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id);
321 event.SetEventObject(btn);
322 btn->GetEventHandler()->ProcessEvent(event);
323
324 return true;
325 #else // !wxUSE_BUTTON
326 wxUnusedVar(id);
327 return false;
328 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
329 }
330
331 bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event)
332 {
333 // for most platforms, Esc key is used to close the dialogs
334 return event.GetKeyCode() == WXK_ESCAPE &&
335 event.GetModifiers() == wxMOD_NONE;
336 }
337
338 void wxDialogBase::OnCharHook(wxKeyEvent& event)
339 {
340 if ( event.GetKeyCode() == WXK_ESCAPE )
341 {
342 int idCancel = GetEscapeId();
343 switch ( idCancel )
344 {
345 case wxID_NONE:
346 // don't handle Esc specially at all
347 break;
348
349 case wxID_ANY:
350 // this value is special: it means translate Esc to wxID_CANCEL
351 // but if there is no such button, then fall back to wxID_OK
352 if ( EmulateButtonClickIfPresent(wxID_CANCEL) )
353 return;
354 idCancel = GetAffirmativeId();
355 // fall through
356
357 default:
358 // translate Esc to button press for the button with given id
359 if ( EmulateButtonClickIfPresent(idCancel) )
360 return;
361 }
362 }
363
364 event.Skip();
365 }
366
367 void wxDialogBase::OnButton(wxCommandEvent& event)
368 {
369 const int id = event.GetId();
370 if ( id == GetAffirmativeId() )
371 {
372 AcceptAndClose();
373 }
374 else if ( id == wxID_APPLY )
375 {
376 if ( Validate() )
377 TransferDataFromWindow();
378
379 // TODO: disable the Apply button until things change again
380 }
381 else if ( id == GetEscapeId() ||
382 (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
383 {
384 EndDialog(wxID_CANCEL);
385 }
386 else // not a standard button
387 {
388 event.Skip();
389 }
390 }
391
392 // ----------------------------------------------------------------------------
393 // other event handlers
394 // ----------------------------------------------------------------------------
395
396 void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
397 {
398 // We'll send a Cancel message by default, which may close the dialog.
399 // Check for looping if the Cancel event handler calls Close().
400
401 // Note that if a cancel button and handler aren't present in the dialog,
402 // nothing will happen when you close the dialog via the window manager, or
403 // via Close(). We wouldn't want to destroy the dialog by default, since
404 // the dialog may have been created on the stack. However, this does mean
405 // that calling dialog->Close() won't delete the dialog unless the handler
406 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
407 // destroy the dialog. The default OnCancel (above) simply ends a modal
408 // dialog, and hides a modeless dialog.
409
410 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
411 // lists here? don't dare to change it now, but should be done later!
412 static wxList closing;
413
414 if ( closing.Member(this) )
415 return;
416
417 closing.Append(this);
418
419 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
420 cancelEvent.SetEventObject( this );
421 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
422
423 closing.DeleteObject(this);
424 }
425
426 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
427 {
428 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
429 Refresh();
430 }