]>
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 |
f6bcfd97 | 30 | #include "wx/button.h" |
e37feda2 | 31 | #include "wx/dcclient.h" |
9f3a38fc | 32 | #include "wx/intl.h" |
e37feda2 | 33 | #include "wx/settings.h" |
9f3a38fc | 34 | #include "wx/stattext.h" |
92afa2b1 | 35 | #include "wx/sizer.h" |
7d9f12f3 | 36 | #include "wx/containr.h" |
e37feda2 VZ |
37 | #endif |
38 | ||
897b24cf WS |
39 | #include "wx/statline.h" |
40 | #include "wx/sysopt.h" | |
39bc0347 | 41 | #include "wx/private/stattext.h" |
897b24cf | 42 | |
5d1b4919 VZ |
43 | |
44 | // ---------------------------------------------------------------------------- | |
92afa2b1 | 45 | // wxDialogBase |
5d1b4919 | 46 | // ---------------------------------------------------------------------------- |
e37feda2 | 47 | |
7d9f12f3 | 48 | BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) |
a9f620da | 49 | EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton) |
2158f4d7 VZ |
50 | |
51 | EVT_CLOSE(wxDialogBase::OnCloseWindow) | |
52 | ||
0be27418 | 53 | EVT_CHAR_HOOK(wxDialogBase::OnCharHook) |
2158f4d7 | 54 | |
7d9f12f3 VS |
55 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase) |
56 | END_EVENT_TABLE() | |
57 | ||
6c20e8f8 | 58 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase, wxTopLevelWindow) |
7d9f12f3 VS |
59 | |
60 | void wxDialogBase::Init() | |
61 | { | |
62 | m_returnCode = 0; | |
9ceeecb9 | 63 | m_affirmativeId = wxID_OK; |
c6ece595 VZ |
64 | m_escapeId = wxID_ANY; |
65 | ||
e4b713a2 VZ |
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 | ||
de160b06 | 71 | WX_INIT_CONTROL_CONTAINER(); |
7d9f12f3 VS |
72 | } |
73 | ||
5d1b4919 | 74 | #if wxUSE_STATTEXT |
1e6feb95 | 75 | |
cfd1ac21 MW |
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 | ||
5d1b4919 VZ |
114 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message) |
115 | { | |
2b5f62a0 VZ |
116 | // I admit that this is complete bogus, but it makes |
117 | // message boxes work for pda screens temporarily.. | |
5d1b4919 VZ |
118 | int widthMax = -1; |
119 | const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; | |
2b5f62a0 VZ |
120 | if (is_pda) |
121 | { | |
5d1b4919 | 122 | widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25; |
2b5f62a0 | 123 | } |
68379eaf | 124 | |
5d1b4919 VZ |
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("&&")); | |
68379eaf | 130 | |
cfd1ac21 | 131 | wxTextSizerWrapper wrapper(this); |
b730516c | 132 | |
cfd1ac21 MW |
133 | return wrapper.CreateSizer(text, widthMax); |
134 | } | |
5d1b4919 | 135 | |
5d1b4919 | 136 | #endif // wxUSE_STATTEXT |
1e6feb95 | 137 | |
25eb10d2 | 138 | wxSizer *wxDialogBase::CreateButtonSizer(long flags) |
e37feda2 | 139 | { |
25eb10d2 | 140 | wxSizer *sizer = NULL; |
897b24cf | 141 | |
25eb10d2 | 142 | #ifdef __SMARTPHONE__ |
102c0454 | 143 | wxDialog* dialog = (wxDialog*) this; |
25eb10d2 | 144 | if ( flags & wxOK ) |
102c0454 | 145 | dialog->SetLeftMenu(wxID_OK); |
102c0454 | 146 | |
25eb10d2 | 147 | if ( flags & wxCANCEL ) |
102c0454 | 148 | dialog->SetRightMenu(wxID_CANCEL); |
102c0454 | 149 | |
25eb10d2 | 150 | if ( flags & wxYES ) |
102c0454 | 151 | dialog->SetLeftMenu(wxID_YES); |
897b24cf | 152 | |
25eb10d2 VZ |
153 | if ( flags & wxNO ) |
154 | dialog->SetRightMenu(wxID_NO); | |
897b24cf WS |
155 | #else // !__SMARTPHONE__ |
156 | ||
25eb10d2 VZ |
157 | #if wxUSE_BUTTON |
158 | ||
897b24cf | 159 | #ifdef __POCKETPC__ |
25eb10d2 VZ |
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__ | |
897b24cf | 167 | { |
25eb10d2 | 168 | sizer = CreateStdDialogButtonSizer(flags); |
897b24cf | 169 | } |
25eb10d2 | 170 | #endif // wxUSE_BUTTON |
897b24cf | 171 | |
25eb10d2 | 172 | #endif // __SMARTPHONE__/!__SMARTPHONE__ |
897b24cf | 173 | |
25eb10d2 VZ |
174 | return sizer; |
175 | } | |
897b24cf | 176 | |
25eb10d2 VZ |
177 | wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags) |
178 | { | |
179 | wxSizer *sizer = CreateButtonSizer(flags); | |
180 | if ( !sizer ) | |
181 | return NULL; | |
897b24cf | 182 | |
25eb10d2 VZ |
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 | |
897b24cf | 192 | |
897b24cf | 193 | return sizer; |
acf2ac37 | 194 | } |
68379eaf | 195 | |
897b24cf WS |
196 | #if wxUSE_BUTTON |
197 | ||
acf2ac37 RR |
198 | wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) |
199 | { | |
200 | wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer(); | |
897b24cf | 201 | |
acf2ac37 | 202 | wxButton *ok = NULL; |
acf2ac37 RR |
203 | wxButton *yes = NULL; |
204 | wxButton *no = NULL; | |
52069700 | 205 | |
25eb10d2 VZ |
206 | if (flags & wxOK) |
207 | { | |
331c1816 | 208 | ok = new wxButton(this, wxID_OK); |
52069700 | 209 | sizer->AddButton(ok); |
2b5f62a0 | 210 | } |
52069700 | 211 | |
25eb10d2 VZ |
212 | if (flags & wxCANCEL) |
213 | { | |
331c1816 | 214 | wxButton *cancel = new wxButton(this, wxID_CANCEL); |
52069700 | 215 | sizer->AddButton(cancel); |
b5b49e42 | 216 | } |
52069700 | 217 | |
25eb10d2 VZ |
218 | if (flags & wxYES) |
219 | { | |
331c1816 | 220 | yes = new wxButton(this, wxID_YES); |
52069700 | 221 | sizer->AddButton(yes); |
e37feda2 | 222 | } |
52069700 | 223 | |
25eb10d2 VZ |
224 | if (flags & wxNO) |
225 | { | |
331c1816 | 226 | no = new wxButton(this, wxID_NO); |
52069700 | 227 | sizer->AddButton(no); |
e37feda2 | 228 | } |
52069700 | 229 | |
57d7f988 VZ |
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 | ||
25eb10d2 VZ |
242 | if (flags & wxHELP) |
243 | { | |
331c1816 | 244 | wxButton *help = new wxButton(this, wxID_HELP); |
52069700 | 245 | sizer->AddButton(help); |
e37feda2 | 246 | } |
52069700 | 247 | |
b730516c RD |
248 | if (flags & wxNO_DEFAULT) |
249 | { | |
250 | if (no) | |
251 | { | |
252 | no->SetDefault(); | |
253 | no->SetFocus(); | |
254 | } | |
255 | } | |
256 | else | |
92afa2b1 RR |
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 | } | |
d30814cd | 269 | |
9ceeecb9 JS |
270 | if (flags & wxOK) |
271 | SetAffirmativeId(wxID_OK); | |
272 | else if (flags & wxYES) | |
273 | SetAffirmativeId(wxID_YES); | |
b730516c | 274 | |
d30814cd MB |
275 | sizer->Realize(); |
276 | ||
acf2ac37 | 277 | return sizer; |
e37feda2 VZ |
278 | } |
279 | ||
1e6feb95 | 280 | #endif // wxUSE_BUTTON |
0be27418 | 281 | |
551f281b | 282 | // ---------------------------------------------------------------------------- |
a9f620da | 283 | // standard buttons handling |
551f281b VZ |
284 | // ---------------------------------------------------------------------------- |
285 | ||
a9f620da VZ |
286 | void wxDialogBase::EndDialog(int rc) |
287 | { | |
288 | if ( IsModal() ) | |
289 | EndModal(rc); | |
290 | else | |
291 | Hide(); | |
292 | } | |
293 | ||
551f281b VZ |
294 | void wxDialogBase::AcceptAndClose() |
295 | { | |
296 | if ( Validate() && TransferDataFromWindow() ) | |
297 | { | |
a9f620da | 298 | EndDialog(m_affirmativeId); |
551f281b VZ |
299 | } |
300 | } | |
301 | ||
302 | void wxDialogBase::SetAffirmativeId(int affirmativeId) | |
303 | { | |
fabd7a7f | 304 | m_affirmativeId = affirmativeId; |
551f281b VZ |
305 | } |
306 | ||
307 | void wxDialogBase::SetEscapeId(int escapeId) | |
308 | { | |
fabd7a7f | 309 | m_escapeId = escapeId; |
551f281b VZ |
310 | } |
311 | ||
0be27418 VZ |
312 | bool wxDialogBase::EmulateButtonClickIfPresent(int id) |
313 | { | |
dbfa7f33 | 314 | #if wxUSE_BUTTON |
0be27418 VZ |
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; | |
dbfa7f33 VS |
325 | #else // !wxUSE_BUTTON |
326 | wxUnusedVar(id); | |
327 | return false; | |
328 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
0be27418 VZ |
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; | |
153a0b78 | 354 | idCancel = GetAffirmativeId(); |
0be27418 VZ |
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 | ||
a9f620da | 367 | void wxDialogBase::OnButton(wxCommandEvent& event) |
2158f4d7 | 368 | { |
a9f620da VZ |
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(); | |
2158f4d7 | 378 | |
a9f620da VZ |
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 | } | |
2158f4d7 VZ |
390 | } |
391 | ||
a9f620da VZ |
392 | // ---------------------------------------------------------------------------- |
393 | // other event handlers | |
394 | // ---------------------------------------------------------------------------- | |
2158f4d7 VZ |
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 | } |