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